Пример #1
0
 /**
  * Uses a GeoIP database to get a visitor's location based on their IP address.
  * 
  * This function will return different results based on the data used and based
  * on how the GeoIP module is configured.
  * 
  * If a region database is used, it may return the country code, region code,
  * city name, area code, latitude, longitude and postal code of the visitor.
  * 
  * Alternatively, only the country code may be returned for another database.
  * 
  * If your HTTP server is not configured to include all GeoIP information, some
  * information will not be available to Piwik.
  * 
  * @param array $info Must have an 'ip' field.
  * @return array
  */
 public function getLocation($info)
 {
     $ip = $this->getIpFromInfo($info);
     // geoip modules that are built into servers can't use a forced IP. in this case we try
     // to fallback to another version.
     $myIP = Piwik_IP::getIpFromHeader();
     if (!self::isSameOrAnonymizedIp($ip, $myIP) && (!isset($info['disable_fallbacks']) || !$info['disable_fallbacks'])) {
         printDebug("The request is for IP address: " . $info['ip'] . " but your IP is: {$myIP}. GeoIP Server Module (apache/nginx) does not support this use case... ");
         $fallbacks = array(Piwik_UserCountry_LocationProvider_GeoIp_Pecl::ID, Piwik_UserCountry_LocationProvider_GeoIp_Php::ID);
         foreach ($fallbacks as $fallbackProviderId) {
             $otherProvider = Piwik_UserCountry_LocationProvider::getProviderById($fallbackProviderId);
             if ($otherProvider) {
                 printDebug("Used {$fallbackProviderId} to detect this visitor IP");
                 return $otherProvider->getLocation($info);
             }
         }
         printDebug("FAILED to lookup the geo location of this IP address, as no fallback location providers is configured. We recommend to configure Geolocation PECL module to fix this error.");
         return false;
     }
     $result = array();
     foreach (self::$geoIpServerVars as $resultKey => $geoipVarName) {
         if (!empty($_SERVER[$geoipVarName])) {
             $result[$resultKey] = $_SERVER[$geoipVarName];
         }
     }
     foreach (self::$geoIpUtfServerVars as $resultKey => $geoipVarName) {
         if (!empty($_SERVER[$geoipVarName])) {
             $result[$resultKey] = utf8_encode($_SERVER[$geoipVarName]);
         }
     }
     $this->completeLocationResult($result);
     return $result;
 }
Пример #2
0
 function index()
 {
     $view = Piwik_View::factory('SitesManager');
     if (Piwik::isUserIsSuperUser()) {
         $sites = Piwik_SitesManager_API::getInstance()->getAllSites();
         Piwik_Site::setSites($sites);
         $sites = array_values($sites);
     } else {
         $sites = Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess();
         Piwik_Site::setSitesFromArray($sites);
     }
     foreach ($sites as &$site) {
         $site['alias_urls'] = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($site['idsite']);
         $site['excluded_ips'] = str_replace(',', '<br/>', $site['excluded_ips']);
         $site['excluded_parameters'] = str_replace(',', '<br/>', $site['excluded_parameters']);
     }
     $view->adminSites = $sites;
     $view->adminSitesCount = count($sites);
     $timezones = Piwik_SitesManager_API::getInstance()->getTimezonesList();
     $view->timezoneSupported = Piwik::isTimezoneSupportEnabled();
     $view->timezones = Piwik_Common::json_encode($timezones);
     $view->defaultTimezone = Piwik_SitesManager_API::getInstance()->getDefaultTimezone();
     $view->currencies = Piwik_Common::json_encode(Piwik_SitesManager_API::getInstance()->getCurrencyList());
     $view->defaultCurrency = Piwik_SitesManager_API::getInstance()->getDefaultCurrency();
     $view->utcTime = Piwik_Date::now()->getDatetime();
     $excludedIpsGlobal = Piwik_SitesManager_API::getInstance()->getExcludedIpsGlobal();
     $view->globalExcludedIps = str_replace(',', "\n", $excludedIpsGlobal);
     $excludedQueryParametersGlobal = Piwik_SitesManager_API::getInstance()->getExcludedQueryParametersGlobal();
     $view->globalExcludedQueryParameters = str_replace(',', "\n", $excludedQueryParametersGlobal);
     $view->currentIpAddress = Piwik_IP::getIpFromHeader();
     $view->showAddSite = (bool) Piwik_Common::getRequestVar('showaddsite', false);
     $this->setBasicVariablesView($view);
     $view->menu = Piwik_GetAdminMenu();
     echo $view->render();
 }
