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