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