Пример #3
0
 /**
  * send email to Piwik team and display nice thanks
  */
 function sendFeedback()
 {
     $email = Piwik_Common::getRequestVar('email', '', 'string');
     $body = Piwik_Common::getRequestVar('body', '', 'string');
     $category = Piwik_Common::getRequestVar('category', '', 'string');
     $nonce = Piwik_Common::getRequestVar('nonce', '', 'string');
     $view = Piwik_View::factory('sent');
     $view->feedbackEmailAddress = Piwik_Config::getInstance()->General['feedback_email_address'];
     try {
         $minimumBodyLength = 40;
         if (strlen($body) < $minimumBodyLength || strpos($email, 'probe@') !== false || strpos($body, '&lt;probe') !== false) {
             throw new Exception(Piwik_TranslateException('Feedback_ExceptionBodyLength', array($minimumBodyLength)));
         }
         if (!Piwik::isValidEmailString($email)) {
             throw new Exception(Piwik_TranslateException('UsersManager_ExceptionInvalidEmail'));
         }
         if (preg_match('/https?:/i', $body)) {
             throw new Exception(Piwik_TranslateException('Feedback_ExceptionNoUrls'));
         }
         if (!Piwik_Nonce::verifyNonce('Piwik_Feedback.sendFeedback', $nonce)) {
             throw new Exception(Piwik_TranslateException('General_ExceptionNonceMismatch'));
         }
         Piwik_Nonce::discardNonce('Piwik_Feedback.sendFeedback');
         $mail = new Piwik_Mail();
         $mail->setFrom(Piwik_Common::unsanitizeInputValue($email));
         $mail->addTo($view->feedbackEmailAddress, 'Piwik Team');
         $mail->setSubject('[ Feedback form - Piwik ] ' . $category);
         $mail->setBodyText(Piwik_Common::unsanitizeInputValue($body) . "\n" . 'Piwik ' . Piwik_Version::VERSION . "\n" . 'IP: ' . Piwik_IP::getIpFromHeader() . "\n" . 'URL: ' . Piwik_Url::getReferer() . "\n");
         @$mail->send();
     } catch (Exception $e) {
         $view->ErrorString = $e->getMessage();
         $view->message = $body;
     }
     echo $view->render();
 }
Пример #4
0
 function index()
 {
     Piwik::checkUserIsNotAnonymous();
     $view = Piwik_View::factory('Settings');
     $view->isSuperUser = Piwik::isUserIsSuperUser();
     $mobileMessagingAPI = Piwik_MobileMessaging_API::getInstance();
     $view->delegatedManagement = $mobileMessagingAPI->getDelegatedManagement();
     $view->credentialSupplied = $mobileMessagingAPI->areSMSAPICredentialProvided();
     $view->accountManagedByCurrentUser = $view->isSuperUser || $view->delegatedManagement;
     $view->strHelpAddPhone = Piwik_Translate('MobileMessaging_Settings_PhoneNumbers_HelpAdd', array(Piwik_Translate('UserSettings_SubmenuSettings'), Piwik_Translate('MobileMessaging_SettingsMenu')));
     if ($view->credentialSupplied && $view->accountManagedByCurrentUser) {
         $view->provider = $mobileMessagingAPI->getSMSProvider();
         $view->creditLeft = $mobileMessagingAPI->getCreditLeft();
     }
     $view->smsProviders = Piwik_MobileMessaging_SMSProvider::$availableSMSProviders;
     // construct the list of countries from the lang files
     $countries = array();
     foreach (Piwik_Common::getCountriesList() as $countryCode => $continentCode) {
         if (isset(Piwik_MobileMessaging_CountryCallingCodes::$countryCallingCodes[$countryCode])) {
             $countries[$countryCode] = array('countryName' => Piwik_CountryTranslate($countryCode), 'countryCallingCode' => Piwik_MobileMessaging_CountryCallingCodes::$countryCallingCodes[$countryCode]);
         }
     }
     $view->countries = $countries;
     $view->defaultCountry = Piwik_Common::getCountry(Piwik_LanguagesManager::getLanguageCodeForCurrentUser(), true, Piwik_IP::getIpFromHeader());
     $view->phoneNumbers = $mobileMessagingAPI->getPhoneNumbers();
     $this->setBasicVariablesView($view);
     $view->menu = Piwik_GetAdminMenu();
     echo $view->render();
 }
