admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * URI Class |
| 20 | * |
| 21 | * Parses URIs and determines routing |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
| 25 | * @category URI |
| 26 | * @author Rick Ellis |
| 27 | * @link http://www.codeigniter.com/user_guide/libraries/uri.html |
| 28 | */ |
| 29 | class CI_URI { |
| 30 | |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 31 | var $router; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 32 | var $keyval = array(); |
| 33 | |
| 34 | /** |
| 35 | * Constructor |
| 36 | * |
| 37 | * Simply globalizes the $RTR object. The front |
| 38 | * loads the Router class early on so it's not available |
| 39 | * normally as other classes are. |
| 40 | * |
| 41 | * @access public |
| 42 | */ |
| 43 | function CI_URI() |
| 44 | { |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 45 | $this->router =& _load_class('Router'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 46 | log_message('debug', "URI Class Initialized"); |
| 47 | } |
| 48 | |
| 49 | // -------------------------------------------------------------------- |
| 50 | |
| 51 | /** |
| 52 | * Fetch a URI Segment |
| 53 | * |
| 54 | * This function returns the URI segment based on the number provided. |
| 55 | * |
| 56 | * @access public |
| 57 | * @param integer |
| 58 | * @param bool |
| 59 | * @return string |
| 60 | */ |
| 61 | function segment($n, $no_result = FALSE) |
| 62 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 63 | return ( ! isset($this->router->segments[$n])) ? $no_result : $this->router->segments[$n]; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // -------------------------------------------------------------------- |
| 67 | |
| 68 | /** |
admin | daf454d | 2006-09-21 17:01:27 +0000 | [diff] [blame] | 69 | * Fetch a URI "routed" Segment |
| 70 | * |
| 71 | * This function returns the re-routed URI segment (assuming routing rules are used) |
| 72 | * based on the number provided. If there is no routing this function returns the |
| 73 | * same result as $this->segment() |
| 74 | * |
| 75 | * @access public |
| 76 | * @param integer |
| 77 | * @param bool |
| 78 | * @return string |
| 79 | */ |
| 80 | function rsegment($n, $no_result = FALSE) |
| 81 | { |
| 82 | return ( ! isset($this->router->rsegments[$n])) ? $no_result : $this->router->rsegments[$n]; |
| 83 | } |
| 84 | |
| 85 | // -------------------------------------------------------------------- |
| 86 | |
| 87 | /** |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 88 | * Generate a key value pair from the URI string |
| 89 | * |
| 90 | * This function generates and associative array of URI data starting |
| 91 | * at the supplied segment. For example, if this is your URI: |
| 92 | * |
| 93 | * www.your-site.com/user/search/name/joe/location/UK/gender/male |
| 94 | * |
| 95 | * You can use this function to generate an array with this prototype: |
| 96 | * |
| 97 | * array ( |
| 98 | * name => joe |
| 99 | * location => UK |
| 100 | * gender => male |
| 101 | * ) |
| 102 | * |
| 103 | * @access public |
| 104 | * @param integer the starting segment number |
| 105 | * @param array an array of default values |
| 106 | * @return array |
| 107 | */ |
| 108 | function uri_to_assoc($n = 3, $default = array()) |
| 109 | { |
admin | 05633d7 | 2006-09-21 17:22:21 +0000 | [diff] [blame] | 110 | return $this->_uri_to_assoc($n, $default, 'segment'); |
| 111 | } |
| 112 | /** |
| 113 | * Identical to above only it uses the re-routed segment array |
| 114 | * |
| 115 | */ |
| 116 | function ruri_to_assoc($n = 3, $default = array()) |
| 117 | { |
| 118 | return $this->_uri_to_assoc($n, $default, 'rsegment'); |
| 119 | } |
| 120 | |
| 121 | // -------------------------------------------------------------------- |
| 122 | |
| 123 | /** |
| 124 | * Generate a key value pair from the URI string or Re-routed URI string |
| 125 | * |
| 126 | * @access private |
| 127 | * @param integer the starting segment number |
| 128 | * @param array an array of default values |
| 129 | * @param string which array we should use |
| 130 | * @return array |
| 131 | */ |
| 132 | function _uri_to_assoc($n = 3, $default = array(), $which = 'segment') |
| 133 | { |
| 134 | if ($which == 'segment') |
| 135 | { |
| 136 | $total_segments = 'total_segments'; |
| 137 | $segment_array = 'segment_array'; |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | $total_segments = 'total_rsegments'; |
| 142 | $segment_array = 'rsegment_array'; |
| 143 | } |
| 144 | |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 145 | if ( ! is_numeric($n)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 146 | { |
| 147 | return $default; |
| 148 | } |
| 149 | |
| 150 | if (isset($this->keyval[$n])) |
| 151 | { |
| 152 | return $this->keyval[$n]; |
| 153 | } |
| 154 | |
admin | 05633d7 | 2006-09-21 17:22:21 +0000 | [diff] [blame] | 155 | if ($this->$total_segments() < $n) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 156 | { |
| 157 | if (count($default) == 0) |
| 158 | { |
| 159 | return array(); |
| 160 | } |
| 161 | |
| 162 | $retval = array(); |
| 163 | foreach ($default as $val) |
| 164 | { |
| 165 | $retval[$val] = FALSE; |
| 166 | } |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 167 | return $retval; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 168 | } |
| 169 | |
admin | 05633d7 | 2006-09-21 17:22:21 +0000 | [diff] [blame] | 170 | $segments = array_slice($this->$segment_array(), ($n - 1)); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 171 | |
| 172 | $i = 0; |
| 173 | $lastval = ''; |
| 174 | $retval = array(); |
| 175 | foreach ($segments as $seg) |
| 176 | { |
| 177 | if ($i % 2) |
| 178 | { |
| 179 | $retval[$lastval] = $seg; |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | $retval[$seg] = FALSE; |
| 184 | $lastval = $seg; |
| 185 | } |
| 186 | |
| 187 | $i++; |
| 188 | } |
| 189 | |
| 190 | if (count($default) > 0) |
| 191 | { |
| 192 | foreach ($default as $val) |
| 193 | { |
| 194 | if ( ! array_key_exists($val, $retval)) |
| 195 | { |
| 196 | $retval[$val] = FALSE; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Cache the array for reuse |
| 202 | $this->keyval[$n] = $retval; |
| 203 | return $retval; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Generate a URI string from an associative array |
| 208 | * |
| 209 | * |
| 210 | * @access public |
| 211 | * @param array an associative array of key/values |
| 212 | * @return array |
| 213 | */ function assoc_to_uri($array) |
| 214 | { |
| 215 | $temp = array(); |
| 216 | foreach ((array)$array as $key => $val) |
| 217 | { |
| 218 | $temp[] = $key; |
| 219 | $temp[] = $val; |
| 220 | } |
| 221 | |
| 222 | return implode('/', $temp); |
| 223 | } |
| 224 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 225 | // -------------------------------------------------------------------- |
| 226 | |
| 227 | /** |
| 228 | * Fetch a URI Segment and add a trailing slash |
| 229 | * |
| 230 | * @access public |
| 231 | * @param integer |
| 232 | * @param string |
| 233 | * @return string |
| 234 | */ |
| 235 | function slash_segment($n, $where = 'trailing') |
admin | daf454d | 2006-09-21 17:01:27 +0000 | [diff] [blame] | 236 | { |
| 237 | return $this->_slash_segment($n, $where, 'segment'); |
| 238 | } |
| 239 | |
| 240 | // -------------------------------------------------------------------- |
| 241 | |
| 242 | /** |
| 243 | * Fetch a URI Segment and add a trailing slash |
| 244 | * |
| 245 | * @access public |
| 246 | * @param integer |
| 247 | * @param string |
| 248 | * @return string |
| 249 | */ |
| 250 | function slash_rsegment($n, $where = 'trailing') |
| 251 | { |
| 252 | return $this->_slash_segment($n, $where, 'rsegment'); |
| 253 | } |
| 254 | |
| 255 | // -------------------------------------------------------------------- |
| 256 | |
| 257 | /** |
| 258 | * Fetch a URI Segment and add a trailing slash - helper function |
| 259 | * |
| 260 | * @access private |
| 261 | * @param integer |
| 262 | * @param string |
| 263 | * @param string |
| 264 | * @return string |
| 265 | */ |
| 266 | function _slash_segment($n, $where = 'trailing', $which = 'segment') |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 267 | { |
| 268 | if ($where == 'trailing') |
| 269 | { |
| 270 | $trailing = '/'; |
| 271 | $leading = ''; |
| 272 | } |
| 273 | elseif ($where == 'leading') |
| 274 | { |
| 275 | $leading = '/'; |
| 276 | $trailing = ''; |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | $leading = '/'; |
| 281 | $trailing = '/'; |
| 282 | } |
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 283 | return $leading.$this->$which($n).$trailing; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // -------------------------------------------------------------------- |
| 287 | |
| 288 | /** |
| 289 | * Segment Array |
| 290 | * |
| 291 | * @access public |
| 292 | * @return array |
| 293 | */ |
| 294 | function segment_array() |
| 295 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 296 | return $this->router->segments; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 297 | } |
admin | daf454d | 2006-09-21 17:01:27 +0000 | [diff] [blame] | 298 | |
| 299 | // -------------------------------------------------------------------- |
| 300 | |
| 301 | /** |
| 302 | * Routed Segment Array |
| 303 | * |
| 304 | * @access public |
| 305 | * @return array |
| 306 | */ |
| 307 | function rsegment_array() |
| 308 | { |
| 309 | return $this->router->rsegments; |
| 310 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 311 | |
| 312 | // -------------------------------------------------------------------- |
| 313 | |
| 314 | /** |
| 315 | * Total number of segments |
| 316 | * |
| 317 | * @access public |
| 318 | * @return integer |
| 319 | */ |
| 320 | function total_segments() |
| 321 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 322 | return count($this->router->segments); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 323 | } |
admin | daf454d | 2006-09-21 17:01:27 +0000 | [diff] [blame] | 324 | |
| 325 | // -------------------------------------------------------------------- |
| 326 | |
| 327 | /** |
| 328 | * Total number of routed segments |
| 329 | * |
| 330 | * @access public |
| 331 | * @return integer |
| 332 | */ |
| 333 | function total_rsegments() |
| 334 | { |
| 335 | return count($this->router->rsegments); |
| 336 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 337 | |
| 338 | // -------------------------------------------------------------------- |
| 339 | |
| 340 | /** |
| 341 | * Fetch the entire URI string |
| 342 | * |
| 343 | * @access public |
| 344 | * @return string |
| 345 | */ |
| 346 | function uri_string() |
| 347 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 348 | return $this->router->uri_string; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 349 | } |
| 350 | |
admin | daf454d | 2006-09-21 17:01:27 +0000 | [diff] [blame] | 351 | |
| 352 | // -------------------------------------------------------------------- |
| 353 | |
| 354 | /** |
| 355 | * Fetch the entire Re-routed URI string |
| 356 | * |
| 357 | * @access public |
| 358 | * @return string |
| 359 | */ |
admin | 05633d7 | 2006-09-21 17:22:21 +0000 | [diff] [blame] | 360 | function ruri_string() |
admin | daf454d | 2006-09-21 17:01:27 +0000 | [diff] [blame] | 361 | { |
| 362 | return '/'.implode('/', $this->rsegment_array()).'/'; |
| 363 | } |
| 364 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 365 | } |
| 366 | // END URI Class |
| 367 | ?> |