blob: ba6279e88ee1c02998c6bb8b44c60b4f42f07156 [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 /**
69 * Generate a key value pair from the URI string
70 *
71 * This function generates and associative array of URI data starting
72 * at the supplied segment. For example, if this is your URI:
73 *
74 * www.your-site.com/user/search/name/joe/location/UK/gender/male
75 *
76 * You can use this function to generate an array with this prototype:
77 *
78 * array (
79 * name => joe
80 * location => UK
81 * gender => male
82 * )
83 *
84 * @access public
85 * @param integer the starting segment number
86 * @param array an array of default values
87 * @return array
88 */
89 function uri_to_assoc($n = 3, $default = array())
90 {
admin1cf89aa2006-09-03 18:24:39 +000091 if ( ! is_numeric($n))
adminb0dd10f2006-08-25 17:25:49 +000092 {
93 return $default;
94 }
95
96 if (isset($this->keyval[$n]))
97 {
98 return $this->keyval[$n];
99 }
100
101 if ($this->total_segments() < $n)
102 {
103 if (count($default) == 0)
104 {
105 return array();
106 }
107
108 $retval = array();
109 foreach ($default as $val)
110 {
111 $retval[$val] = FALSE;
112 }
admin1cf89aa2006-09-03 18:24:39 +0000113 return $retval;
adminb0dd10f2006-08-25 17:25:49 +0000114 }
115
116 $segments = array_slice($this->segment_array(), ($n - 1));
117
118 $i = 0;
119 $lastval = '';
120 $retval = array();
121 foreach ($segments as $seg)
122 {
123 if ($i % 2)
124 {
125 $retval[$lastval] = $seg;
126 }
127 else
128 {
129 $retval[$seg] = FALSE;
130 $lastval = $seg;
131 }
132
133 $i++;
134 }
135
136 if (count($default) > 0)
137 {
138 foreach ($default as $val)
139 {
140 if ( ! array_key_exists($val, $retval))
141 {
142 $retval[$val] = FALSE;
143 }
144 }
145 }
146
147 // Cache the array for reuse
148 $this->keyval[$n] = $retval;
149 return $retval;
150 }
151
152 /**
153 * Generate a URI string from an associative array
154 *
155 *
156 * @access public
157 * @param array an associative array of key/values
158 * @return array
159 */ function assoc_to_uri($array)
160 {
161 $temp = array();
162 foreach ((array)$array as $key => $val)
163 {
164 $temp[] = $key;
165 $temp[] = $val;
166 }
167
168 return implode('/', $temp);
169 }
170
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Fetch a URI Segment and add a trailing slash
176 *
177 * @access public
178 * @param integer
179 * @param string
180 * @return string
181 */
182 function slash_segment($n, $where = 'trailing')
183 {
184 if ($where == 'trailing')
185 {
186 $trailing = '/';
187 $leading = '';
188 }
189 elseif ($where == 'leading')
190 {
191 $leading = '/';
192 $trailing = '';
193 }
194 else
195 {
196 $leading = '/';
197 $trailing = '/';
198 }
adminb071bb52006-08-26 19:28:37 +0000199 return ( ! isset($this->router->segments[$n])) ? '' : $leading.$this->router->segments[$n].$trailing;
adminb0dd10f2006-08-25 17:25:49 +0000200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Segment Array
206 *
207 * @access public
208 * @return array
209 */
210 function segment_array()
211 {
adminb071bb52006-08-26 19:28:37 +0000212 return $this->router->segments;
adminb0dd10f2006-08-25 17:25:49 +0000213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Total number of segments
219 *
220 * @access public
221 * @return integer
222 */
223 function total_segments()
224 {
adminb071bb52006-08-26 19:28:37 +0000225 return count($this->router->segments);
adminb0dd10f2006-08-25 17:25:49 +0000226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Fetch the entire URI string
232 *
233 * @access public
234 * @return string
235 */
236 function uri_string()
237 {
adminb071bb52006-08-26 19:28:37 +0000238 return $this->router->uri_string;
adminb0dd10f2006-08-25 17:25:49 +0000239 }
240
241}
242// END URI Class
243?>