Пример #5
0
 function __construct()
 {
     $logToFileFilename = self::ID;
     $logToDatabaseTableName = self::ID;
     $logToDatabaseColumnMapping = null;
     $screenFormatter = new Piwik_Log_APICall_Formatter_ScreenFormatter();
     $fileFormatter = new Piwik_Log_Formatter_FileFormatter();
     parent::__construct($logToFileFilename, $fileFormatter, $screenFormatter, $logToDatabaseTableName, $logToDatabaseColumnMapping);
     $this->setEventItem('caller_ip', Piwik_IP::P2N(Piwik_IP::getIpFromHeader()));
 }
Пример #6
0
 /**
  * Constructor
  */
 function __construct()
 {
     $logToFileFilename = self::ID;
     $logToDatabaseTableName = self::ID;
     $logToDatabaseColumnMapping = array('class_name' => 'class_name', 'method_name' => 'method_name', 'parameter_names_default_values' => 'parameter_names_default_values', 'parameter_values' => 'parameter_values', 'execution_time' => 'execution_time', 'caller_ip' => 'caller_ip', 'timestamp' => 'timestamp', 'returned_value' => 'returned_value');
     $screenFormatter = new Piwik_Log_APICall_Formatter_ScreenFormatter();
     $fileFormatter = new Piwik_Log_Formatter_FileFormatter();
     parent::__construct($logToFileFilename, $fileFormatter, $screenFormatter, $logToDatabaseTableName, $logToDatabaseColumnMapping);
     $this->setEventItem('caller_ip', Piwik_IP::P2N(Piwik_IP::getIpFromHeader()));
 }
Пример #7
0
 public function __construct($forcedIpString = null, $forcedDateTime = null)
 {
     $this->timestamp = time();
     if (!empty($forcedDateTime)) {
         if (!is_numeric($forcedDateTime)) {
             $forcedDateTime = strtotime($forcedDateTime);
         }
         $this->timestamp = $forcedDateTime;
     }
     $ipString = $forcedIpString;
     if (empty($ipString)) {
         $ipString = Piwik_IP::getIpFromHeader();
     }
     $ip = Piwik_IP::P2N($ipString);
     $this->ip = $ip;
 }
Пример #8
0
	public function __construct($forcedIpString = null, $forcedDateTime = null)
	{
		$this->timestamp = time();
		if(!empty($forcedDateTime))
		{
			if(!is_int($forcedDateTime))
			{
				$forcedDateTime = strtotime($forcedDateTime);
			}
			$this->timestamp = $forcedDateTime;
		}
		$ipString = $forcedIpString;
		if(empty($ipString))
		{
			$ipString = Piwik_IP::getIpFromHeader();
		}

		$ip = Piwik_IP::P2N($ipString);
		Piwik_PostEvent('Tracker.Visit.setVisitorIp', $ip);
		$this->ip = $ip;
	}
Пример #9
0
	function index()
	{
		$view = Piwik_View::factory('SitesManager');
		$sites = Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess();
		$sitesIndexedById = array();
		foreach($sites as $site)
		{
			$sitesIndexedById[$site['idsite']] = $site;
		}
		Piwik_Site::setSites($sitesIndexedById);
		foreach($sites as &$site)
		{
			$site['alias_urls'] = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($site['idsite']);
			$site['excluded_ips'] = str_replace(',','<br/>', $site['excluded_ips']);
			$site['excluded_parameters'] = str_replace(',','<br/>', $site['excluded_parameters']);
		}
		$view->adminSites = $sites;
		$view->adminSitesCount = count($sites);

		$timezones = Piwik_SitesManager_API::getInstance()->getTimezonesList();
		$view->timezoneSupported = Piwik::isTimezoneSupportEnabled();
		$view->timezones = json_encode($timezones);
		$view->defaultTimezone = Piwik_SitesManager_API::getInstance()->getDefaultTimezone();

		$view->currencies = json_encode(Piwik_SitesManager_API::getInstance()->getCurrencyList());
		$view->defaultCurrency = Piwik_SitesManager_API::getInstance()->getDefaultCurrency();

		$view->utcTime = Piwik_Date::now()->getDatetime();
		$excludedIpsGlobal = Piwik_SitesManager_API::getInstance()->getExcludedIpsGlobal();
		$view->globalExcludedIps = str_replace(',',"\n", $excludedIpsGlobal);
		$excludedQueryParametersGlobal = Piwik_SitesManager_API::getInstance()->getExcludedQueryParametersGlobal();
		$view->globalExcludedQueryParameters = str_replace(',',"\n", $excludedQueryParametersGlobal);
		$view->currentIpAddress = Piwik_IP::getIpFromHeader();

		$this->setBasicVariablesView($view);
		$view->menu = Piwik_GetAdminMenu();
		echo $view->render();
	}
