[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[12917] | 5 | // | Copyright(C) 2008-2012 Piwigo Team http://piwigo.org | |
---|
[2297] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
[2] | 23 | |
---|
[491] | 24 | include(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); |
---|
| 25 | |
---|
[467] | 26 | // The function delete_site deletes a site and call the function |
---|
| 27 | // delete_categories for each primary category of the site |
---|
[12] | 28 | function delete_site( $id ) |
---|
| 29 | { |
---|
| 30 | // destruction of the categories of the site |
---|
[467] | 31 | $query = ' |
---|
| 32 | SELECT id |
---|
| 33 | FROM '.CATEGORIES_TABLE.' |
---|
| 34 | WHERE site_id = '.$id.' |
---|
| 35 | ;'; |
---|
[18628] | 36 | $category_ids = array_from_query($query, 'id'); |
---|
[467] | 37 | delete_categories($category_ids); |
---|
[1060] | 38 | |
---|
[12] | 39 | // destruction of the site |
---|
[467] | 40 | $query = ' |
---|
| 41 | DELETE FROM '.SITES_TABLE.' |
---|
| 42 | WHERE id = '.$id.' |
---|
| 43 | ;'; |
---|
[587] | 44 | pwg_query($query); |
---|
[12] | 45 | } |
---|
[345] | 46 | |
---|
[1060] | 47 | |
---|
[467] | 48 | // The function delete_categories deletes the categories identified by the |
---|
| 49 | // (numeric) key of the array $ids. It also deletes (in the database) : |
---|
[8265] | 50 | // - all the elements physically linked to the category (delete_elements, see further) |
---|
[467] | 51 | // - all the links between elements and this category |
---|
[12] | 52 | // - all the restrictions linked to the category |
---|
| 53 | // The function works recursively. |
---|
[8265] | 54 | // |
---|
| 55 | // the $photo_deletion_mode is for photos virtually linked to the categorty |
---|
| 56 | // * no_delete : delete no photo, may create orphans |
---|
| 57 | // * delete_orphans : delete photos that are no longer linked to any category |
---|
| 58 | // * force_delete : delete photos even if they are linked to another category |
---|
| 59 | function delete_categories($ids, $photo_deletion_mode='no_delete') |
---|
[12] | 60 | { |
---|
[521] | 61 | if (count($ids) == 0) |
---|
| 62 | { |
---|
| 63 | return; |
---|
| 64 | } |
---|
[657] | 65 | |
---|
| 66 | // add sub-category ids to the given ids : if a category is deleted, all |
---|
| 67 | // sub-categories must be so |
---|
| 68 | $ids = get_subcat_ids($ids); |
---|
[1060] | 69 | |
---|
[8265] | 70 | // destruction of all photos physically linked to the category |
---|
[467] | 71 | $query = ' |
---|
[1121] | 72 | SELECT id |
---|
| 73 | FROM '.IMAGES_TABLE.' |
---|
| 74 | WHERE storage_category_id IN ( |
---|
| 75 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 76 | ;'; |
---|
[18628] | 77 | $element_ids = array_from_query($query, 'id'); |
---|
[521] | 78 | delete_elements($element_ids); |
---|
[21] | 79 | |
---|
[8265] | 80 | // now, should we delete photos that are virtually linked to the category? |
---|
| 81 | if ('delete_orphans' == $photo_deletion_mode or 'force_delete' == $photo_deletion_mode) |
---|
| 82 | { |
---|
| 83 | $query = ' |
---|
| 84 | SELECT |
---|
| 85 | DISTINCT(image_id) |
---|
| 86 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 87 | WHERE category_id IN ('.implode(',', $ids).') |
---|
| 88 | ;'; |
---|
| 89 | $image_ids_linked = array_from_query($query, 'image_id'); |
---|
[8848] | 90 | |
---|
| 91 | if (count($image_ids_linked) > 0) |
---|
[8265] | 92 | { |
---|
[8848] | 93 | if ('delete_orphans' == $photo_deletion_mode) |
---|
| 94 | { |
---|
| 95 | $query = ' |
---|
[8265] | 96 | SELECT |
---|
| 97 | DISTINCT(image_id) |
---|
| 98 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 99 | WHERE image_id IN ('.implode(',', $image_ids_linked).') |
---|
| 100 | AND category_id NOT IN ('.implode(',', $ids).') |
---|
| 101 | ;'; |
---|
[8848] | 102 | $image_ids_not_orphans = array_from_query($query, 'image_id'); |
---|
| 103 | $image_ids_to_delete = array_diff($image_ids_linked, $image_ids_not_orphans); |
---|
| 104 | } |
---|
[8265] | 105 | |
---|
[8848] | 106 | if ('force_delete' == $photo_deletion_mode) |
---|
| 107 | { |
---|
| 108 | $image_ids_to_delete = $image_ids_linked; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | delete_elements($image_ids_to_delete, true); |
---|
[8265] | 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
[61] | 115 | // destruction of the links between images and this category |
---|
[467] | 116 | $query = ' |
---|
| 117 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
[657] | 118 | WHERE category_id IN ( |
---|
| 119 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 120 | ;'; |
---|
[587] | 121 | pwg_query($query); |
---|
[61] | 122 | |
---|
[21] | 123 | // destruction of the access linked to the category |
---|
[467] | 124 | $query = ' |
---|
| 125 | DELETE FROM '.USER_ACCESS_TABLE.' |
---|
[657] | 126 | WHERE cat_id IN ( |
---|
| 127 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 128 | ;'; |
---|
[587] | 129 | pwg_query($query); |
---|
[1068] | 130 | |
---|
[467] | 131 | $query = ' |
---|
| 132 | DELETE FROM '.GROUP_ACCESS_TABLE.' |
---|
[657] | 133 | WHERE cat_id IN ( |
---|
| 134 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 135 | ;'; |
---|
[587] | 136 | pwg_query($query); |
---|
[21] | 137 | |
---|
[12] | 138 | // destruction of the category |
---|
[467] | 139 | $query = ' |
---|
| 140 | DELETE FROM '.CATEGORIES_TABLE.' |
---|
[657] | 141 | WHERE id IN ( |
---|
| 142 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 143 | ;'; |
---|
[587] | 144 | pwg_query($query); |
---|
[498] | 145 | |
---|
[2882] | 146 | $query=' |
---|
[1866] | 147 | DELETE FROM '.OLD_PERMALINKS_TABLE.' |
---|
[1879] | 148 | WHERE cat_id IN ('.implode(',',$ids).')'; |
---|
[1866] | 149 | pwg_query($query); |
---|
| 150 | |
---|
[2882] | 151 | $query=' |
---|
| 152 | DELETE FROM '.USER_CACHE_CATEGORIES_TABLE.' |
---|
| 153 | WHERE cat_id IN ('.implode(',',$ids).')'; |
---|
| 154 | pwg_query($query); |
---|
| 155 | |
---|
[1605] | 156 | trigger_action('delete_categories', $ids); |
---|
[12] | 157 | } |
---|
[345] | 158 | |
---|
[9191] | 159 | // Deletes all files (on disk) related to given image ids |
---|
| 160 | // @return image ids where files are deleted successfully |
---|
| 161 | function delete_element_files($ids) |
---|
[12] | 162 | { |
---|
[13651] | 163 | global $conf; |
---|
[521] | 164 | if (count($ids) == 0) |
---|
| 165 | { |
---|
[6873] | 166 | return 0; |
---|
[521] | 167 | } |
---|
[1060] | 168 | |
---|
[9191] | 169 | $new_ids = array(); |
---|
[3145] | 170 | |
---|
[9191] | 171 | $query = ' |
---|
[2678] | 172 | SELECT |
---|
| 173 | id, |
---|
| 174 | path, |
---|
[6873] | 175 | representative_ext |
---|
[2678] | 176 | FROM '.IMAGES_TABLE.' |
---|
| 177 | WHERE id IN ('.implode(',', $ids).') |
---|
| 178 | ;'; |
---|
[9191] | 179 | $result = pwg_query($query); |
---|
| 180 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 181 | { |
---|
| 182 | if (url_is_remote($row['path'])) |
---|
[2678] | 183 | { |
---|
[9191] | 184 | continue; |
---|
| 185 | } |
---|
[12917] | 186 | |
---|
[9191] | 187 | $files = array(); |
---|
| 188 | $files[] = get_element_path($row); |
---|
[2678] | 189 | |
---|
[9191] | 190 | if (!empty($row['representative_ext'])) |
---|
| 191 | { |
---|
[12855] | 192 | $files[] = original_to_representative( $files[0], $row['representative_ext']); |
---|
[9191] | 193 | } |
---|
| 194 | |
---|
| 195 | $ok = true; |
---|
[13052] | 196 | if (!isset($conf['never_delete_originals'])) |
---|
| 197 | { |
---|
| 198 | foreach ($files as $path) |
---|
| 199 | { |
---|
| 200 | if (is_file($path) and !unlink($path)) |
---|
| 201 | { |
---|
| 202 | $ok = false; |
---|
| 203 | trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING); |
---|
| 204 | break; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | } |
---|
[12917] | 208 | |
---|
[9191] | 209 | if ($ok) |
---|
| 210 | { |
---|
[12917] | 211 | delete_element_derivatives($row); |
---|
[17724] | 212 | $new_ids[] = $row['id']; |
---|
[9191] | 213 | } |
---|
| 214 | else |
---|
| 215 | { |
---|
| 216 | break; |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | return $new_ids; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | // The function delete_elements deletes the elements identified by the |
---|
| 223 | // (numeric) values of the array $ids. It also deletes (in the database) : |
---|
| 224 | // - all the comments related to elements |
---|
| 225 | // - all the links between categories and elements |
---|
| 226 | // - all the favorites associated to elements |
---|
| 227 | // @return number of deleted elements |
---|
| 228 | function delete_elements($ids, $physical_deletion=false) |
---|
| 229 | { |
---|
| 230 | if (count($ids) == 0) |
---|
| 231 | { |
---|
| 232 | return 0; |
---|
| 233 | } |
---|
| 234 | trigger_action('begin_delete_elements', $ids); |
---|
| 235 | |
---|
| 236 | if ($physical_deletion) |
---|
| 237 | { |
---|
| 238 | $ids = delete_element_files($ids); |
---|
[6873] | 239 | if (count($ids)==0) |
---|
| 240 | { |
---|
| 241 | return 0; |
---|
| 242 | } |
---|
[2678] | 243 | } |
---|
| 244 | |
---|
[12] | 245 | // destruction of the comments on the image |
---|
[467] | 246 | $query = ' |
---|
| 247 | DELETE FROM '.COMMENTS_TABLE.' |
---|
[491] | 248 | WHERE image_id IN ( |
---|
| 249 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 250 | ;'; |
---|
[587] | 251 | pwg_query($query); |
---|
[61] | 252 | |
---|
| 253 | // destruction of the links between images and this category |
---|
[467] | 254 | $query = ' |
---|
| 255 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
[491] | 256 | WHERE image_id IN ( |
---|
| 257 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 258 | ;'; |
---|
[587] | 259 | pwg_query($query); |
---|
[61] | 260 | |
---|
[1119] | 261 | // destruction of the links between images and tags |
---|
| 262 | $query = ' |
---|
| 263 | DELETE FROM '.IMAGE_TAG_TABLE.' |
---|
| 264 | WHERE image_id IN ( |
---|
| 265 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
| 266 | ;'; |
---|
| 267 | pwg_query($query); |
---|
| 268 | |
---|
[12] | 269 | // destruction of the favorites associated with the picture |
---|
[467] | 270 | $query = ' |
---|
| 271 | DELETE FROM '.FAVORITES_TABLE.' |
---|
[491] | 272 | WHERE image_id IN ( |
---|
| 273 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 274 | ;'; |
---|
[587] | 275 | pwg_query($query); |
---|
[523] | 276 | |
---|
| 277 | // destruction of the rates associated to this element |
---|
| 278 | $query = ' |
---|
| 279 | DELETE FROM '.RATE_TABLE.' |
---|
| 280 | WHERE element_id IN ( |
---|
| 281 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
| 282 | ;'; |
---|
[587] | 283 | pwg_query($query); |
---|
[764] | 284 | |
---|
| 285 | // destruction of the rates associated to this element |
---|
| 286 | $query = ' |
---|
| 287 | DELETE FROM '.CADDIE_TABLE.' |
---|
| 288 | WHERE element_id IN ( |
---|
| 289 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
| 290 | ;'; |
---|
| 291 | pwg_query($query); |
---|
[1060] | 292 | |
---|
[12] | 293 | // destruction of the image |
---|
[467] | 294 | $query = ' |
---|
| 295 | DELETE FROM '.IMAGES_TABLE.' |
---|
[491] | 296 | WHERE id IN ( |
---|
| 297 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 298 | ;'; |
---|
[587] | 299 | pwg_query($query); |
---|
[491] | 300 | |
---|
[4731] | 301 | // are the photo used as category representant? |
---|
| 302 | $query = ' |
---|
| 303 | SELECT |
---|
| 304 | id |
---|
| 305 | FROM '.CATEGORIES_TABLE.' |
---|
| 306 | WHERE representative_picture_id IN ( |
---|
| 307 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
| 308 | ;'; |
---|
| 309 | $category_ids = array_from_query($query, 'id'); |
---|
| 310 | if (count($category_ids) > 0) |
---|
| 311 | { |
---|
| 312 | update_category($category_ids); |
---|
| 313 | } |
---|
[5691] | 314 | |
---|
[1605] | 315 | trigger_action('delete_elements', $ids); |
---|
[6873] | 316 | return count($ids); |
---|
[12] | 317 | } |
---|
[345] | 318 | |
---|
[12] | 319 | // The delete_user function delete a user identified by the $user_id |
---|
| 320 | // It also deletes : |
---|
[21] | 321 | // - all the access linked to this user |
---|
| 322 | // - all the links to any group |
---|
[12] | 323 | // - all the favorites linked to this user |
---|
[680] | 324 | // - calculated permissions linked to the user |
---|
[1028] | 325 | // - all datas about notifications for the user |
---|
[650] | 326 | function delete_user($user_id) |
---|
[12] | 327 | { |
---|
[808] | 328 | global $conf; |
---|
[1863] | 329 | $tables = array( |
---|
| 330 | // destruction of the access linked to the user |
---|
| 331 | USER_ACCESS_TABLE, |
---|
| 332 | // destruction of data notification by mail for this user |
---|
| 333 | USER_MAIL_NOTIFICATION_TABLE, |
---|
| 334 | // destruction of data RSS notification for this user |
---|
| 335 | USER_FEED_TABLE, |
---|
| 336 | // deletion of calculated permissions linked to the user |
---|
| 337 | USER_CACHE_TABLE, |
---|
| 338 | // deletion of computed cache data linked to the user |
---|
| 339 | USER_CACHE_CATEGORIES_TABLE, |
---|
| 340 | // destruction of the group links for this user |
---|
| 341 | USER_GROUP_TABLE, |
---|
| 342 | // destruction of the favorites associated with the user |
---|
| 343 | FAVORITES_TABLE, |
---|
| 344 | // destruction of the caddie associated with the user |
---|
| 345 | CADDIE_TABLE, |
---|
[2339] | 346 | // deletion of piwigo specific informations |
---|
[1863] | 347 | USER_INFOS_TABLE, |
---|
| 348 | ); |
---|
[1060] | 349 | |
---|
[1863] | 350 | foreach ($tables as $table) |
---|
| 351 | { |
---|
| 352 | $query = ' |
---|
| 353 | DELETE FROM '.$table.' |
---|
[650] | 354 | WHERE user_id = '.$user_id.' |
---|
| 355 | ;'; |
---|
[1863] | 356 | pwg_query($query); |
---|
| 357 | } |
---|
[21] | 358 | |
---|
[1863] | 359 | // destruction of the user |
---|
[650] | 360 | $query = ' |
---|
[1863] | 361 | DELETE FROM '.SESSIONS_TABLE.' |
---|
[6664] | 362 | WHERE data LIKE \'pwg_uid|i:'.(int)$user_id.';%\' |
---|
[650] | 363 | ;'; |
---|
| 364 | pwg_query($query); |
---|
[21] | 365 | |
---|
[12] | 366 | // destruction of the user |
---|
[650] | 367 | $query = ' |
---|
| 368 | DELETE FROM '.USERS_TABLE.' |
---|
[808] | 369 | WHERE '.$conf['user_fields']['id'].' = '.$user_id.' |
---|
[650] | 370 | ;'; |
---|
| 371 | pwg_query($query); |
---|
[1605] | 372 | |
---|
| 373 | trigger_action('delete_user', $user_id); |
---|
[12] | 374 | } |
---|
[21] | 375 | |
---|
[491] | 376 | /** |
---|
[8762] | 377 | * Deletes all tags linked to no photo |
---|
| 378 | */ |
---|
| 379 | function delete_orphan_tags() |
---|
| 380 | { |
---|
| 381 | $orphan_tags = get_orphan_tags(); |
---|
[12917] | 382 | |
---|
[8762] | 383 | if (count($orphan_tags) > 0) |
---|
| 384 | { |
---|
| 385 | $orphan_tag_ids = array(); |
---|
| 386 | foreach ($orphan_tags as $tag) |
---|
| 387 | { |
---|
[18628] | 388 | $orphan_tag_ids[] = $tag['id']; |
---|
[8762] | 389 | } |
---|
| 390 | |
---|
| 391 | $query = ' |
---|
| 392 | DELETE |
---|
| 393 | FROM '.TAGS_TABLE.' |
---|
| 394 | WHERE id IN ('.implode(',', $orphan_tag_ids).') |
---|
| 395 | ;'; |
---|
| 396 | pwg_query($query); |
---|
| 397 | } |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | /** |
---|
| 401 | * Get all tags (id + name) linked to no photo |
---|
| 402 | */ |
---|
| 403 | function get_orphan_tags() |
---|
| 404 | { |
---|
| 405 | $query = ' |
---|
| 406 | SELECT |
---|
| 407 | id, |
---|
| 408 | name |
---|
| 409 | FROM '.TAGS_TABLE.' |
---|
| 410 | LEFT JOIN '.IMAGE_TAG_TABLE.' ON id = tag_id |
---|
| 411 | WHERE tag_id IS NULL |
---|
| 412 | ;'; |
---|
[18628] | 413 | return array_from_query($query); |
---|
[8762] | 414 | } |
---|
| 415 | |
---|
| 416 | /** |
---|
[2324] | 417 | * Verifies that the representative picture really exists in the db and |
---|
| 418 | * picks up a random represantive if possible and based on config. |
---|
[491] | 419 | * |
---|
| 420 | * @param mixed category id |
---|
| 421 | * @returns void |
---|
| 422 | */ |
---|
[2324] | 423 | function update_category($ids = 'all') |
---|
[61] | 424 | { |
---|
[809] | 425 | global $conf; |
---|
[1060] | 426 | |
---|
[2324] | 427 | if ($ids=='all') |
---|
[61] | 428 | { |
---|
[2324] | 429 | $where_cats = '1=1'; |
---|
[61] | 430 | } |
---|
[2324] | 431 | elseif ( !is_array($ids) ) |
---|
[635] | 432 | { |
---|
[2324] | 433 | $where_cats = '%s='.$ids; |
---|
[635] | 434 | } |
---|
[2324] | 435 | else |
---|
[61] | 436 | { |
---|
[2324] | 437 | if (count($ids) == 0) |
---|
| 438 | { |
---|
| 439 | return false; |
---|
| 440 | } |
---|
| 441 | $where_cats = '%s IN('.wordwrap(implode(', ', $ids), 120, "\n").')'; |
---|
[491] | 442 | } |
---|
[2333] | 443 | |
---|
[2324] | 444 | // find all categories where the setted representative is not possible : |
---|
| 445 | // the picture does not exist |
---|
| 446 | $query = ' |
---|
| 447 | SELECT DISTINCT c.id |
---|
[809] | 448 | FROM '.CATEGORIES_TABLE.' AS c LEFT JOIN '.IMAGES_TABLE.' AS i |
---|
| 449 | ON c.representative_picture_id = i.id |
---|
[491] | 450 | WHERE representative_picture_id IS NOT NULL |
---|
[2324] | 451 | AND '.sprintf($where_cats, 'c.id').' |
---|
[809] | 452 | AND i.id IS NULL |
---|
[491] | 453 | ;'; |
---|
[2324] | 454 | $wrong_representant = array_from_query($query, 'id'); |
---|
[809] | 455 | |
---|
[2324] | 456 | if (count($wrong_representant) > 0) |
---|
| 457 | { |
---|
| 458 | $query = ' |
---|
[809] | 459 | UPDATE '.CATEGORIES_TABLE.' |
---|
| 460 | SET representative_picture_id = NULL |
---|
[2324] | 461 | WHERE id IN ('.wordwrap(implode(', ', $wrong_representant), 120, "\n").') |
---|
[809] | 462 | ;'; |
---|
[2324] | 463 | pwg_query($query); |
---|
| 464 | } |
---|
[809] | 465 | |
---|
[2324] | 466 | if (!$conf['allow_random_representative']) |
---|
| 467 | { |
---|
| 468 | // If the random representant is not allowed, we need to find |
---|
| 469 | // categories with elements and with no representant. Those categories |
---|
| 470 | // must be added to the list of categories to set to a random |
---|
| 471 | // representant. |
---|
| 472 | $query = ' |
---|
| 473 | SELECT DISTINCT id |
---|
| 474 | FROM '.CATEGORIES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' |
---|
| 475 | ON id = category_id |
---|
[635] | 476 | WHERE representative_picture_id IS NULL |
---|
[2324] | 477 | AND '.sprintf($where_cats, 'category_id').' |
---|
[491] | 478 | ;'; |
---|
[2324] | 479 | $to_rand = array_from_query($query, 'id'); |
---|
| 480 | if (count($to_rand) > 0) |
---|
| 481 | { |
---|
| 482 | set_random_representant($to_rand); |
---|
[133] | 483 | } |
---|
[61] | 484 | } |
---|
| 485 | } |
---|
| 486 | |
---|
[825] | 487 | /** |
---|
[657] | 488 | * returns an array containing sub-directories which can be a category, |
---|
| 489 | * recursive by default |
---|
| 490 | * |
---|
| 491 | * directories nammed "thumbnail", "pwg_high" or "pwg_representative" are |
---|
| 492 | * omitted |
---|
| 493 | * |
---|
| 494 | * @param string $basedir |
---|
| 495 | * @return array |
---|
| 496 | */ |
---|
| 497 | function get_fs_directories($path, $recursive = true) |
---|
| 498 | { |
---|
| 499 | $dirs = array(); |
---|
[1060] | 500 | |
---|
[657] | 501 | if (is_dir($path)) |
---|
| 502 | { |
---|
| 503 | if ($contents = opendir($path)) |
---|
| 504 | { |
---|
| 505 | while (($node = readdir($contents)) !== false) |
---|
| 506 | { |
---|
[12642] | 507 | if ($node != '.' |
---|
[657] | 508 | and $node != '..' |
---|
[1006] | 509 | and $node != '.svn' |
---|
[657] | 510 | and $node != 'thumbnail' |
---|
| 511 | and $node != 'pwg_high' |
---|
[12642] | 512 | and $node != 'pwg_representative' |
---|
| 513 | and is_dir($path.'/'.$node)) |
---|
[657] | 514 | { |
---|
| 515 | array_push($dirs, $path.'/'.$node); |
---|
| 516 | if ($recursive) |
---|
| 517 | { |
---|
| 518 | $dirs = array_merge($dirs, get_fs_directories($path.'/'.$node)); |
---|
| 519 | } |
---|
| 520 | } |
---|
| 521 | } |
---|
[2478] | 522 | closedir($contents); |
---|
[657] | 523 | } |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | return $dirs; |
---|
| 527 | } |
---|
| 528 | |
---|
[606] | 529 | /** |
---|
[2306] | 530 | * order categories (update categories.rank and global_rank database fields) |
---|
| 531 | * so that rank field are consecutive integers starting at 1 for each child |
---|
[625] | 532 | * @return void |
---|
| 533 | */ |
---|
[2306] | 534 | function update_global_rank() |
---|
[625] | 535 | { |
---|
| 536 | $query = ' |
---|
[4385] | 537 | SELECT id, id_uppercat, uppercats, rank, global_rank |
---|
[625] | 538 | FROM '.CATEGORIES_TABLE.' |
---|
[2491] | 539 | ORDER BY id_uppercat,rank,name'; |
---|
[625] | 540 | |
---|
[2306] | 541 | $cat_map = array(); |
---|
[625] | 542 | |
---|
[2306] | 543 | $current_rank = 0; |
---|
| 544 | $current_uppercat = ''; |
---|
| 545 | |
---|
[625] | 546 | $result = pwg_query($query); |
---|
[4325] | 547 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[625] | 548 | { |
---|
[2306] | 549 | if ($row['id_uppercat'] != $current_uppercat) |
---|
| 550 | { |
---|
| 551 | $current_rank = 0; |
---|
| 552 | $current_uppercat = $row['id_uppercat']; |
---|
| 553 | } |
---|
| 554 | ++$current_rank; |
---|
| 555 | $cat = |
---|
| 556 | array( |
---|
| 557 | 'rank' => $current_rank, |
---|
| 558 | 'rank_changed' =>$current_rank!=$row['rank'], |
---|
| 559 | 'global_rank' => $row['global_rank'], |
---|
| 560 | 'uppercats' => $row['uppercats'], |
---|
| 561 | ); |
---|
| 562 | $cat_map[ $row['id'] ] = $cat; |
---|
[625] | 563 | } |
---|
[1060] | 564 | |
---|
[625] | 565 | $datas = array(); |
---|
[2306] | 566 | |
---|
| 567 | foreach( $cat_map as $id=>$cat ) |
---|
[625] | 568 | { |
---|
[2306] | 569 | $new_global_rank = preg_replace( |
---|
[1082] | 570 | '/(\d+)/e', |
---|
[2306] | 571 | "\$cat_map['$1']['rank']", |
---|
| 572 | str_replace(',', '.', $cat['uppercats'] ) |
---|
| 573 | ); |
---|
| 574 | if ( $cat['rank_changed'] |
---|
| 575 | or $new_global_rank!=$cat['global_rank'] |
---|
| 576 | ) |
---|
| 577 | { |
---|
| 578 | $datas[] = array( |
---|
| 579 | 'id' => $id, |
---|
| 580 | 'rank' => $cat['rank'], |
---|
| 581 | 'global_rank' => $new_global_rank, |
---|
| 582 | ); |
---|
| 583 | } |
---|
[625] | 584 | } |
---|
| 585 | |
---|
[1082] | 586 | mass_updates( |
---|
| 587 | CATEGORIES_TABLE, |
---|
| 588 | array( |
---|
| 589 | 'primary' => array('id'), |
---|
[2306] | 590 | 'update' => array('rank', 'global_rank') |
---|
[1082] | 591 | ), |
---|
| 592 | $datas |
---|
| 593 | ); |
---|
[2306] | 594 | return count($datas); |
---|
[625] | 595 | } |
---|
[632] | 596 | |
---|
| 597 | /** |
---|
| 598 | * change the visible property on a set of categories |
---|
| 599 | * |
---|
| 600 | * @param array categories |
---|
| 601 | * @param string value |
---|
| 602 | * @return void |
---|
| 603 | */ |
---|
| 604 | function set_cat_visible($categories, $value) |
---|
| 605 | { |
---|
| 606 | if (!in_array($value, array('true', 'false'))) |
---|
| 607 | { |
---|
[2491] | 608 | trigger_error("set_cat_visible invalid param $value", E_USER_WARNING); |
---|
[632] | 609 | return false; |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | // unlocking a category => all its parent categories become unlocked |
---|
| 613 | if ($value == 'true') |
---|
| 614 | { |
---|
[657] | 615 | $uppercats = get_uppercat_ids($categories); |
---|
[632] | 616 | $query = ' |
---|
| 617 | UPDATE '.CATEGORIES_TABLE.' |
---|
| 618 | SET visible = \'true\' |
---|
[2491] | 619 | WHERE id IN ('.implode(',', $uppercats).')'; |
---|
[632] | 620 | pwg_query($query); |
---|
| 621 | } |
---|
| 622 | // locking a category => all its child categories become locked |
---|
| 623 | if ($value == 'false') |
---|
| 624 | { |
---|
| 625 | $subcats = get_subcat_ids($categories); |
---|
| 626 | $query = ' |
---|
| 627 | UPDATE '.CATEGORIES_TABLE.' |
---|
| 628 | SET visible = \'false\' |
---|
[2491] | 629 | WHERE id IN ('.implode(',', $subcats).')'; |
---|
[632] | 630 | pwg_query($query); |
---|
| 631 | } |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | /** |
---|
| 635 | * change the status property on a set of categories : private or public |
---|
| 636 | * |
---|
| 637 | * @param array categories |
---|
| 638 | * @param string value |
---|
| 639 | * @return void |
---|
| 640 | */ |
---|
| 641 | function set_cat_status($categories, $value) |
---|
| 642 | { |
---|
| 643 | if (!in_array($value, array('public', 'private'))) |
---|
| 644 | { |
---|
[2491] | 645 | trigger_error("set_cat_status invalid param $value", E_USER_WARNING); |
---|
[632] | 646 | return false; |
---|
| 647 | } |
---|
| 648 | |
---|
| 649 | // make public a category => all its parent categories become public |
---|
| 650 | if ($value == 'public') |
---|
| 651 | { |
---|
[657] | 652 | $uppercats = get_uppercat_ids($categories); |
---|
[632] | 653 | $query = ' |
---|
| 654 | UPDATE '.CATEGORIES_TABLE.' |
---|
| 655 | SET status = \'public\' |
---|
| 656 | WHERE id IN ('.implode(',', $uppercats).') |
---|
| 657 | ;'; |
---|
| 658 | pwg_query($query); |
---|
| 659 | } |
---|
| 660 | // make a category private => all its child categories become private |
---|
| 661 | if ($value == 'private') |
---|
| 662 | { |
---|
| 663 | $subcats = get_subcat_ids($categories); |
---|
| 664 | $query = ' |
---|
| 665 | UPDATE '.CATEGORIES_TABLE.' |
---|
| 666 | SET status = \'private\' |
---|
[2491] | 667 | WHERE id IN ('.implode(',', $subcats).')'; |
---|
[632] | 668 | pwg_query($query); |
---|
| 669 | } |
---|
| 670 | } |
---|
[633] | 671 | |
---|
| 672 | /** |
---|
[657] | 673 | * returns all uppercats category ids of the given category ids |
---|
| 674 | * |
---|
| 675 | * @param array cat_ids |
---|
| 676 | * @return array |
---|
| 677 | */ |
---|
| 678 | function get_uppercat_ids($cat_ids) |
---|
| 679 | { |
---|
| 680 | if (!is_array($cat_ids) or count($cat_ids) < 1) |
---|
| 681 | { |
---|
| 682 | return array(); |
---|
| 683 | } |
---|
[1060] | 684 | |
---|
[657] | 685 | $uppercats = array(); |
---|
| 686 | |
---|
| 687 | $query = ' |
---|
| 688 | SELECT uppercats |
---|
| 689 | FROM '.CATEGORIES_TABLE.' |
---|
| 690 | WHERE id IN ('.implode(',', $cat_ids).') |
---|
| 691 | ;'; |
---|
| 692 | $result = pwg_query($query); |
---|
[4325] | 693 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[657] | 694 | { |
---|
| 695 | $uppercats = array_merge($uppercats, |
---|
| 696 | explode(',', $row['uppercats'])); |
---|
| 697 | } |
---|
| 698 | $uppercats = array_unique($uppercats); |
---|
| 699 | |
---|
| 700 | return $uppercats; |
---|
| 701 | } |
---|
| 702 | |
---|
| 703 | /** |
---|
[633] | 704 | * set a new random representant to the categories |
---|
| 705 | * |
---|
| 706 | * @param array categories |
---|
| 707 | */ |
---|
| 708 | function set_random_representant($categories) |
---|
| 709 | { |
---|
| 710 | $datas = array(); |
---|
| 711 | foreach ($categories as $category_id) |
---|
| 712 | { |
---|
| 713 | $query = ' |
---|
| 714 | SELECT image_id |
---|
| 715 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 716 | WHERE category_id = '.$category_id.' |
---|
[4331] | 717 | ORDER BY '.DB_RANDOM_FUNCTION.'() |
---|
[4334] | 718 | LIMIT 1 |
---|
[633] | 719 | ;'; |
---|
[4325] | 720 | list($representative) = pwg_db_fetch_row(pwg_query($query)); |
---|
[1428] | 721 | |
---|
| 722 | array_push( |
---|
| 723 | $datas, |
---|
| 724 | array( |
---|
| 725 | 'id' => $category_id, |
---|
| 726 | 'representative_picture_id' => $representative, |
---|
| 727 | ) |
---|
| 728 | ); |
---|
[633] | 729 | } |
---|
| 730 | |
---|
[1428] | 731 | mass_updates( |
---|
| 732 | CATEGORIES_TABLE, |
---|
| 733 | array( |
---|
| 734 | 'primary' => array('id'), |
---|
| 735 | 'update' => array('representative_picture_id') |
---|
| 736 | ), |
---|
| 737 | $datas |
---|
| 738 | ); |
---|
[633] | 739 | } |
---|
[638] | 740 | |
---|
| 741 | /** |
---|
[657] | 742 | * returns the fulldir for each given category id |
---|
| 743 | * |
---|
| 744 | * @param array cat_ids |
---|
| 745 | * @return array |
---|
| 746 | */ |
---|
| 747 | function get_fulldirs($cat_ids) |
---|
| 748 | { |
---|
| 749 | if (count($cat_ids) == 0) |
---|
| 750 | { |
---|
| 751 | return array(); |
---|
| 752 | } |
---|
[1060] | 753 | |
---|
[657] | 754 | // caching directories of existing categories |
---|
| 755 | $query = ' |
---|
| 756 | SELECT id, dir |
---|
| 757 | FROM '.CATEGORIES_TABLE.' |
---|
| 758 | WHERE dir IS NOT NULL |
---|
| 759 | ;'; |
---|
[2560] | 760 | $cat_dirs = simple_hash_from_query($query, 'id', 'dir'); |
---|
[657] | 761 | |
---|
[672] | 762 | // caching galleries_url |
---|
| 763 | $query = ' |
---|
| 764 | SELECT id, galleries_url |
---|
| 765 | FROM '.SITES_TABLE.' |
---|
| 766 | ;'; |
---|
[2560] | 767 | $galleries_url = simple_hash_from_query($query, 'id', 'galleries_url'); |
---|
[672] | 768 | |
---|
| 769 | // categories : id, site_id, uppercats |
---|
[657] | 770 | $query = ' |
---|
[672] | 771 | SELECT id, uppercats, site_id |
---|
[657] | 772 | FROM '.CATEGORIES_TABLE.' |
---|
[2560] | 773 | WHERE dir IS NOT NULL |
---|
| 774 | AND id IN ( |
---|
[657] | 775 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
| 776 | ;'; |
---|
[18628] | 777 | $categories = array_from_query($query); |
---|
[1060] | 778 | |
---|
[657] | 779 | // filling $cat_fulldirs |
---|
| 780 | $cat_fulldirs = array(); |
---|
[672] | 781 | foreach ($categories as $category) |
---|
[657] | 782 | { |
---|
[672] | 783 | $uppercats = str_replace(',', '/', $category['uppercats']); |
---|
| 784 | $cat_fulldirs[$category['id']] = $galleries_url[$category['site_id']]; |
---|
| 785 | $cat_fulldirs[$category['id']].= preg_replace('/(\d+)/e', |
---|
| 786 | "\$cat_dirs['$1']", |
---|
| 787 | $uppercats); |
---|
[657] | 788 | } |
---|
| 789 | |
---|
| 790 | return $cat_fulldirs; |
---|
| 791 | } |
---|
| 792 | |
---|
| 793 | /** |
---|
| 794 | * returns an array with all file system files according to |
---|
| 795 | * $conf['file_ext'] |
---|
| 796 | * |
---|
| 797 | * @param string $path |
---|
| 798 | * @param bool recursive |
---|
| 799 | * @return array |
---|
| 800 | */ |
---|
| 801 | function get_fs($path, $recursive = true) |
---|
| 802 | { |
---|
| 803 | global $conf; |
---|
| 804 | |
---|
| 805 | // because isset is faster than in_array... |
---|
| 806 | if (!isset($conf['flip_picture_ext'])) |
---|
| 807 | { |
---|
| 808 | $conf['flip_picture_ext'] = array_flip($conf['picture_ext']); |
---|
| 809 | } |
---|
| 810 | if (!isset($conf['flip_file_ext'])) |
---|
| 811 | { |
---|
| 812 | $conf['flip_file_ext'] = array_flip($conf['file_ext']); |
---|
| 813 | } |
---|
| 814 | |
---|
| 815 | $fs['elements'] = array(); |
---|
| 816 | $fs['thumbnails'] = array(); |
---|
| 817 | $fs['representatives'] = array(); |
---|
| 818 | $subdirs = array(); |
---|
| 819 | |
---|
| 820 | if (is_dir($path)) |
---|
| 821 | { |
---|
| 822 | if ($contents = opendir($path)) |
---|
| 823 | { |
---|
| 824 | while (($node = readdir($contents)) !== false) |
---|
| 825 | { |
---|
[12640] | 826 | if ($node == '.' or $node == '..') continue; |
---|
| 827 | |
---|
[657] | 828 | if (is_file($path.'/'.$node)) |
---|
| 829 | { |
---|
| 830 | $extension = get_extension($node); |
---|
[1060] | 831 | |
---|
[657] | 832 | // if (in_array($extension, $conf['picture_ext'])) |
---|
| 833 | if (isset($conf['flip_picture_ext'][$extension])) |
---|
| 834 | { |
---|
| 835 | if (basename($path) == 'thumbnail') |
---|
| 836 | { |
---|
| 837 | array_push($fs['thumbnails'], $path.'/'.$node); |
---|
| 838 | } |
---|
| 839 | else if (basename($path) == 'pwg_representative') |
---|
| 840 | { |
---|
| 841 | array_push($fs['representatives'], $path.'/'.$node); |
---|
| 842 | } |
---|
| 843 | else |
---|
| 844 | { |
---|
| 845 | array_push($fs['elements'], $path.'/'.$node); |
---|
| 846 | } |
---|
| 847 | } |
---|
| 848 | // else if (in_array($extension, $conf['file_ext'])) |
---|
| 849 | else if (isset($conf['flip_file_ext'][$extension])) |
---|
| 850 | { |
---|
| 851 | array_push($fs['elements'], $path.'/'.$node); |
---|
| 852 | } |
---|
| 853 | } |
---|
[12640] | 854 | else if (is_dir($path.'/'.$node) and $node != 'pwg_high' and $recursive) |
---|
[657] | 855 | { |
---|
| 856 | array_push($subdirs, $node); |
---|
| 857 | } |
---|
| 858 | } |
---|
| 859 | } |
---|
| 860 | closedir($contents); |
---|
| 861 | |
---|
| 862 | foreach ($subdirs as $subdir) |
---|
| 863 | { |
---|
| 864 | $tmp_fs = get_fs($path.'/'.$subdir); |
---|
| 865 | |
---|
| 866 | $fs['elements'] = array_merge($fs['elements'], |
---|
| 867 | $tmp_fs['elements']); |
---|
[1060] | 868 | |
---|
[657] | 869 | $fs['thumbnails'] = array_merge($fs['thumbnails'], |
---|
| 870 | $tmp_fs['thumbnails']); |
---|
[1060] | 871 | |
---|
[657] | 872 | $fs['representatives'] = array_merge($fs['representatives'], |
---|
| 873 | $tmp_fs['representatives']); |
---|
| 874 | } |
---|
| 875 | } |
---|
| 876 | return $fs; |
---|
| 877 | } |
---|
[672] | 878 | |
---|
| 879 | /** |
---|
[809] | 880 | * synchronize base users list and related users list |
---|
| 881 | * |
---|
| 882 | * compares and synchronizes base users table (USERS_TABLE) with its child |
---|
| 883 | * tables (USER_INFOS_TABLE, USER_ACCESS, USER_CACHE, USER_GROUP) : each |
---|
| 884 | * base user must be present in child tables, users in child tables not |
---|
| 885 | * present in base table must be deleted. |
---|
| 886 | * |
---|
| 887 | * @return void |
---|
[808] | 888 | */ |
---|
| 889 | function sync_users() |
---|
| 890 | { |
---|
| 891 | global $conf; |
---|
[1060] | 892 | |
---|
[808] | 893 | $query = ' |
---|
| 894 | SELECT '.$conf['user_fields']['id'].' AS id |
---|
| 895 | FROM '.USERS_TABLE.' |
---|
| 896 | ;'; |
---|
| 897 | $base_users = array_from_query($query, 'id'); |
---|
| 898 | |
---|
| 899 | $query = ' |
---|
| 900 | SELECT user_id |
---|
| 901 | FROM '.USER_INFOS_TABLE.' |
---|
| 902 | ;'; |
---|
| 903 | $infos_users = array_from_query($query, 'user_id'); |
---|
| 904 | |
---|
| 905 | // users present in $base_users and not in $infos_users must be added |
---|
| 906 | $to_create = array_diff($base_users, $infos_users); |
---|
| 907 | |
---|
| 908 | if (count($to_create) > 0) |
---|
| 909 | { |
---|
[1926] | 910 | create_user_infos($to_create); |
---|
[808] | 911 | } |
---|
| 912 | |
---|
[809] | 913 | // users present in user related tables must be present in the base user |
---|
| 914 | // table |
---|
[1082] | 915 | $tables = array( |
---|
| 916 | USER_MAIL_NOTIFICATION_TABLE, |
---|
| 917 | USER_FEED_TABLE, |
---|
| 918 | USER_INFOS_TABLE, |
---|
| 919 | USER_ACCESS_TABLE, |
---|
| 920 | USER_CACHE_TABLE, |
---|
[1624] | 921 | USER_CACHE_CATEGORIES_TABLE, |
---|
[1082] | 922 | USER_GROUP_TABLE |
---|
| 923 | ); |
---|
[1592] | 924 | |
---|
[809] | 925 | foreach ($tables as $table) |
---|
[808] | 926 | { |
---|
| 927 | $query = ' |
---|
[2095] | 928 | SELECT DISTINCT user_id |
---|
[809] | 929 | FROM '.$table.' |
---|
| 930 | ;'; |
---|
[1082] | 931 | $to_delete = array_diff( |
---|
| 932 | array_from_query($query, 'user_id'), |
---|
| 933 | $base_users |
---|
| 934 | ); |
---|
[1060] | 935 | |
---|
[809] | 936 | if (count($to_delete) > 0) |
---|
| 937 | { |
---|
| 938 | $query = ' |
---|
[808] | 939 | DELETE |
---|
[809] | 940 | FROM '.$table.' |
---|
[808] | 941 | WHERE user_id in ('.implode(',', $to_delete).') |
---|
| 942 | ;'; |
---|
[809] | 943 | pwg_query($query); |
---|
| 944 | } |
---|
| 945 | } |
---|
| 946 | } |
---|
| 947 | |
---|
| 948 | /** |
---|
| 949 | * updates categories.uppercats field based on categories.id + |
---|
| 950 | * categories.id_uppercat |
---|
| 951 | * |
---|
| 952 | * @return void |
---|
| 953 | */ |
---|
| 954 | function update_uppercats() |
---|
| 955 | { |
---|
| 956 | $query = ' |
---|
[2304] | 957 | SELECT id, id_uppercat, uppercats |
---|
[809] | 958 | FROM '.CATEGORIES_TABLE.' |
---|
| 959 | ;'; |
---|
[2304] | 960 | $cat_map = hash_from_query($query, 'id'); |
---|
[1060] | 961 | |
---|
[2304] | 962 | $datas = array(); |
---|
| 963 | foreach ($cat_map as $id => $cat) |
---|
[809] | 964 | { |
---|
[2304] | 965 | $upper_list = array(); |
---|
[809] | 966 | |
---|
| 967 | $uppercat = $id; |
---|
[2304] | 968 | while ($uppercat) |
---|
| 969 | { |
---|
| 970 | array_push($upper_list, $uppercat); |
---|
| 971 | $uppercat = $cat_map[$uppercat]['id_uppercat']; |
---|
| 972 | } |
---|
[809] | 973 | |
---|
[2304] | 974 | $new_uppercats = implode(',', array_reverse($upper_list)); |
---|
| 975 | if ($new_uppercats != $cat['uppercats']) |
---|
[809] | 976 | { |
---|
[2304] | 977 | array_push( |
---|
| 978 | $datas, |
---|
| 979 | array( |
---|
| 980 | 'id' => $id, |
---|
| 981 | 'uppercats' => $new_uppercats |
---|
| 982 | ) |
---|
| 983 | ); |
---|
[809] | 984 | } |
---|
| 985 | } |
---|
| 986 | $fields = array('primary' => array('id'), 'update' => array('uppercats')); |
---|
| 987 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
| 988 | } |
---|
| 989 | |
---|
| 990 | /** |
---|
| 991 | * update images.path field |
---|
| 992 | * |
---|
| 993 | * @return void |
---|
| 994 | */ |
---|
| 995 | function update_path() |
---|
| 996 | { |
---|
| 997 | $query = ' |
---|
[1121] | 998 | SELECT DISTINCT(storage_category_id) |
---|
| 999 | FROM '.IMAGES_TABLE.' |
---|
[2575] | 1000 | WHERE storage_category_id IS NOT NULL |
---|
[809] | 1001 | ;'; |
---|
[1121] | 1002 | $cat_ids = array_from_query($query, 'storage_category_id'); |
---|
| 1003 | $fulldirs = get_fulldirs($cat_ids); |
---|
[1060] | 1004 | |
---|
[1121] | 1005 | foreach ($cat_ids as $cat_id) |
---|
[809] | 1006 | { |
---|
| 1007 | $query = ' |
---|
| 1008 | UPDATE '.IMAGES_TABLE.' |
---|
[5691] | 1009 | SET path = '.pwg_db_concat(array("'".$fulldirs[$cat_id]."/'",'file')).' |
---|
[1121] | 1010 | WHERE storage_category_id = '.$cat_id.' |
---|
[809] | 1011 | ;'; |
---|
[808] | 1012 | pwg_query($query); |
---|
[809] | 1013 | } |
---|
[808] | 1014 | } |
---|
[809] | 1015 | |
---|
| 1016 | /** |
---|
[881] | 1017 | * change the parent category of the given categories. The categories are |
---|
[809] | 1018 | * supposed virtual. |
---|
| 1019 | * |
---|
[881] | 1020 | * @param array category identifiers |
---|
[809] | 1021 | * @param int parent category identifier |
---|
| 1022 | * @return void |
---|
| 1023 | */ |
---|
[881] | 1024 | function move_categories($category_ids, $new_parent = -1) |
---|
[809] | 1025 | { |
---|
[881] | 1026 | global $page; |
---|
| 1027 | |
---|
| 1028 | if (count($category_ids) == 0) |
---|
| 1029 | { |
---|
| 1030 | return; |
---|
| 1031 | } |
---|
| 1032 | |
---|
| 1033 | $new_parent = $new_parent < 1 ? 'NULL' : $new_parent; |
---|
| 1034 | |
---|
| 1035 | $categories = array(); |
---|
[1060] | 1036 | |
---|
[809] | 1037 | $query = ' |
---|
[881] | 1038 | SELECT id, id_uppercat, status, uppercats |
---|
[809] | 1039 | FROM '.CATEGORIES_TABLE.' |
---|
[881] | 1040 | WHERE id IN ('.implode(',', $category_ids).') |
---|
[809] | 1041 | ;'; |
---|
[881] | 1042 | $result = pwg_query($query); |
---|
[4325] | 1043 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[809] | 1044 | { |
---|
[881] | 1045 | $categories[$row['id']] = |
---|
| 1046 | array( |
---|
| 1047 | 'parent' => empty($row['id_uppercat']) ? 'NULL' : $row['id_uppercat'], |
---|
| 1048 | 'status' => $row['status'], |
---|
| 1049 | 'uppercats' => $row['uppercats'] |
---|
| 1050 | ); |
---|
[809] | 1051 | } |
---|
[1060] | 1052 | |
---|
[881] | 1053 | // is the movement possible? The movement is impossible if you try to move |
---|
| 1054 | // a category in a sub-category or itself |
---|
| 1055 | if ('NULL' != $new_parent) |
---|
| 1056 | { |
---|
| 1057 | $query = ' |
---|
| 1058 | SELECT uppercats |
---|
| 1059 | FROM '.CATEGORIES_TABLE.' |
---|
| 1060 | WHERE id = '.$new_parent.' |
---|
| 1061 | ;'; |
---|
[4325] | 1062 | list($new_parent_uppercats) = pwg_db_fetch_row(pwg_query($query)); |
---|
[881] | 1063 | |
---|
| 1064 | foreach ($categories as $category) |
---|
| 1065 | { |
---|
| 1066 | // technically, you can't move a category with uppercats 12,125,13,14 |
---|
| 1067 | // into a new parent category with uppercats 12,125,13,14,24 |
---|
[8967] | 1068 | if (preg_match('/^'.$category['uppercats'].'(,|$)/', $new_parent_uppercats)) |
---|
[881] | 1069 | { |
---|
| 1070 | array_push( |
---|
| 1071 | $page['errors'], |
---|
[6993] | 1072 | l10n('You cannot move an album in its own sub album') |
---|
[881] | 1073 | ); |
---|
| 1074 | return; |
---|
| 1075 | } |
---|
| 1076 | } |
---|
| 1077 | } |
---|
[1060] | 1078 | |
---|
[881] | 1079 | $tables = |
---|
| 1080 | array( |
---|
| 1081 | USER_ACCESS_TABLE => 'user_id', |
---|
| 1082 | GROUP_ACCESS_TABLE => 'group_id' |
---|
| 1083 | ); |
---|
[1060] | 1084 | |
---|
[809] | 1085 | $query = ' |
---|
| 1086 | UPDATE '.CATEGORIES_TABLE.' |
---|
| 1087 | SET id_uppercat = '.$new_parent.' |
---|
[881] | 1088 | WHERE id IN ('.implode(',', $category_ids).') |
---|
[809] | 1089 | ;'; |
---|
| 1090 | pwg_query($query); |
---|
| 1091 | |
---|
| 1092 | update_uppercats(); |
---|
| 1093 | update_global_rank(); |
---|
| 1094 | |
---|
| 1095 | // status and related permissions management |
---|
| 1096 | if ('NULL' == $new_parent) |
---|
| 1097 | { |
---|
| 1098 | $parent_status = 'public'; |
---|
| 1099 | } |
---|
| 1100 | else |
---|
| 1101 | { |
---|
| 1102 | $query = ' |
---|
| 1103 | SELECT status |
---|
| 1104 | FROM '.CATEGORIES_TABLE.' |
---|
| 1105 | WHERE id = '.$new_parent.' |
---|
| 1106 | ;'; |
---|
[4325] | 1107 | list($parent_status) = pwg_db_fetch_row(pwg_query($query)); |
---|
[809] | 1108 | } |
---|
| 1109 | |
---|
| 1110 | if ('private' == $parent_status) |
---|
| 1111 | { |
---|
[881] | 1112 | foreach ($categories as $cat_id => $category) |
---|
[809] | 1113 | { |
---|
[881] | 1114 | switch ($category['status']) |
---|
[809] | 1115 | { |
---|
[881] | 1116 | case 'public' : |
---|
| 1117 | { |
---|
| 1118 | set_cat_status(array($cat_id), 'private'); |
---|
| 1119 | break; |
---|
| 1120 | } |
---|
| 1121 | case 'private' : |
---|
| 1122 | { |
---|
| 1123 | $subcats = get_subcat_ids(array($cat_id)); |
---|
[1060] | 1124 | |
---|
[881] | 1125 | foreach ($tables as $table => $field) |
---|
| 1126 | { |
---|
| 1127 | $query = ' |
---|
[809] | 1128 | SELECT '.$field.' |
---|
| 1129 | FROM '.$table.' |
---|
[881] | 1130 | WHERE cat_id = '.$cat_id.' |
---|
[809] | 1131 | ;'; |
---|
[881] | 1132 | $category_access = array_from_query($query, $field); |
---|
[809] | 1133 | |
---|
[881] | 1134 | $query = ' |
---|
[809] | 1135 | SELECT '.$field.' |
---|
| 1136 | FROM '.$table.' |
---|
[881] | 1137 | WHERE cat_id = '.$new_parent.' |
---|
[809] | 1138 | ;'; |
---|
[881] | 1139 | $parent_access = array_from_query($query, $field); |
---|
[1060] | 1140 | |
---|
[881] | 1141 | $to_delete = array_diff($parent_access, $category_access); |
---|
[1060] | 1142 | |
---|
[881] | 1143 | if (count($to_delete) > 0) |
---|
| 1144 | { |
---|
| 1145 | $query = ' |
---|
[809] | 1146 | DELETE FROM '.$table.' |
---|
| 1147 | WHERE '.$field.' IN ('.implode(',', $to_delete).') |
---|
[881] | 1148 | AND cat_id IN ('.implode(',', $subcats).') |
---|
[809] | 1149 | ;'; |
---|
[881] | 1150 | pwg_query($query); |
---|
| 1151 | } |
---|
[809] | 1152 | } |
---|
[881] | 1153 | break; |
---|
[809] | 1154 | } |
---|
| 1155 | } |
---|
| 1156 | } |
---|
| 1157 | } |
---|
[881] | 1158 | |
---|
| 1159 | array_push( |
---|
| 1160 | $page['infos'], |
---|
[1932] | 1161 | l10n_dec( |
---|
[6951] | 1162 | '%d album moved', '%d albums moved', |
---|
[881] | 1163 | count($categories) |
---|
| 1164 | ) |
---|
| 1165 | ); |
---|
[809] | 1166 | } |
---|
[1064] | 1167 | |
---|
| 1168 | /** |
---|
| 1169 | * create a virtual category |
---|
| 1170 | * |
---|
| 1171 | * @param string category name |
---|
| 1172 | * @param int parent category id |
---|
| 1173 | * @return array with ('info' and 'id') or ('error') key |
---|
| 1174 | */ |
---|
[17669] | 1175 | function create_virtual_category($category_name, $parent_id=null, $options=array()) |
---|
[1064] | 1176 | { |
---|
[11728] | 1177 | global $conf, $user; |
---|
[1068] | 1178 | |
---|
[1064] | 1179 | // is the given category name only containing blank spaces ? |
---|
| 1180 | if (preg_match('/^\s*$/', $category_name)) |
---|
| 1181 | { |
---|
[6988] | 1182 | return array('error' => l10n('The name of an album must not be empty')); |
---|
[1064] | 1183 | } |
---|
[17765] | 1184 | |
---|
[1064] | 1185 | $insert = array( |
---|
| 1186 | 'name' => $category_name, |
---|
[12763] | 1187 | 'rank' => 0, |
---|
[17669] | 1188 | 'global_rank' => 0, |
---|
[1064] | 1189 | ); |
---|
[1068] | 1190 | |
---|
[17669] | 1191 | // is the album commentable? |
---|
| 1192 | if (isset($options['commentable']) and is_bool($options['commentable'])) |
---|
[1064] | 1193 | { |
---|
[17669] | 1194 | $insert['commentable'] = $options['commentable']; |
---|
| 1195 | } |
---|
| 1196 | else |
---|
| 1197 | { |
---|
| 1198 | $insert['commentable'] = $conf['newcat_default_commentable']; |
---|
| 1199 | } |
---|
| 1200 | $insert['commentable'] = boolean_to_string($insert['commentable']); |
---|
| 1201 | |
---|
| 1202 | // is the album temporarily locked? (only visible by administrators, |
---|
| 1203 | // whatever permissions) (may be overwritten if parent album is not |
---|
| 1204 | // visible) |
---|
| 1205 | if (isset($options['visible']) and is_bool($options['visible'])) |
---|
| 1206 | { |
---|
| 1207 | $insert['visible'] = $options['visible']; |
---|
| 1208 | } |
---|
| 1209 | else |
---|
| 1210 | { |
---|
| 1211 | $insert['visible'] = $conf['newcat_default_visible']; |
---|
| 1212 | } |
---|
| 1213 | $insert['visible'] = boolean_to_string($insert['visible']); |
---|
| 1214 | |
---|
| 1215 | // is the album private? (may be overwritten if parent album is private) |
---|
| 1216 | if (isset($options['status']) and 'private' == $options['status']) |
---|
| 1217 | { |
---|
| 1218 | $insert['status'] = 'private'; |
---|
| 1219 | } |
---|
| 1220 | else |
---|
| 1221 | { |
---|
| 1222 | $insert['status'] = $conf['newcat_default_status']; |
---|
| 1223 | } |
---|
| 1224 | |
---|
| 1225 | // any description for this album? |
---|
| 1226 | if (isset($options['comment'])) |
---|
| 1227 | { |
---|
| 1228 | $insert['comment'] = strip_tags($options['comment']); |
---|
| 1229 | } |
---|
| 1230 | |
---|
| 1231 | if (!empty($parent_id) and is_numeric($parent_id)) |
---|
| 1232 | { |
---|
[1064] | 1233 | $query = ' |
---|
| 1234 | SELECT id, uppercats, global_rank, visible, status |
---|
| 1235 | FROM '.CATEGORIES_TABLE.' |
---|
| 1236 | WHERE id = '.$parent_id.' |
---|
| 1237 | ;'; |
---|
[4325] | 1238 | $parent = pwg_db_fetch_assoc(pwg_query($query)); |
---|
[1064] | 1239 | |
---|
[4265] | 1240 | $insert['id_uppercat'] = $parent['id']; |
---|
| 1241 | $insert['global_rank'] = $parent['global_rank'].'.'.$insert['rank']; |
---|
[1068] | 1242 | |
---|
[1064] | 1243 | // at creation, must a category be visible or not ? Warning : if the |
---|
| 1244 | // parent category is invisible, the category is automatically create |
---|
| 1245 | // invisible. (invisible = locked) |
---|
| 1246 | if ('false' == $parent['visible']) |
---|
| 1247 | { |
---|
[4265] | 1248 | $insert['visible'] = 'false'; |
---|
[1064] | 1249 | } |
---|
[1068] | 1250 | |
---|
[1064] | 1251 | // at creation, must a category be public or private ? Warning : if the |
---|
| 1252 | // parent category is private, the category is automatically create |
---|
| 1253 | // private. |
---|
| 1254 | if ('private' == $parent['status']) |
---|
| 1255 | { |
---|
[4265] | 1256 | $insert['status'] = 'private'; |
---|
[1064] | 1257 | } |
---|
[17669] | 1258 | |
---|
| 1259 | $uppercats_prefix = $parent['uppercats'].','; |
---|
[1064] | 1260 | } |
---|
| 1261 | else |
---|
| 1262 | { |
---|
[17669] | 1263 | $uppercats_prefix = ''; |
---|
[1064] | 1264 | } |
---|
| 1265 | |
---|
| 1266 | // we have then to add the virtual category |
---|
[17669] | 1267 | single_insert(CATEGORIES_TABLE, $insert); |
---|
| 1268 | $inserted_id = pwg_db_insert_id(CATEGORIES_TABLE); |
---|
| 1269 | |
---|
| 1270 | single_update( |
---|
[1064] | 1271 | CATEGORIES_TABLE, |
---|
[17669] | 1272 | array('uppercats' => $uppercats_prefix.$inserted_id), |
---|
| 1273 | array('id' => $inserted_id) |
---|
[1064] | 1274 | ); |
---|
| 1275 | |
---|
[12763] | 1276 | update_global_rank(); |
---|
| 1277 | |
---|
[11728] | 1278 | if ('private' == $insert['status']) |
---|
| 1279 | { |
---|
| 1280 | add_permission_on_category($inserted_id, array_unique(array_merge(get_admins(), array($user['id'])))); |
---|
| 1281 | } |
---|
| 1282 | |
---|
[1064] | 1283 | return array( |
---|
[6969] | 1284 | 'info' => l10n('Virtual album added'), |
---|
[1064] | 1285 | 'id' => $inserted_id, |
---|
| 1286 | ); |
---|
| 1287 | } |
---|
[1111] | 1288 | |
---|
| 1289 | /** |
---|
[1119] | 1290 | * Set tags to an image. Warning: given tags are all tags associated to the |
---|
| 1291 | * image, not additionnal tags. |
---|
| 1292 | * |
---|
| 1293 | * @param array tag ids |
---|
| 1294 | * @param int image id |
---|
| 1295 | * @return void |
---|
| 1296 | */ |
---|
| 1297 | function set_tags($tags, $image_id) |
---|
| 1298 | { |
---|
[17724] | 1299 | set_tags_of( array($image_id=>$tags) ); |
---|
[1119] | 1300 | } |
---|
| 1301 | |
---|
| 1302 | /** |
---|
| 1303 | * Add new tags to a set of images. |
---|
| 1304 | * |
---|
| 1305 | * @param array tag ids |
---|
| 1306 | * @param array image ids |
---|
| 1307 | * @return void |
---|
| 1308 | */ |
---|
| 1309 | function add_tags($tags, $images) |
---|
| 1310 | { |
---|
[5691] | 1311 | if (count($tags) == 0 or count($images) == 0) |
---|
[1119] | 1312 | { |
---|
| 1313 | return; |
---|
| 1314 | } |
---|
[1592] | 1315 | |
---|
[1119] | 1316 | // we can't insert twice the same {image_id,tag_id} so we must first |
---|
| 1317 | // delete lines we'll insert later |
---|
| 1318 | $query = ' |
---|
| 1319 | DELETE |
---|
| 1320 | FROM '.IMAGE_TAG_TABLE.' |
---|
| 1321 | WHERE image_id IN ('.implode(',', $images).') |
---|
| 1322 | AND tag_id IN ('.implode(',', $tags).') |
---|
| 1323 | ;'; |
---|
| 1324 | pwg_query($query); |
---|
| 1325 | |
---|
| 1326 | $inserts = array(); |
---|
| 1327 | foreach ($images as $image_id) |
---|
| 1328 | { |
---|
[17724] | 1329 | foreach ( array_unique($tags) as $tag_id) |
---|
[1119] | 1330 | { |
---|
[17724] | 1331 | $inserts[] = array( |
---|
[1119] | 1332 | 'image_id' => $image_id, |
---|
| 1333 | 'tag_id' => $tag_id, |
---|
| 1334 | ); |
---|
| 1335 | } |
---|
| 1336 | } |
---|
| 1337 | mass_inserts( |
---|
| 1338 | IMAGE_TAG_TABLE, |
---|
| 1339 | array_keys($inserts[0]), |
---|
| 1340 | $inserts |
---|
| 1341 | ); |
---|
| 1342 | } |
---|
| 1343 | |
---|
[12032] | 1344 | /** |
---|
| 1345 | * |
---|
| 1346 | */ |
---|
| 1347 | function delete_tags($tag_ids) |
---|
| 1348 | { |
---|
| 1349 | if (is_numeric($tag_ids)) |
---|
| 1350 | { |
---|
| 1351 | $tag_ids = array($tag_ids); |
---|
| 1352 | } |
---|
| 1353 | |
---|
| 1354 | if (!is_array($tag_ids)) |
---|
| 1355 | { |
---|
| 1356 | return false; |
---|
| 1357 | } |
---|
[12917] | 1358 | |
---|
[12032] | 1359 | $query = ' |
---|
| 1360 | DELETE |
---|
| 1361 | FROM '.IMAGE_TAG_TABLE.' |
---|
| 1362 | WHERE tag_id IN ('.implode(',', $tag_ids).') |
---|
| 1363 | ;'; |
---|
| 1364 | pwg_query($query); |
---|
[12917] | 1365 | |
---|
[12032] | 1366 | $query = ' |
---|
| 1367 | DELETE |
---|
| 1368 | FROM '.TAGS_TABLE.' |
---|
| 1369 | WHERE id IN ('.implode(',', $tag_ids).') |
---|
| 1370 | ;'; |
---|
| 1371 | pwg_query($query); |
---|
| 1372 | } |
---|
| 1373 | |
---|
[1119] | 1374 | function tag_id_from_tag_name($tag_name) |
---|
| 1375 | { |
---|
| 1376 | global $page; |
---|
[2478] | 1377 | |
---|
[2350] | 1378 | $tag_name = trim($tag_name); |
---|
[1119] | 1379 | if (isset($page['tag_id_from_tag_name_cache'][$tag_name])) |
---|
| 1380 | { |
---|
| 1381 | return $page['tag_id_from_tag_name_cache'][$tag_name]; |
---|
| 1382 | } |
---|
[1592] | 1383 | |
---|
[17724] | 1384 | // search existing by exact name |
---|
[1119] | 1385 | $query = ' |
---|
| 1386 | SELECT id |
---|
| 1387 | FROM '.TAGS_TABLE.' |
---|
| 1388 | WHERE name = \''.$tag_name.'\' |
---|
| 1389 | ;'; |
---|
[17724] | 1390 | if (count($existing_tags = array_from_query($query, 'id')) == 0) |
---|
[1119] | 1391 | { |
---|
[17724] | 1392 | // search existing by case insensitive name |
---|
| 1393 | $query = ' |
---|
| 1394 | SELECT id |
---|
| 1395 | FROM '.TAGS_TABLE.' |
---|
| 1396 | WHERE CONVERT(name, CHAR) = \''.$tag_name.'\' |
---|
| 1397 | ;'; |
---|
| 1398 | if (count($existing_tags = array_from_query($query, 'id')) == 0) |
---|
| 1399 | { |
---|
| 1400 | $url_name = trigger_event('render_tag_url', $tag_name); |
---|
| 1401 | // search existing by url name |
---|
| 1402 | $query = ' |
---|
| 1403 | SELECT id |
---|
| 1404 | FROM '.TAGS_TABLE.' |
---|
| 1405 | WHERE url_name = \''.$url_name.'\' |
---|
| 1406 | ;'; |
---|
| 1407 | if (count($existing_tags = array_from_query($query, 'id')) == 0) |
---|
| 1408 | { |
---|
| 1409 | mass_inserts( |
---|
| 1410 | TAGS_TABLE, |
---|
| 1411 | array('name', 'url_name'), |
---|
| 1412 | array( |
---|
| 1413 | array( |
---|
| 1414 | 'name' => $tag_name, |
---|
| 1415 | 'url_name' => $url_name, |
---|
| 1416 | ) |
---|
| 1417 | ) |
---|
| 1418 | ); |
---|
| 1419 | $page['tag_id_from_tag_name_cache'][$tag_name] = pwg_db_insert_id(TAGS_TABLE); |
---|
| 1420 | return $page['tag_id_from_tag_name_cache'][$tag_name]; |
---|
| 1421 | } |
---|
| 1422 | } |
---|
[1119] | 1423 | } |
---|
| 1424 | |
---|
[17724] | 1425 | $page['tag_id_from_tag_name_cache'][$tag_name] = $existing_tags[0]; |
---|
[1119] | 1426 | return $page['tag_id_from_tag_name_cache'][$tag_name]; |
---|
| 1427 | } |
---|
| 1428 | |
---|
| 1429 | function set_tags_of($tags_of) |
---|
| 1430 | { |
---|
| 1431 | if (count($tags_of) > 0) |
---|
| 1432 | { |
---|
| 1433 | $query = ' |
---|
| 1434 | DELETE |
---|
| 1435 | FROM '.IMAGE_TAG_TABLE.' |
---|
| 1436 | WHERE image_id IN ('.implode(',', array_keys($tags_of)).') |
---|
| 1437 | ;'; |
---|
| 1438 | pwg_query($query); |
---|
| 1439 | |
---|
| 1440 | $inserts = array(); |
---|
[1592] | 1441 | |
---|
[1119] | 1442 | foreach ($tags_of as $image_id => $tag_ids) |
---|
| 1443 | { |
---|
[17724] | 1444 | foreach (array_unique($tag_ids) as $tag_id) |
---|
[1119] | 1445 | { |
---|
[17724] | 1446 | $inserts[] = array( |
---|
[1119] | 1447 | 'image_id' => $image_id, |
---|
| 1448 | 'tag_id' => $tag_id, |
---|
| 1449 | ); |
---|
| 1450 | } |
---|
| 1451 | } |
---|
| 1452 | |
---|
[17765] | 1453 | if (count($inserts)) |
---|
| 1454 | { |
---|
| 1455 | mass_inserts( |
---|
| 1456 | IMAGE_TAG_TABLE, |
---|
| 1457 | array_keys($inserts[0]), |
---|
| 1458 | $inserts |
---|
| 1459 | ); |
---|
| 1460 | } |
---|
[1119] | 1461 | } |
---|
| 1462 | } |
---|
| 1463 | |
---|
| 1464 | /** |
---|
[1121] | 1465 | * Associate a list of images to a list of categories. |
---|
| 1466 | * |
---|
[17424] | 1467 | * The function will not duplicate links and will preserve ranks |
---|
[1121] | 1468 | * |
---|
| 1469 | * @param array images |
---|
| 1470 | * @param array categories |
---|
| 1471 | * @return void |
---|
| 1472 | */ |
---|
| 1473 | function associate_images_to_categories($images, $categories) |
---|
| 1474 | { |
---|
| 1475 | if (count($images) == 0 |
---|
| 1476 | or count($categories) == 0) |
---|
| 1477 | { |
---|
| 1478 | return false; |
---|
| 1479 | } |
---|
[17765] | 1480 | |
---|
[17424] | 1481 | // get existing associations |
---|
[1121] | 1482 | $query = ' |
---|
[17424] | 1483 | SELECT |
---|
| 1484 | image_id, |
---|
| 1485 | category_id |
---|
[1121] | 1486 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 1487 | WHERE image_id IN ('.implode(',', $images).') |
---|
| 1488 | AND category_id IN ('.implode(',', $categories).') |
---|
| 1489 | ;'; |
---|
[17424] | 1490 | $result = pwg_query($query); |
---|
[17765] | 1491 | |
---|
[17424] | 1492 | $existing = array(); |
---|
| 1493 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 1494 | { |
---|
| 1495 | $existing[ $row['category_id'] ][] = $row['image_id']; |
---|
| 1496 | } |
---|
[1121] | 1497 | |
---|
[17424] | 1498 | // get max rank of each categories |
---|
[17002] | 1499 | $query = ' |
---|
| 1500 | SELECT |
---|
| 1501 | category_id, |
---|
| 1502 | MAX(rank) AS max_rank |
---|
| 1503 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 1504 | WHERE rank IS NOT NULL |
---|
| 1505 | AND category_id IN ('.implode(',', $categories).') |
---|
| 1506 | GROUP BY category_id |
---|
| 1507 | ;'; |
---|
| 1508 | |
---|
| 1509 | $current_rank_of = simple_hash_from_query( |
---|
| 1510 | $query, |
---|
| 1511 | 'category_id', |
---|
| 1512 | 'max_rank' |
---|
| 1513 | ); |
---|
| 1514 | |
---|
[17424] | 1515 | // associate only not already associated images |
---|
[1121] | 1516 | $inserts = array(); |
---|
| 1517 | foreach ($categories as $category_id) |
---|
| 1518 | { |
---|
[17002] | 1519 | if (!isset($current_rank_of[$category_id])) |
---|
| 1520 | { |
---|
| 1521 | $current_rank_of[$category_id] = 0; |
---|
| 1522 | } |
---|
[17424] | 1523 | if (!isset($existing[$category_id])) |
---|
| 1524 | { |
---|
| 1525 | $existing[$category_id] = array(); |
---|
| 1526 | } |
---|
[17002] | 1527 | |
---|
[1121] | 1528 | foreach ($images as $image_id) |
---|
| 1529 | { |
---|
[17424] | 1530 | if (!in_array($image_id, $existing[$category_id])) |
---|
| 1531 | { |
---|
| 1532 | $rank = ++$current_rank_of[$category_id]; |
---|
[17002] | 1533 | |
---|
[17424] | 1534 | array_push( |
---|
| 1535 | $inserts, |
---|
| 1536 | array( |
---|
| 1537 | 'image_id' => $image_id, |
---|
| 1538 | 'category_id' => $category_id, |
---|
| 1539 | 'rank' => $rank, |
---|
| 1540 | ) |
---|
| 1541 | ); |
---|
| 1542 | } |
---|
[1121] | 1543 | } |
---|
| 1544 | } |
---|
[1592] | 1545 | |
---|
[17424] | 1546 | if (count($inserts)) |
---|
| 1547 | { |
---|
| 1548 | mass_inserts( |
---|
| 1549 | IMAGE_CATEGORY_TABLE, |
---|
| 1550 | array_keys($inserts[0]), |
---|
| 1551 | $inserts |
---|
| 1552 | ); |
---|
[1121] | 1553 | |
---|
[17424] | 1554 | update_category($categories); |
---|
| 1555 | } |
---|
[1121] | 1556 | } |
---|
| 1557 | |
---|
| 1558 | /** |
---|
[17424] | 1559 | * Dissociate images from all old categories except their storage category and |
---|
[13077] | 1560 | * associate to new categories. |
---|
| 1561 | * |
---|
[17424] | 1562 | * This function will preserve ranks |
---|
| 1563 | * |
---|
[13077] | 1564 | * @param array images |
---|
| 1565 | * @param array categories |
---|
| 1566 | * @return void |
---|
| 1567 | */ |
---|
| 1568 | function move_images_to_categories($images, $categories) |
---|
| 1569 | { |
---|
| 1570 | if (count($images) == 0) |
---|
| 1571 | { |
---|
| 1572 | return false; |
---|
| 1573 | } |
---|
[17302] | 1574 | |
---|
[17424] | 1575 | // let's first break links with all old albums but their "storage album" |
---|
[13077] | 1576 | $query = ' |
---|
| 1577 | DELETE '.IMAGE_CATEGORY_TABLE.'.* |
---|
| 1578 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 1579 | JOIN '.IMAGES_TABLE.' ON image_id=id |
---|
| 1580 | WHERE id IN ('.implode(',', $images).') |
---|
[17980] | 1581 | '; |
---|
[18628] | 1582 | |
---|
[17980] | 1583 | if (is_array($categories) and count($categories) > 0) |
---|
| 1584 | { |
---|
| 1585 | $query.= ' |
---|
| 1586 | AND category_id NOT IN ('.implode(',', $categories).') |
---|
| 1587 | '; |
---|
| 1588 | } |
---|
| 1589 | |
---|
| 1590 | $query.= ' |
---|
[13077] | 1591 | AND (storage_category_id IS NULL OR storage_category_id != category_id) |
---|
| 1592 | ;'; |
---|
| 1593 | pwg_query($query); |
---|
[17302] | 1594 | |
---|
[13077] | 1595 | if (is_array($categories) and count($categories) > 0) |
---|
| 1596 | { |
---|
| 1597 | associate_images_to_categories($images, $categories); |
---|
| 1598 | } |
---|
| 1599 | } |
---|
| 1600 | |
---|
| 1601 | /** |
---|
[1121] | 1602 | * Associate images associated to a list of source categories to a list of |
---|
| 1603 | * destination categories. |
---|
| 1604 | * |
---|
| 1605 | * @param array sources |
---|
| 1606 | * @param array destinations |
---|
| 1607 | * @return void |
---|
| 1608 | */ |
---|
| 1609 | function associate_categories_to_categories($sources, $destinations) |
---|
| 1610 | { |
---|
| 1611 | if (count($sources) == 0) |
---|
| 1612 | { |
---|
| 1613 | return false; |
---|
| 1614 | } |
---|
| 1615 | |
---|
| 1616 | $query = ' |
---|
| 1617 | SELECT image_id |
---|
| 1618 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 1619 | WHERE category_id IN ('.implode(',', $sources).') |
---|
| 1620 | ;'; |
---|
| 1621 | $images = array_from_query($query, 'image_id'); |
---|
| 1622 | |
---|
| 1623 | associate_images_to_categories($images, $destinations); |
---|
| 1624 | } |
---|
[1682] | 1625 | |
---|
| 1626 | /** |
---|
[2339] | 1627 | * Refer main Piwigo URLs (currently PHPWG_DOMAIN domain) |
---|
[1724] | 1628 | * |
---|
| 1629 | * @param void |
---|
| 1630 | * @return array like $conf['links'] |
---|
| 1631 | */ |
---|
| 1632 | function pwg_URL() |
---|
| 1633 | { |
---|
| 1634 | $urls = array( |
---|
[12875] | 1635 | 'HOME' => PHPWG_URL, |
---|
| 1636 | 'WIKI' => PHPWG_URL.'/doc', |
---|
| 1637 | 'DEMO' => PHPWG_URL.'/demo', |
---|
| 1638 | 'FORUM' => PHPWG_URL.'/forum', |
---|
| 1639 | 'BUGS' => PHPWG_URL.'/bugs', |
---|
| 1640 | 'EXTENSIONS' => PHPWG_URL.'/ext', |
---|
[1724] | 1641 | ); |
---|
| 1642 | return $urls; |
---|
| 1643 | } |
---|
| 1644 | |
---|
[1978] | 1645 | /** |
---|
| 1646 | * Invalidates cahed data (permissions and category counts) for all users. |
---|
| 1647 | */ |
---|
[2890] | 1648 | function invalidate_user_cache($full = true) |
---|
[1978] | 1649 | { |
---|
[2890] | 1650 | if ($full) |
---|
| 1651 | { |
---|
| 1652 | $query = ' |
---|
| 1653 | TRUNCATE TABLE '.USER_CACHE_CATEGORIES_TABLE.';'; |
---|
| 1654 | pwg_query($query); |
---|
| 1655 | $query = ' |
---|
| 1656 | TRUNCATE TABLE '.USER_CACHE_TABLE.';'; |
---|
| 1657 | pwg_query($query); |
---|
| 1658 | } |
---|
| 1659 | else |
---|
| 1660 | { |
---|
| 1661 | $query = ' |
---|
[1978] | 1662 | UPDATE '.USER_CACHE_TABLE.' |
---|
[2890] | 1663 | SET need_update = \'true\';'; |
---|
| 1664 | pwg_query($query); |
---|
| 1665 | } |
---|
[2892] | 1666 | trigger_action('invalidate_user_cache', $full); |
---|
[1978] | 1667 | } |
---|
[2127] | 1668 | |
---|
| 1669 | /** |
---|
| 1670 | * adds the caracter set to a create table sql query. |
---|
| 1671 | * all CREATE TABLE queries must call this function |
---|
| 1672 | * @param string query - the sql query |
---|
| 1673 | */ |
---|
| 1674 | function create_table_add_character_set($query) |
---|
| 1675 | { |
---|
[2502] | 1676 | defined('DB_CHARSET') or fatal_error('create_table_add_character_set DB_CHARSET undefined'); |
---|
[2127] | 1677 | if ('DB_CHARSET'!='') |
---|
| 1678 | { |
---|
| 1679 | if ( version_compare(mysql_get_server_info(), '4.1.0', '<') ) |
---|
| 1680 | { |
---|
| 1681 | return $query; |
---|
| 1682 | } |
---|
| 1683 | $charset_collate = " DEFAULT CHARACTER SET ".DB_CHARSET; |
---|
[2488] | 1684 | if (DB_COLLATE!='') |
---|
[2127] | 1685 | { |
---|
| 1686 | $charset_collate .= " COLLATE ".DB_COLLATE; |
---|
| 1687 | } |
---|
[2488] | 1688 | if ( is_array($query) ) |
---|
[2127] | 1689 | { |
---|
[2488] | 1690 | foreach( $query as $id=>$q) |
---|
| 1691 | { |
---|
| 1692 | $q=trim($q); |
---|
| 1693 | $q=trim($q, ';'); |
---|
| 1694 | if (preg_match('/^CREATE\s+TABLE/i',$q)) |
---|
| 1695 | { |
---|
| 1696 | $q.=$charset_collate; |
---|
| 1697 | } |
---|
| 1698 | $q .= ';'; |
---|
| 1699 | $query[$id] = $q; |
---|
| 1700 | } |
---|
[2127] | 1701 | } |
---|
[2488] | 1702 | else |
---|
| 1703 | { |
---|
| 1704 | $query=trim($query); |
---|
| 1705 | $query=trim($query, ';'); |
---|
| 1706 | if (preg_match('/^CREATE\s+TABLE/i',$query)) |
---|
| 1707 | { |
---|
| 1708 | $query.=$charset_collate; |
---|
| 1709 | } |
---|
| 1710 | $query .= ';'; |
---|
| 1711 | } |
---|
[2127] | 1712 | } |
---|
| 1713 | return $query; |
---|
| 1714 | } |
---|
[2325] | 1715 | |
---|
| 1716 | /** |
---|
| 1717 | * Returns array use on template with html_options method |
---|
| 1718 | * @param Min and Max access to use |
---|
| 1719 | * @return array of user access level |
---|
| 1720 | */ |
---|
| 1721 | function get_user_access_level_html_options($MinLevelAccess = ACCESS_FREE, $MaxLevelAccess = ACCESS_CLOSED) |
---|
| 1722 | { |
---|
| 1723 | $tpl_options = array(); |
---|
| 1724 | for ($level = $MinLevelAccess; $level <= $MaxLevelAccess; $level++) |
---|
| 1725 | { |
---|
| 1726 | $tpl_options[$level] = l10n(sprintf('ACCESS_%d', $level)); |
---|
| 1727 | } |
---|
| 1728 | return $tpl_options; |
---|
| 1729 | } |
---|
| 1730 | |
---|
[2588] | 1731 | /** |
---|
| 1732 | * returns a list of templates currently available in template-extension |
---|
| 1733 | * Each .tpl file is extracted from template-extension. |
---|
| 1734 | * @return array |
---|
| 1735 | */ |
---|
| 1736 | function get_extents($start='') |
---|
| 1737 | { |
---|
| 1738 | if ($start == '') { $start = './template-extension'; } |
---|
| 1739 | $dir = opendir($start); |
---|
| 1740 | $extents = array(); |
---|
| 1741 | |
---|
| 1742 | while (($file = readdir($dir)) !== false) |
---|
| 1743 | { |
---|
| 1744 | if ( $file == '.' or $file == '..' or $file == '.svn') continue; |
---|
| 1745 | $path = $start . '/' . $file; |
---|
| 1746 | if (is_dir($path)) |
---|
| 1747 | { |
---|
| 1748 | $extents = array_merge($extents, get_extents($path)); |
---|
| 1749 | } |
---|
[3145] | 1750 | elseif ( !is_link($path) and file_exists($path) |
---|
[2588] | 1751 | and get_extension($path) == 'tpl' ) |
---|
| 1752 | { |
---|
| 1753 | $extents[] = substr($path, 21); |
---|
| 1754 | } |
---|
| 1755 | } |
---|
| 1756 | return $extents; |
---|
| 1757 | } |
---|
| 1758 | |
---|
[2634] | 1759 | function create_tag($tag_name) |
---|
| 1760 | { |
---|
| 1761 | // does the tag already exists? |
---|
| 1762 | $query = ' |
---|
| 1763 | SELECT id |
---|
| 1764 | FROM '.TAGS_TABLE.' |
---|
| 1765 | WHERE name = \''.$tag_name.'\' |
---|
| 1766 | ;'; |
---|
| 1767 | $existing_tags = array_from_query($query, 'id'); |
---|
| 1768 | |
---|
| 1769 | if (count($existing_tags) == 0) |
---|
| 1770 | { |
---|
| 1771 | mass_inserts( |
---|
| 1772 | TAGS_TABLE, |
---|
| 1773 | array('name', 'url_name'), |
---|
| 1774 | array( |
---|
| 1775 | array( |
---|
| 1776 | 'name' => $tag_name, |
---|
[12553] | 1777 | 'url_name' => trigger_event('render_tag_url', $tag_name), |
---|
[2634] | 1778 | ) |
---|
| 1779 | ) |
---|
| 1780 | ); |
---|
| 1781 | |
---|
[4892] | 1782 | $inserted_id = pwg_db_insert_id(TAGS_TABLE); |
---|
[2634] | 1783 | |
---|
| 1784 | return array( |
---|
| 1785 | 'info' => sprintf( |
---|
[5036] | 1786 | l10n('Tag "%s" was added'), |
---|
[2634] | 1787 | stripslashes($tag_name) |
---|
| 1788 | ), |
---|
| 1789 | 'id' => $inserted_id, |
---|
| 1790 | ); |
---|
| 1791 | } |
---|
| 1792 | else |
---|
| 1793 | { |
---|
| 1794 | return array( |
---|
| 1795 | 'error' => sprintf( |
---|
[5036] | 1796 | l10n('Tag "%s" already exists'), |
---|
[2634] | 1797 | stripslashes($tag_name) |
---|
| 1798 | ) |
---|
| 1799 | ); |
---|
| 1800 | } |
---|
| 1801 | } |
---|
[2777] | 1802 | |
---|
[11828] | 1803 | /** |
---|
| 1804 | * Is the category accessible to the (Admin) user ? |
---|
| 1805 | * |
---|
| 1806 | * Note : if the user is not authorized to see this category, category jump |
---|
| 1807 | * will be replaced by admin cat_modify page |
---|
| 1808 | * |
---|
| 1809 | * @param int category id to verify |
---|
| 1810 | * @return bool |
---|
| 1811 | */ |
---|
| 1812 | function cat_admin_access($category_id) |
---|
| 1813 | { |
---|
| 1814 | global $user; |
---|
[2777] | 1815 | |
---|
[11828] | 1816 | // $filter['visible_categories'] and $filter['visible_images'] |
---|
| 1817 | // are not used because it's not necessary (filter <> restriction) |
---|
| 1818 | if (in_array($category_id, explode(',', $user['forbidden_categories']))) |
---|
| 1819 | { |
---|
| 1820 | return false; |
---|
| 1821 | } |
---|
| 1822 | return true; |
---|
| 1823 | } |
---|
| 1824 | |
---|
[2880] | 1825 | /** |
---|
| 1826 | * Retrieve data from external URL |
---|
| 1827 | * |
---|
| 1828 | * @param string $src: URL |
---|
| 1829 | * @param global $dest: can be a file ressource or string |
---|
| 1830 | * @return bool |
---|
| 1831 | */ |
---|
[8079] | 1832 | function fetchRemote($src, &$dest, $get_data=array(), $post_data=array(), $user_agent='Piwigo', $step=0) |
---|
[2880] | 1833 | { |
---|
[2902] | 1834 | // Try to retrieve data from local file? |
---|
| 1835 | if (!url_is_remote($src)) |
---|
| 1836 | { |
---|
| 1837 | $content = @file_get_contents($src); |
---|
| 1838 | if ($content !== false) |
---|
| 1839 | { |
---|
| 1840 | is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; |
---|
| 1841 | return true; |
---|
| 1842 | } |
---|
| 1843 | else |
---|
| 1844 | { |
---|
| 1845 | return false; |
---|
| 1846 | } |
---|
| 1847 | } |
---|
| 1848 | |
---|
[8079] | 1849 | // After 3 redirections, return false |
---|
| 1850 | if ($step > 3) return false; |
---|
| 1851 | |
---|
[8080] | 1852 | // Initialization |
---|
| 1853 | $method = empty($post_data) ? 'GET' : 'POST'; |
---|
| 1854 | $request = empty($post_data) ? '' : http_build_query($post_data, '', '&'); |
---|
[8090] | 1855 | if (!empty($get_data)) |
---|
| 1856 | { |
---|
| 1857 | $src .= strpos($src, '?') === false ? '?' : '&'; |
---|
| 1858 | $src .= http_build_query($get_data, '', '&'); |
---|
| 1859 | } |
---|
[8080] | 1860 | |
---|
[2900] | 1861 | // Initialize $dest |
---|
[2880] | 1862 | is_resource($dest) or $dest = ''; |
---|
| 1863 | |
---|
| 1864 | // Try curl to read remote file |
---|
| 1865 | if (function_exists('curl_init')) |
---|
| 1866 | { |
---|
| 1867 | $ch = @curl_init(); |
---|
| 1868 | @curl_setopt($ch, CURLOPT_URL, $src); |
---|
[2900] | 1869 | @curl_setopt($ch, CURLOPT_HEADER, 1); |
---|
[2880] | 1870 | @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); |
---|
[2900] | 1871 | @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
---|
[8079] | 1872 | if ($method == 'POST') |
---|
| 1873 | { |
---|
| 1874 | @curl_setopt($ch, CURLOPT_POST, 1); |
---|
[8090] | 1875 | @curl_setopt($ch, CURLOPT_POSTFIELDS, $request); |
---|
[8079] | 1876 | } |
---|
[2880] | 1877 | $content = @curl_exec($ch); |
---|
[2900] | 1878 | $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
---|
[2902] | 1879 | $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE); |
---|
[2880] | 1880 | @curl_close($ch); |
---|
[2902] | 1881 | if ($content !== false and $status >= 200 and $status < 400) |
---|
[2880] | 1882 | { |
---|
[2900] | 1883 | if (preg_match('/Location:\s+?(.+)/', substr($content, 0, $header_length), $m)) |
---|
| 1884 | { |
---|
[8079] | 1885 | return fetchRemote($m[1], $dest, array(), array(), $user_agent, $step+1); |
---|
[2900] | 1886 | } |
---|
| 1887 | $content = substr($content, $header_length); |
---|
| 1888 | is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; |
---|
[2880] | 1889 | return true; |
---|
| 1890 | } |
---|
| 1891 | } |
---|
| 1892 | |
---|
| 1893 | // Try file_get_contents to read remote file |
---|
| 1894 | if (ini_get('allow_url_fopen')) |
---|
| 1895 | { |
---|
[8079] | 1896 | $opts = array( |
---|
| 1897 | 'http' => array( |
---|
| 1898 | 'method' => $method, |
---|
| 1899 | 'user_agent' => $user_agent, |
---|
| 1900 | ) |
---|
| 1901 | ); |
---|
[8090] | 1902 | if ($method == 'POST') |
---|
| 1903 | { |
---|
| 1904 | $opts['http']['content'] = $request; |
---|
| 1905 | } |
---|
[8079] | 1906 | $context = @stream_context_create($opts); |
---|
| 1907 | $content = @file_get_contents($src, false, $context); |
---|
[2880] | 1908 | if ($content !== false) |
---|
| 1909 | { |
---|
| 1910 | is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; |
---|
| 1911 | return true; |
---|
| 1912 | } |
---|
| 1913 | } |
---|
| 1914 | |
---|
| 1915 | // Try fsockopen to read remote file |
---|
| 1916 | $src = parse_url($src); |
---|
| 1917 | $host = $src['host']; |
---|
| 1918 | $path = isset($src['path']) ? $src['path'] : '/'; |
---|
| 1919 | $path .= isset($src['query']) ? '?'.$src['query'] : ''; |
---|
[3145] | 1920 | |
---|
[2880] | 1921 | if (($s = @fsockopen($host,80,$errno,$errstr,5)) === false) |
---|
| 1922 | { |
---|
| 1923 | return false; |
---|
| 1924 | } |
---|
| 1925 | |
---|
[8079] | 1926 | $http_request = $method." ".$path." HTTP/1.0\r\n"; |
---|
| 1927 | $http_request .= "Host: ".$host."\r\n"; |
---|
[8090] | 1928 | if ($method == 'POST') |
---|
| 1929 | { |
---|
| 1930 | $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n"; |
---|
| 1931 | $http_request .= "Content-Length: ".strlen($request)."\r\n"; |
---|
| 1932 | } |
---|
[8079] | 1933 | $http_request .= "User-Agent: ".$user_agent."\r\n"; |
---|
| 1934 | $http_request .= "Accept: */*\r\n"; |
---|
| 1935 | $http_request .= "\r\n"; |
---|
| 1936 | $http_request .= $request; |
---|
[2880] | 1937 | |
---|
[8079] | 1938 | fwrite($s, $http_request); |
---|
| 1939 | |
---|
[2880] | 1940 | $i = 0; |
---|
| 1941 | $in_content = false; |
---|
| 1942 | while (!feof($s)) |
---|
| 1943 | { |
---|
| 1944 | $line = fgets($s); |
---|
| 1945 | |
---|
| 1946 | if (rtrim($line,"\r\n") == '' && !$in_content) |
---|
| 1947 | { |
---|
| 1948 | $in_content = true; |
---|
| 1949 | $i++; |
---|
| 1950 | continue; |
---|
| 1951 | } |
---|
| 1952 | if ($i == 0) |
---|
| 1953 | { |
---|
| 1954 | if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/',rtrim($line,"\r\n"), $m)) |
---|
| 1955 | { |
---|
| 1956 | fclose($s); |
---|
| 1957 | return false; |
---|
| 1958 | } |
---|
| 1959 | $status = (integer) $m[2]; |
---|
| 1960 | if ($status < 200 || $status >= 400) |
---|
| 1961 | { |
---|
| 1962 | fclose($s); |
---|
| 1963 | return false; |
---|
| 1964 | } |
---|
| 1965 | } |
---|
| 1966 | if (!$in_content) |
---|
| 1967 | { |
---|
| 1968 | if (preg_match('/Location:\s+?(.+)$/',rtrim($line,"\r\n"),$m)) |
---|
| 1969 | { |
---|
| 1970 | fclose($s); |
---|
[8079] | 1971 | return fetchRemote(trim($m[1]),$dest,array(),array(),$user_agent,$step+1); |
---|
[2880] | 1972 | } |
---|
| 1973 | $i++; |
---|
| 1974 | continue; |
---|
| 1975 | } |
---|
| 1976 | is_resource($dest) ? @fwrite($dest, $line) : $dest .= $line; |
---|
| 1977 | $i++; |
---|
| 1978 | } |
---|
| 1979 | fclose($s); |
---|
| 1980 | return true; |
---|
| 1981 | } |
---|
| 1982 | |
---|
[3145] | 1983 | |
---|
| 1984 | /** |
---|
| 1985 | * returns the groupname corresponding to the given group identifier if |
---|
| 1986 | * exists |
---|
| 1987 | * |
---|
| 1988 | * @param int group_id |
---|
| 1989 | * @return mixed |
---|
| 1990 | */ |
---|
| 1991 | function get_groupname($group_id) |
---|
| 1992 | { |
---|
| 1993 | $query = ' |
---|
| 1994 | SELECT name |
---|
| 1995 | FROM '.GROUPS_TABLE.' |
---|
| 1996 | WHERE id = '.intval($group_id).' |
---|
| 1997 | ;'; |
---|
| 1998 | $result = pwg_query($query); |
---|
[4325] | 1999 | if (pwg_db_num_rows($result) > 0) |
---|
[3145] | 2000 | { |
---|
[4325] | 2001 | list($groupname) = pwg_db_fetch_row($result); |
---|
[3145] | 2002 | } |
---|
| 2003 | else |
---|
| 2004 | { |
---|
| 2005 | return false; |
---|
| 2006 | } |
---|
| 2007 | |
---|
| 2008 | return $groupname; |
---|
| 2009 | } |
---|
| 2010 | |
---|
| 2011 | /** |
---|
| 2012 | * returns the username corresponding to the given user identifier if exists |
---|
| 2013 | * |
---|
| 2014 | * @param int user_id |
---|
| 2015 | * @return mixed |
---|
| 2016 | */ |
---|
| 2017 | function get_username($user_id) |
---|
| 2018 | { |
---|
| 2019 | global $conf; |
---|
| 2020 | |
---|
| 2021 | $query = ' |
---|
| 2022 | SELECT '.$conf['user_fields']['username'].' |
---|
| 2023 | FROM '.USERS_TABLE.' |
---|
| 2024 | WHERE '.$conf['user_fields']['id'].' = '.intval($user_id).' |
---|
| 2025 | ;'; |
---|
| 2026 | $result = pwg_query($query); |
---|
[4325] | 2027 | if (pwg_db_num_rows($result) > 0) |
---|
[3145] | 2028 | { |
---|
[4325] | 2029 | list($username) = pwg_db_fetch_row($result); |
---|
[3145] | 2030 | } |
---|
| 2031 | else |
---|
| 2032 | { |
---|
| 2033 | return false; |
---|
| 2034 | } |
---|
| 2035 | |
---|
[4304] | 2036 | return stripslashes($username); |
---|
[3145] | 2037 | } |
---|
[3382] | 2038 | |
---|
| 2039 | function get_newsletter_subscribe_base_url($language) { |
---|
[12875] | 2040 | return PHPWG_URL.'/announcement/subscribe/'; |
---|
[3382] | 2041 | } |
---|
[5173] | 2042 | |
---|
| 2043 | /** |
---|
| 2044 | * Accordion menus need to know which section to open by default when |
---|
| 2045 | * loading the page |
---|
| 2046 | */ |
---|
| 2047 | function get_active_menu($menu_page) |
---|
| 2048 | { |
---|
| 2049 | global $page; |
---|
| 2050 | |
---|
| 2051 | if (isset($page['active_menu'])) |
---|
| 2052 | { |
---|
| 2053 | return $page['active_menu']; |
---|
| 2054 | } |
---|
| 2055 | |
---|
| 2056 | switch ($menu_page) |
---|
| 2057 | { |
---|
[13077] | 2058 | case 'photo': |
---|
[5173] | 2059 | case 'photos_add': |
---|
| 2060 | case 'rating': |
---|
| 2061 | case 'tags': |
---|
[8417] | 2062 | case 'batch_manager': |
---|
[5173] | 2063 | return 0; |
---|
| 2064 | |
---|
[13013] | 2065 | case 'album': |
---|
[5173] | 2066 | case 'cat_list': |
---|
| 2067 | case 'cat_move': |
---|
| 2068 | case 'cat_options': |
---|
| 2069 | case 'permalinks': |
---|
| 2070 | return 1; |
---|
[5691] | 2071 | |
---|
[5173] | 2072 | case 'user_list': |
---|
| 2073 | case 'user_perm': |
---|
| 2074 | case 'group_list': |
---|
| 2075 | case 'group_perm': |
---|
| 2076 | case 'notification_by_mail': |
---|
| 2077 | return 2; |
---|
| 2078 | |
---|
[10594] | 2079 | case 'plugins': |
---|
[5173] | 2080 | case 'plugin': |
---|
| 2081 | return 3; |
---|
[5691] | 2082 | |
---|
[6056] | 2083 | case 'site_manager': |
---|
| 2084 | case 'site_update': |
---|
[5173] | 2085 | case 'stats': |
---|
| 2086 | case 'history': |
---|
| 2087 | case 'maintenance': |
---|
[5997] | 2088 | case 'comments': |
---|
[10511] | 2089 | case 'updates': |
---|
[5173] | 2090 | return 4; |
---|
| 2091 | |
---|
| 2092 | case 'configuration': |
---|
[12820] | 2093 | case 'derivatives': |
---|
[5173] | 2094 | case 'extend_for_templates': |
---|
| 2095 | case 'menubar': |
---|
[10594] | 2096 | case 'themes': |
---|
[5448] | 2097 | case 'theme': |
---|
[10594] | 2098 | case 'languages': |
---|
[5173] | 2099 | return 5; |
---|
| 2100 | } |
---|
| 2101 | return 0; |
---|
| 2102 | } |
---|
[5188] | 2103 | |
---|
[12259] | 2104 | function get_taglist($query, $only_user_language=true) |
---|
[5188] | 2105 | { |
---|
| 2106 | $result = pwg_query($query); |
---|
[12917] | 2107 | |
---|
[5188] | 2108 | $taglist = array(); |
---|
[17765] | 2109 | $altlist = array(); |
---|
[5188] | 2110 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 2111 | { |
---|
[17765] | 2112 | $raw_name = $row['name']; |
---|
| 2113 | $name = trigger_event('render_tag_name', $raw_name); |
---|
| 2114 | |
---|
| 2115 | $taglist[] = array( |
---|
| 2116 | 'name' => $name, |
---|
| 2117 | 'id' => '~~'.$row['id'].'~~', |
---|
| 2118 | ); |
---|
| 2119 | |
---|
| 2120 | if (!$only_user_language) |
---|
[11487] | 2121 | { |
---|
[17765] | 2122 | $alt_names = trigger_event('get_tag_alt_names', array(), $raw_name); |
---|
| 2123 | |
---|
| 2124 | foreach( array_diff( array_unique($alt_names), array($name) ) as $alt) |
---|
| 2125 | { |
---|
| 2126 | $altlist[] = array( |
---|
| 2127 | 'name' => $alt, |
---|
[11853] | 2128 | 'id' => '~~'.$row['id'].'~~', |
---|
[11487] | 2129 | ); |
---|
| 2130 | } |
---|
| 2131 | } |
---|
[17765] | 2132 | } |
---|
[12917] | 2133 | |
---|
[17765] | 2134 | usort($taglist, 'tag_alpha_compare'); |
---|
| 2135 | if (count($altlist)) |
---|
| 2136 | { |
---|
| 2137 | usort($altlist, 'tag_alpha_compare'); |
---|
| 2138 | $taglist = array_merge($taglist, $altlist); |
---|
[5188] | 2139 | } |
---|
[12917] | 2140 | |
---|
[5188] | 2141 | return $taglist; |
---|
| 2142 | } |
---|
| 2143 | |
---|
[11853] | 2144 | function get_tag_ids($raw_tags, $allow_create=true) |
---|
[5188] | 2145 | { |
---|
| 2146 | // In $raw_tags we receive something like array('~~6~~', '~~59~~', 'New |
---|
| 2147 | // tag', 'Another new tag') The ~~34~~ means that it is an existing |
---|
| 2148 | // tag. I've added the surrounding ~~ to permit creation of tags like "10" |
---|
| 2149 | // or "1234" (numeric characters only) |
---|
| 2150 | |
---|
| 2151 | $tag_ids = array(); |
---|
[10970] | 2152 | $raw_tags = explode(',',$raw_tags); |
---|
[5691] | 2153 | |
---|
[5188] | 2154 | foreach ($raw_tags as $raw_tag) |
---|
| 2155 | { |
---|
| 2156 | if (preg_match('/^~~(\d+)~~$/', $raw_tag, $matches)) |
---|
| 2157 | { |
---|
[17724] | 2158 | $tag_ids[] = $matches[1]; |
---|
[5188] | 2159 | } |
---|
[11853] | 2160 | elseif ($allow_create) |
---|
[5188] | 2161 | { |
---|
| 2162 | // we have to create a new tag |
---|
[11853] | 2163 | $tag_ids[] = tag_id_from_tag_name($raw_tag); |
---|
[5188] | 2164 | } |
---|
| 2165 | } |
---|
| 2166 | |
---|
| 2167 | return $tag_ids; |
---|
| 2168 | } |
---|
[8247] | 2169 | |
---|
| 2170 | /** returns the argument_ids array with new sequenced keys based on related |
---|
| 2171 | * names. Sequence is not case sensitive. |
---|
| 2172 | * Warning: By definition, this function breaks original keys |
---|
| 2173 | */ |
---|
| 2174 | function order_by_name($element_ids,$name) |
---|
| 2175 | { |
---|
| 2176 | $ordered_element_ids = array(); |
---|
| 2177 | foreach ($element_ids as $k_id => $element_id) |
---|
| 2178 | { |
---|
| 2179 | $key = strtolower($name[$element_id]) .'-'. $name[$element_id] .'-'. $k_id; |
---|
| 2180 | $ordered_element_ids[$key] = $element_id; |
---|
| 2181 | } |
---|
| 2182 | ksort($ordered_element_ids); |
---|
| 2183 | return $ordered_element_ids; |
---|
| 2184 | } |
---|
| 2185 | |
---|
[11728] | 2186 | function add_permission_on_category($category_ids, $user_ids) |
---|
| 2187 | { |
---|
| 2188 | // array-ify categories and users |
---|
| 2189 | if (!is_array($category_ids)) |
---|
| 2190 | { |
---|
| 2191 | $category_ids = array($category_ids); |
---|
| 2192 | } |
---|
| 2193 | |
---|
| 2194 | if (!is_array($user_ids)) |
---|
| 2195 | { |
---|
| 2196 | $user_ids = array($user_ids); |
---|
| 2197 | } |
---|
| 2198 | |
---|
| 2199 | // check for emptiness |
---|
| 2200 | if (count($category_ids) == 0 or count($user_ids) == 0) |
---|
| 2201 | { |
---|
| 2202 | return; |
---|
| 2203 | } |
---|
[12917] | 2204 | |
---|
[12019] | 2205 | // make sure categories are private and select uppercats or subcats |
---|
| 2206 | $cat_ids = (isset($_POST['apply_on_sub'])) ? implode(',', get_subcat_ids($category_ids)).",".implode(',', get_uppercat_ids($category_ids)) : implode(',', get_uppercat_ids($category_ids)); |
---|
[11728] | 2207 | $query = ' |
---|
| 2208 | SELECT |
---|
| 2209 | id |
---|
| 2210 | FROM '.CATEGORIES_TABLE.' |
---|
[12019] | 2211 | WHERE id IN ('.$cat_ids.') |
---|
[11728] | 2212 | AND status = \'private\' |
---|
| 2213 | ;'; |
---|
[12019] | 2214 | $private_cats = array_from_query($query, 'id'); |
---|
[11728] | 2215 | |
---|
[12019] | 2216 | if (count($private_cats) == 0) |
---|
[11728] | 2217 | { |
---|
| 2218 | return; |
---|
| 2219 | } |
---|
[12917] | 2220 | |
---|
[11728] | 2221 | // We must not reinsert already existing lines in user_access table |
---|
| 2222 | $granteds = array(); |
---|
[12019] | 2223 | foreach ($private_cats as $cat_id) |
---|
[11728] | 2224 | { |
---|
| 2225 | $granteds[$cat_id] = array(); |
---|
| 2226 | } |
---|
[12917] | 2227 | |
---|
[11728] | 2228 | $query = ' |
---|
| 2229 | SELECT |
---|
| 2230 | user_id, |
---|
| 2231 | cat_id |
---|
| 2232 | FROM '.USER_ACCESS_TABLE.' |
---|
[12019] | 2233 | WHERE cat_id IN ('.implode(',', $private_cats).') |
---|
[11728] | 2234 | AND user_id IN ('.implode(',', $user_ids).') |
---|
| 2235 | ;'; |
---|
| 2236 | $result = pwg_query($query); |
---|
| 2237 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 2238 | { |
---|
| 2239 | array_push($granteds[$row['cat_id']], $row['user_id']); |
---|
| 2240 | } |
---|
| 2241 | |
---|
| 2242 | $inserts = array(); |
---|
[12917] | 2243 | |
---|
[12019] | 2244 | foreach ($private_cats as $cat_id) |
---|
[11728] | 2245 | { |
---|
| 2246 | $grant_to_users = array_diff($user_ids, $granteds[$cat_id]); |
---|
[12917] | 2247 | |
---|
[11728] | 2248 | foreach ($grant_to_users as $user_id) |
---|
| 2249 | { |
---|
| 2250 | array_push( |
---|
| 2251 | $inserts, |
---|
| 2252 | array( |
---|
| 2253 | 'user_id' => $user_id, |
---|
| 2254 | 'cat_id' => $cat_id |
---|
| 2255 | ) |
---|
| 2256 | ); |
---|
| 2257 | } |
---|
| 2258 | } |
---|
| 2259 | |
---|
| 2260 | if (count($inserts) > 0) |
---|
| 2261 | { |
---|
| 2262 | mass_inserts(USER_ACCESS_TABLE, array_keys($inserts[0]), $inserts); |
---|
| 2263 | } |
---|
| 2264 | } |
---|
| 2265 | |
---|
| 2266 | |
---|
| 2267 | function get_admins($include_webmaster=true) |
---|
| 2268 | { |
---|
| 2269 | $status_list = array('admin'); |
---|
| 2270 | |
---|
| 2271 | if ($include_webmaster) |
---|
| 2272 | { |
---|
| 2273 | $status_list[] = 'webmaster'; |
---|
| 2274 | } |
---|
[12917] | 2275 | |
---|
[11728] | 2276 | $query = ' |
---|
| 2277 | SELECT |
---|
| 2278 | user_id |
---|
| 2279 | FROM '.USER_INFOS_TABLE.' |
---|
| 2280 | WHERE status in (\''.implode("','", $status_list).'\') |
---|
| 2281 | ;'; |
---|
| 2282 | |
---|
| 2283 | return array_from_query($query, 'user_id'); |
---|
| 2284 | } |
---|
[12797] | 2285 | |
---|
[12820] | 2286 | /** delete all derivative files for one or several types */ |
---|
| 2287 | function clear_derivative_cache($types='all') |
---|
[12797] | 2288 | { |
---|
[17302] | 2289 | if ($types === 'all') |
---|
[12797] | 2290 | { |
---|
[12820] | 2291 | $types = ImageStdParams::get_all_types(); |
---|
| 2292 | $types[] = IMG_CUSTOM; |
---|
| 2293 | } |
---|
| 2294 | elseif (!is_array($types)) |
---|
| 2295 | { |
---|
| 2296 | $types = array($types); |
---|
| 2297 | } |
---|
| 2298 | |
---|
[17302] | 2299 | for ($i=0; $i<count($types); $i++) |
---|
[12820] | 2300 | { |
---|
[17302] | 2301 | $type = $types[$i]; |
---|
| 2302 | if ($type == IMG_CUSTOM) |
---|
[12797] | 2303 | { |
---|
[17302] | 2304 | $type = derivative_to_url($type).'[a-zA-Z0-9]+'; |
---|
[12797] | 2305 | } |
---|
[17302] | 2306 | elseif (in_array($type, ImageStdParams::get_all_types())) |
---|
| 2307 | { |
---|
| 2308 | $type = derivative_to_url($type); |
---|
| 2309 | } |
---|
| 2310 | else |
---|
| 2311 | {//assume a custom type |
---|
| 2312 | $type = derivative_to_url(IMG_CUSTOM).'_'.$type; |
---|
| 2313 | } |
---|
| 2314 | $types[$i] = $type; |
---|
[12797] | 2315 | } |
---|
[17302] | 2316 | |
---|
| 2317 | $pattern='#.*-'; |
---|
| 2318 | if (count($types)>1) |
---|
| 2319 | { |
---|
| 2320 | $pattern .= '(' . implode('|',$types) . ')'; |
---|
| 2321 | } |
---|
[12797] | 2322 | else |
---|
| 2323 | { |
---|
[17302] | 2324 | $pattern .= $types[0]; |
---|
[12797] | 2325 | } |
---|
[17302] | 2326 | $pattern.='\.[a-zA-Z0-9]{3,4}$#'; |
---|
[12917] | 2327 | |
---|
[13651] | 2328 | if ($contents = @opendir(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR)) |
---|
[12797] | 2329 | { |
---|
| 2330 | while (($node = readdir($contents)) !== false) |
---|
| 2331 | { |
---|
| 2332 | if ($node != '.' |
---|
| 2333 | and $node != '..' |
---|
| 2334 | and is_dir(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$node)) |
---|
| 2335 | { |
---|
| 2336 | clear_derivative_cache_rec(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$node, $pattern); |
---|
| 2337 | } |
---|
| 2338 | } |
---|
| 2339 | closedir($contents); |
---|
| 2340 | } |
---|
| 2341 | } |
---|
| 2342 | |
---|
| 2343 | function clear_derivative_cache_rec($path, $pattern) |
---|
| 2344 | { |
---|
| 2345 | $rmdir = true; |
---|
| 2346 | $rm_index = false; |
---|
| 2347 | |
---|
| 2348 | if ($contents = opendir($path)) |
---|
| 2349 | { |
---|
| 2350 | while (($node = readdir($contents)) !== false) |
---|
| 2351 | { |
---|
| 2352 | if ($node == '.' or $node == '..') |
---|
| 2353 | continue; |
---|
| 2354 | if (is_dir($path.'/'.$node)) |
---|
| 2355 | { |
---|
| 2356 | $rmdir &= clear_derivative_cache_rec($path.'/'.$node, $pattern); |
---|
| 2357 | } |
---|
| 2358 | else |
---|
| 2359 | { |
---|
| 2360 | if (preg_match($pattern, $node)) |
---|
| 2361 | { |
---|
| 2362 | unlink($path.'/'.$node); |
---|
| 2363 | } |
---|
| 2364 | elseif ($node=='index.htm') |
---|
| 2365 | { |
---|
| 2366 | $rm_index = true; |
---|
| 2367 | } |
---|
| 2368 | else |
---|
| 2369 | { |
---|
| 2370 | $rmdir = false; |
---|
| 2371 | } |
---|
| 2372 | } |
---|
| 2373 | } |
---|
| 2374 | closedir($contents); |
---|
[12917] | 2375 | |
---|
[12797] | 2376 | if ($rmdir) |
---|
| 2377 | { |
---|
| 2378 | if ($rm_index) |
---|
| 2379 | { |
---|
| 2380 | unlink($path.'/index.htm'); |
---|
| 2381 | } |
---|
| 2382 | clearstatcache(); |
---|
[12820] | 2383 | @rmdir($path); |
---|
[12797] | 2384 | } |
---|
| 2385 | return $rmdir; |
---|
| 2386 | } |
---|
| 2387 | } |
---|
[12906] | 2388 | |
---|
[13052] | 2389 | function delete_element_derivatives($infos, $type='all') |
---|
[12906] | 2390 | { |
---|
[12917] | 2391 | $path = $infos['path']; |
---|
| 2392 | if (!empty($infos['representative_ext'])) |
---|
[12906] | 2393 | { |
---|
[12917] | 2394 | $path = original_to_representative( $path, $infos['representative_ext']); |
---|
[12906] | 2395 | } |
---|
[12917] | 2396 | if (substr_compare($path, '../', 0, 3)==0) |
---|
| 2397 | { |
---|
| 2398 | $path = substr($path, 3); |
---|
| 2399 | } |
---|
[13052] | 2400 | $dot = strrpos($path, '.'); |
---|
| 2401 | if ($type=='all') |
---|
| 2402 | { |
---|
| 2403 | $pattern = '-*'; |
---|
| 2404 | } |
---|
| 2405 | else |
---|
| 2406 | { |
---|
| 2407 | $pattern = '-'.derivative_to_url($type).'*'; |
---|
| 2408 | } |
---|
| 2409 | $path = substr_replace($path, $pattern, $dot, 0); |
---|
[16167] | 2410 | if ( ($glob=glob(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$path)) !== false) |
---|
[12917] | 2411 | { |
---|
[16167] | 2412 | foreach( $glob as $file) |
---|
| 2413 | { |
---|
| 2414 | @unlink($file); |
---|
| 2415 | } |
---|
[12917] | 2416 | } |
---|
[12906] | 2417 | } |
---|
[8247] | 2418 | ?> |
---|