function smarty_function_scripts_get_url($url, $baseUrl) { if (String::looksLikeUrl($url)) { return $url; } return $baseUrl . $url; }
/** * Process the request, get the field value * @return null */ public function processRequest() { parent::processRequest(); if ($this->value && !String::looksLikeUrl($this->value)) { $this->value = 'http://' . $this->value; } }
/** * Parses the tag * @param string $content Content of the tag * @param array $parameters Parameters of the tag * @return string HTML replacement for the tag */ public function parseTag($content, array $parameters) { if (!$content || !String::looksLikeUrl($content)) { return false; } $width = null; $height = null; $alt = null; if (array_key_exists(0, $parameters) && strpos($parameters[0], 'x')) { // [img=<width>x<height>]url[/img] list($width, $height) = explode($parameters[0]); } else { if (array_key_exists('width', $parameters)) { // [img width=<width>]url[/img] $width = $parameters['width']; } elseif (array_key_exists('w', $parameters)) { // [img w=<width>]url[/img] $width = $parameters['w']; } if (array_key_exists('height', $parameters)) { // [img height=<height>]url[/img] $height = $parameters['height']; } elseif (array_key_exists('h', $parameters)) { // [img h=<height>]url[/img] $height = $parameters['h']; } } if (array_key_exists('alt', $parameters)) { $alt = $parameters['alt']; } return $this->getImageHtml($content, $width, $height, $alt); }
/** * Get the html for this menu item * @return string */ public function getHtml() { if (String::looksLikeUrl($this->route)) { $url = $this->route; } else { $url = $this->baseUrl . $this->route; } return '<li' . $this->getAttributesHtml() . '>' . '<a href="' . $url . '">' . $this->label . '</a>' . '</li>'; }
/** * Gets the absolute URL of a made reference * @param string $url The made reference * @param string $baseUrl The base URL of the node which is linking the URL * @param string $basePath The base path of the node which is linking the URL * @return string The absolute URL of the reference */ protected function getAbsoluteUrl($url, $baseUrl, $basePath) { if (!String::looksLikeUrl($url)) { if (String::startsWith($url, '/')) { $url = rtrim($baseUrl, '/') . $url; } else { $url = $basePath . $url; } } return $url; }
/** * Gets the redirect url of the provided response * @param HttpRequest $request The performed request * @param HttpResponse $response The received response * @return zibo\library\Url The URL of the redirect location * @throws zibo\library\network\http\exception\HttpException when the provided response has no Location header */ public function getRedirectUrl(HttpRequest $request, HttpResponse $response) { $location = $response->getHeader('Location'); if (!$location) { throw new HttpException('Could not get the redirect URL from the response: no Location header set'); } if (!String::looksLikeUrl($location)) { if ($location[0] == '/') { $base = $request->getUrl()->getBaseUrl(); } else { $base = $request->getUrl()->getBasePath(); } $location = rtrim($base, '/') . '/' . ltrim($location, '/'); } return new Url($location); }
/** * Get the html for this image tag * @return string html of this image tag */ public function getHtml() { $source = $this->getSource(); if (!String::looksLikeUrl($source)) { $source = $this->processSource($source); $request = Zibo::getInstance()->getRequest(); $source = $request->getBaseUrl() . '/' . $source; $this->setSource($source); } $html = '<img' . $this->getAttributeHtml(self::ATTRIBUTE_SRC, $source) . $this->getIdHtml() . $this->getClassHtml() . $this->getAttributesHtml() . ' />'; return $html; }
/** * Starts the crawling * @param integer $delay Delay between each page in miliseconds * @param zibo\library\filesystem\File $statusFile File where the status of the crawling process is written * @param zibo\library\filesystem\File $cancelFile File which will cancel/stop the crawling process when exists * @return null */ public function crawl($delay = 1000, File $statusFile = null, File $cancelFile = null) { $prey = $this->web->resetPrey(); $start = time(); $index = 0; $isCancelled = false; while ($prey) { if ($cancelFile && $cancelFile->exists()) { $cancelFile->delete(); $isCancelled = true; break; } usleep($delay * 1000); $index++; $url = $prey->getUrl(); if ($this->shouldIgnore($url)) { $prey->addType(WebNode::TYPE_IGNORED); $prey = $this->web->getNextPrey(); continue; } if ($statusFile) { $status = new SpiderStatus($url, $index, $this->web->countNodes(), $start); $status->write($statusFile); } if (String::startsWith($url, 'mailto:')) { $prey->addType(WebNode::TYPE_MAILTO); $prey = $this->web->getNextPrey(); continue; } try { $crawl = new Crawl($url); $crawl->performCrawl(); $response = $crawl->getResponse(); $prey->setResponse($response); if ($response->isRedirect()) { $location = $response->getHeader('Location'); if (!String::looksLikeUrl($location)) { if ($location[0] == '/') { $base = $crawl->getBaseUrl(); } else { $base = $crawl->getBasePath(); } $location = rtrim($base, '/') . '/' . ltrim($location, '/'); } if ($url == $location) { throw new Exception('Redirect loop'); } $locationNode = $this->web->getNode($location); $locationNode->addReference($prey); $prey->addLink($locationNode); } if (!String::startsWith($url, $this->baseUrl)) { $prey->addType(WebNode::TYPE_EXTERNAL); if (!$this->willBiteExternalNodes) { $prey = $this->web->getNextPrey(); continue; } } $this->bite($prey, $crawl->getBaseUrl(), $crawl->getBasePath()); } catch (Exception $exception) { if ($crawl) { $response = $crawl->getResponse(); if ($response) { $prey->setResponse($response); } } $prey->setError($exception->getMessage()); } $prey = $this->web->getNextPrey(); } if (!$isCancelled) { if ($statusFile) { $status = new SpiderStatus("reports", $index, $this->web->countNodes(), $start); $status->write($statusFile); } foreach ($this->reports as $report) { $report->setWeb($this->web); } } if ($statusFile) { $status = new SpiderStatus(null, $index, $this->web->countNodes(), $start, time()); $status->write($statusFile); } }
/** * @dataProvider providerLooksLikeUrl */ public function testLooksLikeUrl($expected, $value) { $result = String::looksLikeUrl($value); $this->assertEquals($expected, $result); }