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