Пример #10
0
 /**
  * Sends http request using the specified transport method
  *
  * @param string       $method
  * @param string       $aUrl
  * @param int          $timeout
  * @param string       $userAgent
  * @param string       $destinationPath
  * @param resource     $file
  * @param int          $followDepth
  * @param bool|string  $acceptLanguage               Accept-language header
  * @param bool         $acceptInvalidSslCertificate  Only used with $method == 'curl'. If set to true (NOT recommended!) the SSL certificate will not be checked
  * @throws Exception
  * @return bool  true (or string) on success; false on HTTP response error code (1xx or 4xx)
  */
 public static function sendHttpRequestBy($method = 'socket', $aUrl, $timeout, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $acceptInvalidSslCertificate = false)
 {
     if ($followDepth > 5) {
         throw new Exception('Too many redirects (' . $followDepth . ')');
     }
     $contentLength = 0;
     $fileLength = 0;
     // Piwik services behave like a proxy, so we should act like one.
     $xff = 'X-Forwarded-For: ' . (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] . ',' : '') . Piwik_IP::getIpFromHeader();
     if (empty($userAgent)) {
         $userAgent = self::getUserAgent();
     }
     $via = 'Via: ' . (isset($_SERVER['HTTP_VIA']) && !empty($_SERVER['HTTP_VIA']) ? $_SERVER['HTTP_VIA'] . ', ' : '') . Piwik_Version::VERSION . ' ' . ($userAgent ? " ({$userAgent})" : '');
     // proxy configuration
     $proxyHost = Piwik_Config::getInstance()->proxy['host'];
     $proxyPort = Piwik_Config::getInstance()->proxy['port'];
     $proxyUser = Piwik_Config::getInstance()->proxy['username'];
     $proxyPassword = Piwik_Config::getInstance()->proxy['password'];
     if ($method == 'socket') {
         // initialization
         $url = @parse_url($aUrl);
         if ($url === false || !isset($url['scheme'])) {
             throw new Exception('Malformed URL: ' . $aUrl);
         }
         if ($url['scheme'] != 'http') {
             throw new Exception('Invalid protocol/scheme: ' . $url['scheme']);
         }
         $host = $url['host'];
         $port = isset($url['port)']) ? $url['port'] : 80;
         $path = isset($url['path']) ? $url['path'] : '/';
         if (isset($url['query'])) {
             $path .= '?' . $url['query'];
         }
         $errno = null;
         $errstr = null;
         $proxyAuth = null;
         if (!empty($proxyHost) && !empty($proxyPort)) {
             $connectHost = $proxyHost;
             $connectPort = $proxyPort;
             if (!empty($proxyUser) && !empty($proxyPassword)) {
                 $proxyAuth = 'Proxy-Authorization: Basic ' . base64_encode("{$proxyUser}:{$proxyPassword}") . "\r\n";
             }
             $requestHeader = "GET {$aUrl} HTTP/1.1\r\n";
         } else {
             $connectHost = $host;
             $connectPort = $port;
             $requestHeader = "GET {$path} HTTP/1.0\r\n";
         }
         // connection attempt
         if (($fsock = @fsockopen($connectHost, $connectPort, $errno, $errstr, $timeout)) === false || !is_resource($fsock)) {
             if (is_resource($file)) {
                 @fclose($file);
             }
             throw new Exception("Error while connecting to: {$host}. Please try again later. {$errstr}");
         }
         // send HTTP request header
         $requestHeader .= "Host: {$host}" . ($port != 80 ? ':' . $port : '') . "\r\n" . ($proxyAuth ? $proxyAuth : '') . 'User-Agent: ' . $userAgent . "\r\n" . ($acceptLanguage ? $acceptLanguage . "\r\n" : '') . $xff . "\r\n" . $via . "\r\n" . "Connection: close\r\n" . "\r\n";
         fwrite($fsock, $requestHeader);
         $streamMetaData = array('timed_out' => false);
         @stream_set_blocking($fsock, true);
         if (function_exists('stream_set_timeout')) {
             @stream_set_timeout($fsock, $timeout);
         } elseif (function_exists('socket_set_timeout')) {
             @socket_set_timeout($fsock, $timeout);
         }
         // process header
         $status = null;
         $expectRedirect = false;
         while (!feof($fsock)) {
             $line = fgets($fsock, 4096);
             $streamMetaData = @stream_get_meta_data($fsock);
             if ($streamMetaData['timed_out']) {
                 if (is_resource($file)) {
                     @fclose($file);
                 }
                 @fclose($fsock);
                 throw new Exception('Timed out waiting for server response');
             }
             // a blank line marks the end of the server response header
             if (rtrim($line, "\r\n") == '') {
                 break;
             }
             // parse first line of server response header
             if (!$status) {
                 // expect first line to be HTTP response status line, e.g., HTTP/1.1 200 OK
                 if (!preg_match('~^HTTP/(\\d\\.\\d)\\s+(\\d+)(\\s*.*)?~', $line, $m)) {
                     if (is_resource($file)) {
                         @fclose($file);
                     }
                     @fclose($fsock);
                     throw new Exception('Expected server response code.  Got ' . rtrim($line, "\r\n"));
                 }
                 $status = (int) $m[2];
                 // Informational 1xx or Client Error 4xx
                 if ($status < 200 || $status >= 400) {
                     if (is_resource($file)) {
                         @fclose($file);
                     }
                     @fclose($fsock);
                     return false;
                 }
                 continue;
             }
             // handle redirect
             if (preg_match('/^Location:\\s*(.+)/', rtrim($line, "\r\n"), $m)) {
                 if (is_resource($file)) {
                     @fclose($file);
                 }
                 @fclose($fsock);
                 // Successful 2xx vs Redirect 3xx
                 if ($status < 300) {
                     throw new Exception('Unexpected redirect to Location: ' . rtrim($line) . ' for status code ' . $status);
                 }
                 return self::sendHttpRequestBy($method, trim($m[1]), $timeout, $userAgent, $destinationPath, $file, $followDepth + 1, $acceptLanguage);
             }
             // save expected content length for later verification
             if (preg_match('/^Content-Length:\\s*(\\d+)/', $line, $m)) {
                 $contentLength = (int) $m[1];
             }
         }
         if (feof($fsock)) {
             throw new Exception('Unexpected end of transmission');
         }
         // process content/body
         $response = '';
         while (!feof($fsock)) {
             $line = fread($fsock, 8192);
             $streamMetaData = @stream_get_meta_data($fsock);
             if ($streamMetaData['timed_out']) {
                 if (is_resource($file)) {
                     @fclose($file);
                 }
                 @fclose($fsock);
                 throw new Exception('Timed out waiting for server response');
             }
             $fileLength += Piwik_Common::strlen($line);
             if (is_resource($file)) {
                 // save to file
                 fwrite($file, $line);
             } else {
                 // concatenate to response string
                 $response .= $line;
             }
         }
         // determine success or failure
         @fclose(@$fsock);
     } else {
         if ($method == 'fopen') {
             $response = false;
             // we make sure the request takes less than a few seconds to fail
             // we create a stream_context (works in php >= 5.2.1)
             // we also set the socket_timeout (for php < 5.2.1)
             $default_socket_timeout = @ini_get('default_socket_timeout');
             @ini_set('default_socket_timeout', $timeout);
             $ctx = null;
             if (function_exists('stream_context_create')) {
                 $stream_options = array('http' => array('header' => 'User-Agent: ' . $userAgent . "\r\n" . ($acceptLanguage ? $acceptLanguage . "\r\n" : '') . $xff . "\r\n" . $via . "\r\n", 'max_redirects' => 5, 'timeout' => $timeout));
                 if (!empty($proxyHost) && !empty($proxyPort)) {
                     $stream_options['http']['proxy'] = 'tcp://' . $proxyHost . ':' . $proxyPort;
                     $stream_options['http']['request_fulluri'] = true;
                     // required by squid proxy
                     if (!empty($proxyUser) && !empty($proxyPassword)) {
                         $stream_options['http']['header'] .= 'Proxy-Authorization: Basic ' . base64_encode("{$proxyUser}:{$proxyPassword}") . "\r\n";
                     }
                 }
                 $ctx = stream_context_create($stream_options);
             }
             // save to file
             if (is_resource($file)) {
                 $handle = fopen($aUrl, 'rb', false, $ctx);
                 while (!feof($handle)) {
                     $response = fread($handle, 8192);
                     $fileLength += Piwik_Common::strlen($response);
                     fwrite($file, $response);
                 }
                 fclose($handle);
             } else {
                 $response = @file_get_contents($aUrl, 0, $ctx);
                 $fileLength = Piwik_Common::strlen($response);
             }
             // restore the socket_timeout value
             if (!empty($default_socket_timeout)) {
                 @ini_set('default_socket_timeout', $default_socket_timeout);
             }
         } else {
             if ($method == 'curl') {
                 $ch = @curl_init();
                 if (!empty($proxyHost) && !empty($proxyPort)) {
                     @curl_setopt($ch, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort);
                     if (!empty($proxyUser) && !empty($proxyPassword)) {
                         // PROXYAUTH defaults to BASIC
                         @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyUser . ':' . $proxyPassword);
                     }
                 }
                 $curl_options = array(CURLOPT_BINARYTRANSFER => is_resource($file), CURLOPT_URL => $aUrl, CURLOPT_USERAGENT => $userAgent, CURLOPT_HTTPHEADER => array($xff, $via, $acceptLanguage), CURLOPT_HEADER => false, CURLOPT_CONNECTTIMEOUT => $timeout);
                 // Case archive.php is triggering archiving on https:// and the certificate is not valid
                 if ($acceptInvalidSslCertificate) {
                     $curl_options += array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false);
                 }
                 @curl_setopt_array($ch, $curl_options);
                 self::configCurlCertificate($ch);
                 /*
                  * as of php 5.2.0, CURLOPT_FOLLOWLOCATION can't be set if
                  * in safe_mode or open_basedir is set
                  */
                 if ((string) ini_get('safe_mode') == '' && ini_get('open_basedir') == '') {
                     $curl_options = array(CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 5);
                     @curl_setopt_array($ch, $curl_options);
                 }
                 if (is_resource($file)) {
                     // write output directly to file
                     @curl_setopt($ch, CURLOPT_FILE, $file);
                 } else {
                     // internal to ext/curl
                     @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 }
                 ob_start();
                 $response = @curl_exec($ch);
                 ob_end_clean();
                 if ($response === true) {
                     $response = '';
                 } else {
                     if ($response === false) {
                         $errstr = curl_error($ch);
                         if ($errstr != '') {
                             throw new Exception('curl_exec: ' . $errstr);
                         }
                         $response = '';
                     }
                 }
                 $contentLength = @curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
                 $fileLength = is_resource($file) ? @curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD) : Piwik_Common::strlen($response);
                 @curl_close($ch);
                 unset($ch);
             } else {
                 throw new Exception('Invalid request method: ' . $method);
             }
         }
     }
     if (is_resource($file)) {
         fflush($file);
         @fclose($file);
         $fileSize = filesize($destinationPath);
         if ($contentLength > 0 && $fileLength != $contentLength || $fileSize != $fileLength) {
             throw new Exception('File size error: ' . $destinationPath . '; expected ' . $contentLength . ' bytes; received ' . $fileLength . ' bytes; saved ' . $fileSize . ' bytes to file');
         }
         return true;
     }
     if ($contentLength > 0 && $fileLength != $contentLength) {
         throw new Exception('Content length error: expected ' . $contentLength . ' bytes; received ' . $fileLength . ' bytes');
     }
     return trim($response);
 }
