[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
| 3 | // | functions.php | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
| 5 | // | application : PhpWebGallery <http://phpwebgallery.net> | |
---|
| 6 | // | branch : BSF (Best So Far) | |
---|
| 7 | // +-----------------------------------------------------------------------+ |
---|
| 8 | // | file : $RCSfile$ |
---|
| 9 | // | last update : $Date: 2004-10-30 15:42:29 +0000 (Sat, 30 Oct 2004) $ |
---|
| 10 | // | last modifier : $Author: z0rglub $ |
---|
| 11 | // | revision : $Revision: 587 $ |
---|
| 12 | // +-----------------------------------------------------------------------+ |
---|
| 13 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 14 | // | it under the terms of the GNU General Public License as published by | |
---|
| 15 | // | the Free Software Foundation | |
---|
| 16 | // | | |
---|
| 17 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 20 | // | General Public License for more details. | |
---|
| 21 | // | | |
---|
| 22 | // | You should have received a copy of the GNU General Public License | |
---|
| 23 | // | along with this program; if not, write to the Free Software | |
---|
| 24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 25 | // | USA. | |
---|
| 26 | // +-----------------------------------------------------------------------+ |
---|
[2] | 27 | |
---|
[491] | 28 | include(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); |
---|
| 29 | |
---|
[14] | 30 | $tab_ext_create_TN = array ( 'jpg', 'png', 'JPG', 'PNG' ); |
---|
[12] | 31 | |
---|
[26] | 32 | // is_image returns true if the given $filename (including the path) is a |
---|
| 33 | // picture according to its format and its extension. |
---|
| 34 | // As GD library can only generate pictures from jpeg and png files, if you |
---|
| 35 | // ask if the filename is an image for thumbnail creation (second parameter |
---|
| 36 | // set to true), the only authorized formats are jpeg and png. |
---|
[12] | 37 | function is_image( $filename, $create_thumbnail = false ) |
---|
| 38 | { |
---|
[14] | 39 | global $conf, $tab_ext_create_TN; |
---|
[12] | 40 | |
---|
[26] | 41 | if ( is_file( $filename ) ) |
---|
[12] | 42 | { |
---|
| 43 | $size = getimagesize( $filename ); |
---|
| 44 | // $size[2] == 1 means GIF |
---|
| 45 | // $size[2] == 2 means JPG |
---|
| 46 | // $size[2] == 3 means PNG |
---|
| 47 | if ( !$create_thumbnail ) |
---|
| 48 | { |
---|
| 49 | if ( in_array( get_extension( $filename ), $conf['picture_ext'] ) |
---|
| 50 | and ( $size[2] == 1 or $size[2] == 2 or $size[2] == 3 ) ) |
---|
| 51 | { |
---|
[13] | 52 | return true; |
---|
[12] | 53 | } |
---|
| 54 | } |
---|
| 55 | else |
---|
| 56 | { |
---|
| 57 | if ( in_array( get_extension( $filename ), $tab_ext_create_TN ) |
---|
| 58 | and ( $size[2] == 2 or $size[2] == 3 ) ) |
---|
| 59 | { |
---|
[13] | 60 | return true; |
---|
[12] | 61 | } |
---|
| 62 | } |
---|
| 63 | } |
---|
[13] | 64 | return false; |
---|
[12] | 65 | } |
---|
[345] | 66 | |
---|
| 67 | /** |
---|
[467] | 68 | * returns an array with all picture files according to $conf['file_ext'] |
---|
[345] | 69 | * |
---|
| 70 | * @param string $dir |
---|
| 71 | * @return array |
---|
| 72 | */ |
---|
[467] | 73 | function get_pwg_files($dir) |
---|
[345] | 74 | { |
---|
| 75 | global $conf; |
---|
| 76 | |
---|
| 77 | $pictures = array(); |
---|
[467] | 78 | if ($opendir = opendir($dir)) |
---|
[345] | 79 | { |
---|
[467] | 80 | while ($file = readdir($opendir)) |
---|
[345] | 81 | { |
---|
[467] | 82 | if (in_array(get_extension($file), $conf['file_ext'])) |
---|
[345] | 83 | { |
---|
[467] | 84 | array_push($pictures, $file); |
---|
[345] | 85 | } |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | return $pictures; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * returns an array with all thumbnails according to $conf['picture_ext'] |
---|
| 93 | * and $conf['prefix_thumbnail'] |
---|
| 94 | * |
---|
| 95 | * @param string $dir |
---|
| 96 | * @return array |
---|
| 97 | */ |
---|
[467] | 98 | function get_thumb_files($dir) |
---|
[345] | 99 | { |
---|
| 100 | global $conf; |
---|
| 101 | |
---|
[467] | 102 | $prefix_length = strlen($conf['prefix_thumbnail']); |
---|
[345] | 103 | |
---|
| 104 | $thumbnails = array(); |
---|
[467] | 105 | if ($opendir = @opendir($dir.'/thumbnail')) |
---|
[345] | 106 | { |
---|
[467] | 107 | while ($file = readdir($opendir)) |
---|
[345] | 108 | { |
---|
[467] | 109 | if (in_array(get_extension($file), $conf['picture_ext']) |
---|
| 110 | and substr($file, 0, $prefix_length) == $conf['prefix_thumbnail']) |
---|
[345] | 111 | { |
---|
[467] | 112 | array_push($thumbnails, $file); |
---|
[345] | 113 | } |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | return $thumbnails; |
---|
| 117 | } |
---|
| 118 | |
---|
[467] | 119 | /** |
---|
| 120 | * returns an array with representative picture files of a directory |
---|
| 121 | * according to $conf['picture_ext'] |
---|
| 122 | * |
---|
| 123 | * @param string $dir |
---|
| 124 | * @return array |
---|
| 125 | */ |
---|
| 126 | function get_representative_files($dir) |
---|
| 127 | { |
---|
| 128 | global $conf; |
---|
| 129 | |
---|
| 130 | $pictures = array(); |
---|
[563] | 131 | if ($opendir = @opendir($dir.'/pwg_representative')) |
---|
[467] | 132 | { |
---|
| 133 | while ($file = readdir($opendir)) |
---|
| 134 | { |
---|
| 135 | if (in_array(get_extension($file), $conf['picture_ext'])) |
---|
| 136 | { |
---|
| 137 | array_push($pictures, $file); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | return $pictures; |
---|
| 142 | } |
---|
| 143 | |
---|
[12] | 144 | function TN_exists( $dir, $file ) |
---|
| 145 | { |
---|
| 146 | global $conf; |
---|
| 147 | |
---|
| 148 | $filename = get_filename_wo_extension( $file ); |
---|
| 149 | foreach ( $conf['picture_ext'] as $ext ) { |
---|
[19] | 150 | $test = $dir.'/thumbnail/'.$conf['prefix_thumbnail'].$filename.'.'.$ext; |
---|
[12] | 151 | if ( is_file ( $test ) ) |
---|
| 152 | { |
---|
| 153 | return $ext; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | return false; |
---|
[345] | 157 | } |
---|
[2] | 158 | |
---|
[345] | 159 | |
---|
[467] | 160 | // The function delete_site deletes a site and call the function |
---|
| 161 | // delete_categories for each primary category of the site |
---|
[12] | 162 | function delete_site( $id ) |
---|
| 163 | { |
---|
| 164 | // destruction of the categories of the site |
---|
[467] | 165 | $query = ' |
---|
| 166 | SELECT id |
---|
| 167 | FROM '.CATEGORIES_TABLE.' |
---|
| 168 | WHERE site_id = '.$id.' |
---|
| 169 | ;'; |
---|
[587] | 170 | $result = pwg_query($query); |
---|
[467] | 171 | $category_ids = array(); |
---|
| 172 | while ($row = mysql_fetch_array($result)) |
---|
[12] | 173 | { |
---|
[467] | 174 | array_push($category_ids, $row['id']); |
---|
[12] | 175 | } |
---|
[467] | 176 | delete_categories($category_ids); |
---|
[2] | 177 | |
---|
[12] | 178 | // destruction of the site |
---|
[467] | 179 | $query = ' |
---|
| 180 | DELETE FROM '.SITES_TABLE.' |
---|
| 181 | WHERE id = '.$id.' |
---|
| 182 | ;'; |
---|
[587] | 183 | pwg_query($query); |
---|
[12] | 184 | } |
---|
[2] | 185 | |
---|
[345] | 186 | |
---|
[467] | 187 | // The function delete_categories deletes the categories identified by the |
---|
| 188 | // (numeric) key of the array $ids. It also deletes (in the database) : |
---|
| 189 | // - all the elements of the category (delete_elements, see further) |
---|
| 190 | // - all the links between elements and this category |
---|
[12] | 191 | // - all the restrictions linked to the category |
---|
| 192 | // The function works recursively. |
---|
[467] | 193 | function delete_categories($ids) |
---|
[12] | 194 | { |
---|
[498] | 195 | global $counts; |
---|
[521] | 196 | |
---|
| 197 | if (count($ids) == 0) |
---|
| 198 | { |
---|
| 199 | return; |
---|
| 200 | } |
---|
[498] | 201 | |
---|
[467] | 202 | // destruction of all the related elements |
---|
| 203 | $query = ' |
---|
| 204 | SELECT id |
---|
| 205 | FROM '.IMAGES_TABLE.' |
---|
| 206 | WHERE storage_category_id IN ('.implode(',', $ids).') |
---|
| 207 | ;'; |
---|
[587] | 208 | $result = pwg_query($query); |
---|
[467] | 209 | $element_ids = array(); |
---|
| 210 | while ($row = mysql_fetch_array($result)) |
---|
[12] | 211 | { |
---|
[467] | 212 | array_push($element_ids, $row['id']); |
---|
[12] | 213 | } |
---|
[521] | 214 | delete_elements($element_ids); |
---|
[21] | 215 | |
---|
[61] | 216 | // destruction of the links between images and this category |
---|
[467] | 217 | $query = ' |
---|
| 218 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 219 | WHERE category_id IN ('.implode(',', $ids).') |
---|
| 220 | ;'; |
---|
[587] | 221 | pwg_query($query); |
---|
[61] | 222 | |
---|
[21] | 223 | // destruction of the access linked to the category |
---|
[467] | 224 | $query = ' |
---|
| 225 | DELETE FROM '.USER_ACCESS_TABLE.' |
---|
| 226 | WHERE cat_id IN ('.implode(',', $ids).') |
---|
| 227 | ;'; |
---|
[587] | 228 | pwg_query($query); |
---|
[467] | 229 | $query = ' |
---|
| 230 | DELETE FROM '.GROUP_ACCESS_TABLE.' |
---|
| 231 | WHERE cat_id IN ('.implode(',', $ids).') |
---|
| 232 | ;'; |
---|
[587] | 233 | pwg_query($query); |
---|
[21] | 234 | |
---|
[12] | 235 | // destruction of the sub-categories |
---|
[467] | 236 | $query = ' |
---|
| 237 | SELECT id |
---|
| 238 | FROM '.CATEGORIES_TABLE.' |
---|
| 239 | WHERE id_uppercat IN ('.implode(',', $ids).') |
---|
| 240 | ;'; |
---|
[587] | 241 | $result = pwg_query($query); |
---|
[467] | 242 | $subcat_ids = array(); |
---|
| 243 | while($row = mysql_fetch_array($result)) |
---|
[12] | 244 | { |
---|
[467] | 245 | array_push($subcat_ids, $row['id']); |
---|
[12] | 246 | } |
---|
[491] | 247 | if (count($subcat_ids) > 0) |
---|
| 248 | { |
---|
| 249 | delete_categories($subcat_ids); |
---|
| 250 | } |
---|
[21] | 251 | |
---|
[12] | 252 | // destruction of the category |
---|
[467] | 253 | $query = ' |
---|
| 254 | DELETE FROM '.CATEGORIES_TABLE.' |
---|
| 255 | WHERE id IN ('.implode(',', $ids).') |
---|
| 256 | ;'; |
---|
[587] | 257 | pwg_query($query); |
---|
[498] | 258 | |
---|
| 259 | if (isset($counts['del_categories'])) |
---|
| 260 | { |
---|
| 261 | $counts['del_categories']+= count($ids); |
---|
| 262 | } |
---|
[12] | 263 | } |
---|
[345] | 264 | |
---|
[467] | 265 | // The function delete_elements deletes the elements identified by the |
---|
| 266 | // (numeric) values of the array $ids. It also deletes (in the database) : |
---|
| 267 | // - all the comments related to elements |
---|
| 268 | // - all the links between categories and elements |
---|
| 269 | // - all the favorites associated to elements |
---|
| 270 | function delete_elements($ids) |
---|
[12] | 271 | { |
---|
[498] | 272 | global $counts; |
---|
[521] | 273 | |
---|
| 274 | if (count($ids) == 0) |
---|
| 275 | { |
---|
| 276 | return; |
---|
| 277 | } |
---|
[491] | 278 | |
---|
[12] | 279 | // destruction of the comments on the image |
---|
[467] | 280 | $query = ' |
---|
| 281 | DELETE FROM '.COMMENTS_TABLE.' |
---|
[491] | 282 | WHERE image_id IN ( |
---|
| 283 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 284 | ;'; |
---|
[587] | 285 | pwg_query($query); |
---|
[61] | 286 | |
---|
| 287 | // destruction of the links between images and this category |
---|
[467] | 288 | $query = ' |
---|
| 289 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
[491] | 290 | WHERE image_id IN ( |
---|
| 291 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 292 | ;'; |
---|
[587] | 293 | pwg_query($query); |
---|
[61] | 294 | |
---|
[12] | 295 | // destruction of the favorites associated with the picture |
---|
[467] | 296 | $query = ' |
---|
| 297 | DELETE FROM '.FAVORITES_TABLE.' |
---|
[491] | 298 | WHERE image_id IN ( |
---|
| 299 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 300 | ;'; |
---|
[587] | 301 | pwg_query($query); |
---|
[523] | 302 | |
---|
| 303 | // destruction of the rates associated to this element |
---|
| 304 | $query = ' |
---|
| 305 | DELETE FROM '.RATE_TABLE.' |
---|
| 306 | WHERE element_id IN ( |
---|
| 307 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
| 308 | ;'; |
---|
[587] | 309 | pwg_query($query); |
---|
[2] | 310 | |
---|
[12] | 311 | // destruction of the image |
---|
[467] | 312 | $query = ' |
---|
| 313 | DELETE FROM '.IMAGES_TABLE.' |
---|
[491] | 314 | WHERE id IN ( |
---|
| 315 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
[467] | 316 | ;'; |
---|
[587] | 317 | pwg_query($query); |
---|
[491] | 318 | |
---|
[498] | 319 | if (isset($counts['del_elements'])) |
---|
| 320 | { |
---|
| 321 | $counts['del_elements']+= count($ids); |
---|
| 322 | } |
---|
[12] | 323 | } |
---|
[345] | 324 | |
---|
[12] | 325 | // The delete_user function delete a user identified by the $user_id |
---|
| 326 | // It also deletes : |
---|
[21] | 327 | // - all the access linked to this user |
---|
| 328 | // - all the links to any group |
---|
[12] | 329 | // - all the favorites linked to this user |
---|
[21] | 330 | // - all sessions linked to this user |
---|
[345] | 331 | // - all categories informations linked to this user |
---|
[12] | 332 | function delete_user( $user_id ) |
---|
| 333 | { |
---|
[21] | 334 | // destruction of the access linked to the user |
---|
| 335 | $query = 'DELETE FROM '.PREFIX_TABLE.'user_access'; |
---|
[12] | 336 | $query.= ' WHERE user_id = '.$user_id; |
---|
| 337 | $query.= ';'; |
---|
[587] | 338 | pwg_query( $query ); |
---|
[21] | 339 | |
---|
| 340 | // destruction of the group links for this user |
---|
| 341 | $query = 'DELETE FROM '.PREFIX_TABLE.'user_group'; |
---|
| 342 | $query.= ' WHERE user_id = '.$user_id; |
---|
| 343 | $query.= ';'; |
---|
[587] | 344 | pwg_query( $query ); |
---|
[21] | 345 | |
---|
[12] | 346 | // destruction of the favorites associated with the user |
---|
| 347 | $query = 'DELETE FROM '.PREFIX_TABLE.'favorites'; |
---|
| 348 | $query.= ' WHERE user_id = '.$user_id; |
---|
| 349 | $query.= ';'; |
---|
[587] | 350 | pwg_query( $query ); |
---|
[21] | 351 | |
---|
| 352 | // destruction of the sessions linked with the user |
---|
| 353 | $query = 'DELETE FROM '.PREFIX_TABLE.'sessions'; |
---|
| 354 | $query.= ' WHERE user_id = '.$user_id; |
---|
| 355 | $query.= ';'; |
---|
[587] | 356 | pwg_query( $query ); |
---|
[345] | 357 | |
---|
[12] | 358 | // destruction of the user |
---|
[364] | 359 | $query = 'DELETE FROM '.USERS_TABLE; |
---|
[12] | 360 | $query.= ' WHERE id = '.$user_id; |
---|
| 361 | $query.= ';'; |
---|
[587] | 362 | pwg_query( $query ); |
---|
[12] | 363 | } |
---|
[21] | 364 | |
---|
| 365 | // delete_group deletes a group identified by its $group_id. |
---|
| 366 | // It also deletes : |
---|
| 367 | // - all the access linked to this group |
---|
| 368 | // - all the links between this group and any user |
---|
| 369 | function delete_group( $group_id ) |
---|
| 370 | { |
---|
| 371 | // destruction of the access linked to the group |
---|
| 372 | $query = 'DELETE FROM '.PREFIX_TABLE.'group_access'; |
---|
| 373 | $query.= ' WHERE group_id = '.$group_id; |
---|
| 374 | $query.= ';'; |
---|
[587] | 375 | pwg_query( $query ); |
---|
[21] | 376 | |
---|
[345] | 377 | // synchronize all users linked to the group |
---|
| 378 | synchronize_group( $group_id ); |
---|
| 379 | |
---|
| 380 | // destruction of the users links for this group |
---|
[21] | 381 | $query = 'DELETE FROM '.PREFIX_TABLE.'user_group'; |
---|
| 382 | $query.= ' WHERE group_id = '.$group_id; |
---|
| 383 | $query.= ';'; |
---|
[587] | 384 | pwg_query( $query ); |
---|
[21] | 385 | |
---|
| 386 | // destruction of the group |
---|
| 387 | $query = 'DELETE FROM '.PREFIX_TABLE.'groups'; |
---|
| 388 | $query.= ' WHERE id = '.$group_id; |
---|
| 389 | $query.= ';'; |
---|
[587] | 390 | pwg_query( $query ); |
---|
[21] | 391 | } |
---|
| 392 | |
---|
[12] | 393 | // The check_favorites function deletes all the favorites of a user if he is |
---|
| 394 | // not allowed to see them (the category or an upper category is restricted |
---|
| 395 | // or invisible) |
---|
| 396 | function check_favorites( $user_id ) |
---|
| 397 | { |
---|
[345] | 398 | $query = 'SELECT status,forbidden_categories'; |
---|
[364] | 399 | $query.= ' FROM '.USERS_TABLE; |
---|
[12] | 400 | $query.= ' WHERE id = '.$user_id; |
---|
| 401 | $query.= ';'; |
---|
[587] | 402 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
[12] | 403 | $status = $row['status']; |
---|
| 404 | // retrieving all the restricted categories for this user |
---|
[345] | 405 | if ( isset( $row['forbidden_categories'] ) ) |
---|
| 406 | $restricted_cat = explode( ',', $row['forbidden_categories'] ); |
---|
| 407 | else |
---|
| 408 | $restricted_cat = array(); |
---|
[12] | 409 | // retrieving all the favorites for this user and comparing their |
---|
| 410 | // categories to the restricted categories |
---|
[61] | 411 | $query = 'SELECT image_id'; |
---|
| 412 | $query.= ' FROM '.PREFIX_TABLE.'favorites'; |
---|
[12] | 413 | $query.= ' WHERE user_id = '.$user_id; |
---|
| 414 | $query.= ';'; |
---|
[587] | 415 | $result = pwg_query ( $query ); |
---|
[12] | 416 | while ( $row = mysql_fetch_array( $result ) ) |
---|
| 417 | { |
---|
[61] | 418 | // for each picture, we have to check all the categories it belongs |
---|
| 419 | // to. Indeed if a picture belongs to category_1 and category_2 and that |
---|
| 420 | // category_2 is not restricted to the user, he can have the picture as |
---|
| 421 | // favorite. |
---|
| 422 | $query = 'SELECT DISTINCT(category_id) as category_id'; |
---|
| 423 | $query.= ' FROM '.PREFIX_TABLE.'image_category'; |
---|
| 424 | $query.= ' WHERE image_id = '.$row['image_id']; |
---|
| 425 | $query.= ';'; |
---|
[587] | 426 | $picture_result = pwg_query( $query ); |
---|
[61] | 427 | $picture_cat = array(); |
---|
| 428 | while ( $picture_row = mysql_fetch_array( $picture_result ) ) |
---|
[12] | 429 | { |
---|
[61] | 430 | array_push( $picture_cat, $picture_row['category_id'] ); |
---|
| 431 | } |
---|
[167] | 432 | if ( count( array_diff( $picture_cat, $restricted_cat ) ) == 0 ) |
---|
[61] | 433 | { |
---|
[12] | 434 | $query = 'DELETE FROM '.PREFIX_TABLE.'favorites'; |
---|
| 435 | $query.= ' WHERE image_id = '.$row['image_id']; |
---|
| 436 | $query.= ' AND user_id = '.$user_id; |
---|
| 437 | $query.= ';'; |
---|
[587] | 438 | pwg_query( $query ); |
---|
[12] | 439 | } |
---|
| 440 | } |
---|
| 441 | } |
---|
[61] | 442 | |
---|
[491] | 443 | /** |
---|
| 444 | * updates calculated informations about a category : date_last and |
---|
| 445 | * nb_images. It also verifies that the representative picture is really |
---|
| 446 | * linked to the category. Recursive. |
---|
| 447 | * |
---|
| 448 | * @param mixed category id |
---|
| 449 | * @returns void |
---|
| 450 | */ |
---|
| 451 | function update_category($id = 'all') |
---|
[61] | 452 | { |
---|
[491] | 453 | $cat_ids = array(); |
---|
| 454 | |
---|
| 455 | $query = ' |
---|
| 456 | SELECT category_id, COUNT(image_id) AS count, max(date_available) AS date_last |
---|
| 457 | FROM '.IMAGES_TABLE.' |
---|
| 458 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id'; |
---|
| 459 | if (is_numeric($id)) |
---|
[61] | 460 | { |
---|
[491] | 461 | $query.= ' |
---|
| 462 | WHERE uppercats REGEXP \'(^|,)'.$id.'(,|$)\''; |
---|
[61] | 463 | } |
---|
[491] | 464 | $query.= ' |
---|
| 465 | GROUP BY category_id |
---|
| 466 | ;'; |
---|
[587] | 467 | $result = pwg_query( $query ); |
---|
[491] | 468 | while ( $row = mysql_fetch_array( $result ) ) |
---|
[61] | 469 | { |
---|
[491] | 470 | array_push($cat_ids, $row['category_id']); |
---|
| 471 | $query = ' |
---|
| 472 | UPDATE '.CATEGORIES_TABLE.' |
---|
| 473 | SET date_last = \''.$row['date_last'].'\' |
---|
| 474 | , nb_images = '.$row['count'].' |
---|
| 475 | WHERE id = '.$row['category_id'].' |
---|
| 476 | ;'; |
---|
[587] | 477 | pwg_query($query); |
---|
[491] | 478 | } |
---|
[345] | 479 | |
---|
[491] | 480 | if (count($cat_ids) > 0) |
---|
| 481 | { |
---|
| 482 | $query = ' |
---|
| 483 | SELECT id, representative_picture_id |
---|
| 484 | FROM '.CATEGORIES_TABLE.' |
---|
| 485 | WHERE representative_picture_id IS NOT NULL |
---|
| 486 | AND id IN ('.implode(',', $cat_ids).') |
---|
| 487 | ;'; |
---|
[587] | 488 | $result = pwg_query( $query ); |
---|
[491] | 489 | while ( $row = mysql_fetch_array( $result ) ) |
---|
[133] | 490 | { |
---|
[491] | 491 | $query = ' |
---|
| 492 | SELECT image_id |
---|
| 493 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 494 | WHERE category_id = '.$row['id'].' |
---|
| 495 | AND image_id = '.$row['representative_picture_id'].' |
---|
| 496 | ;'; |
---|
[587] | 497 | $result = pwg_query( $query ); |
---|
[491] | 498 | if (mysql_num_rows($result) == 0) |
---|
[133] | 499 | { |
---|
[491] | 500 | $query = ' |
---|
| 501 | UPDATE '.CATEGORIES_TABLE.' |
---|
| 502 | SET representative_picture_id = NULL |
---|
| 503 | WHERE id = '.$row['id'].' |
---|
| 504 | ;'; |
---|
[587] | 505 | pwg_query( $query ); |
---|
[133] | 506 | } |
---|
| 507 | } |
---|
[61] | 508 | } |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | function check_date_format( $date ) |
---|
| 512 | { |
---|
| 513 | // date arrives at this format : DD/MM/YYYY |
---|
[345] | 514 | @list($day,$month,$year) = explode( '/', $date ); |
---|
[196] | 515 | return @checkdate( $month, $day, $year ); |
---|
[61] | 516 | } |
---|
| 517 | |
---|
| 518 | function date_convert( $date ) |
---|
| 519 | { |
---|
| 520 | // date arrives at this format : DD/MM/YYYY |
---|
| 521 | // It must be transformed in YYYY-MM-DD |
---|
| 522 | list($day,$month,$year) = explode( '/', $date ); |
---|
| 523 | return $year.'-'.$month.'-'.$day; |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | function date_convert_back( $date ) |
---|
| 527 | { |
---|
| 528 | // date arrives at this format : YYYY-MM-DD |
---|
| 529 | // It must be transformed in DD/MM/YYYY |
---|
| 530 | if ( $date != '' ) |
---|
| 531 | { |
---|
| 532 | list($year,$month,$day) = explode( '-', $date ); |
---|
| 533 | return $day.'/'.$month.'/'.$year; |
---|
| 534 | } |
---|
| 535 | else |
---|
| 536 | { |
---|
| 537 | return ''; |
---|
| 538 | } |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | // get_keywords returns an array with relevant keywords found in the string |
---|
| 542 | // given in argument. Keywords must be separated by comma in this string. |
---|
| 543 | // keywords must : |
---|
| 544 | // - be longer or equal to 3 characters |
---|
| 545 | // - not contain ', " or blank characters |
---|
| 546 | // - unique in the string ("test,test" -> "test") |
---|
| 547 | function get_keywords( $keywords_string ) |
---|
| 548 | { |
---|
| 549 | $keywords = array(); |
---|
| 550 | |
---|
| 551 | $candidates = explode( ',', $keywords_string ); |
---|
| 552 | foreach ( $candidates as $candidate ) { |
---|
| 553 | if ( strlen($candidate) >= 3 and !preg_match( '/(\'|"|\s)/', $candidate ) ) |
---|
| 554 | array_push( $keywords, $candidate ); |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | return array_unique( $keywords ); |
---|
| 558 | } |
---|
| 559 | |
---|
[68] | 560 | function display_categories( $categories, $indent, |
---|
| 561 | $selected = -1, $forbidden = -1 ) |
---|
[61] | 562 | { |
---|
| 563 | global $vtp,$sub; |
---|
| 564 | |
---|
| 565 | foreach ( $categories as $category ) { |
---|
[68] | 566 | if ( $category['id'] != $forbidden ) |
---|
| 567 | { |
---|
| 568 | $vtp->addSession( $sub, 'associate_cat' ); |
---|
| 569 | $vtp->setVar( $sub, 'associate_cat.value', $category['id'] ); |
---|
| 570 | $content = $indent.'- '.$category['name']; |
---|
| 571 | $vtp->setVar( $sub, 'associate_cat.content', $content ); |
---|
| 572 | if ( $category['id'] == $selected ) |
---|
| 573 | $vtp->setVar( $sub, 'associate_cat.selected', ' selected="selected"' ); |
---|
| 574 | $vtp->closeSession( $sub, 'associate_cat' ); |
---|
| 575 | display_categories( $category['subcats'], $indent.str_repeat(' ',3), |
---|
| 576 | $selected, $forbidden ); |
---|
| 577 | } |
---|
[61] | 578 | } |
---|
| 579 | } |
---|
[345] | 580 | |
---|
| 581 | /** |
---|
| 582 | * returns an array with the ids of the restricted categories for the user |
---|
| 583 | * |
---|
| 584 | * Returns an array with the ids of the restricted categories for the |
---|
| 585 | * user. If the $check_invisible parameter is set to true, invisible |
---|
| 586 | * categorie are added to the restricted one in the array. |
---|
| 587 | * |
---|
| 588 | * @param int $user_id |
---|
| 589 | * @param string $user_status |
---|
| 590 | * @param bool $check_invisible |
---|
| 591 | * @param bool $use_groups |
---|
| 592 | * @return array |
---|
| 593 | */ |
---|
| 594 | function get_user_restrictions( $user_id, $user_status, |
---|
| 595 | $check_invisible, $use_groups = true ) |
---|
| 596 | { |
---|
| 597 | // 1. retrieving ids of private categories |
---|
[393] | 598 | $query = 'SELECT id FROM '.CATEGORIES_TABLE; |
---|
[345] | 599 | $query.= " WHERE status = 'private'"; |
---|
| 600 | $query.= ';'; |
---|
[587] | 601 | $result = pwg_query( $query ); |
---|
[345] | 602 | $privates = array(); |
---|
| 603 | while ( $row = mysql_fetch_array( $result ) ) |
---|
| 604 | { |
---|
| 605 | array_push( $privates, $row['id'] ); |
---|
| 606 | } |
---|
| 607 | // 2. retrieving all authorized categories for the user |
---|
| 608 | $authorized = array(); |
---|
| 609 | // 2.1. retrieving authorized categories thanks to personnal user |
---|
| 610 | // authorization |
---|
[393] | 611 | $query = 'SELECT cat_id FROM '.USER_ACCESS_TABLE; |
---|
[345] | 612 | $query.= ' WHERE user_id = '.$user_id; |
---|
| 613 | $query.= ';'; |
---|
[587] | 614 | $result = pwg_query( $query ); |
---|
[345] | 615 | while ( $row = mysql_fetch_array( $result ) ) |
---|
| 616 | { |
---|
| 617 | array_push( $authorized, $row['cat_id'] ); |
---|
| 618 | } |
---|
| 619 | // 2.2. retrieving authorized categories thanks to group authorization to |
---|
| 620 | // which the user is a member |
---|
| 621 | if ( $use_groups ) |
---|
| 622 | { |
---|
| 623 | $query = 'SELECT ga.cat_id'; |
---|
[393] | 624 | $query.= ' FROM '.USER_GROUP_TABLE.' as ug'; |
---|
| 625 | $query.= ', '.GROUP_ACCESS_TABLE.' as ga'; |
---|
[345] | 626 | $query.= ' WHERE ug.group_id = ga.group_id'; |
---|
| 627 | $query.= ' AND ug.user_id = '.$user_id; |
---|
| 628 | $query.= ';'; |
---|
[587] | 629 | $result = pwg_query( $query ); |
---|
[345] | 630 | while ( $row = mysql_fetch_array( $result ) ) |
---|
| 631 | { |
---|
| 632 | array_push( $authorized, $row['cat_id'] ); |
---|
| 633 | } |
---|
| 634 | $authorized = array_unique( $authorized ); |
---|
| 635 | } |
---|
| 636 | |
---|
| 637 | $forbidden = array(); |
---|
| 638 | foreach ( $privates as $private ) { |
---|
| 639 | if ( !in_array( $private, $authorized ) ) |
---|
| 640 | { |
---|
| 641 | array_push( $forbidden, $private ); |
---|
| 642 | } |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | if ( $check_invisible ) |
---|
| 646 | { |
---|
| 647 | // 3. adding to the restricted categories, the invisible ones |
---|
| 648 | if ( $user_status != 'admin' ) |
---|
| 649 | { |
---|
[393] | 650 | $query = 'SELECT id FROM '.CATEGORIES_TABLE; |
---|
[345] | 651 | $query.= " WHERE visible = 'false';"; |
---|
[587] | 652 | $result = pwg_query( $query ); |
---|
[345] | 653 | while ( $row = mysql_fetch_array( $result ) ) |
---|
| 654 | { |
---|
| 655 | array_push( $forbidden, $row['id'] ); |
---|
| 656 | } |
---|
| 657 | } |
---|
| 658 | } |
---|
| 659 | return array_unique( $forbidden ); |
---|
| 660 | } |
---|
| 661 | |
---|
| 662 | /** |
---|
| 663 | * updates the calculated data users.forbidden_categories, it includes |
---|
| 664 | * sub-categories of the direct forbidden categories |
---|
| 665 | * |
---|
| 666 | * @param nt $user_id |
---|
| 667 | * @return array |
---|
| 668 | */ |
---|
| 669 | function update_user_restrictions( $user_id ) |
---|
| 670 | { |
---|
| 671 | $restrictions = get_user_all_restrictions( $user_id ); |
---|
| 672 | |
---|
| 673 | // update the users.forbidden_categories in database |
---|
[364] | 674 | $query = 'UPDATE '.USERS_TABLE; |
---|
[345] | 675 | $query.= ' SET forbidden_categories = '; |
---|
| 676 | if ( count( $restrictions ) > 0 ) |
---|
| 677 | $query.= "'".implode( ',', $restrictions )."'"; |
---|
| 678 | else |
---|
| 679 | $query.= 'NULL'; |
---|
| 680 | $query .= ' WHERE id = '.$user_id; |
---|
| 681 | $query.= ';'; |
---|
[587] | 682 | pwg_query( $query ); |
---|
[345] | 683 | |
---|
| 684 | return $restrictions; |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | /** |
---|
| 688 | * returns all the restricted categories ids including sub-categories |
---|
| 689 | * |
---|
| 690 | * @param int $user_id |
---|
| 691 | * @return array |
---|
| 692 | */ |
---|
| 693 | function get_user_all_restrictions( $user_id ) |
---|
| 694 | { |
---|
| 695 | global $page; |
---|
| 696 | |
---|
| 697 | $query = 'SELECT status'; |
---|
[364] | 698 | $query.= ' FROM '.USERS_TABLE; |
---|
[345] | 699 | $query.= ' WHERE id = '.$user_id; |
---|
| 700 | $query.= ';'; |
---|
[587] | 701 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
[345] | 702 | |
---|
| 703 | $base_restrictions=get_user_restrictions($user_id,$row['status'],true,true); |
---|
| 704 | |
---|
| 705 | $restrictions = $base_restrictions; |
---|
| 706 | foreach ( $base_restrictions as $category_id ) { |
---|
| 707 | echo $category_id.' is forbidden to user '.$user_id.'<br />'; |
---|
| 708 | $restrictions = |
---|
| 709 | array_merge( $restrictions, |
---|
| 710 | $page['plain_structure'][$category_id]['all_subcats_ids'] ); |
---|
| 711 | } |
---|
| 712 | |
---|
| 713 | return array_unique( $restrictions ); |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | // The function is_user_allowed returns : |
---|
| 717 | // - 0 : if the category is allowed with this $restrictions array |
---|
| 718 | // - 1 : if this category is not allowed |
---|
| 719 | // - 2 : if an uppercat category is not allowed |
---|
| 720 | // Note : the restrictions array must represent ONLY direct forbidden |
---|
| 721 | // categories, not all forbidden categories |
---|
| 722 | function is_user_allowed( $category_id, $restrictions ) |
---|
| 723 | { |
---|
| 724 | if ( in_array( $category_id, $restrictions ) ) return 1; |
---|
| 725 | |
---|
| 726 | $query = 'SELECT uppercats'; |
---|
[393] | 727 | $query.= ' FROM '.CATEGORIES_TABLE; |
---|
[345] | 728 | $query.= ' WHERE id = '.$category_id; |
---|
| 729 | $query.= ';'; |
---|
[587] | 730 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
[345] | 731 | $uppercats = explode( ',', $row['uppercats'] ); |
---|
| 732 | foreach ( $uppercats as $category_id ) { |
---|
| 733 | if ( in_array( $category_id, $restrictions ) ) return 2; |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | // no restriction found : the user is allowed to access this category |
---|
| 737 | return 0; |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | /** |
---|
| 741 | * returns an array containing sub-directories which can be a category |
---|
| 742 | * |
---|
[563] | 743 | * directories nammed "thumbnail", "pwg_high" or "pwg_representative" are |
---|
| 744 | * omitted |
---|
[345] | 745 | * |
---|
| 746 | * @param string $basedir |
---|
| 747 | * @return array |
---|
| 748 | */ |
---|
| 749 | function get_category_directories( $basedir ) |
---|
| 750 | { |
---|
| 751 | $sub_dirs = array(); |
---|
| 752 | |
---|
| 753 | if ( $opendir = opendir( $basedir ) ) |
---|
| 754 | { |
---|
| 755 | while ( $file = readdir( $opendir ) ) |
---|
| 756 | { |
---|
[540] | 757 | if ($file != '.' |
---|
| 758 | and $file != '..' |
---|
| 759 | and $file != 'thumbnail' |
---|
[563] | 760 | and $file != 'pwg_high' |
---|
| 761 | and $file != 'pwg_representative' |
---|
[540] | 762 | and is_dir($basedir.'/'.$file)) |
---|
[345] | 763 | { |
---|
| 764 | array_push( $sub_dirs, $file ); |
---|
| 765 | } |
---|
| 766 | } |
---|
| 767 | } |
---|
| 768 | return $sub_dirs; |
---|
| 769 | } |
---|
[491] | 770 | |
---|
| 771 | // my_error returns (or send to standard output) the message concerning the |
---|
| 772 | // error occured for the last mysql query. |
---|
| 773 | function my_error($header, $echo = true) |
---|
| 774 | { |
---|
| 775 | $error = $header.'<span style="font-weight:bold;">N°= '.mysql_errno(); |
---|
| 776 | $error.= ' -->> '.mysql_error()."</span><br /><br />\n"; |
---|
| 777 | if ($echo) |
---|
| 778 | { |
---|
| 779 | echo $error; |
---|
| 780 | } |
---|
| 781 | else |
---|
| 782 | { |
---|
| 783 | return $error; |
---|
| 784 | } |
---|
| 785 | } |
---|
[345] | 786 | ?> |
---|