-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.php
331 lines (266 loc) · 8.43 KB
/
install.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
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
<?php
/*
* cgallery v2.3
*
* install.php:
*
* this is a simple, command-line php file that can be used to generate a
* gallery of albums and series based on directory structure.
*
* arguments:
* -p[ath to image dir]
* -m[ode] static|dynamic|local|uninstall default: static
* -t[ype] series|album|auto default: auto
* -d[elete thumbnails]
* -s[ource directory] default:`cwd`
*/
function err($msg, $code = 999) {
if ($code == "quiet") {
print "\naborted.\n\n";
exit(998);
}
print "\n";
print "ERROR: $msg\n\n";
print "required:\n";
print " -p[ath to image dir]\n\n";
print "optional:\n";
print " -m[ode] static|dynamic|local|uninstall default: static\n";
print " -t[ype] series|album|auto default: auto\n";
print " -d[elete thumbnails]\n";
print " -s[ource directory] default:`cwd`\n\n";
print "aborted.\n\n";
exit($code);
}
/* ugh, http://stackoverflow.com/questions/1091107/how-to-join-filesystem-path-strings-in-php.
this is a path join function, it will basically trim up and slash delimit the args */
function path() {
return strip(preg_replace('~[/\\\]+~', DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, array_filter(func_get_args(), function($p) {
return $p !== '';
}))));
}
/* resolves a full path, then follows the symlink if applicable */
function resolve($path) {
$path = realpath($path);
if (is_link($path)) {
$path = readlink($path);
}
return $path;
}
function strip($str) {
return trim(preg_replace('/\s\s+/', ' ', $str));
}
function check_rm($file) {
if (is_dir($file)) {
rmdir($file);
return;
}
if (file_exists($file)) {
print " deleting " . $file . "\n";
unlink($file);
}
}
function exec_to_stdout($cmd) {
/* http://stackoverflow.com/questions/8370628/php-shell-exec-with-realtime-updating */
if(($fp = popen($cmd, "r"))) {
while(!feof($fp)){
echo fread($fp, 1024);
flush();
}
fclose($fp);
}
}
$dirstack = array();
function pushdir($dir) {
global $dirstack;
array_push($dirstack, getcwd());
chdir($dir);
}
function popdir() {
global $dirstack;
$dir = array_pop($dirstack);
chdir($dir);
return $dir;
}
function delete_if_auto_series($dir) {
$f = path($dir, '.series');
if (is_file($f)) {
if (strip(file_get_contents($f)) == "auto") {
check_rm($f);
}
}
}
function write_auto_series($dir) {
$f = path($dir, '.series');
if (!is_file($f)) {
print " writing .series file to $f\n";
file_put_contents($f, "auto");
}
}
/* install the specified directory as an album. will use album.php
to generate a static index.html file */
function install_album($dir) {
$html = path($dir, "index.html");
$php = path($dir, "index.php");
global $album;
global $rethumb;
global $mode;
pushdir($dir);
$thumbs = path($dir, ".thumbs");
if ($rethumb && is_dir($thumbs)) {
pushdir($thumbs);
foreach (preg_grep('/(\.jpg|\.png|\.gif)$/i', glob("*")) as $thumb) {
check_rm($thumb);
}
popdir();
}
check_rm($thumbs);
check_rm($html);
check_rm($php);
if ($mode == "static" || $mode == "local") {
print " generating thumbnails to $thumbs using $album\n";
exec_to_stdout("php $album -t 1"); /* create thumbnails (and log to stdout) */
exec("php $album -m $mode > index.html"); /* generate html */
print " created $mode index.html file\n\n";
}
else if ($mode == "dynamic") {
exec("php $album");
print " linking $album to $php\n";
print " note: no thumbnails will be generated until the next page visit\n\n";
symlink($album, $php);
}
popdir();
}
/* install the specified directory as a series. will use series.php
to generate a static index.html file */
function install_series($dir) {
$html = path($dir, "index.html");
$php = path($dir, "index.php");
global $series;
global $rethumb;
global $mode;
pushdir($dir);
/* install each sub-directory. install will figure out if the
specified directory is an album or a nested series. IMPORTANT,
this must occur before generating the index file so subseries
can be marked correctly */
foreach (glob("*", GLOB_ONLYDIR) as $current) {
$current = path($dir, $current);
$type = guess_type($current);
install($current, $type);
}
check_rm($html);
check_rm($php);
if ($mode == "static" || $mode == "local") { /* generate a static html file. this
will implicitly trigger a thumbnail refresh */
print " generating $mode $html using $series\n";
exec("php $series -m $mode > $html"); /* ugh, but it works and is easy */
}
else if ($mode == "dynamic") {
print " linking $series to $php\n";
symlink($series, $php);
}
if ($mode !== "uninstall") {
write_auto_series($dir);
}
popdir();
}
/* given a directory, installs an album or series */
function install($dir, $override = null) {
$type = file_exists(path($dir, ".series")) ? "series" : "album";
print "type: " . $type . "\n";
print " path: " . $dir . "\n";
/* use override if specified, otherwise we'll
default to album. */
$type = $override ?: $type;
delete_if_auto_series($dir);
if ($dir[0] != ".") {
if ($type == "series") {
install_series($dir);
}
else {
install_album($dir);
}
}
}
function guess_type($dir) {
if (is_file(path($dir, '.series'))) {
return "series";
}
$type = "album";
pushdir($dir);
/* if the specified directory has any sub-directories with
images, assume series. otherwise, album */
foreach (glob("*", GLOB_ONLYDIR) as $current) {
if ($current[0] != ".") {
$current = path($dir, $current);
pushdir($current);
$images = preg_grep('/(\.jpg|\.png|\.gif)$/i', glob("*"));
popdir();
if (count($images) > 0) {
$type = "series";
break;
}
}
}
popdir();
return $type;
}
/* these are the input arguments we accept */
$options = getopt("s:p:dm:t:");
/* resolve working paths */
$src = realpath($options["s"] ?: ".");
$rethumb = array_key_exists("d", $options);
$dst = resolve(realpath($options["p"]));
$mode = $options["m"] ?: "static";
$type = $options["t"] ?: "auto";
$validModes = array("dynamic", "static", "local", "uninstall");
if (!in_array($mode, $validModes)) {
err("'-m $mode' is not valid. please use " . implode('|', $validModes));
}
if (!$options["p"]) {
err("-p is required, please specify a target directory.");
}
if (!is_dir($dst)) {
err("specified path $dst is invalid");
}
if ($type == "auto") {
$type = guess_type($dst);
}
$validTypes = array("album", "series");
if (!in_array($type, $validTypes)) {
err("'-t $type' is not valid. please use " . implode('|', $validTypes));
}
/* check env */
$album = resolve(path($src, "album.php"));
$series = resolve(path($src, "series.php"));
if (!file_exists($album) || !file_exists($series)) { /* src dir invalid */
err("album.php or series.php not found in $src", 2);
}
if (!is_dir($dst)) { /* target dir invalid */
err("target path $dst does not appear to be a directory", 3);
}
/* show configuration to the user */
print "\n";
print "gallery will be installed as follows:\n\n";
print " install dir : " . $options["p"] . "\n";
print " mode : " . $mode . "\n";
print " type : " . $type . "\n";
print " delete thumbs : " . ($rethumb ? "true" : "false") . "\n";
print " source dir : " . $src . "\n\n";
$warndir = $options["p"];
print "warning: this will recursively delete/replace all index.html and index.php files inside '$warndir'. ";
if ($rethumb) {
print "all images contained within any '.thumbs' sub-directories will also be deleted.";
}
print "\n\nconfirm? y/n ";
/* give the user a chance to back out */
$stdin = fopen('php://stdin', 'r');
$confirm = strip(fgets($stdin));
if ($confirm != "y") { /* must be exactly 'y' */
err("user canceled", "quiet");
}
print("\n");
/* kick the whole thing off! */
install($dst, $type);
print("successful\n\n");
?>