Пример #11
0
 /**
  * Echo's a pretty formatted location using a specific LocationProvider.
  * 
  * Input:
  *   The 'id' query parameter must be set to the ID of the LocationProvider to use.
  * 
  * Output:
  *   The pretty formatted location that was obtained. Will be HTML.
  */
 public function getLocationUsingProvider()
 {
     $providerId = Piwik_Common::getRequestVar('id');
     $provider = $provider = Piwik_UserCountry_LocationProvider::getProviderById($providerId);
     if ($provider === false) {
         throw new Exception("Invalid provider ID: '{$providerId}'.");
     }
     $location = $provider->getLocation(array('ip' => Piwik_IP::getIpFromHeader(), 'lang' => Piwik_Common::getBrowserLanguage(), 'disable_fallbacks' => true));
     $location = Piwik_UserCountry_LocationProvider::prettyFormatLocation($location, $newline = '<br/>', $includeExtra = true);
     echo $location;
 }
Пример #12
0
 /**
  * @dataProvider getIpFromHeaderTestData
  * @group Core
  * @group IP
  */
 public function testGetIpFromHeader($description, $test)
 {
     Piwik::createConfigObject();
     Piwik_Config::getInstance()->setTestEnvironment();
     $_SERVER['REMOTE_ADDR'] = $test[0];
     $_SERVER['HTTP_X_FORWARDED_FOR'] = $test[1];
     Piwik_Config::getInstance()->General['proxy_client_headers'] = array($test[2]);
     Piwik_Config::getInstance()->General['proxy_ips'] = array($test[3]);
     $this->assertEquals($test[4], Piwik_IP::getIpFromHeader(), $description);
 }
