Esempio n. 1
0
    /**
     * Checks if the current host is valid and sets variables on the given view, including:
     *
     * - **isValidHost** - true if host is valid, false if otherwise
     * - **invalidHostMessage** - message to display if host is invalid (only set if host is invalid)
     * - **invalidHost** - the invalid hostname (only set if host is invalid)
     * - **mailLinkStart** - the open tag of a link to email the Super User of this problem (only set
     *                       if host is invalid)
     *
     * @param View $view
     * @api
     */
    public static function setHostValidationVariablesView($view)
    {
        // check if host is valid
        $view->isValidHost = Url::isValidHost();
        if (!$view->isValidHost) {
            // invalid host, so display warning to user
            $validHosts = Url::getTrustedHostsFromConfig();
            $validHost = $validHosts[0];
            $invalidHost = Common::sanitizeInputValue($_SERVER['HTTP_HOST']);
            $emailSubject = rawurlencode(Piwik::translate('CoreHome_InjectedHostEmailSubject', $invalidHost));
            $emailBody = rawurlencode(Piwik::translate('CoreHome_InjectedHostEmailBody'));
            $superUserEmail = implode(',', Piwik::getAllSuperUserAccessEmailAddresses());
            $mailToUrl = "mailto:{$superUserEmail}?subject={$emailSubject}&body={$emailBody}";
            $mailLinkStart = "<a href=\"{$mailToUrl}\">";
            $invalidUrl = Url::getCurrentUrlWithoutQueryString($checkIfTrusted = false);
            $validUrl = Url::getCurrentScheme() . '://' . $validHost . Url::getCurrentScriptName();
            $invalidUrl = Common::sanitizeInputValue($invalidUrl);
            $validUrl = Common::sanitizeInputValue($validUrl);
            $changeTrustedHostsUrl = "index.php" . Url::getCurrentQueryStringWithParametersModified(array('module' => 'CoreAdminHome', 'action' => 'generalSettings')) . "#trustedHostsSection";
            $warningStart = Piwik::translate('CoreHome_InjectedHostWarningIntro', array('<strong>' . $invalidUrl . '</strong>', '<strong>' . $validUrl . '</strong>')) . ' <br/>';
            if (Piwik::hasUserSuperUserAccess()) {
                $view->invalidHostMessage = $warningStart . ' ' . Piwik::translate('CoreHome_InjectedHostSuperUserWarning', array("<a href=\"{$changeTrustedHostsUrl}\">", $invalidHost, '</a>', "<br/><a href=\"{$validUrl}\">", $validHost, '</a>'));
            } else {
                if (Piwik::isUserIsAnonymous()) {
                    $view->invalidHostMessage = $warningStart . ' ' . Piwik::translate('CoreHome_InjectedHostNonSuperUserWarning', array("<br/><a href=\"{$validUrl}\">", '</a>', '<span style="display:none">', '</span>'));
                } else {
                    $view->invalidHostMessage = $warningStart . ' ' . Piwik::translate('CoreHome_InjectedHostNonSuperUserWarning', array("<br/><a href=\"{$validUrl}\">", '</a>', $mailLinkStart, '</a>'));
                }
            }
            $view->invalidHostMessageHowToFix = '<p><b>How do I fix this problem and how do I login again?</b><br/> The Piwik Super User can manually edit the file piwik/config/config.ini.php
						and add the following lines: <pre>[General]' . "\n" . 'trusted_hosts[] = "' . $invalidHost . '"</pre>After making the change, you will be able to login again.</p>
						<p>You may also <i>disable this security feature (not recommended)</i>. To do so edit config/config.ini.php and add:
						<pre>[General]' . "\n" . 'enable_trusted_host_check=0</pre>';
            $view->invalidHost = $invalidHost;
            // for UserSettings warning
            $view->invalidHostMailLinkStart = $mailLinkStart;
        }
    }
Esempio n. 2
0
 /**
  * @dataProvider getValidHostData
  * @group Core
  */
 public function testIsValidHost($expected, $host, $trustedHosts, $description)
 {
     Config::getInstance()->General['enable_trusted_host_check'] = 1;
     Config::getInstance()->General['trusted_hosts'] = $trustedHosts;
     $this->assertEquals($expected, Url::isValidHost($host), $description);
 }
Esempio n. 3
0
 private function processPasswordChange($userLogin)
 {
     $alias = Common::getRequestVar('alias');
     $email = Common::getRequestVar('email');
     $newPassword = false;
     $password = Common::getRequestvar('password', false);
     $passwordBis = Common::getRequestvar('passwordBis', false);
     if (!empty($password) || !empty($passwordBis)) {
         if ($password != $passwordBis) {
             throw new Exception($this->translator->translate('Login_PasswordsDoNotMatch'));
         }
         $newPassword = $password;
     }
     // UI disables password change on invalid host, but check here anyway
     if (!Url::isValidHost() && $newPassword !== false) {
         throw new Exception("Cannot change password with untrusted hostname!");
     }
     APIUsersManager::getInstance()->updateUser($userLogin, $newPassword, $email, $alias);
     if ($newPassword !== false) {
         $newPassword = Common::unsanitizeInputValue($newPassword);
     }
     // logs the user in with the new password
     if ($newPassword !== false) {
         $sessionInitializer = new SessionInitializer();
         $auth = StaticContainer::get('Piwik\\Auth');
         $auth->setLogin($userLogin);
         $auth->setPassword($password);
         $sessionInitializer->initSession($auth, $rememberMe = false);
     }
 }
Esempio n. 4
0
 private function processPasswordChange($userLogin)
 {
     $alias = Common::getRequestVar('alias');
     $email = Common::getRequestVar('email');
     $newPassword = false;
     $password = Common::getRequestvar('password', false);
     $passwordBis = Common::getRequestvar('passwordBis', false);
     if (!empty($password) || !empty($passwordBis)) {
         if ($password != $passwordBis) {
             throw new Exception(Piwik::translate('Login_PasswordsDoNotMatch'));
         }
         $newPassword = $password;
     }
     // UI disables password change on invalid host, but check here anyway
     if (!Url::isValidHost() && $newPassword !== false) {
         throw new Exception("Cannot change password with untrusted hostname!");
     }
     if (Piwik::isUserIsSuperUser()) {
         $superUser = Config::getInstance()->superuser;
         $updatedSuperUser = false;
         if ($newPassword !== false) {
             $newPassword = Common::unsanitizeInputValue($newPassword);
             $md5PasswordSuperUser = md5($newPassword);
             $superUser['password'] = $md5PasswordSuperUser;
             $updatedSuperUser = true;
         }
         if ($superUser['email'] != $email) {
             $superUser['email'] = $email;
             $updatedSuperUser = true;
         }
         if ($updatedSuperUser) {
             Config::getInstance()->superuser = $superUser;
             Config::getInstance()->forceSave();
         }
     } else {
         APIUsersManager::getInstance()->updateUser($userLogin, $newPassword, $email, $alias);
         if ($newPassword !== false) {
             $newPassword = Common::unsanitizeInputValue($newPassword);
         }
     }
     // logs the user in with the new password
     if ($newPassword !== false) {
         \Piwik\Registry::get('auth')->initSession($userLogin, md5($newPassword), $rememberMe = false);
     }
 }