Mercurial > hg > fapweb
annotate faptool.php @ 1120:b2bca5f6d0ff default tip
Cosmetic cleanup: remove trailing whitespace.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 13 Dec 2020 13:47:13 +0200 |
parents | 0a2117349f46 |
children |
rev | line source |
---|---|
827 | 1 #!/usr/bin/php |
1071 | 2 <?php |
943 | 3 // |
4 // FAPWeb - Simple Web-based Demoparty Management System | |
5 // Special entry/preview/etc backend management commandline utility | |
1076 | 6 // (C) Copyright 2014-2017 Tecnic Software productions (TNSP) |
943 | 7 // |
1024
2a96426159f1
Fix configuration file checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1019
diff
changeset
|
8 if (!file_exists(realpath(dirname(__FILE__))."/mconfig.inc.php")) |
1019
22b6442d5cb3
Add error messages for missing mconfig.inc.php.
Matti Hamalainen <ccr@tnsp.org>
parents:
1013
diff
changeset
|
9 { |
22b6442d5cb3
Add error messages for missing mconfig.inc.php.
Matti Hamalainen <ccr@tnsp.org>
parents:
1013
diff
changeset
|
10 die( |
22b6442d5cb3
Add error messages for missing mconfig.inc.php.
Matti Hamalainen <ccr@tnsp.org>
parents:
1013
diff
changeset
|
11 "ERROR: Missing site configuration file. Please copy ". |
22b6442d5cb3
Add error messages for missing mconfig.inc.php.
Matti Hamalainen <ccr@tnsp.org>
parents:
1013
diff
changeset
|
12 "'mconfig.inc.php.example' to 'mconfig.inc.php' and ". |
22b6442d5cb3
Add error messages for missing mconfig.inc.php.
Matti Hamalainen <ccr@tnsp.org>
parents:
1013
diff
changeset
|
13 "edit it as needed.\n"); |
22b6442d5cb3
Add error messages for missing mconfig.inc.php.
Matti Hamalainen <ccr@tnsp.org>
parents:
1013
diff
changeset
|
14 } |
22b6442d5cb3
Add error messages for missing mconfig.inc.php.
Matti Hamalainen <ccr@tnsp.org>
parents:
1013
diff
changeset
|
15 |
827 | 16 require_once "mconfig.inc.php"; |
17 require_once "msite.inc.php"; | |
18 | |
19 | |
1042
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
20 function wtGetExecutable($name, $path = FALSE) |
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
21 { |
1043
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
22 if (stChkSetting($name."_exe")) |
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
23 return stGetSetting($name."_exe"); |
1042
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
24 else |
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
25 return $name; |
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
26 } |
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
27 |
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
28 |
1043
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
29 function wtGetExecutableArgs($name) |
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
30 { |
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
31 if (stChkSetting($name."_args")) |
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
32 return " ".stGetSetting($name."_args")." "; |
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
33 else |
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
34 return ""; |
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
35 } |
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
36 |
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
37 |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
38 function wtConvertImage($inFilename, $outFilename, $outDim, $outFormat, $outQuality, $thumb, $useGfxConv) |
882 | 39 { |
40 global $setPreviewPath; | |
912 | 41 $isTemp = FALSE; |
882 | 42 |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
43 if ($useGfxConv) |
884 | 44 { |
45 // Oh great .. we need gfxconv here because Imagick handles ILBM like shit | |
951 | 46 $tmpFilename = tempnam($setPreviewPath, "tmp"); |
912 | 47 $isTemp = TRUE; |
1042
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
48 if (wtExec(wtGetExecutable("gfxconv"), |
1043
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
49 wtGetExecutableArgs("gfxconv"). |
970
9c93a3c4f95c
Clean out the debug code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
969
diff
changeset
|
50 escapeshellarg($inFilename)." -f png -o ".escapeshellarg($tmpFilename), |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
51 0) === FALSE) |
912 | 52 return FALSE; |
53 | |
951 | 54 if (!file_exists($tmpFilename)) |
912 | 55 { |
56 echo "ERROR: gfxconv did not output a temporary conversion inbetween.\n"; | |
57 return FALSE; | |
58 } | |
884 | 59 } |
60 else | |
951 | 61 $tmpFilename = $inFilename; |
884 | 62 |
63 // Create conversion entity | |
951 | 64 $img = new Imagick($tmpFilename); |
1004 | 65 if ($img === FALSE) |
912 | 66 { |
951 | 67 echo "ERROR: Oh noes! ImageMagick could not digest the file '".$tmpFilename."' (".$inFilename.")\n"; |
912 | 68 return FALSE; |
69 } | |
884 | 70 |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
71 if ($outDim !== FALSE) |
884 | 72 { |
1120
b2bca5f6d0ff
Cosmetic cleanup: remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1104
diff
changeset
|
73 // Get dimensions, setup background |
963 | 74 $dim = $img->getImageGeometry(); |
969 | 75 // $img->setBackgroundColor(imagick::COLOR_BLACK); |
963 | 76 $img->setGravity(imagick::GRAVITY_CENTER); |
77 | |
78 // Act based on image size vs. desired size and $thumb mode | |
79 if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1]) | |
80 { | |
81 // Image is larger | |
82 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_QUADRATIC, 1); | |
969 | 83 $img->setImageExtent($outDim[0], $outDim[1]); |
963 | 84 $img->normalizeImage(); |
85 $img->unsharpMaskImage(0, 0.5, 1, 0.05); | |
86 } | |
87 else | |
88 if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1]) | |
89 { | |
90 // Image is smaller than requested dimension(s)? | |
91 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_POINT, 1); | |
969 | 92 $img->setImageExtent($outDim[0], $outDim[1]); |
963 | 93 } |
884 | 94 } |
95 | |
96 $img->setFormat($outFormat); | |
97 $img->setCompressionQuality($outQuality); | |
98 | |
99 $img->stripImage(); | |
100 $img->writeImage($outFilename); | |
101 $img->removeImage(); | |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
102 return TRUE; |
882 | 103 } |
104 | |
105 | |
898 | 106 function wtRenderSample($inFilename, $outFilename) |
882 | 107 { |
898 | 108 $sfreq = intval(stGetSetting("sampleFreq")); |
109 $sduration = intval(stGetSetting("sampleDuration")); | |
110 $schannels = intval(stGetSetting("sampleChannels")); | |
882 | 111 |
902 | 112 return wtExec( |
1042
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
113 wtGetExecutable("openmpt123") |
898 | 114 , |
954 | 115 "--force --quiet ". |
884 | 116 "--samplerate ".$sfreq." ". |
117 "--channels ".$schannels." ". | |
1026
37901bdbc545
Switch to the official --end-time parameter of openmpt123
Matti Hamalainen <ccr@tnsp.org>
parents:
1024
diff
changeset
|
118 "--end-time ".($sduration)." ". |
1043
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
119 wtGetExecutableArgs("openmpt123"). |
954 | 120 escapeshellarg($inFilename)." -o ".escapeshellarg($outFilename), |
963 | 121 0); |
882 | 122 } |
123 | |
124 | |
948
1334b93939a3
Change how output options are handled in wtConvertSample().
Matti Hamalainen <ccr@tnsp.org>
parents:
947
diff
changeset
|
125 function wtConvertSample($inFilename, $outFilename, $outOpts) |
882 | 126 { |
898 | 127 $sfreq = intval(stGetSetting("sampleFreq")); |
128 $sduration = intval(stGetSetting("sampleDuration")); | |
129 $schannels = intval(stGetSetting("sampleChannels")); | |
882 | 130 |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
131 $optStr = ""; |
948
1334b93939a3
Change how output options are handled in wtConvertSample().
Matti Hamalainen <ccr@tnsp.org>
parents:
947
diff
changeset
|
132 foreach ($outOpts as $okey => $oval) |
898 | 133 $optStr .= $okey." ".$oval." "; |
939 | 134 |
902 | 135 return wtExec( |
1042
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
136 wtGetExecutable("avconv"), |
1043
c0e21a405256
Change external tool handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
137 wtGetExecutableArgs("avconv"). |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
138 "-y -v 1 -t ".intval($sduration)." ". |
898 | 139 "-i ".escapeshellarg($inFilename). |
140 " ".$optStr." -ac ".$schannels. | |
141 " -map_metadata -1 ". | |
142 escapeshellarg($outFilename)); | |
882 | 143 } |
144 | |
145 | |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
146 function wtEntryToSource($compo, $inFilename, $edata, $entryFilename, &$outData, &$outFilename, $force) |
882 | 147 { |
963 | 148 // Does compo preview type match this file class? |
149 if ($edata["class"] != $compo["preview_type"]) | |
150 return FALSE; | |
954 | 151 |
969 | 152 switch ($edata["class"]) |
153 { | |
154 case EFILE_AUDIO: $fext = "wav"; break; | |
155 case EFILE_IMAGE: $fext = "png"; break; | |
156 default: | |
157 return FALSE; | |
158 } | |
159 | |
160 $outFilename = stReplaceFileExt($entryFilename, "_preview.".$fext); | |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
161 if (!$force && file_exists($outFilename) && filemtime($outFilename) >= filemtime($entryFilename)) |
969 | 162 return TRUE; |
963 | 163 |
969 | 164 printf("GENERATING: %s -> %s\n", $inFilename, $outFilename); |
165 | |
954 | 166 switch ($edata["class"]) |
939 | 167 { |
954 | 168 case EFILE_AUDIO: |
169 if ($edata["mime"] == "audio/x-mod") | |
963 | 170 $res = wtRenderSample($inFilename, $outFilename); |
954 | 171 else |
969 | 172 $res = wtConvertSample($inFilename, $outFilename, |
173 array("-f" => "wav")); | |
954 | 174 break; |
1120
b2bca5f6d0ff
Cosmetic cleanup: remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1104
diff
changeset
|
175 |
954 | 176 case EFILE_IMAGE: |
963 | 177 $res = wtConvertImage( |
178 $inFilename, | |
179 $outFilename, | |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
180 FALSE, "PNG", 9, |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
181 FALSE, ($edata["mime"] == "gfx")); |
963 | 182 break; |
939 | 183 } |
963 | 184 |
185 return $res; | |
882 | 186 } |
187 | |
188 | |
902 | 189 function wtUnpackArchiveTo($atype, $filename, $path) |
882 | 190 { |
191 // Check file type before doing anything | |
953 | 192 echo "INFO: Preparing to unpack archive file '".$filename."' ...\n"; |
902 | 193 switch ($atype) |
882 | 194 { |
195 case "LHA": | |
1042
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
196 $exe = wtGetExecutable("lha"); |
967 | 197 $args = "x ".escapeshellarg($filename); |
882 | 198 break; |
199 | |
200 case "ZIP": | |
1042
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
201 $exe = wtGetExecutable("unzip"); |
882 | 202 $args = "-d ".escapeshellarg($path)." ".escapeshellarg($filename); |
203 break; | |
204 | |
898 | 205 case "RAR": |
1042
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
206 $exe = wtGetExecutable("rar"); |
968 | 207 $args = "x -w".escapeshellarg($path)." ".escapeshellarg($filename); |
208 break; | |
209 | |
210 case "7ZIP": | |
1042
b17c41ffe43b
Implement wtGetExecutable() and use it for getting executable names for
Matti Hamalainen <ccr@tnsp.org>
parents:
1033
diff
changeset
|
211 $exe = wtGetExecutable("7z"); |
968 | 212 $args = "x -o".escapeshellarg($path)." ".escapeshellarg($filename); |
898 | 213 break; |
214 | |
882 | 215 default: |
902 | 216 echo "Unsupported archive file type: ".$atype."\n"; |
898 | 217 return FALSE; |
882 | 218 } |
219 | |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
220 // Delete any pre-existing directory |
882 | 221 wtPurgeDir($path); |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
222 |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
223 // Create temporary directory for unpacking |
1051 | 224 wtMakeDir($path, 0755); |
882 | 225 |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
226 // Save current working directory and chdir to target |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
227 $cwd = getcwd(); |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
228 if (!is_dir($path) || chdir($path) === FALSE) |
902 | 229 { |
953 | 230 echo "ERROR: Failed to chdir to '".$path."', can't unpack archive.\n"; |
902 | 231 return FALSE; |
232 } | |
882 | 233 |
234 // Unpack archive | |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
235 $ret = wtExec($exe, $args); |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
236 |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
237 // Return to stored cwd |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
238 chdir($cwd); |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
239 return $ret; |
902 | 240 } |
241 | |
242 | |
882 | 243 function wtHandleEntryPreview($compo, $entry, $mode) |
244 { | |
941 | 245 global $fileTypeData, $setEntryPath; |
933 | 246 |
247 // Get current preview file(s) data | |
898 | 248 if (!stGetPreviewFileData($compo, $entry, $pdata)) |
249 return FALSE; | |
250 | |
933 | 251 // Get entry file data |
963 | 252 $efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"]); |
898 | 253 |
254 // Check preview file(s) status | |
949
2179600710d1
Add "lis" as alias to "sta".
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
255 if ($mode == "sta" || $mode == "lis") |
898 | 256 { |
257 printf(" %03d | %s%s%s | %-40s | %-5s | %s\n", | |
258 $entry["id"], | |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
259 ($efile !== FALSE) ? "E" : ".", |
933 | 260 isset($pdata["file"]) ? "P" : ".", |
261 $pdata["valid"] ? "V" : ".", | |
262 substr($entry["name"]." by ".$entry["author"], 0, 40), | |
898 | 263 isset($pdata["file"]) ? $pdata["file"]["filetype"] : "", |
264 isset($pdata["file"]) ? $pdata["file"]["filename"] : "" | |
265 ); | |
954 | 266 return TRUE; |
898 | 267 } |
268 else | |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
269 if ($mode != "gen" && $mode != "upd") |
953 | 270 die("ERROR: Unsupported previews mode ".$mode."\n"); |
933 | 271 |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
272 $force = ($mode == "upd"); |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
273 |
933 | 274 // Check validity of previews |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
275 if ($pdata["valid"] && !$force) |
933 | 276 return TRUE; |
277 | |
278 // Okay, no valid previews .. lets see what we can do .. | |
279 // First, check if we have a source preview file | |
280 if (!isset($pdata["file"])) | |
898 | 281 { |
935 | 282 // No source preview, check if we have entry file either? |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
283 if ($efile === FALSE) |
935 | 284 { |
285 echo | |
286 "INFO: No entry file for ".wtNiceName($compo, $entry, $efile). | |
287 ", can't attempt to generate preview.\n"; | |
288 return FALSE; | |
289 } | |
954 | 290 |
291 // Actual entry filename + path | |
292 $filename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $efile["filename"])); | |
293 if (!file_exists($filename)) | |
294 { | |
295 echo "ERROR: Entry file '".$filename."' for ".wtNiceName($compo, $entry, FALSE)." does not exist!\n"; | |
296 return FALSE; | |
297 } | |
298 | |
935 | 299 // Preview source file does not exist, let's see .. |
963 | 300 $edata = stProbeFileInfo($filename, TRUE); |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
301 if ($edata === FALSE) |
882 | 302 { |
935 | 303 echo |
304 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n"; | |
305 return FALSE; | |
306 } | |
307 | |
942 | 308 $found = FALSE; |
935 | 309 if ($edata["class"] == EFILE_ARCHIVE) |
310 { | |
311 // Entry is an archive file .. | |
963 | 312 $path = stMakePath(FALSE, FALSE, array($setEntryPath, "UNPACKS", $efile["filename"])); |
941 | 313 |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
314 if (wtUnpackArchiveTo($efile["filetype"], $filename, $path) === FALSE) |
941 | 315 return FALSE; |
935 | 316 |
941 | 317 // Scan through files ... |
318 $dir = opendir($path); | |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
319 while (($dentry = readdir($dir)) !== FALSE) |
941 | 320 { |
321 $fname = $path."/".$dentry; | |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
322 if ($dentry != "." && $dentry != "..") |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
323 { |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
324 if (is_dir($fname)) |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
325 echo "XXX: ".$dentry." :: ".$fname."\n"; |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
326 else |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
327 if (is_file($fname) && |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
328 ($mdata = stProbeFileInfo($fname, TRUE)) !== FALSE && |
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
329 ($found = wtEntryToSource($compo, $fname, $mdata, $filename, $pdata, $outFilename, $force)) === TRUE) |
954 | 330 break; |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
331 } |
941 | 332 } |
954 | 333 |
334 // Cleanup | |
941 | 335 closedir($dir); |
336 wtPurgeDir($path); | |
954 | 337 } |
338 else | |
339 if ($edata["class"] == $compo["preview_type"]) | |
340 { | |
341 // Single file | |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
342 $found = wtEntryToSource($compo, $filename, $edata, $filename, $pdata, $outFilename, $force); |
954 | 343 } |
1120
b2bca5f6d0ff
Cosmetic cleanup: remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1104
diff
changeset
|
344 |
950 | 345 if (!$found) |
882 | 346 { |
935 | 347 echo "WARNING: Could not generate preview from entry ".wtNiceName($compo, $entry, $efile)."\n"; |
348 return FALSE; | |
898 | 349 } |
969 | 350 |
351 $inFilename = $outFilename; | |
898 | 352 } |
969 | 353 else |
354 $inFilename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $pdata["file"]["filename"])); | |
898 | 355 |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
356 // Check validity vs. |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
357 if (!$force) |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
358 { |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
359 $valid = TRUE; |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
360 $inMTime = filemtime($inFilename); |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
361 foreach ($pdata["files"] as $stype => $sdata) |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
362 { |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
363 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
|
364 { |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
365 $valid = FALSE; |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
366 break; |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
367 } |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
368 } |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
369 if ($valid) |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
370 return TRUE; |
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
371 } |
1120
b2bca5f6d0ff
Cosmetic cleanup: remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1104
diff
changeset
|
372 |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
373 // |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
374 // Either we now have a "user specified" source file or |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
375 // a generated source file - in any case, now convert it |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
376 // to the final form(s). |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
377 // |
969 | 378 switch ($compo["preview_type"]) |
941 | 379 { |
963 | 380 case EFILE_AUDIO: |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
381 // Convert to each specified sample type (MP3, Ogg, etc.) |
950 | 382 foreach (stGetSetting("sampleTypes") as $stype => $sopts) |
942 | 383 { |
950 | 384 wtConvertSample( |
385 $inFilename, | |
969 | 386 $pdata["files"][$stype]["file"], |
950 | 387 $sopts |
388 ); | |
942 | 389 } |
390 break; | |
969 | 391 |
963 | 392 case EFILE_IMAGE: |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
393 // Images we convert to bigger image and thumbnail |
969 | 394 wtConvertImage( |
395 $inFilename, | |
396 $pdata["files"]["image"]["file"], | |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
397 stGetSetting("previewImageSize"), |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
398 stGetSetting("previewImageType"), |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
399 stGetSetting("previewImageQuality"), |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
400 FALSE, FALSE); |
882 | 401 |
969 | 402 wtConvertImage( |
403 $inFilename, | |
404 $pdata["files"]["thumb"]["file"], | |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
405 stGetSetting("previewThumbSize"), |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
406 stGetSetting("previewThumbType"), |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
407 stGetSetting("previewThumbQuality"), |
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
408 TRUE, FALSE); |
942 | 409 break; |
941 | 410 } |
963 | 411 |
933 | 412 return TRUE; |
882 | 413 } |
414 | |
415 | |
884 | 416 function wtHandleEntry($compo, $entry, $mode) |
417 { | |
418 } | |
419 | |
420 | |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
421 // |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
422 // Crop a filename to specified length |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
423 // |
986 | 424 function wtCropFilename($filename, $len) |
425 { | |
426 if ($len !== FALSE && strlen($filename) > $len) | |
427 { | |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
428 // Check if the filename has a path and attempt |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
429 // to get the filename extension |
986 | 430 $fext = ""; |
431 $dpos = strrpos($filename, "/"); | |
432 if (($spos = strrpos($filename, ".")) !== FALSE && | |
433 ($dpos === FALSE || $dpos < $spos)) | |
434 $fext = substr($filename, $spos); | |
435 | |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
436 // Is the filename extension too long, if yes, just dump it |
986 | 437 if (strlen($fext) >= $len) |
438 $fext = ""; | |
439 | |
987 | 440 return substr($filename, 0, $len - strlen($fext) - 1).$fext; |
986 | 441 } |
442 else | |
443 return $filename; | |
444 } | |
445 | |
446 | |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
447 // |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
448 // Recursively goes through directory/files and subdirectories, |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
449 // cropping file/dir names to specified length. |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
450 // |
986 | 451 function wtCropFilenamesRec($path, $len) |
452 { | |
453 $dir = opendir($path); | |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
454 while (($dentry = readdir($dir)) !== FALSE) |
986 | 455 { |
456 $fname = $path."/".$dentry; | |
987 | 457 $nname = $path."/".wtCropFilename($dentry, $len); |
986 | 458 if ($dentry != "." && $dentry != "..") |
459 { | |
460 if (is_dir($fname)) | |
461 { | |
462 wtCropFilenamesRec($fname, $len); | |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
463 if (rename($fname, $nname) === FALSE) |
987 | 464 { |
465 echo "ERROR: Could not rename DIR '".$fname."' -> '".$nname."'.\n"; | |
466 return FALSE; | |
467 } | |
986 | 468 } |
469 else | |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
470 if (is_file($fname) && rename($fname, $nname) === FALSE) |
987 | 471 { |
472 echo "ERROR: Could not rename FILE '".$fname."' -> '".$nname."'.\n"; | |
473 return FALSE; | |
474 } | |
986 | 475 } |
476 } | |
477 closedir($dir); | |
478 } | |
479 | |
480 | |
991 | 481 function wtUnpackEntry($compo, $entry, $pathPrefix, $useOrig, $cropNames, $copyOnly) |
963 | 482 { |
483 global $setEntryPath; | |
484 | |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
485 // Get latest file for the entry |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
486 if (($efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"])) === FALSE) |
963 | 487 { |
488 echo "INFO: No entry file for ".wtNiceName($compo, $entry, $efile)."\n"; | |
489 return FALSE; | |
490 } | |
491 | |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
492 // Create source file full path |
963 | 493 $filename = stMakePath(FALSE, TRUE, array($setEntryPath, $compo["cpath"], $efile["filename"])); |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
494 |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
495 // Check if the file exists |
963 | 496 if (!file_exists($filename)) |
497 { | |
498 echo "ERROR: Entry file '".$filename."' for ".wtNiceName($compo, $entry, FALSE)." does not exist!\n"; | |
499 return FALSE; | |
500 } | |
501 | |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
502 // Get file data |
963 | 503 $edata = stProbeFileInfo($filename, TRUE); |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
504 if ($edata === FALSE) |
963 | 505 { |
506 echo | |
507 "ERROR: Invalid/unsupported file type for entry ".wtNiceName($compo, $entry, $efile)."\n"; | |
508 return FALSE; | |
509 } | |
510 | |
1050 | 511 // Make information file contents |
512 $sbinfo = [ | |
513 "FAP Entry Information", | |
514 "=====================", | |
515 "Compo : ".$compo["name"]." (ID #".$compo["id"].")", | |
516 "Entry : '".$entry["name"]."' by ".$entry["author"]." (ID #".$entry["id"].")", | |
517 "Show # : ".($entry["show_id"] > 0 ? $entry["show_id"] : "NOT SET!"), | |
518 ]; | |
519 | |
520 if (strlen($entry["info"]) > 0) | |
521 { | |
522 $sbinfo[] = ""; | |
523 $sbinfo[] = "INFO:"; | |
524 foreach (preg_split("/\r\n|\n|\r/", $entry["info"]) as $sline) | |
525 $sbinfo[] = $sline; | |
526 } | |
527 | |
528 if (strlen($entry["notes"]) > 0) | |
529 { | |
530 $sbinfo[] = ""; | |
531 $sbinfo[] = "INTERNAL NOTES:"; | |
532 foreach (preg_split("/\r\n|\n|\r/", $entry["notes"]) as $sline) | |
533 $sbinfo[] = $sline; | |
534 } | |
535 | |
536 $sbinfo[] = ""; | |
1063 | 537 $sbinfoStr = implode("\n", $sbinfo); |
1050 | 538 |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
539 // Create the destination directory |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
540 if (wtMakeDir(stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"])), 0755) === FALSE) |
963 | 541 return FALSE; |
542 | |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
543 // Form the destination path and/or filename |
991 | 544 if ($copyOnly) |
545 $dstFileBase = $useOrig ? $efile["origname"] : $efile["filename"]; | |
546 else | |
547 $dstFileBase = wtCropFilename($entry["show_id"]."-".($useOrig ? $efile["origname"] : $efile["filename"]), $cropNames); | |
963 | 548 |
1050 | 549 $dstPath = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], stReplaceFileExt($dstFileBase, ""))); |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
550 |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
551 // Handle based on class/type and whether we are just copying or not |
1050 | 552 if ($copyOnly) |
553 { | |
554 $dstFilename = stMakePath(FALSE, FALSE, array($pathPrefix, $compo["cpath"], $dstFileBase)); | |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
555 if (copy($filename, $dstFilename) === FALSE) |
1050 | 556 { |
557 echo "ERROR: Failed to copy '".$filename."' to '".$dstFilename."'\n"; | |
558 return FALSE; | |
559 } | |
560 } | |
561 else | |
562 if ($edata["class"] == EFILE_ARCHIVE) | |
963 | 563 { |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
564 // Entry is an archive file, so unpack it |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
565 if (wtUnpackArchiveTo($edata["id"], $filename, $dstPath) === FALSE) |
963 | 566 return FALSE; |
986 | 567 |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
568 // Crop the filenames from the unpacked archive, if we need to |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
569 if ($cropNames !== FALSE) |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
570 wtCropFilenamesRec($dstPath, $cropNames); |
1050 | 571 |
963 | 572 } |
573 else | |
574 { | |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
575 // We have a single file (or copyOnly mode) |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
576 if (wtMakeDir($dstPath, 0755) === FALSE) |
1050 | 577 return FALSE; |
578 | |
1053 | 579 $dstFilename = stMakePath(FALSE, FALSE, array($dstPath, wtCropFilename($efile["origname"], $cropNames))); |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
580 if (copy($filename, $dstFilename) === FALSE) |
963 | 581 { |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
582 echo "ERROR: Failed to copy '".$filename."' to '".$dstFilename."'\n"; |
963 | 583 return FALSE; |
584 } | |
585 } | |
586 | |
1050 | 587 $dstFilename = stMakePath(FALSE, FALSE, array($dstPath, "fapinfo.txt")); |
1063 | 588 if (@file_put_contents($dstFilename, $sbinfoStr) === FALSE) |
1050 | 589 { |
590 echo "ERROR: Failed to output '".$dstFilename."'\n"; | |
591 return FALSE; | |
592 } | |
593 | |
963 | 594 return TRUE; |
595 } | |
596 | |
597 | |
827 | 598 // |
941 | 599 // Misc helper functions |
827 | 600 // |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
601 |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
602 function wtNiceName($compo, $entry, $efile = FALSE) |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
603 { |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
604 return sprintf( |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
605 "%d: %s by %s%s", |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
606 $entry["id"], |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
607 $entry["name"], |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
608 $entry["author"], |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
609 ($efile !== FALSE) ? " [".$efile["filename"]." / TYPE: '".$efile["filetype"]."']" : ""); |
993
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
610 } |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
611 |
fbc2e6514dfa
Cleanups and improved comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
612 |
941 | 613 function wtExec($exe, $args, $expect = 0) |
614 { | |
984
03b245ef3a47
Various fixes to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
983
diff
changeset
|
615 echo "EXEC: ".$exe." @ ".$args."\n"; |
941 | 616 exec(escapeshellcmd($exe)." ".$args, $output, $code); |
617 if ($code !== $expect) | |
618 { | |
619 echo | |
953 | 620 "ERROR: Executing ".$exe.": ".$code."\n".$args."\n". |
941 | 621 "------------------------------------------------\n". |
622 implode("\n", $output). | |
623 "------------------------------------------------\n"; | |
624 return FALSE; | |
625 } | |
626 return TRUE; | |
627 } | |
628 | |
629 | |
630 function wtExecOrDie($exe, $args) | |
631 { | |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
632 if (wtExec($exe, $args) === FALSE) |
941 | 633 die(); |
634 } | |
635 | |
636 | |
637 function wtPurgeDir($path) | |
638 { | |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
639 if ($path != "" && $path !== FALSE && file_exists($path) && is_dir($path)) |
941 | 640 { |
969 | 641 // echo "PURGING: ".$path."\n"; |
942 | 642 foreach (scandir($path) as $file) |
643 if ($file !== "." && $file !== "..") | |
644 { | |
645 $sub = $path."/".$file; | |
646 if (is_dir($sub)) | |
647 wtPurgeDir($sub); | |
648 else | |
649 unlink($sub); | |
650 } | |
651 rmdir($path); | |
941 | 652 } |
653 } | |
654 | |
655 | |
935 | 656 function wtMakeDir($path, $perm) |
827 | 657 { |
658 if (!file_exists($path)) | |
659 { | |
953 | 660 echo "INFO: Creating ".$path."\n"; |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
661 if (mkdir($path, $perm, TRUE) === FALSE) |
827 | 662 die("Could not create directory '".$path."'\n"); |
663 } | |
664 } | |
665 | |
666 | |
935 | 667 function wtInitializeDirs() |
827 | 668 { |
902 | 669 global |
670 $setEntryPath, $setPreviewPath, $setThumbDir, | |
827 | 671 $setEntryPathPerms, $setPrevPathPerms; |
672 | |
673 echo "Checking for missing directories ...\n"; | |
935 | 674 wtMakeDir($setEntryPath, $setEntryPathPerms); |
675 wtMakeDir($setPreviewPath, $setPrevPathPerms); | |
827 | 676 |
677 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo) | |
678 { | |
935 | 679 wtMakeDir(stMakePath(FALSE, FALSE, array($setEntryPath, $compo["cpath"])), $setEntryPathPerms); |
680 wtMakeDir(stMakePath(FALSE, FALSE, array($setPreviewPath, $compo["cpath"])), $setPrevPathPerms); | |
681 wtMakeDir(stMakePath(FALSE, FALSE, array($setPreviewPath, $compo["cpath"], $setThumbDir)), $setPrevPathPerms); | |
827 | 682 } |
683 } | |
684 | |
685 | |
968 | 686 function wtPrintCompoHeader($compo) |
687 { | |
688 global $setTermWidth; | |
689 printf("==%'=-".($setTermWidth-2)."s\n", | |
690 sprintf("[ #%03d - %s ]", $compo["id"], substr($compo["name"], 0, 40)) | |
691 ); | |
692 } | |
693 | |
694 | |
695 function wtPrintDivider($chr = "-") | |
696 { | |
697 global $setTermWidth; | |
698 echo str_repeat($chr, $setTermWidth)."\n"; | |
699 } | |
700 | |
701 | |
935 | 702 function wtShowHelp() |
827 | 703 { |
935 | 704 global $argv; |
827 | 705 echo |
706 "faptool - Do stuff with FAPWeb database\n". | |
1076 | 707 "(C) Copyright 2014-2017 ccr/TNSP\n". |
827 | 708 "\n". |
917 | 709 "Usage: ".$argv[0]." <command> [args]\n". |
710 "Where command is one of following:\n". | |
827 | 711 "\n". |
712 " init\n". | |
713 " Create directories for entries and previews, if needed.\n". | |
714 "\n". | |
928 | 715 /* |
917 | 716 " clean [delete|crap]\n". |
717 " Clean files marked to be deleted and 'stale' files. This includes\n". | |
718 " any old or stale preview files and entry files.\n". | |
719 " Without 'delete' parameter just checks and shows what would deleted.\n". | |
720 " 'crap' parameter will purge EVERYTHING that is NOT in the database,\n". | |
721 " e.g. any other files, like temp files, etc. DANGEROUS IF YOU DO NOT\n". | |
722 " KNOW WHAT YOU ARE DOING!!!!!\n". | |
901 | 723 "\n". |
928 | 724 */ |
938 | 725 " probe <filename> [filename2 ..]\n". |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
726 " Probe specified file for file information.\n". |
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
727 "\n". |
881 | 728 " previews <cmd> [args]\n". |
729 " Where <cmd> is one of:\n". | |
933 | 730 " 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
|
731 " generate - Generate preview files that are missing OR out of date.\n". |
928 | 732 " (older then preview source file OR entry file where appropriate)\n". |
901 | 733 "\n". |
734 " entry <cmd> [args]\n". | |
933 | 735 " status [cid] - List entries for all compos or <cid>.\n". |
966 | 736 " unpack <path> - Create subdirs and copy all entries UNPACKED there.\n". |
991 | 737 " release <path> - Like unpack, but just copies files (does not unpack archives).\n". |
1031 | 738 "\n". |
739 " add <cid> <filename> - Add a new entry with file.\n". | |
740 " Entry info + notes for internal use will be prompted.\n". | |
741 " pre <eid> <filename> - Add file as preview for entry id.\n". | |
742 " del <eid> - Delete entry (does not delete files).\n". | |
928 | 743 /* |
1031 | 744 "\n". |
928 | 745 " package <lha|zip|rar|7z>\n". |
746 " Repackages/packages entries (unpacks first if archive),\n". | |
747 " and adds generated infofile.txt to packages.\n". | |
748 */ | |
827 | 749 "\n"; |
935 | 750 } |
751 | |
1005 | 752 |
753 function wtSigHandler($signo) | |
754 { | |
755 global $flagQuit; | |
756 switch ($signo) | |
757 { | |
758 case SIGTERM: | |
759 mgFatal("Received SIGTERM.\n"); | |
760 break; | |
761 | |
762 case SIGQUIT: | |
763 case SIGINT: | |
764 $flagQuit = TRUE; | |
765 break; | |
1013
720078ca799e
Oops, a missing bracket. Fixed.
Matti Hamalainen <ccr@tnsp.org>
parents:
1006
diff
changeset
|
766 } |
1005 | 767 } |
768 | |
769 | |
935 | 770 // |
771 // Main program starts | |
772 // | |
944
535466595a5a
Move CLI exec check to program start.
Matti Hamalainen <ccr@tnsp.org>
parents:
943
diff
changeset
|
773 stCheckCLIExec(); |
935 | 774 if ($argc < 2) |
775 { | |
776 wtShowHelp(); | |
827 | 777 exit; |
778 } | |
779 | |
1005 | 780 pcntl_signal(SIGTERM, "wtSigHandler"); |
781 pcntl_signal(SIGHUP, "wtSigHandler"); | |
782 pcntl_signal(SIGQUIT, "wtSigHandler"); | |
783 pcntl_signal(SIGINT, "wtSigHandler"); | |
935 | 784 |
827 | 785 // Try to connect to database |
786 $spec = stGetSetting("sqlDB"); | |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
787 if (($db = stConnectSQLDBSpec($spec)) === FALSE) |
827 | 788 die("Could not connect to SQL database '".$spec."'.\n"); |
789 | |
790 echo "Using database spec '".$spec."'.\n"; | |
791 | |
792 // Fetch non-"hardcoded" settings from SQL database | |
793 stReloadSettings(); | |
794 | |
795 // Set some globals for our benefit | |
922 | 796 $setTermWidth = 75; |
827 | 797 $setEntryPath = stGetSetting("entryPath"); |
798 $setPreviewPath = stGetSetting("previewPath"); | |
799 $setPreviewURL = stGetSetting("previewURL"); | |
800 $setThumbDir = stGetSetting("thumbnailSubDir"); | |
832 | 801 $setEntryPathPerms = stGetSetting("entryPathPerms"); |
802 $setPrevPathPerms = stGetSetting("previewPathPerms"); | |
827 | 803 |
804 if ($setEntryPath === FALSE || $setPreviewPath === FALSE || | |
832 | 805 $setPreviewURL === FALSE || $setThumbDir === FALSE || |
806 $setEntryPathPerms === FALSE || $setPrevPathPerms === FALSE) | |
827 | 807 { |
922 | 808 die("Some required settings are not defined in mconfig.inc.php!\n"); |
827 | 809 } |
810 | |
811 | |
812 // Act according to specified command | |
1006
cb23b23a2d8b
Improve commandline parsing, fix some potential bug situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
1005
diff
changeset
|
813 switch (stCArgLC(1, 3)) |
827 | 814 { |
935 | 815 case "-?": case "--h": |
816 wtShowHelp(); | |
817 exit; | |
818 break; | |
819 | |
884 | 820 case "ini": |
827 | 821 // |
822 // Initialize the data directories etc | |
823 // | |
953 | 824 echo "INFO: Checking for missing directories ...\n"; |
935 | 825 wtInitializeDirs(); |
827 | 826 break; |
827 | |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
828 case "pro": |
945 | 829 // |
830 // Probe specified files | |
831 // | |
832 if ($argc < 2) | |
833 die("No filename specified.\n"); | |
834 | |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
835 if (($finfo = finfo_open()) === FALSE) |
954 | 836 die("Internal error. Failed to initialize finfo()."); |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
837 |
945 | 838 for ($i = 2; $i < $argc; $i++) |
839 { | |
840 // Probe one file | |
841 $filename = $argv[$i]; | |
842 $sdata = @finfo_file($finfo, $filename, FILEINFO_NONE); | |
843 $smime = @finfo_file($finfo, $filename, FILEINFO_MIME_TYPE); | |
844 | |
845 echo | |
846 "\n". | |
847 "File : ".$filename."\n". | |
848 "Probed : ".$sdata."\n". | |
849 "Mime : ".$smime."\n"; | |
850 | |
1104
0a2117349f46
s/true/TRUE/g; s/false/FALSE/g;
Matti Hamalainen <ccr@tnsp.org>
parents:
1083
diff
changeset
|
851 if (($info = stProbeFileInfo($filename, TRUE)) !== FALSE) |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
852 { |
945 | 853 $tmp = array(); |
854 foreach ($info as $ikey => $ival) | |
855 $tmp[] = $ikey."='".$ival."'"; | |
856 | |
857 echo "Data : ".implode(", ", $tmp)."\n"; | |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
858 } |
946 | 859 } |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
860 |
946 | 861 finfo_close($finfo); |
925
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
862 break; |
5d53b9d6c319
Add 'probe' function to faptool.
Matti Hamalainen <ccr@tnsp.org>
parents:
922
diff
changeset
|
863 |
901 | 864 case "cle": |
947 | 865 // |
866 // File cleanup / deletion | |
867 // | |
1083 | 868 $mode = stCArgLC(2, 3); |
869 switch ($mode) | |
901 | 870 { |
1083 | 871 case "del": |
872 case FALSE: | |
873 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo) | |
874 if (stFetchSQLColumn("SELECT COUNT(*) FROM entries WHERE compo_id=".$compo["id"]) > 0) | |
875 { | |
876 wtPrintCompoHeader($compo); | |
877 wtPrintDivider(); | |
901 | 878 |
1083 | 879 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) |
901 | 880 { |
1083 | 881 foreach (stExecSQL("SELECT * FROM files WHERE entry_id=".$entry["id"]) as $efile) |
882 { | |
883 if ($efile["deleted"] != 0) | |
884 { | |
885 echo "X"; | |
886 } | |
887 } | |
901 | 888 } |
1083 | 889 echo "\n"; |
890 wtPrintDivider(); | |
901 | 891 } |
1083 | 892 break; |
1120
b2bca5f6d0ff
Cosmetic cleanup: remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1104
diff
changeset
|
893 |
1083 | 894 case "cra": |
895 echo "Not implemented. :D\n"; | |
896 break; | |
1120
b2bca5f6d0ff
Cosmetic cleanup: remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1104
diff
changeset
|
897 |
1083 | 898 default: |
899 die("ERROR! Invalid entry sub-command '".stCArg(2)."'.\n"); | |
900 break; | |
901 | 901 } |
902 break; | |
903 | |
904 case "ent": | |
905 // | |
906 // Entry files handling | |
907 // | |
1006
cb23b23a2d8b
Improve commandline parsing, fix some potential bug situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
1005
diff
changeset
|
908 $mode = stCArgLC(2, 3); |
901 | 909 switch ($mode) |
910 { | |
911 case "sta": | |
949
2179600710d1
Add "lis" as alias to "sta".
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
912 case "lis": |
1006
cb23b23a2d8b
Improve commandline parsing, fix some potential bug situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
1005
diff
changeset
|
913 $sql = (stCArg(3) !== FALSE) ? " AND id=".intval(stCArg(3)) : ""; |
933 | 914 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL".$sql) as $compo) |
901 | 915 if (stFetchSQLColumn("SELECT COUNT(*) FROM entries WHERE compo_id=".$compo["id"]) > 0) |
916 { | |
968 | 917 wtPrintCompoHeader($compo); |
918 wtPrintDivider(); | |
901 | 919 |
920 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
921 wtHandleEntry($compo, $entry, $mode); | |
922 | |
968 | 923 wtPrintDivider(); |
901 | 924 } |
925 break; | |
926 | |
963 | 927 case "unp": |
1006
cb23b23a2d8b
Improve commandline parsing, fix some potential bug situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
1005
diff
changeset
|
928 if (($setPrefix = stCArg(3)) === FALSE) |
963 | 929 die("Unpack needs a destination path.\n"); |
930 | |
931 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo) | |
932 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
933 { | |
991 | 934 // wtUnpackEntry($compo, $entry, $setPrefix, TRUE, 32, FALSE); |
935 wtUnpackEntry($compo, $entry, $setPrefix, TRUE, FALSE, FALSE); | |
936 } | |
937 break; | |
938 | |
939 case "rel": | |
1006
cb23b23a2d8b
Improve commandline parsing, fix some potential bug situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
1005
diff
changeset
|
940 if (($setPrefix = stCArg(3)) === FALSE) |
991 | 941 die("Releases needs a destination path.\n"); |
942 | |
943 if (!file_exists($setPrefix) || is_dir($setPrefix)) | |
944 { | |
945 // Create new one | |
946 wtMakeDir($setPrefix, 0755); | |
1120
b2bca5f6d0ff
Cosmetic cleanup: remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1104
diff
changeset
|
947 |
991 | 948 // Create results.txt if it does not exist |
949 $resultsFile = stMakePath(FALSE, TRUE, array($setPrefix, "results.txt")); | |
950 if (!file_exists($resultsFile)) | |
951 { | |
952 // Use a header file if it exists | |
953 $resultsHeader = "results.logo"; | |
954 if (file_exists($resultsHeader) && is_file($resultsHeader)) | |
955 $resultsText = file_get_contents($resultsHeader); | |
956 else | |
957 { | |
958 $resultsText = $pageTitle." results"; | |
959 $resultsText .= "\n".str_repeat("=", strlen($resultsText))."-- -\n\n"; | |
960 } | |
961 | |
962 file_put_contents( | |
963 $resultsFile, | |
964 $resultsText.stGetCompoResultsASCIIStr(FALSE, 0) | |
965 ); | |
966 } | |
1120
b2bca5f6d0ff
Cosmetic cleanup: remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1104
diff
changeset
|
967 |
991 | 968 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo) |
969 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
970 { | |
971 wtUnpackEntry($compo, $entry, $setPrefix, TRUE, FALSE, TRUE); | |
972 } | |
963 | 973 } |
974 break; | |
975 | |
901 | 976 case "pac": |
929 | 977 echo "Not implemented. :D\n"; |
901 | 978 break; |
979 | |
980 case "add": | |
929 | 981 echo "Not implemented. :D\n"; |
901 | 982 break; |
983 | |
984 default: | |
1083 | 985 if ($mode == FALSE) |
901 | 986 die("ERROR! Entry command requires a sub-command argument.\n"); |
987 else | |
988 die("ERROR! Invalid entry sub-command '".stCArg(2)."'.\n"); | |
989 break; | |
990 } | |
991 break; | |
992 | |
884 | 993 case "pre": |
881 | 994 // |
995 // Preview files handling | |
996 // | |
1006
cb23b23a2d8b
Improve commandline parsing, fix some potential bug situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
1005
diff
changeset
|
997 $mode = stCArgLC(2, 3); |
881 | 998 switch ($mode) |
999 { | |
970
9c93a3c4f95c
Clean out the debug code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
969
diff
changeset
|
1000 case "gen": |
983
1e347288712d
Add "upd" option to previews generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
971
diff
changeset
|
1001 case "upd": |
881 | 1002 case "sta": |
949
2179600710d1
Add "lis" as alias to "sta".
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
1003 case "lis": |
1006
cb23b23a2d8b
Improve commandline parsing, fix some potential bug situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
1005
diff
changeset
|
1004 $sql = (stCArg(3) !== FALSE) ? " AND id=".intval(stCArg(3)) : ""; |
933 | 1005 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL".$sql) as $compo) |
898 | 1006 if (stFetchSQLColumn("SELECT COUNT(*) FROM entries WHERE compo_id=".$compo["id"]) > 0) |
881 | 1007 { |
968 | 1008 wtPrintCompoHeader($compo); |
881 | 1009 printf( |
898 | 1010 "PrevType : %s\n", |
1011 $previewTypeList[$compo["preview_type"]][0]); | |
941 | 1012 |
968 | 1013 if ($mode == "sta" || $mode == "lis") |
941 | 1014 { |
1015 printf(" %-3s | %-3s | %-40s |\n", | |
968 | 1016 "#ID", "EPV", "Entry name/author"); |
941 | 1017 } |
1018 | |
968 | 1019 wtPrintDivider("-"); |
881 | 1020 |
1021 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
1022 wtHandleEntryPreview($compo, $entry, $mode); | |
898 | 1023 |
968 | 1024 wtPrintDivider("="); |
1025 } | |
1026 if ($mode == "sta" || $mode == "lis") | |
1027 { | |
1028 echo "Flags: E = Entry has file, P = Has preview file, V = Generated previews are up to date.\n"; | |
881 | 1029 } |
1030 break; | |
1031 | |
1032 default: | |
1083 | 1033 if ($mode == FALSE) |
881 | 1034 die("ERROR! Previews command requires a sub-command argument.\n"); |
1035 else | |
1036 die("ERROR! Invalid previews sub-command '".stCArg(2)."'.\n"); | |
1037 break; | |
1038 } | |
1039 break; | |
1040 | |
827 | 1041 default: |
1042 echo "ERROR! Invalid operation mode '".stCArg(1)."'.\n"; | |
1043 break; | |
1044 } | |
1045 | |
941 | 1046 /* Dummy |
1047 { | |
1120
b2bca5f6d0ff
Cosmetic cleanup: remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1104
diff
changeset
|
1048 |
941 | 1049 } |
1050 */ | |
827 | 1051 ?> |