Esempio n. 1
0
 public function testIsPortNecessary()
 {
     $this->assertTrue(AgaviToolkit::isPortNecessary('some scheme', 8800));
     $this->assertFalse(AgaviToolkit::isPortNecessary('ftp', 21));
     $this->assertFalse(AgaviToolkit::isPortNecessary('ssh', 22));
     $this->assertFalse(AgaviToolkit::isPortNecessary('https', 443));
     $this->assertFalse(AgaviToolkit::isPortNecessary('nttp', 119));
 }
 /**
  * Retrieve the request URL authority, typically host and port.
  * Example: "foo.example.com:8080".
  *
  * @param      bool Whether or not ports 80 (for HTTP) and 433 (for HTTPS)
  *                  should be included in the return string.
  *
  * @return     string The request URL authority.
  *
  * @author     David Zülke <*****@*****.**>
  * @since      0.11.0
  */
 public function getUrlAuthority($forcePort = false)
 {
     $port = $this->getUrlPort();
     $scheme = $this->getUrlScheme();
     return $this->getUrlHost() . ($forcePort || AgaviToolkit::isPortNecessary($scheme, $port) ? ':' . $port : '');
 }
 /**
  * Generate a formatted Agavi URL.
  *
  * @param      string A route name.
  * @param      array  An associative array of parameters.
  * @param      mixed  An array of options, or the name of an options preset.
  *
  * @return     string The generated URL.
  *
  * @author     David Zülke <*****@*****.**>
  * @since      0.11.0
  */
 public function gen($route, array $params = array(), $options = array())
 {
     $req = $this->context->getRequest();
     if (substr($route, -1) == '*') {
         $options['refill_all_parameters'] = true;
         $route = substr($route, 0, -1);
     }
     $options = $this->resolveGenOptions($options);
     $aso = $this->argSeparatorOutput;
     if ($options['separator'] != $aso) {
         $aso = $options['separator'];
     }
     if ($options['use_trans_sid'] === true && defined('SID') && SID !== '') {
         $params = array_merge($params, array(session_name() => session_id()));
     }
     if ($route === null && empty($params)) {
         $retval = $req->getRequestUri();
         $retval = str_replace(array('[', ']', '\''), array('%5B', '%5D', '%27'), $retval);
         // much quicker than str_replace($this->argSeparatorInput, array_fill(0, count($this->argSeparatorInput), $aso), $retval)
         foreach ($this->argSeparatorInput as $char) {
             $retval = str_replace($char, $aso, $retval);
         }
     } else {
         if ($this->isEnabled()) {
             // the route exists and routing is enabled, the parent method handles it
             $append = '';
             list($path, $usedParams, $options, $extraParams, $isNullRoute) = parent::gen($route, $params, $options);
             if ($isNullRoute) {
                 // add the incoming parameters from the request uri for gen(null) and friends
                 $extraParams = array_merge($this->inputParameters, $extraParams);
             }
             if (count($extraParams) > 0) {
                 $append = http_build_query($extraParams, '', $aso);
                 if ($append !== '') {
                     $append = '?' . $append;
                 }
             }
         } else {
             // the route exists, but we must create a normal index.php?foo=bar URL.
             $isNullRoute = false;
             $routes = $this->getAffectedRoutes($route, $isNullRoute);
             if ($isNullRoute) {
                 $params = array_merge($this->inputParameters, $params);
             }
             if (count($routes) == 0) {
                 $path = $route;
             }
             // we collect the default parameters from the route and make sure
             // new parameters don't overwrite already defined parameters
             $defaults = array();
             $ma = $req->getParameter('module_accessor');
             $aa = $req->getParameter('action_accessor');
             foreach ($routes as $route) {
                 if (isset($this->routes[$route])) {
                     $r = $this->routes[$route];
                     $myDefaults = array();
                     foreach ($r['opt']['defaults'] as $key => $default) {
                         $myDefaults[$key] = $default->getValue();
                     }
                     if ($r['opt']['module']) {
                         $myDefaults[$ma] = $r['opt']['module'];
                     }
                     if ($r['opt']['action']) {
                         $myDefaults[$aa] = $r['opt']['action'];
                     }
                     $defaults = array_merge($myDefaults, $defaults);
                 }
             }
             $params = array_merge($defaults, $params);
         }
         if (!isset($path)) {
             // the route does not exist. we generate a normal index.php?foo=bar URL.
             $path = $_SERVER['SCRIPT_NAME'];
         }
         if (!isset($path)) {
             // routing was off; the name of the route is the input
         }
         if (!isset($append)) {
             $append = '?' . http_build_query($params, '', $aso);
         }
         $retval = $path . $append;
     }
     if (!$options['relative'] || $options['relative'] && ($options['scheme'] !== null || $options['authority'] !== null || $options['host'] !== null || $options['port'] !== null)) {
         $scheme = false;
         if ($options['scheme'] !== false) {
             $scheme = $options['scheme'] === null ? $req->getUrlScheme() : $options['scheme'];
         }
         $authority = '';
         if ($options['authority'] === null) {
             if ($options['host'] !== null && $options['host'] !== false) {
                 $authority = $options['host'];
             } elseif ($options['host'] === false) {
                 $authority = '';
             } else {
                 $authority = $req->getUrlHost();
             }
             $port = null;
             if ($options['port'] !== null && $options['port'] !== false) {
                 if (AgaviToolkit::isPortNecessary($options['scheme'] !== null && $options['scheme'] !== false ? $options['scheme'] : $req->getUrlScheme(), $options['port'])) {
                     $port = $options['port'];
                 } else {
                     $port = null;
                 }
             } elseif ($options['port'] === false) {
                 $port = null;
             } elseif ($options['scheme'] === null) {
                 if (!AgaviToolkit::isPortNecessary($req->getUrlScheme(), $port = $req->getUrlPort())) {
                     $port = null;
                 }
             }
             if ($port !== null) {
                 $authority .= ':' . $port;
             }
         } elseif ($options['authority'] !== false) {
             $authority = $options['authority'];
         }
         if ($scheme === false) {
             // nothing at all, e.g. when displaying a URL without the "http://" prefix
             $scheme = '';
         } elseif (trim($scheme) === '') {
             // a protocol-relative URL (see #1224)
             $scheme = '//';
         } else {
             // given scheme plus "://"
             $scheme = $scheme . '://';
         }
         $retval = $scheme . $authority . $retval;
     }
     if ($options['fragment'] !== null) {
         $retval .= '#' . $options['fragment'];
     }
     return $retval;
 }