MD Auth class for Davron

  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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?
class MD_AUTH_HANDLER
{
var $error=null;
var $arData=array();
var $arFiles=array();
var $auth=false;
var $authHash=null;
var $APP;
var $DB;
var $USER;
var $action;
var $employee;
var $admin=false;
//construct
function __construct()
{
$this->APP=$GLOBALS["APPLICATION"];
$this->DB=$GLOBALS["DB"];
$this->USER=$GLOBALS["USER"];
$this->error=null;
Header('Content-Type: application/json; charset=utf-8');
$this->arData = $_SERVER['REQUEST_METHOD'] == 'POST' ? $_POST : $_GET;
$this->arFiles=$_FILES["files"];
$this->arData = $this->APP->ConvertCharsetArray($this->arData, 'UTF-8', LANG_CHARSET);
$this->productKey=$this->arData["access_key"];
$this->action=$this->arData["action"];
$this->AuthHttp();
$this->CheckAuth(false);
}
//auth http
function AuthHttp()
{
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER["PHP_AUTH_PW"]))
{
$rUser = new CUser();
if (($success = $rUser->Login($_SERVER['PHP_AUTH_USER'], $_SERVER["PHP_AUTH_PW"], 'N', 'Y')))
{
if($success==1){
$this->auth=true;
$this->CreateHash();
}
}
}
}
//check
function CheckAuth($show_success=false)
{
if($this->arData["login"] && $this->arData["password"])
{
$this->Authorize();
}elseif($this->arData["access_token"])
{
$this->LoginByHash();
}elseif(!$this->auth)
{
$this->ShowError(" LOGIN OR PASSWORD IS INCORRECT", 103);
}
if($this->auth && $show_success)
{
$this->ShowSuccess("AUTH IS SUCCESS", array());
}
}
//authorize
function Authorize()
{
$rUser = new CUser();
if (($error = $rUser->Login($this->arData['login'], $this->arData['password'], 'N', 'Y')) !== true)
{
$this->ShowError("LOGIN OR PASSWORD IS INCORRECT", 103);
}else{
$this->auth=true;
$this->CreateHash();
}
}
//Login by HASH
function LoginByHash(){
if (strlen($this->arData['access_token']) > 0)
{
$_REQUEST['bx_hit_hash'] = $this->arData['access_token'];
if($this->USER->LoginHitByHash()){
$this->auth=true;
}else
{
$this->auth=false;
$this->ShowError("INCORRECT HASH", 104);
}
}else{
$this->auth=false;
$this->ShowError("INCORRECT HASH", 104);
}
}
//Create auth hash
function CreateHash()
{
if(!$this->authHash)
$this->authHash=$this->USER->AddHitAuthHash($this->APP->GetCurPage());
}
//error send
function ShowError($str, $code)
{
$response=array(
"status"=>"error",
"statusCode"=>$code,
"message"=>$GLOBALS["APPLICATION"]->ConvertCharset($str, LANG_CHARSET, "UTF-8"),
"result"=>""
);
$GLOBALS["APPLICATION"]->RestartBuffer();
echo CUtil::PhpToJSObject($response);
exit();
}
//send success
function ShowSuccess($str, $result=array())
{
$response=array(
"status"=>"success",
"statusCode"=>"200",
"message"=>$GLOBALS["APPLICATION"]->ConvertCharset($str, LANG_CHARSET, "UTF-8"),
"result"=>$GLOBALS["APPLICATION"]->ConvertCharsetArray($result, LANG_CHARSET, "UTF-8"),
);
if($this->authHash)
{
$response["access_token"]=$this->authHash;
}
$GLOBALS["APPLICATION"]->RestartBuffer();
echo json_encode($response);
exit();
}
}
//use
$rest=new MD_AUTH_HANDLER();
?>