php class Authorization private static instance NULL const LOGIN_VALID

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
class Authorization {
private static $instance = NULL;
const LOGIN_VALID_OK = 0;
const LOGIN_VALID_EMPTY = 1;
const LOGIN_VALID_SMALL = 2;
const LOGIN_VALID_BIG = 3;
const PASSW_VALID_OK = 0;
const PASSW_VALID_EMPTY = 1;
const PASSW_VALID_SMALL = 2;
const PASSW_VALID_BIG = 3;
public static function getInstance()
{
if(self::$instance == NULL)
self::$instance = new self();
return self::$instance;
}
private function validateLogin($login)
{
if(strlen($login) == 0)
$status = self::LOGIN_VALID_EMPTY;
if(!isset($status) AND strlen($login) < 3)
$status = self::LOGIN_VALID_SMALL;
if(!isset($status) AND strlen($login) > 35)
$status = self::LOGIN_VALID_BIG;
if(!isset($status))
$status = self::LOGIN_VALID_OK;
return $status;
}
private function validatePassword($password)
{
if(strlen($password) == 0)
$status = self::PASSW_VALID_EMPTY;
if(!isset($status) AND strlen($password) < 3)
$status = self::PASSW_VALID_SMALL;
if(!isset($status) AND strlen($password) > 35)
$status = self::PASSW_VALID_BIG;
if(!isset($status))
$status = self::PASSW_VALID_OK;
return $status;
}
public function login($login, $password)
{
$login = trim($login);
$password = trim($password);
if(self::validateLogin($login) == 0 AND self::validatePassword($password) == 0)
{
$db = Registry::get('database');
$result = $db->query();
$row = $result->fetch(PDO::FETCH_ASSOC);
if($row)
{
$result = $db->query();
$row['balance'] = $result->fetchColumn();
$result = $db->query();
$row['history'] = $result->fetchAll(PDO::FETCH_ASSOC);
self::storeUserData($row);
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
private function storeUserData($userData)
{
$_SESSION['user_data'] = $userData;
$_SESSION['isUserLogin'] = true;
}
public function getUserData()
{
return $_SESSION['user_data'];
}
public function isUserLogin()
{
return $_SESSION['isUserLogin'];
}
public function logout()
{
//$this->removeDataFromSession();
if(isset($_SESSION['user_data']))
unset($_SESSION['user_data']);
$_SESSION['isUserLogin'] = false;
}
/*
private function removeDataFromSession()
{
if(isset($_SESSION['user_data']))
unset($_SESSION['user_data']);
}
*/
}