1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2011 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | if (!defined('PHPWG_ROOT_PATH')) |
---|
25 | { |
---|
26 | die('Hacking attempt!'); |
---|
27 | } |
---|
28 | |
---|
29 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
30 | |
---|
31 | // +-----------------------------------------------------------------------+ |
---|
32 | // | Check Access and exit when user status is not ok | |
---|
33 | // +-----------------------------------------------------------------------+ |
---|
34 | check_status(ACCESS_ADMINISTRATOR); |
---|
35 | |
---|
36 | if (!is_numeric($_GET['site'])) |
---|
37 | { |
---|
38 | die ('site param missing or invalid'); |
---|
39 | } |
---|
40 | $site_id = $_GET['site']; |
---|
41 | |
---|
42 | $query=' |
---|
43 | SELECT galleries_url |
---|
44 | FROM '.SITES_TABLE.' |
---|
45 | WHERE id = '.$site_id; |
---|
46 | list($site_url) = pwg_db_fetch_row(pwg_query($query)); |
---|
47 | if (!isset($site_url)) |
---|
48 | { |
---|
49 | die('site '.$site_id.' does not exist'); |
---|
50 | } |
---|
51 | $site_is_remote = url_is_remote($site_url); |
---|
52 | |
---|
53 | list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();')); |
---|
54 | define('CURRENT_DATE', $dbnow); |
---|
55 | |
---|
56 | $error_labels = array( |
---|
57 | 'PWG-UPDATE-1' => array( |
---|
58 | l10n('wrong filename'), |
---|
59 | l10n('The name of directories and files must be composed of letters, numbers, "-", "_" or "."') |
---|
60 | ), |
---|
61 | 'PWG-UPDATE-2' => array( |
---|
62 | l10n('missing thumbnail'), |
---|
63 | l10n('a picture filetype requires a thumbnail. The thumbnail must be present in the sub-directory "thumbnail" of the album directory. The thumbnail filename must start with the configured thumbnail prefix and the extension must be among the following list :').implode(',', $conf['picture_ext']) |
---|
64 | ), |
---|
65 | 'PWG-ERROR-NO-FS' => array( |
---|
66 | l10n('File/directory read error'), |
---|
67 | l10n('The file or directory cannot be accessed (either it does not exist or the access is denied)') |
---|
68 | ), |
---|
69 | 'PWG-ERROR-VERSION' => array( |
---|
70 | l10n('Piwigo version differs on the remote site'), |
---|
71 | l10n('Version of create_listing_file.php on the remote site and Piwigo must be the same') |
---|
72 | ), |
---|
73 | 'PWG-ERROR-NOLISTING' => array( |
---|
74 | l10n('listing.xml file was not found'), |
---|
75 | l10n('listing.xml file was not found on the remote site. This file is generated by choosing the "generate listing" command in the Site manager') |
---|
76 | ) |
---|
77 | ); |
---|
78 | $errors = array(); |
---|
79 | $infos = array(); |
---|
80 | |
---|
81 | if ($site_is_remote) |
---|
82 | { |
---|
83 | include_once(PHPWG_ROOT_PATH.'admin/site_reader_remote.php'); |
---|
84 | $local_listing = null; |
---|
85 | if ( isset($_GET['local_listing']) |
---|
86 | and $_GET['local_listing'] ) |
---|
87 | { |
---|
88 | $local_listing = PHPWG_ROOT_PATH.'listing.xml'; |
---|
89 | } |
---|
90 | $site_reader = new RemoteSiteReader($site_url, $local_listing); |
---|
91 | } |
---|
92 | else |
---|
93 | { |
---|
94 | include_once( PHPWG_ROOT_PATH.'admin/site_reader_local.php'); |
---|
95 | $site_reader = new LocalSiteReader($site_url); |
---|
96 | } |
---|
97 | |
---|
98 | $general_failure = true; |
---|
99 | if (isset($_POST['submit'])) |
---|
100 | { |
---|
101 | if (!isset($conf['flip_picture_ext'])) |
---|
102 | { |
---|
103 | $conf['flip_picture_ext'] = array_flip($conf['picture_ext']); |
---|
104 | } |
---|
105 | if ($site_reader->open()) |
---|
106 | { |
---|
107 | $general_failure = false; |
---|
108 | } |
---|
109 | |
---|
110 | // shall we simulate only |
---|
111 | if (isset($_POST['simulate']) and $_POST['simulate'] == 1) |
---|
112 | { |
---|
113 | $simulate = true; |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | $simulate = false; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | // +-----------------------------------------------------------------------+ |
---|
122 | // | directories / categories | |
---|
123 | // +-----------------------------------------------------------------------+ |
---|
124 | if (isset($_POST['submit']) |
---|
125 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')) |
---|
126 | { |
---|
127 | $counts['new_categories'] = 0; |
---|
128 | $counts['del_categories'] = 0; |
---|
129 | $counts['del_elements'] = 0; |
---|
130 | $counts['new_elements'] = 0; |
---|
131 | $counts['upd_elements'] = 0; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | if (isset($_POST['submit']) |
---|
136 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files') |
---|
137 | and !$general_failure) |
---|
138 | { |
---|
139 | $start = get_moment(); |
---|
140 | // which categories to update ? |
---|
141 | $query = ' |
---|
142 | SELECT id, uppercats, global_rank, status, visible |
---|
143 | FROM '.CATEGORIES_TABLE.' |
---|
144 | WHERE dir IS NOT NULL |
---|
145 | AND site_id = '.$site_id; |
---|
146 | if (isset($_POST['cat']) and is_numeric($_POST['cat'])) |
---|
147 | { |
---|
148 | if (isset($_POST['subcats-included']) and $_POST['subcats-included'] == 1) |
---|
149 | { |
---|
150 | $query.= ' |
---|
151 | AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$_POST['cat'].'(,|$)\' |
---|
152 | '; |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | $query.= ' |
---|
157 | AND id = '.$_POST['cat'].' |
---|
158 | '; |
---|
159 | } |
---|
160 | } |
---|
161 | $db_categories = hash_from_query($query, 'id'); |
---|
162 | |
---|
163 | // get categort full directories in an array for comparison with file |
---|
164 | // system directory tree |
---|
165 | $db_fulldirs = get_fulldirs(array_keys($db_categories)); |
---|
166 | |
---|
167 | // what is the base directory to search file system sub-directories ? |
---|
168 | if (isset($_POST['cat']) and is_numeric($_POST['cat'])) |
---|
169 | { |
---|
170 | $basedir = $db_fulldirs[$_POST['cat']]; |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | $basedir = preg_replace('#/*$#', '', $site_url); |
---|
175 | } |
---|
176 | |
---|
177 | // we need to have fulldirs as keys to make efficient comparison |
---|
178 | $db_fulldirs = array_flip($db_fulldirs); |
---|
179 | |
---|
180 | // finding next rank for each id_uppercat. By default, each category id |
---|
181 | // has 1 for next rank on its sub-categories to create |
---|
182 | $next_rank['NULL'] = 1; |
---|
183 | |
---|
184 | $query = ' |
---|
185 | SELECT id |
---|
186 | FROM '.CATEGORIES_TABLE; |
---|
187 | $result = pwg_query($query); |
---|
188 | while ($row = pwg_db_fetch_assoc($result)) |
---|
189 | { |
---|
190 | $next_rank[$row['id']] = 1; |
---|
191 | } |
---|
192 | |
---|
193 | // let's see if some categories already have some sub-categories... |
---|
194 | $query = ' |
---|
195 | SELECT id_uppercat, MAX(rank)+1 AS next_rank |
---|
196 | FROM '.CATEGORIES_TABLE.' |
---|
197 | GROUP BY id_uppercat'; |
---|
198 | $result = pwg_query($query); |
---|
199 | while ($row = pwg_db_fetch_assoc($result)) |
---|
200 | { |
---|
201 | // for the id_uppercat NULL, we write 'NULL' and not the empty string |
---|
202 | if (!isset($row['id_uppercat']) or $row['id_uppercat'] == '') |
---|
203 | { |
---|
204 | $row['id_uppercat'] = 'NULL'; |
---|
205 | } |
---|
206 | $next_rank[$row['id_uppercat']] = $row['next_rank']; |
---|
207 | } |
---|
208 | |
---|
209 | // next category id available |
---|
210 | $next_id = pwg_db_nextval('id', CATEGORIES_TABLE); |
---|
211 | |
---|
212 | // retrieve sub-directories fulldirs from the site reader |
---|
213 | $fs_fulldirs = $site_reader->get_full_directories($basedir); |
---|
214 | |
---|
215 | // get_full_directories doesn't include the base directory, so if it's a |
---|
216 | // category directory, we need to include it in our array |
---|
217 | if (isset($_POST['cat'])) |
---|
218 | { |
---|
219 | array_push($fs_fulldirs, $basedir); |
---|
220 | } |
---|
221 | // If $_POST['subcats-included'] != 1 ("Search in sub-albums" is unchecked) |
---|
222 | // $db_fulldirs doesn't include any subdirectories and $fs_fulldirs does |
---|
223 | // So $fs_fulldirs will be limited to the selected basedir |
---|
224 | // (if that one is in $fs_fulldirs) |
---|
225 | if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) |
---|
226 | { |
---|
227 | $fs_fulldirs = array_intersect($fs_fulldirs, array_keys($db_fulldirs)); |
---|
228 | } |
---|
229 | $inserts = array(); |
---|
230 | // new categories are the directories not present yet in the database |
---|
231 | foreach (array_diff($fs_fulldirs, array_keys($db_fulldirs)) as $fulldir) |
---|
232 | { |
---|
233 | $dir = basename($fulldir); |
---|
234 | if (preg_match('/^[a-zA-Z0-9-_.]+$/', $dir)) |
---|
235 | { |
---|
236 | $insert = array( |
---|
237 | 'id' => $next_id++, |
---|
238 | 'dir' => $dir, |
---|
239 | 'name' => str_replace('_', ' ', $dir), |
---|
240 | 'site_id' => $site_id, |
---|
241 | 'commentable' => |
---|
242 | boolean_to_string($conf['newcat_default_commentable']), |
---|
243 | 'status' => $conf['newcat_default_status'], |
---|
244 | 'visible' => boolean_to_string($conf['newcat_default_visible']), |
---|
245 | ); |
---|
246 | |
---|
247 | if (isset($db_fulldirs[dirname($fulldir)])) |
---|
248 | { |
---|
249 | $parent = $db_fulldirs[dirname($fulldir)]; |
---|
250 | |
---|
251 | $insert['id_uppercat'] = $parent; |
---|
252 | $insert['uppercats'] = |
---|
253 | $db_categories[$parent]['uppercats'].','.$insert['id']; |
---|
254 | $insert['rank'] = $next_rank[$parent]++; |
---|
255 | $insert['global_rank'] = |
---|
256 | $db_categories[$parent]['global_rank'].'.'.$insert['rank']; |
---|
257 | if ('private' == $db_categories[$parent]['status']) |
---|
258 | { |
---|
259 | $insert['status'] = 'private'; |
---|
260 | } |
---|
261 | if ('false' == $db_categories[$parent]['visible']) |
---|
262 | { |
---|
263 | $insert['visible'] = 'false'; |
---|
264 | } |
---|
265 | } |
---|
266 | else |
---|
267 | { |
---|
268 | $insert['uppercats'] = $insert['id']; |
---|
269 | $insert{'rank'} = $next_rank['NULL']++; |
---|
270 | $insert['global_rank'] = $insert['rank']; |
---|
271 | } |
---|
272 | |
---|
273 | array_push($inserts, $insert); |
---|
274 | array_push( |
---|
275 | $infos, |
---|
276 | array( |
---|
277 | 'path' => $fulldir, |
---|
278 | 'info' => l10n('added') |
---|
279 | ) |
---|
280 | ); |
---|
281 | |
---|
282 | // add the new category to $db_categories and $db_fulldirs array |
---|
283 | $db_categories[$insert{'id'}] = |
---|
284 | array( |
---|
285 | 'id' => $insert['id'], |
---|
286 | 'status' => $insert['status'], |
---|
287 | 'visible' => $insert['visible'], |
---|
288 | 'uppercats' => $insert['uppercats'], |
---|
289 | 'global_rank' => $insert['global_rank'] |
---|
290 | ); |
---|
291 | $db_fulldirs[$fulldir] = $insert['id']; |
---|
292 | $next_rank[$insert{'id'}] = 1; |
---|
293 | } |
---|
294 | else |
---|
295 | { |
---|
296 | array_push( |
---|
297 | $errors, |
---|
298 | array( |
---|
299 | 'path' => $fulldir, |
---|
300 | 'type' => 'PWG-UPDATE-1' |
---|
301 | ) |
---|
302 | ); |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | if (count($inserts) > 0) |
---|
307 | { |
---|
308 | if (!$simulate) |
---|
309 | { |
---|
310 | $dbfields = array( |
---|
311 | 'id','dir','name','site_id','id_uppercat','uppercats','commentable', |
---|
312 | 'visible','status','rank','global_rank' |
---|
313 | ); |
---|
314 | mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts); |
---|
315 | |
---|
316 | // add default permissions to categories |
---|
317 | $category_ids = array(); |
---|
318 | foreach ($inserts as $category) |
---|
319 | { |
---|
320 | $category_ids[] = $category['id']; |
---|
321 | } |
---|
322 | add_permission_on_category($category_ids, get_admins()); |
---|
323 | } |
---|
324 | |
---|
325 | $counts['new_categories'] = count($inserts); |
---|
326 | } |
---|
327 | |
---|
328 | // to delete categories |
---|
329 | $to_delete = array(); |
---|
330 | foreach (array_diff(array_keys($db_fulldirs), $fs_fulldirs) as $fulldir) |
---|
331 | { |
---|
332 | array_push($to_delete, $db_fulldirs[$fulldir]); |
---|
333 | unset($db_fulldirs[$fulldir]); |
---|
334 | array_push($infos, array('path' => $fulldir, |
---|
335 | 'info' => l10n('deleted'))); |
---|
336 | } |
---|
337 | if (count($to_delete) > 0) |
---|
338 | { |
---|
339 | if (!$simulate) |
---|
340 | { |
---|
341 | delete_categories($to_delete); |
---|
342 | } |
---|
343 | $counts['del_categories'] = count($to_delete); |
---|
344 | } |
---|
345 | |
---|
346 | $template->append('footer_elements', '<!-- scanning dirs : ' |
---|
347 | . get_elapsed_time($start, get_moment()) |
---|
348 | . ' -->' ); |
---|
349 | } |
---|
350 | // +-----------------------------------------------------------------------+ |
---|
351 | // | files / elements | |
---|
352 | // +-----------------------------------------------------------------------+ |
---|
353 | if (isset($_POST['submit']) and $_POST['sync'] == 'files' |
---|
354 | and !$general_failure) |
---|
355 | { |
---|
356 | $start_files = get_moment(); |
---|
357 | $start= $start_files; |
---|
358 | |
---|
359 | $fs = $site_reader->get_elements($basedir); |
---|
360 | $template->append('footer_elements', '<!-- get_elements: ' |
---|
361 | . get_elapsed_time($start, get_moment()) |
---|
362 | . ' -->' ); |
---|
363 | |
---|
364 | $cat_ids = array_diff(array_keys($db_categories), $to_delete); |
---|
365 | |
---|
366 | $db_elements = array(); |
---|
367 | |
---|
368 | if (count($cat_ids) > 0) |
---|
369 | { |
---|
370 | $query = ' |
---|
371 | SELECT id, path |
---|
372 | FROM '.IMAGES_TABLE.' |
---|
373 | WHERE storage_category_id IN (' |
---|
374 | .wordwrap( |
---|
375 | implode(', ', $cat_ids), |
---|
376 | 80, |
---|
377 | "\n" |
---|
378 | ).')'; |
---|
379 | $db_elements = simple_hash_from_query($query, 'id', 'path'); |
---|
380 | } |
---|
381 | |
---|
382 | // next element id available |
---|
383 | $next_element_id = pwg_db_nextval('id', IMAGES_TABLE); |
---|
384 | |
---|
385 | $start = get_moment(); |
---|
386 | |
---|
387 | $inserts = array(); |
---|
388 | $insert_links = array(); |
---|
389 | |
---|
390 | foreach (array_diff(array_keys($fs), $db_elements) as $path) |
---|
391 | { |
---|
392 | $insert = array(); |
---|
393 | // storage category must exist |
---|
394 | $dirname = dirname($path); |
---|
395 | if (!isset($db_fulldirs[$dirname])) |
---|
396 | { |
---|
397 | continue; |
---|
398 | } |
---|
399 | $filename = basename($path); |
---|
400 | if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename)) |
---|
401 | { |
---|
402 | array_push( |
---|
403 | $errors, |
---|
404 | array( |
---|
405 | 'path' => $path, |
---|
406 | 'type' => 'PWG-UPDATE-1' |
---|
407 | ) |
---|
408 | ); |
---|
409 | |
---|
410 | continue; |
---|
411 | } |
---|
412 | |
---|
413 | if ( isset( $conf['flip_picture_ext'][get_extension($filename)] ) |
---|
414 | and !isset($fs[$path]['tn_ext']) ) |
---|
415 | { // For a picture thumbnail is mandatory and for non picture element, |
---|
416 | // thumbnail and representative are optionnal |
---|
417 | array_push( |
---|
418 | $errors, |
---|
419 | array( |
---|
420 | 'path' => $path, |
---|
421 | 'type' => 'PWG-UPDATE-2' |
---|
422 | ) |
---|
423 | ); |
---|
424 | } |
---|
425 | else |
---|
426 | { |
---|
427 | $insert = array( |
---|
428 | 'id' => $next_element_id++, |
---|
429 | 'file' => $filename, |
---|
430 | 'date_available' => CURRENT_DATE, |
---|
431 | 'path' => $path, |
---|
432 | 'tn_ext' => isset($fs[$path]['tn_ext']) |
---|
433 | ? $fs[$path]['tn_ext'] |
---|
434 | : null, |
---|
435 | 'storage_category_id' => $db_fulldirs[$dirname], |
---|
436 | 'added_by' => $user['id'], |
---|
437 | ); |
---|
438 | |
---|
439 | if ( $_POST['privacy_level']!=0 ) |
---|
440 | { |
---|
441 | $insert['level'] = $_POST['privacy_level']; |
---|
442 | } |
---|
443 | |
---|
444 | array_push( |
---|
445 | $inserts, |
---|
446 | $insert |
---|
447 | ); |
---|
448 | |
---|
449 | array_push( |
---|
450 | $insert_links, |
---|
451 | array( |
---|
452 | 'image_id' => $insert['id'], |
---|
453 | 'category_id' => $insert['storage_category_id'], |
---|
454 | ) |
---|
455 | ); |
---|
456 | |
---|
457 | array_push( |
---|
458 | $infos, |
---|
459 | array( |
---|
460 | 'path' => $insert['path'], |
---|
461 | 'info' => l10n('added') |
---|
462 | ) |
---|
463 | ); |
---|
464 | |
---|
465 | $caddiables[] = $insert['id']; |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | if (count($inserts) > 0) |
---|
470 | { |
---|
471 | if (!$simulate) |
---|
472 | { |
---|
473 | // inserts all new elements |
---|
474 | mass_inserts( |
---|
475 | IMAGES_TABLE, |
---|
476 | array_keys($inserts[0]), |
---|
477 | $inserts |
---|
478 | ); |
---|
479 | |
---|
480 | // inserts all links between new elements and their storage category |
---|
481 | mass_inserts( |
---|
482 | IMAGE_CATEGORY_TABLE, |
---|
483 | array_keys($insert_links[0]), |
---|
484 | $insert_links |
---|
485 | ); |
---|
486 | |
---|
487 | // add new photos to caddie |
---|
488 | if (isset($_POST['add_to_caddie']) and $_POST['add_to_caddie'] == 1) |
---|
489 | { |
---|
490 | fill_caddie($caddiables); |
---|
491 | } |
---|
492 | } |
---|
493 | $counts['new_elements'] = count($inserts); |
---|
494 | } |
---|
495 | |
---|
496 | // delete elements that are in database but not in the filesystem |
---|
497 | $to_delete_elements = array(); |
---|
498 | foreach (array_diff($db_elements, array_keys($fs)) as $path) |
---|
499 | { |
---|
500 | array_push($to_delete_elements, array_search($path, $db_elements)); |
---|
501 | array_push($infos, array('path' => $path, |
---|
502 | 'info' => l10n('deleted'))); |
---|
503 | } |
---|
504 | if (count($to_delete_elements) > 0) |
---|
505 | { |
---|
506 | if (!$simulate) |
---|
507 | { |
---|
508 | delete_elements($to_delete_elements); |
---|
509 | } |
---|
510 | $counts['del_elements'] = count($to_delete_elements); |
---|
511 | } |
---|
512 | |
---|
513 | $template->append('footer_elements', '<!-- scanning files : ' |
---|
514 | . get_elapsed_time($start_files, get_moment()) |
---|
515 | . ' -->' ); |
---|
516 | } |
---|
517 | |
---|
518 | // +-----------------------------------------------------------------------+ |
---|
519 | // | synchronize files | |
---|
520 | // +-----------------------------------------------------------------------+ |
---|
521 | if (isset($_POST['submit']) |
---|
522 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files') |
---|
523 | and !$general_failure ) |
---|
524 | { |
---|
525 | if (!$simulate) |
---|
526 | { |
---|
527 | $start = get_moment(); |
---|
528 | update_category('all'); |
---|
529 | $template->append('footer_elements', '<!-- update_category(all) : ' |
---|
530 | . get_elapsed_time($start,get_moment()) |
---|
531 | . ' -->' ); |
---|
532 | $start = get_moment(); |
---|
533 | update_global_rank(); |
---|
534 | $template->append('footer_elements', '<!-- ordering categories : ' |
---|
535 | . get_elapsed_time($start, get_moment()) |
---|
536 | . ' -->'); |
---|
537 | } |
---|
538 | |
---|
539 | if ($_POST['sync'] == 'files') |
---|
540 | { |
---|
541 | $start = get_moment(); |
---|
542 | $opts['category_id'] = ''; |
---|
543 | $opts['recursive'] = true; |
---|
544 | if (isset($_POST['cat'])) |
---|
545 | { |
---|
546 | $opts['category_id'] = $_POST['cat']; |
---|
547 | if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) |
---|
548 | { |
---|
549 | $opts['recursive'] = false; |
---|
550 | } |
---|
551 | } |
---|
552 | $files = get_filelist($opts['category_id'], $site_id, |
---|
553 | $opts['recursive'], |
---|
554 | false); |
---|
555 | $template->append('footer_elements', '<!-- get_filelist : ' |
---|
556 | . get_elapsed_time($start, get_moment()) |
---|
557 | . ' -->'); |
---|
558 | $start = get_moment(); |
---|
559 | |
---|
560 | $datas = array(); |
---|
561 | foreach ( $files as $id=>$file ) |
---|
562 | { |
---|
563 | $data = $site_reader->get_element_update_attributes($file); |
---|
564 | if ( !is_array($data) ) |
---|
565 | { |
---|
566 | continue; |
---|
567 | } |
---|
568 | $extension = get_extension($file); |
---|
569 | if ( isset($conf['flip_picture_ext'][$extension]) ) |
---|
570 | { |
---|
571 | if ( !isset($data['tn_ext']) ) |
---|
572 | { |
---|
573 | array_push( |
---|
574 | $errors, |
---|
575 | array( |
---|
576 | 'path' => $file, |
---|
577 | 'type' => 'PWG-UPDATE-2' |
---|
578 | ) |
---|
579 | ); |
---|
580 | continue; |
---|
581 | } |
---|
582 | } |
---|
583 | |
---|
584 | $data['id']=$id; |
---|
585 | array_push($datas, $data); |
---|
586 | } // end foreach file |
---|
587 | |
---|
588 | $counts['upd_elements'] = count($datas); |
---|
589 | if (!$simulate and count($datas)>0 ) |
---|
590 | { |
---|
591 | mass_updates( |
---|
592 | IMAGES_TABLE, |
---|
593 | // fields |
---|
594 | array( |
---|
595 | 'primary' => array('id'), |
---|
596 | 'update' => $site_reader->get_update_attributes(), |
---|
597 | ), |
---|
598 | $datas |
---|
599 | ); |
---|
600 | } |
---|
601 | $template->append('footer_elements', '<!-- update files : ' |
---|
602 | . get_elapsed_time($start,get_moment()) |
---|
603 | . ' -->'); |
---|
604 | }// end if sync files |
---|
605 | } |
---|
606 | |
---|
607 | // +-----------------------------------------------------------------------+ |
---|
608 | // | synchronize files | |
---|
609 | // +-----------------------------------------------------------------------+ |
---|
610 | if (isset($_POST['submit']) |
---|
611 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')) |
---|
612 | { |
---|
613 | $template->assign( |
---|
614 | 'update_result', |
---|
615 | array( |
---|
616 | 'NB_NEW_CATEGORIES'=>$counts['new_categories'], |
---|
617 | 'NB_DEL_CATEGORIES'=>$counts['del_categories'], |
---|
618 | 'NB_NEW_ELEMENTS'=>$counts['new_elements'], |
---|
619 | 'NB_DEL_ELEMENTS'=>$counts['del_elements'], |
---|
620 | 'NB_UPD_ELEMENTS'=>$counts['upd_elements'], |
---|
621 | 'NB_ERRORS'=>count($errors), |
---|
622 | )); |
---|
623 | } |
---|
624 | |
---|
625 | // +-----------------------------------------------------------------------+ |
---|
626 | // | synchronize metadata | |
---|
627 | // +-----------------------------------------------------------------------+ |
---|
628 | if (isset($_POST['submit']) and isset($_POST['sync_meta']) |
---|
629 | and !$general_failure) |
---|
630 | { |
---|
631 | // sync only never synchronized files ? |
---|
632 | $opts['only_new'] = isset($_POST['meta_all']) ? false : true; |
---|
633 | $opts['category_id'] = ''; |
---|
634 | $opts['recursive'] = true; |
---|
635 | |
---|
636 | if (isset($_POST['cat'])) |
---|
637 | { |
---|
638 | $opts['category_id'] = $_POST['cat']; |
---|
639 | // recursive ? |
---|
640 | if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) |
---|
641 | { |
---|
642 | $opts['recursive'] = false; |
---|
643 | } |
---|
644 | } |
---|
645 | $start = get_moment(); |
---|
646 | $files = get_filelist($opts['category_id'], $site_id, |
---|
647 | $opts['recursive'], |
---|
648 | $opts['only_new']); |
---|
649 | |
---|
650 | $template->append('footer_elements', '<!-- get_filelist : ' |
---|
651 | . get_elapsed_time($start, get_moment()) |
---|
652 | . ' -->'); |
---|
653 | |
---|
654 | $start = get_moment(); |
---|
655 | $datas = array(); |
---|
656 | $tags_of = array(); |
---|
657 | |
---|
658 | $has_high_images = array(); |
---|
659 | |
---|
660 | $image_ids = array(); |
---|
661 | foreach ($files as $id => $file) |
---|
662 | { |
---|
663 | array_push($image_ids, $id); |
---|
664 | } |
---|
665 | |
---|
666 | if (count($image_ids) > 0) |
---|
667 | { |
---|
668 | $query = ' |
---|
669 | SELECT id |
---|
670 | FROM '.IMAGES_TABLE.' |
---|
671 | WHERE has_high = \'true\' |
---|
672 | AND id IN ( |
---|
673 | '.wordwrap(implode(', ', $image_ids), 80, "\n").' |
---|
674 | )'; |
---|
675 | $has_high_images = array_from_query($query, 'id' ); |
---|
676 | } |
---|
677 | |
---|
678 | foreach ( $files as $id=>$file ) |
---|
679 | { |
---|
680 | $data = $site_reader->get_element_metadata( |
---|
681 | $file, |
---|
682 | in_array($id, $has_high_images) |
---|
683 | ); |
---|
684 | |
---|
685 | if ( is_array($data) ) |
---|
686 | { |
---|
687 | $data['date_metadata_update'] = CURRENT_DATE; |
---|
688 | $data['id']=$id; |
---|
689 | array_push($datas, $data); |
---|
690 | |
---|
691 | foreach (array('keywords', 'tags') as $key) |
---|
692 | { |
---|
693 | if (isset($data[$key])) |
---|
694 | { |
---|
695 | if (!isset($tags_of[$id])) |
---|
696 | { |
---|
697 | $tags_of[$id] = array(); |
---|
698 | } |
---|
699 | |
---|
700 | foreach (explode(',', $data[$key]) as $tag_name) |
---|
701 | { |
---|
702 | array_push( |
---|
703 | $tags_of[$id], |
---|
704 | tag_id_from_tag_name($tag_name) |
---|
705 | ); |
---|
706 | } |
---|
707 | } |
---|
708 | } |
---|
709 | } |
---|
710 | else |
---|
711 | { |
---|
712 | array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS')); |
---|
713 | } |
---|
714 | } |
---|
715 | |
---|
716 | if (!$simulate) |
---|
717 | { |
---|
718 | if (count($datas) > 0) |
---|
719 | { |
---|
720 | mass_updates( |
---|
721 | IMAGES_TABLE, |
---|
722 | // fields |
---|
723 | array( |
---|
724 | 'primary' => array('id'), |
---|
725 | 'update' => array_unique( |
---|
726 | array_merge( |
---|
727 | array_diff( |
---|
728 | $site_reader->get_metadata_attributes(), |
---|
729 | // keywords and tags fields are managed separately |
---|
730 | array('keywords', 'tags') |
---|
731 | ), |
---|
732 | array('date_metadata_update')) |
---|
733 | ) |
---|
734 | ), |
---|
735 | $datas, |
---|
736 | isset($_POST['meta_empty_overrides']) ? 0 : MASS_UPDATES_SKIP_EMPTY |
---|
737 | ); |
---|
738 | } |
---|
739 | set_tags_of($tags_of); |
---|
740 | } |
---|
741 | |
---|
742 | $template->append('footer_elements', '<!-- metadata update : ' |
---|
743 | . get_elapsed_time($start, get_moment()) |
---|
744 | . ' -->'); |
---|
745 | |
---|
746 | $template->assign( |
---|
747 | 'metadata_result', |
---|
748 | array( |
---|
749 | 'NB_ELEMENTS_DONE' => count($datas), |
---|
750 | 'NB_ELEMENTS_CANDIDATES' => count($files), |
---|
751 | 'NB_ERRORS' => count($errors), |
---|
752 | )); |
---|
753 | } |
---|
754 | |
---|
755 | // +-----------------------------------------------------------------------+ |
---|
756 | // | template initialization | |
---|
757 | // +-----------------------------------------------------------------------+ |
---|
758 | $template->set_filenames(array('update'=>'site_update.tpl')); |
---|
759 | $result_title = ''; |
---|
760 | if (isset($simulate) and $simulate) |
---|
761 | { |
---|
762 | $result_title.= l10n('[Simulation]').' '; |
---|
763 | } |
---|
764 | |
---|
765 | // used_metadata string is displayed to inform admin which metadata will be |
---|
766 | // used from files for synchronization |
---|
767 | $used_metadata = implode( ', ', $site_reader->get_metadata_attributes()); |
---|
768 | if ($site_is_remote and !isset($_POST['submit']) ) |
---|
769 | { |
---|
770 | $used_metadata.= ' + ...'; |
---|
771 | } |
---|
772 | |
---|
773 | $template->assign( |
---|
774 | array( |
---|
775 | 'SITE_URL'=>$site_url, |
---|
776 | 'U_SITE_MANAGER'=> get_root_url().'admin.php?page=site_manager', |
---|
777 | 'L_RESULT_UPDATE'=>$result_title.l10n('Search for new images in the directories'), |
---|
778 | 'L_RESULT_METADATA'=>$result_title.l10n('Metadata synchronization results'), |
---|
779 | 'METADATA_LIST' => $used_metadata, |
---|
780 | 'U_HELP' => get_root_url().'admin/popuphelp.php?page=synchronize', |
---|
781 | )); |
---|
782 | |
---|
783 | // +-----------------------------------------------------------------------+ |
---|
784 | // | introduction : choices | |
---|
785 | // +-----------------------------------------------------------------------+ |
---|
786 | if (isset($_POST['submit'])) |
---|
787 | { |
---|
788 | $tpl_introduction = array( |
---|
789 | 'sync' => $_POST['sync'], |
---|
790 | 'sync_meta' => isset($_POST['sync_meta']) ? true : false, |
---|
791 | 'display_info' => isset($_POST['display_info']) and $_POST['display_info']==1, |
---|
792 | 'add_to_caddie' => isset($_POST['add_to_caddie']) and $_POST['add_to_caddie']==1, |
---|
793 | 'subcats_included' => isset($_POST['subcats-included']) and $_POST['subcats-included']==1, |
---|
794 | 'privacy_level_selected' => (int)@$_POST['privacy_level'], |
---|
795 | 'meta_all' => isset($_POST['meta_all']) ? true : false, |
---|
796 | 'meta_empty_overrides' => isset($_POST['meta_empty_overrides']) ? true : false, |
---|
797 | ); |
---|
798 | |
---|
799 | if (isset($_POST['cat']) and is_numeric($_POST['cat'])) |
---|
800 | { |
---|
801 | $cat_selected = array($_POST['cat']); |
---|
802 | } |
---|
803 | else |
---|
804 | { |
---|
805 | $cat_selected = array(); |
---|
806 | } |
---|
807 | } |
---|
808 | else |
---|
809 | { |
---|
810 | $tpl_introduction = array( |
---|
811 | 'sync' => 'dirs', |
---|
812 | 'sync_meta' => true, |
---|
813 | 'display_info' => false, |
---|
814 | 'add_to_caddie' => false, |
---|
815 | 'subcats_included' => true, |
---|
816 | 'privacy_level_selected' => 0, |
---|
817 | 'meta_all' => false, |
---|
818 | 'meta_empty_overrides' => false, |
---|
819 | ); |
---|
820 | |
---|
821 | $cat_selected = array(); |
---|
822 | |
---|
823 | if (isset($_GET['cat_id'])) |
---|
824 | { |
---|
825 | check_input_parameter('cat_id', $_GET, false, PATTERN_ID); |
---|
826 | |
---|
827 | $cat_selected = array($_GET['cat_id']); |
---|
828 | $tpl_introduction['sync'] = 'files'; |
---|
829 | } |
---|
830 | } |
---|
831 | |
---|
832 | $tpl_introduction['privacy_level_options'] = get_privacy_level_options(); |
---|
833 | |
---|
834 | $template->assign('introduction', $tpl_introduction); |
---|
835 | |
---|
836 | $query = ' |
---|
837 | SELECT id,name,uppercats,global_rank |
---|
838 | FROM '.CATEGORIES_TABLE.' |
---|
839 | WHERE site_id = '.$site_id; |
---|
840 | display_select_cat_wrapper($query, |
---|
841 | $cat_selected, |
---|
842 | 'category_options', |
---|
843 | false); |
---|
844 | |
---|
845 | |
---|
846 | if (count($errors) > 0) |
---|
847 | { |
---|
848 | foreach ($errors as $error) |
---|
849 | { |
---|
850 | $template->append( |
---|
851 | 'sync_errors', |
---|
852 | array( |
---|
853 | 'ELEMENT' => $error['path'], |
---|
854 | 'LABEL' => $error['type'].' ('.$error_labels[$error['type']][0].')' |
---|
855 | )); |
---|
856 | } |
---|
857 | |
---|
858 | foreach ($error_labels as $error_type=>$error_description) |
---|
859 | { |
---|
860 | $template->append( |
---|
861 | 'sync_error_captions', |
---|
862 | array( |
---|
863 | 'TYPE' => $error_type, |
---|
864 | 'LABEL' => $error_description[1] |
---|
865 | )); |
---|
866 | } |
---|
867 | } |
---|
868 | |
---|
869 | if (count($infos) > 0 |
---|
870 | and isset($_POST['display_info']) |
---|
871 | and $_POST['display_info'] == 1) |
---|
872 | { |
---|
873 | foreach ($infos as $info) |
---|
874 | { |
---|
875 | $template->append( |
---|
876 | 'sync_infos', |
---|
877 | array( |
---|
878 | 'ELEMENT' => $info['path'], |
---|
879 | 'LABEL' => $info['info'] |
---|
880 | )); |
---|
881 | } |
---|
882 | } |
---|
883 | |
---|
884 | // +-----------------------------------------------------------------------+ |
---|
885 | // | sending html code | |
---|
886 | // +-----------------------------------------------------------------------+ |
---|
887 | $template->assign_var_from_handle('ADMIN_CONTENT', 'update'); |
---|
888 | ?> |
---|