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