/** * 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 = Zend_Registry::get('config')->General->feedback_email_address; try { $minimumBodyLength = 35; if (strlen($body) < $minimumBodyLength) { 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_Common::getIpString() . "\n" . 'URL: ' . Piwik_Url::getReferer() . "\n"); @$mail->send(); } catch (Exception $e) { $view->ErrorString = $e->getMessage(); $view->message = $body; } echo $view->render(); }
function index() { $view = Piwik_View::factory('SitesManager'); $sites = Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess(); 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_Common::getIpString(); $this->setBasicVariablesView($view); $view->menu = Piwik_GetAdminMenu(); echo $view->render(); }
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_Common::getIpString(); } $this->ipString = Piwik_Common::getIp($ipString); }
/** * Sends http request ensuring the request will fail before $timeout seconds * * If no $destinationPath is specified, the trimmed response (without header) is returned as a string. * If a $destinationPath is specified, the response (without header) is saved to a file. * * @param string $aUrl * @param int $timeout * @param string $userAgent * @param string $destinationPath * @param int $followDepth * @return true (or string) on success; false on HTTP response error code (1xx or 4xx); throws exception on all other errors */ public static function sendHttpRequest($aUrl, $timeout, $userAgent = null, $destinationPath = null, $followDepth = 0) { if ($followDepth > 3) { throw new Exception('Too many redirects (' . $followDepth . ')'); } $file = null; if ($destinationPath) { if (($file = @fopen($destinationPath, 'wb')) === false) { throw new Exception('Error while creating the file: ' . $destinationPath); } } // 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; // connection attempt if (($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout)) === false) { if (is_resource($file)) { @fclose($file); } throw new Exception("Error while connecting to: {$host}. Please try again later."); } // send HTTP request header fwrite($fsock, "GET {$path} HTTP/1.0\r\n" . "Host: {$host}" . ($port != 80 ? ':' . $port : '') . "\r\n" . "User-Agent: Piwik/" . Piwik_Version::VERSION . ($userAgent ? " {$userAgent}" : '') . "\r\n" . 'Referer: http://' . Piwik_Common::getIpString() . "/\r\n" . "Connection: close\r\n" . "\r\n"); $streamMetaData = array('timed_out' => false); @stream_set_blocking($fsock, true); @stream_set_timeout($fsock, $timeout); // process header $status = null; $expectRedirect = false; $contentLength = 0; $fileLength = 0; 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($s); return false; } continue; } // handle redirect if (preg_match('/^Location:\\s*(.+)/', rtrim($line, "\r\n"), $m)) { if (is_resource($file)) { @fclose($file); } @fclose($s); // Successful 2xx vs Redirect 3xx if ($status < 300) { throw new Exception('Unexpected redirect to Location: ' . rtrim($line) . ' for status code ' . $status); } return self::sendHttpRequest(trim($m[1]), $pathDestination, $tries + 1); } // 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 = 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'); } if (is_resource($file)) { // save to file $fileLength += fwrite($file, $line); } else { // concatenate to response string $response .= $line; } } // determine success or failure @fclose(@$fsock); if (is_resource($file)) { @fclose($file); if ($contentLength && $fileLength != $contentLength) { throw new Exception('File size error: ' . $destinationPath . '; expected ' . $contentLength . ' bytes; received ' . $fileLength . ' bytes'); } return true; } if ($contentLength && strlen($response) != $contentLength) { throw new Exception('Content length error: expected ' . $contentLength . ' bytes; received ' . $fileLength . ' bytes'); } return trim($response); }
public static function sendHttpRequestBy($method = 'socket', $aUrl, $timeout, $userAgent = null, $file = null, $followDepth = 0) { if ($followDepth > 3) { throw new Exception('Too many redirects (' . $followDepth . ')'); } $contentLength = 0; 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; // connection attempt if (($fsock = @fsockopen($host, $port, $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 fwrite($fsock, "GET {$path} HTTP/1.0\r\n" . "Host: {$host}" . ($port != 80 ? ':' . $port : '') . "\r\n" . "User-Agent: Piwik/" . Piwik_Version::VERSION . ($userAgent ? " {$userAgent}" : '') . "\r\n" . 'Referer: http://' . Piwik_Common::getIpString() . "/\r\n" . "Connection: close\r\n" . "\r\n"); $streamMetaData = array('timed_out' => false); @stream_set_blocking($fsock, true); @stream_set_timeout($fsock, $timeout); // process header $status = null; $expectRedirect = false; $fileLength = 0; 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($s); return false; } continue; } // handle redirect if (preg_match('/^Location:\\s*(.+)/', rtrim($line, "\r\n"), $m)) { if (is_resource($file)) { @fclose($file); } @fclose($s); // Successful 2xx vs Redirect 3xx if ($status < 300) { throw new Exception('Unexpected redirect to Location: ' . rtrim($line) . ' for status code ' . $status); } return self::sendHttpRequest(trim($m[1]), $pathDestination, $tries + 1); } // 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'); } if (is_resource($file)) { // save to file $fileLength += fwrite($file, $line); } else { // concatenate to response string $response .= $line; } } // determine success or failure @fclose(@$fsock); } else { if ($method == 'stream') { $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: Piwik/' . Piwik_Version::VERSION . ($userAgent ? " {$userAgent}" : '') . "\r\n" . 'Referer: http://' . Piwik_Common::getIpString() . "/\r\n", 'max_redirects' => 3, 'timeout' => $timeout)); $ctx = stream_context_create($stream_options); } $response = @file_get_contents($aUrl, 0, $ctx); if (is_resource($file)) { // save to file fwrite($file, $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(); $curl_options = array(CURLOPT_URL => $aUrl, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $timeout, CURLOPT_BINARYTRANSFER => is_resource($file), CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 3, CURLOPT_USERAGENT => 'Piwik/' . Piwik_Version::VERSION . ($userAgent ? " {$userAgent}" : ''), CURLOPT_REFERER => 'http://' . Piwik_Common::getIpString()); @curl_setopt_array($ch, $curl_options); $response = @curl_exec($ch); if (is_resource($file)) { // save to file fwrite($file, $response); } @curl_close($ch); unset($ch); } else { throw new Exception('Invalid request method: ' . $method); } } } if (is_resource($file)) { fflush($file); @fclose($file); if ($contentLength && $fileLength != $contentLength) { throw new Exception('File size error: ' . $destinationPath . '; expected ' . $contentLength . ' bytes; received ' . $fileLength . ' bytes'); } return true; } if ($contentLength && strlen($response) != $contentLength) { throw new Exception('Content length error: expected ' . $contentLength . ' bytes; received ' . $fileLength . ' bytes'); } return trim($response); }
/** * Validate user (by username or email address). * * @param string $loginMail (user name or email address) * @param string $urlToRedirect (URL to redirect to, if successfully validated) * @return string (failure message if unable to validate) */ protected function lostPasswordFormValidated($loginMail, $urlToRedirect) { $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_Common::getIpString(); $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')); $mail->setBodyText(str_replace('\\n', "\n", sprintf(Piwik_Translate('Login_MailPasswordRecoveryBody'), $login, $ip, $url, $resetToken)) . "\n"); $piwikHost = $_SERVER['HTTP_HOST']; if (strlen($piwikHost) == 0) { $piwikHost = 'piwik.org'; } $fromEmailName = Zend_Registry::get('config')->General->login_password_recovery_email_name; $fromEmailAddress = Zend_Registry::get('config')->General->login_password_recovery_email_address; $fromEmailAddress = str_replace('{DOMAIN}', $piwikHost, $fromEmailAddress); $mail->setFrom($fromEmailAddress, $fromEmailName); @$mail->send(); } catch (Exception $e) { $view->ErrorString = $e->getMessage(); } $view->linkTitle = Piwik::getRandomTitle(); $view->urlToRedirect = $urlToRedirect; echo $view->render(); exit; }