blob: 6c8eb2e236e50fe5cf81e057a9d683eb8dfe6099 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?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
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
68 if (is_array($_GET) AND count($_GET) == 1)
69 {
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 Jones500b9d32008-01-19 17:12:31 +000077 if ($path != '' AND $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 Jones500b9d32008-01-19 17:12:31 +000085 if ($path != '' AND $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 Jones500b9d32008-01-19 17:12:31 +000093 if ($path != '' AND $path != '/' AND $path != "/".SELF)
Rick Ellis30b40152007-07-20 00:01:13 +000094 {
95 $this->uri_string = $path;
Rick Ellisde2623c2007-07-21 18:28:24 +000096 return;
Rick Ellis30b40152007-07-20 00:01:13 +000097 }
98
99 // We've exhausted all our options...
100 $this->uri_string = '';
101 }
102 else
103 {
104 $uri = strtoupper($this->config->item('uri_protocol'));
105
106 if ($uri == 'REQUEST_URI')
107 {
108 $this->uri_string = $this->_parse_request_uri();
Rick Ellisde2623c2007-07-21 18:28:24 +0000109 return;
Rick Ellis30b40152007-07-20 00:01:13 +0000110 }
111
112 $this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
113 }
114
115 // If the URI contains only a slash we'll kill it
116 if ($this->uri_string == '/')
117 {
118 $this->uri_string = '';
119 }
120 }
Rick Ellisde2623c2007-07-21 18:28:24 +0000121
Rick Ellis30b40152007-07-20 00:01:13 +0000122 // --------------------------------------------------------------------
123
124 /**
125 * Parse the REQUEST_URI
126 *
127 * Due to the way REQUEST_URI works it usually contains path info
128 * that makes it unusable as URI data. We'll trim off the unnecessary
129 * data, hopefully arriving at a valid URI that we can use.
130 *
131 * @access private
132 * @return string
133 */
134 function _parse_request_uri()
135 {
136 if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '')
137 {
138 return '';
139 }
140
141 $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI']));
142
143 if ($request_uri == '' OR $request_uri == SELF)
144 {
145 return '';
146 }
147
148 $fc_path = FCPATH;
149 if (strpos($request_uri, '?') !== FALSE)
150 {
151 $fc_path .= '?';
152 }
153
154 $parsed_uri = explode("/", $request_uri);
155
156 $i = 0;
157 foreach(explode("/", $fc_path) as $segment)
158 {
159 if (isset($parsed_uri[$i]) AND $segment == $parsed_uri[$i])
160 {
161 $i++;
162 }
163 }
164
165 $parsed_uri = implode("/", array_slice($parsed_uri, $i));
166
167 if ($parsed_uri != '')
168 {
169 $parsed_uri = '/'.$parsed_uri;
170 }
171
172 return $parsed_uri;
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
178 * Filter segments for malicious characters
179 *
180 * @access private
181 * @param string
182 * @return string
183 */
184 function _filter_uri($str)
185 {
Derek Jones500b9d32008-01-19 17:12:31 +0000186 if ($str != '' AND $this->config->item('permitted_uri_chars') != '')
Rick Ellis30b40152007-07-20 00:01:13 +0000187 {
188 if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str))
189 {
190 exit('The URI you submitted has disallowed characters.');
191 }
Derek Jones500b9d32008-01-19 17:12:31 +0000192 }
193
194 return $str;
Rick Ellis30b40152007-07-20 00:01:13 +0000195 }
196
197 // --------------------------------------------------------------------
198
199 /**
200 * Remove the suffix from the URL if needed
201 *
202 * @access private
203 * @return void
204 */
205 function _remove_url_suffix()
206 {
207 if ($this->config->item('url_suffix') != "")
208 {
209 $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);
210 }
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Explode the URI Segments. The individual segments will
217 * be stored in the $this->segments array.
218 *
219 * @access private
220 * @return void
221 */
222 function _explode_segments()
223 {
224 foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
225 {
226 // Filter segments for security
227 $val = trim($this->_filter_uri($val));
228
229 if ($val != '')
230 $this->segments[] = $val;
231 }
232 }
233
234 // --------------------------------------------------------------------
235 /**
236 * Re-index Segments
237 *
238 * This function re-indexes the $this->segment array so that it
239 * starts at 1 rather then 0. Doing so makes it simpler to
240 * use functions like $this->uri->segment(n) since there is
241 * a 1:1 relationship between the segment array and the actual segments.
242 *
243 * @access private
244 * @return void
245 */
246 function _reindex_segments()
247 {
248 // Is the routed segment array different then the main segment array?
249 $diff = (count(array_diff($this->rsegments, $this->segments)) == 0) ? FALSE : TRUE;
250
251 $i = 1;
252 foreach ($this->segments as $val)
253 {
254 $this->segments[$i++] = $val;
255 }
256 unset($this->segments[0]);
257
258 if ($diff == FALSE)
259 {
260 $this->rsegments = $this->segments;
261 }
262 else
263 {
264 $i = 1;
265 foreach ($this->rsegments as $val)
266 {
267 $this->rsegments[$i++] = $val;
268 }
269 unset($this->rsegments[0]);
270 }
271 }
272
Derek Allardd2df9bc2007-04-15 17:41:17 +0000273 // --------------------------------------------------------------------
274
275 /**
276 * Fetch a URI Segment
277 *
278 * This function returns the URI segment based on the number provided.
279 *
280 * @access public
281 * @param integer
282 * @param bool
283 * @return string
284 */
285 function segment($n, $no_result = FALSE)
286 {
Rick Ellis30b40152007-07-20 00:01:13 +0000287 return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * Fetch a URI "routed" Segment
294 *
295 * This function returns the re-routed URI segment (assuming routing rules are used)
296 * based on the number provided. If there is no routing this function returns the
297 * same result as $this->segment()
298 *
299 * @access public
300 * @param integer
301 * @param bool
302 * @return string
303 */
304 function rsegment($n, $no_result = FALSE)
305 {
Rick Ellis30b40152007-07-20 00:01:13 +0000306 return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
Derek Allardd2df9bc2007-04-15 17:41:17 +0000307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Generate a key value pair from the URI string
313 *
314 * This function generates and associative array of URI data starting
315 * at the supplied segment. For example, if this is your URI:
316 *
317 * www.your-site.com/user/search/name/joe/location/UK/gender/male
318 *
319 * You can use this function to generate an array with this prototype:
320 *
321 * array (
322 * name => joe
323 * location => UK
324 * gender => male
325 * )
326 *
327 * @access public
328 * @param integer the starting segment number
329 * @param array an array of default values
330 * @return array
331 */
332 function uri_to_assoc($n = 3, $default = array())
333 {
334 return $this->_uri_to_assoc($n, $default, 'segment');
335 }
336 /**
337 * Identical to above only it uses the re-routed segment array
338 *
339 */
340 function ruri_to_assoc($n = 3, $default = array())
341 {
342 return $this->_uri_to_assoc($n, $default, 'rsegment');
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * Generate a key value pair from the URI string or Re-routed URI string
349 *
350 * @access private
351 * @param integer the starting segment number
352 * @param array an array of default values
353 * @param string which array we should use
354 * @return array
355 */
356 function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
357 {
358 if ($which == 'segment')
359 {
360 $total_segments = 'total_segments';
361 $segment_array = 'segment_array';
362 }
363 else
364 {
365 $total_segments = 'total_rsegments';
366 $segment_array = 'rsegment_array';
367 }
368
369 if ( ! is_numeric($n))
370 {
371 return $default;
372 }
373
374 if (isset($this->keyval[$n]))
375 {
376 return $this->keyval[$n];
377 }
378
379 if ($this->$total_segments() < $n)
380 {
381 if (count($default) == 0)
382 {
383 return array();
384 }
385
386 $retval = array();
387 foreach ($default as $val)
388 {
389 $retval[$val] = FALSE;
390 }
391 return $retval;
392 }
393
394 $segments = array_slice($this->$segment_array(), ($n - 1));
395
396 $i = 0;
397 $lastval = '';
398 $retval = array();
399 foreach ($segments as $seg)
400 {
401 if ($i % 2)
402 {
403 $retval[$lastval] = $seg;
404 }
405 else
406 {
407 $retval[$seg] = FALSE;
408 $lastval = $seg;
409 }
410
411 $i++;
412 }
413
414 if (count($default) > 0)
415 {
416 foreach ($default as $val)
417 {
418 if ( ! array_key_exists($val, $retval))
419 {
420 $retval[$val] = FALSE;
421 }
422 }
423 }
424
425 // Cache the array for reuse
426 $this->keyval[$n] = $retval;
427 return $retval;
428 }
429
Rick Ellis30b40152007-07-20 00:01:13 +0000430 // --------------------------------------------------------------------
431
Derek Allardd2df9bc2007-04-15 17:41:17 +0000432 /**
433 * Generate a URI string from an associative array
434 *
435 *
436 * @access public
437 * @param array an associative array of key/values
438 * @return array
Rick Ellis30b40152007-07-20 00:01:13 +0000439 */
440 function assoc_to_uri($array)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000441 {
442 $temp = array();
443 foreach ((array)$array as $key => $val)
444 {
445 $temp[] = $key;
446 $temp[] = $val;
447 }
448
449 return implode('/', $temp);
450 }
451
452 // --------------------------------------------------------------------
453
454 /**
455 * Fetch a URI Segment and add a trailing slash
456 *
457 * @access public
458 * @param integer
459 * @param string
460 * @return string
461 */
462 function slash_segment($n, $where = 'trailing')
463 {
464 return $this->_slash_segment($n, $where, 'segment');
465 }
466
467 // --------------------------------------------------------------------
468
469 /**
470 * Fetch a URI Segment and add a trailing slash
471 *
472 * @access public
473 * @param integer
474 * @param string
475 * @return string
476 */
477 function slash_rsegment($n, $where = 'trailing')
478 {
479 return $this->_slash_segment($n, $where, 'rsegment');
480 }
481
482 // --------------------------------------------------------------------
483
484 /**
485 * Fetch a URI Segment and add a trailing slash - helper function
486 *
487 * @access private
488 * @param integer
489 * @param string
490 * @param string
491 * @return string
492 */
493 function _slash_segment($n, $where = 'trailing', $which = 'segment')
494 {
495 if ($where == 'trailing')
496 {
497 $trailing = '/';
498 $leading = '';
499 }
500 elseif ($where == 'leading')
501 {
502 $leading = '/';
503 $trailing = '';
504 }
505 else
506 {
507 $leading = '/';
508 $trailing = '/';
509 }
510 return $leading.$this->$which($n).$trailing;
511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * Segment Array
517 *
518 * @access public
519 * @return array
520 */
521 function segment_array()
522 {
Rick Ellis30b40152007-07-20 00:01:13 +0000523 return $this->segments;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000524 }
525
526 // --------------------------------------------------------------------
527
528 /**
529 * Routed Segment Array
530 *
531 * @access public
532 * @return array
533 */
534 function rsegment_array()
535 {
Rick Ellis30b40152007-07-20 00:01:13 +0000536 return $this->rsegments;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000537 }
538
539 // --------------------------------------------------------------------
540
541 /**
542 * Total number of segments
543 *
544 * @access public
545 * @return integer
546 */
547 function total_segments()
548 {
Rick Ellis30b40152007-07-20 00:01:13 +0000549 return count($this->segments);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000550 }
551
552 // --------------------------------------------------------------------
553
554 /**
555 * Total number of routed segments
556 *
557 * @access public
558 * @return integer
559 */
560 function total_rsegments()
561 {
Rick Ellis30b40152007-07-20 00:01:13 +0000562 return count($this->rsegments);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000563 }
564
565 // --------------------------------------------------------------------
566
567 /**
568 * Fetch the entire URI string
569 *
570 * @access public
571 * @return string
572 */
573 function uri_string()
574 {
Rick Ellis30b40152007-07-20 00:01:13 +0000575 return $this->uri_string;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000576 }
577
578
579 // --------------------------------------------------------------------
580
581 /**
582 * Fetch the entire Re-routed URI string
583 *
584 * @access public
585 * @return string
586 */
587 function ruri_string()
588 {
589 return '/'.implode('/', $this->rsegment_array()).'/';
590 }
591
592}
593// END URI Class
adminb0dd10f2006-08-25 17:25:49 +0000594?>