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