[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[593] | 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[675] | 5 | // | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[362] | 6 | // +-----------------------------------------------------------------------+ |
---|
[593] | 7 | // | branch : BSF (Best So Far) |
---|
[362] | 8 | // | file : $RCSfile$ |
---|
| 9 | // | last update : $Date: 2006-03-22 01:01:47 +0000 (Wed, 22 Mar 2006) $ |
---|
| 10 | // | last modifier : $Author: rvelices $ |
---|
| 11 | // | revision : $Revision: 1092 $ |
---|
| 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 | |
---|
[423] | 28 | /** |
---|
| 29 | * Provides functions to handle categories. |
---|
| 30 | * |
---|
[1059] | 31 | * |
---|
[423] | 32 | */ |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * Is the category accessible to the connected user ? |
---|
| 36 | * |
---|
| 37 | * Note : if the user is not authorized to see this category, page creation |
---|
| 38 | * ends (exit command in this function) |
---|
| 39 | * |
---|
| 40 | * @param int category id to verify |
---|
| 41 | * @return void |
---|
| 42 | */ |
---|
[808] | 43 | function check_restrictions($category_id) |
---|
[2] | 44 | { |
---|
[808] | 45 | global $user, $lang; |
---|
[2] | 46 | |
---|
[808] | 47 | if (in_array($category_id, explode(',', $user['forbidden_categories']))) |
---|
[2] | 48 | { |
---|
[1082] | 49 | $login_url = |
---|
[1092] | 50 | get_root_url().'identification.php?redirect=' |
---|
[1082] | 51 | .urlencode(urlencode($_SERVER['REQUEST_URI'])); |
---|
[1092] | 52 | |
---|
[1082] | 53 | if (!$user['is_the_guest']) |
---|
[1052] | 54 | { |
---|
[1082] | 55 | die('Fatal: you are trying to reach a forbidden category'); |
---|
[1052] | 56 | } |
---|
| 57 | else |
---|
| 58 | { |
---|
| 59 | redirect($login_url); |
---|
| 60 | } |
---|
[2] | 61 | } |
---|
| 62 | } |
---|
[345] | 63 | |
---|
[614] | 64 | function get_categories_menu() |
---|
[2] | 65 | { |
---|
[345] | 66 | global $page,$user; |
---|
[1059] | 67 | |
---|
[614] | 68 | $infos = array(''); |
---|
[1059] | 69 | |
---|
[589] | 70 | $query = ' |
---|
[614] | 71 | SELECT name,id,date_last,nb_images,global_rank |
---|
[589] | 72 | FROM '.CATEGORIES_TABLE.' |
---|
| 73 | WHERE 1 = 1'; // stupid but permit using AND after it ! |
---|
| 74 | if (!$user['expand']) |
---|
[345] | 75 | { |
---|
[589] | 76 | $query.= ' |
---|
| 77 | AND (id_uppercat is NULL'; |
---|
[1084] | 78 | if (isset($page['category'])) |
---|
[345] | 79 | { |
---|
[1031] | 80 | $query.= ' OR id_uppercat IN ('.$page['uppercats'].')'; |
---|
[345] | 81 | } |
---|
| 82 | $query.= ')'; |
---|
[2] | 83 | } |
---|
[589] | 84 | if ($user['forbidden_categories'] != '') |
---|
[345] | 85 | { |
---|
[589] | 86 | $query.= ' |
---|
| 87 | AND id NOT IN ('.$user['forbidden_categories'].')'; |
---|
[345] | 88 | } |
---|
[589] | 89 | $query.= ' |
---|
| 90 | ;'; |
---|
[26] | 91 | |
---|
[589] | 92 | $result = pwg_query($query); |
---|
[614] | 93 | $cats = array(); |
---|
[589] | 94 | while ($row = mysql_fetch_array($result)) |
---|
[2] | 95 | { |
---|
[614] | 96 | array_push($cats, $row); |
---|
[26] | 97 | } |
---|
[614] | 98 | usort($cats, 'global_rank_compare'); |
---|
[2] | 99 | |
---|
[614] | 100 | return get_html_menu_category($cats); |
---|
[26] | 101 | } |
---|
[2] | 102 | |
---|
[761] | 103 | /** |
---|
[423] | 104 | * Retrieve informations about a category in the database |
---|
| 105 | * |
---|
| 106 | * Returns an array with following keys : |
---|
| 107 | * |
---|
| 108 | * - comment |
---|
| 109 | * - dir : directory, might be empty for virtual categories |
---|
| 110 | * - name : an array with indexes from 0 (lowest cat name) to n (most |
---|
| 111 | * uppercat name findable) |
---|
| 112 | * - nb_images |
---|
| 113 | * - id_uppercat |
---|
| 114 | * - site_id |
---|
[1059] | 115 | * - |
---|
[423] | 116 | * |
---|
| 117 | * @param int category id |
---|
| 118 | * @return array |
---|
| 119 | */ |
---|
[2] | 120 | function get_cat_info( $id ) |
---|
| 121 | { |
---|
[603] | 122 | $infos = array('nb_images','id_uppercat','comment','site_id' |
---|
| 123 | ,'dir','date_last','uploadable','status','visible' |
---|
| 124 | ,'representative_picture_id','uppercats','commentable'); |
---|
[1059] | 125 | |
---|
[603] | 126 | $query = ' |
---|
| 127 | SELECT '.implode(',', $infos).' |
---|
| 128 | FROM '.CATEGORIES_TABLE.' |
---|
| 129 | WHERE id = '.$id.' |
---|
| 130 | ;'; |
---|
| 131 | $row = mysql_fetch_array(pwg_query($query)); |
---|
[345] | 132 | |
---|
| 133 | $cat = array(); |
---|
[603] | 134 | foreach ($infos as $info) |
---|
| 135 | { |
---|
| 136 | if (isset($row[$info])) |
---|
| 137 | { |
---|
| 138 | $cat[$info] = $row[$info]; |
---|
| 139 | } |
---|
| 140 | else |
---|
| 141 | { |
---|
| 142 | $cat[$info] = ''; |
---|
| 143 | } |
---|
[345] | 144 | // If the field is true or false, the variable is transformed into a |
---|
| 145 | // boolean value. |
---|
[603] | 146 | if ($cat[$info] == 'true' or $cat[$info] == 'false') |
---|
[345] | 147 | { |
---|
| 148 | $cat[$info] = get_boolean( $cat[$info] ); |
---|
| 149 | } |
---|
| 150 | } |
---|
[1046] | 151 | global $conf; |
---|
| 152 | if ( !( $conf['allow_html_descriptions'] and |
---|
| 153 | preg_match('/<(div|br|img).*>/i', $cat['comment']) ) ) |
---|
| 154 | { |
---|
| 155 | $cat['comment'] = nl2br($cat['comment']); |
---|
| 156 | } |
---|
[345] | 157 | |
---|
[672] | 158 | $names = array(); |
---|
[603] | 159 | $query = ' |
---|
| 160 | SELECT name,id |
---|
| 161 | FROM '.CATEGORIES_TABLE.' |
---|
| 162 | WHERE id IN ('.$cat['uppercats'].') |
---|
| 163 | ;'; |
---|
| 164 | $result = pwg_query($query); |
---|
| 165 | while($row = mysql_fetch_array($result)) |
---|
[2] | 166 | { |
---|
[672] | 167 | $names[$row['id']] = $row['name']; |
---|
[2] | 168 | } |
---|
[672] | 169 | |
---|
| 170 | // category names must be in the same order than uppercats list |
---|
| 171 | $cat['name'] = array(); |
---|
| 172 | foreach (explode(',', $cat['uppercats']) as $cat_id) |
---|
| 173 | { |
---|
| 174 | $cat['name'][$cat_id] = $names[$cat_id]; |
---|
| 175 | } |
---|
[1059] | 176 | |
---|
[2] | 177 | return $cat; |
---|
| 178 | } |
---|
[61] | 179 | |
---|
| 180 | // get_complete_dir returns the concatenation of get_site_url and |
---|
| 181 | // get_local_dir |
---|
| 182 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
| 183 | // PhpWebGallery files and this category has 22 for identifier |
---|
| 184 | // get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/" |
---|
| 185 | function get_complete_dir( $category_id ) |
---|
| 186 | { |
---|
[579] | 187 | return get_site_url($category_id).get_local_dir($category_id); |
---|
[61] | 188 | } |
---|
| 189 | |
---|
| 190 | // get_local_dir returns an array with complete path without the site url |
---|
| 191 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
| 192 | // PhpWebGallery files and this category has 22 for identifier |
---|
| 193 | // get_local_dir(22) returns "pets/rex/1_year_old/" |
---|
| 194 | function get_local_dir( $category_id ) |
---|
| 195 | { |
---|
| 196 | global $page; |
---|
| 197 | |
---|
[345] | 198 | $uppercats = ''; |
---|
| 199 | $local_dir = ''; |
---|
| 200 | |
---|
| 201 | if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) ) |
---|
[61] | 202 | { |
---|
[345] | 203 | $uppercats = $page['plain_structure'][$category_id]['uppercats']; |
---|
[61] | 204 | } |
---|
[345] | 205 | else |
---|
| 206 | { |
---|
| 207 | $query = 'SELECT uppercats'; |
---|
| 208 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id; |
---|
| 209 | $query.= ';'; |
---|
[587] | 210 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
[345] | 211 | $uppercats = $row['uppercats']; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | $upper_array = explode( ',', $uppercats ); |
---|
| 215 | |
---|
| 216 | $database_dirs = array(); |
---|
| 217 | $query = 'SELECT id,dir'; |
---|
| 218 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')'; |
---|
| 219 | $query.= ';'; |
---|
[587] | 220 | $result = pwg_query( $query ); |
---|
[345] | 221 | while( $row = mysql_fetch_array( $result ) ) |
---|
| 222 | { |
---|
| 223 | $database_dirs[$row['id']] = $row['dir']; |
---|
| 224 | } |
---|
[579] | 225 | foreach ($upper_array as $id) |
---|
| 226 | { |
---|
[345] | 227 | $local_dir.= $database_dirs[$id].'/'; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | return $local_dir; |
---|
[61] | 231 | } |
---|
| 232 | |
---|
| 233 | // retrieving the site url : "http://domain.com/gallery/" or |
---|
| 234 | // simply "./galleries/" |
---|
[579] | 235 | function get_site_url($category_id) |
---|
[61] | 236 | { |
---|
| 237 | global $page; |
---|
| 238 | |
---|
[579] | 239 | $query = ' |
---|
| 240 | SELECT galleries_url |
---|
| 241 | FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c |
---|
| 242 | WHERE s.id = c.site_id |
---|
| 243 | AND c.id = '.$category_id.' |
---|
| 244 | ;'; |
---|
[587] | 245 | $row = mysql_fetch_array(pwg_query($query)); |
---|
[61] | 246 | return $row['galleries_url']; |
---|
| 247 | } |
---|
| 248 | |
---|
[1020] | 249 | // returns an array of image orders available for users/visitors |
---|
| 250 | function get_category_preferred_image_orders() |
---|
| 251 | { |
---|
[1029] | 252 | global $conf; |
---|
[1020] | 253 | return array( |
---|
[1059] | 254 | array(l10n('default_sort'), '', true), |
---|
[1042] | 255 | array(l10n('Average rate'), 'average_rate DESC', $conf['rate']), |
---|
[1031] | 256 | array(l10n('most_visited_cat'), 'hit DESC', true), |
---|
| 257 | array(l10n('Creation date'), 'date_creation DESC', true), |
---|
[1059] | 258 | array(l10n('Post date'), 'date_available DESC', true), |
---|
[1031] | 259 | array(l10n('File name'), 'file ASC', true) |
---|
[1020] | 260 | ); |
---|
| 261 | } |
---|
| 262 | |
---|
[589] | 263 | function display_select_categories($categories, |
---|
| 264 | $selecteds, |
---|
[602] | 265 | $blockname, |
---|
[614] | 266 | $fullname = true) |
---|
[589] | 267 | { |
---|
[614] | 268 | global $template; |
---|
[589] | 269 | |
---|
| 270 | foreach ($categories as $category) |
---|
| 271 | { |
---|
[614] | 272 | $selected = ''; |
---|
| 273 | if (in_array($category['id'], $selecteds)) |
---|
[589] | 274 | { |
---|
[614] | 275 | $selected = ' selected="selected"'; |
---|
| 276 | } |
---|
[589] | 277 | |
---|
[614] | 278 | if ($fullname) |
---|
| 279 | { |
---|
| 280 | $option = get_cat_display_name_cache($category['uppercats'], |
---|
[1092] | 281 | null, |
---|
[614] | 282 | false); |
---|
[589] | 283 | } |
---|
[614] | 284 | else |
---|
| 285 | { |
---|
| 286 | $option = str_repeat(' ', |
---|
| 287 | (3 * substr_count($category['global_rank'], '.'))); |
---|
| 288 | $option.= '- '.$category['name']; |
---|
| 289 | } |
---|
[1059] | 290 | |
---|
[614] | 291 | $template->assign_block_vars( |
---|
| 292 | $blockname, |
---|
| 293 | array('SELECTED'=>$selected, |
---|
| 294 | 'VALUE'=>$category['id'], |
---|
| 295 | 'OPTION'=>$option |
---|
| 296 | )); |
---|
[589] | 297 | } |
---|
| 298 | } |
---|
[603] | 299 | |
---|
[614] | 300 | function display_select_cat_wrapper($query, $selecteds, $blockname, |
---|
| 301 | $fullname = true) |
---|
| 302 | { |
---|
| 303 | $result = pwg_query($query); |
---|
| 304 | $categories = array(); |
---|
[655] | 305 | if (!empty($result)) |
---|
| 306 | { |
---|
[657] | 307 | while ($row = mysql_fetch_array($result)) |
---|
| 308 | { |
---|
| 309 | array_push($categories, $row); |
---|
| 310 | } |
---|
[614] | 311 | } |
---|
| 312 | usort($categories, 'global_rank_compare'); |
---|
| 313 | display_select_categories($categories, $selecteds, $blockname, $fullname); |
---|
| 314 | } |
---|
| 315 | |
---|
[603] | 316 | /** |
---|
| 317 | * returns all subcategory identifiers of given category ids |
---|
| 318 | * |
---|
| 319 | * @param array ids |
---|
| 320 | * @return array |
---|
| 321 | */ |
---|
| 322 | function get_subcat_ids($ids) |
---|
| 323 | { |
---|
| 324 | $query = ' |
---|
| 325 | SELECT DISTINCT(id) |
---|
| 326 | FROM '.CATEGORIES_TABLE.' |
---|
| 327 | WHERE '; |
---|
| 328 | foreach ($ids as $num => $category_id) |
---|
| 329 | { |
---|
| 330 | if ($num > 0) |
---|
| 331 | { |
---|
| 332 | $query.= ' |
---|
| 333 | OR '; |
---|
| 334 | } |
---|
| 335 | $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\''; |
---|
| 336 | } |
---|
| 337 | $query.= ' |
---|
| 338 | ;'; |
---|
| 339 | $result = pwg_query($query); |
---|
| 340 | |
---|
| 341 | $subcats = array(); |
---|
| 342 | while ($row = mysql_fetch_array($result)) |
---|
| 343 | { |
---|
| 344 | array_push($subcats, $row['id']); |
---|
| 345 | } |
---|
| 346 | return $subcats; |
---|
| 347 | } |
---|
[614] | 348 | |
---|
| 349 | function global_rank_compare($a, $b) |
---|
| 350 | { |
---|
| 351 | return strnatcasecmp($a['global_rank'], $b['global_rank']); |
---|
| 352 | } |
---|
[1036] | 353 | |
---|
| 354 | function rank_compare($a, $b) |
---|
| 355 | { |
---|
| 356 | if ($a['rank'] == $b['rank']) |
---|
| 357 | { |
---|
| 358 | return 0; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | return ($a['rank'] < $b['rank']) ? -1 : 1; |
---|
| 362 | } |
---|
[362] | 363 | ?> |
---|