phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
URI.php
Go to the documentation of this file.
1 <?php
2 namespace Ajax\php\yii;
3 
4 class URI {
5 
13  var $keyval=array ();
29  var $segments=array ();
38  var $rsegments=array ();
39  var $permitted_uri_chars="a-z 0-9~%.:_\-";
41 
49  var $uri_protocol="AUTO";
50  //
51 
62  function __construct() {
63  $this->uri_string=$this->_detect_uri();
64  $this->_explode_segments();
65  }
66 
67  // --------------------------------------------------------------------
68 
76  function _fetch_uri_string() {
77  $uri="";
78  if ($this->uri_protocol == 'AUTO') {
79  // Is the request coming from the command line?
80  if (php_sapi_name() == 'cli' || defined('STDIN')) {
81  $this->_set_uri_string($this->_parse_cli_args());
82  return;
83  }
84 
85  // Let's try the REQUEST_URI first, this will work in most situations
86  if ($uri=$this->_detect_uri()) {
87  $this->_set_uri_string($uri);
88  return;
89  }
90 
91  // Is there a PATH_INFO variable?
92  // Note: some servers seem to have trouble with getenv() so we'll test it two ways
93  $path=(isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
94  if (trim($path, '/') != '' && $path != "/" . SELF) {
95  $this->_set_uri_string($path);
96  return;
97  }
98 
99  // No PATH_INFO?... What about QUERY_STRING?
100  $path=(isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
101  if (trim($path, '/') != '') {
102  $this->_set_uri_string($path);
103  return;
104  }
105 
106  // As a last ditch effort lets try using the $_GET array
107  if (\is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '') {
108  $this->_set_uri_string(key($_GET));
109  return;
110  }
111 
112  // We've exhausted all our options...
113  $this->uri_string='';
114  return;
115  }
116 
117  if ($uri == 'REQUEST_URI') {
118  $this->_set_uri_string($this->_detect_uri());
119  return;
120  }
121 
122  $path=(isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
123  $this->_set_uri_string($path);
124  }
125 
126  // --------------------------------------------------------------------
127 
136  function _set_uri_string($str) {
137  // Filter out control characters
138  // $str = remove_invisible_characters($str, FALSE);
139 
140  // If the URI contains only a slash we'll kill it
141  $this->uri_string=($str == '/') ? '' : $str;
142  }
143 
144  // --------------------------------------------------------------------
145 
156  private function _detect_uri() {
157  if (!isset($_SERVER['REQUEST_URI']) || !isset($_SERVER['SCRIPT_NAME'])) {
158  return '';
159  }
160 
161  $uri=$_SERVER['REQUEST_URI'];
162  if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) {
163  $uri=substr($uri, strlen($_SERVER['SCRIPT_NAME']));
164  } elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) {
165  $uri=substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
166  }
167 
168  // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
169  // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
170  if (strncmp($uri, '?/', 2) === 0) {
171  $uri=substr($uri, 2);
172  }
173  $parts=preg_split('#\?#i', $uri, 2);
174  $uri=$parts[0];
175  if (isset($parts[1])) {
176  $_SERVER['QUERY_STRING']=$parts[1];
177  parse_str($_SERVER['QUERY_STRING'], $_GET);
178  } else {
179  $_SERVER['QUERY_STRING']='';
180  $_GET=array ();
181  }
182 
183  if ($uri == '/' || empty($uri)) {
184  return '/';
185  }
186 
187  $uri=parse_url($uri, PHP_URL_PATH);
188 
189  // Do some final cleaning of the URI and return it
190  return str_replace(array ('//','../' ), '/', trim($uri, '/'));
191  }
192 
193  // --------------------------------------------------------------------
194 
204  private function _parse_cli_args() {
205  $args=array_slice($_SERVER['argv'], 1);
206 
207  return $args ? '/' . implode('/', $args) : '';
208  }
209 
210  // --------------------------------------------------------------------
211 
220  function _filter_uri($str) {
221  if ($str != '' && $this->permitted_uri_chars != '' && $this->enable_query_strings == FALSE) {
222  // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
223  // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
224  if (!preg_match("|^[" . str_replace(array ('\\-','\-' ), '-', preg_quote($this->permitted_uri_chars, '-')) . "]+$|i", $str)) {
225  show_error('The URI you submitted has disallowed characters.', 400);
226  }
227  }
228 
229  // Convert programatic characters to entities
230  $bad=array ('$','(',')','%28','%29' );
231  $good=array ('&#36;','&#40;','&#41;','&#40;','&#41;' );
232 
233  return str_replace($bad, $good, $str);
234  }
235 
245  function _explode_segments() {
246  foreach ( explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val ) {
247  // Filter segments for security
248  $val=trim($this->_filter_uri($val));
249 
250  if ($val != '') {
251  $this->segments[]=$val;
252  }
253  }
254  }
255 
256  // --------------------------------------------------------------------
269  function _reindex_segments() {
270  array_unshift($this->segments, NULL);
271  array_unshift($this->rsegments, NULL);
272  unset($this->segments[0]);
273  unset($this->rsegments[0]);
274  }
275 
276  // --------------------------------------------------------------------
277 
289  function segment($n, $no_result=FALSE) {
290  return (!isset($this->segments[$n])) ? $no_result : $this->segments[$n];
291  }
292 
293  // --------------------------------------------------------------------
294 
308  function rsegment($n, $no_result=FALSE) {
309  return (!isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
310  }
311 
312  // --------------------------------------------------------------------
313 
336  function uri_to_assoc($n=3, $default=array()) {
337  return $this->_uri_to_assoc($n, $default, 'segment');
338  }
339 
350  function ruri_to_assoc($n=3, $default=array()) {
351  return $this->_uri_to_assoc($n, $default, 'rsegment');
352  }
353 
354  // --------------------------------------------------------------------
355 
366  function _uri_to_assoc($n=3, $default=array(), $which='segment') {
367  if ($which == 'segment') {
368  $total_segments='total_segments';
369  $segment_array='segment_array';
370  } else {
371  $total_segments='total_rsegments';
372  $segment_array='rsegment_array';
373  }
374 
375  if (!is_numeric($n)) {
376  return $default;
377  }
378 
379  if (isset($this->keyval[$n])) {
380  return $this->keyval[$n];
381  }
382 
383  if ($this->$total_segments() < $n) {
384  if (count($default) == 0) {
385  return array ();
386  }
387 
388  $retval=array ();
389  foreach ( $default as $val ) {
390  $retval[$val]=FALSE;
391  }
392  return $retval;
393  }
394 
395  $segments=array_slice($this->$segment_array(), ($n - 1));
396 
397  $i=0;
398  $lastval='';
399  $retval=array ();
400  foreach ( $segments as $seg ) {
401  if ($i % 2) {
402  $retval[$lastval]=$seg;
403  } else {
404  $retval[$seg]=FALSE;
405  $lastval=$seg;
406  }
407 
408  $i++;
409  }
410 
411  if (count($default) > 0) {
412  foreach ( $default as $val ) {
413  if (!array_key_exists($val, $retval)) {
414  $retval[$val]=FALSE;
415  }
416  }
417  }
418 
419  // Cache the array for reuse
420  $this->keyval[$n]=$retval;
421  return $retval;
422  }
423 
424  // --------------------------------------------------------------------
425 
435  function assoc_to_uri($array) {
436  $temp=array ();
437  foreach ( ( array ) $array as $key => $val ) {
438  $temp[]=$key;
439  $temp[]=$val;
440  }
441 
442  return implode('/', $temp);
443  }
444 
445  // --------------------------------------------------------------------
446 
456  function slash_segment($n, $where='trailing') {
457  return $this->_slash_segment($n, $where, 'segment');
458  }
459 
460  // --------------------------------------------------------------------
461 
471  function slash_rsegment($n, $where='trailing') {
472  return $this->_slash_segment($n, $where, 'rsegment');
473  }
474 
475  // --------------------------------------------------------------------
476 
487  function _slash_segment($n, $where='trailing', $which='segment') {
488  $leading='/';
489  $trailing='/';
490 
491  if ($where == 'trailing') {
492  $leading='';
493  } elseif ($where == 'leading') {
494  $trailing='';
495  }
496 
497  return $leading . $this->$which($n) . $trailing;
498  }
499 
500  // --------------------------------------------------------------------
501 
509  function segment_array() {
510  return $this->segments;
511  }
512 
513  // --------------------------------------------------------------------
514 
522  function rsegment_array() {
523  return $this->rsegments;
524  }
525 
526  // --------------------------------------------------------------------
527 
535  function total_segments() {
536  return count($this->segments);
537  }
538 
539  // --------------------------------------------------------------------
540 
548  function total_rsegments() {
549  return count($this->rsegments);
550  }
551 
552  // --------------------------------------------------------------------
553 
561  function uri_string() {
562  return $this->uri_string;
563  }
564 
565  // --------------------------------------------------------------------
566 
574  function ruri_string() {
575  return '/' . implode('/', $this->rsegment_array());
576  }
577 }
578 // END URI Class
_explode_segments()
Explode the URI Segments.
Definition: URI.php:245
uri_to_assoc($n=3, $default=array())
Generate a key value pair from the URI string.
Definition: URI.php:336
$enable_query_strings
Definition: URI.php:40
slash_segment($n, $where='trailing')
Fetch a URI Segment and add a trailing slash.
Definition: URI.php:456
segment($n, $no_result=FALSE)
Fetch a URI Segment.
Definition: URI.php:289
total_segments()
Total number of segments.
Definition: URI.php:535
ruri_to_assoc($n=3, $default=array())
Identical to above only it uses the re-routed segment array.
Definition: URI.php:350
_reindex_segments()
Re-index Segments.
Definition: URI.php:269
_set_uri_string($str)
Set the URI String.
Definition: URI.php:136
total_rsegments()
Total number of routed segments.
Definition: URI.php:548
rsegment($n, $no_result=FALSE)
Fetch a URI "routed" Segment.
Definition: URI.php:308
$uri_protocol
| &#39;AUTO&#39; Default - auto detects | &#39;PATH_INFO&#39; Uses the PATH_INFO | &#39;QUERY_STRING&#39; Uses the QUERY_STRI...
Definition: URI.php:49
_fetch_uri_string()
Get the URI String.
Definition: URI.php:76
_filter_uri($str)
Filter segments for malicious characters.
Definition: URI.php:220
_slash_segment($n, $where='trailing', $which='segment')
Fetch a URI Segment and add a trailing slash - helper function.
Definition: URI.php:487
rsegment_array()
Routed Segment Array.
Definition: URI.php:522
segment_array()
Segment Array.
Definition: URI.php:509
_uri_to_assoc($n=3, $default=array(), $which='segment')
Generate a key value pair from the URI string or Re-routed URI string.
Definition: URI.php:366
_detect_uri()
Detects the URI.
Definition: URI.php:156
slash_rsegment($n, $where='trailing')
Fetch a URI Segment and add a trailing slash.
Definition: URI.php:471
$permitted_uri_chars
Definition: URI.php:39
__construct()
Constructor.
Definition: URI.php:62
uri_string()
Fetch the entire URI string.
Definition: URI.php:561
assoc_to_uri($array)
Generate a URI string from an associative array.
Definition: URI.php:435
_parse_cli_args()
Parse cli arguments.
Definition: URI.php:204
ruri_string()
Fetch the entire Re-routed URI string.
Definition: URI.php:574