-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
729 lines (527 loc) · 20.2 KB
/
functions.php
File metadata and controls
729 lines (527 loc) · 20.2 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
<?
function h1($text, $class = false) {
print '<h1'.($class ? ' class="'.$class.'"' : '').'>'.$text.'</h1>';
}
function h2($text, $class = false) {
print '<h2'.($class ? ' class="'.$class.'"' : '').'>'.$text.'</h2>';
}
function h3($text, $class = false) {
print '<h3'.($class ? ' class="'.$class.'"' : '').'>'.$text.'</h3>';
}
function p($text, $class = false) {
print '<p'.($class ? ' class="'.$class.'"' : '').'>'.$text.'</p>';
}
function div($text, $class = false) {
print '<div'.($class ? ' class="'.$class.'"' : '').'>'.$text.'</div>';
}
function debug($vars, $output = "print") {
if(!is_array($vars)) {
$vars = [$vars];
}
foreach($vars as $var) {
if($output == "file") {
writeToFile($var);
}
else {
print_r($var);
print "<br />\n";
}
}
}
// Check if web url points to valid ressource
function web_file_exists($url) {
$context = stream_context_create(['http' => ['method' => 'HEAD']]);
$headers = get_headers($url, 0, $context);
return stripos($headers[0], "200 OK") ? true : false;
}
function makeDirRecursively($path) {
if(!file_exists($path)) {
$parts = explode("/", $path);
$verify_path = "";
for($i = 1; $i < count($parts); $i++) {
$verify_path .= "/".$parts[$i];
if(!file_exists($verify_path)) {
mkdir($verify_path);
}
}
}
}
function safeCopy($src, $dest) {
global $domain;
// h2("safeCopy:" . $src . ";". $dest . ":" . $domain);
global $doc_root;
$clean_src = preg_replace("/([\?#]{1}[^$]+)?/", "", $src);
$clean_dest = preg_replace("/([\?#]{1}[^$]+)?/", "", $dest);
makeDirRecursively(dirname($doc_root.$clean_dest));
// is file available via filesystem
if(file_exists($doc_root.$clean_src)) {
// h2("copy directly:" . $doc_root.$clean_src . " -> " . $doc_root.$clean_dest);
copy($doc_root.$clean_src, $doc_root.$clean_dest);
}
// copy file from web
else {
// h2("copy via web:" . $domain.$src . " -> " . $doc_root.$clean_dest);
file_put_contents($doc_root.$clean_dest, file_get_contents($domain.$src));
}
}
function parseJSFile($file, $fp) {
global $js_include_size;
global $domain;
// global $doc_root;
// If includes start with //, then prefix with http:
if(preg_match("/^\/\//", $file)) {
$file = "http:" . $file;
}
// normalize file path
$file_fragments_raw = explode("/", $file);
$file_fragments = [];
foreach($file_fragments_raw as $folder) {
if($folder == "..") {
array_pop($file_fragments);
}
else {
array_push($file_fragments, $folder);
}
}
$file = implode("/", $file_fragments);
$file_content = @file_get_contents($file);
if($file_content === false) {
print '<div class="file bad">'."\n";
print "\t".'<h2 class="bad">'.$file.": Missing file</h2>\n";
print '</div>';
return;
}
$file_size = strlen($file_content);
$js_include_size += $file_size ? $file_size : 0;
$minisize = 0;
// file header
print '<div class="file">'."\n";
// get lines from file
$lines = file($file);
// if file has content
if(count($lines)) {
print "\t".'<h2 class="good" onclick="this.parentNode.className = this.parentNode.className.match(/open/) ? \'file\' : \'file open\'">'.$file."</h2>\n";
fwrite($fp, "\n");
fwrite($fp, "/*".basename($file)."*/\n");
// print $file_size;
$comment_switch = false;
foreach($lines as $linenumber => $line) {
// adjustment string - modify this string, for reference in matches
$work_line = $line;
$include_line = false;
if($work_line) {
// Deal with comments
// remove one-liner /**/ comments, even if nested inside other string
if(!$comment_switch && preg_match("/\/\*[^$]+\*\//", $work_line)) {
$work_line = preg_replace("/\/\*[^$]+\*\//", "", $work_line);
}
// found for /* comment start
if(!$comment_switch && strpos($work_line, "/*") !== false) {
$com_s_pos = strpos($line, "/*");
$comment_switch = true;
// get line content before comment starts (if any)
$work_line = substr($line, 0, $com_s_pos);
}
// comment switch is on, look for */ comment end
if($comment_switch && strpos($work_line, "*/") !== false) {
$com_e_pos = strpos($line, "*/");
$comment_switch = false;
// get line content after comment is ended
$work_line = substr($line, $com_e_pos+2);
$com_s_pos = 0;
$com_e_pos = 0;
}
// comment switch is on, remove all content
else if($comment_switch) {
$work_line = "";
}
// check for // comment starts the line
// skip work line
if(!$comment_switch && preg_match("/^\/\//", $work_line)) {
$work_line = "";
}
// check for // comment start position within line
// ignore if // is inside quoted string or in regular expression
if(!$comment_switch && preg_match_all("/[^:\\\]{1}\/\//", $work_line, $matches)) {
// multiple matches to be investigated
if(count($matches[0]) > 1) {
// print "multiple occurences<br>";
for($i = 0; $i < count($matches); $i++) {
// start with last occurence
$pos = strrpos($work_line, "//");
// add new newline, because we are get substring from begining only
$additional_test = substr($work_line, 0, $pos)."\n";
// check if removal breaks quoted string or quoted string was already broken
if(
(substr_count($additional_test, '"')%2 === 0 || substr_count($work_line, '"')%2 === 1)
&&
(substr_count($additional_test, "'")%2 === 0 || substr_count($work_line, "'")%2 === 1)
) {
$work_line = $additional_test;
}
}
}
// only one occurence
else {
// remove from occurence to end
$additional_test = preg_replace("/\/\/.*/", "", $work_line);
// check if removal breaks quoted string or quoted string was already broken
if(
(substr_count($additional_test, '"')%2 === 0 || substr_count($work_line, '"')%2 === 1)
&&
(substr_count($additional_test, "'")%2 === 0 || substr_count($work_line, "'")%2 === 1)
) {
$work_line = $additional_test;
}
}
}
// Process line (unless comment switch is on)
// check if line contains new include path
// if it does, then continue iteration in included file and skip work line
if(!$comment_switch && preg_match("/document.write[^$]+script[^$]+src=\"([a-zA-Z0-9\.\/_\:\-\=\?]+)\"/i", $work_line, $matches)) {
// print "matched include:".$matches[1]."<br>";
$work_line = "";
$include_line = true;
// external include
if(preg_match("/(http[s]?:)?\/\//i", $matches[1])) {
$filepath = $matches[1];
}
// local, absolute include
else if(strpos($matches[1], "/") === 0) {
// $filepath = $_SERVER["DOCUMENT_ROOT"].$matches[1];
$filepath = $domain.$matches[1];
}
// relative include
// JS include can only be relative if they are always included from same level dir
// if relative path is found here, expect that is is relative to document root
else {
$filepath = $domain."/".$matches[1];
}
// parse new include file
parseJSFile($filepath, $fp);
// add whitespace
fwrite($fp, "\n");
}
// If work line still contains data, then add it to the output file
if(trim($work_line) && !$comment_switch) {
fwrite($fp, $work_line);
$minisize += strlen($work_line);
}
}
// output result/stats of parsing
// No change was made
if(!$comment_switch && (trim($work_line) && trim($line) == trim($work_line))) {
print "\t".'<div class="notminified"><code>'.$linenumber.':'.htmlentities($line).'</code></div>';
}
// Change was made and it is not an include line
else if(!$include_line) {
print "\t".'<div class="minified"><span class="bad">'.$linenumber.':'.htmlentities(trim($line)).'</span><span class="good">'.htmlentities(trim($work_line)).'</span></div>';
}
}
}
// empty files
else {
print "\t".'<h2 class="warning">'.$file.': Empty file</h2>'."\n";
}
// print "<div class=\"size\">($js_include_size bytes) -> ($minisize bytes)</div>";
// end outer file wrapper
print "</div>";
}
function parseCSSFile($file, $fp) {
global $css_include_size;
global $domain;
// global $doc_root;
global $css_output_path;
global $css_output_relative_paths;
global $variant;
global $escaped_variant;
// If includes start with //, then prefix with http:
if(preg_match("/^\/\//", $file)) {
$file = "http:" . $file;
}
// normalize file path
$file_fragments_raw = explode("/", $file);
$file_fragments = [];
foreach($file_fragments_raw as $folder) {
if($folder == "..") {
array_pop($file_fragments);
}
else {
array_push($file_fragments, $folder);
}
}
$file = implode("/", $file_fragments);
$file_content = @file_get_contents($file);
if($file_content === false) {
print '<div class="file bad">'."\n";
print "\t".'<h2 class="bad">'.$file.": Missing file</h2>\n";
print '</div>';
return;
}
$file_size = strlen($file_content);
$css_include_size += $file_size ? $file_size : 0;
$minisize = 0;
// file header
print '<div class="file">'."\n";
print "\t".'<h2 class="good" onclick="this.parentNode.className = this.parentNode.className.match(/open/) ? \'file\' : \'file open\'">'.$file."</h2>\n";
// get lines from file
$lines = file($file);
// if file has content
if(count($lines)) {
fwrite($fp, "\n");
fwrite($fp, "/*".basename($file)."*/\n");
// print $file_size;
$comment_switch = false;
$comment_block = "";
foreach($lines as $linenumber => $line) {
// adjustment string - modify this string, for reference in matches
$work_line = $line;
$include_line = false;
// Not reduced parts to keep
$but_keep = "";
if($work_line) {
// comment switch off and inline comment
// (is intended to catch any multi-inline comments,
// which might otherwise make the replacement pattern fail)
//
// replace one-liner comments, even if nested inside other string
// if(!$comment_switch && preg_match("/\/\*(^\*\/)+\*\//", $work_line)) {
if(!$comment_switch && preg_match("/\/\*[^$]+\*\//", $work_line)) {
$temp_line = $work_line;
while(strpos($temp_line, "/*") !== false && strpos($temp_line, "*/") !== false) {
// Figure out the nuances
$comment_start = strpos($temp_line, "/*");
$comment_end = strpos($temp_line, "*/")+2;
// $comment_block = substr($temp_line, $comment_start, $comment_end);
// debug(["inline comment:", $temp_line, $work_line, $comment_start, $comment_end]);
$inline_comment = substr($temp_line, $comment_start, $comment_end - $comment_start);
$temp_line = str_replace($inline_comment, "", $temp_line);
$comment_block .= $inline_comment."\n";
}
// Work line after replacing inline comments
$work_line = $temp_line;
}
// comment switch off
// found /* comment start
if(!$comment_switch && strpos($work_line, "/*") !== false) {
// print "Remove inline comment:".$temp_line . ", " . $work_line."<br>";
$com_s_pos = strpos($line, "/*");
$comment_switch = true;
// Save comment block for further investigation (look for "license")
$comment_block .= substr($line, $com_s_pos);
// get line content before comment starts (if any)
// $work_line = substr($line, 0, $com_s_pos);
$but_keep = substr($line, 0, $com_s_pos);
$work_line = substr($line, 0, $com_s_pos);
}
// comment switch on
// look for */ comment end
else if($comment_switch && strpos($work_line, "*/") !== false) {
$com_e_pos = strpos($line, "*/");
$comment_switch = false;
// Reset comment block
// $comment_block = "";
$comment_block .= substr($line, 0, $com_e_pos+2);
// get line content after comment is ended
$work_line = substr($line, $com_e_pos+2);
}
// comment switch is on, remove all content
else if($comment_switch) {
$comment_block .= $work_line;
$work_line = "";
// $work_line = $but_keep;
}
// reset comment start and end position
$com_s_pos = 0;
$com_e_pos = 0;
// check if line contains new include
if(!$comment_switch && preg_match("/@import url\(['\"]?([a-zA-Z0-9\.\/_\:\-\=\?]+)['\"]?\)/i", $work_line, $matches)) {
// debug(["matched include:",$matches[1]]);
$include_line = true;
// external include
if(preg_match("/(http[s]?:)?\/\//i", $matches[1])) {
$filepath = $matches[1];
}
// local, absolute include
else if(strpos($matches[1], "/") === 0) {
$filepath = $domain.$matches[1];
}
// relative include
// should be relative to current file
else {
$filepath = dirname($file)."/".$matches[1];
}
// Is import really a tracker rather than a CSS import?
if(preg_match("/^\/\//", $filepath) && !preg_match("/\.css$/", $filepath)) {
// debug(["I'm a tracker", $filepath]);
}
else {
$work_line = "";
// parse new include file
parseCSSFile($filepath, $fp);
}
// add whitespace
fwrite($fp, "\n");
}
if(!$comment_switch && $comment_block) {
if (preg_match("/license|copyright/i", $comment_block)) {
fwrite($fp, $comment_block."\n");
$minisize += strlen($comment_block);
// debug(["COMMENT BLOCK:", $comment_block]);
}
$comment_block = "";
}
if((trim($work_line) && !$comment_switch) || (trim($but_keep) && $comment_switch)) {
// Additional assets linking via stylesheet
// - Images and fonts
// if located out of default image/font (/img, /css or /assets) location,
// - it should be re-referenced and moved into assets folder
// if located in default location (/img, /css or /assets),
// - then update reference to match output file location
// Look for image references url(*)
// Look for font references url(*)
if(preg_match_all("/url\([\'\"]?([^\'\"\)]+)[\'\"]?\)/", $work_line, $assets)) {
// debug(["assets", $assets]);
if(count($assets) == 2) {
foreach($assets[1] as $asset) {
// debug(["asset", $asset, !preg_match("/^(http[s]?:)?\/\/([^\/]+)/i", $asset, $domains), $domains]);
// Don't change external assets references
// But do update url's with hardcoded "same domain"
// If it doesn't start width http, https or // OR has current domain in it
// Then it must be a local reference which should be included in the build
if(!preg_match("/^(http[s]?:)?\/\/([^\/]+)/i", $asset, $domains) || strpos($domains[0], $_SERVER["HTTP_HOST"]) !== false) {
// debug(["match:", $asset, $domains]);
// $asset_folder = explode("/", dirname(str_replace($_SERVER["DOCUMENT_ROOT"], "", $file)));
$asset_folder = explode("/", dirname(str_replace($domain, "", $file)));
// print "\nfile: $file<br>\n";
// print_r($asset_folder);
// make sure we have clean url (same url allowed)
$asset = preg_replace("/^(http[s]?:\/\/)+[^\/]+/", "", $asset);
$work_line = preg_replace("/^(http[s]?:\/\/)+[^\/]+/", "", $work_line);
// Create normalized absolute path for asset validation
// if path is not absolute
if(!preg_match("/^\//", $asset)) {
// Simplest url normalization
$reference = explode("/", $asset);
foreach($reference as $fragment) {
if($fragment == "..") {
array_shift($reference);
array_pop($asset_folder);
}
}
$normalized_asset = implode("/", $asset_folder) ."/". implode("/", $reference);
}
else {
$normalized_asset = $asset;
}
// Check that asset exists, before continuing asset processing
$header = get_headers($domain.$normalized_asset, 1);
// Asset doesn't exist
// print_r($header);
if(
!$header ||
!isset($header[0]) ||
!preg_match("/[123][0-9]{2}/", $header[0]) ||
!preg_match("/image|css|font/", (is_array($header["Content-Type"]) ? $header["Content-Type"][count($header["Content-Type"])-1] : $header["Content-Type"]))
) {
h2("$asset: Local asset missing", "bad");
}
// Asset exists
else {
// h2("normalized_asset:" . $normalized_asset);
// only consider moving assets that are not located in /img, /css or /assets
// or /#variant#/css, /#variant#/img or /#variant#/assets
if(
(!$variant && !preg_match("/^\/(css|img|assets)\//", $normalized_asset))
||
($variant && !preg_match("/^(".$escaped_variant."\/css|".$escaped_variant."\/img|".$escaped_variant."\/assets)\//", $normalized_asset))
) {
// identify asset type
// font asset
// Put everything in one bowl – and keep the original folderstructure to maintain separation
// TODO: weak spot for svg's - are they a graphic or a font
// (check for id for now - extrmely rarely used for graphic references)
if(preg_match("/\.(woff[2]?|eot|eot|ttf|svg|otf)([\?#][^$]+)?$/i", $normalized_asset)) {
$consolidated_asset = $variant."/assets".$normalized_asset;
}
// graphic asset
else if(preg_match("/\.(jpg|gif|png|svg)([\?#][^$]+)?$/i", $normalized_asset)) {
$consolidated_asset = $variant."/assets".$normalized_asset;
}
// unknown asset
else {
$consolidated_asset = $variant."/assets".$normalized_asset;
h2("$normalized_asset: Unknown asset", "warning");
}
// h2("consolidated_asset:" . $consolidated_asset);
// Copy asset
safeCopy($normalized_asset, $consolidated_asset);
}
else {
$consolidated_asset = $normalized_asset;
}
// Convert to relative paths
if($css_output_relative_paths) {
// Make relative asset url, to update CSS paths
// Remove css fragment (linking happens from css folder)
if(preg_match("/^(\/css|".$escaped_variant."\/css)\//i", $consolidated_asset)) {
$relative_asset = preg_replace("/^(\/css|".$escaped_variant."\/css)\//i", "", $consolidated_asset);
}
// Make path relative to css folder
else {
$relative_asset = "..".preg_replace("/^$escaped_variant/", "", $consolidated_asset);
}
// h2("relative_asset:" . $relative_asset);
// Update the css reference
$work_line = str_replace($asset, $relative_asset, $work_line);
}
// Use absolute paths
else {
// Update the css reference to normalized absolute path
$work_line = str_replace($asset, $consolidated_asset, $work_line);
}
}
}
// Check external url
// Don't process/consolidate external assets – but do check if they exist
else {
// Add http to // references
if(preg_match("/^\/\//", $asset)) {
$asset = "http:" . $asset;
}
// Try to get file, check response code
// Notify about missing files
$header = get_headers($asset, 1);
if(
!$header ||
!isset($header[0]) ||
!preg_match("/[123][0-9]{2}/", $header[0]) ||
!preg_match("/image|css|font/", (is_array($header["Content-Type"]) ? $header["Content-Type"][count($header["Content-Type"])-1] : $header["Content-Type"]))
) {
h2("$asset: External asset missing", "bad");
}
}
}
}
}
fwrite($fp, $work_line);
$minisize += strlen($work_line);
}
}
// output result/stats of parsing
if(!$comment_switch && (trim($work_line) && trim($line) == trim($work_line))) {
print "\t".'<div class="notminified"><code>'.$linenumber.':'.htmlentities($line).'</code></div>';
}
else if(!$include_line) {
print "\t".'<div class="minified"><span class="bad">'.$linenumber.':'.htmlentities(trim($line)).'</span><span class="good">'.htmlentities(trim($work_line)).'</span></div>';
}
}
}
// empty files
else {
print "\t".'<h2 class="warning">'.$file.': Empty file</h2>'."\n";
}
// $_ .= $source . " ($include_size bytes) -> " . $file_output[$index] . " (".filesize($file_output[$index])." bytes)<br />";
// $_ .= count($includes) . " include files<br /><br />";
print "</div>";
}