2019-12-18 16:07:09 -05:00
|
|
|
<?php
|
2014-08-08 15:41:59 -04:00
|
|
|
/**
|
|
|
|
* CodeIgniter
|
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* An open source application development framework for PHP
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* This content is released under the MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 - 2019, British Columbia Institute of Technology
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @package CodeIgniter
|
|
|
|
* @author EllisLab Dev Team
|
|
|
|
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
|
|
|
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
|
|
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
|
|
* @link https://codeigniter.com
|
|
|
|
* @since Version 1.0.0
|
2014-08-08 15:41:59 -04:00
|
|
|
* @filesource
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
2014-08-08 15:41:59 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* URI Class
|
|
|
|
*
|
|
|
|
* Parses URIs and determines routing
|
|
|
|
*
|
|
|
|
* @package CodeIgniter
|
|
|
|
* @subpackage Libraries
|
|
|
|
* @category URI
|
2019-12-18 16:07:09 -05:00
|
|
|
* @author EllisLab Dev Team
|
|
|
|
* @link https://codeigniter.com/user_guide/libraries/uri.html
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
|
|
|
class CI_URI {
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* List of cached URI segments
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @var array
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public $keyval = array();
|
|
|
|
|
2014-08-08 15:41:59 -04:00
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Current URI string
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @var string
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public $uri_string = '';
|
|
|
|
|
2014-08-08 15:41:59 -04:00
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* List of URI segments
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Starts at 1 instead of 0.
|
|
|
|
*
|
|
|
|
* @var array
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public $segments = array();
|
|
|
|
|
2014-08-08 15:41:59 -04:00
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* List of routed URI segments
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Starts at 1 instead of 0.
|
|
|
|
*
|
|
|
|
* @var array
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public $rsegments = array();
|
2014-08-08 15:41:59 -04:00
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Permitted URI chars
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* PCRE character group allowed in URI segments
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @var string
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
protected $_permitted_uri_chars;
|
2014-08-08 15:41:59 -04:00
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Class constructor
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @return void
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function __construct()
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$this->config =& load_class('Config', 'core');
|
2014-08-08 15:41:59 -04:00
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
// If query strings are enabled, we don't need to parse any segments.
|
|
|
|
// However, they don't make sense under CLI.
|
|
|
|
if (is_cli() OR $this->config->item('enable_query_strings') !== TRUE)
|
|
|
|
{
|
|
|
|
$this->_permitted_uri_chars = $this->config->item('permitted_uri_chars');
|
2014-08-08 15:41:59 -04:00
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
// If it's a CLI request, ignore the configuration
|
|
|
|
if (is_cli())
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$uri = $this->_parse_argv();
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
2019-12-18 16:07:09 -05:00
|
|
|
else
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$protocol = $this->config->item('uri_protocol');
|
|
|
|
empty($protocol) && $protocol = 'REQUEST_URI';
|
2014-08-08 15:41:59 -04:00
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
switch ($protocol)
|
|
|
|
{
|
|
|
|
case 'AUTO': // For BC purposes only
|
|
|
|
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;
|
|
|
|
}
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
$this->_set_uri_string($uri);
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
log_message('info', 'URI Class Initialized');
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Set URI String
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @param string $str
|
|
|
|
* @return void
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
protected function _set_uri_string($str)
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
// Filter out control characters and trim slashes
|
|
|
|
$this->uri_string = trim(remove_invisible_characters($str, FALSE), '/');
|
|
|
|
|
|
|
|
if ($this->uri_string !== '')
|
|
|
|
{
|
|
|
|
// Remove the URL suffix, if present
|
|
|
|
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;
|
|
|
|
// Populate the segments array
|
|
|
|
foreach (explode('/', trim($this->uri_string, '/')) as $val)
|
|
|
|
{
|
|
|
|
$val = trim($val);
|
|
|
|
// Filter segments for security
|
|
|
|
$this->filter_uri($val);
|
|
|
|
|
|
|
|
if ($val !== '')
|
|
|
|
{
|
|
|
|
$this->segments[] = $val;
|
|
|
|
}
|
|
|
|
}
|
2014-08-08 15:41:59 -04:00
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
unset($this->segments[0]);
|
|
|
|
}
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Parse REQUEST_URI
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Will parse REQUEST_URI and automatically detect the URI from it,
|
|
|
|
* while fixing the query string if necessary.
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
protected function _parse_request_uri()
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
// parse_url() returns false if no host is present, but the path or query string
|
|
|
|
// contains a colon followed by a number
|
|
|
|
$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]))
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
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'])));
|
|
|
|
}
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
|
|
|
|
// URI is found, and also fixes the QUERY_STRING server var and $_GET array.
|
2019-12-18 16:07:09 -05:00
|
|
|
if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$query = explode('?', $query, 2);
|
|
|
|
$uri = $query[0];
|
|
|
|
$_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$_SERVER['QUERY_STRING'] = $query;
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
parse_str($_SERVER['QUERY_STRING'], $_GET);
|
|
|
|
|
|
|
|
if ($uri === '/' OR $uri === '')
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do some final cleaning of the URI and return it
|
2019-12-18 16:07:09 -05:00
|
|
|
return $this->_remove_relative_directory($uri);
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Parse QUERY_STRING
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Will parse QUERY_STRING and automatically detect the URI from it.
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
protected function _parse_query_string()
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
|
2014-08-08 15:41:59 -04:00
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
if (trim($uri, '/') === '')
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
elseif (strncmp($uri, '/', 1) === 0)
|
|
|
|
{
|
|
|
|
$uri = explode('?', $uri, 2);
|
|
|
|
$_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
|
|
|
|
$uri = $uri[0];
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
parse_str($_SERVER['QUERY_STRING'], $_GET);
|
2014-08-08 15:41:59 -04:00
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
return $this->_remove_relative_directory($uri);
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Parse CLI arguments
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Take each command line argument and assume it is a URI segment.
|
|
|
|
*
|
|
|
|
* @return string
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
protected function _parse_argv()
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$args = array_slice($_SERVER['argv'], 1);
|
|
|
|
return $args ? implode('/', $args) : '';
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Remove relative directory (../) and multi slashes (///)
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Do some final cleaning of the URI and return it, currently only used in self::_parse_request_uri()
|
|
|
|
*
|
|
|
|
* @param string $uri
|
|
|
|
* @return string
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
protected function _remove_relative_directory($uri)
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$uris = array();
|
|
|
|
$tok = strtok($uri, '/');
|
|
|
|
while ($tok !== FALSE)
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$uris[] = $tok;
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
2019-12-18 16:07:09 -05:00
|
|
|
$tok = strtok('/');
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
2019-12-18 16:07:09 -05:00
|
|
|
|
|
|
|
return implode('/', $uris);
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
2019-12-18 16:07:09 -05:00
|
|
|
|
2014-08-08 15:41:59 -04:00
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Filter URI
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Filters segments for malicious characters.
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @param string $str
|
2014-08-08 15:41:59 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function filter_uri(&$str)
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
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);
|
|
|
|
}
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Fetch URI Segment
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @see CI_URI::$segments
|
|
|
|
* @param int $n Index
|
|
|
|
* @param mixed $no_result What to return if the segment index is not found
|
|
|
|
* @return mixed
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function segment($n, $no_result = NULL)
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
return isset($this->segments[$n]) ? $this->segments[$n] : $no_result;
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Fetch URI "routed" Segment
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Returns the re-routed URI segment (assuming routing rules are used)
|
|
|
|
* based on the index provided. If there is no routing, will return
|
|
|
|
* the same result as CI_URI::segment().
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @see CI_URI::$rsegments
|
|
|
|
* @see CI_URI::segment()
|
|
|
|
* @param int $n Index
|
|
|
|
* @param mixed $no_result What to return if the segment index is not found
|
|
|
|
* @return mixed
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function rsegment($n, $no_result = NULL)
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
return isset($this->rsegments[$n]) ? $this->rsegments[$n] : $no_result;
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* URI to assoc
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Generates an associative array of URI data starting at the supplied
|
|
|
|
* segment index. For example, if this is your URI:
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
|
|
|
* example.com/user/search/name/joe/location/UK/gender/male
|
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* You can use this method to generate an array with this prototype:
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* array (
|
|
|
|
* name => joe
|
|
|
|
* location => UK
|
|
|
|
* gender => male
|
|
|
|
* )
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @param int $n Index (default: 3)
|
|
|
|
* @param array $default Default values
|
2014-08-08 15:41:59 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function uri_to_assoc($n = 3, $default = array())
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return $this->_uri_to_assoc($n, $default, 'segment');
|
|
|
|
}
|
2019-12-18 16:07:09 -05:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
2014-08-08 15:41:59 -04:00
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Routed URI to assoc
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Identical to CI_URI::uri_to_assoc(), only it uses the re-routed
|
|
|
|
* segment array.
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @see CI_URI::uri_to_assoc()
|
|
|
|
* @param int $n Index (default: 3)
|
|
|
|
* @param array $default Default values
|
|
|
|
* @return array
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function ruri_to_assoc($n = 3, $default = array())
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return $this->_uri_to_assoc($n, $default, 'rsegment');
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Internal URI-to-assoc
|
|
|
|
*
|
|
|
|
* Generates a key/value pair from the URI string or re-routed URI string.
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @used-by CI_URI::uri_to_assoc()
|
|
|
|
* @used-by CI_URI::ruri_to_assoc()
|
|
|
|
* @param int $n Index (default: 3)
|
|
|
|
* @param array $default Default values
|
|
|
|
* @param string $which Array name ('segment' or 'rsegment')
|
2014-08-08 15:41:59 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
if ( ! is_numeric($n))
|
|
|
|
{
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
if (isset($this->keyval[$which], $this->keyval[$which][$n]))
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
return $this->keyval[$which][$n];
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
$total_segments = "total_{$which}s";
|
|
|
|
$segment_array = "{$which}_array";
|
|
|
|
|
2014-08-08 15:41:59 -04:00
|
|
|
if ($this->$total_segments() < $n)
|
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
return (count($default) === 0)
|
|
|
|
? array()
|
|
|
|
: array_fill_keys($default, NULL);
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$segments = array_slice($this->$segment_array(), ($n - 1));
|
|
|
|
$i = 0;
|
|
|
|
$lastval = '';
|
2019-12-18 16:07:09 -05:00
|
|
|
$retval = array();
|
2014-08-08 15:41:59 -04:00
|
|
|
foreach ($segments as $seg)
|
|
|
|
{
|
|
|
|
if ($i % 2)
|
|
|
|
{
|
|
|
|
$retval[$lastval] = $seg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$retval[$seg] = NULL;
|
2014-08-08 15:41:59 -04:00
|
|
|
$lastval = $seg;
|
|
|
|
}
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($default) > 0)
|
|
|
|
{
|
|
|
|
foreach ($default as $val)
|
|
|
|
{
|
|
|
|
if ( ! array_key_exists($val, $retval))
|
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$retval[$val] = NULL;
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the array for reuse
|
2019-12-18 16:07:09 -05:00
|
|
|
isset($this->keyval[$which]) OR $this->keyval[$which] = array();
|
|
|
|
$this->keyval[$which][$n] = $retval;
|
2014-08-08 15:41:59 -04:00
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Assoc to URI
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Generates a URI string from an associative array.
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @param array $array Input array of key/value pairs
|
|
|
|
* @return string URI string
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function assoc_to_uri($array)
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
$temp = array();
|
2019-12-18 16:07:09 -05:00
|
|
|
foreach ((array) $array as $key => $val)
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
$temp[] = $key;
|
|
|
|
$temp[] = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode('/', $temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Slash segment
|
|
|
|
*
|
|
|
|
* Fetches an URI segment with a slash.
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @param int $n Index
|
|
|
|
* @param string $where Where to add the slash ('trailing' or 'leading')
|
2014-08-08 15:41:59 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function slash_segment($n, $where = 'trailing')
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return $this->_slash_segment($n, $where, 'segment');
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Slash routed segment
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Fetches an URI routed segment with a slash.
|
|
|
|
*
|
|
|
|
* @param int $n Index
|
|
|
|
* @param string $where Where to add the slash ('trailing' or 'leading')
|
2014-08-08 15:41:59 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function slash_rsegment($n, $where = 'trailing')
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return $this->_slash_segment($n, $where, 'rsegment');
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Internal Slash segment
|
|
|
|
*
|
|
|
|
* Fetches an URI Segment and adds a slash to it.
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @used-by CI_URI::slash_segment()
|
|
|
|
* @used-by CI_URI::slash_rsegment()
|
|
|
|
*
|
|
|
|
* @param int $n Index
|
|
|
|
* @param string $where Where to add the slash ('trailing' or 'leading')
|
|
|
|
* @param string $which Array name ('segment' or 'rsegment')
|
2014-08-08 15:41:59 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
protected function _slash_segment($n, $where = 'trailing', $which = 'segment')
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$leading = $trailing = '/';
|
2014-08-08 15:41:59 -04:00
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
if ($where === 'trailing')
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
$leading = '';
|
|
|
|
}
|
2019-12-18 16:07:09 -05:00
|
|
|
elseif ($where === 'leading')
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
$trailing = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $leading.$this->$which($n).$trailing;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Segment Array
|
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @return array CI_URI::$segments
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function segment_array()
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return $this->segments;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Routed Segment Array
|
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @return array CI_URI::$rsegments
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function rsegment_array()
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return $this->rsegments;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Total number of segments
|
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @return int
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function total_segments()
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return count($this->segments);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Total number of routed segments
|
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @return int
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function total_rsegments()
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return count($this->rsegments);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Fetch URI string
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @return string CI_URI::$uri_string
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function uri_string()
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return $this->uri_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Fetch Re-routed URI string
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function ruri_string()
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
return ltrim(load_class('Router', 'core')->directory, '/').implode('/', $this->rsegments);
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|