Пример #13
0
 /**
  * Sends email confirmation link for a password reset request.
  * 
  * @param array $user User info for the requested password reset.
  */
 private function sendEmailConfirmationLink($user)
 {
     $login = $user['login'];
     $email = $user['email'];
     // construct a password reset token from user information
     $resetToken = self::generatePasswordResetToken($user);
     $ip = Piwik_IP::getIpFromHeader();
     $url = Piwik_Url::getCurrentUrlWithoutQueryString() . "?module=Login&action=confirmResetPassword&login="******"&resetToken=" . urlencode($resetToken);
     // send email with new password
     $mail = new Piwik_Mail();
     $mail->addTo($email, $login);
     $mail->setSubject(Piwik_Translate('Login_MailTopicPasswordChange'));
     $bodyText = str_replace('\\n', "\n", sprintf(Piwik_Translate('Login_MailPasswordChangeBody'), $login, $ip, $url)) . "\n";
     $mail->setBodyText($bodyText);
     $fromEmailName = Piwik_Config::getInstance()->General['login_password_recovery_email_name'];
     $fromEmailAddress = Piwik_Config::getInstance()->General['login_password_recovery_email_address'];
     $mail->setFrom($fromEmailAddress, $fromEmailName);
     @$mail->send();
 }
Пример #14
0
 function test_getIpFromHeader()
 {
     Piwik::createConfigObject();
     Piwik_Config::getInstance()->setTestEnvironment();
     $saved = $this->saveGlobals(array('REMOTE_ADDR', 'HTTP_X_FORWARDED_FOR'));
     $tests = array('localhost inside LAN' => array('127.0.0.1', '', null, null, '127.0.0.1'), 'outside LAN, no proxy' => array('128.252.135.4', '', null, null, '128.252.135.4'), 'outside LAN, no (trusted) proxy' => array('128.252.135.4', '137.18.2.13, 128.252.135.4', '', null, '128.252.135.4'), 'outside LAN, one trusted proxy' => array('192.168.1.10', '137.18.2.13, 128.252.135.4, 192.168.1.10', 'HTTP_X_FORWARDED_FOR', null, '128.252.135.4'), 'outside LAN, proxy' => array('192.168.1.10', '128.252.135.4, 192.168.1.10', 'HTTP_X_FORWARDED_FOR', null, '128.252.135.4'), 'outside LAN, misconfigured proxy' => array('192.168.1.10', '128.252.135.4, 192.168.1.10, 192.168.1.10', 'HTTP_X_FORWARDED_FOR', null, '128.252.135.4'), 'outside LAN, multiple proxies' => array('192.168.1.10', '128.252.135.4, 192.168.1.20, 192.168.1.10', 'HTTP_X_FORWARDED_FOR', '192.168.1.*', '128.252.135.4'), 'outside LAN, multiple proxies' => array('[::ffff:7f00:10]', '128.252.135.4, [::ffff:7f00:20], [::ffff:7f00:10]', 'HTTP_X_FORWARDED_FOR', '::ffff:7f00:0/120', '128.252.135.4'));
     foreach ($tests as $description => $test) {
         $_SERVER['REMOTE_ADDR'] = $test[0];
         $_SERVER['HTTP_X_FORWARDED_FOR'] = $test[1];
         Piwik_Config::getInstance()->General['proxy_client_headers'] = array($test[2]);
         Piwik_Config::getInstance()->General['proxy_ips'] = array($test[3]);
         $this->assertEqual(Piwik_IP::getIpFromHeader(), $test[4], $description);
     }
     $this->restoreGlobals($saved);
 }
