PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Modifications Since 08/www/events/system/libraries/URI.php

https://github.com/holsinger/openfloor
PHP | 367 lines | 151 code | 43 blank | 173 comment | 14 complexity | 90a919e3db9e8fc8e9fd88a1077f023f MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  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, EllisLab, 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. * URI Class
  18. *
  19. * Parses URIs and determines routing
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Libraries
  23. * @category URI
  24. * @author Rick Ellis
  25. * @link http://www.codeigniter.com/user_guide/libraries/uri.html
  26. */
  27. class CI_URI {
  28. var $router;
  29. var $keyval = array();
  30. /**
  31. * Constructor
  32. *
  33. * Simply globalizes the $RTR object. The front
  34. * loads the Router class early on so it's not available
  35. * normally as other classes are.
  36. *
  37. * @access public
  38. */
  39. function CI_URI()
  40. {
  41. $this->router =& load_class('Router');
  42. log_message('debug', "URI Class Initialized");
  43. }
  44. // --------------------------------------------------------------------
  45. /**
  46. * Fetch a URI Segment
  47. *
  48. * This function returns the URI segment based on the number provided.
  49. *
  50. * @access public
  51. * @param integer
  52. * @param bool
  53. * @return string
  54. */
  55. function segment($n, $no_result = FALSE)
  56. {
  57. return ( ! isset($this->router->segments[$n])) ? $no_result : $this->router->segments[$n];
  58. }
  59. // --------------------------------------------------------------------
  60. /**
  61. * Fetch a URI "routed" Segment
  62. *
  63. * This function returns the re-routed URI segment (assuming routing rules are used)
  64. * based on the number provided. If there is no routing this function returns the
  65. * same result as $this->segment()
  66. *
  67. * @access public
  68. * @param integer
  69. * @param bool
  70. * @return string
  71. */
  72. function rsegment($n, $no_result = FALSE)
  73. {
  74. return ( ! isset($this->router->rsegments[$n])) ? $no_result : $this->router->rsegments[$n];
  75. }
  76. // --------------------------------------------------------------------
  77. /**
  78. * Generate a key value pair from the URI string
  79. *
  80. * This function generates and associative array of URI data starting
  81. * at the supplied segment. For example, if this is your URI:
  82. *
  83. * www.your-site.com/user/search/name/joe/location/UK/gender/male
  84. *
  85. * You can use this function to generate an array with this prototype:
  86. *
  87. * array (
  88. * name => joe
  89. * location => UK
  90. * gender => male
  91. * )
  92. *
  93. * @access public
  94. * @param integer the starting segment number
  95. * @param array an array of default values
  96. * @return array
  97. */
  98. function uri_to_assoc($n = 3, $default = array())
  99. {
  100. return $this->_uri_to_assoc($n, $default, 'segment');
  101. }
  102. /**
  103. * Identical to above only it uses the re-routed segment array
  104. *
  105. */
  106. function ruri_to_assoc($n = 3, $default = array())
  107. {
  108. return $this->_uri_to_assoc($n, $default, 'rsegment');
  109. }
  110. // --------------------------------------------------------------------
  111. /**
  112. * Generate a key value pair from the URI string or Re-routed URI string
  113. *
  114. * @access private
  115. * @param integer the starting segment number
  116. * @param array an array of default values
  117. * @param string which array we should use
  118. * @return array
  119. */
  120. function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
  121. {
  122. if ($which == 'segment')
  123. {
  124. $total_segments = 'total_segments';
  125. $segment_array = 'segment_array';
  126. }
  127. else
  128. {
  129. $total_segments = 'total_rsegments';
  130. $segment_array = 'rsegment_array';
  131. }
  132. if ( ! is_numeric($n))
  133. {
  134. return $default;
  135. }
  136. if (isset($this->keyval[$n]))
  137. {
  138. return $this->keyval[$n];
  139. }
  140. if ($this->$total_segments() < $n)
  141. {
  142. if (count($default) == 0)
  143. {
  144. return array();
  145. }
  146. $retval = array();
  147. foreach ($default as $val)
  148. {
  149. $retval[$val] = FALSE;
  150. }
  151. return $retval;
  152. }
  153. $segments = array_slice($this->$segment_array(), ($n - 1));
  154. $i = 0;
  155. $lastval = '';
  156. $retval = array();
  157. foreach ($segments as $seg)
  158. {
  159. if ($i % 2)
  160. {
  161. $retval[$lastval] = $seg;
  162. }
  163. else
  164. {
  165. $retval[$seg] = FALSE;
  166. $lastval = $seg;
  167. }
  168. $i++;
  169. }
  170. if (count($default) > 0)
  171. {
  172. foreach ($default as $val)
  173. {
  174. if ( ! array_key_exists($val, $retval))
  175. {
  176. $retval[$val] = FALSE;
  177. }
  178. }
  179. }
  180. // Cache the array for reuse
  181. $this->keyval[$n] = $retval;
  182. return $retval;
  183. }
  184. /**
  185. * Generate a URI string from an associative array
  186. *
  187. *
  188. * @access public
  189. * @param array an associative array of key/values
  190. * @return array
  191. */ function assoc_to_uri($array)
  192. {
  193. $temp = array();
  194. foreach ((array)$array as $key => $val)
  195. {
  196. $temp[] = $key;
  197. $temp[] = $val;
  198. }
  199. return implode('/', $temp);
  200. }
  201. // --------------------------------------------------------------------
  202. /**
  203. * Fetch a URI Segment and add a trailing slash
  204. *
  205. * @access public
  206. * @param integer
  207. * @param string
  208. * @return string
  209. */
  210. function slash_segment($n, $where = 'trailing')
  211. {
  212. return $this->_slash_segment($n, $where, 'segment');
  213. }
  214. // --------------------------------------------------------------------
  215. /**
  216. * Fetch a URI Segment and add a trailing slash
  217. *
  218. * @access public
  219. * @param integer
  220. * @param string
  221. * @return string
  222. */
  223. function slash_rsegment($n, $where = 'trailing')
  224. {
  225. return $this->_slash_segment($n, $where, 'rsegment');
  226. }
  227. // --------------------------------------------------------------------
  228. /**
  229. * Fetch a URI Segment and add a trailing slash - helper function
  230. *
  231. * @access private
  232. * @param integer
  233. * @param string
  234. * @param string
  235. * @return string
  236. */
  237. function _slash_segment($n, $where = 'trailing', $which = 'segment')
  238. {
  239. if ($where == 'trailing')
  240. {
  241. $trailing = '/';
  242. $leading = '';
  243. }
  244. elseif ($where == 'leading')
  245. {
  246. $leading = '/';
  247. $trailing = '';
  248. }
  249. else
  250. {
  251. $leading = '/';
  252. $trailing = '/';
  253. }
  254. return $leading.$this->$which($n).$trailing;
  255. }
  256. // --------------------------------------------------------------------
  257. /**
  258. * Segment Array
  259. *
  260. * @access public
  261. * @return array
  262. */
  263. function segment_array()
  264. {
  265. return $this->router->segments;
  266. }
  267. // --------------------------------------------------------------------
  268. /**
  269. * Routed Segment Array
  270. *
  271. * @access public
  272. * @return array
  273. */
  274. function rsegment_array()
  275. {
  276. return $this->router->rsegments;
  277. }
  278. // --------------------------------------------------------------------
  279. /**
  280. * Total number of segments
  281. *
  282. * @access public
  283. * @return integer
  284. */
  285. function total_segments()
  286. {
  287. return count($this->router->segments);
  288. }
  289. // --------------------------------------------------------------------
  290. /**
  291. * Total number of routed segments
  292. *
  293. * @access public
  294. * @return integer
  295. */
  296. function total_rsegments()
  297. {
  298. return count($this->router->rsegments);
  299. }
  300. // --------------------------------------------------------------------
  301. /**
  302. * Fetch the entire URI string
  303. *
  304. * @access public
  305. * @return string
  306. */
  307. function uri_string()
  308. {
  309. return $this->router->uri_string;
  310. }
  311. // --------------------------------------------------------------------
  312. /**
  313. * Fetch the entire Re-routed URI string
  314. *
  315. * @access public
  316. * @return string
  317. */
  318. function ruri_string()
  319. {
  320. return '/'.implode('/', $this->rsegment_array()).'/';
  321. }
  322. }
  323. // END URI Class
  324. ?>