/** * @inheritdoc */ public function benchmark($count, $uri) { $start = microtime(true); $memory = memory_get_usage(); $uriParser = new Uri(null); foreach ($this->generateUri($count, $uri) as $url) { $uriParser->parse($url); } return ['memory' => memory_get_usage() - $memory, 'duration' => microtime(true) - $start]; }
/** * Get a list of thumbs from a URI * @param string $uri * @param integer $limit * @return array */ public function getThumbs($uri, $limit = 5) { $validator = new \Zend\Validator\Uri(array('allowRelative' => false)); $return = array(); if ($validator->isValid($uri)) { $parseInfo = parent::parse($uri); switch ($parseInfo->host) { // Youtube.com case "www.youtube.com": $queryArray = array(); parse_str($parseInfo->query, $queryArray); if (isset($queryArray['v'])) { $return = array("http://img.youtube.com/vi/" . $queryArray['v'] . "/0.jpg", "http://img.youtube.com/vi/" . $queryArray['v'] . "/1.jpg", "http://img.youtube.com/vi/" . $queryArray['v'] . "/2.jpg", "http://img.youtube.com/vi/" . $queryArray['v'] . "/3.jpg"); } break; // Dailymotion.com // Dailymotion.com case "www.dailymotion.com": if (strpos($parseInfo->path, "/video") !== false) { $return = array('http://www.dailymotion.com/thumbnail' . $parseInfo->path); } break; // Vimeo.com // Vimeo.com case "vimeo.com": $id = str_replace("/", "", $parseInfo->path); $data = \Zend\Json\Json::decode(file_get_contents("http://vimeo.com/api/v2/video/{$id}.json")); $return = array($data[0]->thumbnail_medium); break; // others webpage // others webpage default: /** * Credit to http://www.bitrepository.com * http://www.bitrepository.com/extract-images-from-an-url.html */ // Fetch page $string = $this->fetchPage($uri); $out = array(); // Regex for SRC Value $image_regex_src_url = '/<img[^>]*' . 'src=[\\"|\'](.*)[\\"|\']/Ui'; preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER); $return = $out[1]; for ($i = 0; $i < count($return); $i++) { $tUri = new Uri(); $parseInfoThumb = $tUri->parse($return[$i]); if (!$parseInfoThumb->isAbsolute()) { $return[$i] = $parseInfo->scheme . "://" . $parseInfo->host . "" . $return[$i]; } } } } // check && return return array_slice($return, 0, $limit); }
/** * * @param string $uri * @return string */ public function getType($uri) { $validator = new \Zend\Validator\Uri(array('allowRelative' => false)); if ($validator->isValid($uri)) { $parseInfo = parent::parse($uri); switch ($parseInfo->host) { // Youtube.com case "www.youtube.com": $queryArray = array(); parse_str($parseInfo->query, $queryArray); if (isset($queryArray['v'])) { return self::TYPE_VIDEO_YOUTUBE; } break; // Dailymotion.com // Dailymotion.com case "www.dailymotion.com": if (strpos($parseInfo->path, "/video") !== false) { return self::TYPE_VIDEO_DAILYMOTION; } break; // Vimeo.com // Vimeo.com case "vimeo.com": if (is_int($parseInfo->path)) { return self::TYPE_VIDEO_VIMEO; } break; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $uri); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $head = curl_exec($ch); $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); curl_close($ch); if (strpos($contentType, "image") !== false) { return self::TYPE_IMAGE; } else { if ($contentType == "application/x-shockwave-flash") { return self::TYPE_SWF; } } return self::TYPE_WEBPAGE; } else { return ""; } }
public function getRedirect($urlString, $stayLocal = true, $preserveHttps = true) { /** * Check that the URL has the correct format expected of a valid HTTP * or HTTPS URL. If so, normalize the URL. */ $valid = false; $url = new Uri(); try { $url->parse($urlString); if ($url->isValid() && $url->isAbsolute()) { $url->normalize(); $valid = true; } } catch (\Exception $e) { } if (false === $valid) { throw new Exception\InvalidArgumentException("Given value was not a valid absolute HTTP(S) URL: " . $url); } /** * Make sure we don't redirect from HTTPS to HTTP unless flagged by * the user. Using a Strict-Transport-Security header helps too! */ if (true === (bool) $preserveHttps && HttpsDetector::isHttpsRequest()) { if (!$this->isHttps($url)) { throw new Exception\InvalidArgumentException("Given value was not a HTTPS URL as expected: " . $url); } } /** * Check if the URL meets the local host restriction unless disabled */ if (true === $stayLocal && !$this->isLocal($url)) { throw new Exception\InvalidArgumentException("Given value was not a local HTTP(S) URL: " . $url); } /** * Check if the URL host exists on a whitelist of allowed hosts */ $whitelist = $this->getWhitelist(); if (!empty($whitelist) && !$this->isWhitelisted($url)) { throw new Exception\InvalidArgumentException("Given value was not a whitelisted URL as expected: " . $url); } /** * Get URL string after URL encoding checks and return a Location header * object. */ $header = new Header\Location(array('url' => $url->toString(), 'status_code' => 302)); return $header; }
public function testParseTwice() { $uri = new Uri(); $uri->parse('http://user@example.com:1/absolute/url?query#fragment'); $uri->parse('/relative/url'); $this->assertNull($uri->getScheme()); $this->assertNull($uri->getHost()); $this->assertNull($uri->getUserInfo()); $this->assertNull($uri->getPort()); $this->assertNull($uri->getQuery()); $this->assertNull($uri->getFragment()); }
/** * Parse a URI string * * @param string $uri * @return \Zend\Uri\Uri */ public function parse($uri) { return $this->uri->parse($uri); }
/** * Parse a URI string * * @access public * @param string $uri * @return Uri */ public function parse($uri) { $this->uri = $uri; return parent::parse($uri); }