|
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
if($type == 'head') |
|
316
|
|
|
{ |
|
317
|
|
|
$map = &$this->jsHeadMap; |
|
318
|
|
|
$mapIndex = &$this->jsHeadMapIndex; |
|
319
|
|
|
} |
|
320
|
|
|
else |
|
321
|
|
|
{ |
|
322
|
|
|
$map = &$this->jsBodyMap; |
|
323
|
|
|
$mapIndex = &$this->jsBodyMapIndex; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
$this->_sortMap($map, $mapIndex); |
|
327
|
|
|
|
|
328
|
|
|
$result = array(); |
|
329
|
|
View Code Duplication |
foreach($map as $indexedMap) |
|
330
|
|
|
{ |
|
331
|
|
|
foreach($indexedMap as $file) |
|
332
|
|
|
{ |
|
333
|
|
|
$query = ''; |
|
334
|
|
|
if(!$file->external && is_readable($file->cdnPath . '/' . $file->fileName)) |
|
335
|
|
|
{ |
|
336
|
|
|
$query = date('YmdHis', filemtime($file->cdnPath . '/' . $file->fileName)); |
|
337
|
|
|
} |
|
338
|
|
|
if($file->query) |
|
339
|
|
|
{ |
|
340
|
|
|
if($query) $query .= '&'; |
|
341
|
|
|
$query .= $file->query; |
|
342
|
|
|
} |
|
343
|
|
|
$query = ($query) ? '?' . $query : ''; |
|
344
|
|
|
|
|
345
|
|
|
$fullFilePath = $file->filePath . '/' . $file->fileName . $query; |
|
346
|
|
|
$result[] = array( |
|
347
|
|
|
'file' => $fullFilePath, |
|
348
|
|
|
'targetie' => $file->targetIe |
|
349
|
|
|
); |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
return $result; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* Sort a map |
|
358
|
|
|
* |
|
359
|
|
|
* @param array $map Array to sort |
|
360
|
|
|
* @param array $index Not used |
|
361
|
|
|
* @return void |
|
362
|
|
|
*/ |
|
363
|
|
|
function _sortMap(&$map, &$index) |
|
|
|
|
|
|
364
|
|
|
{ |
|
365
|
|
|
ksort($map); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Normalize File path |
|
370
|
|
|
* |
|
371
|
|
|
* @param string $path Path to normalize |
|
372
|
|
|
* @return string Normalized path |
|
373
|
|
|
*/ |
|
374
|
|
|
function _normalizeFilePath($path) |
|
375
|
|
|
{ |
|
376
|
|
|
if(strpos($path, '://') === FALSE && $path{0} != '/' && $path{0} != '.') |
|
377
|
|
|
{ |
|
378
|
|
|
$path = './' . $path; |
|
379
|
|
|
} |
|
380
|
|
|
elseif(!strncmp($path, '//', 2)) |
|
381
|
|
|
{ |
|
382
|
|
|
return preg_replace('#^//+#', '//', $path); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
$path = preg_replace('@/\./|(?<!:)\/\/@', '/', $path); |
|
386
|
|
|
|
|
387
|
|
|
while(strpos($path, '/../')) |
|
388
|
|
|
{ |
|
389
|
|
|
$path = preg_replace('/\/([^\/]+)\/\.\.\//s', '/', $path, 1); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
return $path; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* Get absolute file url |
|
397
|
|
|
* |
|
398
|
|
|
* @param string $path Path to get absolute url |
|
399
|
|
|
* @return string Absolute url |
|
400
|
|
|
*/ |
|
401
|
|
|
function _getAbsFileUrl($path) |
|
402
|
|
|
{ |
|
403
|
|
|
$path = $this->_normalizeFilePath($path); |
|
404
|
|
|
$script_path = getScriptPath(); |
|
405
|
|
|
|
|
406
|
|
|
if(strpos($path, './') === 0) |
|
407
|
|
|
{ |
|
408
|
|
|
if($script_path == '/' || $script_path == '\\') |
|
409
|
|
|
{ |
|
410
|
|
|
$path = '/' . substr($path, 2); |
|
411
|
|
|
} |
|
412
|
|
|
else |
|
413
|
|
|
{ |
|
414
|
|
|
$path = $script_path . substr($path, 2); |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
else if(strpos($file, '../') === 0) |
|
|
|
|
|
|
418
|
|
|
{ |
|
419
|
|
|
$path = $this->_normalizeFilePath($script_path . $path); |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
return $path; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* Arrage css index |
|
427
|
|
|
* |
|
428
|
|
|
* @param string $dirName First directory name of css path |
|
429
|
|
|
* @param array $file file info. |
|
430
|
|
|
* @return void |
|
431
|
|
|
*/ |
|
432
|
|
|
function _arrangeCssIndex($dirName, &$file) |
|
433
|
|
|
{ |
|
434
|
|
|
if($file->index !== 0) |
|
435
|
|
|
{ |
|
436
|
|
|
return; |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
$dirName = str_replace('./', '', $dirName); |
|
440
|
|
|
$tmp = explode('/', $dirName); |
|
441
|
|
|
|
|
442
|
|
|
$cssSortList = array('common' => -100000, 'layouts' => -90000, 'modules' => -80000, 'widgets' => -70000, 'addons' => -60000); |
|
443
|
|
|
$file->index = $cssSortList[$tmp[0]]; |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
} |
|
447
|
|
|
/* End of file FrontEndFileHandler.class.php */ |
|
448
|
|
|
/* Location: ./classes/frontendfile/FrontEndFileHandler.class.php */ |
|
449
|
|
|
|
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.