Mercurial > hg > fapweb
annotate faptool.php @ 969:26ae3f21a3bb
Moar work on previews.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 29 Nov 2014 10:13:20 +0200 |
parents | c71afc1a3a85 |
children | 9c93a3c4f95c |
rev | line source |
---|---|
827 | 1 #!/usr/bin/php |
2 <? | |
943 | 3 // |
4 // FAPWeb - Simple Web-based Demoparty Management System | |
5 // Special entry/preview/etc backend management commandline utility | |
6 // (C) Copyright 2012-2014 Tecnic Software productions (TNSP) | |
7 // | |
827 | 8 require_once "mconfig.inc.php"; |
9 require_once "msite.inc.php"; | |
10 | |
11 | |
12 | |
882 | 13 function wtConvertImage($inFilename, $inFileType, $outFilename, $setDim, $setFormat, $setQuality, $thumb) |
14 { | |
15 global $setPreviewPath; | |
912 | 16 $isTemp = FALSE; |
882 | 17 |
884 | 18 if ($inFileType == "gfx") |
19 { | |
20 // Oh great .. we need gfxconv here because Imagick handles ILBM like shit | |
951 | 21 $tmpFilename = tempnam($setPreviewPath, "tmp"); |
912 | 22 $isTemp = TRUE; |
23 if (wtExec( | |
884 | 24 "/usr/local/bin/gfxconv", |
951 | 25 escapeshellarg($inFilename)." -f png -o ".escapeshellarg($tmpFilename)) === false) |
912 | 26 return FALSE; |
27 | |
951 | 28 if (!file_exists($tmpFilename)) |
912 | 29 { |
30 echo "ERROR: gfxconv did not output a temporary conversion inbetween.\n"; | |
31 return FALSE; | |
32 } | |
884 | 33 } |
34 else | |
951 | 35 $tmpFilename = $inFilename; |
884 | 36 |
882 | 37 // convert -resize 640x480 -background black -gravity center -extent 640x480 |
38 // -unsharp "0x0.75+0.75+0.008" lol.lbm -quality 95 test.jpg | |
884 | 39 |
40 // Create conversion entity | |
951 | 41 $img = new Imagick($tmpFilename); |
884 | 42 if ($img === false) |
912 | 43 { |
951 | 44 echo "ERROR: Oh noes! ImageMagick could not digest the file '".$tmpFilename."' (".$inFilename.")\n"; |
912 | 45 return FALSE; |
46 } | |
884 | 47 |
963 | 48 if ($setDim !== FALSE) |
884 | 49 { |
963 | 50 if (($outDim = stGetSetting($setDim)) === FALSE || |
51 ($outFormat = stGetSetting($setFormat)) === FALSE || | |
52 ($outQuality = stGetSetting($setQuality)) === FALSE) | |
53 { | |
54 die("Missing one of res/format/quality settings for '".$outFilename."'\n"); | |
55 } | |
56 | |
57 // Get dimensions, setup background | |
58 $dim = $img->getImageGeometry(); | |
969 | 59 // $img->setBackgroundColor(imagick::COLOR_BLACK); |
963 | 60 $img->setGravity(imagick::GRAVITY_CENTER); |
61 | |
62 // Act based on image size vs. desired size and $thumb mode | |
63 if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1]) | |
64 { | |
65 // Image is larger | |
66 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_QUADRATIC, 1); | |
969 | 67 $img->setImageExtent($outDim[0], $outDim[1]); |
963 | 68 $img->normalizeImage(); |
69 $img->unsharpMaskImage(0, 0.5, 1, 0.05); | |
70 } | |
71 else | |
72 if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1]) | |
73 { | |
74 // Image is smaller than requested dimension(s)? | |
75 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_POINT, 1); | |
969 | 76 $img->setImageExtent($outDim[0], $outDim[1]); |
963 | 77 } |
884 | 78 } |
79 | |
80 $img->setFormat($outFormat); | |
81 $img->setCompressionQuality($outQuality); | |
82 | |
83 $img->stripImage(); | |
84 $img->writeImage($outFilename); | |
85 $img->removeImage(); | |
882 | 86 } |
87 | |
88 | |
898 | 89 function wtRenderSample($inFilename, $outFilename) |
882 | 90 { |
898 | 91 $sfreq = intval(stGetSetting("sampleFreq")); |
92 $sduration = intval(stGetSetting("sampleDuration")); | |
93 $schannels = intval(stGetSetting("sampleChannels")); | |
882 | 94 |
902 | 95 return wtExec( |
898 | 96 "/usr/local/bin/openmpt123" |
97 , | |
954 | 98 "--force --quiet ". |
884 | 99 "--samplerate ".$sfreq." ". |
100 "--channels ".$schannels." ". | |
101 "--playtime ".$sduration." ". | |
954 | 102 escapeshellarg($inFilename)." -o ".escapeshellarg($outFilename), |
963 | 103 0); |
882 | 104 } |
105 | |
106 | |
948
1334b93939a3
Change how output options are handled in wtConvertSample().
Matti Hamalainen <ccr@tnsp.org>
parents:
947
diff
changeset
|
107 function wtConvertSample($inFilename, $outFilename, $outOpts) |
882 | 108 { |
898 | 109 $sfreq = intval(stGetSetting("sampleFreq")); |
110 $sduration = intval(stGetSetting("sampleDuration")); | |
111 $schannels = intval(stGetSetting("sampleChannels")); | |
882 | 112 |
948
1334b93939a3
Change how output options are handled in wtConvertSample().
Matti Hamalainen <ccr@tnsp.org>
parents:
947
diff
changeset
|
113 foreach ($outOpts as $okey => $oval) |
898 | 114 $optStr .= $okey." ".$oval." "; |
939 | 115 |
902 | 116 return wtExec( |
898 | 117 "/usr/local/bin/avconv", |
118 "-y -t ".intval($sduration)." ". | |
119 "-i ".escapeshellarg($inFilename). | |
120 " ".$optStr." -ac ".$schannels. | |
121 " -map_metadata -1 ". | |
122 escapeshellarg($outFilename)); | |
882 | 123 } |
124 | |
125 | |
969 | 126 function wtEntryToSource($compo, $inFilename, $edata, $entryFilename, &$outData, &$outFilename) |
882 | 127 { |
963 | 128 // Does compo preview type match this file class? |
129 if ($edata["class"] != $compo["preview_type"]) | |
130 return FALSE; | |
954 | 131 |
969 | 132 switch ($edata["class"]) |
133 { | |
134 case EFILE_AUDIO: $fext = "wav"; break; | |
135 case EFILE_IMAGE: $fext = "png"; break; | |
136 default: | |
137 return FALSE; | |
138 } | |
139 | |
140 $outFilename = stReplaceFileExt($entryFilename, "_preview.".$fext); | |
963 | 141 if (file_exists($outFilename) && filemtime($outFilename) >= filemtime($entryFilename)) |
969 | 142 return TRUE; |
963 | 143 |
969 | 144 printf("GENERATING: %s -> %s\n", $inFilename, $outFilename); |
145 | |
954 | 146 switch ($edata["class"]) |
939 | 147 { |
954 | 148 case EFILE_AUDIO: |
149 if ($edata["mime"] == "audio/x-mod") | |
963 | 150 $res = wtRenderSample($inFilename, $outFilename); |
954 | 151 else |
969 | 152 $res = wtConvertSample($inFilename, $outFilename, |
153 array("-f" => "wav")); | |
954 | 154 break; |
963 | 155 |
954 | 156 case EFILE_IMAGE: |
963 | 157 $res = wtConvertImage( |
158 $inFilename, | |
159 $edata["mime"], | |
160 $outFilename, | |
161 FALSE, "PNG", 9, FALSE); | |
162 break; | |
939 | 163 } |
963 | 164 |
165 return $res; | |
882 | 166 } |
167 | |
168 | |
902 | 169 function wtUnpackArchiveTo($atype, $filename, $path) |
882 | 170 { |
171 // Check file type before doing anything | |
953 | 172 echo "INFO: Preparing to unpack archive file '".$filename."' ...\n"; |
902 | 173 switch ($atype) |
882 | 174 { |
175 case "LHA": | |
176 $exe = "/usr/bin/lha"; | |
967 | 177 $args = "x ".escapeshellarg($filename); |
882 | 178 break; |
179 | |
180 case "ZIP": | |
181 $exe = "/usr/bin/unzip"; | |
182 $args = "-d ".escapeshellarg($path)." ".escapeshellarg($filename); | |
183 break; | |
184 | |
898 | 185 case "RAR": |
186 $exe = "/usr/bin/rar"; | |
968 | 187 $args = "x -w".escapeshellarg($path)." ".escapeshellarg($filename); |
188 break; | |
189 | |
190 case "7ZIP": | |
191 $exe = "/usr/bin/7z"; | |
192 $args = "x -o".escapeshellarg($path)." ".escapeshellarg($filename); | |
898 | 193 break; |
194 | |
882 | 195 default: |
902 | 196 echo "Unsupported archive file type: ".$atype."\n"; |
898 | 197 return FALSE; |
882 | 198 } |
199 | |
200 // Create temporary directory | |
201 wtPurgeDir($path); | |
935 | 202 wtMakeDir($path, 0700); |
882 | 203 |
204 if (!is_dir($path) || chdir($path) === false) | |
902 | 205 { |
953 | 206 echo "ERROR: Failed to chdir to '".$path."', can't unpack archive.\n"; |
902 | 207 return FALSE; |
208 } | |
882 | 209 |
210 // Unpack archive | |
902 | 211 return wtExec($exe, $args); |
212 } | |
213 | |
214 | |
933 | 215 function wtNiceName($compo, $entry, $efile = FALSE) |
216 { | |
217 return sprintf( | |
218 "%d: %s by %s%s", | |
219 $entry["id"], | |
220 $entry["name"], | |
221 $entry["author"], | |
222 ($efile !== false) ? " [".$efile["filename"]." / TYPE: '".$efile["filetype"]."']" : ""); | |
223 } | |
224 | |
225 | |
882 | 226 function wtHandleEntryPreview($compo, $entry, $mode) |
227 { | |
941 | 228 global $fileTypeData, $setEntryPath; |
933 | 229 |
230 // Get current preview file(s) data | |
898 | 231 if (!stGetPreviewFileData($compo, $entry, $pdata)) |
232 return FALSE; | |
233 | |
933 | 234 // Get entry file data |
963 | 235 $efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"]); |
898 | 236 |
237 // Check preview file(s) status | |
949
2179600710d1
Add "lis" as alias to "sta".
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
238 if ($mode == "sta" || $mode == "lis") |
898 | 239 { |
240 printf(" %03d | %s%s%s | %-40s | %-5s | %s\n", | |
241 $entry["id"], | |
933 | 242 ($efile !== false) ? "E" : ".", |
243 isset($pdata["file"]) ? "P" : ".", | |
244 $pdata["valid"] ? "V" : ".", | |
245 substr($entry["name"]." by ".$entry["author"], 0, 40), | |
898 | 246 isset($pdata["file"]) ? $pdata["file"]["filetype"] : "", |
247 isset($pdata["file"]) ? $pdata["file"]["filename"] : "" | |
248 ); | |
954 | 249 return TRUE; |
898 | 250 } |
251 else | |
933 | 252 if ($mode != "upd") |
953 | 253 die("ERROR: Unsupported previews mode ".$mode."\n"); |
933 | 254 |
255 // Check validity of previews | |
256 if ($pdata["valid"]) | |
257 return TRUE; | |
258 | |
259 // Okay, no valid previews .. lets see what we can do .. | |
260 // First, check if we have a source preview file | |
261 if (!isset($pdata["file"])) | |
898 | 262 { |
935 | 263 // No source preview, check if we have entry file either? |
264 if ($efile === false) | |
265 { | |
266 echo | |
267 "INFO: No entry file for ".wtNiceName($compo, $entry, $efile). | |
268 ", can't attempt to generate preview.\n"; | |
269 return FALSE; | |
270 } | |
954 | 271 |
272 // Actual entry filename + path | |
273 $filename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $efile["filename"])); | |
274 if (!file_exists($filename)) | |
275 { | |
276 echo "ERROR: Entry file '".$filename."' for ".wtNiceName($compo, $entry, FALSE)." does not exist!\n"; | |
277 return FALSE; | |
278 } | |
279 | |
935 | 280 // Preview source file does not exist, let's see .. |
963 | 281 $edata = stProbeFileInfo($filename, TRUE); |
282 if ($edata === false) | |
882 | 283 { |
935 | 284 echo |
285 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n"; | |
286 return FALSE; | |
287 } | |
288 | |
942 | 289 $found = FALSE; |
935 | 290 if ($edata["class"] == EFILE_ARCHIVE) |
291 { | |
292 // Entry is an archive file .. | |
963 | 293 $path = stMakePath(FALSE, FALSE, array($setEntryPath, "UNPACKS", $efile["filename"])); |
941 | 294 |
953 | 295 echo "INFO: Entry file is an ".$efile["filetype"]." archive, scanning '".$filename."' ...\n"; |
941 | 296 |
297 if (wtUnpackArchiveTo($efile["filetype"], $filename, $path) === false) | |
298 return FALSE; | |
935 | 299 |
941 | 300 // Scan through files ... |
301 $dir = opendir($path); | |
302 while (($dentry = readdir($dir)) !== false) | |
303 { | |
304 $fname = $path."/".$dentry; | |
954 | 305 // check file type against entry's preview_type |
306 // if match, render / convert / cp to as source and exit loop. | |
307 if (is_file($fname) && | |
308 ($mdata = stProbeFileInfo($fname, TRUE)) !== false && | |
969 | 309 ($found = wtEntryToSource($compo, $fname, $mdata, $filename, $pdata, $outFilename)) === true) |
954 | 310 break; |
941 | 311 } |
954 | 312 |
313 // Cleanup | |
941 | 314 closedir($dir); |
315 wtPurgeDir($path); | |
954 | 316 } |
317 else | |
318 if ($edata["class"] == $compo["preview_type"]) | |
319 { | |
320 // Single file | |
969 | 321 $found = wtEntryToSource($compo, $filename, $edata, $filename, $pdata, $outFilename); |
954 | 322 } |
969 | 323 |
324 printf("X: %d, %d\n", $edata["class"], $compo["preview_type"]); | |
325 print_r($edata); | |
954 | 326 |
950 | 327 if (!$found) |
882 | 328 { |
935 | 329 echo "WARNING: Could not generate preview from entry ".wtNiceName($compo, $entry, $efile)."\n"; |
330 return FALSE; | |
898 | 331 } |
969 | 332 |
333 $inFilename = $outFilename; | |
898 | 334 } |
969 | 335 else |
336 $inFilename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $pdata["file"]["filename"])); | |
898 | 337 |
935 | 338 // Either we now have a sourcefile or generated file |
969 | 339 switch ($compo["preview_type"]) |
941 | 340 { |
963 | 341 case EFILE_AUDIO: |
950 | 342 foreach (stGetSetting("sampleTypes") as $stype => $sopts) |
942 | 343 { |
950 | 344 wtConvertSample( |
345 $inFilename, | |
969 | 346 $pdata["files"][$stype]["file"], |
950 | 347 $sopts |
348 ); | |
942 | 349 } |
350 break; | |
969 | 351 |
963 | 352 case EFILE_IMAGE: |
969 | 353 wtConvertImage( |
354 $inFilename, | |
355 "png", | |
356 $pdata["files"]["image"]["file"], | |
942 | 357 "previewImageSize", "previewImageType", |
358 "previewImageQuality", FALSE); | |
882 | 359 |
969 | 360 wtConvertImage( |
361 $inFilename, | |
362 "png", | |
363 $pdata["files"]["thumb"]["file"], | |
942 | 364 "previewThumbSize", "previewThumbType", |
365 "previewThumbQuality", TRUE); | |
366 break; | |
941 | 367 } |
963 | 368 |
933 | 369 return TRUE; |
882 | 370 } |
371 | |
372 | |
884 | 373 function wtHandleEntry($compo, $entry, $mode) |
374 { | |
375 } | |
376 | |
377 | |
963 | 378 function wtUnpackEntry($compo, $entry, $pathPrefix, $useOrig = TRUE) |
379 { | |
380 global $setEntryPath; | |
381 | |
382 if (($efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"])) === false) | |
383 { | |
384 echo "INFO: No entry file for ".wtNiceName($compo, $entry, $efile)."\n"; | |
385 return FALSE; | |
386 } | |
387 | |
388 $filename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $efile["filename"])); | |
389 if (!file_exists($filename)) | |
390 { | |
391 echo "ERROR: Entry file '".$filename."' for ".wtNiceName($compo, $entry, FALSE)." does not exist!\n"; | |
392 return FALSE; | |
393 } | |
394 | |
395 $edata = stProbeFileInfo($filename, TRUE); | |
396 if ($edata === false) | |
397 { | |
398 echo | |
399 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n"; | |
400 return FALSE; | |
401 } | |
402 | |
403 if (wtMakeDir(stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"])), 0755) === false) | |
404 return FALSE; | |
405 | |
964 | 406 $dstFileBase = $entry["show_id"]."--".($useOrig ? $efile["origname"] : $efile["filename"]); |
963 | 407 |
408 if ($edata["class"] == EFILE_ARCHIVE) | |
409 { | |
410 // Entry is an archive file .. | |
411 $dstPath = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], stReplaceFileExt($dstFileBase, ""))); | |
964 | 412 if (wtUnpackArchiveTo($edata["id"], $filename, $dstPath) === false) |
963 | 413 return FALSE; |
414 } | |
415 else | |
416 { | |
417 $dstFilename = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], $dstFileBase)); | |
418 if (copy($filename, $dstFilename) === false) | |
419 { | |
420 echo "Failed to copy '".$filename."' to '".$dstFilename."'\n"; | |
421 return FALSE; | |
422 } | |
423 } | |
424 | |
425 return TRUE; | |
426 } | |
427 | |
428 | |
827 | 429 // |
941 | 430 // Misc helper functions |
827 | 431 // |
941 | 432 function wtExec($exe, $args, $expect = 0) |
433 { | |
953 | 434 echo "EXEC: ".$exe." ".$args."\n"; |
941 | 435 exec(escapeshellcmd($exe)." ".$args, $output, $code); |
436 if ($code !== $expect) | |
437 { | |
438 echo | |
953 | 439 "ERROR: Executing ".$exe.": ".$code."\n".$args."\n". |
941 | 440 "------------------------------------------------\n". |
441 implode("\n", $output). | |
442 "------------------------------------------------\n"; | |
443 return FALSE; | |
444 } | |
445 return TRUE; | |
446 } | |
447 | |
448 | |
449 function wtExecOrDie($exe, $args) | |
450 { | |
451 if (wtExec($exe, $args) === false) | |
452 die(); | |
453 } | |
454 | |
455 | |
456 function wtPurgeDir($path) | |
457 { | |
942 | 458 if ($path != "" && $path !== false && file_exists($path) && is_dir($path)) |
941 | 459 { |
969 | 460 // echo "PURGING: ".$path."\n"; |
942 | 461 foreach (scandir($path) as $file) |
462 if ($file !== "." && $file !== "..") | |
463 { | |
464 $sub = $path."/".$file; | |
465 if (is_dir($sub)) | |
466 wtPurgeDir($sub); | |
467 else | |
468 unlink($sub); | |
469 } | |
470 rmdir($path); | |
941 | 471 } |
472 } | |
473 | |
474 | |
935 | 475 function wtMakeDir($path, $perm) |
827 | 476 { |
477 if (!file_exists($path)) | |
478 { | |
953 | 479 echo "INFO: Creating ".$path."\n"; |
827 | 480 if (mkdir($path, $perm, TRUE) === false) |
481 die("Could not create directory '".$path."'\n"); | |
482 } | |
483 } | |
484 | |
485 | |
935 | 486 function wtInitializeDirs() |
827 | 487 { |
902 | 488 global |
489 $setEntryPath, $setPreviewPath, $setThumbDir, | |
827 | 490 $setEntryPathPerms, $setPrevPathPerms; |
491 | |
492 echo "Checking for missing directories ...\n"; | |
935 | 493 wtMakeDir($setEntryPath, $setEntryPathPerms); |
494 wtMakeDir($setPreviewPath, $setPrevPathPerms); | |
827 | 495 |
496 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo) | |
497 { | |
935 | 498 wtMakeDir(stMakePath(FALSE, FALSE, array($setEntryPath, $compo["cpath"])), $setEntryPathPerms); |
499 wtMakeDir(stMakePath(FALSE, FALSE, array($setPreviewPath, $compo["cpath"])), $setPrevPathPerms); | |
500 wtMakeDir(stMakePath(FALSE, FALSE, array($setPreviewPath, $compo["cpath"], $setThumbDir)), $setPrevPathPerms); | |
827 | 501 } |
502 } | |
503 | |
504 | |
968 | 505 function wtPrintCompoHeader($compo) |
506 { | |
507 global $setTermWidth; | |
508 printf("==%'=-".($setTermWidth-2)."s\n", | |
509 sprintf("[ #%03d - %s ]", $compo["id"], substr($compo["name"], 0, 40)) | |
510 ); | |
511 } | |
512 | |
513 | |
514 function wtPrintDivider($chr = "-") | |
515 { | |
516 global $setTermWidth; | |
517 echo str_repeat($chr, $setTermWidth)."\n"; | |
518 } | |
519 | |
520 | |
935 | 521 function wtShowHelp() |
827 | 522 { |
935 | 523 global $argv; |
827 | 524 echo |
525 "faptool - Do stuff with FAPWeb database\n". | |
526 "(C) Copyright 2014 ccr/TNSP\n". | |
527 "\n". | |
917 | 528 "Usage: ".$argv[0]." <command> [args]\n". |
529 "Where command is one of following:\n". | |
827 | 530 "\n". |
531 " init\n". | |
532 " Create directories for entries and previews, if needed.\n". | |
533 "\n". | |
928 | 534 /* |
917 | 535 " clean [delete|crap]\n". |
536 " Clean files marked to be deleted and 'stale' files. This includes\n". | |
537 " any old or stale preview files and entry files.\n". | |
538 " Without 'delete' parameter just checks and shows what would deleted.\n". | |
539 " 'crap' parameter will purge EVERYTHING that is NOT in the database,\n". | |
540 " e.g. any other files, like temp files, etc. DANGEROUS IF YOU DO NOT\n". | |
541 " KNOW WHAT YOU ARE DOING!!!!!\n". | |
901 | 542 "\n". |
928 | 543 */ |
938 | 544 " probe <filename> [filename2 ..]\n". |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
545 " Probe specified file for file information.\n". |
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
546 "\n". |
881 | 547 " previews <cmd> [args]\n". |
548 " Where <cmd> is one of:\n". | |
933 | 549 " status [cid] - List files and show what is missing, etc. (All or <cid>)\n". |
928 | 550 " update - Generate preview files that are missing OR out of date.\n". |
551 " (older then preview source file OR entry file where appropriate)\n". | |
552 // " add <entry_id> <filename> - Add file as preview for entry_id.\n". | |
901 | 553 "\n". |
554 " entry <cmd> [args]\n". | |
933 | 555 " status [cid] - List entries for all compos or <cid>.\n". |
966 | 556 " unpack <path> - Create subdirs and copy all entries UNPACKED there.\n". |
928 | 557 /* |
558 " add <filename> '<name>' '<author>' - Add file as entry file\n". | |
559 " (works same as uploading from admin interface)\n". | |
560 " package <lha|zip|rar|7z>\n". | |
561 " Repackages/packages entries (unpacks first if archive),\n". | |
562 " and adds generated infofile.txt to packages.\n". | |
563 */ | |
827 | 564 "\n"; |
935 | 565 } |
566 | |
567 // | |
568 // Main program starts | |
569 // | |
944
535466595a5a
Move CLI exec check to program start.
Matti Hamalainen <ccr@tnsp.org>
parents:
943
diff
changeset
|
570 stCheckCLIExec(); |
935 | 571 if ($argc < 2) |
572 { | |
573 wtShowHelp(); | |
827 | 574 exit; |
575 } | |
576 | |
935 | 577 |
827 | 578 // Try to connect to database |
579 $spec = stGetSetting("sqlDB"); | |
580 if (($db = stConnectSQLDBSpec($spec)) === false) | |
581 die("Could not connect to SQL database '".$spec."'.\n"); | |
582 | |
583 echo "Using database spec '".$spec."'.\n"; | |
584 | |
585 // Fetch non-"hardcoded" settings from SQL database | |
586 stReloadSettings(); | |
587 | |
588 // Set some globals for our benefit | |
922 | 589 $setTermWidth = 75; |
827 | 590 $setEntryPath = stGetSetting("entryPath"); |
591 $setPreviewPath = stGetSetting("previewPath"); | |
592 $setPreviewURL = stGetSetting("previewURL"); | |
593 $setThumbDir = stGetSetting("thumbnailSubDir"); | |
832 | 594 $setEntryPathPerms = stGetSetting("entryPathPerms"); |
595 $setPrevPathPerms = stGetSetting("previewPathPerms"); | |
827 | 596 |
597 if ($setEntryPath === FALSE || $setPreviewPath === FALSE || | |
832 | 598 $setPreviewURL === FALSE || $setThumbDir === FALSE || |
599 $setEntryPathPerms === FALSE || $setPrevPathPerms === FALSE) | |
827 | 600 { |
922 | 601 die("Some required settings are not defined in mconfig.inc.php!\n"); |
827 | 602 } |
603 | |
604 | |
605 // Act according to specified command | |
884 | 606 switch (substr(stCArgLC(1), 0, 3)) |
827 | 607 { |
935 | 608 case "-?": case "--h": |
609 wtShowHelp(); | |
610 exit; | |
611 break; | |
612 | |
884 | 613 case "ini": |
827 | 614 // |
615 // Initialize the data directories etc | |
616 // | |
953 | 617 echo "INFO: Checking for missing directories ...\n"; |
935 | 618 wtInitializeDirs(); |
827 | 619 break; |
620 | |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
621 case "pro": |
945 | 622 // |
623 // Probe specified files | |
624 // | |
625 if ($argc < 2) | |
626 die("No filename specified.\n"); | |
627 | |
628 if (($finfo = finfo_open()) === false) | |
954 | 629 die("Internal error. Failed to initialize finfo()."); |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
630 |
945 | 631 for ($i = 2; $i < $argc; $i++) |
632 { | |
633 // Probe one file | |
634 $filename = $argv[$i]; | |
635 $sdata = @finfo_file($finfo, $filename, FILEINFO_NONE); | |
636 $smime = @finfo_file($finfo, $filename, FILEINFO_MIME_TYPE); | |
637 | |
638 echo | |
639 "\n". | |
640 "File : ".$filename."\n". | |
641 "Probed : ".$sdata."\n". | |
642 "Mime : ".$smime."\n"; | |
643 | |
954 | 644 if (($info = stProbeFileInfo($filename, TRUE)) !== false) |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
645 { |
945 | 646 $tmp = array(); |
647 foreach ($info as $ikey => $ival) | |
648 $tmp[] = $ikey."='".$ival."'"; | |
649 | |
650 echo "Data : ".implode(", ", $tmp)."\n"; | |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
651 } |
946 | 652 } |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
653 |
946 | 654 finfo_close($finfo); |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
655 break; |
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
656 |
901 | 657 case "cle": |
947 | 658 // |
659 // File cleanup / deletion | |
660 // | |
901 | 661 $doDelete = (stCArgLC(2) == "delete"); |
662 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo) | |
663 if (stFetchSQLColumn("SELECT COUNT(*) FROM entries WHERE compo_id=".$compo["id"]) > 0) | |
664 { | |
968 | 665 wtPrintCompoHeader($compo); |
666 wtPrintDivider(); | |
901 | 667 |
668 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
669 { | |
670 foreach (stExecSQL("SELECT * FROM files WHERE entry_id=".$entry["id"]) as $efile) | |
671 { | |
935 | 672 if ($efile["deleted"] != 0) |
901 | 673 { |
674 echo "X"; | |
675 } | |
676 } | |
677 } | |
678 echo "\n"; | |
968 | 679 wtPrintDivider(); |
901 | 680 } |
681 break; | |
682 | |
683 case "ent": | |
684 // | |
685 // Entry files handling | |
686 // | |
687 $mode = substr(stCArgLC(2), 0, 3); | |
688 switch ($mode) | |
689 { | |
690 case "sta": | |
949
2179600710d1
Add "lis" as alias to "sta".
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
691 case "lis": |
933 | 692 $sql = (stCArg(3) != "") ? " AND id=".intval(stCArg(3)) : ""; |
693 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL".$sql) as $compo) | |
901 | 694 if (stFetchSQLColumn("SELECT COUNT(*) FROM entries WHERE compo_id=".$compo["id"]) > 0) |
695 { | |
968 | 696 wtPrintCompoHeader($compo); |
697 wtPrintDivider(); | |
901 | 698 |
699 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
700 wtHandleEntry($compo, $entry, $mode); | |
701 | |
968 | 702 wtPrintDivider(); |
901 | 703 } |
704 break; | |
705 | |
963 | 706 case "unp": |
707 if (($setPrefix = stCArg(3)) == "") | |
708 die("Unpack needs a destination path.\n"); | |
709 | |
710 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo) | |
711 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
712 { | |
713 wtUnpackEntry($compo, $entry, $setPrefix); | |
714 } | |
715 break; | |
716 | |
901 | 717 case "pac": |
929 | 718 echo "Not implemented. :D\n"; |
901 | 719 break; |
720 | |
721 case "add": | |
929 | 722 echo "Not implemented. :D\n"; |
901 | 723 break; |
724 | |
725 default: | |
726 if ($mode == "") | |
727 die("ERROR! Entry command requires a sub-command argument.\n"); | |
728 else | |
729 die("ERROR! Invalid entry sub-command '".stCArg(2)."'.\n"); | |
730 break; | |
731 } | |
732 break; | |
733 | |
884 | 734 case "pre": |
881 | 735 // |
736 // Preview files handling | |
737 // | |
738 $mode = substr(stCArgLC(2), 0, 3); | |
739 switch ($mode) | |
740 { | |
898 | 741 case "upd": |
881 | 742 case "sta": |
949
2179600710d1
Add "lis" as alias to "sta".
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
743 case "lis": |
933 | 744 $sql = (stCArg(3) != "") ? " AND id=".intval(stCArg(3)) : ""; |
745 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL".$sql) as $compo) | |
898 | 746 if (stFetchSQLColumn("SELECT COUNT(*) FROM entries WHERE compo_id=".$compo["id"]) > 0) |
881 | 747 { |
968 | 748 wtPrintCompoHeader($compo); |
881 | 749 printf( |
898 | 750 "PrevType : %s\n", |
751 $previewTypeList[$compo["preview_type"]][0]); | |
941 | 752 |
968 | 753 if ($mode == "sta" || $mode == "lis") |
941 | 754 { |
755 printf(" %-3s | %-3s | %-40s |\n", | |
968 | 756 "#ID", "EPV", "Entry name/author"); |
941 | 757 } |
758 | |
968 | 759 wtPrintDivider("-"); |
881 | 760 |
761 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
762 wtHandleEntryPreview($compo, $entry, $mode); | |
898 | 763 |
968 | 764 wtPrintDivider("="); |
765 } | |
766 if ($mode == "sta" || $mode == "lis") | |
767 { | |
768 echo "Flags: E = Entry has file, P = Has preview file, V = Generated previews are up to date.\n"; | |
881 | 769 } |
770 break; | |
771 | |
901 | 772 case "add": |
929 | 773 echo "Not implemented. :D\n"; |
901 | 774 break; |
775 | |
881 | 776 default: |
777 if ($mode == "") | |
778 die("ERROR! Previews command requires a sub-command argument.\n"); | |
779 else | |
780 die("ERROR! Invalid previews sub-command '".stCArg(2)."'.\n"); | |
781 break; | |
782 } | |
783 break; | |
784 | |
827 | 785 default: |
786 echo "ERROR! Invalid operation mode '".stCArg(1)."'.\n"; | |
787 break; | |
788 } | |
789 | |
941 | 790 /* Dummy |
791 { | |
792 | |
793 } | |
794 */ | |
827 | 795 ?> |