[1109] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
| 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
| 5 | // +-----------------------------------------------------------------------+ |
---|
| 6 | // | branch : BSF (Best So Far) |
---|
| 7 | // | file : $RCSfile$ |
---|
| 8 | // | last update : $Date: 2006-07-26 21:00:16 +0000 (Wed, 26 Jul 2006) $ |
---|
| 9 | // | last modifier : $Author: nikrou $ |
---|
| 10 | // | revision : $Revision: 1503 $ |
---|
| 11 | // +-----------------------------------------------------------------------+ |
---|
| 12 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 13 | // | it under the terms of the GNU General Public License as published by | |
---|
| 14 | // | the Free Software Foundation | |
---|
| 15 | // | | |
---|
| 16 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 17 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 18 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 19 | // | General Public License for more details. | |
---|
| 20 | // | | |
---|
| 21 | // | You should have received a copy of the GNU General Public License | |
---|
| 22 | // | along with this program; if not, write to the Free Software | |
---|
| 23 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 24 | // | USA. | |
---|
| 25 | // +-----------------------------------------------------------------------+ |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * returns a prefix for each url link on displayed page |
---|
[1374] | 30 | * and return an empty string for current path |
---|
[1109] | 31 | * @return string |
---|
| 32 | */ |
---|
| 33 | function get_root_url() |
---|
| 34 | { |
---|
| 35 | global $page; |
---|
| 36 | if ( isset($page['root_path']) ) |
---|
| 37 | { |
---|
[1374] | 38 | $root_url = $page['root_path']; |
---|
[1109] | 39 | } |
---|
[1403] | 40 | else |
---|
[1374] | 41 | { |
---|
| 42 | $root_url = PHPWG_ROOT_PATH; |
---|
| 43 | } |
---|
| 44 | if ( dirname($root_url)!='.' ) |
---|
| 45 | { |
---|
| 46 | return $root_url; |
---|
[1403] | 47 | } |
---|
| 48 | else |
---|
[1374] | 49 | { |
---|
[1403] | 50 | return substr($root_url, 2); |
---|
[1374] | 51 | } |
---|
[1109] | 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * adds one or more _GET style parameters to an url |
---|
| 56 | * example: add_url_params('/x', array('a'=>'b')) returns /x?a=b |
---|
| 57 | * add_url_params('/x?cat_id=10', array('a'=>'b')) returns /x?cat_id=10&a=b |
---|
| 58 | * @param string url |
---|
| 59 | * @param array params |
---|
| 60 | * @return string |
---|
| 61 | */ |
---|
| 62 | function add_url_params($url, $params) |
---|
| 63 | { |
---|
| 64 | if ( !empty($params) ) |
---|
| 65 | { |
---|
| 66 | assert( is_array($params) ); |
---|
| 67 | $is_first = true; |
---|
| 68 | foreach($params as $param=>$val) |
---|
| 69 | { |
---|
| 70 | if ($is_first) |
---|
| 71 | { |
---|
| 72 | $is_first = false; |
---|
| 73 | $url .= ( strstr($url, '?')===false ) ? '?' :'&'; |
---|
| 74 | } |
---|
| 75 | else |
---|
| 76 | { |
---|
| 77 | $url .= '&'; |
---|
| 78 | } |
---|
| 79 | $url .= $param; |
---|
| 80 | if (isset($val)) |
---|
| 81 | { |
---|
| 82 | $url .= '='.$val; |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | return $url; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * build an index URL for a specific section |
---|
| 91 | * |
---|
| 92 | * @param array |
---|
| 93 | * @return string |
---|
| 94 | */ |
---|
[1503] | 95 | function make_index_url($params = array()) |
---|
[1109] | 96 | { |
---|
| 97 | global $conf; |
---|
| 98 | $url = get_root_url().'index'; |
---|
| 99 | if ($conf['php_extension_in_urls']) |
---|
| 100 | { |
---|
| 101 | $url .= '.php'; |
---|
| 102 | } |
---|
| 103 | if ($conf['question_mark_in_urls']) |
---|
| 104 | { |
---|
| 105 | $url .= '?'; |
---|
| 106 | } |
---|
[1503] | 107 | $url.= make_section_in_url($params); |
---|
[1109] | 108 | $url = add_well_known_params_in_url($url, $params); |
---|
| 109 | return $url; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * build an index URL with current page parameters, but with redefinitions |
---|
| 114 | * and removes. |
---|
| 115 | * |
---|
[1503] | 116 | * duplicate_index_url(array('category' => 12), array('start')) will create |
---|
[1109] | 117 | * an index URL on the current section (categories), but on a redefined |
---|
| 118 | * category and without the start URL parameter. |
---|
| 119 | * |
---|
| 120 | * @param array redefined keys |
---|
| 121 | * @param array removed keys |
---|
| 122 | * @return string |
---|
| 123 | */ |
---|
[1503] | 124 | function duplicate_index_url($redefined = array(), $removed = array()) |
---|
[1109] | 125 | { |
---|
[1503] | 126 | return make_index_url( |
---|
[1109] | 127 | params_for_duplication($redefined, $removed) |
---|
| 128 | ); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * returns $page global array with key redefined and key removed |
---|
| 133 | * |
---|
| 134 | * @param array redefined keys |
---|
| 135 | * @param array removed keys |
---|
| 136 | * @return array |
---|
| 137 | */ |
---|
| 138 | function params_for_duplication($redefined, $removed) |
---|
| 139 | { |
---|
| 140 | global $page; |
---|
| 141 | |
---|
| 142 | if (count($removed) > 0) |
---|
| 143 | { |
---|
| 144 | $params = array(); |
---|
| 145 | |
---|
| 146 | foreach ($page as $page_item_key => $page_item_value) |
---|
| 147 | { |
---|
| 148 | if (!in_array($page_item_key, $removed)) |
---|
| 149 | { |
---|
| 150 | $params[$page_item_key] = $page_item_value; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | else |
---|
| 155 | { |
---|
| 156 | $params = $page; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | foreach ($redefined as $redefined_param => $redefined_value) |
---|
| 160 | { |
---|
| 161 | $params[$redefined_param] = $redefined_value; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | return $params; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * create a picture URL with current page parameters, but with redefinitions |
---|
[1503] | 169 | * and removes. See duplicate_index_url. |
---|
[1109] | 170 | * |
---|
| 171 | * @param array redefined keys |
---|
| 172 | * @param array removed keys |
---|
| 173 | * @return string |
---|
| 174 | */ |
---|
[1503] | 175 | function duplicate_picture_url($redefined = array(), $removed = array()) |
---|
[1109] | 176 | { |
---|
[1503] | 177 | return make_picture_url( |
---|
[1109] | 178 | params_for_duplication($redefined, $removed) |
---|
| 179 | ); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * create a picture URL on a specific section for a specific picture |
---|
| 184 | * |
---|
| 185 | * @param array |
---|
| 186 | * @return string |
---|
| 187 | */ |
---|
[1503] | 188 | function make_picture_url($params) |
---|
[1109] | 189 | { |
---|
| 190 | global $conf; |
---|
| 191 | if (!isset($params['image_id'])) |
---|
| 192 | { |
---|
[1503] | 193 | die('make_picture_url: image_id is a required parameter'); |
---|
[1109] | 194 | } |
---|
| 195 | |
---|
| 196 | $url = get_root_url().'picture'; |
---|
| 197 | if ($conf['php_extension_in_urls']) |
---|
| 198 | { |
---|
| 199 | $url .= '.php'; |
---|
| 200 | } |
---|
| 201 | if ($conf['question_mark_in_urls']) |
---|
| 202 | { |
---|
| 203 | $url .= '?'; |
---|
| 204 | } |
---|
| 205 | $url.= '/'; |
---|
| 206 | switch ( $conf['picture_url_style'] ) |
---|
| 207 | { |
---|
| 208 | case 'id-file': |
---|
| 209 | $url .= $params['image_id']; |
---|
| 210 | if ( isset($params['image_file']) ) |
---|
| 211 | { |
---|
| 212 | $url .= '-'.get_filename_wo_extension($params['image_file']); |
---|
| 213 | } |
---|
| 214 | break; |
---|
| 215 | case 'file': |
---|
| 216 | if ( isset($params['image_file']) |
---|
| 217 | and !is_numeric($params['image_file']) ) |
---|
| 218 | { |
---|
| 219 | $url .= get_filename_wo_extension($params['image_file']); |
---|
| 220 | } |
---|
| 221 | else |
---|
| 222 | { |
---|
| 223 | $url .= $params['image_id']; |
---|
| 224 | } |
---|
| 225 | break; |
---|
| 226 | default: |
---|
| 227 | $url .= $params['image_id']; |
---|
| 228 | } |
---|
[1503] | 229 | $url .= make_section_in_url($params); |
---|
[1109] | 230 | $url = add_well_known_params_in_url($url, $params); |
---|
| 231 | return $url; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | /** |
---|
| 235 | *adds to the url the chronology and start parameters |
---|
| 236 | */ |
---|
| 237 | function add_well_known_params_in_url($url, $params) |
---|
| 238 | { |
---|
| 239 | if ( isset($params['chronology_field']) ) |
---|
| 240 | { |
---|
| 241 | $url .= '/'. $params['chronology_field']; |
---|
| 242 | $url .= '-'. $params['chronology_style']; |
---|
| 243 | if ( isset($params['chronology_view']) ) |
---|
| 244 | { |
---|
| 245 | $url .= '-'. $params['chronology_view']; |
---|
| 246 | } |
---|
| 247 | if ( !empty($params['chronology_date']) ) |
---|
| 248 | { |
---|
| 249 | $url .= '-'. implode('-', $params['chronology_date'] ); |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | if (isset($params['start']) and $params['start'] > 0) |
---|
| 254 | { |
---|
| 255 | $url.= '/start-'.$params['start']; |
---|
| 256 | } |
---|
| 257 | return $url; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | /** |
---|
| 261 | * return the section token of an index or picture URL. |
---|
| 262 | * |
---|
| 263 | * Depending on section, other parameters are required (see function code |
---|
| 264 | * for details) |
---|
| 265 | * |
---|
| 266 | * @param array |
---|
| 267 | * @return string |
---|
| 268 | */ |
---|
[1503] | 269 | function make_section_in_url($params) |
---|
[1109] | 270 | { |
---|
[1131] | 271 | global $conf; |
---|
[1109] | 272 | $section_string = ''; |
---|
| 273 | |
---|
[1119] | 274 | $section_of = array( |
---|
| 275 | 'category' => 'categories', |
---|
| 276 | 'tags' => 'tags', |
---|
| 277 | 'list' => 'list', |
---|
| 278 | 'search' => 'search', |
---|
| 279 | ); |
---|
| 280 | |
---|
| 281 | foreach ($section_of as $param => $section) |
---|
[1109] | 282 | { |
---|
[1119] | 283 | if (isset($params[$param])) |
---|
[1109] | 284 | { |
---|
[1119] | 285 | $params['section'] = $section; |
---|
[1109] | 286 | } |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | if (!isset($params['section'])) |
---|
| 290 | { |
---|
| 291 | $params['section'] = 'categories'; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | switch($params['section']) |
---|
| 295 | { |
---|
| 296 | case 'categories' : |
---|
| 297 | { |
---|
| 298 | if (!isset($params['category'])) |
---|
| 299 | { |
---|
[1119] | 300 | $section_string.= '/categories'; |
---|
[1109] | 301 | } |
---|
| 302 | else |
---|
| 303 | { |
---|
| 304 | $section_string.= '/category/'.$params['category']; |
---|
[1131] | 305 | if ($conf['category_url_style']=='id-name' and isset($params['cat_name']) ) |
---|
| 306 | { |
---|
| 307 | if ( is_string($params['cat_name']) ) |
---|
| 308 | { |
---|
| 309 | $section_string.= '-'.str2url($params['cat_name']); |
---|
| 310 | } |
---|
| 311 | elseif ( is_array( $params['cat_name'] ) and |
---|
| 312 | isset( $params['cat_name'][$params['category']] ) ) |
---|
| 313 | { |
---|
| 314 | $section_string.= '-' |
---|
| 315 | .str2url($params['cat_name'][$params['category']]); |
---|
| 316 | } |
---|
| 317 | } |
---|
[1109] | 318 | } |
---|
| 319 | |
---|
| 320 | break; |
---|
| 321 | } |
---|
| 322 | case 'tags' : |
---|
| 323 | { |
---|
| 324 | if (!isset($params['tags']) or count($params['tags']) == 0) |
---|
| 325 | { |
---|
[1503] | 326 | die('make_section_in_url: require at least one tag'); |
---|
[1109] | 327 | } |
---|
| 328 | |
---|
| 329 | $section_string.= '/tags'; |
---|
| 330 | |
---|
| 331 | foreach ($params['tags'] as $tag) |
---|
| 332 | { |
---|
[1131] | 333 | switch ( $conf['tag_url_style'] ) |
---|
[1119] | 334 | { |
---|
[1131] | 335 | case 'id': |
---|
| 336 | $section_string.= '/'.$tag['id']; |
---|
| 337 | break; |
---|
| 338 | case 'tag': |
---|
| 339 | if (isset($tag['url_name']) and !is_numeric($tag['url_name']) ) |
---|
| 340 | { |
---|
| 341 | $section_string.= '/'.$tag['url_name']; |
---|
| 342 | break; |
---|
| 343 | } |
---|
| 344 | default: |
---|
| 345 | $section_string.= '/'.$tag['id']; |
---|
| 346 | if (isset($tag['url_name'])) |
---|
| 347 | { |
---|
| 348 | $section_string.= '-'.$tag['url_name']; |
---|
| 349 | } |
---|
[1119] | 350 | } |
---|
[1109] | 351 | } |
---|
| 352 | |
---|
| 353 | break; |
---|
| 354 | } |
---|
| 355 | case 'search' : |
---|
| 356 | { |
---|
| 357 | if (!isset($params['search'])) |
---|
| 358 | { |
---|
[1503] | 359 | die('make_section_in_url: require a search identifier'); |
---|
[1109] | 360 | } |
---|
| 361 | |
---|
| 362 | $section_string.= '/search/'.$params['search']; |
---|
| 363 | |
---|
| 364 | break; |
---|
| 365 | } |
---|
| 366 | case 'list' : |
---|
| 367 | { |
---|
| 368 | if (!isset($params['list'])) |
---|
| 369 | { |
---|
[1503] | 370 | die('make_section_in_url: require a list of items'); |
---|
[1109] | 371 | } |
---|
| 372 | |
---|
| 373 | $section_string.= '/list/'.implode(',', $params['list']); |
---|
| 374 | |
---|
| 375 | break; |
---|
| 376 | } |
---|
| 377 | default : |
---|
| 378 | { |
---|
| 379 | $section_string.= '/'.$params['section']; |
---|
| 380 | } |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | return $section_string; |
---|
| 384 | } |
---|
| 385 | ?> |
---|