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