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