PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/ecerp/System/Libraries/SP_Uri.php

http://phpfor.googlecode.com/
PHP | 279 lines | 164 code | 40 blank | 75 comment | 50 complexity | f4b17641d905ff07ad95361c3f8861eb MD5 | raw file
  1. <?php
  2. class SP_Uri{
  3. var $uri_string = FALSE;
  4. var $segments = Array();
  5. var $rsegments = Array();
  6. var $routes = Array();
  7. var $class = '';
  8. var $method = 'index';
  9. var $default_controller;
  10. function SP_Uri(){
  11. $this->config =& Loader::config();
  12. $this->_set_routing();
  13. }
  14. function _set_routing(){
  15. @include(APPBASE.'Config/routes.php');
  16. $this->routes = ( ! isset($ROUTE) OR ! is_array($ROUTE)) ? array() : $ROUTE;
  17. unset($ROUTE);
  18. $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
  19. $this->_fetch_uri_string();
  20. if ($this->uri_string == ''){
  21. if ($this->default_controller === FALSE){
  22. showError(__T('Unable to determine what should be displayed. A default route has not been specified in the routing file.'));
  23. }
  24. if(!file_exists(APPBASE.'Controllers/'.$this->default_controller.'.php')){
  25. showError(__T('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.'));
  26. }
  27. $this->class = $this->default_controller;
  28. $this->method = 'index';
  29. $this->_reindex_segments();
  30. return;
  31. }
  32. $this->_remove_url_suffix();
  33. $this->_explode_segments();
  34. $this->_parse_routes();
  35. $this->_reindex_segments();
  36. }
  37. function _parse_routes(){
  38. // Do we even have any custom routing to deal with?
  39. // There is a default scaffolding trigger, so we'll look just for 1
  40. if (count($this->routes) == 1)
  41. {
  42. $this->_set_request($this->segments);
  43. return;
  44. }
  45. // Turn the segment array into a URI string
  46. $uri = implode('/', $this->segments);
  47. // Is there a literal match? If so we're done
  48. if (isset($this->routes[$uri]))
  49. {
  50. $this->_set_request(explode('/', $this->routes[$uri]));
  51. return;
  52. }
  53. // Loop through the route array looking for wild-cards
  54. foreach ($this->routes as $key => $val)
  55. {
  56. // Convert wild-cards to RegEx
  57. $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
  58. // Does the RegEx match?
  59. if (preg_match('#^'.$key.'$#', $uri))
  60. {
  61. // Do we have a back-reference?
  62. if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
  63. {
  64. $val = preg_replace('#^'.$key.'$#', $val, $uri);
  65. }
  66. $this->_set_request(explode('/', $val));
  67. return;
  68. }
  69. }
  70. // If we got this far it means we didn't encounter a
  71. // matching route so we'll set the site default route
  72. $this->_set_request($this->segments);
  73. }
  74. function _set_request($segments = array()){
  75. //$segments = $this->_validate_request($segments);
  76. if (count($segments) == 0)
  77. {
  78. return;
  79. }
  80. $this->class = $segments[0];
  81. if (isset($segments[1])){
  82. // A standard method request
  83. $this->method = $segments[1];
  84. }else{
  85. // This lets the "routed" segment array identify that the default
  86. // index method is being used.
  87. $segments[1] = 'index';
  88. }
  89. // Update our "routed" segment array to contain the segments.
  90. // Note: If there is no custom routing, this array will be
  91. // identical to $this->uri->segments
  92. $this->rsegments = $segments;
  93. }
  94. function _fetch_uri_string(){
  95. if (strtoupper($this->config['uri_protocol']) == 'AUTO'){
  96. // Is there a PATH_INFO variable?
  97. // Note: some servers seem to have trouble with getenv() so we'll test it two ways
  98. $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
  99. if (trim($path, '/') != '' && $path != "/".SELF){
  100. $this->uri_string = $path;
  101. return;
  102. }
  103. // No PATH_INFO?... What about QUERY_STRING?
  104. $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
  105. if (trim($path, '/') != ''){
  106. $uri_arr = explode('&',str_replace('&amp;','&',$path));
  107. $this->uri_string = $uri_arr[0];
  108. return;
  109. }
  110. // No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists?
  111. $path = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO');
  112. if (trim($path, '/') != '' && $path != "/".SELF){
  113. // remove path and script information so we have good URI data
  114. $this->uri_string = str_replace($_SERVER['SCRIPT_NAME'], '', $path);
  115. return;
  116. }
  117. // We've exhausted all our options...
  118. $this->uri_string = '';
  119. }
  120. else{
  121. $uri = strtoupper($this->config['uri_protocol']);
  122. if ($uri == 'REQUEST_URI'){
  123. $this->uri_string = $this->_parse_request_uri();
  124. return;
  125. }elseif ($uri == 'PATH_INFO'){
  126. $this->uri_string = $this->_parse_path_info();
  127. }elseif ($uri == 'QUERY_STRING'){
  128. $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
  129. $uri_arr = explode('&',str_replace('&amp;','&',$path));
  130. $this->uri_string = $uri_arr[0];
  131. }else{
  132. $this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
  133. }
  134. }
  135. // If the URI contains only a slash we'll kill it
  136. if ($this->uri_string == '/'){
  137. $this->uri_string = '';
  138. }
  139. }
  140. // --------------------------------------------------------------------
  141. function _parse_path_info(){
  142. if ( ! isset($_SERVER['PATH_INFO']) || $_SERVER['PATH_INFO'] == ''){
  143. $strlen = strlen(_getScriptPath());
  144. $totallen = strlen($_SERVER['PHP_SELF']);
  145. return substr($_SERVER['PHP_SELF'],$strlen,$totallen);
  146. }else{
  147. return $_SERVER['PATH_INFO'];
  148. }
  149. }
  150. /**
  151. * Parse the REQUEST_URI
  152. *
  153. * @access private
  154. * @return string
  155. */
  156. function _parse_request_uri(){
  157. if ( ! isset($_SERVER['REQUEST_URI']) || $_SERVER['REQUEST_URI'] == ''){
  158. return '';
  159. }
  160. $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI']));
  161. if ($request_uri == '' || $request_uri == SELF){
  162. return '';
  163. }
  164. $fc_path = FCPATH;
  165. $spos = strpos($request_uri, '?');
  166. if ($spos !== FALSE){
  167. $fc_path .= '?';
  168. $request_uri = substr($request_uri,0,$spos);
  169. }
  170. $fc_path = str_replace("\\", "/", $fc_path);
  171. $parsed_uri = explode("/", $request_uri);
  172. $i = 0;
  173. foreach(explode("/", $fc_path) as $segment){
  174. if (isset($parsed_uri[$i]) && $segment == $parsed_uri[$i]){
  175. $i++;
  176. }
  177. }
  178. $parsed_uri = implode("/", array_slice($parsed_uri, $i));
  179. if ($parsed_uri != ''){
  180. $parsed_uri = '/'.$parsed_uri;
  181. }
  182. return $parsed_uri;
  183. }
  184. function _filter_uri($str){
  185. if ($str != ''){
  186. if ( ! preg_match("|^[a-z 0-9~%.:_\-]+$|i", urlencode($str))){
  187. showError(__T('The URI you submitted has disallowed characters.'));
  188. }
  189. }
  190. // Convert programatic characters to entities
  191. $bad = array('$', '(', ')', '%28', '%29');
  192. $good = array('&#36;', '&#40;', '&#41;', '&#40;', '&#41;');
  193. return str_replace($bad, $good, $str);
  194. }
  195. function _explode_segments(){
  196. foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val){
  197. // Filter segments for security
  198. $val = trim($this->_filter_uri($val));
  199. if ($val != ''){
  200. $this->segments[] = $val;
  201. }
  202. }
  203. }
  204. function _reindex_segments()
  205. {
  206. array_unshift($this->segments, NULL);
  207. array_unshift($this->rsegments, NULL);
  208. unset($this->segments[0]);
  209. unset($this->rsegments[0]);
  210. }
  211. function _remove_url_suffix(){
  212. if ($this->config['url_suffix'] != ""){
  213. $this->uri_string = preg_replace("|".preg_quote($this->config['url_suffix'])."$|", "", $this->uri_string);
  214. }
  215. }
  216. function segment($n, $no_result = FALSE){
  217. return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
  218. }
  219. function rsegment($n, $no_result = FALSE){
  220. return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
  221. }
  222. function getRsegments(){
  223. return $this->rsegments;
  224. }
  225. function getSegments(){
  226. return $this->segments;
  227. }
  228. function getClass(){
  229. return $this->class;
  230. }
  231. function getMethod(){
  232. return $this->method;
  233. }
  234. }