123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 |
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class CI_URI {
-
- public $keyval = array();
-
- public $uri_string = '';
-
- public $segments = array();
-
- public $rsegments = array();
-
- protected $_permitted_uri_chars;
-
- public function __construct()
- {
- $this->config =& load_class('Config', 'core');
-
-
- if (is_cli() OR $this->config->item('enable_query_strings') !== TRUE)
- {
- $this->_permitted_uri_chars = $this->config->item('permitted_uri_chars');
-
- if (is_cli())
- {
- $uri = $this->_parse_argv();
- }
- else
- {
- $protocol = $this->config->item('uri_protocol');
- empty($protocol) && $protocol = 'REQUEST_URI';
- switch ($protocol)
- {
- case 'AUTO':
- case 'REQUEST_URI':
- $uri = $this->_parse_request_uri();
- break;
- case 'QUERY_STRING':
- $uri = $this->_parse_query_string();
- break;
- case 'PATH_INFO':
- default:
- $uri = isset($_SERVER[$protocol])
- ? $_SERVER[$protocol]
- : $this->_parse_request_uri();
- break;
- }
- }
- $this->_set_uri_string($uri);
- }
- log_message('info', 'URI Class Initialized');
- }
-
-
- protected function _set_uri_string($str)
- {
-
- $this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
- if ($this->uri_string !== '')
- {
-
- if (($suffix = (string) $this->config->item('url_suffix')) !== '')
- {
- $slen = strlen($suffix);
- if (substr($this->uri_string, -$slen) === $suffix)
- {
- $this->uri_string = substr($this->uri_string, 0, -$slen);
- }
- }
- $this->segments[0] = NULL;
-
- foreach (explode('/', trim($this->uri_string, '/')) as $val)
- {
- $val = trim($val);
-
- $this->filter_uri($val);
- if ($val !== '')
- {
- $this->segments[] = $val;
- }
- }
- unset($this->segments[0]);
- }
- }
-
-
- protected function _parse_request_uri()
- {
- if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
- {
- return '';
- }
-
-
- $uri = parse_url('http://dummy'.$_SERVER['REQUEST_URI']);
- $query = isset($uri['query']) ? $uri['query'] : '';
- $uri = isset($uri['path']) ? $uri['path'] : '';
- if (isset($_SERVER['SCRIPT_NAME'][0]))
- {
- if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
- {
- $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
- }
- elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
- {
- $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
- }
- }
-
-
- if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
- {
- $query = explode('?', $query, 2);
- $uri = $query[0];
- $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
- }
- else
- {
- $_SERVER['QUERY_STRING'] = $query;
- }
- parse_str($_SERVER['QUERY_STRING'], $_GET);
- if ($uri === '/' OR $uri === '')
- {
- return '/';
- }
-
- return $this->_remove_relative_directory($uri);
- }
-
-
- protected function _parse_query_string()
- {
- $uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
- if (trim($uri, '/') === '')
- {
- return '';
- }
- elseif (strncmp($uri, '/', 1) === 0)
- {
- $uri = explode('?', $uri, 2);
- $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
- $uri = $uri[0];
- }
- parse_str($_SERVER['QUERY_STRING'], $_GET);
- return $this->_remove_relative_directory($uri);
- }
-
-
- protected function _parse_argv()
- {
- $args = array_slice($_SERVER['argv'], 1);
- return $args ? implode('/', $args) : '';
- }
-
-
- protected function _remove_relative_directory($uri)
- {
- $uris = array();
- $tok = strtok($uri, '/');
- while ($tok !== FALSE)
- {
- if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
- {
- $uris[] = $tok;
- }
- $tok = strtok('/');
- }
- return implode('/', $uris);
- }
-
-
- public function filter_uri(&$str)
- {
- if ( ! empty($str) && ! empty($this->_permitted_uri_chars) && ! preg_match('/^['.$this->_permitted_uri_chars.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $str))
- {
- show_error('The URI you submitted has disallowed characters.', 400);
- }
- }
-
-
- public function segment($n, $no_result = NULL)
- {
- return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
- }
-
-
- public function rsegment($n, $no_result = NULL)
- {
- return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
- }
-
-
- public function uri_to_assoc($n = 3, $default = array())
- {
- return $this->_uri_to_assoc($n, $default, 'segment');
- }
-
-
- public function ruri_to_assoc($n = 3, $default = array())
- {
- return $this->_uri_to_assoc($n, $default, 'rsegment');
- }
-
-
- protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
- {
- if ( ! is_numeric($n))
- {
- return $default;
- }
- if (isset($this->keyval[$which], $this->keyval[$which][$n]))
- {
- return $this->keyval[$which][$n];
- }
- $total_segments = "total_{$which}s";
- $segment_array = "{$which}_array";
- if ($this->$total_segments() < $n)
- {
- return (count($default) === 0)
- ? array()
- : array_fill_keys($default, NULL);
- }
- $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] = NULL;
- $lastval = $seg;
- }
- $i++;
- }
- if (count($default) > 0)
- {
- foreach ($default as $val)
- {
- if ( ! array_key_exists($val, $retval))
- {
- $retval[$val] = NULL;
- }
- }
- }
-
- isset($this->keyval[$which]) OR $this->keyval[$which] = array();
- $this->keyval[$which][$n] = $retval;
- return $retval;
- }
-
-
- public function assoc_to_uri($array)
- {
- $temp = array();
- foreach ((array) $array as $key => $val)
- {
- $temp[] = $key;
- $temp[] = $val;
- }
- return implode('/', $temp);
- }
-
-
- public function slash_segment($n, $where = 'trailing')
- {
- return $this->_slash_segment($n, $where, 'segment');
- }
-
-
- public function slash_rsegment($n, $where = 'trailing')
- {
- return $this->_slash_segment($n, $where, 'rsegment');
- }
-
-
- protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
- {
- $leading = $trailing = '/';
- if ($where === 'trailing')
- {
- $leading = '';
- }
- elseif ($where === 'leading')
- {
- $trailing = '';
- }
- return $leading.$this->$which($n).$trailing;
- }
-
-
- public function segment_array()
- {
- return $this->segments;
- }
-
-
- public function rsegment_array()
- {
- return $this->rsegments;
- }
-
-
- public function total_segments()
- {
- return count($this->segments);
- }
-
-
- public function total_rsegments()
- {
- return count($this->rsegments);
- }
-
-
- public function uri_string()
- {
- return $this->uri_string;
- }
-
-
- public function ruri_string()
- {
- return ltrim(load_class('Router', 'core')->directory, '/').implode('/', $this->rsegments);
- }
- }
|