-
Notifications
You must be signed in to change notification settings - Fork 42
/
edit.php
419 lines (356 loc) · 11.3 KB
/
edit.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
<?php
/**
* Edit Page
*
* The edit form for adding and editing video data
*
* @todo Add error message for unknown genres
*
* @package videoDB
* @author Andreas Gohr <[email protected]>
* @author Chinamann <[email protected]>
* @version $Id: edit.php,v 2.90 2013/03/11 19:00:26 andig2 Exp $
*/
require_once './core/functions.php';
require_once './core/genres.php';
require_once './core/custom.php';
require_once './core/edit.core.php';
require_once './engines/engines.php';
// check for localnet
localnet_or_die();
/**
* input id
*/
$id = req_int('id');
// multiuser permission check
permission_or_die(PERM_WRITE, ($id) ? get_owner_id($id) : PERM_ANY);
/**
* input
*/
$save = req_int('save');
$ajax_type = req_string('ajax_type');
$ajax_prefetch_id = req_string('ajax_prefetch_id'); // elegant only
$ajax_autocomplete_title = req_string('ajax_autocomplete_title'); // elegent and nexgen
$ajax_autocomplete_subtitle = req_string('ajax_autocomplete_subtitle'); // not used
$ajax_check_duplicate = req_string('ajax_check_duplicate'); // not used
$import = req_string('import');
$lookup = req_int('lookup');
$engine = req_string('engine');
$genres = req_array('genres');
$copy = req_int('copy');
$copyid = req_int('copyid');
// $imdb_set_fields
$md5 = req_string('md5');
$title = req_string('title');
$subtitle = req_string('subtitle');
$language = req_string('language');
$diskid = req_string('diskid');
$mediatype = req_string('mediatype');
$comment = req_string('comment');
$disklabel = req_string('disklabel');
$imdbID = req_string('imdbID');
$year = req_string('year');
$imgurl = req_string('imgurl');
$director = req_string('director');
$actors = req_string('actors');
$runtime = req_string('runtime');
$country = req_string('country');
$plot = req_string('plot');
$filename = req_string('filename');
$filesize = req_string('filesize');
$filedate = req_string('filedate');
$audio_codec = req_string('audio_codec');
$video_codec = req_string('video_codec');
$video_width = req_string('video_width');
$video_height = req_string('video_height');
$istv = req_int('istv');
$rating = req_string('rating');
$custom1 = req_string('custom1');
$custom2 = req_string('custom2');
$custom3 = req_string('custom3');
$custom4 = req_string('custom4');
// from engineGetData (specific imdb)
$mpaa = req_string('mpaa'); // if customXtype == mpaa => imdbdata[mpaa]
$origtitle = req_string('origtitle'); // ?
$tvseries_id = req_string('tvseries_id'); // ?
// from edit.tpl
$autoid = req_string('autoid');
$oldmediatype = req_string('oldmediatype');
$owner_id = req_int('owner_id');
$seen = req_int('seen');
$add_flag = req_int('add_flag');
// clean input data
$genres = (is_array($genres)) ? array_filter($genres) : array();
// ajax autocomplete?
if ($ajax_prefetch_id || $ajax_autocomplete_title || $ajax_autocomplete_subtitle)
{
// add some delay for debugging
if ($config['debug'] && $_SERVER['SERVER_ADDR'] == '127.0.0.1') usleep(rand(200,1000)*1000);
// prefetch external data
if ($ajax_prefetch_id)
{
$data = engineGetData($ajax_prefetch_id, engineGetEngine($ajax_prefetch_id));
if (count($data))
{
$data['imdbID'] = $ajax_prefetch_id;
$data['actors'] = $data['cast'];
$data['imgurl'] = $data['coverurl'];
}
/*
// load languages and config into Smarty
tpl_language();
tpl_edit($data);
$content = $smarty->fetch('edit.tpl');
# file_append(LOG_FILE, $content);
echo $content;
*/
exit;
}
// use subtitle for aka search
$data = ($ajax_autocomplete_title) ?
engineSearch($ajax_autocomplete_title, engineGetDefault()) :
engineSearch($ajax_autocomplete_subtitle, engineGetDefault(), true);
if ($ajax_type == 'json') {
# file_append(LOG_FILE, $res);
header("Content-Type: application/json");
echo(json_encode($data));
exit;
}
foreach ($data as $item)
{
$text = preg_replace('/('.$ajax_autocomplete_title.')/i', '<em>\1</em>', $item['title']);
$text.= (($item['year']) ? "<span class='informal'> (".$item['year'].")</span>" : '');
$ret .= "<li id='".$item['id']."'>".$text."</li>";
}
$ret = "<ul>$ret</ul>";
exit($ret);
}
// duplicate check
if ($ajax_check_duplicate)
{
$q = escapeSQL($ajax_check_duplicate);
$res = runSQL("SELECT id, title FROM ".TBL_DATA." WHERE imdbid='".$q."' OR title LIKE '%".$q."%' AND owner_id=".get_current_user_id());
header('X-JSON: '.json_encode($res));
exit;
}
// XML import
if ($config['xml'] && ($import == 'xml'))
{
require_once './core/xml.php';
// xml file upload
if (isset($_FILES['xmlfile']) && is_uploaded_file($_FILES['xmlfile']['tmp_name']))
{
$file = $_FILES['xmlfile']['tmp_name'];
$xmldata = file_get_contents($file);
unlink($file);
}
// uploading XML data directly or loaded from file
if (!empty($xmldata))
{
$error = '';
$item_id = 0;
require_once './core/xmlimport.php'; // @todo 404
if (function_exists('xmlimport') && ($xmlitems = xmlimport($xmldata, $error)) !== false)
{
// multiple items imported
if ($xmlitems === true)
{
redirect('index.php?filter=new');
}
// exactly one movie imported?
else
{
redirect('show.php?id='.$xmlitems);
}
}
$smarty->assign('xmlerror', $error);
}
// prepare templates
tpl_page();
// display templates
tpl_display('xmlimport.tpl');
exit;
}
// legacy
if ($imdb) $lookup = 1;
// get default lookup mode (0=ignore, 1=lookup, 2=overwrite) if not set
if (!isset($lookup)) $lookup = (empty($id)) ? $lookup = $config['lookupdefault_new'] : $config['lookupdefault_edit'];
// preload old data for refresh all mechanism
if ($lookup > 2)
{
// get a list of movies in DB
$video = runSQL('SELECT * FROM '.TBL_DATA.' WHERE id = '.$id);
// get fields (according to list) from db to be saved later
foreach ($video[0] as $name => $val)
{
if (in_array($name, $imdb_set_fields)) $$name = $val;
}
$owner_id = $video[0]['owner_id'];
// Build a list of all fields which are allowed to be overwritten
$overwrites = array();
foreach ($imdb_set_fields as $field)
{
$tempFieldName = 'update_'.$field;
if (isset($$tempFieldName) && $$tempFieldName == 1) $overwrites[] = $field;
}
$imdb_set_fields = $overwrites;
// valid input values for lookup > 2 are either 5 (add missing) or 6 (overwrite)
$lookup -= 4;
}
// lookup imdb
if ($lookup && $imdbID)
{
// get engine from id
if (empty($engine)) $engine = engineGetEngine($imdbID);
// get external data
$imdbdata = engineGetData($imdbID, $engine);
// lookup cover
if (empty($imgurl) || ($lookup > 1))
{
$imgurl = $imdbdata['coverurl'];
}
// lookup genres
if (count($genres) == 0 || ($lookup > 1))
{
$genres = array();
$gnames = $imdbdata['genres'];
if (isset($gnames))
{
foreach ($gnames as $gname)
{
// check if genre is found- otherwise fail silently
if (is_numeric($genre = getGenreId($gname)))
{
$genres[] = $genre;
}
}
}
}
// lookup actors
if (empty($actors) || ($lookup > 1))
{
$actors = $imdbdata['cast'];
}
// lookup all other fields
foreach (array_keys($imdbdata) as $name)
{
if (in_array($name, array('coverurl', 'genres', 'cast', 'id'))) continue;
// use !$$ as empty($$) doesn't seem to work
if (!$$name || ($lookup > 1))
{
$$name = $imdbdata[$name];
}
}
// custom fields
for ($i=1; $i<=4; $i++)
{
$custom = 'custom'.$i;
$type = $config[$custom.'type'];
if (!empty($type) && isset($$type))
{
// copy imdb data into corresponding custom field
$$custom = $$type;
}
}
}
// get fields from db if copying
if ($copy && $copyid)
{
$video = runSQL('SELECT * FROM '.TBL_DATA.' WHERE id = '.$copyid);
// get fields (according to list) from db to be saved later
foreach ($video[0] as $name => $val)
{
// don't copy diskid
if ($name == 'diskid')
{
if ($config['autoid'])
{
$$name = getDiskId();
}
}
else
{
if (in_array($name, $imdb_set_fields)) $$name = $val;
}
}
$genres = getItemGenres($copyid);
}
// save data
if ($save)
{
// uncomment the following line to provide simple protection for your own public access videoDB
//if (!preg_match('/[0-9]{2+}/', $id)) break;
// implicit owner id if not set
if (!$owner_id) $owner_id = get_current_user_id();
// generate diskid
if (empty($diskid) && $config['autoid'] && $mediatype != MEDIA_WISHLIST) $diskid = getDiskId();
// write videodata table
$SETS = prepareSQL($GLOBALS);
$id = updateDB($SETS, $id, ($oldmediatype == MEDIA_WISHLIST) && ($mediatype != MEDIA_WISHLIST));
// save genres
setItemGenres($id, $genres);
// set seen for currently logged in user
set_userseen($id, $seen);
// uploaded cover?
processUpload($id, $_FILES['coverupload']['tmp_name'], $_FILES['coverupload']['type'], $_FILES['coverupload']['name']);
// make sure no artifacts
$smarty->clearCache('list.tpl');
$smarty->clearCache('show.tpl', get_current_user_id().'|'.$id);
// add another?
if ($add_flag)
{
// remove id to prevent edit mode instead of new
$id = '';
$smarty->assign('add_flag', $add_flag);
}
else
{
// show the saved movie
redirect('show.php?id='.$id);
}
}
// load existing data
if ($id)
{
$SELECT = '';
// select all fields according to list, plus id
foreach ($imdb_set_fields as $name)
{
if ($SELECT) $SELECT .= ', ';
$SELECT .= $name;
}
$SELECT = 'SELECT '.TBL_DATA.'.id, '.TBL_DATA.'.owner_id, '.TBL_USERS.'.name AS owner,
!ISNULL('.TBL_USERSEEN.'.video_id) AS seen, '.$SELECT.'
FROM '.TBL_DATA.'
LEFT JOIN '.TBL_USERS.' ON '.TBL_DATA.'.owner_id = '.TBL_USERS.'.id
LEFT JOIN '.TBL_USERSEEN.'
ON '.TBL_DATA.'.id = '.TBL_USERSEEN.'.video_id AND '.TBL_USERSEEN.'.user_id = '.get_current_user_id().'
WHERE '.TBL_DATA.'.id = '.$id;
$video = runSQL($SELECT);
// diskid to global scope:
$diskid = $video[0]['diskid'];
}
else
{
$video[0]['language'] = $config['langdefault'];
}
// assign automatic disk id
if ($config['autoid'] && (empty($diskid) || $add_flag) && $mediatype != MEDIA_WISHLIST)
{
$video[0]['diskid'] = getDiskId();
// Fix for Bugreport [1122052] Automatic DiskID generation problem
$smarty->assign('autoid', $result[0]['max']);
}
if (empty($video[0]['owner_id']) && !empty($owner_id))
{
$video[0]['owner_id'] = $owner_id;
}
// prepare templates
tpl_page();
tpl_edit($video[0]);
$smarty->assign('lookup_id', $lookup);
$smarty->assign('http_error', $CLIENTERROR);
// allow XML import
if ($config['xml'] && empty($id)) $smarty->assign('xmlimport', true);
// display templates
tpl_display('edit.tpl');