1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle CMS |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2014-2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\Page; |
9
|
|
|
use |
10
|
|
|
cs\App, |
11
|
|
|
cs\Core, |
12
|
|
|
cs\Config, |
13
|
|
|
cs\Event, |
14
|
|
|
cs\Language, |
15
|
|
|
cs\Request, |
16
|
|
|
cs\User, |
17
|
|
|
h, |
18
|
|
|
cs\Page\Includes\RequireJS; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Includes management for `cs\Page` class |
22
|
|
|
* |
23
|
|
|
* @property string $Title |
24
|
|
|
* @property string $Description |
25
|
|
|
* @property string $canonical_url |
26
|
|
|
* @property string $Head |
27
|
|
|
* @property string $post_Body |
28
|
|
|
* @property string $theme |
29
|
|
|
*/ |
30
|
|
|
trait Includes { |
31
|
|
|
use |
32
|
|
|
RequireJS; |
33
|
|
|
/** |
34
|
|
|
* @var array[] |
35
|
|
|
*/ |
36
|
|
|
protected $core_html; |
37
|
|
|
/** |
38
|
|
|
* @var array[] |
39
|
|
|
*/ |
40
|
|
|
protected $core_js; |
41
|
|
|
/** |
42
|
|
|
* @var array[] |
43
|
|
|
*/ |
44
|
|
|
protected $core_css; |
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $core_config; |
49
|
|
|
/** |
50
|
|
|
* @var array[] |
51
|
|
|
*/ |
52
|
|
|
protected $html; |
53
|
|
|
/** |
54
|
|
|
* @var array[] |
55
|
|
|
*/ |
56
|
|
|
protected $js; |
57
|
|
|
/** |
58
|
|
|
* @var array[] |
59
|
|
|
*/ |
60
|
|
|
protected $css; |
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $config; |
65
|
|
|
/** |
66
|
|
|
* Base name is used as prefix when creating CSS/JS/HTML cache files in order to avoid collisions when having several themes and languages |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $pcache_basename; |
70
|
|
|
protected function init_includes () { |
71
|
|
|
$this->core_html = [0 => [], 1 => []]; |
72
|
|
|
$this->core_js = [0 => [], 1 => []]; |
73
|
|
|
$this->core_css = [0 => [], 1 => []]; |
74
|
|
|
$this->core_config = ''; |
75
|
|
|
$this->html = [0 => [], 1 => []]; |
76
|
|
|
$this->js = [0 => [], 1 => []]; |
77
|
|
|
$this->css = [0 => [], 1 => []]; |
78
|
|
|
$this->config = ''; |
79
|
|
|
$this->pcache_basename = ''; |
80
|
|
|
} |
81
|
|
|
/** |
82
|
|
|
* Including of Web Components |
83
|
|
|
* |
84
|
|
|
* @param string|string[] $add Path to including file, or code |
85
|
|
|
* @param string $mode Can be <b>file</b> or <b>code</b> |
86
|
|
|
* |
87
|
|
|
* @return \cs\Page |
88
|
|
|
*/ |
89
|
|
|
function html ($add, $mode = 'file') { |
90
|
|
|
return $this->html_internal($add, $mode); |
91
|
|
|
} |
92
|
|
|
/** |
93
|
|
|
* @param string|string[] $add |
94
|
|
|
* @param string $mode |
95
|
|
|
* @param bool $core |
96
|
|
|
* |
97
|
|
|
* @return \cs\Page |
98
|
|
|
*/ |
99
|
|
|
protected function html_internal ($add, $mode = 'file', $core = false) { |
100
|
|
|
if (!$add) { |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
if (is_array($add)) { |
104
|
|
|
foreach (array_filter($add) as $script) { |
105
|
|
|
$this->html_internal($script, $mode, $core); |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
if ($core) { |
109
|
|
|
$html = &$this->core_html; |
110
|
|
|
} else { |
111
|
|
|
$html = &$this->html; |
112
|
|
|
} |
113
|
|
|
if ($mode == 'file') { |
114
|
|
|
$html[0][] = h::link( |
115
|
|
|
[ |
116
|
|
|
'href' => $add, |
117
|
|
|
'rel' => 'import' |
118
|
|
|
] |
119
|
|
|
); |
120
|
|
|
} elseif ($mode == 'code') { |
121
|
|
|
$html[1][] = "$add\n"; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
/** |
127
|
|
|
* Including of JavaScript |
128
|
|
|
* |
129
|
|
|
* @param string|string[] $add Path to including file, or code |
130
|
|
|
* @param string $mode Can be <b>file</b> or <b>code</b> |
131
|
|
|
* |
132
|
|
|
* @return \cs\Page |
133
|
|
|
*/ |
134
|
|
|
function js ($add, $mode = 'file') { |
135
|
|
|
return $this->js_internal($add, $mode); |
136
|
|
|
} |
137
|
|
|
/** |
138
|
|
|
* @param string|string[] $add |
139
|
|
|
* @param string $mode |
140
|
|
|
* @param bool $core |
141
|
|
|
* |
142
|
|
|
* @return \cs\Page |
143
|
|
|
*/ |
144
|
|
|
protected function js_internal ($add, $mode = 'file', $core = false) { |
145
|
|
|
if (!$add) { |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
if (is_array($add)) { |
149
|
|
|
foreach (array_filter($add) as $script) { |
150
|
|
|
$this->js_internal($script, $mode, $core); |
151
|
|
|
} |
152
|
|
|
} else { |
153
|
|
|
if ($core) { |
154
|
|
|
$js = &$this->core_js; |
155
|
|
|
} else { |
156
|
|
|
$js = &$this->js; |
157
|
|
|
} |
158
|
|
|
if ($mode == 'file') { |
159
|
|
|
$js[0][] = h::script( |
160
|
|
|
[ |
161
|
|
|
'src' => $add |
162
|
|
|
] |
163
|
|
|
); |
164
|
|
|
} elseif ($mode == 'code') { |
165
|
|
|
$js[1][] = "$add\n"; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
/** |
171
|
|
|
* Including of CSS |
172
|
|
|
* |
173
|
|
|
* @param string|string[] $add Path to including file, or code |
174
|
|
|
* @param string $mode Can be <b>file</b> or <b>code</b> |
175
|
|
|
* |
176
|
|
|
* @return \cs\Page |
177
|
|
|
*/ |
178
|
|
|
function css ($add, $mode = 'file') { |
179
|
|
|
return $this->css_internal($add, $mode); |
180
|
|
|
} |
181
|
|
|
/** |
182
|
|
|
* @param string|string[] $add |
183
|
|
|
* @param string $mode |
184
|
|
|
* @param bool $core |
185
|
|
|
* |
186
|
|
|
* @return \cs\Page |
187
|
|
|
*/ |
188
|
|
|
protected function css_internal ($add, $mode = 'file', $core = false) { |
189
|
|
|
if (!$add) { |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
if (is_array($add)) { |
193
|
|
|
foreach (array_filter($add) as $style) { |
194
|
|
|
$this->css_internal($style, $mode, $core); |
195
|
|
|
} |
196
|
|
|
} else { |
197
|
|
|
if ($core) { |
198
|
|
|
$css = &$this->core_css; |
199
|
|
|
} else { |
200
|
|
|
$css = &$this->css; |
201
|
|
|
} |
202
|
|
|
if ($mode == 'file') { |
203
|
|
|
$css[0][] = h::link( |
204
|
|
|
[ |
205
|
|
|
'href' => $add, |
206
|
|
|
'rel' => 'stylesheet', |
207
|
|
|
'shim-shadowdom' => true |
208
|
|
|
] |
209
|
|
|
); |
210
|
|
|
} elseif ($mode == 'code') { |
211
|
|
|
$css[1][] = "$add\n"; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
/** |
217
|
|
|
* Add config on page to make it available on frontend |
218
|
|
|
* |
219
|
|
|
* @param mixed $config_structure Any scalar type or array |
220
|
|
|
* @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
221
|
|
|
* are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
222
|
|
|
* |
223
|
|
|
* @return \cs\Page |
224
|
|
|
*/ |
225
|
|
|
function config ($config_structure, $target) { |
226
|
|
|
return $this->config_internal($config_structure, $target); |
227
|
|
|
} |
228
|
|
|
/** |
229
|
|
|
* @param mixed $config_structure |
230
|
|
|
* @param string $target |
231
|
|
|
* @param bool $core |
232
|
|
|
* |
233
|
|
|
* @return \cs\Page |
234
|
|
|
*/ |
235
|
|
|
protected function config_internal ($config_structure, $target, $core = false) { |
236
|
|
|
$config = h::script( |
237
|
|
|
json_encode($config_structure, JSON_UNESCAPED_UNICODE), |
238
|
|
|
[ |
239
|
|
|
'target' => $target, |
240
|
|
|
'class' => 'cs-config', |
241
|
|
|
'type' => 'application/json' |
242
|
|
|
] |
243
|
|
|
); |
244
|
|
|
if ($core) { |
245
|
|
|
$this->core_config .= $config; |
246
|
|
|
} else { |
247
|
|
|
$this->config .= $config; |
248
|
|
|
} |
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
/** |
252
|
|
|
* Getting of HTML, JS and CSS includes |
253
|
|
|
* |
254
|
|
|
* @return \cs\Page |
255
|
|
|
*/ |
256
|
|
|
protected function add_includes_on_page () { |
257
|
|
|
$Config = Config::instance(true); |
258
|
|
|
if (!$Config) { |
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
/** |
262
|
|
|
* Base name for cache files |
263
|
|
|
*/ |
264
|
|
|
$this->pcache_basename = "_{$this->theme}_".Language::instance()->clang; |
265
|
|
|
/** |
266
|
|
|
* Some JS configs required by system |
267
|
|
|
*/ |
268
|
|
|
$this->add_system_configs(); |
269
|
|
|
// TODO: I hope some day we'll get rid of this sh*t :( |
270
|
|
|
$this->ie_edge(); |
271
|
|
|
$Request = Request::instance(); |
272
|
|
|
/** |
273
|
|
|
* If CSS and JavaScript compression enabled |
274
|
|
|
*/ |
275
|
|
|
if ($Config->core['cache_compress_js_css'] && !($Request->admin_path && isset($Request->query['debug']))) { |
276
|
|
|
$this->webcomponents_polyfill($Request, true); |
277
|
|
|
$includes = $this->get_includes_for_page_with_compression(); |
278
|
|
|
} else { |
279
|
|
|
$this->webcomponents_polyfill($Request, false); |
280
|
|
|
/** |
281
|
|
|
* Language translation is added explicitly only when compression is disabled, otherwise it will be in compressed JS file |
282
|
|
|
*/ |
283
|
|
|
/** |
284
|
|
|
* @var \cs\Page $this |
285
|
|
|
*/ |
286
|
|
|
$this->config_internal(Language::instance(), 'cs.Language', true); |
287
|
|
|
$this->config_internal($this->get_requirejs_paths(), 'requirejs.paths', true); |
288
|
|
|
$includes = $this->get_includes_for_page_without_compression($Config); |
289
|
|
|
} |
290
|
|
|
$this->css_internal($includes['css'], 'file', true); |
291
|
|
|
$this->js_internal($includes['js'], 'file', true); |
292
|
|
|
$this->html_internal($includes['html'], 'file', true); |
293
|
|
|
$this->add_includes_on_page_manually_added($Config); |
294
|
|
|
return $this; |
295
|
|
|
} |
296
|
|
|
/** |
297
|
|
|
* @param string[] $path |
298
|
|
|
* |
299
|
|
|
* @return string[] |
300
|
|
|
*/ |
301
|
|
|
protected function absolute_path_to_relative ($path) { |
302
|
|
|
return _substr($path, strlen(DIR) + 1); |
303
|
|
|
} |
304
|
|
|
/** |
305
|
|
|
* Add JS polyfills for IE/Edge |
306
|
|
|
*/ |
307
|
|
|
protected function ie_edge () { |
308
|
|
|
if (preg_match('/Trident|Edge/', Request::instance()->header('user-agent'))) { |
309
|
|
|
$this->js_internal( |
310
|
|
|
get_files_list(DIR."/includes/js/microsoft_sh*t", "/.*\\.js$/i", 'f', "includes/js/microsoft_sh*t", true), |
311
|
|
|
'file', |
312
|
|
|
true |
313
|
|
|
); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
/** |
317
|
|
|
* Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
318
|
|
|
* |
319
|
|
|
* @param Request $Request |
320
|
|
|
* @param bool $with_compression |
321
|
|
|
*/ |
322
|
|
|
protected function webcomponents_polyfill ($Request, $with_compression) { |
323
|
|
|
if ($Request->cookie('shadow_dom') != 1) { |
324
|
|
|
$file = 'includes/js/WebComponents-polyfill/webcomponents-custom.min.js'; |
325
|
|
|
if ($with_compression) { |
326
|
|
|
$compressed_file = PUBLIC_CACHE.'/webcomponents.js'; |
327
|
|
|
if (!file_exists($compressed_file)) { |
328
|
|
|
$content = file_get_contents(DIR."/$file"); |
329
|
|
|
file_put_contents($compressed_file, gzencode($content, 9), LOCK_EX | FILE_BINARY); |
330
|
|
|
file_put_contents("$compressed_file.hash", substr(md5($content), 0, 5)); |
331
|
|
|
} |
332
|
|
|
$hash = file_get_contents("$compressed_file.hash"); |
333
|
|
|
$this->js_internal("storage/pcache/webcomponents.js?$hash", 'file', true); |
334
|
|
|
} else { |
335
|
|
|
$this->js_internal($file, 'file', true); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
protected function add_system_configs () { |
340
|
|
|
$Config = Config::instance(); |
341
|
|
|
$Request = Request::instance(); |
342
|
|
|
$User = User::instance(); |
343
|
|
|
$current_module = $Request->current_module; |
344
|
|
|
$this->config_internal( |
345
|
|
|
[ |
346
|
|
|
'base_url' => $Config->base_url(), |
347
|
|
|
'current_base_url' => $Config->base_url().'/'.($Request->admin_path ? 'admin/' : '').$current_module, |
348
|
|
|
'public_key' => Core::instance()->public_key, |
349
|
|
|
'module' => $current_module, |
350
|
|
|
'in_admin' => (int)$Request->admin_path, |
351
|
|
|
'is_admin' => (int)$User->admin(), |
352
|
|
|
'is_user' => (int)$User->user(), |
353
|
|
|
'is_guest' => (int)$User->guest(), |
354
|
|
|
'password_min_length' => (int)$Config->core['password_min_length'], |
355
|
|
|
'password_min_strength' => (int)$Config->core['password_min_strength'], |
356
|
|
|
'debug' => (int)DEBUG, |
357
|
|
|
'route' => $Request->route, |
358
|
|
|
'route_path' => $Request->route_path, |
359
|
|
|
'route_ids' => $Request->route_ids |
360
|
|
|
], |
361
|
|
|
'cs', |
362
|
|
|
true |
363
|
|
|
); |
364
|
|
|
if ($User->admin()) { |
365
|
|
|
$this->config_internal((int)$Config->core['simple_admin_mode'], 'cs.simple_admin_mode', true); |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
/** |
369
|
|
|
* @return array[] |
370
|
|
|
*/ |
371
|
|
|
protected function get_includes_for_page_with_compression () { |
372
|
|
|
/** |
373
|
|
|
* Rebuild cache if necessary |
374
|
|
|
*/ |
375
|
|
|
if (!file_exists(PUBLIC_CACHE."/$this->pcache_basename.json")) { |
376
|
|
|
$this->rebuild_cache(); |
377
|
|
|
} |
378
|
|
|
list($dependencies, $structure) = file_get_json(PUBLIC_CACHE."/$this->pcache_basename.json"); |
379
|
|
|
$system_includes = [ |
380
|
|
|
'css' => ["storage/pcache/$this->pcache_basename.css?{$structure['']['css']}"], |
381
|
|
|
'js' => ["storage/pcache/$this->pcache_basename.js?{$structure['']['js']}"], |
382
|
|
|
'html' => ["storage/pcache/$this->pcache_basename.html?{$structure['']['html']}"] |
383
|
|
|
]; |
384
|
|
|
list($includes, $dependencies_includes, $dependencies, $current_url) = $this->get_includes_prepare($dependencies, '+'); |
385
|
|
|
foreach ($structure as $filename_prefix => $hashes) { |
386
|
|
|
if (!$filename_prefix) { |
387
|
|
|
continue; |
388
|
|
|
} |
389
|
|
|
$is_dependency = $this->get_includes_is_dependency($dependencies, $filename_prefix, '+'); |
390
|
|
|
if ($is_dependency || mb_strpos($current_url, $filename_prefix) === 0) { |
391
|
|
|
foreach ($hashes as $extension => $hash) { |
392
|
|
|
if ($is_dependency) { |
393
|
|
|
$dependencies_includes[$extension][] = "storage/pcache/$filename_prefix$this->pcache_basename.$extension?$hash"; |
394
|
|
|
} else { |
395
|
|
|
$includes[$extension][] = "storage/pcache/$filename_prefix$this->pcache_basename.$extension?$hash"; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
return array_merge_recursive($system_includes, $dependencies_includes, $includes); |
401
|
|
|
} |
402
|
|
|
/** |
403
|
|
|
* @param Config $Config |
404
|
|
|
* |
405
|
|
|
* @return array[] |
406
|
|
|
*/ |
407
|
|
|
protected function get_includes_for_page_without_compression ($Config) { |
408
|
|
|
// To determine all dependencies and stuff we need `$Config` object to be already created |
409
|
|
|
if ($Config) { |
410
|
|
|
list($dependencies, $includes_map) = $this->includes_dependencies_and_map(); |
411
|
|
|
$system_includes = $includes_map['']; |
412
|
|
|
list($includes, $dependencies_includes, $dependencies, $current_url) = $this->get_includes_prepare($dependencies, '/'); |
413
|
|
|
foreach ($includes_map as $url => $local_includes) { |
414
|
|
|
if (!$url) { |
415
|
|
|
continue; |
416
|
|
|
} |
417
|
|
|
$is_dependency = $this->get_includes_is_dependency($dependencies, $url, '/'); |
418
|
|
|
if ($is_dependency) { |
419
|
|
|
$dependencies_includes = array_merge_recursive($dependencies_includes, $local_includes); |
420
|
|
|
} elseif (mb_strpos($current_url, $url) === 0) { |
421
|
|
|
$includes = array_merge_recursive($includes, $local_includes); |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
$includes = array_merge_recursive($system_includes, $dependencies_includes, $includes); |
425
|
|
|
} else { |
426
|
|
|
$includes = $this->get_includes_list(); |
427
|
|
|
} |
428
|
|
|
return $this->add_versions_hash($this->absolute_path_to_relative($includes)); |
429
|
|
|
} |
430
|
|
|
/** |
431
|
|
|
* @param array $dependencies |
432
|
|
|
* @param string $separator `+` or `/` |
433
|
|
|
* |
434
|
|
|
* @return array |
435
|
|
|
*/ |
436
|
|
|
protected function get_includes_prepare ($dependencies, $separator) { |
437
|
|
|
$Request = Request::instance(); |
438
|
|
|
$includes = [ |
439
|
|
|
'css' => [], |
440
|
|
|
'js' => [], |
441
|
|
|
'html' => [] |
442
|
|
|
]; |
443
|
|
|
$dependencies_includes = $includes; |
444
|
|
|
$current_module = $Request->current_module; |
445
|
|
|
/** |
446
|
|
|
* Current URL based on controller path (it better represents how page was rendered) |
447
|
|
|
*/ |
448
|
|
|
$current_url = array_slice(App::instance()->controller_path, 1); |
449
|
|
|
$current_url = ($Request->admin_path ? "admin$separator" : '')."$current_module$separator".implode($separator, $current_url); |
450
|
|
|
/** |
451
|
|
|
* Narrow the dependencies to current module only |
452
|
|
|
*/ |
453
|
|
|
$dependencies = array_merge( |
454
|
|
|
isset($dependencies[$current_module]) ? $dependencies[$current_module] : [], |
455
|
|
|
$dependencies['System'] |
456
|
|
|
); |
457
|
|
|
return [$includes, $dependencies_includes, $dependencies, $current_url]; |
458
|
|
|
} |
459
|
|
|
/** |
460
|
|
|
* @param array $dependencies |
461
|
|
|
* @param string $url |
462
|
|
|
* @param string $separator `+` or `/` |
463
|
|
|
* |
464
|
|
|
* @return bool |
465
|
|
|
*/ |
466
|
|
|
protected function get_includes_is_dependency ($dependencies, $url, $separator) { |
467
|
|
|
$url_exploded = explode($separator, $url); |
468
|
|
|
/** @noinspection NestedTernaryOperatorInspection */ |
469
|
|
|
$url_module = $url_exploded[0] != 'admin' ? $url_exploded[0] : (@$url_exploded[1] ?: ''); |
470
|
|
|
$Request = Request::instance(); |
471
|
|
|
return |
472
|
|
|
$url_module !== Config::SYSTEM_MODULE && |
473
|
|
|
in_array($url_module, $dependencies) && |
474
|
|
|
( |
475
|
|
|
$Request->admin_path || $Request->admin_path == ($url_exploded[0] == 'admin') |
476
|
|
|
); |
477
|
|
|
} |
478
|
|
|
protected function add_versions_hash ($includes) { |
479
|
|
|
$content = array_map('file_get_contents', get_files_list(DIR.'/components', '/meta\.json/', 'f', true, true)); |
480
|
|
|
$content = implode('', $content); |
481
|
|
|
$hash = substr(md5($content), 0, 5); |
482
|
|
|
foreach ($includes as &$files) { |
483
|
|
|
foreach ($files as &$file) { |
484
|
|
|
$file .= "?$hash"; |
485
|
|
|
} |
486
|
|
|
unset($file); |
487
|
|
|
} |
488
|
|
|
return $includes; |
489
|
|
|
} |
490
|
|
|
/** |
491
|
|
|
* @param Config $Config |
492
|
|
|
*/ |
493
|
|
|
protected function add_includes_on_page_manually_added ($Config) { |
494
|
|
|
foreach (['core_html', 'core_js', 'core_css', 'html', 'js', 'css'] as $type) { |
495
|
|
|
foreach ($this->$type as &$elements) { |
496
|
|
|
$elements = implode('', array_unique($elements)); |
497
|
|
|
} |
498
|
|
|
unset($elements); |
499
|
|
|
} |
500
|
|
|
$this->Head .= |
501
|
|
|
$this->core_config. |
502
|
|
|
$this->config. |
503
|
|
|
$this->core_css[0].$this->css[0]. |
504
|
|
|
h::style($this->core_css[1].$this->css[1] ?: false); |
505
|
|
|
$js_html_insert_to = $Config->core['put_js_after_body'] ? 'post_Body' : 'Head'; |
506
|
|
|
$js_html = |
507
|
|
|
$this->core_js[0]. |
508
|
|
|
h::script($this->core_js[1] ?: false). |
509
|
|
|
$this->js[0]. |
510
|
|
|
h::script($this->js[1] ?: false). |
511
|
|
|
$this->core_html[0].$this->html[0]. |
512
|
|
|
$this->core_html[1].$this->html[1]; |
513
|
|
|
$this->$js_html_insert_to .= $js_html; |
514
|
|
|
} |
515
|
|
|
/** |
516
|
|
|
* Getting of HTML, JS and CSS files list to be included |
517
|
|
|
* |
518
|
|
|
* @return string[][] |
519
|
|
|
*/ |
520
|
|
|
protected function get_includes_list () { |
521
|
|
|
$includes = []; |
522
|
|
|
/** |
523
|
|
|
* Get includes of system and theme |
524
|
|
|
*/ |
525
|
|
|
$this->get_includes_list_add_includes(DIR.'/includes', $includes); |
526
|
|
|
$this->get_includes_list_add_includes(THEMES."/$this->theme", $includes); |
527
|
|
|
$Config = Config::instance(); |
528
|
|
|
foreach ($Config->components['modules'] as $module_name => $module_data) { |
529
|
|
|
if ($module_data['active'] == Config\Module_Properties::UNINSTALLED) { |
530
|
|
|
continue; |
531
|
|
|
} |
532
|
|
|
$this->get_includes_list_add_includes(MODULES."/$module_name/includes", $includes); |
533
|
|
|
} |
534
|
|
|
foreach ($Config->components['plugins'] as $plugin_name) { |
535
|
|
|
$this->get_includes_list_add_includes(PLUGINS."/$plugin_name/includes", $includes); |
536
|
|
|
} |
537
|
|
|
return [ |
538
|
|
|
'html' => array_merge(...$includes['html']), |
539
|
|
|
'js' => array_merge(...$includes['js']), |
540
|
|
|
'css' => array_merge(...$includes['css']) |
541
|
|
|
]; |
542
|
|
|
} |
543
|
|
|
/** |
544
|
|
|
* @param string $base_dir |
545
|
|
|
* @param string[][] $includes |
546
|
|
|
*/ |
547
|
|
|
protected function get_includes_list_add_includes ($base_dir, &$includes) { |
548
|
|
|
$includes['html'][] = $this->get_includes_list_add_includes_internal($base_dir, 'html'); |
549
|
|
|
$includes['js'][] = $this->get_includes_list_add_includes_internal($base_dir, 'js'); |
550
|
|
|
$includes['css'][] = $this->get_includes_list_add_includes_internal($base_dir, 'css'); |
551
|
|
|
} |
552
|
|
|
/** |
553
|
|
|
* @param string $base_dir |
554
|
|
|
* @param string $ext |
555
|
|
|
* |
556
|
|
|
* @return array |
557
|
|
|
*/ |
558
|
|
|
protected function get_includes_list_add_includes_internal ($base_dir, $ext) { |
559
|
|
|
return get_files_list("$base_dir/$ext", "/.*\\.$ext\$/i", 'f', true, true, 'name', '!include') ?: []; |
560
|
|
|
} |
561
|
|
|
/** |
562
|
|
|
* Rebuilding of HTML, JS and CSS cache |
563
|
|
|
* |
564
|
|
|
* @return \cs\Page |
565
|
|
|
*/ |
566
|
|
|
protected function rebuild_cache () { |
567
|
|
|
list($dependencies, $includes_map) = $this->includes_dependencies_and_map(); |
568
|
|
|
$structure = []; |
569
|
|
|
foreach ($includes_map as $filename_prefix => $includes) { |
570
|
|
|
// We replace `/` by `+` to make it suitable for filename |
571
|
|
|
$filename_prefix = str_replace('/', '+', $filename_prefix); |
572
|
|
|
$structure[$filename_prefix] = $this->create_cached_includes_files($filename_prefix, $includes); |
573
|
|
|
} |
574
|
|
|
unset($includes_map, $filename_prefix, $includes); |
575
|
|
|
file_put_json( |
576
|
|
|
PUBLIC_CACHE."/$this->pcache_basename.json", |
577
|
|
|
[$dependencies, $structure] |
578
|
|
|
); |
579
|
|
|
unset($structure); |
580
|
|
|
Event::instance()->fire('System/Page/rebuild_cache'); |
581
|
|
|
return $this; |
582
|
|
|
} |
583
|
|
|
/** |
584
|
|
|
* Creates cached version of given HTML, JS and CSS files. |
585
|
|
|
* Resulting file name consists of <b>$filename_prefix</b> and <b>$this->pcache_basename</b> |
586
|
|
|
* |
587
|
|
|
* @param string $filename_prefix |
588
|
|
|
* @param array $includes Array of paths to files, may have keys: <b>css</b> and/or <b>js</b> and/or <b>html</b> |
589
|
|
|
* |
590
|
|
|
* @return array |
591
|
|
|
*/ |
592
|
|
|
protected function create_cached_includes_files ($filename_prefix, $includes) { |
593
|
|
|
$cache_hash = []; |
594
|
|
|
/** @noinspection AlterInForeachInspection */ |
595
|
|
|
foreach ($includes as $extension => $files) { |
596
|
|
|
$content = $this->create_cached_includes_files_process_files( |
597
|
|
|
$extension, |
598
|
|
|
$filename_prefix, |
599
|
|
|
$files |
600
|
|
|
); |
601
|
|
|
file_put_contents(PUBLIC_CACHE."/$filename_prefix$this->pcache_basename.$extension", gzencode($content, 9), LOCK_EX | FILE_BINARY); |
602
|
|
|
$cache_hash[$extension] = substr(md5($content), 0, 5); |
603
|
|
|
} |
604
|
|
|
return $cache_hash; |
605
|
|
|
} |
606
|
|
|
protected function create_cached_includes_files_process_files ($extension, $filename_prefix, $files) { |
607
|
|
|
$content = ''; |
608
|
|
|
switch ($extension) { |
609
|
|
|
/** |
610
|
|
|
* Insert external elements into resulting css file. |
611
|
|
|
* It is needed, because those files will not be copied into new destination of resulting css file. |
612
|
|
|
*/ |
613
|
|
|
case 'css': |
614
|
|
|
$callback = function ($content, $file) { |
615
|
|
|
return |
616
|
|
|
$content. |
617
|
|
|
Includes_processing::css( |
618
|
|
|
file_get_contents($file), |
619
|
|
|
$file |
620
|
|
|
); |
621
|
|
|
}; |
622
|
|
|
break; |
623
|
|
|
/** |
624
|
|
|
* Combine css and js files for Web Component into resulting files in order to optimize loading process |
625
|
|
|
*/ |
626
|
|
|
case 'html': |
627
|
|
|
/** |
628
|
|
|
* For CSP-compatible HTML files we need to know destination to put there additional JS/CSS files |
629
|
|
|
*/ |
630
|
|
|
$destination = Config::instance()->core['vulcanization'] ? false : PUBLIC_CACHE; |
631
|
|
|
$callback = function ($content, $file) use ($filename_prefix, $destination) { |
632
|
|
|
return |
633
|
|
|
$content. |
634
|
|
|
Includes_processing::html( |
635
|
|
|
file_get_contents($file), |
636
|
|
|
$file, |
637
|
|
|
"$filename_prefix$this->pcache_basename-".basename($file).'+'.substr(md5($file), 0, 5), |
638
|
|
|
$destination |
639
|
|
|
); |
640
|
|
|
}; |
641
|
|
|
break; |
642
|
|
|
case 'js': |
643
|
|
|
$callback = function ($content, $file) { |
644
|
|
|
return |
645
|
|
|
$content. |
646
|
|
|
Includes_processing::js(file_get_contents($file)); |
647
|
|
|
}; |
648
|
|
|
if ($filename_prefix == '') { |
649
|
|
|
$content = 'window.cs={Language:'._json_encode(Language::instance()).'};'; |
650
|
|
|
$content .= 'window.requirejs={paths:'._json_encode($this->get_requirejs_paths()).'};'; |
651
|
|
|
} |
652
|
|
|
} |
653
|
|
|
/** @noinspection PhpUndefinedVariableInspection */ |
654
|
|
|
return array_reduce(array_filter($files, 'file_exists'), $callback, $content); |
655
|
|
|
} |
656
|
|
|
/** |
657
|
|
|
* Get dependencies of components between each other (only that contains some HTML, JS and CSS files) and mapping HTML, JS and CSS files to URL paths |
658
|
|
|
* |
659
|
|
|
* @return array[] [$dependencies, $includes_map] |
660
|
|
|
*/ |
661
|
|
|
protected function includes_dependencies_and_map () { |
662
|
|
|
/** |
663
|
|
|
* Get all includes |
664
|
|
|
*/ |
665
|
|
|
$all_includes = $this->get_includes_list(); |
666
|
|
|
$includes_map = []; |
667
|
|
|
/** |
668
|
|
|
* Array [package => [list of packages it depends on]] |
669
|
|
|
*/ |
670
|
|
|
$dependencies = []; |
671
|
|
|
$functionalities = []; |
672
|
|
|
/** |
673
|
|
|
* According to components's maps some files should be included only on specific pages. |
674
|
|
|
* Here we read this rules, and remove from whole includes list such items, that should be included only on specific pages. |
675
|
|
|
* Also collect dependencies. |
676
|
|
|
*/ |
677
|
|
|
$Config = Config::instance(); |
678
|
|
|
foreach ($Config->components['modules'] as $module_name => $module_data) { |
679
|
|
|
if ($module_data['active'] == Config\Module_Properties::UNINSTALLED) { |
680
|
|
|
continue; |
681
|
|
|
} |
682
|
|
|
if (file_exists(MODULES."/$module_name/meta.json")) { |
683
|
|
|
$this->process_meta( |
684
|
|
|
file_get_json(MODULES."/$module_name/meta.json"), |
685
|
|
|
$dependencies, |
686
|
|
|
$functionalities |
687
|
|
|
); |
688
|
|
|
} |
689
|
|
|
if (file_exists(MODULES."/$module_name/includes/map.json")) { |
690
|
|
|
$this->process_map( |
691
|
|
|
file_get_json_nocomments(MODULES."/$module_name/includes/map.json"), |
692
|
|
|
MODULES."/$module_name/includes", |
693
|
|
|
$includes_map, |
694
|
|
|
$all_includes |
695
|
|
|
); |
696
|
|
|
} |
697
|
|
|
} |
698
|
|
|
unset($module_name, $module_data); |
699
|
|
|
foreach ($Config->components['plugins'] as $plugin_name) { |
700
|
|
|
if (file_exists(PLUGINS."/$plugin_name/meta.json")) { |
701
|
|
|
$this->process_meta( |
702
|
|
|
file_get_json(PLUGINS."/$plugin_name/meta.json"), |
703
|
|
|
$dependencies, |
704
|
|
|
$functionalities |
705
|
|
|
); |
706
|
|
|
} |
707
|
|
|
if (file_exists(PLUGINS."/$plugin_name/includes/map.json")) { |
708
|
|
|
$this->process_map( |
709
|
|
|
file_get_json_nocomments(PLUGINS."/$plugin_name/includes/map.json"), |
710
|
|
|
PLUGINS."/$plugin_name/includes", |
711
|
|
|
$includes_map, |
712
|
|
|
$all_includes |
713
|
|
|
); |
714
|
|
|
} |
715
|
|
|
} |
716
|
|
|
unset($plugin_name); |
717
|
|
|
/** |
718
|
|
|
* For consistency |
719
|
|
|
*/ |
720
|
|
|
$includes_map[''] = $all_includes; |
721
|
|
|
Event::instance()->fire( |
722
|
|
|
'System/Page/includes_dependencies_and_map', |
723
|
|
|
[ |
724
|
|
|
'dependencies' => &$dependencies, |
725
|
|
|
'includes_map' => &$includes_map |
726
|
|
|
] |
727
|
|
|
); |
728
|
|
|
$dependencies = $this->normalize_dependencies($dependencies, $functionalities); |
729
|
|
|
$includes_map = $this->clean_includes_arrays_without_files($dependencies, $includes_map); |
730
|
|
|
$dependencies = array_map('array_values', $dependencies); |
731
|
|
|
$dependencies = array_filter($dependencies); |
732
|
|
|
return [$dependencies, $includes_map]; |
733
|
|
|
} |
734
|
|
|
/** |
735
|
|
|
* Process meta information and corresponding entries to dependencies and functionalities |
736
|
|
|
* |
737
|
|
|
* @param array $meta |
738
|
|
|
* @param array $dependencies |
739
|
|
|
* @param array $functionalities |
740
|
|
|
*/ |
741
|
|
|
protected function process_meta ($meta, &$dependencies, &$functionalities) { |
742
|
|
|
$package = $meta['package']; |
743
|
|
|
if (isset($meta['require'])) { |
744
|
|
|
foreach ((array)$meta['require'] as $r) { |
745
|
|
|
/** |
746
|
|
|
* Get only name of package or functionality |
747
|
|
|
*/ |
748
|
|
|
$r = preg_split('/[=<>]/', $r, 2)[0]; |
749
|
|
|
$dependencies[$package][] = $r; |
750
|
|
|
} |
751
|
|
|
} |
752
|
|
|
if (isset($meta['optional'])) { |
753
|
|
|
foreach ((array)$meta['optional'] as $o) { |
754
|
|
|
/** |
755
|
|
|
* Get only name of package or functionality |
756
|
|
|
*/ |
757
|
|
|
$o = preg_split('/[=<>]/', $o, 2)[0]; |
758
|
|
|
$dependencies[$package][] = $o; |
759
|
|
|
} |
760
|
|
|
} |
761
|
|
|
if (isset($meta['provide'])) { |
762
|
|
|
foreach ((array)$meta['provide'] as $p) { |
763
|
|
|
/** |
764
|
|
|
* If provides sub-functionality for other component (for instance, `Blog/post_patch`) - inverse "providing" to "dependency" |
765
|
|
|
* Otherwise it is just functionality alias to package name |
766
|
|
|
*/ |
767
|
|
|
if (strpos($p, '/') !== false) { |
768
|
|
|
/** |
769
|
|
|
* Get name of package or functionality |
770
|
|
|
*/ |
771
|
|
|
$p = explode('/', $p)[0]; |
772
|
|
|
$dependencies[$p][] = $package; |
773
|
|
|
} else { |
774
|
|
|
$functionalities[$p] = $package; |
775
|
|
|
} |
776
|
|
|
} |
777
|
|
|
} |
778
|
|
|
} |
779
|
|
|
/** |
780
|
|
|
* Process map structure, fill includes map and remove files from list of all includes (remaining files will be included on all pages) |
781
|
|
|
* |
782
|
|
|
* @param array $map |
783
|
|
|
* @param string $includes_dir |
784
|
|
|
* @param array $includes_map |
785
|
|
|
* @param array $all_includes |
786
|
|
|
*/ |
787
|
|
|
protected function process_map ($map, $includes_dir, &$includes_map, &$all_includes) { |
788
|
|
|
foreach ($map as $path => $files) { |
789
|
|
|
foreach ((array)$files as $file) { |
790
|
|
|
$extension = file_extension($file); |
791
|
|
|
if (in_array($extension, ['css', 'js', 'html'])) { |
792
|
|
|
$file = "$includes_dir/$extension/$file"; |
793
|
|
|
$includes_map[$path][$extension][] = $file; |
794
|
|
|
$all_includes[$extension] = array_diff($all_includes[$extension], [$file]); |
795
|
|
|
} else { |
796
|
|
|
$file = rtrim($file, '*'); |
797
|
|
|
/** |
798
|
|
|
* Wildcard support, it is possible to specify just path prefix and all files with this prefix will be included |
799
|
|
|
*/ |
800
|
|
|
$found_files = array_filter( |
801
|
|
|
get_files_list($includes_dir, '/.*\.(css|js|html)$/i', 'f', '', true, 'name', '!include') ?: [], |
802
|
|
|
function ($f) use ($file) { |
803
|
|
|
// We need only files with specified mask and only those located in directory that corresponds to file's extension |
804
|
|
|
return preg_match("#^(css|js|html)/$file.*\\1$#i", $f); |
805
|
|
|
} |
806
|
|
|
); |
807
|
|
|
// Drop first level directory |
808
|
|
|
$found_files = _preg_replace('#^[^/]+/(.*)#', '$1', $found_files); |
809
|
|
|
$this->process_map([$path => $found_files], $includes_dir, $includes_map, $all_includes); |
810
|
|
|
} |
811
|
|
|
} |
812
|
|
|
} |
813
|
|
|
} |
814
|
|
|
/** |
815
|
|
|
* Replace functionalities by real packages names, take into account recursive dependencies |
816
|
|
|
* |
817
|
|
|
* @param array $dependencies |
818
|
|
|
* @param array $functionalities |
819
|
|
|
* |
820
|
|
|
* @return array |
821
|
|
|
*/ |
822
|
|
|
protected function normalize_dependencies ($dependencies, $functionalities) { |
823
|
|
|
/** |
824
|
|
|
* First of all remove packages without any dependencies |
825
|
|
|
*/ |
826
|
|
|
$dependencies = array_filter($dependencies); |
827
|
|
|
/** |
828
|
|
|
* First round, process aliases among keys |
829
|
|
|
*/ |
830
|
|
|
foreach (array_keys($dependencies) as $d) { |
831
|
|
|
if (isset($functionalities[$d])) { |
832
|
|
|
$package = $functionalities[$d]; |
833
|
|
|
/** |
834
|
|
|
* Add dependencies to existing package dependencies |
835
|
|
|
*/ |
836
|
|
|
foreach ($dependencies[$d] as $dependency) { |
837
|
|
|
$dependencies[$package][] = $dependency; |
838
|
|
|
} |
839
|
|
|
/** |
840
|
|
|
* Drop alias |
841
|
|
|
*/ |
842
|
|
|
unset($dependencies[$d]); |
843
|
|
|
} |
844
|
|
|
} |
845
|
|
|
unset($d, $dependency); |
846
|
|
|
/** |
847
|
|
|
* Second round, process aliases among dependencies |
848
|
|
|
*/ |
849
|
|
|
foreach ($dependencies as &$depends_on) { |
850
|
|
|
foreach ($depends_on as &$dependency) { |
851
|
|
|
if (isset($functionalities[$dependency])) { |
852
|
|
|
$dependency = $functionalities[$dependency]; |
853
|
|
|
} |
854
|
|
|
} |
855
|
|
|
} |
856
|
|
|
unset($depends_on, $dependency); |
857
|
|
|
/** |
858
|
|
|
* Third round, process recursive dependencies |
859
|
|
|
*/ |
860
|
|
|
foreach ($dependencies as &$depends_on) { |
861
|
|
|
foreach ($depends_on as &$dependency) { |
862
|
|
|
if ($dependency != 'System' && isset($dependencies[$dependency])) { |
863
|
|
|
foreach (array_diff($dependencies[$dependency], $depends_on) as $new_dependency) { |
864
|
|
|
$depends_on[] = $new_dependency; |
865
|
|
|
} |
866
|
|
|
} |
867
|
|
|
} |
868
|
|
|
} |
869
|
|
|
return array_map('array_unique', $dependencies); |
870
|
|
|
} |
871
|
|
|
/** |
872
|
|
|
* Includes array is composed from dependencies and sometimes dependencies doesn't have any files, so we'll clean that |
873
|
|
|
* |
874
|
|
|
* @param array $dependencies |
875
|
|
|
* @param array $includes_map |
876
|
|
|
* |
877
|
|
|
* @return array |
878
|
|
|
*/ |
879
|
|
|
protected function clean_includes_arrays_without_files ($dependencies, $includes_map) { |
880
|
|
|
foreach ($dependencies as &$depends_on) { |
881
|
|
|
foreach ($depends_on as $index => &$dependency) { |
882
|
|
|
if (!isset($includes_map[$dependency])) { |
883
|
|
|
unset($depends_on[$index]); |
884
|
|
|
} |
885
|
|
|
} |
886
|
|
|
unset($dependency); |
887
|
|
|
} |
888
|
|
|
return $includes_map; |
889
|
|
|
} |
890
|
|
|
} |
891
|
|
|
|