-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.class.php
executable file
·200 lines (181 loc) · 6.55 KB
/
User.class.php
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
require_once("RESTObject.class.php");
require_once("Location.class.php");
require_once("apiDB.php"); // This sucks a LOT
class User extends RESTObject
{
public $email = '';
public $password = '';
public $access;
public $verified;
public $firstname;
public $lastname;
public $phone;
public $postal;
public $sub_summary;
public $sub_gwadi;
public $sub_stats;
public $locations = Array();
public function get_array_instance() {
$array = Array();
$array["email"] = $this->email;
$array["password"] = $this->password;
$array["id"] = $this->id;
$array["firstname"] = $this->firstname;
$array["lastname"] = $this->lastname;
$array["phone"] = $this->phone;
$array["postal"] = $this->postal;
$array["sub_summary"] = $this->sub_summary;
$array["sub_gwadi"] = $this->sub_gwadi;
$array["sub_stats"] = $this->sub_stats;
$array["locations"] = $this->locations;
return $array;
}
public function get_array_all() {
if ($this->access > 1) {
return apiDB::getUsers();
} else { // only return the user that you have access to
$users = Array();
$user = apiDB::getUserByEmail( $_SERVER['PHP_AUTH_USER'] );
array_push($users, $user);
return $users;
}
}
public function put_array($array) {
if ($this->access < 1) {
return "Not authorized to make any updates : guest account";
}
$user = new User();
if (!empty($array["id"])) {
$user = apiDB::getUser($array["id"]);
if (($_SERVER['PHP_AUTH_USER'] != $user->email) && ($this->access <= 1)) {
return "Not authorized to update User ".$user->id;
}
} else {
if (!empty($array["email"])) {
$user = apiDB::getUserByEmail($array["email"]);
if (($_SERVER['PHP_AUTH_USER'] != $user->email) && ($this->access <= 1)) {
return "Not authorized to update User ".$user->id;
}
}
}
$user->email = empty($array["email"]) ? $user->email : $array["email"];
$user->password = empty($array["password"]) ? $user->password : $array["password"];
$user->verified = empty($array["verified"]) ? $user->verified : $array["verified"];
$user->firstname = empty($array["firstname"]) ? $user->firstname : $array["firstname"];
$user->lastname = empty($array["lastname"]) ? $user->lastname : $array["lastname"];
$user->postal = empty($array["postal"]) ? $user->postal : $array["postal"];
$user->phone = empty($array["phone"]) ? $user->phone : $array["phone"];
$user->sub_summary = isset($array["sub_summary"]) ? $array["sub_summary"] : $user->sub_summary;
$user->sub_stats = isset($array["sub_stats"]) ? $array["sub_stats"] : $user->sub_stats;
$user->sub_gwadi = isset($array["sub_gwadi"]) ? $array["sub_gwadi"] : $user->sub_gwadi;
if ($this->access > 1) {
$user->access = empty($array["access"]) ? $user->access : $array["access"]; // not sure if this is still a security flaw...
}
if (empty($user->id)) {
return apiDB::addUser($user);
} else {
return apiDB::updateUser($user->id, $user);
}
}
public function post_array($array, &$message) {
$user = new User();
$user->email = $array["email"];
$user->password = $array["password"];
// access is 1 by database default
$user->verified = 0;
$user->firstname = $array["firstname"];
$user->lastname = $array["lastname"];
$user->postal = $array["postal"];
$user->phone = $array["phone"];
$user->sub_summary = 1;
$user->sub_gwadi = 1;
$user->sub_stats = 1;
//NOTE: No access restrictions. Anyone with a login (also guest:guest) can add a newuser
return apiDB::addUser($user, $message);
}
public function delete_array($array) {
if (!empty($array["id"])) {
$user = apiDB::getUser($array["id"]);
if ($this->access <= 1) {
return "Not authorized to delete User ".$user->id;
} else {
return apiDB::deleteUser($array["id"]);
}
} else {
if (!empty($array["email"])) {
if ($this->access <= 1) {
return "Not authorized to delete User ".$array["email"];
} else {
return apiDB::deleteUserByEmail($array["email"]);
}
}
}
return "ERROR: No user ID or email specified for deletion";
}
public function getInstanceDetails($id) {
$user = apiDB::getUser($id, 2);
if (empty($user->id)) {
return self::NO_SUCH_ID;
}
if (($_SERVER['PHP_AUTH_USER'] != $user->email) && ($this->access <= 1)) {
return self::ACCESS_DENIED;
}
$this->id = $user->id;
$this->email = $user->email;
$this->password = $user->password;
$this->firstname = $user->firstname;
$this->lastname = $user->lastname;
$this->phone = $user->phone;
$this->postal = $user->postal;
$this->sub_summary = $user->sub_summary;
$this->sub_gwadi = $user->sub_gwadi;
$this->sub_stats = $user->sub_stats;
$this->locations = $user->locations; // Does this array need cloning?
// Preserving $this->access however, to retain admin rights.
return self::SETUP_OK;
}
/**
* Locations Endpoint
*/
protected function locations() {
if (empty($this->id)) {
return $this->_response("User id not set. Cannot run \"locations\" without a valid user id.", 404);
}
$loc = new Location();
$loc->setupClass($this->args, $this->access, $this->extension);
$loc->userid = $this->id;
return $loc->process();
}
function apiLink() {
$linkString = "https://".apiDB::getServerName()."/".apiDB::dirname()."/users/" . $this->id ;
return "<a href=\"".$linkString."\">".$this->email."</a>";
}
protected function latestmeasurements() {
$userid = $this->id;
if (empty($this->id)) {
$user = apiDB::getUserByEmail($_SERVER['PHP_AUTH_USER']);
$userid = $user->id;
}
return $this->display(apiDB::getLatestMeasurements($userid));
}
protected function authenticate () {
return $_SERVER['PHP_AUTH_USER'];
}
protected function verify() {
if (empty($this->id)) {
$user = apiDB::getUserByEmail($_SERVER['PHP_AUTH_USER']);
$this->id = $user->id;
}
if (empty($this->id)) {
return $this->_response("User id not set. Cannot run \"verify\" without a valid user id.", 404);
}
if (empty($_GET["token"])) {
return $this->_response("No token specified. Cannot run \"verify\" without a valid \"token\" parameter.", 404);
}
$message = "";
$code = apiDB::verifyUser($this->id, $_GET["token"], $message);
return $this->_response($message, $code);
}
}
?>