-
Notifications
You must be signed in to change notification settings - Fork 0
/
proclame.php
179 lines (147 loc) · 4.76 KB
/
proclame.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
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
<?php
# Copyright 2014 Kevin Lagaisse
#
# Licensed under the Creative Commons BY-NC-SA 4.0 licence (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://creativecommons.org/licenses/by-nc-sa/4.0/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
//helper function
function pretify_uri($uri,$root) {
$aux=$uri;
$aux = str_replace($root,"",$aux);
$aux = str_replace(DIRECTORY_SEPARATOR,"/",$aux);
return $aux;
}
function usage($programName) {
$usage[]= "\n";
$usage[]= "Usage : php ${programName} PROJECTPATH [CONFPATH]" ;
$usage[]= "" ;
$usage[]= "Startup :" ;
$usage[]= " PROJECTPATH : your project directory where the website is stored" ;
$usage[]= " CONFPATH : path to the config file appcache.json" ;
$usage[]= " default : appcache.json in your project path" ;
echo implode(PHP_EOL, $usage);
exit(1);
}
if ($argc<=1) {
usage($argv[0]);
}
if ($argc>=2) {
$projectpath=$argv[1];
}
if (false===realpath($projectpath)) {
echo "Projectpath not found. exiting...".PHP_EOL;
exit(1);
}
$projectpath=realpath($projectpath);
if (!is_dir($projectpath)) {
echo "Projectpath not a dir. exiting...".PHP_EOL;
exit(1);
}
$projectpath=$projectpath.DIRECTORY_SEPARATOR;
if ($argc==3) {
$config=$argv[2];
}
else {
$config=$projectpath."appcache.json";
}
if (false===realpath($config)) {
echo "Config file not found. in ".$config." exiting...".PHP_EOL;
exit(1);
}
$c=json_decode(file_get_contents(realpath($config)));
if ($c==null||$c===false) {
echo "Config file : bad json. exiting...".PHP_EOL;
exit(1);
}
if (!isset($c->exclude)) {
echo "No file exclusion".PHP_EOL;
}
if (!isset($c->include)) {
echo "No additional url to include".PHP_EOL;
}
if (!isset($c->network) || !in_array("*",$c->network)) {
echo "No * in network resources".PHP_EOL;
$c->network[] = "*";
}
if (!isset($c->fallback)) {
echo "No fallback to add".PHP_EOL;
}
if (!isset($c->manifest)) {
echo "No manifest file. Using default : manifest.appcache".PHP_EOL;
$c->manifest="manifest.appcache";
}
$c->exclude[]=$c->manifest;
$c->exclude[]=$config;
//Listing all files in the project directory
$directory = new RecursiveDirectoryIterator($projectpath, RecursiveDirectoryIterator::SKIP_DOTS);
$flattened = new RecursiveIteratorIterator($directory,
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);
$i = 0;
$filter = array();
$filter[$i] = $flattened;
/*******************************************\
Managing the cache section by filtering
the the files using the exclusion part
of the config file
\*******************************************/
foreach ($c->exclude as $ckey => $cvalue) {
if(substr($cvalue, -1) == "/") { //we have a directory
$cvalue = substr($cvalue, 0, -1);
$cvalue = $projectpath.$cvalue;
}
$cvalue = str_replace("/",DIRECTORY_SEPARATOR,$cvalue);
$cvalue = preg_quote($cvalue);
$cvalue = str_replace("\*",".+",$cvalue);
$filter[$i+1] = new RegexIterator($filter[$i], "#^((?!".$cvalue.").)+$#sDi");
$i++;
}
//getting the files from the filtered directory iterator
$cachelist[] = "\n# Cache list";
foreach($filter[$i] as $entry) {
if (!$entry->isDir()) {
$cachelist[] = pretify_uri($entry->getPathname(),$projectpath).(($entry->isDir())?"/":"") ;
}
}
$include[]="#path to include";
if (isset($c->include)) {
foreach ($c->include as $ikey => $ivalue) {
$include[]=$ivalue;
}
}
/*******************************************\
Writing the appcache file
\*******************************************/
$entete[]="CACHE MANIFEST";
$entete[]="# DO NOT EDIT : generated by proclame.php";
$entete[]="# ".gmdate("Y-m-d")." at ".time();
//cachelist done before...
$fallback[]="\nFALLBACK:";
if (isset($c->fallback)) {
foreach ($c->fallback as $fkey => $fvalue) {
$fallback[]=$fvalue->url ." ".$fvalue->fb;
}
}
$network[]="\nNETWORK:"; //use from network if available
if (isset($c->network)) {
foreach ($c->network as $nkey => $nvalue) {
$network[]=$nvalue;
}
}
//writing everything to file
$ret=file_put_contents($projectpath.$c->manifest,implode("\n",array_merge($entete,$cachelist,$include,$fallback,$network)));
if ($ret===false) {
echo "Error while writing the manifest file. exiting...";
exit(1);
}
echo "Manifest File has been generated to ". $projectpath.$c->manifest . PHP_EOL;
exit(0);