Example #1
0
 /**
  * @param string	$basedir
  * @param bool		$load_GET
  *
  * @access public
  *
  * @uses			$basedir
  * @uses			$prepareString
  * @uses			$prepareCount
  * @uses			$paramsCount
  * @uses			$routed
  * @uses			$allParams
  *
  * @return array	Returns an array with the uri-params on success or FALSE on failure
  */
 public static function execute($basedir = null, $load_GET = true)
 {
     /**
      * Check params.
      */
     if (is_null($basedir) && self::$basedir == true) {
         $basedir = self::$basedir;
     }
     if (self::$prepareString === false) {
         return false;
     }
     /**
      * Current path.
      */
     if (is_null($basedir)) {
         if (!isset($_SERVER['HTTPS'])) {
             $uri = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
         } else {
             $uri = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
         }
     } else {
         if (substr($basedir, 0, 7) == "http://") {
             $uri = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
         } elseif (substr($basedir, 0, 8) == "https://") {
             $uri = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
         } else {
             return false;
         }
     }
     /**
      * Generate basedir.
      */
     if (is_null($basedir)) {
         if (!isset($_SERVER['HTTPS'])) {
             $scheme = 'http://';
         } else {
             $scheme = 'https://';
         }
         $basedir = $scheme . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
         $basedir = str_replace('/' . basename($scheme . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']), '', $basedir);
     }
     /**
      * Delete last slash from the root directory.
      */
     if (substr($basedir, -1, 1) == "/") {
         $basedir = substr($basedir, 0, strlen($basedir) - 1);
     }
     /**
      * Is there a slash at the end of the path? (only if there are no parameters!)
      */
     $slash = "";
     if ($uri == $basedir) {
         $slash = "/";
     } elseif ($uri == $basedir . "/") {
         $slash = "";
     } elseif (substr($uri, -1, 1) == "/") {
         $uri = substr($uri, 0, strlen($uri) - 1);
     }
     /**
      * Filter out the index file and the base directory.
      */
     $uri = str_replace($basedir . "/", "", $uri . $slash);
     $paramsCount = 0;
     /**
      * Parse prepare string and load the individual parameters in the $_GET array.
      */
     $prepareString = self::$prepareString;
     $parts = explode('/', $prepareString);
     $partsCount = count($parts);
     $prepareParams = array();
     $uriParams = array();
     $uriParts = explode('/', $uri);
     for ($i = 0; $partsCount > $i; $i++) {
         if (strpos($parts[$i], '-') == true) {
             $prepareParams = explode('-', $parts[$i]);
             $uriParams = explode('-', $uriParts[$i], count($prepareParams));
             for ($j = 0; count($prepareParams) > $j; $j++) {
                 if (isset($uriParams[$j]) && !is_null($uriParams[$j]) && !empty($uriParams[$j])) {
                     $__GET[substr($prepareParams[$j], 1, strlen($prepareParams[$j]) - 2)] = $uriParams[$j];
                     $latestKey = substr($prepareParams[$j], 1, strlen($prepareParams[$j]) - 2);
                 }
             }
             $prepareParams = $parts[$i];
             $uriParams = @$uriParts[$i];
             if (isset($uriParams) && !is_null($uriParams) && !empty($uriParams)) {
                 $paramsCount++;
             }
         } else {
             $prepareParams = $parts[$i];
             $uriParams = @$uriParts[$i];
             if (isset($uriParams) && !is_null($uriParams) && !empty($uriParams)) {
                 $__GET[substr($prepareParams, 1, strlen($prepareParams) - 2)] = $uriParams;
                 $latestKey = substr($prepareParams, 1, strlen($prepareParams) - 2);
                 $paramsCount++;
             }
         }
     }
     if (count($uriParts) - 1 >= $i) {
         $remains = count($uriParts) - 1 - $i;
         for ($j = 0; $remains >= $j; $j++) {
             $__GET[$j + $i + 1] = $uriParts[$j + $i];
         }
     }
     if (isset($__GET) && isset($latestKey) && strpos($__GET[$latestKey], '?') == true) {
         # a question mark must be present in the parameter
         if (substr($__GET[$latestKey], -1, 1) == '?') {
             # is the last character a question mark?
             $__GET[$latestKey] = substr($__GET[$latestKey], 0, strlen($__GET[$latestKey]) - 1);
             $glue = "?";
             $glueGetParam = false;
         } else {
             $glue = "";
             $glueGetParam = false;
         }
         if (strpos($__GET[$latestKey], '?') == true) {
             $glueGetParam = true;
             $elements = explode('?', $__GET[$latestKey], 2);
             $__GET[$latestKey] = $elements[0];
             $elements[1] .= $glue;
             $getParams = explode('&', $elements[1]);
             $trueGetParams = array();
             foreach ($getParams as $getParam) {
                 if (strpos($getParam, '=') == true) {
                     $paramArray = explode('=', $getParam, 2);
                     $trueGetParams[$paramArray[0]] = $paramArray[1];
                 }
             }
         }
         $__GET[$latestKey] .= $glueGetParam == false ? $glue : '';
     }
     if (isset($trueGetParams) && count($trueGetParams) > 0) {
         foreach ($trueGetParams as $key => $value) {
             $__GET[$key] = $value;
         }
     }
     self::$paramsCount = $paramsCount;
     self::$routed = true;
     self::$allParams = isset($__GET) ? $__GET : false;
     self::$getCount = isset($__GET) ? count($__GET) : 0;
     if ($load_GET === true) {
         if (isset($__GET)) {
             $GLOBALS['_GET'] = $__GET;
             return;
         }
     } else {
         /**
          * Return array.
          */
         if (isset($__GET)) {
             return $__GET;
         }
     }
 }