1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2009 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) = mysql_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) = mysql_fetch_row(pwg_query('SELECT NOW();')); |
---|
54 | define('CURRENT_DATE', $dbnow); |
---|
55 | |
---|
56 | $error_labels = array( |
---|
57 | 'PWG-UPDATE-1' => array( |
---|
58 | l10n('update_wrong_dirname_short'), |
---|
59 | l10n('update_wrong_dirname_info') |
---|
60 | ), |
---|
61 | 'PWG-UPDATE-2' => array( |
---|
62 | l10n('update_missing_tn_short'), |
---|
63 | l10n('update_missing_tn_info').implode(',', $conf['picture_ext']) |
---|
64 | ), |
---|
65 | 'PWG-ERROR-NO-FS' => array( |
---|
66 | l10n('update_missing_file_or_dir'), |
---|
67 | l10n('update_missing_file_or_dir_info') |
---|
68 | ), |
---|
69 | 'PWG-ERROR-VERSION' => array( |
---|
70 | l10n('update_err_pwg_version_differs'), |
---|
71 | l10n('update_err_pwg_version_differs_info') |
---|
72 | ), |
---|
73 | 'PWG-ERROR-NOLISTING' => array( |
---|
74 | l10n('update_err_remote_listing_not_found'), |
---|
75 | l10n('update_err_remote_listing_not_found_info') |
---|
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 REGEXP \'(^|,)'.$_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 = mysql_fetch_array($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 = mysql_fetch_array($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 | $query = ' |
---|
211 | SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_id |
---|
212 | FROM '.CATEGORIES_TABLE; |
---|
213 | list($next_id) = mysql_fetch_array(pwg_query($query)); |
---|
214 | |
---|
215 | // retrieve sub-directories fulldirs from the site reader |
---|
216 | $fs_fulldirs = $site_reader->get_full_directories($basedir); |
---|
217 | |
---|
218 | // get_full_directories doesn't include the base directory, so if it's a |
---|
219 | // category directory, we need to include it in our array |
---|
220 | if (isset($_POST['cat'])) |
---|
221 | { |
---|
222 | array_push($fs_fulldirs, $basedir); |
---|
223 | } |
---|
224 | // If $_POST['subcats-included'] != 1 ("Search in subcategories" is unchecked) |
---|
225 | // $db_fulldirs doesn't include any subdirectories and $fs_fulldirs does |
---|
226 | // So $fs_fulldirs will be limited to the selected basedir |
---|
227 | // (if that one is in $fs_fulldirs) |
---|
228 | if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) |
---|
229 | { |
---|
230 | $fs_fulldirs = array_intersect($fs_fulldirs, array_keys($db_fulldirs)); |
---|
231 | } |
---|
232 | $inserts = array(); |
---|
233 | // new categories are the directories not present yet in the database |
---|
234 | foreach (array_diff($fs_fulldirs, array_keys($db_fulldirs)) as $fulldir) |
---|
235 | { |
---|
236 | $dir = basename($fulldir); |
---|
237 | if (preg_match('/^[a-zA-Z0-9-_.]+$/', $dir)) |
---|
238 | { |
---|
239 | $insert = array( |
---|
240 | 'id' => $next_id++, |
---|
241 | 'dir' => $dir, |
---|
242 | 'name' => str_replace('_', ' ', $dir), |
---|
243 | 'site_id' => $site_id, |
---|
244 | 'commentable' => |
---|
245 | boolean_to_string($conf['newcat_default_commentable']), |
---|
246 | 'uploadable' => $site_is_remote |
---|
247 | ? 'false' |
---|
248 | : boolean_to_string($conf['newcat_default_uploadable']), |
---|
249 | 'status' => $conf{'newcat_default_status'}, |
---|
250 | 'visible' => boolean_to_string($conf{'newcat_default_visible'}), |
---|
251 | ); |
---|
252 | |
---|
253 | if (isset($db_fulldirs[dirname($fulldir)])) |
---|
254 | { |
---|
255 | $parent = $db_fulldirs[dirname($fulldir)]; |
---|
256 | |
---|
257 | $insert{'id_uppercat'} = $parent; |
---|
258 | $insert{'uppercats'} = |
---|
259 | $db_categories[$parent]['uppercats'].','.$insert{'id'}; |
---|
260 | $insert{'rank'} = $next_rank[$parent]++; |
---|
261 | $insert{'global_rank'} = |
---|
262 | $db_categories[$parent]['global_rank'].'.'.$insert{'rank'}; |
---|
263 | if ('private' == $db_categories[$parent]['status']) |
---|
264 | { |
---|
265 | $insert{'status'} = 'private'; |
---|
266 | } |
---|
267 | if ('false' == $db_categories[$parent]['visible']) |
---|
268 | { |
---|
269 | $insert{'visible'} = 'false'; |
---|
270 | } |
---|
271 | } |
---|
272 | else |
---|
273 | { |
---|
274 | $insert{'uppercats'} = $insert{'id'}; |
---|
275 | $insert{'rank'} = $next_rank['NULL']++; |
---|
276 | $insert{'global_rank'} = $insert{'rank'}; |
---|
277 | } |
---|
278 | |
---|
279 | array_push($inserts, $insert); |
---|
280 | array_push( |
---|
281 | $infos, |
---|
282 | array( |
---|
283 | 'path' => $fulldir, |
---|
284 | 'info' => l10n('update_research_added') |
---|
285 | ) |
---|
286 | ); |
---|
287 | |
---|
288 | // add the new category to $db_categories and $db_fulldirs array |
---|
289 | $db_categories[$insert{'id'}] = |
---|
290 | array( |
---|
291 | 'id' => $insert{'id'}, |
---|
292 | 'status' => $insert{'status'}, |
---|
293 | 'visible' => $insert{'visible'}, |
---|
294 | 'uppercats' => $insert{'uppercats'}, |
---|
295 | 'global_rank' => $insert{'global_rank'} |
---|
296 | ); |
---|
297 | $db_fulldirs[$fulldir] = $insert{'id'}; |
---|
298 | $next_rank[$insert{'id'}] = 1; |
---|
299 | } |
---|
300 | else |
---|
301 | { |
---|
302 | array_push( |
---|
303 | $errors, |
---|
304 | array( |
---|
305 | 'path' => $fulldir, |
---|
306 | 'type' => 'PWG-UPDATE-1' |
---|
307 | ) |
---|
308 | ); |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | if (count($inserts) > 0) |
---|
313 | { |
---|
314 | if (!$simulate) |
---|
315 | { |
---|
316 | $dbfields = array( |
---|
317 | 'id','dir','name','site_id','id_uppercat','uppercats','commentable', |
---|
318 | 'uploadable','visible','status','rank','global_rank' |
---|
319 | ); |
---|
320 | mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts); |
---|
321 | } |
---|
322 | |
---|
323 | $counts['new_categories'] = count($inserts); |
---|
324 | } |
---|
325 | |
---|
326 | // to delete categories |
---|
327 | $to_delete = array(); |
---|
328 | foreach (array_diff(array_keys($db_fulldirs), $fs_fulldirs) as $fulldir) |
---|
329 | { |
---|
330 | array_push($to_delete, $db_fulldirs[$fulldir]); |
---|
331 | unset($db_fulldirs[$fulldir]); |
---|
332 | array_push($infos, array('path' => $fulldir, |
---|
333 | 'info' => l10n('update_research_deleted'))); |
---|
334 | } |
---|
335 | if (count($to_delete) > 0) |
---|
336 | { |
---|
337 | if (!$simulate) |
---|
338 | { |
---|
339 | delete_categories($to_delete); |
---|
340 | } |
---|
341 | $counts['del_categories'] = count($to_delete); |
---|
342 | } |
---|
343 | |
---|
344 | $template->append('footer_elements', '<!-- scanning dirs : ' |
---|
345 | . get_elapsed_time($start, get_moment()) |
---|
346 | . ' -->' ); |
---|
347 | } |
---|
348 | // +-----------------------------------------------------------------------+ |
---|
349 | // | files / elements | |
---|
350 | // +-----------------------------------------------------------------------+ |
---|
351 | if (isset($_POST['submit']) and $_POST['sync'] == 'files' |
---|
352 | and !$general_failure) |
---|
353 | { |
---|
354 | $start_files = get_moment(); |
---|
355 | $start= $start_files; |
---|
356 | |
---|
357 | $fs = $site_reader->get_elements($basedir); |
---|
358 | $template->append('footer_elements', '<!-- get_elements: ' |
---|
359 | . get_elapsed_time($start, get_moment()) |
---|
360 | . ' -->' ); |
---|
361 | |
---|
362 | $cat_ids = array_diff(array_keys($db_categories), $to_delete); |
---|
363 | |
---|
364 | $db_elements = array(); |
---|
365 | $db_unvalidated = array(); |
---|
366 | |
---|
367 | if (count($cat_ids) > 0) |
---|
368 | { |
---|
369 | $query = ' |
---|
370 | SELECT id, path |
---|
371 | FROM '.IMAGES_TABLE.' |
---|
372 | WHERE storage_category_id IN (' |
---|
373 | .wordwrap( |
---|
374 | implode(', ', $cat_ids), |
---|
375 | 80, |
---|
376 | "\n" |
---|
377 | ).')'; |
---|
378 | $db_elements = simple_hash_from_query($query, 'id', 'path'); |
---|
379 | |
---|
380 | // searching the unvalidated waiting elements (they must not be taken into |
---|
381 | // account) |
---|
382 | $query = ' |
---|
383 | SELECT file,storage_category_id |
---|
384 | FROM '.WAITING_TABLE.' |
---|
385 | WHERE storage_category_id IN ( |
---|
386 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
387 | AND validated = \'false\''; |
---|
388 | $result = pwg_query($query); |
---|
389 | while ($row = mysql_fetch_array($result)) |
---|
390 | { |
---|
391 | array_push( |
---|
392 | $db_unvalidated, |
---|
393 | array_search( |
---|
394 | $row['storage_category_id'], |
---|
395 | $db_fulldirs) |
---|
396 | .'/'.$row['file'] |
---|
397 | ); |
---|
398 | } |
---|
399 | } |
---|
400 | |
---|
401 | // next element id available |
---|
402 | $query = ' |
---|
403 | SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id |
---|
404 | FROM '.IMAGES_TABLE; |
---|
405 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
406 | |
---|
407 | $start = get_moment(); |
---|
408 | |
---|
409 | $inserts = array(); |
---|
410 | $insert_links = array(); |
---|
411 | |
---|
412 | foreach (array_diff(array_keys($fs), $db_elements, $db_unvalidated) as $path) |
---|
413 | { |
---|
414 | $insert = array(); |
---|
415 | // storage category must exist |
---|
416 | $dirname = dirname($path); |
---|
417 | if (!isset($db_fulldirs[$dirname])) |
---|
418 | { |
---|
419 | continue; |
---|
420 | } |
---|
421 | $filename = basename($path); |
---|
422 | if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename)) |
---|
423 | { |
---|
424 | array_push( |
---|
425 | $errors, |
---|
426 | array( |
---|
427 | 'path' => $path, |
---|
428 | 'type' => 'PWG-UPDATE-1' |
---|
429 | ) |
---|
430 | ); |
---|
431 | |
---|
432 | continue; |
---|
433 | } |
---|
434 | |
---|
435 | if ( isset( $conf['flip_picture_ext'][get_extension($filename)] ) |
---|
436 | and !isset($fs[$path]['tn_ext']) ) |
---|
437 | { // For a picture thumbnail is mandatory and for non picture element, |
---|
438 | // thumbnail and representative are optionnal |
---|
439 | array_push( |
---|
440 | $errors, |
---|
441 | array( |
---|
442 | 'path' => $path, |
---|
443 | 'type' => 'PWG-UPDATE-2' |
---|
444 | ) |
---|
445 | ); |
---|
446 | } |
---|
447 | else |
---|
448 | { |
---|
449 | $insert = array( |
---|
450 | 'id' => $next_element_id++, |
---|
451 | 'file' => $filename, |
---|
452 | 'date_available' => CURRENT_DATE, |
---|
453 | 'path' => $path, |
---|
454 | 'tn_ext' => isset($fs[$path]['tn_ext']) |
---|
455 | ? $fs[$path]['tn_ext'] |
---|
456 | : null, |
---|
457 | 'storage_category_id' => $db_fulldirs[$dirname], |
---|
458 | ); |
---|
459 | |
---|
460 | if ( $_POST['privacy_level']!=0 ) |
---|
461 | { |
---|
462 | $insert['level'] = $_POST['privacy_level']; |
---|
463 | } |
---|
464 | |
---|
465 | array_push( |
---|
466 | $inserts, |
---|
467 | $insert |
---|
468 | ); |
---|
469 | |
---|
470 | array_push( |
---|
471 | $insert_links, |
---|
472 | array( |
---|
473 | 'image_id' => $insert{'id'}, |
---|
474 | 'category_id' => $insert['storage_category_id'], |
---|
475 | ) |
---|
476 | ); |
---|
477 | |
---|
478 | array_push( |
---|
479 | $infos, |
---|
480 | array( |
---|
481 | 'path' => $insert{'path'}, |
---|
482 | 'info' => l10n('update_research_added') |
---|
483 | ) |
---|
484 | ); |
---|
485 | |
---|
486 | $caddiables[] = $insert['id']; |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | if (count($inserts) > 0) |
---|
491 | { |
---|
492 | if (!$simulate) |
---|
493 | { |
---|
494 | // inserts all new elements |
---|
495 | mass_inserts( |
---|
496 | IMAGES_TABLE, |
---|
497 | array_keys($inserts[0]), |
---|
498 | $inserts |
---|
499 | ); |
---|
500 | |
---|
501 | // inserts all links between new elements and their storage category |
---|
502 | mass_inserts( |
---|
503 | IMAGE_CATEGORY_TABLE, |
---|
504 | array_keys($insert_links[0]), |
---|
505 | $insert_links |
---|
506 | ); |
---|
507 | |
---|
508 | // add new elements to caddie |
---|
509 | if (isset($_POST['add_to_caddie']) and $_POST['add_to_caddie'] == 1) |
---|
510 | { |
---|
511 | fill_caddie($caddiables); |
---|
512 | } |
---|
513 | } |
---|
514 | $counts['new_elements'] = count($inserts); |
---|
515 | } |
---|
516 | |
---|
517 | // delete elements that are in database but not in the filesystem |
---|
518 | $to_delete_elements = array(); |
---|
519 | foreach (array_diff($db_elements, array_keys($fs)) as $path) |
---|
520 | { |
---|
521 | array_push($to_delete_elements, array_search($path, $db_elements)); |
---|
522 | array_push($infos, array('path' => $path, |
---|
523 | 'info' => l10n('update_research_deleted'))); |
---|
524 | } |
---|
525 | if (count($to_delete_elements) > 0) |
---|
526 | { |
---|
527 | if (!$simulate) |
---|
528 | { |
---|
529 | delete_elements($to_delete_elements); |
---|
530 | } |
---|
531 | $counts['del_elements'] = count($to_delete_elements); |
---|
532 | } |
---|
533 | |
---|
534 | $template->append('footer_elements', '<!-- scanning files : ' |
---|
535 | . get_elapsed_time($start_files, get_moment()) |
---|
536 | . ' -->' ); |
---|
537 | |
---|
538 | // retrieving informations given by uploaders |
---|
539 | if (!$simulate and count($cat_ids) > 0) |
---|
540 | { |
---|
541 | $query = ' |
---|
542 | SELECT id,file,storage_category_id,infos |
---|
543 | FROM '.WAITING_TABLE.' |
---|
544 | WHERE storage_category_id IN ( |
---|
545 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
546 | AND validated = \'true\''; |
---|
547 | $result = pwg_query($query); |
---|
548 | |
---|
549 | $datas = array(); |
---|
550 | $fields = |
---|
551 | array( |
---|
552 | 'primary' => array('id'), |
---|
553 | 'update' => array('date_creation', 'author', 'name', 'comment') |
---|
554 | ); |
---|
555 | |
---|
556 | $waiting_to_delete = array(); |
---|
557 | |
---|
558 | while ($row = mysql_fetch_array($result)) |
---|
559 | { |
---|
560 | $data = array(); |
---|
561 | |
---|
562 | $query = ' |
---|
563 | SELECT id |
---|
564 | FROM '.IMAGES_TABLE.' |
---|
565 | WHERE storage_category_id = '.$row['storage_category_id'].' |
---|
566 | AND file = \''.$row['file'].'\''; |
---|
567 | list($data['id']) = mysql_fetch_array(pwg_query($query)); |
---|
568 | |
---|
569 | foreach ($fields['update'] as $field) |
---|
570 | { |
---|
571 | $data[$field] = addslashes( getAttribute($row['infos'], $field) ); |
---|
572 | } |
---|
573 | |
---|
574 | array_push($datas, $data); |
---|
575 | array_push($waiting_to_delete, $row['id']); |
---|
576 | } |
---|
577 | |
---|
578 | if (count($datas) > 0) |
---|
579 | { |
---|
580 | mass_updates(IMAGES_TABLE, $fields, $datas); |
---|
581 | |
---|
582 | // delete now useless waiting elements |
---|
583 | $query = ' |
---|
584 | DELETE |
---|
585 | FROM '.WAITING_TABLE.' |
---|
586 | WHERE id IN ('.implode(',', $waiting_to_delete).') |
---|
587 | ;'; |
---|
588 | pwg_query($query); |
---|
589 | } |
---|
590 | } |
---|
591 | } |
---|
592 | |
---|
593 | // +-----------------------------------------------------------------------+ |
---|
594 | // | synchronize files | |
---|
595 | // +-----------------------------------------------------------------------+ |
---|
596 | if (isset($_POST['submit']) |
---|
597 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files') |
---|
598 | and !$general_failure ) |
---|
599 | { |
---|
600 | if (!$simulate) |
---|
601 | { |
---|
602 | $start = get_moment(); |
---|
603 | update_category('all'); |
---|
604 | $template->append('footer_elements', '<!-- update_category(all) : ' |
---|
605 | . get_elapsed_time($start,get_moment()) |
---|
606 | . ' -->' ); |
---|
607 | $start = get_moment(); |
---|
608 | update_global_rank(); |
---|
609 | $template->append('footer_elements', '<!-- ordering categories : ' |
---|
610 | . get_elapsed_time($start, get_moment()) |
---|
611 | . ' -->'); |
---|
612 | } |
---|
613 | |
---|
614 | if ($_POST['sync'] == 'files') |
---|
615 | { |
---|
616 | $start = get_moment(); |
---|
617 | $opts['category_id'] = ''; |
---|
618 | $opts['recursive'] = true; |
---|
619 | if (isset($_POST['cat'])) |
---|
620 | { |
---|
621 | $opts['category_id'] = $_POST['cat']; |
---|
622 | if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) |
---|
623 | { |
---|
624 | $opts['recursive'] = false; |
---|
625 | } |
---|
626 | } |
---|
627 | $files = get_filelist($opts['category_id'], $site_id, |
---|
628 | $opts['recursive'], |
---|
629 | false); |
---|
630 | $template->append('footer_elements', '<!-- get_filelist : ' |
---|
631 | . get_elapsed_time($start, get_moment()) |
---|
632 | . ' -->'); |
---|
633 | $start = get_moment(); |
---|
634 | |
---|
635 | $datas = array(); |
---|
636 | foreach ( $files as $id=>$file ) |
---|
637 | { |
---|
638 | $data = $site_reader->get_element_update_attributes($file); |
---|
639 | if ( !is_array($data) ) |
---|
640 | { |
---|
641 | continue; |
---|
642 | } |
---|
643 | $extension = get_extension($file); |
---|
644 | if ( isset($conf['flip_picture_ext'][$extension]) ) |
---|
645 | { |
---|
646 | if ( !isset($data['tn_ext']) ) |
---|
647 | { |
---|
648 | array_push( |
---|
649 | $errors, |
---|
650 | array( |
---|
651 | 'path' => $file, |
---|
652 | 'type' => 'PWG-UPDATE-2' |
---|
653 | ) |
---|
654 | ); |
---|
655 | continue; |
---|
656 | } |
---|
657 | } |
---|
658 | |
---|
659 | $data['id']=$id; |
---|
660 | array_push($datas, $data); |
---|
661 | } // end foreach file |
---|
662 | |
---|
663 | $counts['upd_elements'] = count($datas); |
---|
664 | if (!$simulate and count($datas)>0 ) |
---|
665 | { |
---|
666 | mass_updates( |
---|
667 | IMAGES_TABLE, |
---|
668 | // fields |
---|
669 | array( |
---|
670 | 'primary' => array('id'), |
---|
671 | 'update' => $site_reader->get_update_attributes(), |
---|
672 | ), |
---|
673 | $datas |
---|
674 | ); |
---|
675 | } |
---|
676 | $template->append('footer_elements', '<!-- update files : ' |
---|
677 | . get_elapsed_time($start,get_moment()) |
---|
678 | . ' -->'); |
---|
679 | }// end if sync files |
---|
680 | } |
---|
681 | |
---|
682 | // +-----------------------------------------------------------------------+ |
---|
683 | // | synchronize files | |
---|
684 | // +-----------------------------------------------------------------------+ |
---|
685 | if (isset($_POST['submit']) |
---|
686 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')) |
---|
687 | { |
---|
688 | $template->assign( |
---|
689 | 'update_result', |
---|
690 | array( |
---|
691 | 'NB_NEW_CATEGORIES'=>$counts['new_categories'], |
---|
692 | 'NB_DEL_CATEGORIES'=>$counts['del_categories'], |
---|
693 | 'NB_NEW_ELEMENTS'=>$counts['new_elements'], |
---|
694 | 'NB_DEL_ELEMENTS'=>$counts['del_elements'], |
---|
695 | 'NB_UPD_ELEMENTS'=>$counts['upd_elements'], |
---|
696 | 'NB_ERRORS'=>count($errors), |
---|
697 | )); |
---|
698 | } |
---|
699 | |
---|
700 | // +-----------------------------------------------------------------------+ |
---|
701 | // | synchronize metadata | |
---|
702 | // +-----------------------------------------------------------------------+ |
---|
703 | if (isset($_POST['submit']) and isset($_POST['sync_meta']) |
---|
704 | and !$general_failure) |
---|
705 | { |
---|
706 | // sync only never synchronized files ? |
---|
707 | $opts['only_new'] = isset($_POST['meta_all']) ? false : true; |
---|
708 | $opts['category_id'] = ''; |
---|
709 | $opts['recursive'] = true; |
---|
710 | |
---|
711 | if (isset($_POST['cat'])) |
---|
712 | { |
---|
713 | $opts['category_id'] = $_POST['cat']; |
---|
714 | // recursive ? |
---|
715 | if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) |
---|
716 | { |
---|
717 | $opts['recursive'] = false; |
---|
718 | } |
---|
719 | } |
---|
720 | $start = get_moment(); |
---|
721 | $files = get_filelist($opts['category_id'], $site_id, |
---|
722 | $opts['recursive'], |
---|
723 | $opts['only_new']); |
---|
724 | |
---|
725 | $template->append('footer_elements', '<!-- get_filelist : ' |
---|
726 | . get_elapsed_time($start, get_moment()) |
---|
727 | . ' -->'); |
---|
728 | |
---|
729 | $start = get_moment(); |
---|
730 | $datas = array(); |
---|
731 | $tags_of = array(); |
---|
732 | |
---|
733 | $has_high_images = array(); |
---|
734 | |
---|
735 | $image_ids = array(); |
---|
736 | foreach ($files as $id => $file) |
---|
737 | { |
---|
738 | array_push($image_ids, $id); |
---|
739 | } |
---|
740 | |
---|
741 | if (count($image_ids) > 0) |
---|
742 | { |
---|
743 | $query = ' |
---|
744 | SELECT id |
---|
745 | FROM '.IMAGES_TABLE.' |
---|
746 | WHERE has_high = \'true\' |
---|
747 | AND id IN ( |
---|
748 | '.wordwrap(implode(', ', $image_ids), 80, "\n").' |
---|
749 | )'; |
---|
750 | $has_high_images = array_from_query($query, 'id' ); |
---|
751 | } |
---|
752 | |
---|
753 | foreach ( $files as $id=>$file ) |
---|
754 | { |
---|
755 | $data = $site_reader->get_element_metadata( |
---|
756 | $file, |
---|
757 | in_array($id, $has_high_images) |
---|
758 | ); |
---|
759 | |
---|
760 | if ( is_array($data) ) |
---|
761 | { |
---|
762 | $data['date_metadata_update'] = CURRENT_DATE; |
---|
763 | $data['id']=$id; |
---|
764 | array_push($datas, $data); |
---|
765 | |
---|
766 | foreach (array('keywords', 'tags') as $key) |
---|
767 | { |
---|
768 | if (isset($data[$key])) |
---|
769 | { |
---|
770 | if (!isset($tags_of[$id])) |
---|
771 | { |
---|
772 | $tags_of[$id] = array(); |
---|
773 | } |
---|
774 | |
---|
775 | foreach (explode(',', $data[$key]) as $tag_name) |
---|
776 | { |
---|
777 | array_push( |
---|
778 | $tags_of[$id], |
---|
779 | tag_id_from_tag_name($tag_name) |
---|
780 | ); |
---|
781 | } |
---|
782 | } |
---|
783 | } |
---|
784 | } |
---|
785 | else |
---|
786 | { |
---|
787 | array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS')); |
---|
788 | } |
---|
789 | } |
---|
790 | |
---|
791 | if (!$simulate) |
---|
792 | { |
---|
793 | if (count($datas) > 0) |
---|
794 | { |
---|
795 | mass_updates( |
---|
796 | IMAGES_TABLE, |
---|
797 | // fields |
---|
798 | array( |
---|
799 | 'primary' => array('id'), |
---|
800 | 'update' => array_unique( |
---|
801 | array_merge( |
---|
802 | array_diff( |
---|
803 | $site_reader->get_metadata_attributes(), |
---|
804 | // keywords and tags fields are managed separately |
---|
805 | array('keywords', 'tags') |
---|
806 | ), |
---|
807 | array('date_metadata_update')) |
---|
808 | ) |
---|
809 | ), |
---|
810 | $datas, |
---|
811 | isset($_POST['meta_empty_overrides']) ? 0 : MASS_UPDATES_SKIP_EMPTY |
---|
812 | ); |
---|
813 | } |
---|
814 | set_tags_of($tags_of); |
---|
815 | } |
---|
816 | |
---|
817 | $template->append('footer_elements', '<!-- metadata update : ' |
---|
818 | . get_elapsed_time($start, get_moment()) |
---|
819 | . ' -->'); |
---|
820 | |
---|
821 | $template->assign( |
---|
822 | 'metadata_result', |
---|
823 | array( |
---|
824 | 'NB_ELEMENTS_DONE' => count($datas), |
---|
825 | 'NB_ELEMENTS_CANDIDATES' => count($files), |
---|
826 | 'NB_ERRORS' => count($errors), |
---|
827 | )); |
---|
828 | } |
---|
829 | |
---|
830 | // +-----------------------------------------------------------------------+ |
---|
831 | // | template initialization | |
---|
832 | // +-----------------------------------------------------------------------+ |
---|
833 | $template->set_filenames(array('update'=>'site_update.tpl')); |
---|
834 | $result_title = ''; |
---|
835 | if (isset($simulate) and $simulate) |
---|
836 | { |
---|
837 | $result_title.= l10n('update_simulation_title').' '; |
---|
838 | } |
---|
839 | |
---|
840 | // used_metadata string is displayed to inform admin which metadata will be |
---|
841 | // used from files for synchronization |
---|
842 | $used_metadata = implode( ', ', $site_reader->get_metadata_attributes()); |
---|
843 | if ($site_is_remote and !isset($_POST['submit']) ) |
---|
844 | { |
---|
845 | $used_metadata.= ' + ...'; |
---|
846 | } |
---|
847 | |
---|
848 | $template->assign( |
---|
849 | array( |
---|
850 | 'SITE_URL'=>$site_url, |
---|
851 | 'U_SITE_MANAGER'=> get_root_url().'admin.php?page=site_manager', |
---|
852 | 'L_RESULT_UPDATE'=>$result_title.l10n('update_part_research'), |
---|
853 | 'L_RESULT_METADATA'=>$result_title.l10n('update_result_metadata'), |
---|
854 | 'METADATA_LIST' => $used_metadata, |
---|
855 | 'U_HELP' => get_root_url().'popuphelp.php?page=synchronize', |
---|
856 | )); |
---|
857 | |
---|
858 | // +-----------------------------------------------------------------------+ |
---|
859 | // | introduction : choices | |
---|
860 | // +-----------------------------------------------------------------------+ |
---|
861 | if (isset($_POST['submit'])) |
---|
862 | { |
---|
863 | $tpl_introduction = array( |
---|
864 | 'sync' => $_POST['sync'], |
---|
865 | 'sync_meta' => isset($_POST['sync_meta']) ? true : false, |
---|
866 | 'display_info' => isset($_POST['display_info']) and $_POST['display_info']==1, |
---|
867 | 'add_to_caddie' => isset($_POST['add_to_caddie']) and $_POST['add_to_caddie']==1, |
---|
868 | 'subcats_included' => isset($_POST['subcats-included']) and $_POST['subcats-included']==1, |
---|
869 | 'privacy_level_selected' => (int)@$_POST['privacy_level'], |
---|
870 | 'meta_all' => isset($_POST['meta_all']) ? true : false, |
---|
871 | 'meta_empty_overrides' => isset($_POST['meta_empty_overrides']) ? true : false, |
---|
872 | ); |
---|
873 | |
---|
874 | if (isset($_POST['cat']) and is_numeric($_POST['cat'])) |
---|
875 | { |
---|
876 | $cat_selected = array($_POST['cat']); |
---|
877 | } |
---|
878 | else |
---|
879 | { |
---|
880 | $cat_selected = array(); |
---|
881 | } |
---|
882 | } |
---|
883 | else |
---|
884 | { |
---|
885 | $tpl_introduction = array( |
---|
886 | 'sync' => 'dirs', |
---|
887 | 'sync_meta' => true, |
---|
888 | 'display_info' => false, |
---|
889 | 'add_to_caddie' => false, |
---|
890 | 'subcats_included' => true, |
---|
891 | 'privacy_level_selected' => 0, |
---|
892 | 'meta_all' => false, |
---|
893 | 'meta_empty_overrides' => false, |
---|
894 | ); |
---|
895 | |
---|
896 | $cat_selected = array(); |
---|
897 | } |
---|
898 | |
---|
899 | $tpl_introduction['privacy_level_options']=array(); |
---|
900 | foreach ($conf['available_permission_levels'] as $level) |
---|
901 | { |
---|
902 | $tpl_introduction['privacy_level_options'][$level] = l10n( sprintf('Level %d', $level) ); |
---|
903 | } |
---|
904 | |
---|
905 | $template->assign('introduction', $tpl_introduction); |
---|
906 | |
---|
907 | $query = ' |
---|
908 | SELECT id,name,uppercats,global_rank |
---|
909 | FROM '.CATEGORIES_TABLE.' |
---|
910 | WHERE site_id = '.$site_id; |
---|
911 | display_select_cat_wrapper($query, |
---|
912 | $cat_selected, |
---|
913 | 'category_options', |
---|
914 | false); |
---|
915 | |
---|
916 | |
---|
917 | if (count($errors) > 0) |
---|
918 | { |
---|
919 | foreach ($errors as $error) |
---|
920 | { |
---|
921 | $template->append( |
---|
922 | 'sync_errors', |
---|
923 | array( |
---|
924 | 'ELEMENT' => $error['path'], |
---|
925 | 'LABEL' => $error['type'].' ('.$error_labels[$error['type']][0].')' |
---|
926 | )); |
---|
927 | } |
---|
928 | |
---|
929 | foreach ($error_labels as $error_type=>$error_description) |
---|
930 | { |
---|
931 | $template->append( |
---|
932 | 'sync_error_captions', |
---|
933 | array( |
---|
934 | 'TYPE' => $error_type, |
---|
935 | 'LABEL' => $error_description[1] |
---|
936 | )); |
---|
937 | } |
---|
938 | } |
---|
939 | |
---|
940 | if (count($infos) > 0 |
---|
941 | and isset($_POST['display_info']) |
---|
942 | and $_POST['display_info'] == 1) |
---|
943 | { |
---|
944 | foreach ($infos as $info) |
---|
945 | { |
---|
946 | $template->append( |
---|
947 | 'sync_infos', |
---|
948 | array( |
---|
949 | 'ELEMENT' => $info['path'], |
---|
950 | 'LABEL' => $info['info'] |
---|
951 | )); |
---|
952 | } |
---|
953 | } |
---|
954 | |
---|
955 | // +-----------------------------------------------------------------------+ |
---|
956 | // | sending html code | |
---|
957 | // +-----------------------------------------------------------------------+ |
---|
958 | $template->assign_var_from_handle('ADMIN_CONTENT', 'update'); |
---|
959 | ?> |
---|