CI_URI Class Reference

List of all members.


Public Member Functions

 CI_URI ()
 Constructor.
 _fetch_uri_string ()
 Get the URI String.
 _parse_request_uri ()
 Parse the REQUEST_URI.
 _filter_uri ($str)
 Filter segments for malicious characters.
 _remove_url_suffix ()
 Remove the suffix from the URL if needed.
 _explode_segments ()
 Explode the URI Segments.
 _reindex_segments ()
 Re-index Segments.
 segment ($n, $no_result=FALSE)
 Fetch a URI Segment.
 rsegment ($n, $no_result=FALSE)
 Fetch a URI "routed" Segment.
 uri_to_assoc ($n=3, $default=array())
 Generate a key value pair from the URI string.
 ruri_to_assoc ($n=3, $default=array())
 Identical to above only it uses the re-routed segment array.
 _uri_to_assoc ($n=3, $default=array(), $which= 'segment')
 Generate a key value pair from the URI string or Re-routed URI string.
 assoc_to_uri ($array)
 Generate a URI string from an associative array.
 slash_segment ($n, $where= 'trailing')
 Fetch a URI Segment and add a trailing slash.
 slash_rsegment ($n, $where= 'trailing')
 Fetch a URI Segment and add a trailing slash.
 _slash_segment ($n, $where= 'trailing', $which= 'segment')
 Fetch a URI Segment and add a trailing slash - helper function.
 segment_array ()
 Segment Array.
 rsegment_array ()
 Routed Segment Array.
 total_segments ()
 Total number of segments.
 total_rsegments ()
 Total number of routed segments.
 uri_string ()
 Fetch the entire URI string.
 ruri_string ()
 Fetch the entire Re-routed URI string.

Public Attributes

 $keyval = array()
 $uri_string
 $segments = array()
 $rsegments = array()

Detailed Description

Definition at line 29 of file URI.php.


Member Function Documentation

CI_URI::_explode_segments (  ) 

Explode the URI Segments.

The individual segments will be stored in the $this->segments array.

private

Returns:
void

Definition at line 223 of file URI.php.

References _filter_uri(), and uri_string().

00224         {
00225                 foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
00226                 {
00227                         // Filter segments for security
00228                         $val = trim($this->_filter_uri($val));
00229                         
00230                         if ($val != '')
00231                                 $this->segments[] = $val;
00232                 }
00233         }

Here is the call graph for this function:

CI_URI::_fetch_uri_string (  ) 

Get the URI String.

private

Returns:
string

Definition at line 60 of file URI.php.

References _parse_request_uri(), and uri_string().

00061         {       
00062                 if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
00063                 {
00064                         // If the URL has a question mark then it's simplest to just
00065                         // build the URI string from the zero index of the $_GET array.
00066                         // This avoids having to deal with $_SERVER variables, which
00067                         // can be unreliable in some environments
00068                         if (is_array($_GET) AND count($_GET) == 1 AND trim(key($_GET), '/') != '')
00069                         {
00070                                 $this->uri_string = key($_GET);                 
00071                                 return;
00072                         }
00073                 
00074                         // Is there a PATH_INFO variable?
00075                         // Note: some servers seem to have trouble with getenv() so we'll test it two ways              
00076                         $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');                  
00077                         if (trim($path, '/') != '' AND $path != "/".SELF)
00078                         {
00079                                 $this->uri_string = $path;
00080                                 return;
00081                         }
00082                                         
00083                         // No PATH_INFO?... What about QUERY_STRING?
00084                         $path =  (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');        
00085                         if (trim($path, '/') != '')
00086                         {
00087                                 $this->uri_string = $path;
00088                                 return;
00089                         }
00090                         
00091                         // No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists?
00092                         $path = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO');   
00093                         if (trim($path, '/') != '' AND $path != "/".SELF)
00094                         {
00095                                 // remove path and script information so we have good URI data
00096                                 $this->uri_string = str_replace($_SERVER['SCRIPT_NAME'], '', $path);
00097                                 return;
00098                         }
00099 
00100                         // We've exhausted all our options...
00101                         $this->uri_string = '';
00102                 }
00103                 else
00104                 {
00105                         $uri = strtoupper($this->config->item('uri_protocol'));
00106                         
00107                         if ($uri == 'REQUEST_URI')
00108                         {
00109                                 $this->uri_string = $this->_parse_request_uri();
00110                                 return;
00111                         }
00112                         
00113                         $this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
00114                 }
00115                 
00116                 // If the URI contains only a slash we'll kill it
00117                 if ($this->uri_string == '/')
00118                 {
00119                         $this->uri_string = '';
00120                 }               
00121         }

Here is the call graph for this function:

CI_URI::_filter_uri ( str  ) 

Filter segments for malicious characters.

private

Parameters:
string 
Returns:
string

Definition at line 185 of file URI.php.

Referenced by _explode_segments().

00186         {
00187                 if ($str != '' AND $this->config->item('permitted_uri_chars') != '')
00188                 {
00189                         if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str))
00190                         {
00191                                 exit('The URI you submitted has disallowed characters.');
00192                         }
00193                 }
00194                         
00195                 return $str;
00196         }

