blob: c5fd4625bd782554dd04624b7172771c5716e516 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Code Igniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author Rick Ellis
* @copyright Copyright (c) 2006, pMachine, Inc.
* @license http://www.codeignitor.com/user_guide/license.html
* @link http://www.codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* URI Class
*
* Parses URIs and determines routing
*
* @package CodeIgniter
* @subpackage Libraries
* @category URI
* @author Rick Ellis
* @link http://www.codeigniter.com/user_guide/libraries/uri.html
*/
class CI_URI {
var $router;
var $keyval = array();
/**
* Constructor
*
* Simply globalizes the $RTR object. The front
* loads the Router class early on so it's not available
* normally as other classes are.
*
* @access public
*/
function CI_URI()
{
$this->router =& _load_class('CI_Router');
log_message('debug', "URI Class Initialized");
}
// --------------------------------------------------------------------
/**
* Fetch a URI Segment
*
* This function returns the URI segment based on the number provided.
*
* @access public
* @param integer
* @param bool
* @return string
*/
function segment($n, $no_result = FALSE)
{
return ( ! isset($this->router->segments[$n])) ? $no_result : $this->router->segments[$n];
}
// --------------------------------------------------------------------
/**
* Generate a key value pair from the URI string
*
* This function generates and associative array of URI data starting
* at the supplied segment. For example, if this is your URI:
*
* www.your-site.com/user/search/name/joe/location/UK/gender/male
*
* You can use this function to generate an array with this prototype:
*
* array (
* name => joe
* location => UK
* gender => male
* )
*
* @access public
* @param integer the starting segment number
* @param array an array of default values
* @return array
*/
function uri_to_assoc($n = 3, $default = array())
{
if ( ! ctype_digit($n))
{
return $default;
}
if (isset($this->keyval[$n]))
{
return $this->keyval[$n];
}
if ($this->total_segments() < $n)
{
if (count($default) == 0)
{
return array();
}
$retval = array();
foreach ($default as $val)
{
$retval[$val] = FALSE;
}
return $default;
}
$segments = array_slice($this->segment_array(), ($n - 1));
$i = 0;
$lastval = '';
$retval = array();
foreach ($segments as $seg)
{
if ($i % 2)
{
$retval[$lastval] = $seg;
}
else
{
$retval[$seg] = FALSE;
$lastval = $seg;
}
$i++;
}
if (count($default) > 0)
{
foreach ($default as $val)
{
if ( ! array_key_exists($val, $retval))
{
$retval[$val] = FALSE;
}
}
}
// Cache the array for reuse
$this->keyval[$n] = $retval;
return $retval;
}
/**
* Generate a URI string from an associative array
*
*
* @access public
* @param array an associative array of key/values
* @return array
*/ function assoc_to_uri($array)
{
$temp = array();
foreach ((array)$array as $key => $val)
{
$temp[] = $key;
$temp[] = $val;
}
return implode('/', $temp);
}
// --------------------------------------------------------------------
/**
* Fetch a URI Segment and add a trailing slash
*
* @access public
* @param integer
* @param string
* @return string
*/
function slash_segment($n, $where = 'trailing')
{
if ($where == 'trailing')
{
$trailing = '/';
$leading = '';
}
elseif ($where == 'leading')
{
$leading = '/';
$trailing = '';
}
else
{
$leading = '/';
$trailing = '/';
}
return ( ! isset($this->router->segments[$n])) ? '' : $leading.$this->router->segments[$n].$trailing;
}
// --------------------------------------------------------------------
/**
* Segment Array
*
* @access public
* @return array
*/
function segment_array()
{
return $this->router->segments;
}
// --------------------------------------------------------------------
/**
* Total number of segments
*
* @access public
* @return integer
*/
function total_segments()
{
return count($this->router->segments);
}
// --------------------------------------------------------------------
/**
* Fetch the entire URI string
*
* @access public
* @return string
*/
function uri_string()
{
return $this->router->uri_string;
}
}
// END URI Class
?>
|