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