blob: 668b1ee017304afe4eff20bb62d37d6ef7b7f4bc [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000026 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000027 * @link http://codeigniter.com/user_guide/libraries/uri.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000028 */
29class CI_URI {
30
Derek Allardd2df9bc2007-04-15 17:41:17 +000031 var $keyval = array();
Rick Ellis30b40152007-07-20 00:01:13 +000032 var $uri_string;
33 var $segments = array();
34 var $rsegments = array();
Derek Allardd2df9bc2007-04-15 17:41:17 +000035
36 /**
37 * Constructor
38 *
39 * Simply globalizes the $RTR object. The front
40 * loads the Router class early on so it's not available
41 * normally as other classes are.
42 *
43 * @access public
44 */
45 function CI_URI()
46 {
Rick Ellis30b40152007-07-20 00:01:13 +000047 $this->config =& load_class('Config');
Derek Allardd2df9bc2007-04-15 17:41:17 +000048 log_message('debug', "URI Class Initialized");
49 }
50
Rick Ellis30b40152007-07-20 00:01:13 +000051
52 // --------------------------------------------------------------------
53
54 /**
55 * Get the URI String
56 *
57 * @access private
58 * @return string
59 */
60 function _fetch_uri_string()
Rick Ellisde2623c2007-07-21 18:28:24 +000061 {
Rick Ellis30b40152007-07-20 00:01:13 +000062 if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
63 {
64 // If the URL has a question mark then it's simplest to just
65 // build the URI string from the zero index of the $_GET array.
66 // This avoids having to deal with $_SERVER variables, which
67 // can be unreliable in some environments
Derek Jones27b50052008-04-14 14:03:04 +000068 if (is_array($_GET) AND count($_GET) == 1 AND trim(key($_GET), '/') != '')
Rick Ellis30b40152007-07-20 00:01:13 +000069 {
Derek Jones27126102008-01-29 23:43:09 +000070 $this->uri_string = key($_GET);
Rick Ellisde2623c2007-07-21 18:28:24 +000071 return;
Rick Ellis30b40152007-07-20 00:01:13 +000072 }
73
74 // Is there a PATH_INFO variable?
75 // Note: some servers seem to have trouble with getenv() so we'll test it two ways
76 $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
Derek Jones27b50052008-04-14 14:03:04 +000077 if (trim($path, '/') != '' AND $path != "/".SELF)
Rick Ellis30b40152007-07-20 00:01:13 +000078 {
79 $this->uri_string = $path;
Rick Ellisde2623c2007-07-21 18:28:24 +000080 return;
Rick Ellis30b40152007-07-20 00:01:13 +000081 }
82
83 // No PATH_INFO?... What about QUERY_STRING?
84 $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
Derek Jones27b50052008-04-14 14:03:04 +000085 if (trim($path, '/') != '')
Rick Ellis30b40152007-07-20 00:01:13 +000086 {
87 $this->uri_string = $path;
Rick Ellisde2623c2007-07-21 18:28:24 +000088 return;
Rick Ellis30b40152007-07-20 00:01:13 +000089 }
90
91 // No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists?
92 $path = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO');
Derek Jones27b50052008-04-14 14:03:04 +000093 if (trim($path, '/') != '' AND $path != "/".SELF)
Rick Ellis30b40152007-07-20 00:01:13 +000094 {
Derek Jones62a90202008-05-07 16:40:28 +000095 // remove path and script information so we have good URI data
96 $this->uri_string = str_replace($_SERVER['SCRIPT_NAME'], '', $path);
Rick Ellisde2623c2007-07-21 18:28:24 +000097 return;
Rick Ellis30b40152007-07-20 00:01:13 +000098 }
99
100 // We've exhausted all our options...
101 $this->uri_string = '';
102 }
103 else
104 {
105 $uri = strtoupper($this->config->item('uri_protocol'));
106
107 if ($uri == 'REQUEST_URI')
108 {
109 $this->uri_string = $this->_parse_request_uri();
Rick Ellisde2623c2007-07-21 18:28:24 +0000110 return;
Rick Ellis30b40152007-07-20 00:01:13 +0000111 }
112
113 $this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
114 }
115
116 // If the URI contains only a slash we'll kill it
117 if ($this->uri_string == '/')
118 {
119 $this->uri_string = '';
120 }
121 }
Rick Ellisde2623c2007-07-21 18:28:24 +0000122
Rick Ellis30b40152007-07-20 00:01:13 +0000123 // --------------------------------------------------------------------
124
125 /**
126 * Parse the REQUEST_URI
127 *
128 * Due to the way REQUEST_URI works it usually contains path info
129 * that makes it unusable as URI data. We'll trim off the unnecessary
130 * data, hopefully arriving at a valid URI that we can use.
131 *
132 * @access private
133 * @return string
134 */
135 function _parse_request_uri()
136 {
Derek Jones0b59f272008-05-13 04:22:33 +0000137 if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '')
Rick Ellis30b40152007-07-20 00:01:13 +0000138 {
139 return '';
140 }
141
142 $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI']));
143
144 if ($request_uri == '' OR $request_uri == SELF)
145 {
146 return '';
147 }
148
149 $fc_path = FCPATH;
150 if (strpos($request_uri, '?') !== FALSE)
151 {
152 $fc_path .= '?';
153 }
154
155 $parsed_uri = explode("/", $request_uri);
156
157 $i = 0;
158 foreach(explode("/", $fc_path) as $segment)
159 {
160 if (isset($parsed_uri[$i]) AND $segment == $parsed_uri[$i])
161 {
162 $i++;
163 }
164 }
165
166 $parsed_uri = implode("/", array_slice($parsed_uri, $i));
167
168 if ($parsed_uri != '')
169 {
170 $parsed_uri = '/'.$parsed_uri;
171 }
172
173 return $parsed_uri;
174 }
175
176 // --------------------------------------------------------------------
177
178 /**
179 * Filter segments for malicious characters
180 *
181 * @access private
182 * @param string
183 * @return string
184 */
185 function _filter_uri($str)
186 {
Derek Jones500b9d32008-01-19 17:12:31 +0000187 if ($str != '' AND $this->config->item('permitted_uri_chars') != '')
Rick Ellis30b40152007-07-20 00:01:13 +0000188 {
Derek Jones0b59f272008-05-13 04:22:33 +0000189 if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str))
Rick Ellis30b40152007-07-20 00:01:13 +0000190 {
191 exit('The URI you submitted has disallowed characters.');
192 }
Derek Jones500b9d32008-01-19 17:12:31 +0000193 }
194
195 return $str;
Rick Ellis30b40152007-07-20 00:01:13 +0000196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Remove the suffix from the URL if needed
202 *
203 * @access private
204 * @return void
205 */
206 function _remove_url_suffix()
207 {
208 if ($this->config->item('url_suffix') != "")
209 {
210 $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);
211 }
212 }
213
214 // --------------------------------------------------------------------
215
216 /**
217 * Explode the URI Segments. The individual segments will
218 * be stored in the $this->segments array.
219 *
220 * @access private
221 * @return void
222 */
223 function _explode_segments()
224 {
225 foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
226 {
227 // Filter segments for security
228 $val = trim($this->_filter_uri($val));
229
230 if ($val != '')
231 $this->segments[] = $val;
232 }
233 }
234
235 // --------------------------------------------------------------------
236 /**
237 * Re-index Segments
238 *
239 * This function re-indexes the $this->segment array so that it
Derek Allardcb36e342008-08-31 14:24:56 +0000240 * starts at 1 rather than 0. Doing so makes it simpler to
Rick Ellis30b40152007-07-20 00:01:13 +0000241 * use functions like $this->uri->segment(n) since there is
242 * a 1:1 relationship between the segment array and the actual segments.
243 *
244 * @access private
245 * @return void
246 */
247 function _reindex_segments()
248 {
Derek Jones5453b8e2008-05-16 14:38:40 +0000249 array_unshift($this->segments, NULL);
250 array_unshift($this->rsegments, NULL);
Rick Ellis30b40152007-07-20 00:01:13 +0000251 unset($this->segments[0]);
Derek Jonesb0697892008-02-13 03:38:39 +0000252 unset($this->rsegments[0]);
Rick Ellis30b40152007-07-20 00:01:13 +0000253 }
254
Derek Allardd2df9bc2007-04-15 17:41:17 +0000255 // --------------------------------------------------------------------
256
257 /**
258 * Fetch a URI Segment
259 *
260 * This function returns the URI segment based on the number provided.
261 *
262 * @access public
263 * @param integer
264 * @param bool
265 * @return string
266 */
267 function segment($n, $no_result = FALSE)
268 {
Derek Jones0b59f272008-05-13 04:22:33 +0000269 return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Fetch a URI "routed" Segment
276 *
277 * This function returns the re-routed URI segment (assuming routing rules are used)
278 * based on the number provided. If there is no routing this function returns the
279 * same result as $this->segment()
280 *
281 * @access public
282 * @param integer
283 * @param bool
284 * @return string
285 */
286 function rsegment($n, $no_result = FALSE)
287 {
Derek Jones0b59f272008-05-13 04:22:33 +0000288 return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * Generate a key value pair from the URI string
295 *
296 * This function generates and associative array of URI data starting
297 * at the supplied segment. For example, if this is your URI:
298 *
Derek Jonesff845f92008-06-26 17:05:55 +0000299 * example.com/user/search/name/joe/location/UK/gender/male
Derek Allardd2df9bc2007-04-15 17:41:17 +0000300 *
301 * You can use this function to generate an array with this prototype:
302 *
303 * array (
304 * name => joe
305 * location => UK
306 * gender => male
307 * )
308 *
309 * @access public
310 * @param integer the starting segment number
311 * @param array an array of default values
312 * @return array
313 */
314 function uri_to_assoc($n = 3, $default = array())
315 {
316 return $this->_uri_to_assoc($n, $default, 'segment');
317 }
318 /**
319 * Identical to above only it uses the re-routed segment array
320 *
321 */
322 function ruri_to_assoc($n = 3, $default = array())
323 {
324 return $this->_uri_to_assoc($n, $default, 'rsegment');
325 }
326
327 // --------------------------------------------------------------------
328
329 /**
330 * Generate a key value pair from the URI string or Re-routed URI string
331 *
332 * @access private
333 * @param integer the starting segment number
334 * @param array an array of default values
335 * @param string which array we should use
336 * @return array
337 */
338 function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
339 {
340 if ($which == 'segment')
341 {
342 $total_segments = 'total_segments';
343 $segment_array = 'segment_array';
344 }
345 else
346 {
347 $total_segments = 'total_rsegments';
348 $segment_array = 'rsegment_array';
349 }
350
Derek Jones0b59f272008-05-13 04:22:33 +0000351 if ( ! is_numeric($n))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000352 {
353 return $default;
354 }
355
356 if (isset($this->keyval[$n]))
357 {
358 return $this->keyval[$n];
359 }
360
361 if ($this->$total_segments() < $n)
362 {
363 if (count($default) == 0)
364 {
365 return array();
366 }
367
368 $retval = array();
369 foreach ($default as $val)
370 {
371 $retval[$val] = FALSE;
372 }
373 return $retval;
374 }
375
376 $segments = array_slice($this->$segment_array(), ($n - 1));
377
378 $i = 0;
379 $lastval = '';
380 $retval = array();
381 foreach ($segments as $seg)
382 {
383 if ($i % 2)
384 {
385 $retval[$lastval] = $seg;
386 }
387 else
388 {
389 $retval[$seg] = FALSE;
390 $lastval = $seg;
391 }
392
393 $i++;
394 }
395
396 if (count($default) > 0)
397 {
398 foreach ($default as $val)
399 {
Derek Jones0b59f272008-05-13 04:22:33 +0000400 if ( ! array_key_exists($val, $retval))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000401 {
402 $retval[$val] = FALSE;
403 }
404 }
405 }
406
407 // Cache the array for reuse
408 $this->keyval[$n] = $retval;
409 return $retval;
410 }
411
Rick Ellis30b40152007-07-20 00:01:13 +0000412 // --------------------------------------------------------------------
413
Derek Allardd2df9bc2007-04-15 17:41:17 +0000414 /**
415 * Generate a URI string from an associative array
416 *
417 *
418 * @access public
419 * @param array an associative array of key/values
420 * @return array
Rick Ellis30b40152007-07-20 00:01:13 +0000421 */
422 function assoc_to_uri($array)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000423 {
424 $temp = array();
425 foreach ((array)$array as $key => $val)
426 {
427 $temp[] = $key;
428 $temp[] = $val;
429 }
430
431 return implode('/', $temp);
432 }
433
434 // --------------------------------------------------------------------
435
436 /**
437 * Fetch a URI Segment and add a trailing slash
438 *
439 * @access public
440 * @param integer
441 * @param string
442 * @return string
443 */
444 function slash_segment($n, $where = 'trailing')
445 {
446 return $this->_slash_segment($n, $where, 'segment');
447 }
448
449 // --------------------------------------------------------------------
450
451 /**
452 * Fetch a URI Segment and add a trailing slash
453 *
454 * @access public
455 * @param integer
456 * @param string
457 * @return string
458 */
459 function slash_rsegment($n, $where = 'trailing')
460 {
461 return $this->_slash_segment($n, $where, 'rsegment');
462 }
463
464 // --------------------------------------------------------------------
465
466 /**
467 * Fetch a URI Segment and add a trailing slash - helper function
468 *
469 * @access private
470 * @param integer
471 * @param string
472 * @param string
473 * @return string
474 */
475 function _slash_segment($n, $where = 'trailing', $which = 'segment')
476 {
477 if ($where == 'trailing')
478 {
479 $trailing = '/';
480 $leading = '';
481 }
482 elseif ($where == 'leading')
483 {
484 $leading = '/';
485 $trailing = '';
486 }
487 else
488 {
489 $leading = '/';
490 $trailing = '/';
491 }
492 return $leading.$this->$which($n).$trailing;
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Segment Array
499 *
500 * @access public
501 * @return array
502 */
503 function segment_array()
504 {
Rick Ellis30b40152007-07-20 00:01:13 +0000505 return $this->segments;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000506 }
507
508 // --------------------------------------------------------------------
509
510 /**
511 * Routed Segment Array
512 *
513 * @access public
514 * @return array
515 */
516 function rsegment_array()
517 {
Rick Ellis30b40152007-07-20 00:01:13 +0000518 return $this->rsegments;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Total number of segments
525 *
526 * @access public
527 * @return integer
528 */
529 function total_segments()
530 {
Rick Ellis30b40152007-07-20 00:01:13 +0000531 return count($this->segments);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000532 }
533
534 // --------------------------------------------------------------------
535
536 /**
537 * Total number of routed segments
538 *
539 * @access public
540 * @return integer
541 */
542 function total_rsegments()
543 {
Rick Ellis30b40152007-07-20 00:01:13 +0000544 return count($this->rsegments);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000545 }
546
547 // --------------------------------------------------------------------
548
549 /**
550 * Fetch the entire URI string
551 *
552 * @access public
553 * @return string
554 */
555 function uri_string()
556 {
Rick Ellis30b40152007-07-20 00:01:13 +0000557 return $this->uri_string;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000558 }
559
560
561 // --------------------------------------------------------------------
562
563 /**
564 * Fetch the entire Re-routed URI string
565 *
566 * @access public
567 * @return string
568 */
569 function ruri_string()
570 {
571 return '/'.implode('/', $this->rsegment_array()).'/';
572 }
573
574}
575// END URI Class
Derek Jones0b59f272008-05-13 04:22:33 +0000576
577/* End of file URI.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000578/* Location: ./system/libraries/URI.php */