Here is the caller graph for this function:

CI_URI::_parse_request_uri (  ) 

Parse the REQUEST_URI.

Due to the way REQUEST_URI works it usually contains path info that makes it unusable as URI data. We'll trim off the unnecessary data, hopefully arriving at a valid URI that we can use.

private

Returns:
string

Definition at line 135 of file URI.php.

Referenced by _fetch_uri_string().

00136         {
00137                 if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '')
00138                 {
00139                         return '';
00140                 }
00141                 
00142                 $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI']));
00143 
00144                 if ($request_uri == '' OR $request_uri == SELF)
00145                 {
00146                         return '';
00147                 }
00148                 
00149                 $fc_path = FCPATH;              
00150                 if (strpos($request_uri, '?') !== FALSE)
00151                 {
00152                         $fc_path .= '?';
00153                 }
00154                 
00155                 $parsed_uri = explode("/", $request_uri);
00156                                 
00157                 $i = 0;
00158                 foreach(explode("/", $fc_path) as $segment)
00159                 {
00160                         if (isset($parsed_uri[$i]) AND $segment == $parsed_uri[$i])
00161                         {
00162                                 $i++;
00163                         }
00164                 }
00165                 
00166                 $parsed_uri = implode("/", array_slice($parsed_uri, $i));
00167                 
00168                 if ($parsed_uri != '')
00169                 {
00170                         $parsed_uri = '/'.$parsed_uri;
00171                 }
00172 
00173                 return $parsed_uri;
00174         }

Here is the caller graph for this function:

CI_URI::_reindex_segments (  ) 

Re-index Segments.

This function re-indexes the $this->segment array so that it starts at 1 rather then 0. Doing so makes it simpler to use functions like $this->uri->segment(n) since there is a 1:1 relationship between the segment array and the actual segments.

private

Returns:
void

Definition at line 247 of file URI.php.

00248         {
00249                 array_unshift($this->segments, NULL);
00250                 array_unshift($this->rsegments, NULL);
00251                 unset($this->segments[0]);
00252                 unset($this->rsegments[0]);
00253         }       

CI_URI::_remove_url_suffix (  ) 

Remove the suffix from the URL if needed.

private

Returns:
void

Definition at line 206 of file URI.php.

References uri_string().

00207         {
00208                 if  ($this->config->item('url_suffix') != "")
00209                 {
00210                         $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);
00211                 }
00212         }

Here is the call graph for this function:

CI_URI::_slash_segment ( n,
where = 'trailing',
which = 'segment' 
)

Fetch a URI Segment and add a trailing slash - helper function.

private

Parameters:
integer 
string 
string 
Returns:
string

Definition at line 475 of file URI.php.

