[1058] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[2297] | 3 | // | Piwigo - a PHP based picture gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[3046] | 5 | // | Copyright(C) 2008-2009 Piwigo Team http://piwigo.org | |
---|
[2297] | 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 | // +-----------------------------------------------------------------------+ |
---|
[1058] | 23 | |
---|
| 24 | if (!defined('PHPWG_ROOT_PATH')) |
---|
| 25 | { |
---|
[1064] | 26 | die('Hacking attempt!'); |
---|
[1058] | 27 | } |
---|
| 28 | |
---|
[1072] | 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 | |
---|
[1064] | 36 | if (!is_numeric($_GET['site'])) |
---|
[1058] | 37 | { |
---|
| 38 | die ('site param missing or invalid'); |
---|
| 39 | } |
---|
| 40 | $site_id = $_GET['site']; |
---|
[1064] | 41 | |
---|
| 42 | $query=' |
---|
| 43 | SELECT galleries_url |
---|
| 44 | FROM '.SITES_TABLE.' |
---|
[2491] | 45 | WHERE id = '.$site_id; |
---|
[1064] | 46 | list($site_url) = mysql_fetch_row(pwg_query($query)); |
---|
| 47 | if (!isset($site_url)) |
---|
[1058] | 48 | { |
---|
[1064] | 49 | die('site '.$site_id.' does not exist'); |
---|
[1058] | 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( |
---|
[1064] | 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 | ); |
---|
[1058] | 78 | $errors = array(); |
---|
| 79 | $infos = array(); |
---|
| 80 | |
---|
| 81 | if ($site_is_remote) |
---|
| 82 | { |
---|
[1064] | 83 | include_once(PHPWG_ROOT_PATH.'admin/site_reader_remote.php'); |
---|
[1107] | 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); |
---|
[1058] | 91 | } |
---|
| 92 | else |
---|
| 93 | { |
---|
| 94 | include_once( PHPWG_ROOT_PATH.'admin/site_reader_local.php'); |
---|
| 95 | $site_reader = new LocalSiteReader($site_url); |
---|
| 96 | } |
---|
| 97 | |
---|
[1064] | 98 | $general_failure = true; |
---|
[1058] | 99 | if (isset($_POST['submit'])) |
---|
| 100 | { |
---|
[1204] | 101 | if (!isset($conf['flip_picture_ext'])) |
---|
| 102 | { |
---|
| 103 | $conf['flip_picture_ext'] = array_flip($conf['picture_ext']); |
---|
| 104 | } |
---|
[1058] | 105 | if ($site_reader->open()) |
---|
| 106 | { |
---|
| 107 | $general_failure = false; |
---|
| 108 | } |
---|
[1458] | 109 | |
---|
[1058] | 110 | // shall we simulate only |
---|
[1458] | 111 | if ((isset($_POST['simulate']) and $_POST['simulate'] == 1) or is_adviser()) |
---|
[1058] | 112 | { |
---|
| 113 | $simulate = true; |
---|
| 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
| 117 | $simulate = false; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | // +-----------------------------------------------------------------------+ |
---|
| 122 | // | directories / categories | |
---|
| 123 | // +-----------------------------------------------------------------------+ |
---|
| 124 | if (isset($_POST['submit']) |
---|
[2038] | 125 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')) |
---|
[1058] | 126 | { |
---|
| 127 | $counts['new_categories'] = 0; |
---|
| 128 | $counts['del_categories'] = 0; |
---|
| 129 | $counts['del_elements'] = 0; |
---|
| 130 | $counts['new_elements'] = 0; |
---|
[1204] | 131 | $counts['upd_elements'] = 0; |
---|
[2038] | 132 | } |
---|
[1058] | 133 | |
---|
[2038] | 134 | |
---|
| 135 | if (isset($_POST['submit']) |
---|
| 136 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files') |
---|
| 137 | and !$general_failure) |
---|
| 138 | { |
---|
[1058] | 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 | } |
---|
[2491] | 161 | $db_categories = hash_from_query($query, 'id'); |
---|
[1058] | 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 |
---|
[2491] | 186 | FROM '.CATEGORIES_TABLE; |
---|
[1058] | 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.' |
---|
[2491] | 197 | GROUP BY id_uppercat'; |
---|
[1058] | 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 |
---|
[2491] | 212 | FROM '.CATEGORIES_TABLE; |
---|
[1058] | 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 | } |
---|
[2344] | 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 | } |
---|
[1058] | 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 | { |
---|
[1064] | 239 | $insert = array( |
---|
| 240 | 'id' => $next_id++, |
---|
| 241 | 'dir' => $dir, |
---|
| 242 | 'name' => str_replace('_', ' ', $dir), |
---|
| 243 | 'site_id' => $site_id, |
---|
[1278] | 244 | 'commentable' => |
---|
| 245 | boolean_to_string($conf['newcat_default_commentable']), |
---|
[1064] | 246 | 'uploadable' => $site_is_remote |
---|
[1296] | 247 | ? 'false' |
---|
[1278] | 248 | : boolean_to_string($conf['newcat_default_uploadable']), |
---|
[1284] | 249 | 'status' => $conf{'newcat_default_status'}, |
---|
[1278] | 250 | 'visible' => boolean_to_string($conf{'newcat_default_visible'}), |
---|
[1064] | 251 | ); |
---|
[1058] | 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); |
---|
[1064] | 280 | array_push( |
---|
| 281 | $infos, |
---|
| 282 | array( |
---|
| 283 | 'path' => $fulldir, |
---|
| 284 | 'info' => l10n('update_research_added') |
---|
| 285 | ) |
---|
| 286 | ); |
---|
[1058] | 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 | { |
---|
[1064] | 302 | array_push( |
---|
| 303 | $errors, |
---|
| 304 | array( |
---|
| 305 | 'path' => $fulldir, |
---|
| 306 | 'type' => 'PWG-UPDATE-1' |
---|
| 307 | ) |
---|
| 308 | ); |
---|
[1058] | 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 | |
---|
[2276] | 344 | $template->append('footer_elements', '<!-- scanning dirs : ' |
---|
[2107] | 345 | . get_elapsed_time($start, get_moment()) |
---|
[2276] | 346 | . ' -->' ); |
---|
[1058] | 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); |
---|
[2276] | 358 | $template->append('footer_elements', '<!-- get_elements: ' |
---|
[2107] | 359 | . get_elapsed_time($start, get_moment()) |
---|
[2276] | 360 | . ' -->' ); |
---|
[1058] | 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.' |
---|
[1121] | 372 | WHERE storage_category_id IN (' |
---|
| 373 | .wordwrap( |
---|
[1064] | 374 | implode(', ', $cat_ids), |
---|
| 375 | 80, |
---|
| 376 | "\n" |
---|
[2491] | 377 | ).')'; |
---|
| 378 | $db_elements = simple_hash_from_query($query, 'id', 'path'); |
---|
[1058] | 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").') |
---|
[2491] | 387 | AND validated = \'false\''; |
---|
[1058] | 388 | $result = pwg_query($query); |
---|
| 389 | while ($row = mysql_fetch_array($result)) |
---|
| 390 | { |
---|
| 391 | array_push( |
---|
| 392 | $db_unvalidated, |
---|
[1064] | 393 | array_search( |
---|
| 394 | $row['storage_category_id'], |
---|
| 395 | $db_fulldirs) |
---|
| 396 | .'/'.$row['file'] |
---|
[1058] | 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 |
---|
[2491] | 404 | FROM '.IMAGES_TABLE; |
---|
[1058] | 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 | { |
---|
[1064] | 424 | array_push( |
---|
| 425 | $errors, |
---|
| 426 | array( |
---|
| 427 | 'path' => $path, |
---|
| 428 | 'type' => 'PWG-UPDATE-1' |
---|
| 429 | ) |
---|
| 430 | ); |
---|
[1107] | 431 | |
---|
[1058] | 432 | continue; |
---|
| 433 | } |
---|
| 434 | |
---|
[2306] | 435 | if ( isset( $conf['flip_picture_ext'][get_extension($filename)] ) |
---|
[2292] | 436 | and !isset($fs[$path]['tn_ext']) ) |
---|
[2306] | 437 | { // For a picture thumbnail is mandatory and for non picture element, |
---|
[2292] | 438 | // thumbnail and representative are optionnal |
---|
| 439 | array_push( |
---|
| 440 | $errors, |
---|
| 441 | array( |
---|
| 442 | 'path' => $path, |
---|
| 443 | 'type' => 'PWG-UPDATE-2' |
---|
| 444 | ) |
---|
| 445 | ); |
---|
[1058] | 446 | } |
---|
| 447 | else |
---|
| 448 | { |
---|
[1064] | 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, |
---|
[1121] | 457 | 'storage_category_id' => $db_fulldirs[$dirname], |
---|
[1064] | 458 | ); |
---|
[2306] | 459 | |
---|
[2292] | 460 | if ( $_POST['privacy_level']!=0 ) |
---|
| 461 | { |
---|
| 462 | $insert['level'] = $_POST['privacy_level']; |
---|
| 463 | } |
---|
[1058] | 464 | |
---|
[1064] | 465 | array_push( |
---|
| 466 | $inserts, |
---|
| 467 | $insert |
---|
| 468 | ); |
---|
[1058] | 469 | |
---|
[1064] | 470 | array_push( |
---|
| 471 | $insert_links, |
---|
| 472 | array( |
---|
| 473 | 'image_id' => $insert{'id'}, |
---|
[1121] | 474 | 'category_id' => $insert['storage_category_id'], |
---|
[1064] | 475 | ) |
---|
| 476 | ); |
---|
| 477 | |
---|
| 478 | array_push( |
---|
| 479 | $infos, |
---|
| 480 | array( |
---|
| 481 | 'path' => $insert{'path'}, |
---|
| 482 | 'info' => l10n('update_research_added') |
---|
| 483 | ) |
---|
| 484 | ); |
---|
[2114] | 485 | |
---|
| 486 | $caddiables[] = $insert['id']; |
---|
[1058] | 487 | } |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | if (count($inserts) > 0) |
---|
| 491 | { |
---|
| 492 | if (!$simulate) |
---|
| 493 | { |
---|
| 494 | // inserts all new elements |
---|
[1064] | 495 | mass_inserts( |
---|
| 496 | IMAGES_TABLE, |
---|
[1122] | 497 | array_keys($inserts[0]), |
---|
[1064] | 498 | $inserts |
---|
[1058] | 499 | ); |
---|
| 500 | |
---|
[1064] | 501 | // inserts all links between new elements and their storage category |
---|
| 502 | mass_inserts( |
---|
| 503 | IMAGE_CATEGORY_TABLE, |
---|
[1122] | 504 | array_keys($insert_links[0]), |
---|
[1064] | 505 | $insert_links |
---|
| 506 | ); |
---|
[2114] | 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 | } |
---|
[1058] | 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 | |
---|
[2276] | 534 | $template->append('footer_elements', '<!-- scanning files : ' |
---|
[2107] | 535 | . get_elapsed_time($start_files, get_moment()) |
---|
[2276] | 536 | . ' -->' ); |
---|
[1058] | 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").') |
---|
[2491] | 546 | AND validated = \'true\''; |
---|
[1058] | 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.' |
---|
[1121] | 565 | WHERE storage_category_id = '.$row['storage_category_id'].' |
---|
[2491] | 566 | AND file = \''.$row['file'].'\''; |
---|
[1058] | 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']) |
---|
[1204] | 597 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files') |
---|
| 598 | and !$general_failure ) |
---|
[1058] | 599 | { |
---|
| 600 | if (!$simulate) |
---|
| 601 | { |
---|
| 602 | $start = get_moment(); |
---|
| 603 | update_category('all'); |
---|
[2276] | 604 | $template->append('footer_elements', '<!-- update_category(all) : ' |
---|
[2107] | 605 | . get_elapsed_time($start,get_moment()) |
---|
[2276] | 606 | . ' -->' ); |
---|
[1058] | 607 | $start = get_moment(); |
---|
| 608 | update_global_rank(); |
---|
[2276] | 609 | $template->append('footer_elements', '<!-- ordering categories : ' |
---|
[2107] | 610 | . get_elapsed_time($start, get_moment()) |
---|
[2276] | 611 | . ' -->'); |
---|
[1058] | 612 | } |
---|
[1204] | 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); |
---|
[2276] | 630 | $template->append('footer_elements', '<!-- get_filelist : ' |
---|
[2107] | 631 | . get_elapsed_time($start, get_moment()) |
---|
[2276] | 632 | . ' -->'); |
---|
[1204] | 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 | } |
---|
[2276] | 676 | $template->append('footer_elements', '<!-- update files : ' |
---|
[2107] | 677 | . get_elapsed_time($start,get_moment()) |
---|
[2276] | 678 | . ' -->'); |
---|
[1204] | 679 | }// end if sync files |
---|
[1058] | 680 | } |
---|
| 681 | |
---|
| 682 | // +-----------------------------------------------------------------------+ |
---|
[1204] | 683 | // | synchronize files | |
---|
| 684 | // +-----------------------------------------------------------------------+ |
---|
| 685 | if (isset($_POST['submit']) |
---|
| 686 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')) |
---|
| 687 | { |
---|
[2276] | 688 | $template->assign( |
---|
[1204] | 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 | // +-----------------------------------------------------------------------+ |
---|
[1058] | 701 | // | synchronize metadata | |
---|
| 702 | // +-----------------------------------------------------------------------+ |
---|
[2491] | 703 | if (isset($_POST['submit']) and isset($_POST['sync_meta']) |
---|
[1058] | 704 | and !$general_failure) |
---|
| 705 | { |
---|
| 706 | // sync only never synchronized files ? |
---|
[2491] | 707 | $opts['only_new'] = isset($_POST['meta_all']) ? false : true; |
---|
[1058] | 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 | |
---|
[2276] | 725 | $template->append('footer_elements', '<!-- get_filelist : ' |
---|
[2107] | 726 | . get_elapsed_time($start, get_moment()) |
---|
[2276] | 727 | . ' -->'); |
---|
[1058] | 728 | |
---|
| 729 | $start = get_moment(); |
---|
| 730 | $datas = array(); |
---|
[1119] | 731 | $tags_of = array(); |
---|
[1883] | 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").' |
---|
[2491] | 749 | )'; |
---|
| 750 | $has_high_images = array_from_query($query, 'id' ); |
---|
[1883] | 751 | } |
---|
[2107] | 752 | |
---|
[1058] | 753 | foreach ( $files as $id=>$file ) |
---|
| 754 | { |
---|
[1883] | 755 | $data = $site_reader->get_element_metadata( |
---|
| 756 | $file, |
---|
| 757 | in_array($id, $has_high_images) |
---|
| 758 | ); |
---|
[2107] | 759 | |
---|
[1058] | 760 | if ( is_array($data) ) |
---|
| 761 | { |
---|
| 762 | $data['date_metadata_update'] = CURRENT_DATE; |
---|
| 763 | $data['id']=$id; |
---|
| 764 | array_push($datas, $data); |
---|
[1119] | 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 | } |
---|
[1204] | 774 | |
---|
[1119] | 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 | } |
---|
[1058] | 784 | } |
---|
| 785 | else |
---|
| 786 | { |
---|
| 787 | array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS')); |
---|
| 788 | } |
---|
| 789 | } |
---|
[1119] | 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( |
---|
[1204] | 803 | $site_reader->get_metadata_attributes(), |
---|
[1119] | 804 | // keywords and tags fields are managed separately |
---|
| 805 | array('keywords', 'tags') |
---|
| 806 | ), |
---|
| 807 | array('date_metadata_update')) |
---|
| 808 | ) |
---|
| 809 | ), |
---|
[2491] | 810 | $datas, |
---|
| 811 | isset($_POST['meta_empty_overrides']) ? 0 : MASS_UPDATES_SKIP_EMPTY |
---|
[1058] | 812 | ); |
---|
[1119] | 813 | } |
---|
| 814 | set_tags_of($tags_of); |
---|
[1058] | 815 | } |
---|
| 816 | |
---|
[2276] | 817 | $template->append('footer_elements', '<!-- metadata update : ' |
---|
[2107] | 818 | . get_elapsed_time($start, get_moment()) |
---|
[2276] | 819 | . ' -->'); |
---|
[1058] | 820 | |
---|
[2276] | 821 | $template->assign( |
---|
[1058] | 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 | // +-----------------------------------------------------------------------+ |
---|
[2530] | 833 | $template->set_filenames(array('update'=>'site_update.tpl')); |
---|
[1058] | 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 |
---|
[1204] | 842 | $used_metadata = implode( ', ', $site_reader->get_metadata_attributes()); |
---|
[1058] | 843 | if ($site_is_remote and !isset($_POST['submit']) ) |
---|
| 844 | { |
---|
| 845 | $used_metadata.= ' + ...'; |
---|
| 846 | } |
---|
| 847 | |
---|
[2276] | 848 | $template->assign( |
---|
[1058] | 849 | array( |
---|
| 850 | 'SITE_URL'=>$site_url, |
---|
[2276] | 851 | 'U_SITE_MANAGER'=> get_root_url().'admin.php?page=site_manager', |
---|
[1058] | 852 | 'L_RESULT_UPDATE'=>$result_title.l10n('update_part_research'), |
---|
| 853 | 'L_RESULT_METADATA'=>$result_title.l10n('update_result_metadata'), |
---|
[2276] | 854 | 'METADATA_LIST' => $used_metadata, |
---|
| 855 | 'U_HELP' => get_root_url().'popuphelp.php?page=synchronize', |
---|
[1058] | 856 | )); |
---|
| 857 | |
---|
| 858 | // +-----------------------------------------------------------------------+ |
---|
| 859 | // | introduction : choices | |
---|
| 860 | // +-----------------------------------------------------------------------+ |
---|
[2491] | 861 | if (isset($_POST['submit'])) |
---|
[1058] | 862 | { |
---|
[2491] | 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'])) |
---|
[1058] | 875 | { |
---|
[2491] | 876 | $cat_selected = array($_POST['cat']); |
---|
[1058] | 877 | } |
---|
| 878 | else |
---|
| 879 | { |
---|
| 880 | $cat_selected = array(); |
---|
| 881 | } |
---|
[2491] | 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 | ); |
---|
[1058] | 895 | |
---|
[2491] | 896 | $cat_selected = array(); |
---|
| 897 | } |
---|
[2292] | 898 | |
---|
[2491] | 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 | } |
---|
[2276] | 904 | |
---|
[2491] | 905 | $template->assign('introduction', $tpl_introduction); |
---|
| 906 | |
---|
| 907 | $query = ' |
---|
[1058] | 908 | SELECT id,name,uppercats,global_rank |
---|
| 909 | FROM '.CATEGORIES_TABLE.' |
---|
[2491] | 910 | WHERE site_id = '.$site_id; |
---|
| 911 | display_select_cat_wrapper($query, |
---|
| 912 | $cat_selected, |
---|
| 913 | 'category_options', |
---|
| 914 | false); |
---|
[1058] | 915 | |
---|
[2491] | 916 | |
---|
[1058] | 917 | if (count($errors) > 0) |
---|
| 918 | { |
---|
| 919 | foreach ($errors as $error) |
---|
| 920 | { |
---|
[2276] | 921 | $template->append( |
---|
| 922 | 'sync_errors', |
---|
[1058] | 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 | { |
---|
[2276] | 931 | $template->append( |
---|
| 932 | 'sync_error_captions', |
---|
[1058] | 933 | array( |
---|
| 934 | 'TYPE' => $error_type, |
---|
| 935 | 'LABEL' => $error_description[1] |
---|
| 936 | )); |
---|
| 937 | } |
---|
[2276] | 938 | } |
---|
[1058] | 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 | { |
---|
[2276] | 946 | $template->append( |
---|
| 947 | 'sync_infos', |
---|
[1058] | 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'); |
---|
[1903] | 959 | ?> |
---|