Пример #15
0
 /**
  * Validate user (by username or email address).
  *
  * @param string $loginMail user name or email address
  * @return string failure message if unable to validate
  */
 protected function lostPasswordFormValidated($loginMail)
 {
     if ($loginMail === 'anonymous') {
         return Piwik_Translate('Login_InvalidUsernameEmail');
     }
     $user = self::getUserInformation($loginMail);
     if ($user === null) {
         return Piwik_Translate('Login_InvalidUsernameEmail');
     }
     $view = Piwik_View::factory('passwordsent');
     $login = $user['login'];
     $email = $user['email'];
     // construct a password reset token from user information
     $resetToken = self::generatePasswordResetToken($user);
     $ip = Piwik_IP::getIpFromHeader();
     $url = Piwik_Url::getCurrentUrlWithoutQueryString() . "?module=Login&action=resetPassword&token={$resetToken}";
     // send email with new password
     try {
         $mail = new Piwik_Mail();
         $mail->addTo($email, $login);
         $mail->setSubject(Piwik_Translate('Login_MailTopicPasswordRecovery'));
         $bodyText = str_replace('\\n', "\n", sprintf(Piwik_Translate('Login_MailPasswordRecoveryBody'), $login, $ip, $url, $resetToken)) . "\n";
         $mail->setBodyText($bodyText);
         $fromEmailName = Piwik_Config::getInstance()->General['login_password_recovery_email_name'];
         $fromEmailAddress = Piwik_Config::getInstance()->General['login_password_recovery_email_address'];
         $mail->setFrom($fromEmailAddress, $fromEmailName);
         @$mail->send();
     } catch (Exception $e) {
         $view->ErrorString = $e->getMessage();
     }
     $this->configureView($view);
     echo $view->render();
     exit;
 }
