blob: 10654a69c5f8de6ffd4afc5c0d7374d00d75ba54 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * URI Class
admine334c472006-10-21 19:44:22 +000020 *
adminb0dd10f2006-08-25 17:25:49 +000021 * 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 */
29class CI_URI {
30
adminb071bb52006-08-26 19:28:37 +000031 var $router;
adminb0dd10f2006-08-25 17:25:49 +000032 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
admine334c472006-10-21 19:44:22 +000039 * normally as other classes are.
adminb0dd10f2006-08-25 17:25:49 +000040 *
41 * @access public
42 */
43 function CI_URI()
44 {
admin7099a582006-10-10 17:47:59 +000045 $this->router =& load_class('Router');
adminb0dd10f2006-08-25 17:25:49 +000046 log_message('debug', "URI Class Initialized");
47 }
48
49 // --------------------------------------------------------------------
50
51 /**
52 * Fetch a URI Segment
53 *
admine334c472006-10-21 19:44:22 +000054 * This function returns the URI segment based on the number provided.
adminb0dd10f2006-08-25 17:25:49 +000055 *
56 * @access public
57 * @param integer
58 * @param bool
59 * @return string
60 */
61 function segment($n, $no_result = FALSE)
62 {
adminb071bb52006-08-26 19:28:37 +000063 return ( ! isset($this->router->segments[$n])) ? $no_result : $this->router->segments[$n];
adminb0dd10f2006-08-25 17:25:49 +000064 }
65
66 // --------------------------------------------------------------------
67
68 /**
admindaf454d2006-09-21 17:01:27 +000069 * 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 /**
adminb0dd10f2006-08-25 17:25:49 +000088 * Generate a key value pair from the URI string
89 *
admine334c472006-10-21 19:44:22 +000090 * This function generates and associative array of URI data starting
adminb0dd10f2006-08-25 17:25:49 +000091 * 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 * )
admine334c472006-10-21 19:44:22 +0000102 *
adminb0dd10f2006-08-25 17:25:49 +0000103 * @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 {
admin05633d72006-09-21 17:22:21 +0000110 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
admine334c472006-10-21 19:44:22 +0000125 *
admin05633d72006-09-21 17:22:21 +0000126 * @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
admin1cf89aa2006-09-03 18:24:39 +0000145 if ( ! is_numeric($n))
adminb0dd10f2006-08-25 17:25:49 +0000146 {
147 return $default;
148 }
149
150 if (isset($this->keyval[$n]))
151 {
152 return $this->keyval[$n];
153 }
154
admin05633d72006-09-21 17:22:21 +0000155 if ($this->$total_segments() < $n)
adminb0dd10f2006-08-25 17:25:49 +0000156 {
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 }
admin1cf89aa2006-09-03 18:24:39 +0000167 return $retval;
adminb0dd10f2006-08-25 17:25:49 +0000168 }
169
admin05633d72006-09-21 17:22:21 +0000170 $segments = array_slice($this->$segment_array(), ($n - 1));
adminb0dd10f2006-08-25 17:25:49 +0000171
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 *
admine334c472006-10-21 19:44:22 +0000209 *
adminb0dd10f2006-08-25 17:25:49 +0000210 * @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
adminb0dd10f2006-08-25 17:25:49 +0000225 // --------------------------------------------------------------------
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')
admindaf454d2006-09-21 17:01:27 +0000236 {
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')
adminb0dd10f2006-08-25 17:25:49 +0000267 {
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 }
adminc1fa0742006-09-21 23:50:23 +0000283 return $leading.$this->$which($n).$trailing;
adminb0dd10f2006-08-25 17:25:49 +0000284 }
285
286 // --------------------------------------------------------------------
287
288 /**
289 * Segment Array
290 *
291 * @access public
292 * @return array
293 */
294 function segment_array()
295 {
adminb071bb52006-08-26 19:28:37 +0000296 return $this->router->segments;
adminb0dd10f2006-08-25 17:25:49 +0000297 }
admindaf454d2006-09-21 17:01:27 +0000298
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 }
adminb0dd10f2006-08-25 17:25:49 +0000311
312 // --------------------------------------------------------------------
313
314 /**
315 * Total number of segments
316 *
317 * @access public
318 * @return integer
319 */
320 function total_segments()
321 {
adminb071bb52006-08-26 19:28:37 +0000322 return count($this->router->segments);
adminb0dd10f2006-08-25 17:25:49 +0000323 }
admindaf454d2006-09-21 17:01:27 +0000324
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 }
adminb0dd10f2006-08-25 17:25:49 +0000337
338 // --------------------------------------------------------------------
339
340 /**
341 * Fetch the entire URI string
342 *
343 * @access public
344 * @return string
345 */
346 function uri_string()
347 {
adminb071bb52006-08-26 19:28:37 +0000348 return $this->router->uri_string;
adminb0dd10f2006-08-25 17:25:49 +0000349 }
350
admindaf454d2006-09-21 17:01:27 +0000351
352 // --------------------------------------------------------------------
353
354 /**
355 * Fetch the entire Re-routed URI string
356 *
357 * @access public
358 * @return string
359 */
admin05633d72006-09-21 17:22:21 +0000360 function ruri_string()
admindaf454d2006-09-21 17:01:27 +0000361 {
362 return '/'.implode('/', $this->rsegment_array()).'/';
363 }
364
adminb0dd10f2006-08-25 17:25:49 +0000365}
366// END URI Class
367?>