1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) NAVER <http://www.navercorp.com> */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Handle front end files |
6
|
|
|
* @author NAVER ([email protected]) |
7
|
|
|
* */ |
8
|
|
|
class FrontEndFileHandler extends Handler |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
static $isSSL = null; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Map for css |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
var $cssMap = array(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Map for Javascript at head |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
var $jsHeadMap = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Map for Javascript at body |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
var $jsBodyMap = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Index for css |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
var $cssMapIndex = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Index for javascript at head |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
var $jsHeadMapIndex = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Index for javascript at body |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
var $jsBodyMapIndex = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check SSL |
51
|
|
|
* |
52
|
|
|
* @return bool If using ssl returns true, otherwise returns false. |
53
|
|
|
* @deprecated |
54
|
|
|
*/ |
55
|
|
|
function isSsl() |
56
|
|
|
{ |
57
|
|
|
if(!is_null(self::$isSSL)) |
58
|
|
|
{ |
59
|
|
|
return self::$isSSL; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$url_info = parse_url(Context::getRequestUrl()); |
63
|
|
|
self::$isSSL = ($url_info['scheme'] == 'https'); |
64
|
|
|
|
65
|
|
|
return self::$isSSL; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Load front end file |
70
|
|
|
* |
71
|
|
|
* The $args is use as below. File type(js, css) is detected by file extension. |
72
|
|
|
* |
73
|
|
|
* <pre> |
74
|
|
|
* case js |
75
|
|
|
* $args[0]: file name |
76
|
|
|
* $args[1]: type (head | body) |
77
|
|
|
* $args[2]: target IE |
78
|
|
|
* $args[3]: index |
79
|
|
|
* case css |
80
|
|
|
* $args[0]: file name |
81
|
|
|
* $args[1]: media |
82
|
|
|
* $args[2]: target IE |
83
|
|
|
* $args[3]: index |
84
|
|
|
* </pre> |
85
|
|
|
* |
86
|
|
|
* @param array $args Arguments |
87
|
|
|
* @return void |
88
|
|
|
* */ |
89
|
|
|
function loadFile($args) |
90
|
|
|
{ |
91
|
|
|
if(!is_array($args)) |
92
|
|
|
{ |
93
|
|
|
$args = array($args); |
94
|
|
|
} |
95
|
|
|
$file = $this->getFileInfo($args[0], $args[2], $args[1]); |
96
|
|
|
|
97
|
|
|
$availableExtension = array('css' => 1, 'js' => 1); |
98
|
|
|
if(!isset($availableExtension[$file->fileExtension])) |
99
|
|
|
{ |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$file->index = (int) $args[3]; |
104
|
|
|
|
105
|
|
|
if($file->fileExtension == 'css') |
106
|
|
|
{ |
107
|
|
|
$map = &$this->cssMap; |
108
|
|
|
$mapIndex = &$this->cssMapIndex; |
109
|
|
|
|
110
|
|
|
$this->_arrangeCssIndex($pathInfo['dirname'], $file); |
|
|
|
|
111
|
|
|
} |
112
|
|
View Code Duplication |
else if($file->fileExtension == 'js') |
113
|
|
|
{ |
114
|
|
|
if($args[1] == 'body') |
115
|
|
|
{ |
116
|
|
|
$map = &$this->jsBodyMap; |
117
|
|
|
$mapIndex = &$this->jsBodyMapIndex; |
118
|
|
|
} |
119
|
|
|
else |
120
|
|
|
{ |
121
|
|
|
$map = &$this->jsHeadMap; |
122
|
|
|
$mapIndex = &$this->jsHeadMapIndex; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
(is_null($file->index)) ? $file->index = 0 : $file->index = $file->index; |
127
|
|
|
if(!isset($mapIndex[$file->key]) || $mapIndex[$file->key] > $file->index) |
128
|
|
|
{ |
129
|
|
|
$this->unloadFile($args[0], $args[2], $args[1]); |
130
|
|
|
$map[$file->index][$file->key] = $file; |
|
|
|
|
131
|
|
|
$mapIndex[$file->key] = $file->index; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get file information |
137
|
|
|
* |
138
|
|
|
* @param string $fileName The file name |
139
|
|
|
* @param string $targetIe Target IE of file |
140
|
|
|
* @param string $media Media of file |
141
|
|
|
* @return stdClass The file information |
142
|
|
|
*/ |
143
|
|
|
private function getFileInfo($fileName, $targetIe = '', $media = 'all') |
144
|
|
|
{ |
145
|
|
|
static $existsInfo = array(); |
146
|
|
|
|
147
|
|
|
if(isset($existsInfo[$existsKey])) |
148
|
|
|
{ |
149
|
|
|
return $existsInfo[$existsKey]; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$fileName = preg_replace('/(?:[\/]{3,})(.*)/', '//$1', $fileName); |
153
|
|
|
$url_info = parse_url($fileName); |
154
|
|
|
$pathInfo = pathinfo(str_replace('?' . $url_info['query'], '', $fileName)); |
155
|
|
|
|
156
|
|
|
$file = new stdClass(); |
157
|
|
|
$file->fileName = basename($url_info['path']); |
158
|
|
|
$file->filePath = $this->_getAbsFileUrl($pathInfo['dirname']); |
159
|
|
|
$file->fileRealPath = FileHandler::getRealPath($pathInfo['dirname']); |
160
|
|
|
$file->fileExtension = strtolower($pathInfo['extension']); |
161
|
|
|
$file->fileNameNoExt = preg_replace('/\.min$/', '', $pathInfo['filename']); |
162
|
|
|
$file->query = $url_info['query']; |
163
|
|
|
$file->external = !!$url_info['host']; |
164
|
|
|
$file->keyName = implode('.', array($file->fileNameNoExt, $file->fileExtension)); |
165
|
|
|
$file->cdnPath = $this->_normalizeFilePath($pathInfo['dirname']); |
166
|
|
|
|
167
|
|
|
if(!$file->external) |
168
|
|
|
{ |
169
|
|
|
if(!__DEBUG__ && __XE_VERSION_STABLE__) |
170
|
|
|
{ |
171
|
|
|
// if no debug mode, load minifed file |
172
|
|
|
$minifiedFileName = implode('.', array($file->fileNameNoExt, 'min', $file->fileExtension)); |
173
|
|
|
$minifiedRealPath = implode('/', array($file->fileRealPath, $minifiedFileName)); |
174
|
|
|
if(file_exists($minifiedRealPath)) |
175
|
|
|
{ |
176
|
|
|
$file->fileName = $minifiedFileName; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
else |
180
|
|
|
{ |
181
|
|
|
// Remove .min |
182
|
|
|
if(file_exists(implode('/', array($file->fileRealPath, $file->keyName)))) |
183
|
|
|
{ |
184
|
|
|
$file->fileName = $file->keyName; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$file->targetIe = $targetIe; |
190
|
|
|
|
191
|
|
|
if($file->fileExtension == 'css') |
192
|
|
|
{ |
193
|
|
|
$file->media = $media; |
194
|
|
|
if(!$file->media) |
195
|
|
|
{ |
196
|
|
|
$file->media = 'all'; |
197
|
|
|
} |
198
|
|
|
$file->key = $file->filePath . $file->keyName . "\t" . $file->targetIe . "\t" . $file->media; |
199
|
|
|
} |
200
|
|
|
else if($file->fileExtension == 'js') |
201
|
|
|
{ |
202
|
|
|
$file->key = $file->filePath . $file->keyName . "\t" . $file->targetIe; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $file; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Unload front end file |
210
|
|
|
* |
211
|
|
|
* @param string $fileName The file name to unload |
212
|
|
|
* @param string $targetIe Target IE of file to unload |
213
|
|
|
* @param string $media Media of file to unload. Only use when file is css. |
214
|
|
|
* @return void |
215
|
|
|
*/ |
216
|
|
|
function unloadFile($fileName, $targetIe = '', $media = 'all') |
217
|
|
|
{ |
218
|
|
|
$file = $this->getFileInfo($fileName, $targetIe, $media); |
219
|
|
|
|
220
|
|
|
if($file->fileExtension == 'css') |
221
|
|
|
{ |
222
|
|
|
if(isset($this->cssMapIndex[$file->key])) |
223
|
|
|
{ |
224
|
|
|
$index = $this->cssMapIndex[$file->key]; |
225
|
|
|
unset($this->cssMap[$index][$file->key], $this->cssMapIndex[$file->key]); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
else |
229
|
|
|
{ |
230
|
|
|
if(isset($this->jsHeadMapIndex[$file->key])) |
231
|
|
|
{ |
232
|
|
|
$index = $this->jsHeadMapIndex[$file->key]; |
233
|
|
|
unset($this->jsHeadMap[$index][$file->key], $this->jsHeadMapIndex[$file->key]); |
234
|
|
|
} |
235
|
|
|
if(isset($this->jsBodyMapIndex[$file->key])) |
236
|
|
|
{ |
237
|
|
|
$index = $this->jsBodyMapIndex[$file->key]; |
238
|
|
|
unset($this->jsBodyMap[$index][$file->key], $this->jsBodyMapIndex[$file->key]); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Unload all front end file |
245
|
|
|
* |
246
|
|
|
* @param string $type Type to unload. all, css, js |
247
|
|
|
* @return void |
248
|
|
|
*/ |
249
|
|
|
function unloadAllFiles($type = 'all') |
250
|
|
|
{ |
251
|
|
|
if($type == 'css' || $type == 'all') |
252
|
|
|
{ |
253
|
|
|
$this->cssMap = array(); |
254
|
|
|
$this->cssMapIndex = array(); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if($type == 'js' || $type == 'all') |
258
|
|
|
{ |
259
|
|
|
$this->jsHeadMap = array(); |
260
|
|
|
$this->jsBodyMap = array(); |
261
|
|
|
$this->jsHeadMapIndex = array(); |
262
|
|
|
$this->jsBodyMapIndex = array(); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get css file list |
268
|
|
|
* |
269
|
|
|
* @return array Returns css file list. Array contains file, media, targetie. |
270
|
|
|
*/ |
271
|
|
|
function getCssFileList() |
272
|
|
|
{ |
273
|
|
|
$map = &$this->cssMap; |
274
|
|
|
$mapIndex = &$this->cssMapIndex; |
275
|
|
|
|
276
|
|
|
$this->_sortMap($map, $mapIndex); |
277
|
|
|
|
278
|
|
|
$result = array(); |
279
|
|
|
foreach($map as $indexedMap) |
280
|
|
|
{ |
281
|
|
|
foreach($indexedMap as $file) |
282
|
|
|
{ |
283
|
|
|
$query = ''; |
284
|
|
|
if(!$file->external && is_readable($file->cdnPath . '/' . $file->fileName)) |
285
|
|
|
{ |
286
|
|
|
$query = date('YmdHis', filemtime($file->cdnPath . '/' . $file->fileName)); |
287
|
|
|
} |
288
|
|
|
if($file->query) |
289
|
|
|
{ |
290
|
|
|
if($query) $query .= '&'; |
291
|
|
|
$query .= $file->query; |
292
|
|
|
} |
293
|
|
|
$query = ($query) ? '?' . $query : ''; |
294
|
|
|
|
295
|
|
|
$fullFilePath = $file->filePath . '/' . $file->fileName . $query; |
296
|
|
|
$result[] = array( |
297
|
|
|
'file' => $fullFilePath, |
298
|
|
|
'media' => $file->media, |
299
|
|
|
'targetie' => $file->targetIe |
300
|
|
|
); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return $result; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Get javascript file list |
309
|
|
|
* |
310
|
|
|
* @param string $type Type of javascript. head, body |
311
|
|
|
* @return array Returns javascript file list. Array contains file, targetie. |
312
|
|
|
*/ |
313
|
|
|
function getJsFileList($type = 'head') |
314
|
|
|
{ |
315
|
|
|
|
316
|
|
|
$ignore = array('modernizr.js', 'common.js', 'js_app.js', 'xml2json.js', 'xml_handler.js', 'xml_js_filter.js'); |
317
|
|
|
|
318
|
|
View Code Duplication |
if($type == 'head') |
319
|
|
|
{ |
320
|
|
|
$map = &$this->jsHeadMap; |
321
|
|
|
$mapIndex = &$this->jsHeadMapIndex; |
322
|
|
|
} |
323
|
|
|
else |
324
|
|
|
{ |
325
|
|
|
$map = &$this->jsBodyMap; |
326
|
|
|
$mapIndex = &$this->jsBodyMapIndex; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$this->_sortMap($map, $mapIndex); |
330
|
|
|
|
331
|
|
|
$result = array(); |
332
|
|
|
foreach($map as $indexedMap) |
333
|
|
|
{ |
334
|
|
|
foreach($indexedMap as $file) |
335
|
|
|
{ |
336
|
|
|
if((!__DEBUG__ && __XE_VERSION_STABLE__) && $file->filePath === '/common/js') |
337
|
|
|
{ |
338
|
|
|
if(in_array($file->fileName, $ignore)) |
339
|
|
|
{ |
340
|
|
|
continue; |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
$query = ''; |
345
|
|
|
if(!$file->external && is_readable($file->cdnPath . '/' . $file->fileName)) |
346
|
|
|
{ |
347
|
|
|
$query = date('YmdHis', filemtime($file->cdnPath . '/' . $file->fileName)); |
348
|
|
|
} |
349
|
|
|
if($file->query) |
350
|
|
|
{ |
351
|
|
|
if($query) $query .= '&'; |
352
|
|
|
$query .= $file->query; |
353
|
|
|
} |
354
|
|
|
$query = ($query) ? '?' . $query : ''; |
355
|
|
|
|
356
|
|
|
$fullFilePath = $file->filePath . '/' . $file->fileName . $query; |
357
|
|
|
$result[] = array( |
358
|
|
|
'file' => $fullFilePath, |
359
|
|
|
'targetie' => $file->targetIe |
360
|
|
|
); |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
return $result; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Sort a map |
369
|
|
|
* |
370
|
|
|
* @param array $map Array to sort |
371
|
|
|
* @param array $index Not used |
372
|
|
|
* @return void |
373
|
|
|
*/ |
374
|
|
|
function _sortMap(&$map, &$index) |
|
|
|
|
375
|
|
|
{ |
376
|
|
|
ksort($map); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Normalize File path |
381
|
|
|
* |
382
|
|
|
* @param string $path Path to normalize |
383
|
|
|
* @return string Normalized path |
384
|
|
|
*/ |
385
|
|
|
function _normalizeFilePath($path) |
386
|
|
|
{ |
387
|
|
|
if(strpos($path, '://') === FALSE && $path{0} != '/' && $path{0} != '.') |
388
|
|
|
{ |
389
|
|
|
$path = './' . $path; |
390
|
|
|
} |
391
|
|
|
elseif(!strncmp($path, '//', 2)) |
392
|
|
|
{ |
393
|
|
|
return preg_replace('#^//+#', '//', $path); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
$path = preg_replace('@/\./|(?<!:)\/\/@', '/', $path); |
397
|
|
|
|
398
|
|
|
while(strpos($path, '/../')) |
399
|
|
|
{ |
400
|
|
|
$path = preg_replace('/\/([^\/]+)\/\.\.\//s', '/', $path, 1); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
return $path; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Get absolute file url |
408
|
|
|
* |
409
|
|
|
* @param string $path Path to get absolute url |
410
|
|
|
* @return string Absolute url |
411
|
|
|
*/ |
412
|
|
|
function _getAbsFileUrl($path) |
413
|
|
|
{ |
414
|
|
|
$path = $this->_normalizeFilePath($path); |
415
|
|
|
$script_path = getScriptPath(); |
416
|
|
|
|
417
|
|
|
if(strpos($path, './') === 0) |
418
|
|
|
{ |
419
|
|
|
if($script_path == '/' || $script_path == '\\') |
420
|
|
|
{ |
421
|
|
|
$path = '/' . substr($path, 2); |
422
|
|
|
} |
423
|
|
|
else |
424
|
|
|
{ |
425
|
|
|
$path = $script_path . substr($path, 2); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
else if(strpos($file, '../') === 0) |
|
|
|
|
429
|
|
|
{ |
430
|
|
|
$path = $this->_normalizeFilePath($script_path . $path); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
return $path; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Arrage css index |
438
|
|
|
* |
439
|
|
|
* @param string $dirName First directory name of css path |
440
|
|
|
* @param array $file file info. |
441
|
|
|
* @return void |
442
|
|
|
*/ |
443
|
|
|
function _arrangeCssIndex($dirName, &$file) |
444
|
|
|
{ |
445
|
|
|
if($file->index !== 0) |
446
|
|
|
{ |
447
|
|
|
return; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
$dirName = str_replace('./', '', $dirName); |
451
|
|
|
$tmp = explode('/', $dirName); |
452
|
|
|
|
453
|
|
|
$cssSortList = array('common' => -100000, 'layouts' => -90000, 'modules' => -80000, 'widgets' => -70000, 'addons' => -60000); |
454
|
|
|
$file->index = $cssSortList[$tmp[0]]; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
} |
458
|
|
|
/* End of file FrontEndFileHandler.class.php */ |
459
|
|
|
/* Location: ./classes/frontendfile/FrontEndFileHandler.class.php */ |
460
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.