/** * Adds a referer to the database. * * @param refererHeader The referer header as was received by PHP. * @param articleId The article being hit by this referer. * @return Returns true if successful or false otherwise. */ function addReferer($refererHeader, $articleId, $blogId) { // quit inmediately if this is not enabled... if (!$this->_enabled) { return; } // we only add a new referer if we come from somewhere else than our own server $ourHost = $_SERVER["HTTP_HOST"]; $refererUrl = new Url($refererHeader); // if they're the same, we quit if ($refererUrl->getHost() == $ourHost || $refererUrl->getHost() == "") { return; } // we have to check if a referer with that information exists // in the database $query = "UPDATE " . $this->getPrefix() . "referers SET hits = hits + 1 WHERE url = '" . Db::qstr($refererHeader) . "' AND article_id = '" . Db::qstr($articleId) . "' AND blog_id = '" . Db::qstr($blogId) . "';"; $result = $this->Execute($query); if (!$result) { return false; } // check how many rows were updated this time. if ($this->_db->Affected_Rows() == 0) { // we have to insert the row manually $query2 = "INSERT INTO " . $this->getPrefix() . "referers (url,article_id,blog_id) \n VALUES ('" . Db::qstr($refererHeader) . "','" . Db::qstr($articleId) . "','" . Db::qstr($blogId) . "');"; $result2 = $this->Execute($query2); } return true; }
/** * @private * @static * Sets the right domain for the cookie * @return nothing */ function setSessionCookieDomain() { $scriptUrl = HttpVars::getBaseUrl(); $url = new Url($scriptUrl); $domain = $url->getHost(); // this won't work for top level domains and domains such as // 'localhost' or internal domains for obvious security reasons... // See comments in http://fi.php.net/manual/en/function.session-set-cookie-params.php if (count(explode('.', $domain)) > 1) { return $domain; } }
/** * * @return boolean */ private function areHostsEquivalent() { if ((string) $this->sourceUrl->getHost() === (string) $this->comparatorUrl->getHost()) { return true; } foreach ($this->equivalentHosts as $equivalentHostSet) { if (in_array($this->sourceUrl->getHost(), $equivalentHostSet) && in_array($this->comparatorUrl->getHost(), $equivalentHostSet)) { return true; } } return false; }
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'); }
/** * returns true if a given url is using a subdomain or not. It works by comparing * the url with "base_url" from the plog_config table. If they match, then the incoming * url is *not* using subdomains. Otherwise, it will return true * * @param url If null, use $_SERVER["HTTP_HOST"] * @return true if the given url is subdomained or not * @static */ function isSubdomainUrl($url = null) { // prepare the url if ($url == null) { $server = HttpVars::getServer(); $urlObject = new Url("http://" . $server["HTTP_HOST"]); } else { $urlObject = new Url($url); } // and now get the base_url $config =& Config::getConfig(); $baseUrlObject = new Url($config->getValue("base_url")); // and finally check if whether they match or not if ($urlObject->getHost() == $baseUrlObject->getHost()) { $isSubdomain = false; } else { $isSubdomain = true; } // return it... return $isSubdomain; }
/** * 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; }
/** * Checks this url is base uri for the given url. * * @param Url $targetUri The url to inspect the base part. * * @return boolean */ public function isBaseOf(Url $targetUri) { if ($this->_parts['scheme'] !== $targetUri->getScheme() || $this->_parts['host'] !== $targetUri->getHost() || $this->getPort() !== $targetUri->getPort()) { return false; } $srcSegmentCount = count($this->_segments); $targetSegments = $targetUri->getSegments(); $targetSegmentCount = count($targetSegments); if ($srcSegmentCount > $targetSegmentCount) { return false; } for ($i = 0; $i < $srcSegmentCount; $i++) { if ($this->_segments[$i] !== $targetSegments[$i]) { return false; } } return true; }
/** * 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(); }
/** * @param $url * @param $expectedHost * * @dataProvider dataProviderForTestGetHost */ public function testGetHost($url, $expectedHost) { $url = new Url($url); $this->assertEquals($expectedHost, $url->getHost()); }
public function testUrlFragmentEncoding() { $url = new Url('http://127.0.0.1/foobar?bar=foo#!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'); $this->assertEquals('http', $url->getScheme()); $this->assertEquals(null, $url->getUserInfo()); $this->assertEquals('127.0.0.1', $url->getHost()); $this->assertEquals(null, $url->getPort()); $this->assertEquals('/foobar', $url->getPath()); $this->assertEquals(array('bar' => 'foo'), $url->getParameters()); $this->assertEquals('!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', $url->getFragment()); }
/** * generates a unique atom id for the entry. This is not as easy * as it sounds, take a look http://diveintomark.org/archives/2004/05/28/howto-atom-id * * @param article An Article object * @return A unique atom article id */ function getAtomUniqueId($article) { include_once PLOG_CLASS_PATH . "class/net/url.class.php"; $config =& Config::getConfig(); $url = new Url($config->getValue("base_url")); $articleDate = $article->getDateObject(); $date = $articleDate->getYear() . "-" . $articleDate->getMonth() . "-" . $articleDate->getDay(); $tag = "tag:post:" . $url->getHost() . "," . $date . ":" . $article->getId(); return $tag; }
/** * 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; } }