Ejemplo n.º 1
0
 public function testStringBase()
 {
     $amount = 0;
     $this->assertEquals("string", AgaviToolkit::stringBase("stringbase", "stringother"));
     $this->assertEquals("string", AgaviToolkit::stringBase("stringbase", "stringother", $amount));
     $this->assertEquals(6, $amount);
     $this->assertEquals("hu", AgaviToolkit::stringBase("hurray", "hungry"));
     $this->assertEquals(NULL, AgaviToolkit::stringBase("astringbase", "stringother"));
 }
 /**
  * Initialize the routing instance.
  *
  * @param      AgaviContext The Context.
  * @param      array        An array of initialization parameters.
  *
  * @author     David Zülke <*****@*****.**>
  * @author     Veikko Mäkinen <*****@*****.**>
  * @author     Dominik del Bondio <*****@*****.**>
  * @since      0.11.0
  */
 public function initialize(AgaviContext $context, array $parameters = array())
 {
     parent::initialize($context, $parameters);
     $rq = $this->context->getRequest();
     $rd = $rq->getRequestData();
     // 'scheme://authority' is necessary so parse_url doesn't stumble over '://' in the request URI
     $ru = array_merge(array('path' => '', 'query' => ''), parse_url('scheme://authority' . $rq->getRequestUri()));
     if (isset($_SERVER['QUERY_STRING'])) {
         $qs = $_SERVER['QUERY_STRING'];
     } else {
         $qs = '';
     }
     // when rewriting, apache strips one (not all) trailing ampersand from the end of QUERY_STRING... normalize:
     $rewritten = preg_replace('/&+$/D', '', $qs) !== preg_replace('/&+$/D', '', $ru['query']);
     if ($this->isEnabled() && $rewritten) {
         // strip the one trailing ampersand, see above
         $queryWasEmptied = false;
         if ($ru['query'] !== '' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) {
             $ru['query'] = preg_replace('/&$/D', '', $ru['query']);
             if ($ru['query'] == '') {
                 $queryWasEmptied = true;
             }
         }
         $stripFromQuery = '&' . $ru['query'];
         if ($ru['query'] == '' && !$queryWasEmptied && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) {
             // if the query is empty, simply give apache2 nothing instead of an "&", since that could kill a real trailing ampersand in the path, as Apache strips those from the query string (which has the rewritten path), but not the request uri
             $stripFromQuery = '';
         }
         $this->input = preg_replace('/' . preg_quote($stripFromQuery, '/') . '$/D', '', $qs);
         if (isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache/2') !== false) {
             $sru = $_SERVER['REQUEST_URI'];
             if (($fqmp = strpos($sru, '?')) !== false && $fqmp == strlen($sru) - 1) {
                 // strip a trailing question mark, but only if it really is the query string separator (i.e. the only question mark in the URI)
                 $sru = substr($sru, 0, -1);
             } elseif ($ru['query'] !== '' || $queryWasEmptied) {
                 // if there is a trailing ampersand (in query string or path, whatever ends the URL), strip it (but just one)
                 $sru = preg_replace('/&$/D', '', $sru);
             }
             // multiple consecutive slashes got lost in our input thanks to an apache bug
             // let's fix that
             $cqs = preg_replace('#/{2,}#', '/', rawurldecode($ru['query']));
             $cru = preg_replace('#/{2,}#', '/', rawurldecode($sru));
             $tmp = preg_replace('/' . preg_quote($this->input . ($cqs != '' || $queryWasEmptied ? '?' . $cqs : ''), '/') . '$/D', '', $cru);
             $input = preg_replace('/^' . preg_quote($tmp, '/') . '/', '', $sru);
             if ($ru['query'] !== '' || $queryWasEmptied) {
                 $input = preg_replace('/' . preg_quote('?' . $ru['query'], '/') . '$/D', '', $input);
             }
             $this->input = $input;
         }
         if (!(isset($_SERVER['SERVER_SOFTWARE']) && (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache/1') !== false || strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false && isset($_SERVER['UNENCODED_URL'])))) {
             // don't do that for Apache 1 or IIS 7 with URL Rewrite Module, it's already rawurldecode()d there
             $this->input = rawurldecode($this->input);
         }
         $xrup = rawurldecode($ru['path']);
         $this->basePath = $this->prefix = preg_replace('/' . preg_quote($this->input, '/') . '$/D', '', rawurldecode($ru['path']));
         // that was easy. now clean up $_GET and the Request
         $parsedRuQuery = $parsedInput = '';
         parse_str($ru['query'], $parsedRuQuery);
         parse_str($this->input, $parsedInput);
         if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
             $parsedRuQuery = AgaviWebRequest::clearMagicQuotes($parsedRuQuery);
             $parsedInput = AgaviWebRequest::clearMagicQuotes($parsedInput, false);
         }
         foreach (array_diff(array_keys($parsedInput), array_keys($parsedRuQuery)) as $unset) {
             // our element is in $_GET
             unset($_GET[$unset]);
             unset($GLOBALS['HTTP_GET_VARS'][$unset]);
             // if it is not also in $_POST, then we need to remove it from the request params
             if (!isset($_POST[$unset])) {
                 $rd->removeParameter($unset);
                 // and from $_REQUEST, too!
                 unset($_REQUEST[$unset]);
             }
         }
     } else {
         $sn = $_SERVER['SCRIPT_NAME'];
         $path = rawurldecode($ru['path']);
         $appendFrom = 0;
         $this->prefix = AgaviToolkit::stringBase($sn, $path, $appendFrom);
         $this->prefix .= substr($sn, $appendFrom);
         $this->input = substr($path, $appendFrom);
         if (!isset($_SERVER['SERVER_SOFTWARE']) || strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') === false || isset($_SERVER['HTTP_X_REWRITE_URL']) || !isset($_SERVER['GATEWAY_INTERFACE']) || strpos($_SERVER['GATEWAY_INTERFACE'], 'CGI') === false) {
             // don't do that for IIS-CGI, it's already rawurldecode()d there
             $this->input = rawurldecode($this->input);
         }
         $this->basePath = str_replace('\\', '/', dirname($this->prefix));
     }
     $this->inputParameters = $_GET;
     if (!$this->input) {
         $this->input = "/";
     }
     if (substr($this->basePath, -1, 1) != '/') {
         $this->basePath .= '/';
     }
     $this->baseHref = $rq->getUrlScheme() . '://' . $rq->getUrlAuthority() . $this->basePath;
 }