blob: 11562eb2932751870bf297002abc04bf14cf7f77 [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.
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 */
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
39 * normally as other classes are.
40 *
41 * @access public
42 */
43 function CI_URI()
44 {
adminb071bb52006-08-26 19:28:37 +000045 $this->router =& _load_class('CI_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 *
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 {
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 *
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 {
admin1cf89aa2006-09-03 18:24:39 +0000110 if ( ! is_numeric($n))
adminb0dd10f2006-08-25 17:25:49 +0000111 {
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 }
admin1cf89aa2006-09-03 18:24:39 +0000132 return $retval;
adminb0dd10f2006-08-25 17:25:49 +0000133 }
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
adminb0dd10f2006-08-25 17:25:49 +0000190 // --------------------------------------------------------------------
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')
admindaf454d2006-09-21 17:01:27 +0000201 {
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')
adminb0dd10f2006-08-25 17:25:49 +0000232 {
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 }
admindaf454d2006-09-21 17:01:27 +0000248 return ( ! isset($this->router->$which[$n])) ? '' : $leading.$this->router->$which[$n].$trailing;
adminb0dd10f2006-08-25 17:25:49 +0000249 }
250
251 // --------------------------------------------------------------------
252
253 /**
254 * Segment Array
255 *
256 * @access public
257 * @return array
258 */
259 function segment_array()
260 {
adminb071bb52006-08-26 19:28:37 +0000261 return $this->router->segments;
adminb0dd10f2006-08-25 17:25:49 +0000262 }
admindaf454d2006-09-21 17:01:27 +0000263
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 }
adminb0dd10f2006-08-25 17:25:49 +0000276
277 // --------------------------------------------------------------------
278
279 /**
280 * Total number of segments
281 *
282 * @access public
283 * @return integer
284 */
285 function total_segments()
286 {
adminb071bb52006-08-26 19:28:37 +0000287 return count($this->router->segments);
adminb0dd10f2006-08-25 17:25:49 +0000288 }
admindaf454d2006-09-21 17:01:27 +0000289
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 }
adminb0dd10f2006-08-25 17:25:49 +0000302
303 // --------------------------------------------------------------------
304
305 /**
306 * Fetch the entire URI string
307 *
308 * @access public
309 * @return string
310 */
311 function uri_string()
312 {
adminb071bb52006-08-26 19:28:37 +0000313 return $this->router->uri_string;
adminb0dd10f2006-08-25 17:25:49 +0000314 }
315
admindaf454d2006-09-21 17:01:27 +0000316
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
adminb0dd10f2006-08-25 17:25:49 +0000330}
331// END URI Class
332?>