Exemplo n.º 1
0
 /**
  * Parse a SEF URL path into URL parameters
  *
  * @param   string $path The path to parse, e.g. /foo/bar/1/2/3
  *
  * @return  array|null  The URL parameters or null if we can't parse the route with this rule
  */
 public function parse($path)
 {
     $extraParams = array();
     if (strpos($path, '?') !== false) {
         $uri = new Uri($path);
         $path = $uri->getPath();
         $extraParams = $uri->getQuery(true);
     }
     if ($this->useCallableForParse) {
         $ret = call_user_func($this->parseCallable, $path);
     } else {
         // Explode the path parts
         $segments = explode('/', $path);
         // Try to extract the URL parameters by parsing the segments
         $params = $this->parseRoute($segments);
         // If we got null back we can't parse this route
         if (is_null($params)) {
             return null;
         }
         // Mix in the push variables
         $params = array_merge($this->pushVars, $params);
         // Return the URL parameters
         $ret = $params;
     }
     return array_merge($ret, $extraParams);
 }
Exemplo n.º 2
0
 /**
  * Sets the additional URL parameters from the input. If no input is specified we will use the application's
  * input. The URL parameters of the base URL will be automatically removed.
  *
  * @param   Input  $input  The input object to use
  *
  * @return  void
  */
 public function setAdditionalUrlParamsFromInput(Input $input = null)
 {
     // Make sure we have an input
     if (!is_object($input)) {
         $input = $this->application->getContainer()->input;
     }
     // Get the input's data array
     $data = $input->getData();
     // Get the rebase URL parameters to eliminate
     $rebase = Uri::rebase('index.php', $this->application->getContainer());
     $rebase = new Uri($rebase);
     $eliminateParams = $rebase->getQuery(true);
     $eliminateParams = array_keys($eliminateParams);
     // Set the additional URL parameters
     foreach ($data as $key => $value) {
         // We can't process object data automatically
         if (is_object($value)) {
             continue;
         }
         // We can't process array data automatically
         if (is_array($value)) {
             continue;
         }
         // Ignore the URL parameters from the URL rebasing
         if (in_array($key, $eliminateParams)) {
             continue;
         }
         $this->setAdditionalUrlParam($key, $value);
     }
 }
Exemplo n.º 3
0
 /**
  * A utility function to rebase a (partial) URL based on the live_site and base_url of the application configuration
  * in the provided container. You can override the base_url of the application through the $baseUrl parameter.
  *
  * It's simpler to explain with a short example. Let's consider an application with
  * live_site = 'http://www.example.com' and base_url='/administrator/index.php?option=com_foobar&baz=1'.
  * Using Uri::rebase('?view=report&item=export', $container) will result to the following absolute URL:
  * http://www.example.com/administrator/index.php?baz=1&item=export&option=com_foobar&view=report
  * Using Uri::rebase('?option=com_whatever', $container) will result to the following absolute URL:
  * http://www.example.com/administrator/index.php?baz=1&option=com_whatever
  * Therefore by manipulating the base_url in the application's configuration you can have an application which runs
  * inside another application (no matter if the other application is based on Awf or not). The "only" thing you will
  * have to do is specialise your Application object to act as a bridge to the parent application.
  *
  * @param             $url
  * @param Application $container
  * @param null        $baseUrl
  *
  * @return string
  */
 public static function rebase($url, Container $container, $baseUrl = null)
 {
     if (empty($baseUrl)) {
         $baseUrl = $container->appConfig->get('base_url', 'index.php');
         if (empty($baseUrl)) {
             $baseUrl = 'index.php';
         }
         $baseUrl = rtrim($baseUrl, '/');
     }
     $base = self::base(false, $container);
     $base = rtrim($base, '/') . '/' . ltrim($baseUrl, '/');
     $rebaseURI = new Uri($base);
     $oldURI = new Uri($url);
     $vars = $oldURI->getQuery(true);
     if (!empty($vars)) {
         foreach ($vars as $k => $v) {
             $rebaseURI->setVar($k, $v);
         }
     }
     if ($oldURI->getFragment()) {
         $rebaseURI->setFragment($oldURI->getFragment());
     }
     return (string) $rebaseURI;
 }
Exemplo n.º 4
0
 /**
  * Parse a routed URL based on the routing rules, setting the input variables of the attached application.
  *
  * @param   string  $url    The URL to parse. If omitted the current URL will be used.
  * @param   boolean $rebase Is this a rebased URL? If false we assume we're given a relative URL.
  */
 public function parse($url = null, $rebase = true)
 {
     // If we are not given a URL, use the current URL of the site
     if (empty($url)) {
         $url = Uri::current();
     }
     // Initialise
     $removePath = null;
     $removeVars = null;
     if ($rebase) {
         // Get the base URL
         $baseUrl = $this->container->appConfig->get('base_url', '');
         if (empty($baseUrl)) {
             $baseUrl = '';
         }
         $baseUrl = rtrim($baseUrl, '/');
         $base = Uri::base(false, $this->container);
         $base = rtrim($base, '/') . '/' . $baseUrl;
         $rebaseURI = new Uri($base);
         // Get the path and vars to remove from the parsed route
         $removePath = $rebaseURI->getPath();
         $removePath = trim($removePath, '/');
         $removeVars = $rebaseURI->getQuery(true);
     }
     $uri = new Uri($url);
     $path = $uri->getPath();
     $path = trim($path, '/');
     // Remove the $removePath
     if (!empty($removePath)) {
         if (strpos($path, $removePath) === 0) {
             $path = substr($path, strlen($removePath));
         }
     }
     // Use the routing rules to parse the URL
     $routeVars = null;
     if (!empty($this->rules)) {
         /** @var Rule $rule */
         foreach ($this->rules as $rule) {
             $routeVars = $rule->parse($path);
             if (is_array($routeVars)) {
                 break;
             }
         }
     }
     if (is_null($routeVars)) {
         $routeVars = array();
     }
     // Mix route and URI vars
     $uriVars = $uri->getQuery(true);
     $routeVars = array_merge($routeVars, $uriVars);
     // Eliminate $removeVars
     if (is_array($removeVars) && !empty($removeVars)) {
         foreach ($removeVars as $k => $v) {
             if (isset($routeVars[$k]) && $routeVars[$k] == $v) {
                 unset($routeVars[$k]);
             }
         }
     }
     // Set the query vars to the application
     if (is_array($routeVars) && !empty($routeVars)) {
         $this->container->input->setData($routeVars);
     }
 }