1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 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 | $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 | $inserts[] = $insert; |
---|
257 | $infos[] = array( |
---|
258 | 'path' => $fulldir, |
---|
259 | 'info' => l10n('added'), |
---|
260 | ); |
---|
261 | |
---|
262 | // add the new category to $db_categories and $db_fulldirs array |
---|
263 | $db_categories[$insert{'id'}] = |
---|
264 | array( |
---|
265 | 'id' => $insert['id'], |
---|
266 | 'parent' => (isset($parent)) ? $parent : Null, |
---|
267 | 'status' => $insert['status'], |
---|
268 | 'visible' => $insert['visible'], |
---|
269 | 'uppercats' => $insert['uppercats'], |
---|
270 | 'global_rank' => $insert['global_rank'] |
---|
271 | ); |
---|
272 | $db_fulldirs[$fulldir] = $insert['id']; |
---|
273 | $next_rank[$insert{'id'}] = 1; |
---|
274 | } |
---|
275 | else |
---|
276 | { |
---|
277 | $errors[] = array( |
---|
278 | 'path' => $fulldir, |
---|
279 | 'type' => 'PWG-UPDATE-1' |
---|
280 | ); |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | if (count($inserts) > 0) |
---|
285 | { |
---|
286 | if (!$simulate) |
---|
287 | { |
---|
288 | $dbfields = array( |
---|
289 | 'id','dir','name','site_id','id_uppercat','uppercats','commentable', |
---|
290 | 'visible','status','rank','global_rank' |
---|
291 | ); |
---|
292 | mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts); |
---|
293 | |
---|
294 | // add default permissions to categories |
---|
295 | $category_ids = array(); |
---|
296 | $category_up = array(); |
---|
297 | foreach ($inserts as $category) |
---|
298 | { |
---|
299 | $category_ids[] = $category['id']; |
---|
300 | if (!empty($category['id_uppercat'])) |
---|
301 | { |
---|
302 | $category_up[] = $category['id_uppercat']; |
---|
303 | } |
---|
304 | } |
---|
305 | $category_up=implode(',',array_unique($category_up)); |
---|
306 | if ($conf['inheritance_by_default']) |
---|
307 | { |
---|
308 | $query = ' |
---|
309 | SELECT * |
---|
310 | FROM '.GROUP_ACCESS_TABLE.' |
---|
311 | WHERE cat_id IN ('.$category_up.') |
---|
312 | ;'; |
---|
313 | $result = pwg_query($query); |
---|
314 | if (!empty($result)) |
---|
315 | { |
---|
316 | $granted_grps = array(); |
---|
317 | while ($row = pwg_db_fetch_assoc($result)) |
---|
318 | { |
---|
319 | if (!isset($granted_grps[$row['cat_id']])) |
---|
320 | { |
---|
321 | $granted_grps[$row['cat_id']]=array(); |
---|
322 | } |
---|
323 | // TODO: explanaition |
---|
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 | } |
---|
332 | $query = ' |
---|
333 | SELECT * |
---|
334 | FROM '.USER_ACCESS_TABLE.' |
---|
335 | WHERE cat_id IN ('.$category_up.') |
---|
336 | ;'; |
---|
337 | $result = pwg_query($query); |
---|
338 | if (!empty($result)) |
---|
339 | { |
---|
340 | $granted_users = array(); |
---|
341 | while ($row = pwg_db_fetch_assoc($result)) |
---|
342 | { |
---|
343 | if (!isset($granted_users[$row['cat_id']])) |
---|
344 | { |
---|
345 | $granted_users[$row['cat_id']]=array(); |
---|
346 | } |
---|
347 | // TODO: explanaition |
---|
348 | array_push( |
---|
349 | $granted_users, |
---|
350 | array( |
---|
351 | $row['cat_id'] => array_push($granted_users[$row['cat_id']],$row['user_id']) |
---|
352 | ) |
---|
353 | ); |
---|
354 | } |
---|
355 | } |
---|
356 | $insert_granted_users=array(); |
---|
357 | $insert_granted_grps=array(); |
---|
358 | foreach ($category_ids as $ids) |
---|
359 | { |
---|
360 | $parent_id=$db_categories[$ids]['parent']; |
---|
361 | while (in_array($parent_id, $category_ids)) |
---|
362 | { |
---|
363 | $parent_id= $db_categories[$parent_id]['parent']; |
---|
364 | } |
---|
365 | if ($db_categories[$ids]['status']=='private' and !is_null($parent_id)) |
---|
366 | { |
---|
367 | if (isset($granted_grps[$parent_id])) |
---|
368 | { |
---|
369 | foreach ($granted_grps[$parent_id] as $granted_grp) |
---|
370 | { |
---|
371 | $insert_granted_grps[] = array( |
---|
372 | 'group_id' => $granted_grp, |
---|
373 | 'cat_id' => $ids |
---|
374 | ); |
---|
375 | } |
---|
376 | } |
---|
377 | if (isset($granted_users[$parent_id])) |
---|
378 | { |
---|
379 | foreach ($granted_users[$parent_id] as $granted_user) |
---|
380 | { |
---|
381 | $insert_granted_users[] = array( |
---|
382 | 'user_id' => $granted_user, |
---|
383 | 'cat_id' => $ids |
---|
384 | ); |
---|
385 | } |
---|
386 | } |
---|
387 | foreach (get_admins() as $granted_user) |
---|
388 | { |
---|
389 | $insert_granted_users[] = array( |
---|
390 | 'user_id' => $granted_user, |
---|
391 | 'cat_id' => $ids |
---|
392 | ); |
---|
393 | } |
---|
394 | } |
---|
395 | } |
---|
396 | mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $insert_granted_grps); |
---|
397 | $insert_granted_users=array_unique($insert_granted_users, SORT_REGULAR); |
---|
398 | mass_inserts(USER_ACCESS_TABLE, array('user_id','cat_id'), $insert_granted_users); |
---|
399 | } |
---|
400 | else |
---|
401 | { |
---|
402 | add_permission_on_category($category_ids, get_admins()); |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | $counts['new_categories'] = count($inserts); |
---|
407 | } |
---|
408 | |
---|
409 | // to delete categories |
---|
410 | $to_delete = array(); |
---|
411 | $to_delete_derivative_dirs = array(); |
---|
412 | |
---|
413 | foreach (array_diff(array_keys($db_fulldirs), $fs_fulldirs) as $fulldir) |
---|
414 | { |
---|
415 | $to_delete[] = $db_fulldirs[$fulldir]; |
---|
416 | unset($db_fulldirs[$fulldir]); |
---|
417 | |
---|
418 | $infos[] = array( |
---|
419 | 'path' => $fulldir, |
---|
420 | 'info' => l10n('deleted') |
---|
421 | ); |
---|
422 | |
---|
423 | if (substr_compare($fulldir, '../', 0, 3)==0) |
---|
424 | { |
---|
425 | $fulldir = substr($fulldir, 3); |
---|
426 | } |
---|
427 | $to_delete_derivative_dirs[] = PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$fulldir; |
---|
428 | } |
---|
429 | |
---|
430 | if (count($to_delete) > 0) |
---|
431 | { |
---|
432 | if (!$simulate) |
---|
433 | { |
---|
434 | delete_categories($to_delete); |
---|
435 | foreach($to_delete_derivative_dirs as $to_delete_dir) |
---|
436 | { |
---|
437 | if (is_dir($to_delete_dir)) |
---|
438 | { |
---|
439 | clear_derivative_cache_rec($to_delete_dir, '#.+#'); |
---|
440 | } |
---|
441 | } |
---|
442 | } |
---|
443 | $counts['del_categories'] = count($to_delete); |
---|
444 | } |
---|
445 | |
---|
446 | $template->append('footer_elements', '<!-- scanning dirs : ' |
---|
447 | . get_elapsed_time($start, get_moment()) |
---|
448 | . ' -->' ); |
---|
449 | } |
---|
450 | // +-----------------------------------------------------------------------+ |
---|
451 | // | files / elements | |
---|
452 | // +-----------------------------------------------------------------------+ |
---|
453 | if (isset($_POST['submit']) and $_POST['sync'] == 'files' |
---|
454 | and !$general_failure) |
---|
455 | { |
---|
456 | $start_files = get_moment(); |
---|
457 | $start= $start_files; |
---|
458 | |
---|
459 | $fs = $site_reader->get_elements($basedir); |
---|
460 | $template->append('footer_elements', '<!-- get_elements: ' |
---|
461 | . get_elapsed_time($start, get_moment()) |
---|
462 | . ' -->' ); |
---|
463 | |
---|
464 | $cat_ids = array_diff(array_keys($db_categories), $to_delete); |
---|
465 | |
---|
466 | $db_elements = array(); |
---|
467 | |
---|
468 | if (count($cat_ids) > 0) |
---|
469 | { |
---|
470 | $query = ' |
---|
471 | SELECT id, path |
---|
472 | FROM '.IMAGES_TABLE.' |
---|
473 | WHERE storage_category_id IN (' |
---|
474 | .wordwrap( |
---|
475 | implode(', ', $cat_ids), |
---|
476 | 160, |
---|
477 | "\n" |
---|
478 | ).')'; |
---|
479 | $db_elements = simple_hash_from_query($query, 'id', 'path'); |
---|
480 | } |
---|
481 | |
---|
482 | // next element id available |
---|
483 | $next_element_id = pwg_db_nextval('id', IMAGES_TABLE); |
---|
484 | |
---|
485 | $start = get_moment(); |
---|
486 | |
---|
487 | $inserts = array(); |
---|
488 | $insert_links = array(); |
---|
489 | |
---|
490 | foreach (array_diff(array_keys($fs), $db_elements) as $path) |
---|
491 | { |
---|
492 | $insert = array(); |
---|
493 | // storage category must exist |
---|
494 | $dirname = dirname($path); |
---|
495 | if (!isset($db_fulldirs[$dirname])) |
---|
496 | { |
---|
497 | continue; |
---|
498 | } |
---|
499 | $filename = basename($path); |
---|
500 | if (!preg_match($conf['sync_chars_regex'], $filename)) |
---|
501 | { |
---|
502 | $errors[] = array( |
---|
503 | 'path' => $path, |
---|
504 | 'type' => 'PWG-UPDATE-1' |
---|
505 | ); |
---|
506 | |
---|
507 | continue; |
---|
508 | } |
---|
509 | |
---|
510 | $insert = array( |
---|
511 | 'id' => $next_element_id++, |
---|
512 | 'file' => $filename, |
---|
513 | 'name' => get_name_from_file($filename), |
---|
514 | 'date_available' => CURRENT_DATE, |
---|
515 | 'path' => $path, |
---|
516 | 'representative_ext' => $fs[$path]['representative_ext'], |
---|
517 | 'storage_category_id' => $db_fulldirs[$dirname], |
---|
518 | 'added_by' => $user['id'], |
---|
519 | ); |
---|
520 | |
---|
521 | if ( $_POST['privacy_level']!=0 ) |
---|
522 | { |
---|
523 | $insert['level'] = $_POST['privacy_level']; |
---|
524 | } |
---|
525 | |
---|
526 | $inserts[] = $insert; |
---|
527 | |
---|
528 | $insert_links[] = array( |
---|
529 | 'image_id' => $insert['id'], |
---|
530 | 'category_id' => $insert['storage_category_id'], |
---|
531 | ); |
---|
532 | |
---|
533 | $infos[] = array( |
---|
534 | 'path' => $insert['path'], |
---|
535 | 'info' => l10n('added') |
---|
536 | ); |
---|
537 | |
---|
538 | $caddiables[] = $insert['id']; |
---|
539 | } |
---|
540 | |
---|
541 | if (count($inserts) > 0) |
---|
542 | { |
---|
543 | if (!$simulate) |
---|
544 | { |
---|
545 | // inserts all new elements |
---|
546 | mass_inserts( |
---|
547 | IMAGES_TABLE, |
---|
548 | array_keys($inserts[0]), |
---|
549 | $inserts |
---|
550 | ); |
---|
551 | |
---|
552 | // inserts all links between new elements and their storage category |
---|
553 | mass_inserts( |
---|
554 | IMAGE_CATEGORY_TABLE, |
---|
555 | array_keys($insert_links[0]), |
---|
556 | $insert_links |
---|
557 | ); |
---|
558 | |
---|
559 | // add new photos to caddie |
---|
560 | if (isset($_POST['add_to_caddie']) and $_POST['add_to_caddie'] == 1) |
---|
561 | { |
---|
562 | fill_caddie($caddiables); |
---|
563 | } |
---|
564 | } |
---|
565 | $counts['new_elements'] = count($inserts); |
---|
566 | } |
---|
567 | |
---|
568 | // delete elements that are in database but not in the filesystem |
---|
569 | $to_delete_elements = array(); |
---|
570 | foreach (array_diff($db_elements, array_keys($fs)) as $path) |
---|
571 | { |
---|
572 | $to_delete_elements[] = array_search($path, $db_elements); |
---|
573 | $infos[] = array( |
---|
574 | 'path' => $path, |
---|
575 | 'info' => l10n('deleted') |
---|
576 | ); |
---|
577 | } |
---|
578 | if (count($to_delete_elements) > 0) |
---|
579 | { |
---|
580 | if (!$simulate) |
---|
581 | { |
---|
582 | delete_elements($to_delete_elements); |
---|
583 | } |
---|
584 | $counts['del_elements'] = count($to_delete_elements); |
---|
585 | } |
---|
586 | |
---|
587 | $template->append('footer_elements', '<!-- scanning files : ' |
---|
588 | . get_elapsed_time($start_files, get_moment()) |
---|
589 | . ' -->' ); |
---|
590 | } |
---|
591 | |
---|
592 | // +-----------------------------------------------------------------------+ |
---|
593 | // | synchronize files | |
---|
594 | // +-----------------------------------------------------------------------+ |
---|
595 | if (isset($_POST['submit']) |
---|
596 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files') |
---|
597 | and !$general_failure ) |
---|
598 | { |
---|
599 | if (!$simulate) |
---|
600 | { |
---|
601 | $start = get_moment(); |
---|
602 | update_category('all'); |
---|
603 | $template->append('footer_elements', '<!-- update_category(all) : ' |
---|
604 | . get_elapsed_time($start,get_moment()) |
---|
605 | . ' -->' ); |
---|
606 | $start = get_moment(); |
---|
607 | update_global_rank(); |
---|
608 | $template->append('footer_elements', '<!-- ordering categories : ' |
---|
609 | . get_elapsed_time($start, get_moment()) |
---|
610 | . ' -->'); |
---|
611 | } |
---|
612 | |
---|
613 | if ($_POST['sync'] == 'files') |
---|
614 | { |
---|
615 | $start = get_moment(); |
---|
616 | $opts['category_id'] = ''; |
---|
617 | $opts['recursive'] = true; |
---|
618 | if (isset($_POST['cat'])) |
---|
619 | { |
---|
620 | $opts['category_id'] = $_POST['cat']; |
---|
621 | if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) |
---|
622 | { |
---|
623 | $opts['recursive'] = false; |
---|
624 | } |
---|
625 | } |
---|
626 | $files = get_filelist($opts['category_id'], $site_id, |
---|
627 | $opts['recursive'], |
---|
628 | false); |
---|
629 | $template->append('footer_elements', '<!-- get_filelist : ' |
---|
630 | . get_elapsed_time($start, get_moment()) |
---|
631 | . ' -->'); |
---|
632 | $start = get_moment(); |
---|
633 | |
---|
634 | $datas = array(); |
---|
635 | foreach ( $files as $id=>$file ) |
---|
636 | { |
---|
637 | $file = $file['path']; |
---|
638 | $data = $site_reader->get_element_update_attributes($file); |
---|
639 | if ( !is_array($data) ) |
---|
640 | { |
---|
641 | continue; |
---|
642 | } |
---|
643 | |
---|
644 | $data['id']=$id; |
---|
645 | $datas[] = $data; |
---|
646 | } // end foreach file |
---|
647 | |
---|
648 | $counts['upd_elements'] = count($datas); |
---|
649 | if (!$simulate and count($datas)>0 ) |
---|
650 | { |
---|
651 | mass_updates( |
---|
652 | IMAGES_TABLE, |
---|
653 | // fields |
---|
654 | array( |
---|
655 | 'primary' => array('id'), |
---|
656 | 'update' => $site_reader->get_update_attributes(), |
---|
657 | ), |
---|
658 | $datas |
---|
659 | ); |
---|
660 | } |
---|
661 | $template->append('footer_elements', '<!-- update files : ' |
---|
662 | . get_elapsed_time($start,get_moment()) |
---|
663 | . ' -->'); |
---|
664 | }// end if sync files |
---|
665 | } |
---|
666 | |
---|
667 | // +-----------------------------------------------------------------------+ |
---|
668 | // | synchronize files | |
---|
669 | // +-----------------------------------------------------------------------+ |
---|
670 | if (isset($_POST['submit']) |
---|
671 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')) |
---|
672 | { |
---|
673 | $template->assign( |
---|
674 | 'update_result', |
---|
675 | array( |
---|
676 | 'NB_NEW_CATEGORIES'=>$counts['new_categories'], |
---|
677 | 'NB_DEL_CATEGORIES'=>$counts['del_categories'], |
---|
678 | 'NB_NEW_ELEMENTS'=>$counts['new_elements'], |
---|
679 | 'NB_DEL_ELEMENTS'=>$counts['del_elements'], |
---|
680 | 'NB_UPD_ELEMENTS'=>$counts['upd_elements'], |
---|
681 | 'NB_ERRORS'=>count($errors), |
---|
682 | )); |
---|
683 | } |
---|
684 | |
---|
685 | // +-----------------------------------------------------------------------+ |
---|
686 | // | synchronize metadata | |
---|
687 | // +-----------------------------------------------------------------------+ |
---|
688 | if (isset($_POST['submit']) and isset($_POST['sync_meta']) |
---|
689 | and !$general_failure) |
---|
690 | { |
---|
691 | // sync only never synchronized files ? |
---|
692 | $opts['only_new'] = isset($_POST['meta_all']) ? false : true; |
---|
693 | $opts['category_id'] = ''; |
---|
694 | $opts['recursive'] = true; |
---|
695 | |
---|
696 | if (isset($_POST['cat'])) |
---|
697 | { |
---|
698 | $opts['category_id'] = $_POST['cat']; |
---|
699 | // recursive ? |
---|
700 | if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) |
---|
701 | { |
---|
702 | $opts['recursive'] = false; |
---|
703 | } |
---|
704 | } |
---|
705 | $start = get_moment(); |
---|
706 | $files = get_filelist($opts['category_id'], $site_id, |
---|
707 | $opts['recursive'], |
---|
708 | $opts['only_new']); |
---|
709 | |
---|
710 | $template->append('footer_elements', '<!-- get_filelist : ' |
---|
711 | . get_elapsed_time($start, get_moment()) |
---|
712 | . ' -->'); |
---|
713 | |
---|
714 | $start = get_moment(); |
---|
715 | $datas = array(); |
---|
716 | $tags_of = array(); |
---|
717 | |
---|
718 | foreach ( $files as $id => $element_infos ) |
---|
719 | { |
---|
720 | $data = $site_reader->get_element_metadata($element_infos); |
---|
721 | |
---|
722 | if ( is_array($data) ) |
---|
723 | { |
---|
724 | $data['date_metadata_update'] = CURRENT_DATE; |
---|
725 | $data['id']=$id; |
---|
726 | $datas[] = $data; |
---|
727 | |
---|
728 | foreach (array('keywords', 'tags') as $key) |
---|
729 | { |
---|
730 | if (isset($data[$key])) |
---|
731 | { |
---|
732 | if (!isset($tags_of[$id])) |
---|
733 | { |
---|
734 | $tags_of[$id] = array(); |
---|
735 | } |
---|
736 | |
---|
737 | foreach (explode(',', $data[$key]) as $tag_name) |
---|
738 | { |
---|
739 | $tags_of[$id][] = tag_id_from_tag_name($tag_name); |
---|
740 | } |
---|
741 | } |
---|
742 | } |
---|
743 | } |
---|
744 | else |
---|
745 | { |
---|
746 | $errors[] = array( |
---|
747 | 'path' => $element_infos['path'], |
---|
748 | 'type' => 'PWG-ERROR-NO-FS' |
---|
749 | ); |
---|
750 | } |
---|
751 | } |
---|
752 | |
---|
753 | if (!$simulate) |
---|
754 | { |
---|
755 | if (count($datas) > 0) |
---|
756 | { |
---|
757 | mass_updates( |
---|
758 | IMAGES_TABLE, |
---|
759 | // fields |
---|
760 | array( |
---|
761 | 'primary' => array('id'), |
---|
762 | 'update' => array_unique( |
---|
763 | array_merge( |
---|
764 | array_diff( |
---|
765 | $site_reader->get_metadata_attributes(), |
---|
766 | // keywords and tags fields are managed separately |
---|
767 | array('keywords', 'tags') |
---|
768 | ), |
---|
769 | array('date_metadata_update')) |
---|
770 | ) |
---|
771 | ), |
---|
772 | $datas, |
---|
773 | isset($_POST['meta_empty_overrides']) ? 0 : MASS_UPDATES_SKIP_EMPTY |
---|
774 | ); |
---|
775 | } |
---|
776 | set_tags_of($tags_of); |
---|
777 | } |
---|
778 | |
---|
779 | $template->append('footer_elements', '<!-- metadata update : ' |
---|
780 | . get_elapsed_time($start, get_moment()) |
---|
781 | . ' -->'); |
---|
782 | |
---|
783 | $template->assign( |
---|
784 | 'metadata_result', |
---|
785 | array( |
---|
786 | 'NB_ELEMENTS_DONE' => count($datas), |
---|
787 | 'NB_ELEMENTS_CANDIDATES' => count($files), |
---|
788 | 'NB_ERRORS' => count($errors), |
---|
789 | )); |
---|
790 | } |
---|
791 | |
---|
792 | // +-----------------------------------------------------------------------+ |
---|
793 | // | template initialization | |
---|
794 | // +-----------------------------------------------------------------------+ |
---|
795 | $template->set_filenames(array('update'=>'site_update.tpl')); |
---|
796 | $result_title = ''; |
---|
797 | if (isset($simulate) and $simulate) |
---|
798 | { |
---|
799 | $result_title.= '['.l10n('Simulation').'] '; |
---|
800 | } |
---|
801 | |
---|
802 | // used_metadata string is displayed to inform admin which metadata will be |
---|
803 | // used from files for synchronization |
---|
804 | $used_metadata = implode( ', ', $site_reader->get_metadata_attributes()); |
---|
805 | if ($site_is_remote and !isset($_POST['submit']) ) |
---|
806 | { |
---|
807 | $used_metadata.= ' + ...'; |
---|
808 | } |
---|
809 | |
---|
810 | $template->assign( |
---|
811 | array( |
---|
812 | 'SITE_URL'=>$site_url, |
---|
813 | 'U_SITE_MANAGER'=> get_root_url().'admin.php?page=site_manager', |
---|
814 | 'L_RESULT_UPDATE'=>$result_title.l10n('Search for new images in the directories'), |
---|
815 | 'L_RESULT_METADATA'=>$result_title.l10n('Metadata synchronization results'), |
---|
816 | 'METADATA_LIST' => $used_metadata, |
---|
817 | 'U_HELP' => get_root_url().'admin/popuphelp.php?page=synchronize', |
---|
818 | )); |
---|
819 | |
---|
820 | // +-----------------------------------------------------------------------+ |
---|
821 | // | introduction : choices | |
---|
822 | // +-----------------------------------------------------------------------+ |
---|
823 | if (isset($_POST['submit'])) |
---|
824 | { |
---|
825 | $tpl_introduction = array( |
---|
826 | 'sync' => $_POST['sync'], |
---|
827 | 'sync_meta' => isset($_POST['sync_meta']) ? true : false, |
---|
828 | 'display_info' => isset($_POST['display_info']) and $_POST['display_info']==1, |
---|
829 | 'add_to_caddie' => isset($_POST['add_to_caddie']) and $_POST['add_to_caddie']==1, |
---|
830 | 'subcats_included' => isset($_POST['subcats-included']) and $_POST['subcats-included']==1, |
---|
831 | 'privacy_level_selected' => (int)@$_POST['privacy_level'], |
---|
832 | 'meta_all' => isset($_POST['meta_all']) ? true : false, |
---|
833 | 'meta_empty_overrides' => isset($_POST['meta_empty_overrides']) ? true : false, |
---|
834 | ); |
---|
835 | |
---|
836 | if (isset($_POST['cat']) and is_numeric($_POST['cat'])) |
---|
837 | { |
---|
838 | $cat_selected = array($_POST['cat']); |
---|
839 | } |
---|
840 | else |
---|
841 | { |
---|
842 | $cat_selected = array(); |
---|
843 | } |
---|
844 | } |
---|
845 | else |
---|
846 | { |
---|
847 | $tpl_introduction = array( |
---|
848 | 'sync' => 'dirs', |
---|
849 | 'sync_meta' => true, |
---|
850 | 'display_info' => false, |
---|
851 | 'add_to_caddie' => false, |
---|
852 | 'subcats_included' => true, |
---|
853 | 'privacy_level_selected' => 0, |
---|
854 | 'meta_all' => false, |
---|
855 | 'meta_empty_overrides' => false, |
---|
856 | ); |
---|
857 | |
---|
858 | $cat_selected = array(); |
---|
859 | |
---|
860 | if (isset($_GET['cat_id'])) |
---|
861 | { |
---|
862 | check_input_parameter('cat_id', $_GET, false, PATTERN_ID); |
---|
863 | |
---|
864 | $cat_selected = array($_GET['cat_id']); |
---|
865 | $tpl_introduction['sync'] = 'files'; |
---|
866 | } |
---|
867 | } |
---|
868 | |
---|
869 | $tpl_introduction['privacy_level_options'] = get_privacy_level_options(); |
---|
870 | |
---|
871 | $template->assign('introduction', $tpl_introduction); |
---|
872 | |
---|
873 | $query = ' |
---|
874 | SELECT id,name,uppercats,global_rank |
---|
875 | FROM '.CATEGORIES_TABLE.' |
---|
876 | WHERE site_id = '.$site_id; |
---|
877 | display_select_cat_wrapper($query, |
---|
878 | $cat_selected, |
---|
879 | 'category_options', |
---|
880 | false); |
---|
881 | |
---|
882 | |
---|
883 | if (count($errors) > 0) |
---|
884 | { |
---|
885 | foreach ($errors as $error) |
---|
886 | { |
---|
887 | $template->append( |
---|
888 | 'sync_errors', |
---|
889 | array( |
---|
890 | 'ELEMENT' => $error['path'], |
---|
891 | 'LABEL' => $error['type'].' ('.$error_labels[$error['type']][0].')' |
---|
892 | )); |
---|
893 | } |
---|
894 | |
---|
895 | foreach ($error_labels as $error_type=>$error_description) |
---|
896 | { |
---|
897 | $template->append( |
---|
898 | 'sync_error_captions', |
---|
899 | array( |
---|
900 | 'TYPE' => $error_type, |
---|
901 | 'LABEL' => $error_description[1] |
---|
902 | )); |
---|
903 | } |
---|
904 | } |
---|
905 | |
---|
906 | if (count($infos) > 0 |
---|
907 | and isset($_POST['display_info']) |
---|
908 | and $_POST['display_info'] == 1) |
---|
909 | { |
---|
910 | foreach ($infos as $info) |
---|
911 | { |
---|
912 | $template->append( |
---|
913 | 'sync_infos', |
---|
914 | array( |
---|
915 | 'ELEMENT' => $info['path'], |
---|
916 | 'LABEL' => $info['info'] |
---|
917 | )); |
---|
918 | } |
---|
919 | } |
---|
920 | |
---|
921 | // +-----------------------------------------------------------------------+ |
---|
922 | // | sending html code | |
---|
923 | // +-----------------------------------------------------------------------+ |
---|
924 | $template->assign_var_from_handle('ADMIN_CONTENT', 'update'); |
---|
925 | ?> |
---|