mirror of
https://github.com/UnickSoft/graphonline.git
synced 2026-02-16 02:30:51 +00:00
first commit
This commit is contained in:
68
model/AdminModel.php
Executable file
68
model/AdminModel.php
Executable file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Модель для авторизации администратора
|
||||
*/
|
||||
class AdminModel
|
||||
{
|
||||
const REMEMBER_PERIOD = 604800; // Время хранения авторизации (в секундах)
|
||||
|
||||
public function MakeHash($pwd)
|
||||
{
|
||||
global $g_config;
|
||||
return md5($pwd . $g_config['admin_sector']['salt']);
|
||||
}
|
||||
|
||||
public function IsAuth()
|
||||
{
|
||||
global $g_config;
|
||||
$login = isset($_COOKIE['auto_admin_auth_login']) ? $_COOKIE['auto_admin_auth_login'] : '';
|
||||
$pwd_hash = isset($_COOKIE['auto_admin_auth_pwd_hash']) ? $_COOKIE['auto_admin_auth_pwd_hash'] : '';
|
||||
|
||||
return $login == $g_config['admin_sector']['def_login'] &&
|
||||
$pwd_hash == $this->MakeHash($g_config['admin_sector']['def_pwd']);
|
||||
}
|
||||
|
||||
// Авторизует пользователя, что бы при будущих обращениях он проходил как авторизованный при успехе вернёт true и запомнит что авторизован, иначе false
|
||||
public function Login($login, $pwd_hash)
|
||||
{
|
||||
global $g_config;
|
||||
if (empty($login) || empty($pwd_hash))
|
||||
{
|
||||
trigger_error("Can not login: login or pwd_hash are empty!", E_USER_ERROR);
|
||||
}
|
||||
|
||||
$ret = false;
|
||||
if ($login == $g_config['admin_sector']['def_login'] &&
|
||||
$pwd_hash == $this->MakeHash($g_config['admin_sector']['def_pwd']))
|
||||
{
|
||||
setcookie('auto_admin_auth_login', $login, time() + self::REMEMBER_PERIOD, '/', DOMAIN_COOKIE);
|
||||
setcookie('auto_admin_auth_pwd_hash', $pwd_hash, time() + self::REMEMBER_PERIOD, '/', DOMAIN_COOKIE);
|
||||
$ret = true;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
// Проверяет авторизацию, и если человек не авторизован, то редиректим
|
||||
public function ChkLogin()
|
||||
{
|
||||
global $g_config;
|
||||
$query = strtolower(GetQuery());
|
||||
|
||||
if (!$this->IsAuth())
|
||||
{
|
||||
if (SiteRoot($query) !== SiteRoot($g_config['admin_sector']['after_logout_page']))
|
||||
{
|
||||
header("Location: " . SiteRoot($g_config['admin_sector']['after_logout_page']));
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function Logout()
|
||||
{
|
||||
setcookie('auto_admin_auth_login', '', -1, '/', DOMAIN_COOKIE);
|
||||
setcookie('auto_admin_auth_pwd_hash', '', -1, '/', DOMAIN_COOKIE);
|
||||
}
|
||||
};
|
||||
?>
|
||||
194
model/Model.php
Executable file
194
model/Model.php
Executable file
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Дефолтная модель
|
||||
*
|
||||
* @author Zmi
|
||||
* @updated GYL
|
||||
*/
|
||||
abstract class Model
|
||||
{
|
||||
protected $table;
|
||||
protected $db;
|
||||
private $idField;
|
||||
private $id;
|
||||
private $data = array();
|
||||
private $dataStart = array();
|
||||
private $readOnly = false;
|
||||
|
||||
private $isDel = false;
|
||||
|
||||
abstract protected function CreateTable();
|
||||
|
||||
public function __construct($db, $table, $idField, $id = NULL, $readOnly = false)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->table = $table;
|
||||
$this->idField = $idField;
|
||||
$this->id = $id;
|
||||
$this->readOnly = $readOnly;
|
||||
|
||||
if (!$readOnly)
|
||||
{
|
||||
$this->CreateTable();
|
||||
}
|
||||
|
||||
if ($this->id)
|
||||
{
|
||||
$this->data = $this->db->selectRow("SELECT
|
||||
*
|
||||
FROM
|
||||
?#
|
||||
WHERE
|
||||
?# = ?d",
|
||||
$this->table,
|
||||
$this->idField,
|
||||
$this->id
|
||||
);
|
||||
$this->dataStart = $this->data;
|
||||
}
|
||||
}
|
||||
|
||||
public function __set($key, $value)
|
||||
{
|
||||
if ($this->isDel)
|
||||
{
|
||||
trigger_error("Can not change removed object!", E_USER_ERROR);
|
||||
}
|
||||
|
||||
if ($this->readOnly)
|
||||
{
|
||||
trigger_error("Can not change readonly object!", E_USER_ERROR);
|
||||
}
|
||||
|
||||
if ($key == $this->idField)
|
||||
{
|
||||
trigger_error("Can not change id field!", E_USER_ERROR);
|
||||
}
|
||||
|
||||
return $this->data[$key] = $value;
|
||||
}
|
||||
|
||||
public function __get($key)
|
||||
{
|
||||
// Нельзя получать данные из объекта который удалён
|
||||
if ($this->isDel)
|
||||
{
|
||||
trigger_error("Can not get value from removed object!", E_USER_ERROR);
|
||||
}
|
||||
|
||||
// Если спрашивают ключевое поле, но сейчас идёт только заполнение данных то пытаетмся сделать запись в БД и вернуть id который она скажет
|
||||
if (in_array($key, array($this->idField, 'id')) && !$this->id && count($this->data))
|
||||
{
|
||||
return $this->Flush();
|
||||
}
|
||||
return $this->data[$key];
|
||||
}
|
||||
|
||||
public function Flush()
|
||||
{
|
||||
$ret = false;
|
||||
|
||||
if ($this->readOnly)
|
||||
{
|
||||
return $this->id ? $this->id : false;
|
||||
}
|
||||
|
||||
if ($this->isDel)
|
||||
{
|
||||
return $ret;
|
||||
}
|
||||
|
||||
if (count($this->data))
|
||||
{
|
||||
if ($this->id)
|
||||
{
|
||||
if ($this->dataStart != $this->data)
|
||||
{
|
||||
$data = array();
|
||||
foreach ($this->data as $k => $v)
|
||||
{
|
||||
if ($k == $this->idField) continue;
|
||||
$data[$k] = $v;
|
||||
}
|
||||
|
||||
$ret = $this->db->query("UPDATE
|
||||
?#
|
||||
SET
|
||||
?a
|
||||
WHERE
|
||||
?# = ?d",
|
||||
$this->table,
|
||||
$data,
|
||||
$this->idField,
|
||||
$this->id
|
||||
);
|
||||
|
||||
$this->dataStart = $this->data;
|
||||
}
|
||||
|
||||
$ret = $this->id;
|
||||
}
|
||||
else
|
||||
{
|
||||
$ret = $this->db->query("INSERT INTO
|
||||
?#(?#)
|
||||
VALUES
|
||||
(?a)",
|
||||
$this->table,
|
||||
array_keys($this->data),
|
||||
array_values($this->data)
|
||||
);
|
||||
|
||||
$this->id = $this->data[$this->idField] = $ret;
|
||||
}
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function IsExists()
|
||||
{
|
||||
return $this->db->selectCell("SELECT COUNT(*) FROM ?# WHERE ?# = ?d", $this->table, $this->idField, $this->id) > 0;
|
||||
}
|
||||
|
||||
public function HasChanges()
|
||||
{
|
||||
return $this->data != $this->dataStart;
|
||||
}
|
||||
|
||||
public function IsOnlyShow()
|
||||
{
|
||||
return $this->readOnly;
|
||||
}
|
||||
|
||||
public function IsDeleted()
|
||||
{
|
||||
return $this->isDel;
|
||||
}
|
||||
|
||||
public function Delete()
|
||||
{
|
||||
$ret = false;
|
||||
if ($this->readOnly || $this->isDel)
|
||||
{
|
||||
return $ret;
|
||||
}
|
||||
|
||||
if ($this->id)
|
||||
{
|
||||
$ret = $this->db->query("DELETE FROM ?# WHERE ?# = ?d",
|
||||
$this->table,
|
||||
$this->idField,
|
||||
$this->id);
|
||||
}
|
||||
$this->isDel = $ret;
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
return $this->Flush();
|
||||
}
|
||||
};
|
||||
?>
|
||||
Reference in New Issue
Block a user