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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
<?php
/*
* SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to UNLICENSE
*/
require_once "class/agent.class.php";
/*
* This class models Scrott users, including users without valid logins
* (external-users).
*/
class user extends agent
{
/*
* Constructor
*/
public function __construct(?string $guid = NULL)
{
$this->fields['users'] = array(
"guid",
"auth",
"salt",
"alias",
"email",
"emailVer",
"admin",
"reg",
"emailConf",
);
parent::__construct($guid);
$this->expectType("user");
}
/*
* Get the GUID of a user object from a given username, or NULL if
* the username is not in use. Therefore, this function can be
* used to test the existence of a user with the given username.
*/
public static function getGuidByUname(string $uname) : ?string
{
$uname = database::esc($uname);
$query = "SELECT guid FROM objects WHERE objtype = 'user' AND name = '" . $uname . "'";
$res = database::query($query);
if (count($res) == 0)
return NULL;
return $res[0]['guid'];
}
/*
* Get a user object from a given username, or NULL if the username
* is not in use. This function can be used to test the existence
* of a user with the given username.
*/
public static function getByUname(string $uname) : ?user
{
if (($guid = self::getGuidByUname($uname)))
return new user($guid);
return NULL;
}
/*
* Get an array of all users, sorted by username
*/
public static function getAll_ordByUname() : array
{
$query = "SELECT guid FROM objects WHERE objtype = 'user' ORDER BY name";
$res = database::query($query);
$users = array();
foreach ($res as $u)
$users[] = new user($u['guid']);
return $users;
}
/*
* Get an array of all users, sorted by admin (descending, admins
* first), then by username.
*/
public static function getAll_ordByAdminByUname() : array
{
$query = "SELECT o.guid FROM objects o JOIN users u ON o.guid = u.guid " .
"WHERE o.objtype = 'user' ORDER BY u.admin DESC, o.name";
$res = database::query($query);
$users = array();
foreach ($res as $u)
$users[] = new user($u['guid']);
return $users;
}
/*
* Get an array of all admins, sorted by username
*/
public static function getAllAdmin_ordByUname() : array
{
$query = "SELECT o.guid FROM objects o JOIN users u ON o.guid = u.guid " .
"WHERE o.objtype = 'user' AND u.admin = 1 ORDER BY o.name";
$res = database::query($query);
$users = array();
foreach ($res as $u)
$users[] = new user($u['guid']);
return $users;
}
/*
* Get the currently logged in user, or NULL if logged out. This
* function will throw if unable to aquire a PHP session. This
* function will also forcibly log the current user out if it
* detects any changes in the user-agent or remote IP address.
*/
public static function getCurrent() : ?user
{
if ((session_status() != PHP_SESSION_ACTIVE) && !session_start())
throw new Exception("Unable to aquire a PHP session");
if (!isset($_SESSION['userguid']))
return NULL;
/* detect session hijacking */
if (($_SESSION['useragent'] != $_SERVER['HTTP_USER_AGENT']) ||
($_SESSION['userip'] != $_SERVER['REMOTE_ADDR']))
{
self::setCurrent();
location("/");
return NULL;
}
try
{
return new user($_SESSION['userguid']);
}
catch (Exception $e)
{
/* invalid user */
self::setCurrent();
location("/");
return NULL;
}
}
/*
* Set the currently logged in user. Using NULL will logout any
* current user. This function will throw if unable to aquire a
* PHP session. This function will also cache the user-agent and
* remote IP address of the current request to help validate future
* requests made under the same session.
*/
public static function setCurrent(?user $user = NULL) : void
{
if ((session_status() != PHP_SESSION_ACTIVE) && !session_start())
throw new Exception("Unable to aquire a PHP session");
unset($_SESSION['userguid']);
unset($_SESSION['useragent']);
unset($_SESSION['userip']);
if ($user)
{
$_SESSION['userguid'] = $user->guid;
$_SESSION['useragent'] = $_SERVER['HTTP_USER_AGENT'];
$_SESSION['userip'] = $_SERVER['REMOTE_ADDR'];
}
}
/*
* Initialize a new user object with the given username and plain
* text password. This function returns NULL if $uname is already
* being used.
*/
public static function initNew(string $uname, string $passwd) : ?user
{
if (self::getByUname($uname))
return NULL;
$user = new user();
/* if there exist no users already, make this new one an admin */
if (count(self::getAll_ordByUname()) == 0)
$user->admin = 1;
$user->name = $uname;
$user->objtype = "user";
$user->setPasswd($passwd);
$user->setEmail("");
$user->reg = 1;
$user->saveObj();
return $user;
}
/*
* Get the salted and hashed form of a password
*/
private static function getAuth(string $passwd, string $salt) : string
{
return hash("sha256", $passwd . $salt);
}
/*
* Validate the given plain-text password for this user. Returns true if
* correct, false otherwise.
*/
public function validatePasswd(string $passwd) : bool
{
$auth = self::getAuth($passwd, $this->salt);
return $auth == $this->auth;
}
/*
* Update the auth and salt for this user, given a new plain-text
* password.
*/
public function setPasswd(string $passwd) : void
{
$this->salt = self::getBlob();
$this->auth = self::getAuth($passwd, $this->salt);
$this->saveObj();
}
/*
* Validate the email confirmation code for this user. Returns true if
* correct, false otherwise. On success, $this->emailConf is also set
* to 1
*/
public function verifyEmail(string $ver) : bool
{
if ($ver != $this->emailVer)
return false;
$this->emailConf = 1;
$this->saveObj();
return true;
}
/*
* Update the email address for this user. This function will automatically
* reset the emailConf flag and confirmation code for this user as well.
*/
public function setEmail(string $email) : void
{
$this->email = $email;
$this->emailVer = substr(self::getBlob(), 0, 8);
$this->emailConf = 0;
$this->saveObj();
}
/*
* Get all groups this user owns or is a member of. This isn't necessarily
* all groups this user has access permissions for. Results are sorted by
* ownership, then by name.
*/
public function getGroups_ordByOwnByName() : array
{
$groups = array();
/* owner */
$query = "SELECT guid FROM objects WHERE objtype = 'group' AND owner = '" . database::esc($this->guid) . "' " .
"ORDER BY name";
$res = database::query($query);
foreach ($res as $g)
$groups[] = new group($g['guid']);
/* member */
$query = "SELECT o.guid FROM objects o JOIN members m ON o.guid = m.guid WHERE o.objtype = 'group' AND " .
"m.member = '" . database::esc($this->guid) . "' ORDER BY o.name";
$res = database::query($query);
foreach ($res as $g)
$groups[] = new group($g['guid']);
return $groups;
}
/*
* Send an email message to this user using stored configuration
* parameters. If config is not established, delivery is not
* attempted. Return status.
*/
public function sendEmail(string $subj, string $mesg,
?string $attachPath = NULL, ?string $attachName = NULL,
bool $ignoreEmailConf = false) : bool
{
if (settings::smtpServer() == "")
return false;
if (!$ignoreEmailConf && !$this->emailConf)
return true;
if ($this->email == "")
return true;
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = settings::smtpServer();
$mail->Port = settings::smtpPort();
$mail->Username = settings::smtpUname();
$mail->Password = settings::smtpPasswd();
$mail->SMTPSecure = settings::smtpSecurity();
$mail->setFrom(settings::smtpEmailAddress(), settings::smtpFrom());
$mail->addAddress($this->email, $this->getDisplayName());
$mail->Subject = $subj;
$mail->Body = $mesg;
if ($attachPath && $attachName)
$mail->addAttachment($attachPath, $attachName);
return $mail->send();
}
}
?>
|