From 681f56042193fc97cf5c9d1bfbf55274bf535ec4 Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Tue, 5 Feb 2019 12:02:52 +1300 Subject: [PATCH] Not possible to create multiple Piwik tracker instances having different API urls fix https://github.com/matomo-org/matomo-php-tracker/issues/14 --- PiwikTracker.php | 62 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/PiwikTracker.php b/PiwikTracker.php index 6eaa652..01163cd 100644 --- a/PiwikTracker.php +++ b/PiwikTracker.php @@ -27,6 +27,7 @@ class PiwikTracker * PiwikTracker::$URL = 'http://yourwebsite.org/piwik/'; * * @var string + * @deprecated use setTrackerUrl instead */ static public $URL = ''; @@ -123,6 +124,7 @@ public function __construct($idSite, $apiUrl = '') if (!empty($apiUrl)) { self::$URL = $apiUrl; } + $this->trackerUrl = $apiUrl; // Life of the visitor cookie (in sec) $this->configVisitorCookieTimeout = 33955200; // 13 months (365 + 28 days) @@ -158,11 +160,21 @@ public function __construct($idSite, $apiUrl = '') $this->sendImageResponse = true; $this->visitorCustomVar = $this->getCustomVariablesFromCookie(); - + $this->outgoingTrackerCookies = array(); $this->incomingTrackerCookies = array(); } + /** + * Sets (overwrites) the Matomo base URL, for example http://example.org/matomo/. + * + * @param string $trackerUrl + */ + public function setTrackerUrl($trackerUrl) + { + $this->trackerUrl = $trackerUrl; + } + /** * By default, Piwik expects utf-8 encoded values, for example * for the page URL parameter values, Page Title, etc. @@ -1121,7 +1133,7 @@ public function getUrlTrackAction($actionUrl, $actionType) * * Allowed only for Admin/Super User, must be used along with setTokenAuth() * @see setTokenAuth() - * @param string $dateTime Date with the format 'Y-m-d H:i:s', or a UNIX timestamp. + * @param string $dateTime Date with the format 'Y-m-d H:i:s', or a UNIX timestamp. * If the datetime is older than one day (default value for tracking_requests_require_authentication_when_custom_timestamp_newer_than), then you must call setTokenAuth() with a valid Admin/Super user token. * @return $this */ @@ -1581,7 +1593,7 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal $options[CURLOPT_COOKIE] = http_build_query($this->outgoingTrackerCookies); $this->outgoingTrackerCookies = array(); } - + $ch = curl_init(); curl_setopt_array($ch, $options); ob_start(); @@ -1592,7 +1604,7 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal if (!empty($response)) { list($header, $content) = explode("\r\n\r\n", $response, $limitCount = 2); } - + $this->parseIncomingCookies(explode("\r\n", $header)); } elseif (function_exists('stream_context_create')) { @@ -1619,11 +1631,11 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal $stream_options['http']['header'] .= 'Cookie: ' . http_build_query($this->outgoingTrackerCookies) . "\r\n"; $this->outgoingTrackerCookies = array(); } - + $ctx = stream_context_create($stream_options); $response = file_get_contents($url, 0, $ctx); $content = $response; - + $this->parseIncomingCookies($http_response_header); } @@ -1646,21 +1658,29 @@ protected function getTimestamp() */ protected function getBaseUrl() { - if (empty(self::$URL)) { + if (empty($this->trackerUrl) && !empty(self::$URL)) { + // for BC + $this->trackerUrl = self::$URL; + } + + // for BC + self::$URL = $this->trackerUrl; + + if (empty($this->trackerUrl)) { throw new Exception( 'You must first set the Piwik Tracker URL by calling PiwikTracker::$URL = \'http://your-website.org/piwik/\';' ); } - if (strpos(self::$URL, '/piwik.php') === false - && strpos(self::$URL, '/proxy-piwik.php') === false - && strpos(self::$URL, '/matomo.php') === false - && strpos(self::$URL, '/proxy-matomo.php') === false + if (strpos($this->trackerUrl, '/piwik.php') === false + && strpos($this->trackerUrl, '/proxy-piwik.php') === false + && strpos($this->trackerUrl, '/matomo.php') === false + && strpos($this->trackerUrl, '/proxy-matomo.php') === false ) { - self::$URL .= '/piwik.php'; + $this->trackerUrl .= '/piwik.php'; } - return self::$URL; + return $this->trackerUrl; } /** @@ -1883,9 +1903,9 @@ protected static function getCurrentQueryString() protected static function getCurrentUrl() { return self::getCurrentScheme() . '://' - . self::getCurrentHost() - . self::getCurrentScriptName() - . self::getCurrentQueryString(); + . self::getCurrentHost() + . self::getCurrentScriptName() + . self::getCurrentQueryString(); } /** @@ -1976,7 +1996,7 @@ public function setOutgoingTrackerCookie($name, $value) $this->outgoingTrackerCookies[$name] = $value; } } - + /** * Gets a cookie which was set by the tracking server. * @@ -1989,7 +2009,7 @@ public function getIncomingTrackerCookie($name) if (isset($this->incomingTrackerCookies[$name])) { return $this->incomingTrackerCookies[$name]; } - + return false; } @@ -2001,11 +2021,11 @@ public function getIncomingTrackerCookie($name) protected function parseIncomingCookies($headers) { $this->incomingTrackerCookies = array(); - + if (!empty($headers)) { $headerName = 'set-cookie:'; $headerNameLength = strlen($headerName); - + foreach($headers as $header) { if (strpos(strtolower($header), $headerName) !== 0) { continue; @@ -2017,7 +2037,7 @@ protected function parseIncomingCookies($headers) } parse_str($cookies, $this->incomingTrackerCookies); } - } + } } }