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 | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 45 | $this->router =& _load_class('CI_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 | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 110 | if ( ! is_numeric($n)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 111 | { |
| 112 | return $default; |
| 113 | } |
| 114 | |
| 115 | if (isset($this->keyval[$n])) |
| 116 | { |
| 117 | return $this->keyval[$n]; |
| 118 | } |
| 119 | |
| 120 | if ($this->total_segments() < $n) |
| 121 | { |
| 122 | if (count($default) == 0) |
| 123 | { |
| 124 | return array(); |
| 125 | } |
| 126 | |
| 127 | $retval = array(); |
| 128 | foreach ($default as $val) |
| 129 | { |
| 130 | $retval[$val] = FALSE; |
| 131 | } |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 132 | return $retval; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | $segments = array_slice($this->segment_array(), ($n - 1)); |
| 136 | |
| 137 | $i = 0; |
| 138 | $lastval = ''; |
| 139 | $retval = array(); |
| 140 | foreach ($segments as $seg) |
| 141 | { |
| 142 | if ($i % 2) |
| 143 | { |
| 144 | $retval[$lastval] = $seg; |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | $retval[$seg] = FALSE; |
| 149 | $lastval = $seg; |
| 150 | } |
| 151 | |
| 152 | $i++; |
| 153 | } |
| 154 | |
| 155 | if (count($default) > 0) |
| 156 | { |
| 157 | foreach ($default as $val) |
| 158 | { |
| 159 | if ( ! array_key_exists($val, $retval)) |
| 160 | { |
| 161 | $retval[$val] = FALSE; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Cache the array for reuse |
| 167 | $this->keyval[$n] = $retval; |
| 168 | return $retval; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Generate a URI string from an associative array |
| 173 | * |
| 174 | * |
| 175 | * @access public |
| 176 | * @param array an associative array of key/values |
| 177 | * @return array |
| 178 | */ function assoc_to_uri($array) |
| 179 | { |
| 180 | $temp = array(); |
| 181 | foreach ((array)$array as $key => $val) |
| 182 | { |
| 183 | $temp[] = $key; |
| 184 | $temp[] = $val; |
| 185 | } |
| 186 | |
| 187 | return implode('/', $temp); |
| 188 | } |
| 189 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 190 | // -------------------------------------------------------------------- |
| 191 | |
| 192 | /** |
| 193 | * Fetch a URI Segment and add a trailing slash |
| 194 | * |
| 195 | * @access public |
| 196 | * @param integer |
| 197 | * @param string |
| 198 | * @return string |
| 199 | */ |
| 200 | function slash_segment($n, $where = 'trailing') |
admin | daf454d | 2006-09-21 17:01:27 +0000 | [diff] [blame^] | 201 | { |
| 202 | return $this->_slash_segment($n, $where, 'segment'); |
| 203 | } |
| 204 | |
| 205 | // -------------------------------------------------------------------- |
| 206 | |
| 207 | /** |
| 208 | * Fetch a URI Segment and add a trailing slash |
| 209 | * |
| 210 | * @access public |
| 211 | * @param integer |
| 212 | * @param string |
| 213 | * @return string |
| 214 | */ |
| 215 | function slash_rsegment($n, $where = 'trailing') |
| 216 | { |
| 217 | return $this->_slash_segment($n, $where, 'rsegment'); |
| 218 | } |
| 219 | |
| 220 | // -------------------------------------------------------------------- |
| 221 | |
| 222 | /** |
| 223 | * Fetch a URI Segment and add a trailing slash - helper function |
| 224 | * |
| 225 | * @access private |
| 226 | * @param integer |
| 227 | * @param string |
| 228 | * @param string |
| 229 | * @return string |
| 230 | */ |
| 231 | function _slash_segment($n, $where = 'trailing', $which = 'segment') |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 232 | { |
| 233 | if ($where == 'trailing') |
| 234 | { |
| 235 | $trailing = '/'; |
| 236 | $leading = ''; |
| 237 | } |
| 238 | elseif ($where == 'leading') |
| 239 | { |
| 240 | $leading = '/'; |
| 241 | $trailing = ''; |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | $leading = '/'; |
| 246 | $trailing = '/'; |
| 247 | } |
admin | daf454d | 2006-09-21 17:01:27 +0000 | [diff] [blame^] | 248 | return ( ! isset($this->router->$which[$n])) ? '' : $leading.$this->router->$which[$n].$trailing; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // -------------------------------------------------------------------- |
| 252 | |
| 253 | /** |
| 254 | * Segment Array |
| 255 | * |
| 256 | * @access public |
| 257 | * @return array |
| 258 | */ |
| 259 | function segment_array() |
| 260 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 261 | return $this->router->segments; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 262 | } |
admin | daf454d | 2006-09-21 17:01:27 +0000 | [diff] [blame^] | 263 | |
| 264 | // -------------------------------------------------------------------- |
| 265 | |
| 266 | /** |
| 267 | * Routed Segment Array |
| 268 | * |
| 269 | * @access public |
| 270 | * @return array |
| 271 | */ |
| 272 | function rsegment_array() |
| 273 | { |
| 274 | return $this->router->rsegments; |
| 275 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 276 | |
| 277 | // -------------------------------------------------------------------- |
| 278 | |
| 279 | /** |
| 280 | * Total number of segments |
| 281 | * |
| 282 | * @access public |
| 283 | * @return integer |
| 284 | */ |
| 285 | function total_segments() |
| 286 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 287 | return count($this->router->segments); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 288 | } |
admin | daf454d | 2006-09-21 17:01:27 +0000 | [diff] [blame^] | 289 | |
| 290 | // -------------------------------------------------------------------- |
| 291 | |
| 292 | /** |
| 293 | * Total number of routed segments |
| 294 | * |
| 295 | * @access public |
| 296 | * @return integer |
| 297 | */ |
| 298 | function total_rsegments() |
| 299 | { |
| 300 | return count($this->router->rsegments); |
| 301 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 302 | |
| 303 | // -------------------------------------------------------------------- |
| 304 | |
| 305 | /** |
| 306 | * Fetch the entire URI string |
| 307 | * |
| 308 | * @access public |
| 309 | * @return string |
| 310 | */ |
| 311 | function uri_string() |
| 312 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 313 | return $this->router->uri_string; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 314 | } |
| 315 | |
admin | daf454d | 2006-09-21 17:01:27 +0000 | [diff] [blame^] | 316 | |
| 317 | // -------------------------------------------------------------------- |
| 318 | |
| 319 | /** |
| 320 | * Fetch the entire Re-routed URI string |
| 321 | * |
| 322 | * @access public |
| 323 | * @return string |
| 324 | */ |
| 325 | function uri_rstring() |
| 326 | { |
| 327 | return '/'.implode('/', $this->rsegment_array()).'/'; |
| 328 | } |
| 329 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 330 | } |
| 331 | // END URI Class |
| 332 | ?> |