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