827
|
1 #!/usr/bin/php
|
|
2 <?
|
|
3 require_once "mconfig.inc.php";
|
|
4 require_once "msite.inc.php";
|
|
5
|
|
6 stCheckCLIExec();
|
|
7
|
|
8
|
882
|
9 function wtExecOrDie($exe, $args)
|
|
10 {
|
|
11 echo "EXEC: ".$exe." ".$args."\n";
|
|
12 //passthru($exe." ".$args) == 0 or die("Error executing ".$exe.":\n".$args);
|
|
13 }
|
|
14
|
|
15
|
|
16 function wtConvertImage($inFilename, $inFileType, $outFilename, $setDim, $setFormat, $setQuality, $thumb)
|
|
17 {
|
|
18 global $setPreviewPath;
|
|
19
|
|
20 if (($outDim = stGetSetting($setDim)) === FALSE ||
|
|
21 ($outFormat = stGetSetting($osetFormat)) === FALSE ||
|
|
22 ($outQuality = stGetSetting($setQuality)) === FALSE)
|
|
23 {
|
|
24 die("Missing one of res/format/quality settings for '".$outFilename."'\n");
|
|
25 }
|
|
26
|
884
|
27 if ($inFileType == "gfx")
|
|
28 {
|
|
29 // Oh great .. we need gfxconv here because Imagick handles ILBM like shit
|
|
30 $filename = tempnam($setPreviewPath, "tmp");
|
|
31 wtExecOrDie(
|
|
32 "/usr/local/bin/gfxconv",
|
|
33 escapeshellarg($inFilename)." -f png -o ".escapeshellarg($filename));
|
|
34 }
|
|
35 else
|
|
36 $filename = $inFilename;
|
|
37
|
882
|
38 // convert -resize 640x480 -background black -gravity center -extent 640x480
|
|
39 // -unsharp "0x0.75+0.75+0.008" lol.lbm -quality 95 test.jpg
|
884
|
40
|
|
41 // Create conversion entity
|
|
42 $img = new Imagick($filename);
|
|
43 if ($img === false)
|
|
44 die("Oh noes! ImageMagick could not digest the file '".$filename."' (".$inFilename.")\n");
|
|
45
|
|
46 // Get dimensions, setup background
|
|
47 $dim = $img->getImageGeometry();
|
|
48 $img->setBackgroundColor(imagick::COLOR_BLACK);
|
|
49 $img->setGravity(imagick::GRAVITY_CENTER);
|
|
50
|
|
51 // Act based on image size vs. desired size and $thumb mode
|
|
52 if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1])
|
|
53 {
|
|
54 // Image is larger
|
|
55 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_QUADRATIC, 1);
|
|
56 $img->setExtent($outDim[0], $outDim[1]);
|
|
57 $img->normalizeImage();
|
|
58 $img->unsharpMaskImage(0, 0.5, 1, 0.05);
|
|
59 }
|
|
60 else
|
|
61 if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1])
|
|
62 {
|
|
63 // Image is smaller than requested dimension(s)?
|
|
64 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_POINT, 1);
|
|
65 $img->setExtent($outDim[0], $outDim[1]);
|
|
66 }
|
|
67
|
|
68 $img->setFormat($outFormat);
|
|
69 $img->setCompressionQuality($outQuality);
|
|
70
|
|
71 $img->stripImage();
|
|
72 $img->writeImage($outFilename);
|
|
73 $img->removeImage();
|
882
|
74 }
|
|
75
|
|
76
|
898
|
77 function wtRenderSample($inFilename, $outFilename)
|
882
|
78 {
|
898
|
79 $sfreq = intval(stGetSetting("sampleFreq"));
|
|
80 $sduration = intval(stGetSetting("sampleDuration"));
|
|
81 $schannels = intval(stGetSetting("sampleChannels"));
|
882
|
82
|
884
|
83 wtExecOrDie(
|
898
|
84 "/usr/local/bin/openmpt123"
|
|
85 ,
|
884
|
86 "--quiet --render ".
|
|
87 "--samplerate ".$sfreq." ".
|
|
88 "--channels ".$schannels." ".
|
|
89 "--playtime ".$sduration." ".
|
898
|
90 escapeshellarg($inFilename)." -o ".escapeshellarg($outFilename));
|
882
|
91 }
|
|
92
|
|
93
|
898
|
94 function wtConvertSample($inFilename, $outFilename, $opts)
|
882
|
95 {
|
898
|
96 $sfreq = intval(stGetSetting("sampleFreq"));
|
|
97 $sduration = intval(stGetSetting("sampleDuration"));
|
|
98 $schannels = intval(stGetSetting("sampleChannels"));
|
882
|
99
|
898
|
100 foreach ($opts as $okey => $oval)
|
|
101 $optStr .= $okey." ".$oval." ";
|
|
102
|
|
103 wtExecOrDie(
|
|
104 "/usr/local/bin/avconv",
|
|
105 "-y -t ".intval($sduration)." ".
|
|
106 "-i ".escapeshellarg($inFilename).
|
|
107 " ".$optStr." -ac ".$schannels.
|
|
108 " -map_metadata -1 ".
|
|
109 escapeshellarg($outFilename));
|
882
|
110 }
|
|
111
|
|
112
|
|
113 function wtPurgeDir($path)
|
|
114 {
|
|
115 if (file_exists($path))
|
|
116 wtExecOrDie("/bin/echo", "-fR ".escapeshellarg($path));
|
|
117 }
|
|
118
|
|
119
|
|
120 function wtScanArchive($compo, $efile, $filename)
|
|
121 {
|
|
122 global $setEntryPath;
|
|
123
|
|
124 $path = stMakePath(FALSE, FALSE, array($setEntryPath, "UNPACKS", $filename));
|
|
125
|
|
126 echo "Attempting to scan archive file '".$filename."' ...\n";
|
|
127
|
|
128 // Check file type before doing anything
|
|
129 switch ($efile["id"])
|
|
130 {
|
|
131 case "LHA":
|
|
132 $exe = "/usr/bin/lha";
|
|
133 $args = "e ".escapeshellarg($filename);
|
|
134 break;
|
|
135
|
|
136 case "ZIP":
|
|
137 $exe = "/usr/bin/unzip";
|
|
138 $args = "-d ".escapeshellarg($path)." ".escapeshellarg($filename);
|
|
139 break;
|
|
140
|
898
|
141 case "RAR":
|
|
142 $exe = "/usr/bin/rar";
|
|
143 $args = "e ".escapeshellarg($filename);
|
|
144 break;
|
|
145
|
882
|
146 default:
|
898
|
147 echo "Unsupported archive file type: ".$efile["id"]."\n";
|
|
148 return FALSE;
|
882
|
149 }
|
|
150
|
|
151 // Create temporary directory
|
|
152 wtPurgeDir($path);
|
|
153 stMakeDir($path, 0700);
|
|
154
|
|
155 if (!is_dir($path) || chdir($path) === false)
|
|
156 die("Failed to chdir to ".$path."\n");
|
|
157
|
|
158 // Unpack archive
|
|
159 wtExecOrDie($exe, $args);
|
|
160
|
|
161 // Scan through files ...
|
|
162 $dir = opendir($path);
|
|
163 while (($dentry = readdir($dir)) !== false)
|
|
164 {
|
|
165 }
|
|
166 closedir($dir);
|
|
167
|
|
168 wtPurgeDir($path);
|
|
169 }
|
|
170
|
|
171
|
|
172 function wtHandleEntryPreview($compo, $entry, $mode)
|
|
173 {
|
898
|
174 // Get current preview file data
|
|
175 if (!stGetPreviewFileData($compo, $entry, $pdata))
|
|
176 return FALSE;
|
|
177
|
|
178 if ($entry["file_id"] != 0)
|
|
179 $efile = stFetchSQL("SELECT * FROM files WHERE deleted=0 AND id=".$entry["file_id"]);
|
|
180 else
|
|
181 $efile = FALSE;
|
|
182
|
|
183 // Check preview file(s) status
|
|
184 if ($mode == "sta")
|
|
185 {
|
|
186 printf(" %03d | %s%s%s | %-40s | %-5s | %s\n",
|
|
187 $entry["id"],
|
|
188 ($efile !== false) ? "1" : ".",
|
|
189 isset($pdata["file"]) ? "2" : ".",
|
|
190 $pdata["valid"] ? "3" : ".",
|
|
191 $entry["name"]." by ".$entry["author"],
|
|
192 isset($pdata["file"]) ? $pdata["file"]["filetype"] : "",
|
|
193 isset($pdata["file"]) ? $pdata["file"]["filename"] : ""
|
|
194 );
|
|
195 }
|
|
196 else
|
|
197 if ($mode == "upd")
|
|
198 {
|
882
|
199 /*
|
898
|
200 if (previewSourceFile does not exist)
|
882
|
201 {
|
|
202 if (entry is module file)
|
|
203 {
|
|
204 render sample
|
|
205 }
|
|
206 else
|
|
207 if (entry is image file)
|
|
208 {
|
|
209 convert image
|
|
210 }
|
898
|
211 else
|
|
212 if (entry is archive)
|
|
213 {
|
|
214 if compo preview type is image, scan archive for images ..
|
|
215 }
|
|
216 }
|
|
217
|
|
218 if (previewFiles older than previewSourceFile)
|
|
219 {
|
|
220 if (source == audio)
|
|
221 convert via avconv
|
|
222 else
|
|
223 if (source == image)
|
|
224 convert via imagick
|
882
|
225 }
|
|
226
|
|
227 // Based on compo / entry preview type ..
|
|
228
|
|
229 // Convert preview image file, if any
|
|
230 // Create thumbnail for it, too
|
|
231 wtConvertImage($inFilename, $outFilename,
|
|
232 "previewImageSize", "previewImageType",
|
|
233 "previewImageQuality", FALSE);
|
|
234
|
|
235 wtConvertImage($inFilename, $outFilename,
|
|
236 "previewThumbSize", "previewThumbType",
|
|
237 "previewThumbQuality", TRUE);
|
|
238 */
|
898
|
239 }
|
|
240 else
|
|
241 die("OMG!\n");
|
882
|
242 }
|
|
243
|
|
244
|
884
|
245 function wtHandleEntry($compo, $entry, $mode)
|
|
246 {
|
|
247 }
|
|
248
|
|
249
|
827
|
250 //
|
|
251 // Create directories
|
|
252 //
|
|
253 function stMakeDir($path, $perm)
|
|
254 {
|
|
255 if (!file_exists($path))
|
|
256 {
|
|
257 echo " - Creating ".$path."\n";
|
|
258 if (mkdir($path, $perm, TRUE) === false)
|
|
259 die("Could not create directory '".$path."'\n");
|
|
260 }
|
|
261 }
|
|
262
|
|
263
|
|
264 function stInitializeDirs()
|
|
265 {
|
|
266 global $setEntryPath, $setPreviewPath, $setThumbDir,
|
|
267 $setEntryPathPerms, $setPrevPathPerms;
|
|
268
|
|
269 echo "Checking for missing directories ...\n";
|
|
270 stMakeDir($setEntryPath, $setEntryPathPerms);
|
|
271 stMakeDir($setPreviewPath, $setPrevPathPerms);
|
880
|
272 stMakeDir(stMakePath(FALSE, FALSE, array($setPreviewPath, $setThumbDir)), $setPrevPathPerms);
|
827
|
273
|
|
274 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo)
|
|
275 {
|
|
276 stMakeDir(stMakePath(FALSE, FALSE, array($setEntryPath, $compo["cpath"])), $setEntryPathPerms);
|
|
277 }
|
|
278 }
|
|
279
|
|
280
|
|
281 //
|
|
282 // Main program starts
|
|
283 //
|
|
284 if ($argc < 2)
|
|
285 {
|
|
286 echo
|
|
287 "faptool - Do stuff with FAPWeb database\n".
|
|
288 "(C) Copyright 2014 ccr/TNSP\n".
|
|
289 "\n".
|
|
290 "Usage: ".$argv[0]." <mode> [args]\n".
|
|
291 "Where mode is one of following:\n".
|
|
292 "\n".
|
|
293 " init\n".
|
|
294 " Create directories for entries and previews, if needed.\n".
|
|
295 "\n".
|
881
|
296 " previews <cmd> [args]\n".
|
|
297 " Where <cmd> is one of:\n".
|
|
298 " status - List files and show what is missing, etc.\n".
|
827
|
299 "\n";
|
|
300 exit;
|
|
301 }
|
|
302
|
|
303 // Try to connect to database
|
|
304 $spec = stGetSetting("sqlDB");
|
|
305 if (($db = stConnectSQLDBSpec($spec)) === false)
|
|
306 die("Could not connect to SQL database '".$spec."'.\n");
|
|
307
|
|
308 echo "Using database spec '".$spec."'.\n";
|
|
309
|
|
310 // Fetch non-"hardcoded" settings from SQL database
|
|
311 stReloadSettings();
|
|
312
|
|
313 // Set some globals for our benefit
|
900
|
314 $setWidth = 75;
|
827
|
315 $setEntryPath = stGetSetting("entryPath");
|
|
316 $setPreviewPath = stGetSetting("previewPath");
|
|
317 $setPreviewURL = stGetSetting("previewURL");
|
|
318 $setThumbDir = stGetSetting("thumbnailSubDir");
|
832
|
319 $setEntryPathPerms = stGetSetting("entryPathPerms");
|
|
320 $setPrevPathPerms = stGetSetting("previewPathPerms");
|
827
|
321
|
|
322 if ($setEntryPath === FALSE || $setPreviewPath === FALSE ||
|
832
|
323 $setPreviewURL === FALSE || $setThumbDir === FALSE ||
|
|
324 $setEntryPathPerms === FALSE || $setPrevPathPerms === FALSE)
|
827
|
325 {
|
831
|
326 die("Some required settings not defined in mconfig.inc.php!\n");
|
827
|
327 }
|
|
328
|
|
329
|
|
330 // Act according to specified command
|
884
|
331 switch (substr(stCArgLC(1), 0, 3))
|
827
|
332 {
|
884
|
333 case "ini":
|
827
|
334 //
|
|
335 // Initialize the data directories etc
|
|
336 //
|
|
337 stInitializeDirs();
|
|
338 break;
|
|
339
|
884
|
340 case "pre":
|
881
|
341 //
|
|
342 // Preview files handling
|
|
343 //
|
|
344 $mode = substr(stCArgLC(2), 0, 3);
|
|
345 switch ($mode)
|
|
346 {
|
898
|
347 case "upd":
|
881
|
348 case "sta":
|
|
349 foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo)
|
898
|
350 if (stFetchSQLColumn("SELECT COUNT(*) FROM entries WHERE compo_id=".$compo["id"]) > 0)
|
881
|
351 {
|
|
352 printf(
|
900
|
353 "==%'=-".($setWidth-2)."s\n".
|
898
|
354 "PrevType : %s\n",
|
|
355 sprintf("[ #%03d - %s ]", $compo["id"], substr($compo["name"], 0, 40)),
|
|
356 $previewTypeList[$compo["preview_type"]][0]);
|
|
357
|
|
358 printf(" %-3s | %-3s | %-40s |\n",
|
|
359 "#ID", "FLG", "Entry name/author");
|
|
360
|
900
|
361 echo str_repeat("-", $setWidth)."\n";
|
881
|
362
|
|
363 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry)
|
|
364 wtHandleEntryPreview($compo, $entry, $mode);
|
898
|
365
|
900
|
366 echo str_repeat("=", $setWidth)."\n\n";
|
881
|
367 }
|
|
368 break;
|
|
369
|
|
370 default:
|
|
371 if ($mode == "")
|
|
372 die("ERROR! Previews command requires a sub-command argument.\n");
|
|
373 else
|
|
374 die("ERROR! Invalid previews sub-command '".stCArg(2)."'.\n");
|
|
375 break;
|
|
376 }
|
|
377 break;
|
|
378
|
827
|
379 default:
|
|
380 echo "ERROR! Invalid operation mode '".stCArg(1)."'.\n";
|
|
381 break;
|
|
382 }
|
|
383
|
|
384 ?> |