Mercurial > hg > fapweb
comparison faptool.php @ 1104:0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 14 May 2019 14:45:36 +0300 |
parents | 1f77195b4f95 |
children | b2bca5f6d0ff |
comparison
equal
deleted
inserted
replaced
1103:1fe7df7c77fb | 1104:0a2117349f46 |
---|---|
46 $tmpFilename = tempnam($setPreviewPath, "tmp"); | 46 $tmpFilename = tempnam($setPreviewPath, "tmp"); |
47 $isTemp = TRUE; | 47 $isTemp = TRUE; |
48 if (wtExec(wtGetExecutable("gfxconv"), | 48 if (wtExec(wtGetExecutable("gfxconv"), |
49 wtGetExecutableArgs("gfxconv"). | 49 wtGetExecutableArgs("gfxconv"). |
50 escapeshellarg($inFilename)." -f png -o ".escapeshellarg($tmpFilename), | 50 escapeshellarg($inFilename)." -f png -o ".escapeshellarg($tmpFilename), |
51 0) === false) | 51 0) === FALSE) |
52 return FALSE; | 52 return FALSE; |
53 | 53 |
54 if (!file_exists($tmpFilename)) | 54 if (!file_exists($tmpFilename)) |
55 { | 55 { |
56 echo "ERROR: gfxconv did not output a temporary conversion inbetween.\n"; | 56 echo "ERROR: gfxconv did not output a temporary conversion inbetween.\n"; |
223 // Create temporary directory for unpacking | 223 // Create temporary directory for unpacking |
224 wtMakeDir($path, 0755); | 224 wtMakeDir($path, 0755); |
225 | 225 |
226 // Save current working directory and chdir to target | 226 // Save current working directory and chdir to target |
227 $cwd = getcwd(); | 227 $cwd = getcwd(); |
228 if (!is_dir($path) || chdir($path) === false) | 228 if (!is_dir($path) || chdir($path) === FALSE) |
229 { | 229 { |
230 echo "ERROR: Failed to chdir to '".$path."', can't unpack archive.\n"; | 230 echo "ERROR: Failed to chdir to '".$path."', can't unpack archive.\n"; |
231 return FALSE; | 231 return FALSE; |
232 } | 232 } |
233 | 233 |
254 // Check preview file(s) status | 254 // Check preview file(s) status |
255 if ($mode == "sta" || $mode == "lis") | 255 if ($mode == "sta" || $mode == "lis") |
256 { | 256 { |
257 printf(" %03d | %s%s%s | %-40s | %-5s | %s\n", | 257 printf(" %03d | %s%s%s | %-40s | %-5s | %s\n", |
258 $entry["id"], | 258 $entry["id"], |
259 ($efile !== false) ? "E" : ".", | 259 ($efile !== FALSE) ? "E" : ".", |
260 isset($pdata["file"]) ? "P" : ".", | 260 isset($pdata["file"]) ? "P" : ".", |
261 $pdata["valid"] ? "V" : ".", | 261 $pdata["valid"] ? "V" : ".", |
262 substr($entry["name"]." by ".$entry["author"], 0, 40), | 262 substr($entry["name"]." by ".$entry["author"], 0, 40), |
263 isset($pdata["file"]) ? $pdata["file"]["filetype"] : "", | 263 isset($pdata["file"]) ? $pdata["file"]["filetype"] : "", |
264 isset($pdata["file"]) ? $pdata["file"]["filename"] : "" | 264 isset($pdata["file"]) ? $pdata["file"]["filename"] : "" |
278 // Okay, no valid previews .. lets see what we can do .. | 278 // Okay, no valid previews .. lets see what we can do .. |
279 // First, check if we have a source preview file | 279 // First, check if we have a source preview file |
280 if (!isset($pdata["file"])) | 280 if (!isset($pdata["file"])) |
281 { | 281 { |
282 // No source preview, check if we have entry file either? | 282 // No source preview, check if we have entry file either? |
283 if ($efile === false) | 283 if ($efile === FALSE) |
284 { | 284 { |
285 echo | 285 echo |
286 "INFO: No entry file for ".wtNiceName($compo, $entry, $efile). | 286 "INFO: No entry file for ".wtNiceName($compo, $entry, $efile). |
287 ", can't attempt to generate preview.\n"; | 287 ", can't attempt to generate preview.\n"; |
288 return FALSE; | 288 return FALSE; |
296 return FALSE; | 296 return FALSE; |
297 } | 297 } |
298 | 298 |
299 // Preview source file does not exist, let's see .. | 299 // Preview source file does not exist, let's see .. |
300 $edata = stProbeFileInfo($filename, TRUE); | 300 $edata = stProbeFileInfo($filename, TRUE); |
301 if ($edata === false) | 301 if ($edata === FALSE) |
302 { | 302 { |
303 echo | 303 echo |
304 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n"; | 304 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n"; |
305 return FALSE; | 305 return FALSE; |
306 } | 306 } |
309 if ($edata["class"] == EFILE_ARCHIVE) | 309 if ($edata["class"] == EFILE_ARCHIVE) |
310 { | 310 { |
311 // Entry is an archive file .. | 311 // Entry is an archive file .. |
312 $path = stMakePath(FALSE, FALSE, array($setEntryPath, "UNPACKS", $efile["filename"])); | 312 $path = stMakePath(FALSE, FALSE, array($setEntryPath, "UNPACKS", $efile["filename"])); |
313 | 313 |
314 if (wtUnpackArchiveTo($efile["filetype"], $filename, $path) === false) | 314 if (wtUnpackArchiveTo($efile["filetype"], $filename, $path) === FALSE) |
315 return FALSE; | 315 return FALSE; |
316 | 316 |
317 // Scan through files ... | 317 // Scan through files ... |
318 $dir = opendir($path); | 318 $dir = opendir($path); |
319 while (($dentry = readdir($dir)) !== false) | 319 while (($dentry = readdir($dir)) !== FALSE) |
320 { | 320 { |
321 $fname = $path."/".$dentry; | 321 $fname = $path."/".$dentry; |
322 if ($dentry != "." && $dentry != "..") | 322 if ($dentry != "." && $dentry != "..") |
323 { | 323 { |
324 if (is_dir($fname)) | 324 if (is_dir($fname)) |
325 echo "XXX: ".$dentry." :: ".$fname."\n"; | 325 echo "XXX: ".$dentry." :: ".$fname."\n"; |
326 else | 326 else |
327 if (is_file($fname) && | 327 if (is_file($fname) && |
328 ($mdata = stProbeFileInfo($fname, TRUE)) !== false && | 328 ($mdata = stProbeFileInfo($fname, TRUE)) !== FALSE && |
329 ($found = wtEntryToSource($compo, $fname, $mdata, $filename, $pdata, $outFilename, $force)) === true) | 329 ($found = wtEntryToSource($compo, $fname, $mdata, $filename, $pdata, $outFilename, $force)) === TRUE) |
330 break; | 330 break; |
331 } | 331 } |
332 } | 332 } |
333 | 333 |
334 // Cleanup | 334 // Cleanup |
449 // cropping file/dir names to specified length. | 449 // cropping file/dir names to specified length. |
450 // | 450 // |
451 function wtCropFilenamesRec($path, $len) | 451 function wtCropFilenamesRec($path, $len) |
452 { | 452 { |
453 $dir = opendir($path); | 453 $dir = opendir($path); |
454 while (($dentry = readdir($dir)) !== false) | 454 while (($dentry = readdir($dir)) !== FALSE) |
455 { | 455 { |
456 $fname = $path."/".$dentry; | 456 $fname = $path."/".$dentry; |
457 $nname = $path."/".wtCropFilename($dentry, $len); | 457 $nname = $path."/".wtCropFilename($dentry, $len); |
458 if ($dentry != "." && $dentry != "..") | 458 if ($dentry != "." && $dentry != "..") |
459 { | 459 { |
460 if (is_dir($fname)) | 460 if (is_dir($fname)) |
461 { | 461 { |
462 wtCropFilenamesRec($fname, $len); | 462 wtCropFilenamesRec($fname, $len); |
463 if (rename($fname, $nname) === false) | 463 if (rename($fname, $nname) === FALSE) |
464 { | 464 { |
465 echo "ERROR: Could not rename DIR '".$fname."' -> '".$nname."'.\n"; | 465 echo "ERROR: Could not rename DIR '".$fname."' -> '".$nname."'.\n"; |
466 return FALSE; | 466 return FALSE; |
467 } | 467 } |
468 } | 468 } |
469 else | 469 else |
470 if (is_file($fname) && rename($fname, $nname) === false) | 470 if (is_file($fname) && rename($fname, $nname) === FALSE) |
471 { | 471 { |
472 echo "ERROR: Could not rename FILE '".$fname."' -> '".$nname."'.\n"; | 472 echo "ERROR: Could not rename FILE '".$fname."' -> '".$nname."'.\n"; |
473 return FALSE; | 473 return FALSE; |
474 } | 474 } |
475 } | 475 } |
481 function wtUnpackEntry($compo, $entry, $pathPrefix, $useOrig, $cropNames, $copyOnly) | 481 function wtUnpackEntry($compo, $entry, $pathPrefix, $useOrig, $cropNames, $copyOnly) |
482 { | 482 { |
483 global $setEntryPath; | 483 global $setEntryPath; |
484 | 484 |
485 // Get latest file for the entry | 485 // Get latest file for the entry |
486 if (($efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"])) === false) | 486 if (($efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"])) === FALSE) |
487 { | 487 { |
488 echo "INFO: No entry file for ".wtNiceName($compo, $entry, $efile)."\n"; | 488 echo "INFO: No entry file for ".wtNiceName($compo, $entry, $efile)."\n"; |
489 return FALSE; | 489 return FALSE; |
490 } | 490 } |
491 | 491 |
499 return FALSE; | 499 return FALSE; |
500 } | 500 } |
501 | 501 |
502 // Get file data | 502 // Get file data |
503 $edata = stProbeFileInfo($filename, TRUE); | 503 $edata = stProbeFileInfo($filename, TRUE); |
504 if ($edata === false) | 504 if ($edata === FALSE) |
505 { | 505 { |
506 echo | 506 echo |
507 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n"; | 507 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n"; |
508 return FALSE; | 508 return FALSE; |
509 } | 509 } |
535 | 535 |
536 $sbinfo[] = ""; | 536 $sbinfo[] = ""; |
537 $sbinfoStr = implode("\n", $sbinfo); | 537 $sbinfoStr = implode("\n", $sbinfo); |
538 | 538 |
539 // Create the destination directory | 539 // Create the destination directory |
540 if (wtMakeDir(stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"])), 0755) === false) | 540 if (wtMakeDir(stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"])), 0755) === FALSE) |
541 return FALSE; | 541 return FALSE; |
542 | 542 |
543 // Form the destination path and/or filename | 543 // Form the destination path and/or filename |
544 if ($copyOnly) | 544 if ($copyOnly) |
545 $dstFileBase = $useOrig ? $efile["origname"] : $efile["filename"]; | 545 $dstFileBase = $useOrig ? $efile["origname"] : $efile["filename"]; |
550 | 550 |
551 // Handle based on class/type and whether we are just copying or not | 551 // Handle based on class/type and whether we are just copying or not |
552 if ($copyOnly) | 552 if ($copyOnly) |
553 { | 553 { |
554 $dstFilename = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], $dstFileBase)); | 554 $dstFilename = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], $dstFileBase)); |
555 if (copy($filename, $dstFilename) === false) | 555 if (copy($filename, $dstFilename) === FALSE) |
556 { | 556 { |
557 echo "ERROR: Failed to copy '".$filename."' to '".$dstFilename."'\n"; | 557 echo "ERROR: Failed to copy '".$filename."' to '".$dstFilename."'\n"; |
558 return FALSE; | 558 return FALSE; |
559 } | 559 } |
560 } | 560 } |
561 else | 561 else |
562 if ($edata["class"] == EFILE_ARCHIVE) | 562 if ($edata["class"] == EFILE_ARCHIVE) |
563 { | 563 { |
564 // Entry is an archive file, so unpack it | 564 // Entry is an archive file, so unpack it |
565 if (wtUnpackArchiveTo($edata["id"], $filename, $dstPath) === false) | 565 if (wtUnpackArchiveTo($edata["id"], $filename, $dstPath) === FALSE) |
566 return FALSE; | 566 return FALSE; |
567 | 567 |
568 // Crop the filenames from the unpacked archive, if we need to | 568 // Crop the filenames from the unpacked archive, if we need to |
569 if ($cropNames !== false) | 569 if ($cropNames !== FALSE) |
570 wtCropFilenamesRec($dstPath, $cropNames); | 570 wtCropFilenamesRec($dstPath, $cropNames); |
571 | 571 |
572 } | 572 } |
573 else | 573 else |
574 { | 574 { |
575 // We have a single file (or copyOnly mode) | 575 // We have a single file (or copyOnly mode) |
576 if (wtMakeDir($dstPath, 0755) === false) | 576 if (wtMakeDir($dstPath, 0755) === FALSE) |
577 return FALSE; | 577 return FALSE; |
578 | 578 |
579 $dstFilename = stMakePath(FALSE, FALSE, array($dstPath, wtCropFilename($efile["origname"], $cropNames))); | 579 $dstFilename = stMakePath(FALSE, FALSE, array($dstPath, wtCropFilename($efile["origname"], $cropNames))); |
580 if (copy($filename, $dstFilename) === false) | 580 if (copy($filename, $dstFilename) === FALSE) |
581 { | 581 { |
582 echo "ERROR: Failed to copy '".$filename."' to '".$dstFilename."'\n"; | 582 echo "ERROR: Failed to copy '".$filename."' to '".$dstFilename."'\n"; |
583 return FALSE; | 583 return FALSE; |
584 } | 584 } |
585 } | 585 } |
604 return sprintf( | 604 return sprintf( |
605 "%d: %s by %s%s", | 605 "%d: %s by %s%s", |
606 $entry["id"], | 606 $entry["id"], |
607 $entry["name"], | 607 $entry["name"], |
608 $entry["author"], | 608 $entry["author"], |
609 ($efile !== false) ? " [".$efile["filename"]." / TYPE: '".$efile["filetype"]."']" : ""); | 609 ($efile !== FALSE) ? " [".$efile["filename"]." / TYPE: '".$efile["filetype"]."']" : ""); |
610 } | 610 } |
611 | 611 |
612 | 612 |
613 function wtExec($exe, $args, $expect = 0) | 613 function wtExec($exe, $args, $expect = 0) |
614 { | 614 { |
627 } | 627 } |
628 | 628 |
629 | 629 |
630 function wtExecOrDie($exe, $args) | 630 function wtExecOrDie($exe, $args) |
631 { | 631 { |
632 if (wtExec($exe, $args) === false) | 632 if (wtExec($exe, $args) === FALSE) |
633 die(); | 633 die(); |
634 } | 634 } |
635 | 635 |
636 | 636 |
637 function wtPurgeDir($path) | 637 function wtPurgeDir($path) |
638 { | 638 { |
639 if ($path != "" && $path !== false && file_exists($path) && is_dir($path)) | 639 if ($path != "" && $path !== FALSE && file_exists($path) && is_dir($path)) |
640 { | 640 { |
641 // echo "PURGING: ".$path."\n"; | 641 // echo "PURGING: ".$path."\n"; |
642 foreach (scandir($path) as $file) | 642 foreach (scandir($path) as $file) |
643 if ($file !== "." && $file !== "..") | 643 if ($file !== "." && $file !== "..") |
644 { | 644 { |
656 function wtMakeDir($path, $perm) | 656 function wtMakeDir($path, $perm) |
657 { | 657 { |
658 if (!file_exists($path)) | 658 if (!file_exists($path)) |
659 { | 659 { |
660 echo "INFO: Creating ".$path."\n"; | 660 echo "INFO: Creating ".$path."\n"; |
661 if (mkdir($path, $perm, TRUE) === false) | 661 if (mkdir($path, $perm, TRUE) === FALSE) |
662 die("Could not create directory '".$path."'\n"); | 662 die("Could not create directory '".$path."'\n"); |
663 } | 663 } |
664 } | 664 } |
665 | 665 |
666 | 666 |
782 pcntl_signal(SIGQUIT, "wtSigHandler"); | 782 pcntl_signal(SIGQUIT, "wtSigHandler"); |
783 pcntl_signal(SIGINT, "wtSigHandler"); | 783 pcntl_signal(SIGINT, "wtSigHandler"); |
784 | 784 |
785 // Try to connect to database | 785 // Try to connect to database |
786 $spec = stGetSetting("sqlDB"); | 786 $spec = stGetSetting("sqlDB"); |
787 if (($db = stConnectSQLDBSpec($spec)) === false) | 787 if (($db = stConnectSQLDBSpec($spec)) === FALSE) |
788 die("Could not connect to SQL database '".$spec."'.\n"); | 788 die("Could not connect to SQL database '".$spec."'.\n"); |
789 | 789 |
790 echo "Using database spec '".$spec."'.\n"; | 790 echo "Using database spec '".$spec."'.\n"; |
791 | 791 |
792 // Fetch non-"hardcoded" settings from SQL database | 792 // Fetch non-"hardcoded" settings from SQL database |
830 // Probe specified files | 830 // Probe specified files |
831 // | 831 // |
832 if ($argc < 2) | 832 if ($argc < 2) |
833 die("No filename specified.\n"); | 833 die("No filename specified.\n"); |
834 | 834 |
835 if (($finfo = finfo_open()) === false) | 835 if (($finfo = finfo_open()) === FALSE) |
836 die("Internal error. Failed to initialize finfo()."); | 836 die("Internal error. Failed to initialize finfo()."); |
837 | 837 |
838 for ($i = 2; $i < $argc; $i++) | 838 for ($i = 2; $i < $argc; $i++) |
839 { | 839 { |
840 // Probe one file | 840 // Probe one file |
846 "\n". | 846 "\n". |
847 "File : ".$filename."\n". | 847 "File : ".$filename."\n". |
848 "Probed : ".$sdata."\n". | 848 "Probed : ".$sdata."\n". |
849 "Mime : ".$smime."\n"; | 849 "Mime : ".$smime."\n"; |
850 | 850 |
851 if (($info = stProbeFileInfo($filename, TRUE)) !== false) | 851 if (($info = stProbeFileInfo($filename, TRUE)) !== FALSE) |
852 { | 852 { |
853 $tmp = array(); | 853 $tmp = array(); |
854 foreach ($info as $ikey => $ival) | 854 foreach ($info as $ikey => $ival) |
855 $tmp[] = $ikey."='".$ival."'"; | 855 $tmp[] = $ikey."='".$ival."'"; |
856 | 856 |