Пример #16
0
 /**
  * Returns an array mapping provider IDs w/ information about the provider,
  * for each location provider.
  * 
  * The following information is provided for each provider:
  *   'id' - The provider's unique string ID.
  *   'title' - The provider's title.
  *   'description' - A description of how the location provider works.
  *   'status' - Either self::NOT_INSTALLED, self::INSTALLED or self::BROKEN.
  *   'statusMessage' - If the status is self::BROKEN, then the message describes why.
  *   'location' - A pretty formatted location of the current IP address
  *                (Piwik_IP::getIpFromHeader()).
  * 
  * An example result:
  * array(
  *     'geoip_php' => array('id' => 'geoip_php',
  *                          'title' => '...',
  *                          'desc' => '...',
  *                          'status' => Piwik_UserCountry_LocationProvider_GeoIp::BROKEN,
  *                          'statusMessage' => '...',
  *                          'location' => '...')
  *     'geoip_serverbased' => array(...)
  * )
  * 
  * @param string $newline What to separate lines with in the pretty locations.
  * @param bool $includeExtra Whether to include ISP/Org info in formatted location.
  * @return array
  */
 public static function getAllProviderInfo($newline = "\n", $includeExtra = false)
 {
     $allInfo = array();
     foreach (self::getAllProviders() as $provider) {
         $info = $provider->getInfo();
         $status = self::INSTALLED;
         $location = false;
         $statusMessage = false;
         $availableOrMessage = $provider->isAvailable();
         if ($availableOrMessage !== true) {
             $status = self::NOT_INSTALLED;
             if (is_string($availableOrMessage)) {
                 $statusMessage = $availableOrMessage;
             }
         } else {
             $workingOrError = $provider->isWorking();
             if ($workingOrError === true) {
                 $locInfo = array('ip' => Piwik_IP::getIpFromHeader(), 'lang' => Piwik_Common::getBrowserLanguage(), 'disable_fallbacks' => true);
                 $location = $provider->getLocation($locInfo);
                 $location = self::prettyFormatLocation($location, $newline, $includeExtra);
             } else {
                 $status = self::BROKEN;
                 $statusMessage = $workingOrError;
             }
         }
         $info['status'] = $status;
         $info['statusMessage'] = $statusMessage;
         $info['location'] = $location;
         $allInfo[$info['order']] = $info;
     }
     ksort($allInfo);
     $result = array();
     foreach ($allInfo as $info) {
         $result[$info['id']] = $info;
     }
     return $result;
 }