function makeAbsoluteStr($base, $url)
{
    $base = new SimplePie_IRI($base);
    // remove '//' in URL path (causes URLs not to resolve properly)
    if (isset($base->path)) {
        $base->path = preg_replace('!//+!', '/', $base->path);
    }
    if (preg_match('!^https?://!i', $url)) {
        // already absolute
        return $url;
    } else {
        if ($absolute = SimplePie_IRI::absolutize($base, $url)) {
            return $absolute;
        }
        return false;
    }
}
 /**
  * Create a new IRI object by resolving a relative IRI
  *
  * Returns false if $base is not absolute, otherwise an IRI.
  *
  * @param IRI|string $base (Absolute) Base IRI
  * @param IRI|string $relative Relative IRI
  * @return IRI|false
  */
 public static function absolutize($base, $relative)
 {
     if (!$relative instanceof SimplePie_IRI) {
         $relative = new SimplePie_IRI($relative);
     }
     if (!$relative->is_valid()) {
         return false;
     } elseif ($relative->scheme !== null) {
         return clone $relative;
     } else {
         if (!$base instanceof SimplePie_IRI) {
             $base = new SimplePie_IRI($base);
         }
         if ($base->scheme !== null && $base->is_valid()) {
             if ($relative->get_iri() !== '') {
                 if ($relative->iuserinfo !== null || $relative->ihost !== null || $relative->port !== null) {
                     $target = clone $relative;
                     $target->scheme = $base->scheme;
                 } else {
                     $target = new SimplePie_IRI();
                     $target->scheme = $base->scheme;
                     $target->iuserinfo = $base->iuserinfo;
                     $target->ihost = $base->ihost;
                     $target->port = $base->port;
                     if ($relative->ipath !== '') {
                         if ($relative->ipath[0] === '/') {
                             $target->ipath = $relative->ipath;
                         } elseif (($base->iuserinfo !== null || $base->ihost !== null || $base->port !== null) && $base->ipath === '') {
                             $target->ipath = '/' . $relative->ipath;
                         } elseif (($last_segment = strrpos($base->ipath, '/')) !== false) {
                             $target->ipath = substr($base->ipath, 0, $last_segment + 1) . $relative->ipath;
                         } else {
                             $target->ipath = $relative->ipath;
                         }
                         $target->ipath = $target->remove_dot_segments($target->ipath);
                         $target->iquery = $relative->iquery;
                     } else {
                         $target->ipath = $base->ipath;
                         if ($relative->iquery !== null) {
                             $target->iquery = $relative->iquery;
                         } elseif ($base->iquery !== null) {
                             $target->iquery = $base->iquery;
                         }
                     }
                     $target->ifragment = $relative->ifragment;
                 }
             } else {
                 $target = clone $base;
                 $target->ifragment = null;
             }
             $target->scheme_normalization();
             return $target;
         } else {
             return false;
         }
     }
 }
Esempio n. 3
0
 /**
  * Try to find the refresh url from the meta.
  *
  * @param string $url  Absolute url
  * @param string $html First characters of the response (hopefully it'll be enough to find some meta)
  *
  * @return false|string
  */
 private function getMetaRefreshURL($url, $html)
 {
     if ($html == '') {
         return false;
     }
     // <meta HTTP-EQUIV="REFRESH" content="0; url=http://www.bernama.com/bernama/v6/newsindex.php?id=943513">
     if (!preg_match('!<meta http-equiv=["\']?refresh["\']? content=["\']?[0-9];\\s*url=["\']?([^"\'>]+)["\']?!i', $html, $match)) {
         return false;
     }
     $redirect_url = trim($match[1]);
     if (preg_match('!^https?://!i', $redirect_url)) {
         // already absolute
         $this->logger->log('debug', 'Meta refresh redirect found (http-equiv="refresh"), new URL: ' . $redirect_url);
         return $redirect_url;
     }
     // absolutize redirect URL
     $base = new \SimplePie_IRI($url);
     // remove '//' in URL path (causes URLs not to resolve properly)
     if (isset($base->ipath)) {
         $base->ipath = str_replace('//', '/', $base->ipath);
     }
     if ($absolute = \SimplePie_IRI::absolutize($base, $redirect_url)) {
         $this->logger->log('debug', 'Meta refresh redirect found (http-equiv="refresh"), new URL: ' . $absolute);
         return $absolute->get_iri();
     }
     return false;
 }
Esempio n. 4
0
 public static function compress_parse_url($scheme = '', $authority = '', $path = '', $query = '', $fragment = '')
 {
     $iri = new SimplePie_IRI('');
     $iri->scheme = $scheme;
     $iri->authority = $authority;
     $iri->path = $path;
     $iri->query = $query;
     $iri->fragment = $fragment;
     return $iri->get_uri();
 }
Esempio n. 5
0
 public static function normalize_url($url)
 {
     $iri = new SimplePie_IRI($url);
     return $iri->get_uri();
 }
Esempio n. 6
0
 public static function absolutize($base, $relative)
 {
     $relative = (string) $relative;
     if ($relative !== '') {
         $relative = new SimplePie_IRI($relative);
         if ($relative->get_scheme() !== null) {
             $target = $relative;
         } elseif ($base->get_iri() !== null) {
             if ($relative->get_authority() !== null) {
                 $target = $relative;
                 $target->set_scheme($base->get_scheme());
             } else {
                 $target = new SimplePie_IRI('');
                 $target->set_scheme($base->get_scheme());
                 $target->set_userinfo($base->get_userinfo());
                 $target->set_host($base->get_host());
                 $target->set_port($base->get_port());
                 if ($relative->get_path() !== null) {
                     if (strpos($relative->get_path(), '/') === 0) {
                         $target->set_path($relative->get_path());
                     } elseif (($base->get_userinfo() !== null || $base->get_host() !== null || $base->get_port() !== null) && $base->get_path() === null) {
                         $target->set_path('/' . $relative->get_path());
                     } elseif (($last_segment = strrpos($base->get_path(), '/')) !== false) {
                         $target->set_path(substr($base->get_path(), 0, $last_segment + 1) . $relative->get_path());
                     } else {
                         $target->set_path($relative->get_path());
                     }
                     $target->set_query($relative->get_query());
                 } else {
                     $target->set_path($base->get_path());
                     if ($relative->get_query() !== null) {
                         $target->set_query($relative->get_query());
                     } elseif ($base->get_query() !== null) {
                         $target->set_query($base->get_query());
                     }
                 }
             }
             $target->set_fragment($relative->get_fragment());
         } else {
             $target = $relative;
         }
     } else {
         $target = $base;
     }
     return $target;
 }
Esempio n. 7
0
 private function makeAbsoluteAttr($base, $e, $attr)
 {
     if ($e->hasAttribute($attr)) {
         // Trim leading and trailing white space. I don't really like this but
         // unfortunately it does appear on some sites. e.g.  <img src=" /path/to/image.jpg" />
         $url = trim(str_replace('%20', ' ', $e->getAttribute($attr)));
         $url = str_replace(' ', '%20', $url);
         if (!preg_match('!https?://!i', $url)) {
             if ($absolute = \SimplePie_IRI::absolutize($base, $url)) {
                 $e->setAttribute($attr, $absolute);
             }
         }
     }
 }
Esempio n. 8
0
 /**
  * @dataProvider normalization_tests
  */
 public function testStringNormalization($input, $output)
 {
     $input = new SimplePie_IRI($input);
     $this->assertEquals($output, $input->get_iri());
 }