Referenced by slash_rsegment(), and slash_segment().

00476         {       
00477                 if ($where == 'trailing')
00478                 {
00479                         $trailing       = '/';
00480                         $leading        = '';
00481                 }
00482                 elseif ($where == 'leading')
00483                 {
00484                         $leading        = '/';
00485                         $trailing       = '';
00486                 }
00487                 else
00488                 {
00489                         $leading        = '/';
00490                         $trailing       = '/';
00491                 }
00492                 return $leading.$this->$which($n).$trailing;
00493         }

Here is the caller graph for this function:

CI_URI::_uri_to_assoc ( n = 3,
default = array(),
which = 'segment' 
)

Generate a key value pair from the URI string or Re-routed URI string.

private

Parameters:
integer the starting segment number
array an array of default values
string which array we should use
Returns:
array

Definition at line 338 of file URI.php.

References $segments.

Referenced by ruri_to_assoc(), and uri_to_assoc().

00339         {
00340                 if ($which == 'segment')
00341                 {
00342                         $total_segments = 'total_segments';
00343                         $segment_array = 'segment_array';
00344                 }
00345                 else
00346                 {
00347                         $total_segments = 'total_rsegments';
00348                         $segment_array = 'rsegment_array';
00349                 }
00350                 
00351                 if ( ! is_numeric($n))
00352                 {
00353                         return $default;
00354                 }
00355         
00356                 if (isset($this->keyval[$n]))
00357                 {
00358                         return $this->keyval[$n];
00359                 }
00360         
00361                 if ($this->$total_segments() < $n)
00362                 {
00363                         if (count($default) == 0)
00364                         {
00365                                 return array();
00366                         }
00367                         
00368                         $retval = array();
00369                         foreach ($default as $val)
00370                         {
00371                                 $retval[$val] = FALSE;
00372                         }               
00373                         return $retval;
00374                 }
00375 
00376                 $segments = array_slice($this->$segment_array(), ($n - 1));
00377 
00378                 $i = 0;
00379                 $lastval = '';
00380                 $retval  = array();
00381                 foreach ($segments as $seg)
00382                 {
00383                         if ($i % 2)
00384                         {
00385                                 $retval[$lastval] = $seg;
00386                         }
00387                         else
00388                         {
00389                                 $retval[$seg] = FALSE;
00390                                 $lastval = $seg;
00391                         }
00392                 
00393                         $i++;
00394                 }
00395 
00396                 if (count($default) > 0)
00397                 {
00398                         foreach ($default as $val)
00399                         {
00400                                 if ( ! array_key_exists($val, $retval))
00401                                 {
00402                                         $retval[$val] = FALSE;
00403                                 }
00404                         }
00405                 }
00406 
00407                 // Cache the array for reuse
00408                 $this->keyval[$n] = $retval;
00409                 return $retval;
00410         }

Here is the caller graph for this function:

CI_URI::assoc_to_uri ( array  ) 

Generate a URI string from an associative array.

public

Parameters:
array an associative array of key/values
Returns:
array

Definition at line 422 of file URI.php.

00423         {       
00424                 $temp = array();
00425                 foreach ((array)$array as $key => $val)
00426                 {
00427                         $temp[] = $key;
00428                         $temp[] = $val;
00429                 }
00430                 
00431                 return implode('/', $temp);
00432         }

CI_URI::CI_URI (  ) 

Constructor.

Simply globalizes the $RTR object. The front loads the Router class early on so it's not available normally as other classes are.

public

Definition at line 45 of file URI.php.

References load_class(), and log_message().

00046         {
00047                 $this->config =& load_class('Config');
00048                 log_message('debug', "URI Class Initialized");
00049         }

Here is the call graph for this function:

CI_URI::rsegment ( n,
no_result = FALSE 
)

Fetch a URI "routed" Segment.

This function returns the re-routed URI segment (assuming routing rules are used) based on the number provided. If there is no routing this function returns the same result as $this->segment()

