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