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