admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?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 | */ |
| 29 | class CI_URI { |
| 30 | |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 31 | var $router; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 32 | 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 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 45 | $this->router =& _load_class('CI_Router'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 46 | 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 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 63 | return ( ! isset($this->router->segments[$n])) ? $no_result : $this->router->segments[$n]; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 64 | } |
| 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 | { |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 91 | if ( ! is_numeric($n)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 92 | { |
| 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 | } |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 113 | return $retval; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 114 | } |
| 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 | } |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 199 | return ( ! isset($this->router->segments[$n])) ? '' : $leading.$this->router->segments[$n].$trailing; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
| 205 | * Segment Array |
| 206 | * |
| 207 | * @access public |
| 208 | * @return array |
| 209 | */ |
| 210 | function segment_array() |
| 211 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 212 | return $this->router->segments; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // -------------------------------------------------------------------- |
| 216 | |
| 217 | /** |
| 218 | * Total number of segments |
| 219 | * |
| 220 | * @access public |
| 221 | * @return integer |
| 222 | */ |
| 223 | function total_segments() |
| 224 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 225 | return count($this->router->segments); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // -------------------------------------------------------------------- |
| 229 | |
| 230 | /** |
| 231 | * Fetch the entire URI string |
| 232 | * |
| 233 | * @access public |
| 234 | * @return string |
| 235 | */ |
| 236 | function uri_string() |
| 237 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 238 | return $this->router->uri_string; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | } |
| 242 | // END URI Class |
| 243 | ?> |