Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorMatthieu Napoli <matthieu@mnapoli.fr>2014-10-31 02:10:11 +0300
committerMatthieu Napoli <matthieu@mnapoli.fr>2014-10-31 02:11:20 +0300
commit1cd924d0371a2271798279b78e30585f4c7a80d9 (patch)
treed459921c818bda0a3f23fc53b9fcb60f92be3ced /core
parentf5b6b4ccb021a3b082e578fc9a42c715985d1fae (diff)
parentbca9fdab124fb27aa0a7c04da768011fba26589f (diff)
Merge branch 'master' into php-di
Conflicts: composer.json composer.lock
Diffstat (limited to 'core')
-rw-r--r--core/Columns/Dimension.php10
-rw-r--r--core/Exceptions/HtmlMessageException.php12
-rw-r--r--core/Exceptions/HtmlMessageExceptionInterface.php20
-rw-r--r--core/FrontController.php17
-rw-r--r--core/IP.php242
-rw-r--r--core/Tracker/Request.php5
-rw-r--r--core/Tracker/Visit.php8
-rw-r--r--core/Tracker/VisitExcluded.php5
-rw-r--r--core/Url.php5
9 files changed, 136 insertions, 188 deletions
diff --git a/core/Columns/Dimension.php b/core/Columns/Dimension.php
index d591cea9db..e760a09083 100644
--- a/core/Columns/Dimension.php
+++ b/core/Columns/Dimension.php
@@ -56,11 +56,11 @@ abstract class Dimension
* dimension. Example:
*
* ```
- $segment = new Segment();
- $segment->setSegment('exitPageUrl');
- $segment->setName('Actions_ColumnExitPageURL');
- $segment->setCategory('General_Visit');
- $this->addSegment($segment);
+ * $segment = new Segment();
+ * $segment->setSegment('exitPageUrl');
+ * $segment->setName('Actions_ColumnExitPageURL');
+ * $segment->setCategory('General_Visit');
+ * $this->addSegment($segment);
* ```
*/
protected function configureSegments()
diff --git a/core/Exceptions/HtmlMessageException.php b/core/Exceptions/HtmlMessageException.php
index 243a0f1726..69f809e1e0 100644
--- a/core/Exceptions/HtmlMessageException.php
+++ b/core/Exceptions/HtmlMessageException.php
@@ -16,15 +16,7 @@ use Exception;
*
* @api
*/
-class HtmlMessageException extends Exception
+class HtmlMessageException extends Exception implements HtmlMessageExceptionInterface
{
- /**
- * Returns the exception message.
- *
- * @return string
- */
- public function getHtmlMessage()
- {
- return $this->getMessage();
- }
+ // empty
} \ No newline at end of file
diff --git a/core/Exceptions/HtmlMessageExceptionInterface.php b/core/Exceptions/HtmlMessageExceptionInterface.php
new file mode 100644
index 0000000000..b96c2e0a84
--- /dev/null
+++ b/core/Exceptions/HtmlMessageExceptionInterface.php
@@ -0,0 +1,20 @@
+<?php
+/**
+ * Piwik - free/libre analytics platform
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ */
+namespace Piwik\Exceptions;
+
+/**
+ * Exceptions that implement this interface are assumed to have HTML content
+ * in their messages.
+ *
+ * @api
+ */
+interface HtmlMessageExceptionInterface
+{
+ // empty
+} \ No newline at end of file
diff --git a/core/FrontController.php b/core/FrontController.php
index a45a21da7c..85fbbc8994 100644
--- a/core/FrontController.php
+++ b/core/FrontController.php
@@ -13,6 +13,7 @@ use Exception;
use Piwik\API\Request;
use Piwik\API\ResponseBuilder;
use Piwik\Exceptions\HtmlMessageException;
+use Piwik\Exceptions\HtmlMessageExceptionInterface;
use Piwik\Http\Router;
use Piwik\Plugin\Controller;
use Piwik\Plugin\Report;
@@ -614,14 +615,24 @@ class FrontController extends Singleton
{
$debugTrace = $ex->getTraceAsString();
- if (method_exists($ex, 'getHtmlMessage')) {
- $message = $ex->getHtmlMessage();
+ if ($ex instanceof HtmlMessageExceptionInterface) {
+ $message = $ex->getMessage();
} else {
$message = Common::sanitizeInputValue($ex->getMessage());
}
$logo = new CustomLogo();
- $result = Piwik_GetErrorMessagePage($message, $debugTrace, true, true, $logo->getHeaderLogoUrl(), $logo->getPathUserFavicon());
+
+ $logoHeaderUrl = false;
+ $logoFaviconUrl = false;
+ try {
+ $logoHeaderUrl = $logo->getHeaderLogoUrl();
+ $logoFaviconUrl = $logo->getPathUserFavicon();
+ } catch (Exception $ex) {
+ Log::debug($ex);
+ }
+
+ $result = Piwik_GetErrorMessagePage($message, $debugTrace, true, true, $logoHeaderUrl, $logoFaviconUrl);
/**
* Triggered before a Piwik error page is displayed to the user.
diff --git a/core/IP.php b/core/IP.php
index 691bc7442c..e952ddbe27 100644
--- a/core/IP.php
+++ b/core/IP.php
@@ -9,9 +9,20 @@
namespace Piwik;
+use Piwik\Network\IPUtils;
+use Piwik\Network\IPv4;
+use Piwik\Network\IPv6;
+
/**
* Contains IP address helper functions (for both IPv4 and IPv6).
*
+ * As of Piwik 2.9, most methods in this class are deprecated. You are
+ * encouraged to use classes from the Piwik "Network" component:
+ *
+ * @see \Piwik\Network\IP
+ * @see \Piwik\Network\IPUtils
+ * @link https://github.com/piwik/component-network
+ *
* As of Piwik 1.3, IP addresses are stored in the DB has VARBINARY(16),
* and passed around in network address format which has the advantage of
* being in big-endian byte order. This allows for binary-safe string
@@ -28,47 +39,18 @@ namespace Piwik;
*/
class IP
{
- const MAPPED_IPv4_START = '::ffff:';
-
/**
* Removes the port and the last portion of a CIDR IP address.
*
* @param string $ipString The IP address to sanitize.
* @return string
+ *
+ * @deprecated Use IPUtils::sanitizeIp() instead
+ * @see \Piwik\Network\IPUtils
*/
public static function sanitizeIp($ipString)
{
- $ipString = trim($ipString);
-
- // CIDR notation, A.B.C.D/E
- $posSlash = strrpos($ipString, '/');
- if ($posSlash !== false) {
- $ipString = substr($ipString, 0, $posSlash);
- }
-
- $posColon = strrpos($ipString, ':');
- $posDot = strrpos($ipString, '.');
- if ($posColon !== false) {
- // IPv6 address with port, [A:B:C:D:E:F:G:H]:EEEE
- $posRBrac = strrpos($ipString, ']');
- if ($posRBrac !== false && $ipString[0] == '[') {
- $ipString = substr($ipString, 1, $posRBrac - 1);
- }
-
- if ($posDot !== false) {
- // IPv4 address with port, A.B.C.D:EEEE
- if ($posColon > $posDot) {
- $ipString = substr($ipString, 0, $posColon);
- }
- // else: Dotted quad IPv6 address, A:B:C:D:E:F:G.H.I.J
- } else if (strpos($ipString, ':') === $posColon) {
- $ipString = substr($ipString, 0, $posColon);
- }
- // else: IPv6 address, A:B:C:D:E:F:G:H
- }
- // else: IPv4 address, A.B.C.D
-
- return $ipString;
+ return IPUtils::sanitizeIp($ipString);
}
/**
@@ -83,43 +65,15 @@ class IP
*
* @param string $ipRangeString IP address range
* @return string|bool IP address range in CIDR notation OR false
+ *
+ * @deprecated Use IPUtils::sanitizeIpRange() instead
+ * @see \Piwik\Network\IPUtils
*/
public static function sanitizeIpRange($ipRangeString)
{
- $ipRangeString = trim($ipRangeString);
- if (empty($ipRangeString)) {
- return false;
- }
+ $result = IPUtils::sanitizeIpRange($ipRangeString);
- // IPv4 address with wildcards '*'
- if (strpos($ipRangeString, '*') !== false) {
- if (preg_match('~(^|\.)\*\.\d+(\.|$)~D', $ipRangeString)) {
- return false;
- }
-
- $bits = 32 - 8 * substr_count($ipRangeString, '*');
- $ipRangeString = str_replace('*', '0', $ipRangeString);
- }
-
- // CIDR
- if (($pos = strpos($ipRangeString, '/')) !== false) {
- $bits = substr($ipRangeString, $pos + 1);
- $ipRangeString = substr($ipRangeString, 0, $pos);
- }
-
- // single IP
- if (($ip = @inet_pton($ipRangeString)) === false)
- return false;
-
- $maxbits = strlen($ip) * 8;
- if (!isset($bits))
- $bits = $maxbits;
-
- if ($bits < 0 || $bits > $maxbits) {
- return false;
- }
-
- return "$ipRangeString/$bits";
+ return $result === null ? false : $result;
}
/**
@@ -127,12 +81,13 @@ class IP
*
* @param string $ipString IP address, either IPv4 or IPv6, e.g., `"127.0.0.1"`.
* @return string Binary-safe string, e.g., `"\x7F\x00\x00\x01"`.
+ *
+ * @deprecated Use IPUtils::stringToBinaryIP() instead
+ * @see \Piwik\Network\IPUtils
*/
public static function P2N($ipString)
{
- // use @inet_pton() because it throws an exception and E_WARNING on invalid input
- $ip = @inet_pton($ipString);
- return $ip === false ? "\x00\x00\x00\x00" : $ip;
+ return IPUtils::stringToBinaryIP($ipString);
}
/**
@@ -142,12 +97,12 @@ class IP
*
* @param string $ip IP address in network address format.
* @return string IP address in presentation format.
+ *
+ * @deprecated Use IPUtils::binaryToStringIP() instead
*/
public static function N2P($ip)
{
- // use @inet_ntop() because it throws an exception and E_WARNING on invalid input
- $ipStr = @inet_ntop($ip);
- return $ipStr === false ? '0.0.0.0' : $ipStr;
+ return IPUtils::binaryToStringIP($ip);
}
/**
@@ -155,10 +110,12 @@ class IP
*
* @param string $ip IP address in network address format.
* @return string IP address in presentation format.
+ *
+ * @deprecated Will be removed
*/
public static function prettyPrint($ip)
{
- return self::N2P($ip);
+ return IPUtils::binaryToStringIP($ip);
}
/**
@@ -167,27 +124,15 @@ class IP
*
* @param string $ip IP address in network address format.
* @return bool True if IPv4, else false.
+ *
+ * @deprecated Will be removed
+ * @see \Piwik\Network\IP
*/
public static function isIPv4($ip)
{
- // in case mbstring overloads strlen function
- $strlen = function_exists('mb_orig_strlen') ? 'mb_orig_strlen' : 'strlen';
+ $ip = Network\IP::fromBinaryIP($ip);
- // IPv4
- if ($strlen($ip) == 4) {
- return true;
- }
-
- // IPv6 - transitional address?
- if ($strlen($ip) == 16) {
- if (substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff", 0, 12) === 0
- || substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0, 12) === 0
- ) {
- return true;
- }
- }
-
- return false;
+ return $ip instanceof IPv4;
}
/**
@@ -200,12 +145,14 @@ class IP
*
* @param string $ip IPv4 address in network address format.
* @return string IP address in presentation format.
+ *
+ * @deprecated This method was kept for backward compatibility and doesn't seem used
*/
public static function long2ip($ip)
{
// IPv4
if (strlen($ip) == 4) {
- return self::N2P($ip);
+ return IPUtils::binaryToStringIP($ip);
}
// IPv6 - transitional address?
@@ -214,7 +161,7 @@ class IP
|| substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0, 12) === 0
) {
// remap 128-bit IPv4-mapped and IPv4-compat addresses
- return self::N2P(substr($ip, 12));
+ return IPUtils::binaryToStringIP(substr($ip, 12));
}
}
@@ -227,10 +174,15 @@ class IP
*
* @param string $ip
* @return bool
+ *
+ * @deprecated Will be removed
+ * @see \Piwik\Network\IP
*/
public static function isIPv6($ip)
{
- return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
+ $ip = Network\IP::fromBinaryIP($ip);
+
+ return $ip instanceof IPv6;
}
/**
@@ -238,10 +190,19 @@ class IP
*
* @param string $ip
* @return bool
+ *
+ * @deprecated Will be removed
+ * @see \Piwik\Network\IP
*/
public static function isMappedIPv4($ip)
{
- return substr($ip, 0, strlen(self::MAPPED_IPv4_START)) === self::MAPPED_IPv4_START;
+ $ip = Network\IP::fromStringIP($ip);
+
+ if (! $ip instanceof IPv6) {
+ return false;
+ }
+
+ return $ip->isMappedIPv4();
}
/**
@@ -249,10 +210,15 @@ class IP
*
* @param string $ip eg, `'::ffff:192.0.2.128'`
* @return string eg, `'192.0.2.128'`
+ *
+ * @deprecated Use Piwik\Network\IP::toIPv4String() instead
+ * @see \Piwik\Network\IP
*/
public static function getIPv4FromMappedIPv6($ip)
{
- return substr($ip, strlen(self::MAPPED_IPv4_START));
+ $ip = Network\IP::fromStringIP($ip);
+
+ return $ip->toIPv4String();
}
/**
@@ -260,37 +226,15 @@ class IP
*
* @param array $ipRange An IP address range in presentation format.
* @return array|bool Array `array($lowIp, $highIp)` in network address format, or false on failure.
+ *
+ * @deprecated Use Piwik\Network\IPUtils::getIPRangeBounds() instead
+ * @see \Piwik\Network\IPUtils
*/
public static function getIpsForRange($ipRange)
{
- if (strpos($ipRange, '/') === false) {
- $ipRange = self::sanitizeIpRange($ipRange);
- }
- $pos = strpos($ipRange, '/');
-
- $bits = substr($ipRange, $pos + 1);
- $range = substr($ipRange, 0, $pos);
- $high = $low = @inet_pton($range);
- if ($low === false) {
- return false;
- }
+ $result = IPUtils::getIPRangeBounds($ipRange);
- $lowLen = strlen($low);
- $i = $lowLen - 1;
- $bits = $lowLen * 8 - $bits;
-
- for ($n = (int)($bits / 8); $n > 0; $n--, $i--) {
- $low[$i] = chr(0);
- $high[$i] = chr(255);
- }
-
- $n = $bits % 8;
- if ($n) {
- $low[$i] = chr(ord($low[$i]) & ~((1 << $n) - 1));
- $high[$i] = chr(ord($high[$i]) | ((1 << $n) - 1));
- }
-
- return array($low, $high);
+ return $result === null ? false : $result;
}
/**
@@ -301,40 +245,15 @@ class IP
* @param string $ip IP address in network address format
* @param array $ipRanges List of IP address ranges
* @return bool True if in any of the specified IP address ranges; else false.
+ *
+ * @deprecated Use Piwik\Network\IP::isInRanges() instead
+ * @see \Piwik\Network\IP
*/
public static function isIpInRange($ip, $ipRanges)
{
- $ipLen = strlen($ip);
- if (empty($ip) || empty($ipRanges) || ($ipLen != 4 && $ipLen != 16)) {
- return false;
- }
+ $ip = Network\IP::fromBinaryIP($ip);
- foreach ($ipRanges as $range) {
- if (is_array($range)) {
- // already split into low/high IP addresses
- $range[0] = self::P2N($range[0]);
- $range[1] = self::P2N($range[1]);
- } else {
- // expect CIDR format but handle some variations
- $range = self::getIpsForRange($range);
- }
- if ($range === false) {
- continue;
- }
-
- $low = $range[0];
- $high = $range[1];
- if (strlen($low) != $ipLen) {
- continue;
- }
-
- // binary-safe string comparison
- if ($ip >= $low && $ip <= $high) {
- return true;
- }
- }
-
- return false;
+ return $ip->isInRanges($ipRanges);
}
/**
@@ -356,7 +275,7 @@ class IP
}
$ipString = self::getNonProxyIpFromHeader($default, $clientHeaders);
- return self::sanitizeIp($ipString);
+ return IPUtils::sanitizeIp($ipString);
}
/**
@@ -406,7 +325,8 @@ class IP
$elements = explode(',', $csv);
for ($i = count($elements); $i--;) {
$element = trim(Common::sanitizeInputValue($elements[$i]));
- if (empty($excludedIps) || (!in_array($element, $excludedIps) && !self::isIpInRange(self::P2N(self::sanitizeIp($element)), $excludedIps))) {
+ $ip = \Piwik\Network\IP::fromStringIP(IPUtils::sanitizeIp($element));
+ if (empty($excludedIps) || (!in_array($element, $excludedIps) && !$ip->isInRanges($excludedIps))) {
return $element;
}
}
@@ -415,16 +335,20 @@ class IP
}
/**
- * Retirms the hostname for a given IP address.
+ * Returns the hostname for a given IP address.
*
* @param string $ipStr Human-readable IP address.
* @return string The hostname or unmodified $ipStr on failure.
+ *
+ * @deprecated Use Piwik\Network\IP::getHostname() instead
+ * @see \Piwik\Network\IP
*/
public static function getHostByAddr($ipStr)
{
- // PHP's reverse lookup supports ipv4 and ipv6
- // except on Windows before PHP 5.3
- $host = strtolower(@gethostbyaddr($ipStr));
- return $host === '' ? $ipStr : $host;
+ $ip = Network\IP::fromStringIP($ipStr);
+
+ $host = $ip->getHostname();
+
+ return $host === null ? $ipStr : $host;
}
}
diff --git a/core/Tracker/Request.php b/core/Tracker/Request.php
index 36b1e3a5b9..b733ae30ea 100644
--- a/core/Tracker/Request.php
+++ b/core/Tracker/Request.php
@@ -13,6 +13,7 @@ use Piwik\Common;
use Piwik\Config;
use Piwik\Cookie;
use Piwik\IP;
+use Piwik\Network\IPUtils;
use Piwik\Piwik;
use Piwik\Plugins\CustomVariables\CustomVariables;
use Piwik\Registry;
@@ -563,9 +564,7 @@ class Request
public function getIp()
{
- $ipString = $this->getIpString();
- $ip = IP::P2N($ipString);
- return $ip;
+ return IPUtils::stringToBinaryIP($this->getIpString());
}
public function getForcedUserId()
diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php
index c8fb12d389..8bdf641ece 100644
--- a/core/Tracker/Visit.php
+++ b/core/Tracker/Visit.php
@@ -11,7 +11,7 @@ namespace Piwik\Tracker;
use Piwik\Common;
use Piwik\Config;
-use Piwik\IP;
+use Piwik\Network\IPUtils;
use Piwik\Piwik;
use Piwik\Plugin\Dimension\VisitDimension;
use Piwik\Tracker;
@@ -241,7 +241,7 @@ class Visit implements VisitInterface
*/
protected function handleExistingVisit($visitor, $action, $visitIsConverted)
{
- Common::printDebug("Visit is known (IP = " . IP::N2P($this->getVisitorIp()) . ")");
+ Common::printDebug("Visit is known (IP = " . IPUtils::binaryToStringIP($this->getVisitorIp()) . ")");
$valuesToUpdate = $this->getExistingVisitFieldsToUpdate($visitor, $action, $visitIsConverted);
@@ -301,7 +301,7 @@ class Visit implements VisitInterface
*/
protected function handleNewVisit($visitor, $action, $visitIsConverted)
{
- Common::printDebug("New Visit (IP = " . IP::N2P($this->getVisitorIp()) . ")");
+ Common::printDebug("New Visit (IP = " . IPUtils::binaryToStringIP($this->getVisitorIp()) . ")");
$this->setNewVisitorInformation($visitor);
@@ -467,7 +467,7 @@ class Visit implements VisitInterface
$debugVisitInfo = $this->visitorInfo;
$debugVisitInfo['idvisitor'] = bin2hex($debugVisitInfo['idvisitor']);
$debugVisitInfo['config_id'] = bin2hex($debugVisitInfo['config_id']);
- $debugVisitInfo['location_ip'] = IP::N2P($debugVisitInfo['location_ip']);
+ $debugVisitInfo['location_ip'] = IPUtils::binaryToStringIP($debugVisitInfo['location_ip']);
Common::printDebug($debugVisitInfo);
}
diff --git a/core/Tracker/VisitExcluded.php b/core/Tracker/VisitExcluded.php
index 48eb1e3db7..ca481218cb 100644
--- a/core/Tracker/VisitExcluded.php
+++ b/core/Tracker/VisitExcluded.php
@@ -213,8 +213,9 @@ class VisitExcluded
$websiteAttributes = Cache::getCacheWebsiteAttributes($this->idSite);
if (!empty($websiteAttributes['excluded_ips'])) {
- if (IP::isIpInRange($this->ip, $websiteAttributes['excluded_ips'])) {
- Common::printDebug('Visitor IP ' . IP::N2P($this->ip) . ' is excluded from being tracked');
+ $ip = \Piwik\Network\IP::fromBinaryIP($this->ip);
+ if ($ip->isInRanges($websiteAttributes['excluded_ips'])) {
+ Common::printDebug('Visitor IP ' . $ip->toString() . ' is excluded from being tracked');
return true;
}
}
diff --git a/core/Url.php b/core/Url.php
index 1d34c17ca3..dd9503ca66 100644
--- a/core/Url.php
+++ b/core/Url.php
@@ -10,6 +10,7 @@ namespace Piwik;
use Exception;
+use Piwik\Network\IPUtils;
use Piwik\Session;
/**
@@ -547,7 +548,7 @@ class Url
$disableHostCheck = Config::getInstance()->General['enable_trusted_host_check'] == 0;
// compare scheme and host
$parsedUrl = @parse_url($url);
- $host = IP::sanitizeIp(@$parsedUrl['host']);
+ $host = IPUtils::sanitizeIp(@$parsedUrl['host']);
return !empty($host)
&& ($disableHostCheck || in_array($host, $hosts))
&& !empty($parsedUrl['scheme'])
@@ -585,7 +586,7 @@ class Url
*/
public static function getHostSanitized($host)
{
- return IP::sanitizeIp($host);
+ return IPUtils::sanitizeIp($host);
}
protected static function getHostsFromConfig($domain, $key)