Пример #1
0
 function testUrl()
 {
     $url = new Url(self::TEST_URL);
     $this->assertEquals($url->getFull(), self::TEST_URL);
     $this->assertEquals($url->getScheme(), 'https');
     $this->assertEquals($url->getHost(), 'www.google.com');
     $this->assertEquals($url->getPort(), '80');
     $this->assertEquals($url->getPath(), '/some_path');
     // The HTML entities remain intact
     $this->assertEquals($url->getQuery(), 'some=query&something=%20weird');
     // The HTML entities are resolved
     $this->assertEquals($url->getParam('some'), 'query');
     $this->assertEquals($url->getParam('something'), ' weird');
     $this->assertEquals($url->getParams(), ['some' => 'query', 'something' => ' weird']);
     $this->assertEquals($url->getFragment(), 'some_fragment');
     $this->assertEquals($url->getUser(), 'user');
     $this->assertEquals($url->getPass(), 'pass');
 }
Пример #2
0
 /**
  * Generate a base string for a RSA-SHA1 signature
  * based on the given a url, method, and any parameters.
  *
  * @param Url    $url
  * @param string $method
  * @param array  $parameters
  *
  * @return string
  */
 protected function baseString(Url $url, $method = 'POST', array $parameters = [])
 {
     $baseString = rawurlencode($method) . '&';
     $schemeHostPath = $url->getScheme() . '://' . $url->getHost();
     if ($url->getPort() != '') {
         $schemeHostPath .= ':' . $url->getPort();
     }
     if ($url->getPath() != '') {
         $schemeHostPath .= $url->getPath();
     }
     $baseString .= rawurlencode($schemeHostPath) . '&';
     $data = [];
     parse_str($url->getQuery(), $query);
     foreach (array_merge($query, $parameters) as $key => $value) {
         $data[rawurlencode($key)] = rawurlencode($value);
     }
     ksort($data);
     array_walk($data, function (&$value, $key) {
         $value = $key . '=' . $value;
     });
     $baseString .= rawurlencode(implode('&', $data));
     return $baseString;
 }
Пример #3
0
 /**
  * get a link from the current URL to another one
  * @param  UrlInterface|string $url the URL to link to
  * @param  boolean $forceAbsolute   should an absolute path be used, defaults to `true`)
  * @return string  the link
  */
 public function linkTo($url, $forceAbsolute = true)
 {
     if (is_string($url)) {
         $url = new Url($url);
     }
     $str = (string) $url;
     if ($this->getScheme() !== $url->getScheme()) {
         return $str;
     }
     $str = preg_replace('(^[^/]+//)', '', $str);
     if ($this->getHost() !== $url->getHost() || $this->getPort() !== $url->getPort()) {
         return '//' . $str;
     }
     $str = preg_replace('(^[^/]+)', '', $str);
     if ($this->getPath() !== $url->getPath()) {
         if ($forceAbsolute) {
             return $str;
         }
         $cnt = 0;
         $tseg = $this->getSegments();
         $useg = $url->getSegments();
         foreach ($tseg as $k => $v) {
             if (!isset($useg[$k]) || $useg[$k] !== $v) {
                 break;
             }
             $cnt++;
         }
         $str = './' . str_repeat('../', count($useg) - $cnt) . implode('/', array_slice($useg, $cnt));
         if ($url->getQuery()) {
             $str .= '?' . $url->getQuery();
         }
         if ($url->getFragment()) {
             $str .= '#' . $url->getFragment();
         }
         return $str;
     }
     $str = preg_replace('(^[^?]+)', '', $str);
     if ($this->getQuery() !== $url->getQuery() || $url->getFragment() === null) {
         return $str;
     }
     return '#' . $url->getFragment();
 }
Пример #4
0
 public function formatLink(Url $url)
 {
     // save and reset query to save variable order
     $query = $url->getQuery();
     $url->setQuery([]);
     // extract path to separate params and remove actions
     $parts = explode(self::REWRITE_PARAM_TO_ACTION_DELIMITER, $url->getPath());
     foreach ($parts as $part) {
         // only extract "normal" parameters, avoid actions
         if (strpos($part, '-action') === false) {
             $paths = explode('/', strip_tags($part));
             array_shift($paths);
             // create key => value pairs from the current request
             $x = 0;
             while ($x <= count($paths) - 1) {
                 if (isset($paths[$x + 1])) {
                     $url->setQueryParameter($paths[$x], $paths[$x + 1]);
                 }
                 // increment by 2, because the next offset is the key!
                 $x = $x + 2;
             }
         }
     }
     // reset the path to not have duplicate path due to generic param generation
     $url->setPath(null);
     // merge query now to overwrite values already contained in the url
     $url->mergeQuery($query);
     $resultUrl = $this->getFormattedBaseUrl($url);
     $query = $url->getQuery();
     if (count($query) > 0) {
         // get URL mappings and try to resolve mapped actions
         $mappings = $this->getActionUrMappingTokens();
         foreach ($query as $name => $value) {
             // allow empty params that are action definitions to not
             // exclude actions with no params!
             if (!$this->isValueEmpty($value) || $this->isValueEmpty($value) && strpos($name, '-action') !== false || $this->isValueEmpty($value) && in_array($name, $mappings) !== false) {
                 if (strpos($name, '-action') === false && in_array($name, $mappings) === false) {
                     $resultUrl .= '/' . $name . '/' . $value;
                 } else {
                     // action blocks must be separated with group indicator
                     // to be able to parse the parameters
                     $resultUrl .= self::REWRITE_PARAM_TO_ACTION_DELIMITER . $name;
                     // check whether value is empty and append action only
                     if (!$this->isValueEmpty($value)) {
                         $resultUrl .= '/' . $value;
                     }
                 }
             }
         }
     }
     // apply query to detect duplicate action definitions
     $actions = $this->getActionsUrlRepresentation($query, true);
     if (!empty($actions)) {
         $resultUrl .= self::REWRITE_PARAM_TO_ACTION_DELIMITER . $actions;
     }
     // encode blanks if desired
     if ($this->getEncodeBlanks() === true) {
         $resultUrl = strtr($resultUrl, [' ' => '%20']);
     }
     return $this->sanitizeUrl($this->appendAnchor($resultUrl, $url));
 }
Пример #5
0
 public function testGetQuery()
 {
     $this->assertEquals('search=hello&page=2', $this->absoluteUrl->getQuery());
 }
Пример #6
0
 /**
  * Resolve given relative Url using this url
  * @param Url $Url
  * @return Url
  */
 public function resolve($Url)
 {
     $new = clone $this;
     if (!$Url->getHost()) {
         $new->setHost($this->getHost());
         $new->setProtocol($this->getProtocol());
         $new->setPort($this->getPort());
     }
     $new->setQuery($Url->getQuery());
     /* @var $new Url */
     if (String::StartsWith($Url->getPath(), '/')) {
         $new->setPath($Url->getPath());
         return $new;
     } else {
         $file = basename($Url->getPath());
         $path = trim(trim(dirname($this->getPath()), '/') . '/' . trim(dirname($Url->getPath()), '/'), '/');
         $parts = explode('/', $path);
         $resolved = array();
         foreach ($parts as $part) {
             switch ($part) {
                 case '.':
                 case '':
                     //Do nothing...
                     break;
                 case '..':
                     array_pop($resolved);
                     break;
                 default:
                     array_push($resolved, $part);
                     break;
             }
         }
         if (count($resolved) > 0) {
             $new->setPath('/' . implode('/', $resolved) . '/' . $file);
         } else {
             $new->setPath('/' . $file);
         }
         return $new;
     }
 }