-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthUpdateServer.php
More file actions
137 lines (121 loc) · 4.43 KB
/
Copy pathauthUpdateServer.php
File metadata and controls
137 lines (121 loc) · 4.43 KB
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
<?php
/*
* Auth Server
* cokeeffe <cokeeffe@gmail.com>
*
* Class authUser to handle auth requests, check for updates, send response in JSON.
*
* Table Structure for authentication:
CREATE TABLE IF NOT EXISTS `authUpdates` (
`intID` int(11) NOT NULL AUTO_INCREMENT,
`txtCustomerName` text NOT NULL,
`intCustomerID` int(11) NOT NULL,
`boolActive` tinyint(1) NOT NULL,
`txtUserKey` text NOT NULL,
`txtAuthSalt` text NOT NULL,
`txtLastVersionID` text NOT NULL,
`dateLastVersionDate` datetime NOT NULL,
PRIMARY KEY (`intID`)
) ;
* Table Structure for version values:
CREATE TABLE IF NOT EXISTS `authVersion` (
`txtCurrentVersionID` text NOT NULL
) ;
*/
class authUser
{
private $db;
private $userKey;
private $authToken;
private $authSalt;
private $isAuth;
private $customerID;
private $responseCode;
function __construct() {
$this->db = mysql_connect('host','user','password');
$this->db = mysql_select_db('database', $this->db);
$this->isAuth = false;
$this->responseCode = 5; //oops
}
function __destruct() {
//
}
function getResponseMessage($key) {
$codes = array(0 => "Database Error",
1 => "Authenticated",
2 => "Authentication Error",
3 => "Version up-to-date",
4 => "New Version Available",
5 => "Unknown");
return $codes[$key];
}
function checkAuth() {
if(isset($_GET['authToken'])) {
$this->authToken = mysql_escape_string($_GET['authToken']);
}
if(isset($_GET['customerID']) && is_numeric($_GET['customerID'])) {
$this->customerID = (int)$_GET['customerID'];
}
//compute the auth
if(isset($this->authToken) && isset($this->customerID)) {
$query = mysql_query("SELECT txtUserKey, txtAuthSalt FROM authUpdates WHERE intCustomerID = '".$this->customerID."' LIMIT 1");
if(mysql_num_rows($query) > 0) {
$result = mysql_fetch_array($query);
$this->userKey = $result['txtUserKey'];
$this->authSalt = $result['txtAuthSalt'];
if(hash_hmac('md5', $this->userKey, $this->authSalt) == $this->authToken) {
$this->isAuth = true;
$this->responseCode = 1; //authenticated
}else{
$this->responseCode = 2; //auth error
}
}else{
$this->responseCode = 0;
}
} //
} //end func
function checkUpdates() {
if($this->isAuth == 1) {
if(isset($_GET['versionID'])) {
$versionID = mysql_escape_string($_GET['versionID']);
$query = mysql_query("SELECT txtCurrentVersionID FROM authVersion LIMIT 1");
if(mysql_num_rows($query) > 0) {
$result = mysql_fetch_array($query);
if($versionID == $result['txtLastVersionID']) {
$this->responseCode = 3; //up to date
}else{
$this->responseCode = 4; //new version available
}
}else{
$this->responseCode = 0; //DB error
}
}
}else{
$this->responseCode = 2; //auth error
}
}
function printStatus() {
echo "user Key: ".$this->userKey."<br/>\n";
echo "auth salt: ".$this->authSalt."<br/>\n";
echo "is auth: ".$this->isAuth."<br/>\n";
echo "response code: ".$this->getResponseMessage($this->responseCode)."<br/>\n";
}
function jsonResponse() {
$json = array("response_code" => $this->responseCode,
"response_message" => $this->getResponseMessage($this->responseCode) );
return $json;
}
function sendResponse()
{
$status_header = 'HTTP/1.1 ' . $status . ' OK';
header($status_header);
header('Content-type: text/html');
echo json_encode($this->jsonResponse());
}
}
$auth = new authUpdate;
$auth->checkAuth();
$auth->checkUpdates();
//$auth->printStatus();
$auth->sendResponse();
?>