_step = $_POST['step'];
}
session_start();
}
public function setTemplate($tpl)
{
$this->_template = $tpl;
}
private function _getConfigPath()
{
if (file_exists('application/config/config.ini.php')) {
$path = 'application/config/config.ini.php';
} else {
$path = 'application/config/config.ini';
}
return $path;
}
private function _step1()
{
$this->_title = $this->t('main_title');
$path = $this->_getConfigPath();
if (!file_exists($path)) {
$this->_stepStatus = false;
$this->_content .= $this->_wrapError($this->t('place_to_tds_dir'));
}
$this->_connect();
if ($this->_stepStatus) {
$this->_content .= '
';
foreach ($this->_getLogins() as $login) {
$this->_content .= '';
}
$this->_content .= '
';
$this->_content .= '
';
$this->_footer .= ''."\r\n";
} else {
$this->_footer .= ''."\r\n";
}
}
private function _wrapSuccess($text)
{
return ' ' . $text . '
';
}
private function _wrapError($text)
{
return ' ' . $text . '
';
}
private function _getLogins()
{
$items = [];
$stmt = $this->_pdo->query("SELECT login FROM ".$this->_cnf['db']['prefix']."users");
foreach($stmt->fetchAll(\PDO::FETCH_COLUMN) as $row) {
$items[] = $row;
}
return $items;
}
private function _step2()
{
$this->_connect();
if (isset($_POST) AND count($_POST)) {
if (strlen($_POST['login']) == 0) {
$this->_stepStatus = false;
$this->_footer .= $this->_wrapError($this->t('empty_login'));
} elseif (strlen($_POST['password']) == 0) {
$this->_stepStatus = false;
$this->_footer .= $this->_wrapError($this->t('empty_password'));
} else {
$password = md5($_POST['password'].$this->_cnf['system']['salt']);
$sql="UPDATE ".$this->_cnf['db']['prefix']."users SET `password`='".$password."' WHERE `login` = '".htmlspecialchars($_POST['login'], ENT_QUOTES)."'";
$this->_pdo->exec($sql);
try {
$sql="UPDATE ".$this->_cnf['db']['prefix']."users SET `password_hash`= NULL WHERE `login` = '".htmlspecialchars($_POST['login'], ENT_QUOTES)."'";
$this->_pdo->exec($sql);
} catch(\PDOException $e) {
}
}
}
if ($this->_stepStatus) {
$this->_content .= $this->t('success');
$this->_footer .= '' . $this->t('login'). ''."\r\n";
} else {
$this->_stepStatus = true;
$this->_step1();
}
}
private function _initConfig()
{
$this->_cnf = parse_ini_string(file_get_contents($this->_getConfigPath()), true);
}
private function _connect()
{
$this->_initConfig();
$host = $this->_cnf['db']['server'];
$user = $this->_cnf['db']['user'];
$pass = $this->_cnf['db']['password'];
$db = $this->_cnf['db']['name'];
if (!($this->_pdo instanceof \PDO)) {
$dsn = "mysql:host=$host;dbname=INFORMATION_SCHEMA;charset=utf8";
$opt = array(
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
);
try {
$this->_pdo = new \PDO($dsn, $user, $pass, $opt);
} catch (\PDOException $e) {
$this->_stepStatus = false;
$this->_footer .= 'MySQL connection error: ' . $e->getMessage() . '
';
return;
}
}
$stmt = $this->_pdo->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '" . $db . "'");
if (!(bool) $stmt->fetchColumn()) {
$this->_stepStatus = false;
$this->_content .= 'Db ' . $db . ' not found
';
return;
}
$this->_pdo->exec('USE `' . $db . '`;');
$this->_pdo->exec("set character_set_client=utf8");
$this->_pdo->exec("set character_set_connection=utf8");
$this->_pdo->exec("set character_set_results=utf8");
$this->_pdo->exec("set collation_connection=utf8_unicode_ci");
}
public function dispatch()
{
if (!session_id()) {
session_start();
}
$this->_checkLang();
$content = $this->_template;
if (defined('CHECKER') && CHECKER) {
$this->_step = 1;
}
$action = '_step'.$this->_step;
if (method_exists($this, $action)){
$this->$action();
} else {
$this->_title = 'Error';
$this->_content = 'Step not exists';
}
$content = str_replace('#DOMAIN#', $this->_site, $content);
$content = str_replace('#CONTENT#', $this->_content, $content);
$content = str_replace('#FOOTER#', $this->_footer, $content);
$content = str_replace('#STEP#', $this->_step, $content);
$content = str_replace('#TITLE#', $this->_title, $content);
$content = str_replace('#MAIN_TITLE#', $this->t('main_title'), $content);
$content = str_replace('#DOCUMENTATION#', $this->t('documentation'), $content);
$content = str_replace('#CURRENT_STEP#', (int) $this->_step, $content);
$content = str_replace('#ANOTHER_LANG#', $this->_getAnotherLang(), $content);
$content = str_replace('#CHANGE_LANG#', $this->_getChangeLangText(), $content);
echo $content;
}
public function t($key, $params = null)
{
return KLocale::get($key, $params);
}
private function _getAnotherLang()
{
if (KLocale::getPreferredLanguage() == 'ru') {
return 'en';
} else {
return 'ru';
}
}
private function _getChangeLangText()
{
if (KLocale::getPreferredLanguage() == 'ru') {
return 'RU → EN';
} else {
return 'EN → RU';
}
}
private function _checkLang()
{
if (isset($_GET['lang'])) {
KLocale::setPreferredLanguage($_GET['lang']);
}
}
}
class KLocale
{
protected static $_data = array(
'ru' => array(
'main_title' => 'Сброс пароля Keitaro',
'place_to_tds_dir' => 'Пожалуйста, загрузите скрипт в директорию Keitaro',
'login' => 'Логин',
'new_password' => 'Новый пароль',
'change' => 'Сменить',
'select_login' => 'Выберите логин',
'empty_password' => 'Введите пароль',
'empty_login' => 'Выберите пользователя',
'sign_in' => 'Авторизоваться',
'success' => 'Пароль заменен.
Не забудьте удалить файл reset_password.php
'
),
'en' => array(
'main_title' => 'Password Reset',
'place_to_tds_dir' => 'Please upload this script to directory of Keitaro',
'login' => 'User Login',
'new_password' => 'New Password',
'change' => 'Change password',
'select_login' => 'Choose a login',
'empty_password' => 'Enter password',
'empty_login' => 'Choose a user',
'sign_in' => 'Sign in',
'success' => 'Password changed.
Do not forget to remove "reset_password.php"
'
)
);
protected static $_language;
protected static $_languages = array('ru', 'en');
const DEFAULT_LANGUAGE = 'ru';
public static function setPreferredLanguage($lang)
{
if (!in_array($lang, self::$_languages)) {
throw new Exception('Language ' . $lang . ' not exists');
}
setcookie('installer_lang', $lang);
$_COOKIE['installer_lang'] = $lang;
}
public static function getPreferredLanguage()
{
if (isset($_COOKIE['installer_lang'])) {
$lang = $_COOKIE['installer_lang'];
} else {
if (strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 'en')) {
$lang = 'en';
}
if (strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 'ru')) {
$lang = 'ru';
}
}
if (!in_array($lang, self::$_languages)) {
$lang = self::DEFAULT_LANGUAGE;
}
return $lang;
}
public static function get($key, $params = null)
{
$path = explode('.', $key);
$data = self::$_data[self::getPreferredLanguage()];
$translation = self::_find($path, $data);
if (!isset($translation)) {
$translation = $key;
}
if ($params) {
if (!is_array($params)) {
$params = array($params);
}
$args = array_merge(array($translation), $params);
$translation = call_user_func_array('sprintf', $args);
}
return $translation;
}
public static function t($key, $params = null)
{
return KLocale::get($key, $params);
}
protected static function _find($path, $data)
{
$key = array_shift($path);
if (!isset($data[$key])) {
return null;
}
if (count($path)) {
return self::_find($path, $data[$key]);
}
return $data[$key];
}
}
$Installer = new ResetPassword();
$Installer->setTemplate(ob_get_contents());
ob_clean();
$Installer->dispatch();