Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
| 3 | * CodeIgniter
|
| 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, EllisLab, 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 |
|
| 31 | var $router;
|
| 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 | {
|
| 45 | $this->router =& load_class('Router');
|
| 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 | {
|
| 63 | return ( ! isset($this->router->segments[$n])) ? $no_result : $this->router->segments[$n];
|
| 64 | }
|
| 65 |
|
| 66 | // --------------------------------------------------------------------
|
| 67 |
|
| 68 | /**
|
| 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 | /**
|
| 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 | {
|
| 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 |
|
| 145 | if ( ! is_numeric($n))
|
| 146 | {
|
| 147 | return $default;
|
| 148 | }
|
| 149 |
|
| 150 | if (isset($this->keyval[$n]))
|
| 151 | {
|
| 152 | return $this->keyval[$n];
|
| 153 | }
|
| 154 |
|
| 155 | if ($this->$total_segments() < $n)
|
| 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 | }
|
| 167 | return $retval;
|
| 168 | }
|
| 169 |
|
| 170 | $segments = array_slice($this->$segment_array(), ($n - 1));
|
| 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 |
|
| 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')
|
| 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')
|
| 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 | }
|
| 283 | return $leading.$this->$which($n).$trailing;
|
| 284 | }
|
| 285 |
|
| 286 | // --------------------------------------------------------------------
|
| 287 |
|
| 288 | /**
|
| 289 | * Segment Array
|
| 290 | *
|
| 291 | * @access public
|
| 292 | * @return array
|
| 293 | */
|
| 294 | function segment_array()
|
| 295 | {
|
| 296 | return $this->router->segments;
|
| 297 | }
|
| 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 | }
|
| 311 |
|
| 312 | // --------------------------------------------------------------------
|
| 313 |
|
| 314 | /**
|
| 315 | * Total number of segments
|
| 316 | *
|
| 317 | * @access public
|
| 318 | * @return integer
|
| 319 | */
|
| 320 | function total_segments()
|
| 321 | {
|
| 322 | return count($this->router->segments);
|
| 323 | }
|
| 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 | }
|
| 337 |
|
| 338 | // --------------------------------------------------------------------
|
| 339 |
|
| 340 | /**
|
| 341 | * Fetch the entire URI string
|
| 342 | *
|
| 343 | * @access public
|
| 344 | * @return string
|
| 345 | */
|
| 346 | function uri_string()
|
| 347 | {
|
| 348 | return $this->router->uri_string;
|
| 349 | }
|
| 350 |
|
| 351 |
|
| 352 | // --------------------------------------------------------------------
|
| 353 |
|
| 354 | /**
|
| 355 | * Fetch the entire Re-routed URI string
|
| 356 | *
|
| 357 | * @access public
|
| 358 | * @return string
|
| 359 | */
|
| 360 | function ruri_string()
|
| 361 | {
|
| 362 | return '/'.implode('/', $this->rsegment_array()).'/';
|
| 363 | }
|
| 364 |
|
| 365 | }
|
| 366 | // END URI Class
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 367 | ?> |