Froxlor/index.php

398 lines
14 KiB
PHP
Raw Normal View History

<?php
/**
2010-01-22 15:03:14 +00:00
* This file is part of the Froxlor project.
* Copyright (c) 2003-2009 the SysCP Team (see authors).
2010-01-22 15:03:14 +00:00
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
2010-01-22 15:03:14 +00:00
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
2010-01-22 15:03:14 +00:00
* @author Florian Lippert <flo@syscp.org> (2003-2009)
* @author Froxlor team <team@froxlor.org> (2010-)
2010-01-22 15:03:14 +00:00
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Panel
*
*/
define('AREA', 'login');
/**
* Include our init.php, which manages Sessions, Language etc.
*/
2013-04-27 07:06:19 +00:00
require ('./lib/init.php');
2013-04-27 07:06:19 +00:00
if ($action == '') {
$action = 'login';
}
2013-04-27 07:06:19 +00:00
if ($action == 'login') {
if (isset($_POST['send']) && $_POST['send'] == 'send') {
$loginname = validate($_POST['loginname'], 'loginname');
$password = validate($_POST['password'], 'password');
$stmt = Database::prepare("SELECT `loginname` AS `customer` FROM `" . TABLE_PANEL_CUSTOMERS . "`
WHERE `loginname`= :loginname"
);
Database::pexecute($stmt, array("loginname" => $loginname));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
2013-04-27 07:06:19 +00:00
if ($row['customer'] == $loginname) {
$table = "`" . TABLE_PANEL_CUSTOMERS . "`";
$uid = 'customerid';
$adminsession = '0';
$is_admin = false;
2013-04-27 07:06:19 +00:00
} else {
$is_admin = true;
if ((int)$settings['login']['domain_login'] == 1) {
$domainname = $idna_convert->encode(preg_replace(Array('/\:(\d)+$/', '/^https?\:\/\//'), '', $loginname));
$stmt = Database::prepare("SELECT `customerid` FROM `" . TABLE_PANEL_DOMAINS . "`
WHERE `domain` = :domain"
);
Database::pexecute($stmt, array("domain" => $domainname));
$row2 = $stmt->fetch(PDO::FETCH_ASSOC);
2013-04-27 07:06:19 +00:00
if (isset($row2['customerid']) && $row2['customerid'] > 0) {
$loginname = getCustomerDetail($row2['customerid'], 'loginname');
2013-04-27 07:06:19 +00:00
if ($loginname !== false) {
$stmt = Database::prepare("SELECT `loginname` AS `customer` FROM `" . TABLE_PANEL_CUSTOMERS . "`
WHERE `loginname`= :loginname"
);
Database::pexecute($stmt, array("loginname" => $loginname));
$row3 = $stmt->fetch(PDO::FETCH_ASSOC);
2013-04-27 07:06:19 +00:00
if ($row3['customer'] == $loginname) {
$table = "`" . TABLE_PANEL_CUSTOMERS . "`";
$uid = 'customerid';
$adminsession = '0';
$is_admin = false;
}
}
}
}
}
2013-04-27 07:06:19 +00:00
if (hasUpdates($version) && $is_admin == false) {
2010-01-26 08:59:19 +00:00
redirectTo('index.php');
exit;
}
2013-04-27 07:06:19 +00:00
if ($is_admin) {
if (hasUpdates($version)) {
$stmt = Database::prepare("SELECT `loginname` AS `admin` FROM `" . TABLE_PANEL_ADMINS . "`
WHERE `loginname`= :loginname
AND `change_serversettings` = '1'"
);
Database::pexecute($stmt, array("loginname" => $loginname));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
2013-04-27 07:06:19 +00:00
if (!isset($row['admin'])) {
// not an admin who can see updates
2010-01-26 08:59:19 +00:00
redirectTo('index.php');
exit;
}
2013-04-27 07:06:19 +00:00
} else {
$stmt = Database::prepare("SELECT `loginname` AS `admin` FROM `" . TABLE_PANEL_ADMINS . "`
WHERE `loginname`= :loginname"
);
Database::pexecute($stmt, array("loginname" => $loginname));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
2010-01-26 08:59:19 +00:00
}
2013-04-27 07:06:19 +00:00
if ($row['admin'] == $loginname) {
$table = "`" . TABLE_PANEL_ADMINS . "`";
$uid = 'adminid';
$adminsession = '1';
2013-04-27 07:06:19 +00:00
} else {
redirectTo('index.php', Array('showmessage' => '2'), true);
exit;
}
}
$userinfo_stmt = Database::prepare("SELECT * FROM $table
WHERE `loginname`= :loginname"
);
Database::pexecute($userinfo_stmt, array("loginname" => $loginname));
$userinfo = $userinfo_stmt->fetch(PDO::FETCH_ASSOC);
if ($userinfo['loginfail_count'] >= $settings['login']['maxloginattempts'] && $userinfo['lastlogin_fail'] > (time() - $settings['login']['deactivatetime'])) {
redirectTo('index.php', Array('showmessage' => '3'), true);
exit;
2013-04-27 07:06:19 +00:00
} elseif($userinfo['password'] == md5($password)) {
// login correct
// reset loginfail_counter, set lastlogin_succ
$stmt = Database::prepare("UPDATE $table
SET `lastlogin_succ`= :lastlogin_succ, `loginfail_count`='0'
WHERE `$uid`= :uid"
);
Database::pexecute($stmt, array("lastlogin_succ" => time(), "uid" => $userinfo[$uid]));
$userinfo['userid'] = $userinfo[$uid];
$userinfo['adminsession'] = $adminsession;
2013-04-27 07:06:19 +00:00
} else {
// login incorrect
$stmt = Database::prepare("UPDATE $table
SET `lastlogin_fail`= :lastlogin_fail, `loginfail_count`=`loginfail_count`+1
WHERE `$uid`= :uid"
);
Database::pexecute($stmt, array("lastlogin_fail" => time(), "uid" => $userinfo[$uid]));
unset($userinfo);
redirectTo('index.php', Array('showmessage' => '2'), true);
exit;
}
if (isset($userinfo['userid']) && $userinfo['userid'] != '') {
$s = md5(uniqid(microtime(), 1));
2013-04-27 07:06:19 +00:00
if (isset($_POST['language'])) {
$language = validate($_POST['language'], 'language');
2013-04-27 07:06:19 +00:00
if ($language == 'profile') {
$language = $userinfo['def_language'];
2013-04-27 07:06:19 +00:00
} elseif(!isset($languages[$language])) {
$language = $settings['panel']['standardlanguage'];
}
2013-04-27 07:06:19 +00:00
} else {
$language = $settings['panel']['standardlanguage'];
}
2013-04-27 07:06:19 +00:00
if (isset($userinfo['theme']) && $userinfo['theme'] != '') {
$theme = $userinfo['theme'];
2013-04-27 07:06:19 +00:00
} else {
$theme = $settings['panel']['default_theme'];
}
2013-04-27 07:06:19 +00:00
if ($settings['session']['allow_multiple_login'] != '1') {
$stmt = Database::prepare("DELETE FROM `" . TABLE_PANEL_SESSIONS . "`
WHERE `userid` = :uid
AND `adminsession` = :adminsession"
);
Database::pexecute($stmt, array("uid" => $userinfo['userid'], "adminsession" => $userinfo['adminsession']));
}
// check for field 'theme' in session-table, refs #607
// Changed with #1287 to new method
$theme_field = false;
$stmt = Database::query("SHOW COLUMNS FROM panel_sessions LIKE 'theme'");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($row['Field'] == "theme") {
$has_theme = true;
}
}
$params = array(
"hash" => $s,
"userid" => $userinfo['userid'],
"ipaddress" => $remote_addr,
"useragent" => $http_user_agent,
"lastactivity" => time(),
"language" => $language,
"adminsession" => $userinfo['adminsession']
);
if ($has_theme) {
$params["theme"] = $theme;
$stmt = Database::prepare("INSERT INTO `" . TABLE_PANEL_SESSIONS . "`
(`hash`, `userid`, `ipaddress`, `useragent`, `lastactivity`, `language`, `adminsession`, `theme`)
VALUES (:hash, :userid, :ipaddress, :useragent, :lastactivity, :language, :adminsession, :theme)"
);
} else {
$stmt = Database::prepare("INSERT INTO `" . TABLE_PANEL_SESSIONS . "`
(`hash`, `userid`, `ipaddress`, `useragent`, `lastactivity`, `language`, `adminsession`)
VALUES (:hash, :userid, :ipaddress, :useragent, :lastactivity, :language, :adminsession)"
);
}
Database::pexecute($stmt, $params);
2013-04-27 07:06:19 +00:00
if ($userinfo['adminsession'] == '1') {
if (hasUpdates($version)) {
2010-01-26 08:59:19 +00:00
redirectTo('admin_updates.php', Array('s' => $s), true);
2013-04-27 07:06:19 +00:00
} else {
2010-01-26 08:59:19 +00:00
redirectTo('admin_index.php', Array('s' => $s), true);
}
2013-04-27 07:06:19 +00:00
} else {
redirectTo('customer_index.php', Array('s' => $s), true);
}
2013-04-27 07:06:19 +00:00
} else {
redirectTo('index.php', Array('showmessage' => '2'), true);
}
2013-04-27 07:06:19 +00:00
exit;
} else {
$language_options = '';
2013-04-27 07:06:19 +00:00
$language_options .= makeoption($lng['login']['profile_lng'], 'profile', 'profile', true, true);
2013-04-27 07:06:19 +00:00
while (list($language_file, $language_name) = each($languages)) {
$language_options .= makeoption($language_name, $language_file, 'profile', true);
}
$smessage = isset($_GET['showmessage']) ? (int)$_GET['showmessage'] : 0;
$message = '';
$successmessage = '';
2013-04-27 07:06:19 +00:00
switch ($smessage) {
case 1:
$successmessage = $lng['pwdreminder']['success'];
break;
case 2:
$message = $lng['error']['login'];
break;
case 3:
$message = sprintf($lng['error']['login_blocked'],$settings['login']['deactivatetime']);
break;
case 4:
$cmail = isset($_GET['customermail']) ? $_GET['customermail'] : 'unknown';
$message = str_replace('%s', $cmail, $lng['error']['errorsendingmail']);
break;
case 5:
$message = $lng['error']['user_banned'];
break;
}
2010-01-26 08:59:19 +00:00
$update_in_progress = '';
2013-04-27 07:06:19 +00:00
if (hasUpdates($version)) {
$update_in_progress = $lng['update']['updateinprogress_onlyadmincanlogin'];
}
2013-04-27 07:06:19 +00:00
eval("echo \"" . getTemplate('login') . "\";");
}
}
2013-04-27 07:06:19 +00:00
if ($action == 'forgotpwd') {
$adminchecked = false;
$message = '';
if (isset($_POST['send']) && $_POST['send'] == 'send') {
$loginname = validate($_POST['loginname'], 'loginname');
$email = validateEmail($_POST['loginemail'], 'email');
$result_stmt = Database::prepare("SELECT `adminid`, `customerid`, `firstname`, `name`, `company`, `email`, `loginname`, `def_language`, `deactivated` FROM `" . TABLE_PANEL_CUSTOMERS . "`
WHERE `loginname`= :loginname
AND `email`= :email"
);
Database::pexecute($result_stmt, array("loginname" => $loginname, "email" => $email));
if (Database::num_rows() == 0) {
$result_stmt = Database::prepare("SELECT `adminid`, `name`, `email`, `loginname`, `def_language` FROM `" . TABLE_PANEL_ADMINS . "`
WHERE `loginname`= :loginname
AND `email`= :email"
);
Database::pexecute($result_stmt, array("loginname" => $loginname, "email" => $email));
if (Database::num_rows() > 0) {
$adminchecked = true;
2013-04-27 07:06:19 +00:00
} else {
$result_stmt = null;
}
}
if ($result_stmt !== null) {
$user = $result_stmt->fetch(PDO::FETCH_ASSOC);
/* Check whether user is banned */
2013-04-27 07:06:19 +00:00
if ($user['deactivated']) {
$message = $lng['pwdreminder']['notallowed'];
redirectTo('index.php', Array('showmessage' => '5'), true);
}
if (($adminchecked && $settings['panel']['allow_preset_admin'] == '1') || $adminchecked == false) {
2013-04-27 07:06:19 +00:00
if ($user !== false) {
if ($settings['panel']['password_min_length'] <= 6) {
$password = substr(md5(uniqid(microtime(), 1)), 12, 6);
} else {
// make it two times larger than password_min_length
$rnd = '';
$minlength = $settings['panel']['password_min_length'];
2013-04-27 07:06:19 +00:00
while (strlen($rnd) < ($minlength * 2)) {
$rnd .= md5(uniqid(microtime(), 1));
}
$password = substr($rnd, (int)($minlength / 2), $minlength);
}
2013-04-27 07:06:19 +00:00
$passwordTable = $adminchecked ? TABLE_PANEL_ADMINS : TABLE_PANEL_CUSTOMERS;
$stmt = Database::prepare("UPDATE `" . $passwordTable . "` SET `password`= :password
WHERE `loginname`= :loginname
AND `email`= :email"
);
Database::pexecute($stmt, array("password" => md5($password), "loginname" => $user['loginname'], "email" => $user['email']));
$rstlog = FroxlorLogger::getInstanceOf(array('loginname' => 'password_reset'), $settings);
$rstlog->logAction(USR_ACTION, LOG_WARNING, "Password for user '" . $user['loginname'] . "' has been reset!");
$replace_arr = array(
'SALUTATION' => getCorrectUserSalutation($user),
'USERNAME' => $user['loginname'],
'PASSWORD' => $password
);
$body = strtr($lng['pwdreminder']['body'], array('%s' => $user['firstname'] . ' ' . $user['name'], '%p' => $password));
$def_language = ($user['def_language'] != '') ? $user['def_language'] : $settings['panel']['standardlanguage'];
$result_stmt = Database::prepare('SELECT `value` FROM `' . TABLE_PANEL_TEMPLATES . '`
WHERE `adminid`= :adminid
AND `language`= :lang
AND `templategroup`=\'mails\'
AND `varname`=\'password_reset_subject\''
);
Database::pexecute($result_stmt, array("adminid" => $user['adminid'], "lang" => $def_language));
$result = $result_stmt->fetch(PDO::FETCH_ASSOC);
$mail_subject = html_entity_decode(replace_variables((($result['value'] != '') ? $result['value'] : $lng['pwdreminder']['subject']), $replace_arr));
$result_stmt = Database::prepare('SELECT `value` FROM `' . TABLE_PANEL_TEMPLATES . '`
WHERE `adminid`= :adminid
AND `language`= :lang
AND `templategroup`=\'mails\'
AND `varname`=\'password_reset_mailbody\''
);
Database::pexecute($result_stmt, array("adminid" => $user['adminid'], "lang" => $def_language));
$result = $result_stmt->fetch(PDO::FETCH_ASSOC);
$mail_body = html_entity_decode(replace_variables((($result['value'] != '') ? $result['value'] : $body), $replace_arr));
$_mailerror = false;
try {
$mail->Subject = $mail_subject;
$mail->AltBody = $mail_body;
$mail->MsgHTML(str_replace("\n", "<br />", $mail_body));
$mail->AddAddress($user['email'], $user['firstname'] . ' ' . $user['name']);
$mail->Send();
} catch(phpmailerException $e) {
$mailerr_msg = $e->errorMessage();
$_mailerror = true;
} catch (Exception $e) {
$mailerr_msg = $e->getMessage();
$_mailerror = true;
}
if ($_mailerror) {
$rstlog = FroxlorLogger::getInstanceOf(array('loginname' => 'password_reset'), $settings);
$rstlog->logAction(ADM_ACTION, LOG_ERR, "Error sending mail: " . $mailerr_msg);
redirectTo('index.php', Array('showmessage' => '4', 'customermail' => $user['email']), true);
exit;
}
$mail->ClearAddresses();
redirectTo('index.php', Array('showmessage' => '1'), true);
exit;
2013-04-27 07:06:19 +00:00
} else {
$rstlog = FroxlorLogger::getInstanceOf(array('loginname' => 'password_reset'), $settings);
$rstlog->logAction(USR_ACTION, LOG_WARNING, "User '" . $loginname . "' tried to reset pwd but wasn't found in database!");
$message = $lng['login']['combination_not_found'];
}
unset($user);
}
2013-04-27 07:06:19 +00:00
} else {
$message = $lng['login']['usernotfound'];
}
}
2013-04-27 07:06:19 +00:00
if ($adminchecked) {
if ($settings['panel']['allow_preset_admin'] != '1') {
$message = $lng['pwdreminder']['notallowed'];
unset ($adminchecked);
}
2013-04-27 07:06:19 +00:00
} else {
if ($settings['panel']['allow_preset'] != '1') {
$message = $lng['pwdreminder']['notallowed'];
}
}
2013-04-27 07:06:19 +00:00
eval("echo \"" . getTemplate('fpwd') . "\";");
}