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