/
jms.inc.php
488 lines (438 loc) · 11.5 KB
/
jms.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
@require_once('config.inc.php');
function content_dir() {
return rtrim(config('content_dir', 'JODI'), '/');
}
function cache_needs_update($name, $max_age = 3600) {
$modified = @filemtime(content_dir() . '/_cache/' . $name . '.html');
return ($modified === false || $max_age < time()-$modified);
}
function filext($fn) {
$pos = strrpos($fn, '.');
if ($pos !== false) {
return substr($fn, $pos+1);
} else {
return '';
}
}
function format_class($s) {
$s = strtolower($s);
$s = str_replace(' ', '-', $s);
$s = str_replace('&', '', $s);
$s = str_replace('/', '-', $s);
return $s;
}
function generate_browse() {
// check if there is anything cached
$cached = load_cached('browse');
if ($cached !== false) {
// yes, output
echo $cached;
flush();
}
// check if we still want/need to generate the page
if (cache_needs_update('browse', config('cache_time'))) {
$data = array();
foreach (list_works() as $name) {
$work = load_work($name);
if ($work === false) {
continue;
}
$data[] = $work;
}
$generated = run_template('template-browse.php', $data);
// save the generated page
save_cached('browse', $generated);
// output if we haven't already above
if ($cached === false) {
echo $generated;
}
}
}
function generate_work($name) {
$data = load_work($name);
if ($data === false) {
return '';
}
return run_template('template-work.php', $data);
}
function get_image_size($fn) {
// try gd
if (function_exists('getimagesize')) {
$ret = @getimagesize($fn);
if ($ret !== false) {
return array('width' => $ret[0], 'height' => $ret[1], 'mime' => $ret['mime']);
}
}
// try imagemagick
if (extension_loaded('imagick')) {
try {
$im = new Imagick($fn);
return $im->getImageGeometry();
} catch (Exception $e) {
}
}
// XXX: use bin?
return false;
}
function get_mime($fn) {
if (function_exists('finfo_file')) {
$finfo = @finfo_open(FILEINFO_MIME_TYPE);
$mime = @finfo_file($finfo, $fn);
@finfo_close($finfo);
}
$mime = false;
if (!@is_string($mime)) {
// heuristic
switch (strtolower(filext($fn))) {
case 'aif':
case 'aiff':
$mime = 'audio/aiff'; break;
case 'avi':
$mime = 'video/x-msvideo'; break;
case 'bmp':
$mime = 'image/bmp'; break;
case 'gif':
$mime = 'image/gif'; break;
case 'jpg':
case 'jpeg':
$mime = 'image/jpeg'; break;
case 'mov':
$mime = 'video/quicktime'; break;
case 'mp3':
$mime = 'audio/mpeg3'; break;
case 'mp4':
$mime = 'video/mp4'; break;
case 'mpg':
case 'mpeg':
$mime = 'video/mpeg'; break;
case 'oga':
case 'ogg':
$mime = 'audio/ogg'; break;
case 'ogv':
$mime = 'video/ogg'; break;
case 'pdf':
$mime = 'application/pdf'; break;
case 'png':
$mime = 'image/png'; break;
case 'tga':
$mime = 'image/tga'; break;
case 'tif':
case 'tiff':
$mime = 'image/tiff'; break;
case 'txt':
$mime = 'text/plain'; break;
case 'wav':
$mime = 'audio/wav'; break;
case 'webm':
$mime = 'video/webm'; break;
case 'zip':
$mime = 'application/zip'; break;
default:
$mime = '';
}
}
return $mime;
}
function get_name_from_url($url) {
$names = list_works();
foreach ($names as $name) {
// same transformations as in get_url_from_name
$transformed = strtolower($name);
$transformed = str_replace(' ', '-', $transformed);
$transformed = str_replace('?', '', $transformed);
$transformed = str_replace('&', '', $transformed);
if ($transformed === $url) {
return $name;
}
}
// not found
return false;
}
function get_url_from_name($name) {
$name = strtolower($name);
$name = str_replace(' ', '-', $name);
$name = str_replace('?', '', $name);
$name = str_replace('&', '', $name);
return 'index.php?object/' . $name;
}
function get_work_name_from_title($title) {
$names = list_works();
foreach ($names as $name) {
$work = load_work($name);
if ($work === false) {
continue;
}
if (isset($work['title']) && $title === $work['title']) {
return $name;
}
}
return false;
}
function list_works() {
$fns = @scandir(content_dir());
if (!is_array($fns)) {
$fns = array();
}
for ($i=0; $i < count($fns); $i++) {
if (in_array($fns[$i], array('.', '..')) ||
substr($fns[$i], 0, 1) == '_') {
array_splice($fns, $i, 1);
$i--;
}
}
return $fns;
}
function load_cached($name) {
if (strpos($name, '../') !== false) {
return false;
}
return @file_get_contents(content_dir() . '/_cache/' . $name . '.html');
}
function load_media($name) {
$fns = @scandir(content_dir() . '/' . $name);
if ($fns === false) {
return false;
}
$media = array();
foreach ($fns as $fn) {
if (in_array($fn, array('.', '..'))) {
continue;
}
if (substr($fn, 0, 1) == '_') {
continue;
}
$category = load_media_category($name, $fn);
if ($category !== false) {
// ignore errors
$media[] = $category;
}
}
// sort categories
$defined_categories = config('media_categories', array());
$needles = array_keys($defined_categories);
for ($i=count($needles)-1; 0 <= $i; $i--) {
// loop through all defined categories in reverse
for ($j=1; $j < count($media); $j++) {
if ($media[$j]['name'] === $needles[$i]) {
// if we find the category, push it to the top
$tmp = array_splice($media, $j, 1)[0];
array_unshift($media, $tmp);
break;
}
}
}
return $media;
}
function load_media_category($work_name, $category_name) {
$fns = @scandir(content_dir() . '/' . $work_name . '/' . $category_name);
if ($fns === false) {
return false;
}
$category = array();
$category['name'] = $category_name;
// check if we have a pretty name for the category
$defined_categories = config('media_categories', array());
if (isset($defined_categories[$category_name])) {
$category['title'] = $defined_categories[$category_name];
} else {
$category['title'] = $category_name;
}
// check if we have custom ordering and descriptions
$descriptions = @file_get_contents(content_dir() . '/' . $work_name . '/' . $category_name . '/list.txt');
if ($descriptions !== false) {
$descriptions = explode("\n", $descriptions);
} else {
$descriptions = array();
}
for ($i = 0; $i < count($descriptions); $i++) {
$tmp = explode(':', $descriptions[$i]);
if (!empty($tmp[0])) {
$descriptions[$i] = array('fn' => $tmp[0], 'description' => ltrim(implode(':', array_slice($tmp, 1))));
} else {
// handle empty lines
array_splice($descriptions, $i, 1);
$i--;
}
}
$category['media'] = array();
foreach ($fns as $fn) {
if (in_array($fn, array('.', '..', 'list.txt'))) {
continue;
}
if (substr($fn, 0, 1) == '_') {
continue;
}
$full_fn = $work_name . '/' . $category_name . '/' . $fn;
if (!@is_file(content_dir() . '/' . $full_fn)) {
continue;
}
$media = array('url' => 'JODI/' . $full_fn, 'fn' => $fn, 'description' => '', 'mime' => get_mime(content_dir() . '/' . $full_fn));
// load image dimensions
if (explode('/', $media['mime'])[0] === 'image') {
$media = array_merge($media, get_image_size(content_dir() . '/' . $full_fn));
}
$category['media'][] = $media;
}
// sort media items
for ($i=count($descriptions)-1; 0 <= $i; $i--) {
// loop through all defined descriptions in reverse
for ($j=0; $j < count($category['media']); $j++) {
if ($category['media'][$j]['fn'] === $descriptions[$i]['fn']) {
// if we find the filename, push it to the top
$tmp = array_splice($category['media'], $j, 1)[0];
// and add the description
$tmp['description'] = $descriptions[$i]['description'];
array_unshift($category['media'], $tmp);
break;
}
}
}
return $category;
}
function load_work($name, $resolve_references = true) {
$s = @file_get_contents(content_dir() . '/' . $name . '/meta.txt');
if ($s === false) {
return false;
}
$work = @json_decode($s, true);
if (is_null($work)) {
// be vocal about error
echo 'Error decoding ' . $name . '/meta.txt';
if (function_exists(json_last_error_msg())) {
echo ': ' . json_last_error_msg();
}
echo '. Try validating the file with http://jsonlint.com/.';
exit(1);
} else if (!is_array($work)) {
echo 'Error decoding ' . $name . '/meta.txt: Not an object.';
exit(2);
}
/* set name */
$work['name'] = $name;
/* set description */
$s = @file_get_contents(content_dir() . '/' . $name . '/description.html');
if ($s !== false) {
$work['description'] = $s;
} else {
$work['description'] = '';
}
/* normalize all other fields */
$work = normalize_work($work, $resolve_references);
// XXX: not necessary for !resolve_references
$media = load_media($name);
if ($media !== false) {
$work['media'] = $media;
} else {
$work['media'] = array();
}
// XXX: as above
$work['primary_representation'] = null;
$fns = @scandir(content_dir() . '/' . $name);
foreach ($fns as $fn) {
if (in_array($fn, array('.', '..', 'description.html', 'meta.txt'))) {
continue;
}
if (substr($fn, 0, 1) === '_') {
continue;
}
if (!@is_file(content_dir() . '/' . $name . '/' . $fn)) {
// not a file
continue;
}
$full_fn = $name . '/' . $fn;
// check mime type
$mime = get_mime(content_dir() . '/' . $full_fn);
if (!in_array(explode('/', $mime)[0], array('image', 'video'))) {
// only accept images and videos
continue;
}
$media = array('url' => 'JODI/' . $full_fn, 'fn' => $fn, 'description' => '', 'mime' => get_mime(content_dir() . '/' . $full_fn));
// load image dimensions
if (explode('/', $media['mime'])[0] === 'image') {
$media = array_merge($media, get_image_size(content_dir() . '/' . $full_fn));
}
$work['primary_representation'] = $media;
}
return $work;
}
function normalize_work($work, $resolve_references = true) {
/* handle "materials" */
if (!@is_array($work['materials'])) {
$work['materials'] = array();
}
/* handle "reference" */
if (!@is_array($work['reference'])) {
$work['reference'] = array();
}
if ($resolve_references) {
$resolved = array();
foreach ($work['reference'] as $reference) {
// get title for each reference
$referenced = load_work($reference, false);
if ($referenced === false) {
// reference not found
continue;
}
$resolved[] = array(
'name' => $reference,
'title' => $referenced['title'],
'url' => get_url_from_name($reference)
);
}
$work['reference'] = $resolved;
}
/* handle "sortYear" */
if (!isset($work['sortYear'])) {
echo 'Field sortYear is not set in ' . $work['name'] . '/meta.txt';
exit(3);
} else if ($work['sortYear'] === 'ongoing') {
// add one to the current year, so that "ongoing" works are always most recent
$work['sortYear'] = intval(date('Y'))+1;
} else {
// force integer
$work['sortYear'] = intval($work['sortYear']);
}
/* handle "title" */
if (!@is_string($work['title'])) {
$work['title'] = $work['name'];
}
/* handle "type" */
if (!@is_string($work['type'])) {
echo 'Field type is not set in ' . $work['name'] . '/meta.txt';
exit(4);
}
/* handle "url_work" */
if (!@is_array($work['url_work'])) {
$work['url_work'] = array();
}
/* handle "year" */
if (!@is_string($work['year'])) {
$work['year'] = '';
}
return $work;
}
function run_template($fn, $data = array()) {
@ob_start();
@include($fn);
$ret = @ob_get_contents();
@ob_end_clean();
return $ret;
}
function save_cached($name, $data) {
if (strpos($name, '../') !== false) {
return false;
}
$old_umask = @umask(0000);
@mkdir(content_dir() . '/_cache/');
$ret = @file_put_contents(content_dir() . '/_cache/' . $name . '.html', $data);
@umask($old_umask);
return $ret;
}
function serve_404() {
@header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
if (is_file('template-404.php')) {
echo run_template('template-404.php');
}
exit(3);
}