Till Brehm
2015-11-10 b79c5bac527dc5f93f1fab6e1d11daf27135403b
interface/web/login/index.php
@@ -1,7 +1,7 @@
<?php
/*
Copyright (c) 2005, Till Brehm, projektfarm Gmbh
Copyright (c) 2005 - 2015, Till Brehm, ISPConfig UG
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
@@ -28,28 +28,18 @@
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//
require_once '../../lib/config.inc.php';
require_once '../../lib/app.inc.php';
class login_index {
   public $status = '';
   private $target = '';
   private $app;
   private $conf;
   public function render() {
      global $app, $conf;
      /* Redirect to page, if login form was NOT send */
      if(count($_POST) == 0) {
         if(isset($_SESSION['s']['user']) && is_array($_SESSION['s']['user']) && is_array($_SESSION['s']['module'])) {
            die('HEADER_REDIRECT:'.$_SESSION['s']['module']['startpage']);
         }
// Check if we have an active users ession and no login_as.
if($_SESSION['s']['user']['active'] == 1 && @$_POST['login_as'] != 1) {
   header('Location: /index.php');
   die();
      }
      $app->uses('tpl');
      $app->tpl->newTemplate('form.tpl.htm');
$app->tpl->newTemplate('main_login.tpl.htm');
$app->tpl->setInclude('content_tpl', 'templates/index.htm');
      $error = '';
@@ -70,16 +60,16 @@
         //** Check variables
         if(!preg_match("/^[\w\.\-\_\@]{1,128}$/", $_POST['username'])) $error = $app->lng('user_regex_error');
         if(!preg_match("/^.{1,64}$/i", $_POST['passwort'])) $error = $app->lng('pw_error_length');
   if(!preg_match("/^.{1,64}$/i", $_POST['password'])) $error = $app->lng('pw_error_length');
         //** iporting variables
   //** importing variables
         $ip    = ip2long($_SERVER['REMOTE_ADDR']);
         $username = $_POST['username'];
         $passwort = $_POST['passwort'];
   $password = $_POST['password'];
         $loginAs  = false;
         $time = time();
         if($username != '' && $passwort != '' && $error == '') {
   if($username != '' && $password != '' && $error == '') {
            /*
             *  Check, if there is a "login as" instead of a "normal" login
             */
@@ -107,7 +97,7 @@
                        
                        /* this is the reseller, that shall be re-logged in */
                        $sql = "SELECT * FROM sys_user WHERE USERNAME = ? and PASSWORT = ?";
                        $tmp = $app->db->queryOneRecord($sql, $username, $passwort);
                  $tmp = $app->db->queryOneRecord($sql, $username, $password);
                        $client_group_id = $app->functions->intval($tmp['default_group']);
                        $tmp_client = $app->db->queryOneRecord("SELECT client.client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = ?", $client_group_id);
                        
@@ -129,7 +119,7 @@
                  
                  /* this is the user the reseller wants to 'login as' */
                  $sql = "SELECT * FROM sys_user WHERE USERNAME = ? and PASSWORT = ?";
                  $tmp = $app->db->queryOneRecord($sql, $username, $passwort);
            $tmp = $app->db->queryOneRecord($sql, $username, $password);
                  $tmp_client = $app->db->queryOneRecord("SELECT client.client_id, client.parent_client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = ?", $tmp["default_group"]);
                  
                  if(!$tmp || $tmp_client["parent_client_id"] != $res_client["client_id"]) {
@@ -140,8 +130,8 @@
                  unset($tmp_client);
               }
               $loginAs = true;
            }
            else {
      } else {
               /* normal login */
               $loginAs = false;
            }
@@ -149,6 +139,7 @@
            //* Check if there are already wrong logins
            $sql = "SELECT * FROM `attempts_login` WHERE `ip`= ? AND  `login_time` > (NOW() - INTERVAL 1 MINUTE) LIMIT 1";
            $alreadyfailed = $app->db->queryOneRecord($sql, $ip);
            //* too many failedlogins
            if($alreadyfailed['times'] > 5) {
               $error = $app->lng('error_user_too_many_logins');
@@ -156,8 +147,9 @@
               if ($loginAs){
                  $sql = "SELECT * FROM sys_user WHERE USERNAME = ? and PASSWORT = ?";
                  $user = $app->db->queryOneRecord($sql, $username, $passwort);
            $user = $app->db->queryOneRecord($sql, $username, $password);
               } else {
                  if(stristr($username, '@')) {
                     //* mailuser login
                     $sql = "SELECT * FROM mail_user WHERE login = ? or email = ?";
@@ -167,7 +159,7 @@
                        $saved_password = stripslashes($mailuser['password']);
                        $salt = '$1$'.substr($saved_password, 3, 8).'$';
                        //* Check if mailuser password is correct
                        if(crypt(stripslashes($passwort), $salt) == $saved_password) {
                  if(crypt(stripslashes($password), $salt) == $saved_password) {
                           //* we build a fake user here which has access to the mailuser module only and userid 0
                           $user = array();
                           $user['userid'] = 0;
@@ -184,33 +176,27 @@
                           $user['default_group'] = $mailuser['sys_groupid'];
                        }
                     }
                  } else {
                     //* normal cp user login
                     $sql = "SELECT * FROM sys_user WHERE USERNAME = ?";
                     $user = $app->db->queryOneRecord($sql, $username);
                     if($user) {
                        $saved_password = stripslashes($user['passwort']);
                        if(substr($saved_password, 0, 3) == '$1$') {
                           //* The password is crypt-md5 encrypted
                           $salt = '$1$'.substr($saved_password, 3, 8).'$';
                           if(crypt(stripslashes($passwort), $salt) != $saved_password) {
                        if(crypt(stripslashes($password), $salt) != $saved_password) {
                              $user = false;
                           }
                        } elseif(substr($saved_password, 0, 3) == '$5$') {
                           //* The password is crypt-sha256 encrypted
                           $salt = '$5$'.substr($saved_password, 3, 16).'$';
                           if(crypt(stripslashes($passwort), $salt) != $saved_password) {
                        if(crypt(stripslashes($password), $salt) != $saved_password) {
                              $user = false;
                           }
                        } else {
                           //* The password is md5 encrypted
                           if(md5($passwort) != $saved_password) {
                     if(md5($password) != $saved_password) {
                              $user = false;
                           }
                        }
@@ -224,13 +210,14 @@
                  if($user['active'] == 1) {
                     // Maintenance mode - allow logins only when maintenance mode is off or if the user is admin
                     if(!$maintenance_mode || $user['typ'] == 'admin'){
                        // User login right, so attempts can be deleted
                        $sql = "DELETE FROM `attempts_login` WHERE `ip`=?";
                        $app->db->query($sql, $ip);
                        $user = $app->db->toLower($user);
                        if ($loginAs) $oldSession = $_SESSION['s'];
                        session_regenerate_id();
                  if (!$loginAs) session_regenerate_id(true);
                        $_SESSION = array();
                        if ($loginAs) $_SESSION['s_old'] = $oldSession; // keep the way back!
                        $_SESSION['s']['user'] = $user;
@@ -241,7 +228,6 @@
                        if(is_file($_SESSION['s']['user']['startmodule'].'/lib/module.conf.php')) {
                           include_once $_SESSION['s']['user']['startmodule'].'/lib/module.conf.php';
                           $menu_dir = ISPC_WEB_PATH.'/' . $_SESSION['s']['user']['startmodule'] . '/lib/menu.d';
                           if (is_dir($menu_dir)) {
                              if ($dh = opendir($menu_dir)) {
                                 //** Go through all files in the menu dir
@@ -254,7 +240,6 @@
                           }
                           $_SESSION['s']['module'] = $module;
                        }
                        // check if the user theme is valid
                        if($_SESSION['s']['user']['theme'] != 'default') {
                           $tmp_path = ISPC_THEMES_PATH."/".$_SESSION['s']['user']['theme'];
@@ -278,14 +263,18 @@
                        * We need LOGIN_REDIRECT instead of HEADER_REDIRECT to load the
                        * new theme, if the logged-in user has another
                        */
                        echo 'LOGIN_REDIRECT:'.$_SESSION['s']['module']['startpage'];
                  if ($loginAs){
                     echo 'LOGIN_REDIRECT:'.$_SESSION['s']['module']['startpage'];
                        exit;
                  } else {
                     header('Location: /index.php?phpsessid='.session_id());
                     die();
                  }
                     }
                  } else {
                     $error = $app->lng('error_user_blocked');
                  }
               } else {
                  if(!$alreadyfailed['times'] )
                  {
@@ -302,7 +291,6 @@
                  if($app->db->errorMessage != '') $error .= '<br />'.$app->db->errorMessage != '';
                  $app->plugin->raiseEvent('login_failed', $this);
                  //* Save failed login message to var
                  $authlog = 'Failed login for user \''. $username .'\' from '. long2ip($ip) .' at '. date('Y-m-d H:i:s');
                  $authlog_handle = fopen($conf['ispconfig_log_dir'].'/auth.log', 'a');
@@ -310,11 +298,9 @@
                  fclose($authlog_handle);
               }
            }
         } else {
            //* Username or password empty
            if($error == '') $error = $app->lng('error_user_password_empty');
            $app->plugin->raiseEvent('login_empty', $this);
         }
      }
@@ -344,15 +330,28 @@
      $app->tpl->setVar('login_button_txt', $app->lng('login_button_txt'));
      $app->tpl->setVar('session_timeout', $server_config_array['session_timeout']);
      $app->tpl->setVar('session_allow_endless', $server_config_array['session_allow_endless']);
      $app->tpl->setInclude('content_tpl', 'login/templates/index.htm');
//$app->tpl->setInclude('content_tpl', 'login/templates/index.htm');
$app->tpl->setVar('current_theme', isset($_SESSION['s']['theme']) ? $_SESSION['s']['theme'] : 'default');
//die(isset($_SESSION['s']['theme']) ? $_SESSION['s']['theme'] : 'default');
// Logo
$logo = $app->db->queryOneRecord("SELECT * FROM sys_ini WHERE sysini_id = 1");
if($logo['custom_logo'] != ''){
   $base64_logo_txt = $logo['custom_logo'];
} else {
   $base64_logo_txt = $logo['default_logo'];
}
$tmp_base64 = explode(',', $base64_logo_txt, 2);
$logo_dimensions = $app->functions->getimagesizefromstring(base64_decode($tmp_base64[1]));
$app->tpl->setVar('base64_logo_width', $logo_dimensions[0].'px');
$app->tpl->setVar('base64_logo_height', $logo_dimensions[1].'px');
$app->tpl->setVar('base64_logo_txt', $base64_logo_txt);
// Title
$app->tpl->setVar('company_name', $sys_config['company_name']. ' :: ');
      $app->tpl_defaults();
      $this->status = 'OK';
      return $app->tpl->grab();
   } // << end function
} // << end class
$app->tpl->pparse();
?>