-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminprep.php
125 lines (104 loc) · 3.81 KB
/
minprep.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
<?php
/*
Prepare for Minimization - minprep
This script will open a specified file and search
its contents for CSS links and JS file script tags.
The file can be: *.html, *.php
For each type of file it finds the contents are read
in and then appended to a single CSS or JS file.
NOTES:
Commented out lines will be ignored. But they
must be commented out correctly. The comment MUST
start and end on the same line as the link or script
tags.
Links or source tags that have "http://" or "https://"
in them will also be ignored. And source tags with
"jquery" will also be ignored.
Be sure to verify that the files found match what
is expected. Just in case something was not commented
correctly.
For additional details: https://github.com/jxmot/minimize-prep/#readme
*/
$minprep = json_decode(file_get_contents('./minprep.json'));
if(file_exists($minprep->fileroot.$minprep->input) === false) {
echo 'ERROR ' . $minprep->fileroot.$minprep->input . ' does not exist!';
die();
}
// for silent running set to false
$_dbgmp = $minprep->verbose;
require_once 'minimize-prep.php';
if($_dbgmp) {
echo "Starting preparation...\n";
echo "Input: {$minprep->fileroot}{$minprep->input}\n";
echo "Files Root Path: {$minprep->fileroot}\n";
echo "{$minprep->fileroot}{$minprep->cssout} and {$minprep->fileroot}{$minprep->jsout} will be overwritten.\n\n";
}
// open output files and input file...
$cssout = @fopen($minprep->fileroot . $minprep->cssout, 'w');
$jsout = @fopen($minprep->fileroot . $minprep->jsout, 'w');
$htmlin = @fopen($minprep->fileroot . $minprep->input, 'r');
// Create the script to remove resources?
if($minprep->mkremove === true) {
if($_dbgmp) echo "Creating {$minprep->rmvscript} file\n\n";
$bashout = @fopen($minprep->rmvscript, 'w');
fwrite($bashout, "#!/bin/bash\n");
} else if($_dbgmp) echo "\n";
// read lines until the end...
while(!feof($htmlin)) {
$hline = fgets($htmlin);
$fpath = '';
// parse the line...
// link?
if(($r = isLink($hline)) > 0) {
// it's a link...
$offset = $r + 6;
// is it excluded?
if(isExcluded($hline, $minprep->cssexclude)) {
if($_dbgmp) echo 'Excluded - ' . ltrim($hline);
continue;
}
// get the full path
$fpath = getFilePath(getURL($hline, $offset), $minprep->fileroot);
// transfer the contents...
if(putContents($fpath, $cssout)) {
if($_dbgmp) echo "Found - " . getURL($hline, $offset) . "\n";
if($minprep->mkremove === true) {
fwrite($bashout, "rm -f {$fpath}\n");
}
} else {
echo "ERROR File Not Found: {$fpath}\n";
}
} else {
if($r === false) continue;
else {
if(($r = isScript($hline)) > 0) {
// it's a script
$offset = $r + 5;
if(isExcluded($hline, $minprep->jsexclude)) {
if($_dbgmp) echo 'Excluded - ' . ltrim($hline);
continue;
}
$fpath = getFilePath(getURL($hline, $offset), $minprep->fileroot);
if(putContents($fpath, $jsout)) {
if($_dbgmp) echo "Found - " . getURL($hline, $offset) . "\n";
if($minprep->mkremove === true) {
fwrite($bashout, "rm -f {$fpath}\n");
}
} else {
echo "ERROR File Not Found: {$fpath}\n";
}
}
}
}
}
fflush($cssout);
fclose($cssout);
fflush($jsout);
fclose($jsout);
fclose($htmlin);
if($minprep->mkremove === true) {
fflush($bashout);
fclose($bashout);
}
if($_dbgmp) echo "\nPreparation Complete.\n";
?>