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