public

Parameters:
integer 
bool 
Returns:
string

Definition at line 286 of file URI.php.

00287         {
00288                 return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
00289         }

CI_URI::rsegment_array (  ) 

Routed Segment Array.

public

Returns:
array

Definition at line 516 of file URI.php.

Referenced by ruri_string().

00517         {
00518                 return $this->rsegments;
00519         }

Here is the caller graph for this function:

CI_URI::ruri_string (  ) 

Fetch the entire Re-routed URI string.

public

Returns:
string

Definition at line 569 of file URI.php.

References rsegment_array().

00570         {
00571                 return '/'.implode('/', $this->rsegment_array()).'/';
00572         }

Here is the call graph for this function:

CI_URI::ruri_to_assoc ( n = 3,
default = array() 
)

Identical to above only it uses the re-routed segment array.

Definition at line 322 of file URI.php.

References _uri_to_assoc().

00323         {
00324                 return $this->_uri_to_assoc($n, $default, 'rsegment');
00325         }

Here is the call graph for this function:

CI_URI::segment ( n,
no_result = FALSE 
)

Fetch a URI Segment.

This function returns the URI segment based on the number provided.

public

Parameters:
integer 
bool 
Returns:
string

Definition at line 267 of file URI.php.

00268         {
00269                 return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
00270         }

CI_URI::segment_array (  ) 

Segment Array.

public

Returns:
array

Definition at line 503 of file URI.php.

00504         {
00505                 return $this->segments;
00506         }

CI_URI::slash_rsegment ( n,
where = 'trailing' 
)

Fetch a URI Segment and add a trailing slash.

public

Parameters:
integer 
string 
Returns:
string

Definition at line 459 of file URI.php.

References _slash_segment().

00460         {
00461                 return $this->_slash_segment($n, $where, 'rsegment');
00462         }

Here is the call graph for this function:

CI_URI::slash_segment ( n,
where = 'trailing' 
)

Fetch a URI Segment and add a trailing slash.

public

Parameters:
integer 
string 
Returns:
string

Definition at line 444 of file URI.php.

References _slash_segment().

00445         {
00446                 return $this->_slash_segment($n, $where, 'segment');
00447         }

Here is the call graph for this function:

CI_URI::total_rsegments (  ) 

Total number of routed segments.

public

Returns:
integer

Definition at line 542 of file URI.php.

00543         {
00544                 return count($this->rsegments);
00545         }

CI_URI::total_segments (  ) 

Total number of segments.

public

Returns:
integer

Definition at line 529 of file URI.php.

00530         {
00531                 return count($this->segments);
00532         }

CI_URI::uri_string (  ) 

Fetch the entire URI string.

public

Returns:
string

Definition at line 555 of file URI.php.

Referenced by _explode_segments(), _fetch_uri_string(), and _remove_url_suffix().

00556         {
00557                 return $this->uri_string;
00558         }

Here is the caller graph for this function:

CI_URI::uri_to_assoc ( n = 3,
default = array() 
)

Generate a key value pair from the URI string.

This function generates and associative array of URI data starting at the supplied segment. For example, if this is your URI:

example.com/user/search/name/joe/location/UK/gender/male

You can use this function to generate an array with this prototype:

array ( name => joe location => UK gender => male )

public

Parameters:
integer the starting segment number
array an array of default values
Returns:
array

Definition at line 314 of file URI.php.

References _uri_to_assoc().

00315         {
00316                 return $this->_uri_to_assoc($n, $default, 'segment');
00317         }

Here is the call graph for this function:


Member Data Documentation

CI_URI::$keyval = array()

Definition at line 31 of file URI.php.

CI_URI::$rsegments = array()

Definition at line 34 of file URI.php.

CI_URI::$segments = array()

Definition at line 33 of file URI.php.

Referenced by _uri_to_assoc().

CI_URI::$uri_string

Definition at line 32 of file URI.php.


The documentation for this class was generated from the following file: