-
Notifications
You must be signed in to change notification settings - Fork 0
/
MlurlPlugin.inc.php
executable file
·294 lines (263 loc) · 9.79 KB
/
MlurlPlugin.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
import("lib.pkp.classes.plugins.GenericPlugin");
class MlurlPlugin extends GenericPlugin {
function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
if ($success && $this->getEnabled($mainContextId)) {
// static page integration
// needle to load first
if(class_exists('StaticPagesPlugin')) {
import("plugins.generic.staticPages.StaticPagesPlugin");
$staticPages = new StaticPagesPlugin();
if($staticPages->getEnabled(null)) {
HookRegistry::register('LoadHandler', array(&$this, 'handleStaticPages'));
}
}
// router
HookRegistry::register('LoadHandler', array($this, 'handleLocales'));
// filters
HookRegistry::register('TemplateManager::display', array($this, 'handleTemplateDisplay'));
// redirect
HookRegistry::register('Request::redirect', array($this, 'redirectLocaleUrl'));
}
$this->_registerTemplateResource();
return $success;
}
function getDisplayName() {
return __('plugins.generic.mlurl.displayName');
}
function getDescription() {
return __('plugins.generic.mlurl.description');
}
function getHandlerPath() {
return $this->getPluginPath() . DIRECTORY_SEPARATOR . 'pages';
}
function isStaticPage($page) {
$staticPagesDao = DAORegistry::getDAO('StaticPagesDAO');
if(isset($staticPagesDao)) {
$request = Application::getRequest();
$context = $request->getContext();
$staticPage = $staticPagesDao->getByPath(
$context?$context->getId():CONTEXT_ID_NONE,
$page
);
return $staticPage;
} else {
return false;
}
return false;
}
function handleStaticPages($hookName, $args) {
$page =& $args[0];
$op =& $args[1];
$path =& $args[2];
// get static page by path
$staticPage = $this->isStaticPage($op);
if($staticPage) {
$page = $op;
$op = 'index';
define('HANDLER_CLASS', 'MlurlStaticPagesHandler');
$this->import("pages.static.StaticPagesHandler");
StaticPagesHandler::setPage($staticPage);
return true;
}
return false;
}
function handleTemplateDisplay($hookName, $args) {
$templateMgr =& $args[0];
$template =& $args[1];
$request = Application::getRequest();
$templateMgr->registerFilter("output", array($this, 'languageUrlFilter'));
return false;
}
function handleLocales($hookName, $args) {
$page =& $args[0];
$op =& $args[1];
$path =& $args[2];
$urlLocales = self::convertLocalesToUrl(AppLocale::getSupportedLocales());
$pageLocale = self::convertLocale($page,true);
if($this->getEnabled()) {
$request = Application::getRequest();
$session = $request->getSession();
if(in_array($page, $urlLocales)) {
// flip page and op
$url_parts = array_filter(explode("/", parse_url($request->getCompleteUrl())['path']));
// delete index php in url part
if(in_array('index.php', $url_parts)) {
$key = array_search('index.php', $url_parts);
unset($url_parts[$key]);
}
$url_parts = array_values($url_parts);
// set page and op by url parts
if(isset($url_parts[2])) {
$page = $url_parts[2];
}
if(isset($url_parts[3])) {
$op = $url_parts[3];
} else {
$op = 'index';
}
// some pages can have other path
$page = $this->pageFilter($page);
// rewrite locale in session
$currentLocale = $session->getSessionVar('currentLocale');
if($currentLocale != $pageLocale) {
$session->setSessionVar('currentLocale', $pageLocale);
$request->redirectUrl($request->getCompleteUrl());
}
switch($page) {
case 'article':
case 'download':
require_once(__DIR__ . "/pages/article/index.php");
break;
case 'issue': require_once(__DIR__ . "/pages/issue/index.php");
break;
default: if(is_file("pages/{$page}/index.php")) {
require_once("pages/{$page}/index.php");
} else {
if(is_file("pages/{$op}/index.php")) {
require_once("pages/{$op}/index.php");
} else {
return false;
}
}
break;
}
return true;
} else {
if(!$page && $op == 'index') {
$currentLocale = self::convertLocale($session->getSessionVar('currentLocale'));
if(!$currentLocale) $currentLocale = self::convertLocale(AppLocale::getPrimaryLocale());
$request->redirectUrl(trim($request->getCompleteUrl(), "/") . "/" . $currentLocale);
}
}
}
return false;
}
function languageUrlFilter($output, $templateMgr) {
require_once("phpQuery.php");
$request = Application::getRequest();
$journalPath = $request->getRequestedJournalPath();
$currentUrl = $request->getCompleteUrl();
$baseUrl = $request->getIndexUrl() . "/" . $journalPath;
$indexUrl = $request->getIndexUrl() . "/index";
$session = $request->getSession();
$currentLocale = self::convertLocale($session->getSessionVar('currentLocale'));
if(!$currentLocale) $currentLocale = self::convertLocale(AppLocale::getPrimaryLocale());
$document = phpQuery::newDocument($output);
$lang_links = $document->find("a");
foreach($lang_links as $link) {
$pqLink = pq($link);
$href = $pqLink->attr('href');
if(strpos($href, '/' . $currentLocale . '/') === false
&& strpos($href, 'setLocale') === false
&& strpos($href, $baseUrl . '/admin') === false
&& strpos($href, $baseUrl . '/submission') === false
&& strpos($href, $baseUrl . '/submissions') === false
&& strpos($href, $baseUrl . '/login') === false
&& strpos($href, $baseUrl . '/user') === false
&& strpos($href, $baseUrl . '/management') === false
&& strpos($href, $baseUrl . '/stats') === false
&& strpos($href, $baseUrl . '/manageIssues') === false
&& strpos($href, $baseUrl . '/$$$call$$$') === false
) {
$pqLink->attr('href', trim(preg_replace('/\s+/', ' ', str_replace(array($baseUrl . "/", $indexUrl), $baseUrl . "/" . $currentLocale . '/', $href))));
} else {
// issue fix
if(strpos($currentUrl, 'issue') !== false && strpos($href, 'issue') === false && strpos($href, 'view') !== false) {
$pqLink->attr('href', str_replace($baseUrl . "/" . $currentLocale, $baseUrl . "/" . $currentLocale . "/issue", $href));
}
}
// home link fix
continue;
}
return $document;
}
function redirectLocaleUrl($hookName, $args) {
$url =& $args[0];
$request = Application::getRequest();
$path = parse_url($request->getCompleteUrl())['path'];
// locale variable in session
$session = $request->getSession();
$currentLocale = $session->getSessionVar('currentLocale');
$base = basename($path);
$localePartUrl = self::convertLocale($base);
// language switching
if(strpos($path, 'setLocale') !== false && $_GET['source']) {
// delete index php in url part
$index_php = false;
$url_parts = explode("/", trim($_GET['source'],"/"));
if(in_array('index.php', $url_parts)) {
$key = array_search('index.php', $url_parts);
$index_php = true;
unset($url_parts[$key]);
}
$url_parts = array_values($url_parts);
$replacePartUrl = $url_parts[1];
$url_parts[1] = $localePartUrl;
if($index_php) {
$url = $request->getBaseUrl() . "/index.php/" . implode("/", $url_parts);
} else {
$url = $request->getBaseUrl() . "/" . implode("/", $url_parts);
}
// fix
if(substr($url, -1) != '/') {
$url .= '/';
}
}
// download articles
if(strpos($url, 'download') !== false && strpos($path, 'article') !== false) {
$url = str_replace("/download/", "/article/download/", $url);
}
// current issue redirect
if(strpos($path, 'issue/current') !== false) {
$replacePartUrl = explode("/", trim($path,"/"))[2];
$url = str_replace("/issue", "/" . $replacePartUrl . "/issue", $url);
}
return false;
}
static function convertLocale($locale, $return_key = false) {
$language_array = array(
'ca_ES' => 'caes',
'cs_CZ' => 'cz',
'da_DK' => 'dk',
'de_DE' => 'de',
'en_US' => 'en',
'es_ES' => 'es',
'eu_ES' => 'eues',
'fi_FI' => 'fi',
'fr_CA' => 'ca',
'fr_FR' => 'fr',
'hr_HR' => 'hr',
'id_ID' => 'id',
'it_IT' => 'it',
'nb_NO' => 'no',
'nl_NL' => 'nl',
'pl_PL' => 'pl',
'pt_BR' => 'br',
'pt_PT' => 'pt',
'ru_RU' => 'ru',
'sl_SI' => 'sl',
'sr_RS@cyrillic' => 'srcyr',
'sr_RS@latin' => 'srlat',
'sv_SE' => 'se',
'tr_TR' => 'tr',
'uk_UA' => 'ua',
'zh_CN' => 'cn',
'ar_IQ' => 'iq',
'fa_IR' => 'ir',
);
if($return_key) $language_array = array_flip($language_array);
return (isset($language_array[$locale])) ? $language_array[$locale] : false;
}
static function convertLocalesToUrl($locales) {
$urlLocales = array();
foreach($locales as $code=>$locale) {
$urlLocales[] = self::convertLocale($code);
}
return $urlLocales;
}
function pageFilter($page) {
return $page;
}
}