-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.user.php
executable file
·485 lines (363 loc) · 16 KB
/
class.user.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
require_once 'class.layout.php';
require_once 'class.borrower.php';
require_once 'class.dbh.php';
/**
* Class User - Controller class for users
*
*
* This class adds, removes, modifies and authenticates users
*
*
*
* @author Peter Lorimer <[email protected]>
* @license Interleaf
* @copyright Interleaf Technology Ltd
* @package LQ
* Date: 22 July 2011
*/
class User {
protected $username, $password, $library_id, $department_id, $email;
public function __construct($username = '', $password = '',$email = '[email protected]', $department_id = 1, $library_id = 1)
{
$this->username = $username;
$this->password = $password;
}
public function __destruct()
{
$username = $this->username;
$password = $this->password;
unset($username);
unset($password);
}
public function library($user_id)
{
global $dbh;
$query = "
SELECT library.library
FROM users, library
WHERE
users.library_id=library.library_id
AND
users.users_id = " .
$user_id;
$res = $dbh->query($query);
$obj = @$dbh->fetch($res,'');
return $obj->library;
}
public function libraryId($user_id)
{
global $dbh;
$query = "
SELECT library.library_id
FROM users, library
WHERE
users.library_id=library.library_id
AND
users.users_id = " .
$user_id;
$res = $dbh->query($query);
$obj = $dbh->fetch($res,'');
return $obj->library_id;
}
public function Auth()
{
global $dbh;
$query = "SELECT users_id,user_name, password, permissions FROM users
WHERE user_name = '" . $_POST['username'] . "' AND DECODE(password," . SEED . ") = '" .
$_POST['password'] . "'";
//print $query;
$res = $dbh->query($query);
if ($dbh->rows($res) == 1)
{
$obj = $dbh->fetch($res,'');
if (session_name()=='') {
// Session not started yet
session_start();
}
else {
// Session was started, so destroy
@session_destroy();
// But we do want a session started for the next request
session_start();
session_regenerate_id();
// PHP < 4.3.3, since it does not put
setcookie(session_name(), session_id());
}
if (isset($_POST['username']))
{
$_SESSION['username'] = $_POST['username'];
}
$query = "UPDATE users set session_id = '" . session_id() . "' where users_id =" . $obj->users_id;
$dbh->query($query);
//echo $query;
return $obj;
}
else { return 0; }
}
public function checkAuth()
{
global $dbh;
// return 1;
$user_id = $_SESSION['user_id'];
$query = "select session_id FROM users
WHERE users_id = " . $user_id . " and session_id = '" . session_id() . "'";
if ($res = $dbh->query($query))
$rows = @$dbh->rows($res);
// die($query);
if ( $rows > 0)
return $_SESSION['auth'];
else if ($_GET['op'] != 'login') header("Location: /?op=login");
}
public function logInForm()
{
echo "<center>Please enter your username and password<form name=logInForm method=post>
<table><tr><th>Username</th> <td><input type=text size=12 name=username></td></tr>
<tr><th>Password</th><td><input type=password name=password size=12></td></tr>
<tr><td> </td><td align = center><input class = lq-button type=submit name = logon value=' Log In '></td>
</table> </form>";
}
public function department($user_id)
{
global $dbh;
$query =
"SELECT department.department, department.department_id
FROM users, department
WHERE
users.department_id=department.department_id
AND
users.users_id = " .
$user_id;
$res = $dbh->query($query);
$obj = $dbh->fetch($res,'');
return $obj;
}
public function del()
{
global $dbh;
$id = $_REQUEST['uid'];
if (!empty($id))
{
$query = "DELETE FROM users where users_id = " . $id;
$res = $dbh->query($query);
}
if ($res)
{
echo '<script>alert("Record Deleted"); location.href="?op=edituser"</script>';
}
else {
echo '<script>alert("ERROR:\r\n This record cannot be deleted. Please contact your systems administrator, the message associated with this error is \"Referential Integrity Violation\""); location.href="?op=edituser"</script>';
}
}
public function save($user_id,$name)
{
global $dbh;
$query = "UPDATE users SET email = '" . $_POST['email'] . "', library_id=" . $_POST['library'] .
",department_id = " . $_POST['department'] . ",permissions=" . $_POST['type'] . ", user_name = '" . $name . "' ,password= encode('" . $_POST['password'] . "','31415927')
WHERE users_id = " . $user_id;
$dbh->query($query);
debug("UPDATE USER:" . $query);
header("Location: ?op=edituser");
}
public function listAll()
{
global $dbh;
// $objArr = array();
$sql = "SELECT users_id, user_name from users WHERE library_id = " .
$_SESSION['library_id'] . " order by user_name asc";
$res = $dbh->query($sql);
debug("ALL USERS: " . $sql);
while ($obj = $dbh->fetch($res,'') )
{
$objArr[$obj->users_id] = $obj->user_name;
}
return $objArr;
}
public function view()
{
global $dbh;
$even = 2;
if (isset($_REQUEST['uid']) && !empty($_REQUEST['uid']))
{
$query = "SELECT user_name, decode(password,'31415927') as password, email,library_id,department_id,permissions
FROM users
WHERE users_id = " . $_REQUEST['uid'];
$res = $dbh->query($query);
$row = $dbh->fetch($res,'');
echo "<p><center><form action = ?op=saveuser METHOD=POST onSubmit='return validateUserForm();'>
<table>
<tr><td>Username</td><td><input type=text name=user id=username value = '" . $row->user_name . "'>
" .'<span id="username_error" class="username_error"> -- This Field is Required</span>'. "
</td></tr>
<input type=hidden name=user_id value=" . $_REQUEST['uid'] . ">
<input type=hidden name=password value = " . $row->password . ">
<tr><td>Password</td><td><input type=text name=password id=password value = '" . $row->password . "'>
" .'<span id="password_error" class="password_error"> -- This Field is Required</span>'. "
</td></tr>
<tr><td>Email</td><td><input type=text name=email id=email value = '" . $row->email . "'>
" .'<span id="email_error" class="email_error"> -- Please enter a valid email address</span>'. "
</td></tr>
<tr><td>Library</td><td><select name=library id = library onChange = 'libDepartments(this);'>";
$libraryArr = Library::getAll();
foreach ($libraryArr as $key=>$lib)
{
if ($row->library_id == $key)
$selected = "selected";
else $selected = null;
echo "<option value=" . $key . " " .$selected . ">" . $lib . "</option>";
}
$query = "SELECT department_id, department from department where library_id = " . $row->library_id;
$res = $dbh->query($query);
while ($rec = $dbh->fetch($res,'array'))
{
$deptArr[$rec['department_id']] = $rec['department'];
}
echo "</select></td></tr>
" .'<span id="library_error" class="library_error"> -- This Field is Required</span>'. "
<tr><td>Department</td><td><select name=department id=department>";
foreach ($deptArr as $key=>$val)
{
if ($row->department_id == $key)
$selected = "selected";
else $selected = null;
echo "<option value = " . $key . " " . $selected . ">" . $val . "</option>";
}
echo "</select></td></tr>
" .'<span id="department_error" class="department_error"> -- This Field is Required</span>'. "
<tr><td>User Type</td><td>";
$perms = array("1"=>"Local Library Admin","2"=>"Standard User","3"=>"Global Consortium Admin");
echo "<select name=type>
";
foreach ($perms as $key=>$val)
{
if ($row->permissions == $key)
$selected = "selected";
else $selected = "";
echo "<option value = " . $key . " " . $selected . ">" . $val . "</option>";
}
echo "</select>
</td></tr>
<tr><td colspan=2 align=center><input type=submit value=Update class=lq-button></td></tr></table></form></center></p>";
}
else
{
$query = "SELECT * from users";
$res = $dbh->query($query);
echo '<center><h2>View / Modify Users </h2><br>
<table width=600>
<tr bgcolor = "#343434">
<th class=admin>User Name</th><th class=admin>Library</th>
<th class=admin>Department</th><th class=admin>Email</th><th colspan="2" class=admin>Action</th>
</tr>
';
while ($row = $dbh->fetch($res,''))
{
$library = User::library($row->users_id);
$department = User::department($row->users_id);
if ($even%2==0)
$color = '#e0e0e0';
else $color = '#a1a1a1';
echo '<tr bgcolor=' . $color . ' class="results"><td>' . $row->user_name . '</td>
<td class="results">' .User::library($row->users_id) . '</td><td>' . User::department($row->users_id)->department . '</td><td>' . $row->email . '</td>
<td align=right width=30><a href =?op=edituser&uid=' . $row->users_id . '>Edit</a></td><td align=right width=40><a href=?op=deluser&uid=' . $row->users_id . '>Delete</td></tr>';
$even++;
}
echo '</table>';
}
}
public function idFromName($user_name)
{
global $dbh;
$sql = "SELECT users_id from users where user_name = '" . $user_name . "'";
$res = $dbh->query($sql);
$obj = $dbh->fetch($res,'');
return $obj->users_id;
}
public function nameFromId($user_id)
{
global $dbh;
$sql = "SELECT user_name from users where users_id = " . $user_id;
$res = $dbh->query($sql);
$obj = $dbh->fetch($res,'');
return $obj->user_name;
}
public function getUserInput($type = '')
{
global $dbh;
$this->type = $type;
$libraryArr = array();
$deptArr = array();
$query = "SELECT library_id, library from library";
$res = $dbh->query($query);
//$libraryArr[1] = "Please Choose ....";
while ($row = $dbh->fetch($res,'array'))
{
$libraryArr[$row['library_id']] = $row['library'];
}
$query = "SELECT department_id, department from department";
$res = $dbh->query($query);
while ($row = $dbh->fetch($res,'array'))
{
$deptArr[$row['department_id']] = $row['department'];
}
$form = new HTML_Form();
$output = '<h2 align="center">Add a new user</h2><br><br><p>' .
$form->startForm('#', 'post', 'frmAddUser',array('name'=>'addUser','autocomplete'=>'off','onSubmit'=>'return validateUserForm()')) .
'<center><table><tr><td>' .
$form->addLabelFor('newusername', 'Username') . '</td><td>' .
$form->addInput('text', 'newusername', $username, array('id'=>'username', 'size'=>32, 'maxlength'=>50) ) .
'<span id="username_error" class="username_error"> -- This Field is Required</span></td></tr><tr><td>' .
$form->addLabelFor('newpassword', 'Password') . '</td><td>' .
$form->addInput('newpassword', 'newpassword', $newpassword, array('id'=>'password', 'size'=>32, 'maxlength'=>50) ) .
'<span id="password_error" class="password_error"> -- This Field is Required</span><tr><td>' .
$form->addLabelFor('email', 'E-Mail') . '</td><td>' .
$form->addInput('text', 'email', $email, array('id'=>'email', 'size'=>32, 'maxlength'=>50) ) .
'<span id="email_error" class="email_error"> -- Please enter a valid email address</span>
</td></tr><tr><td>' .
$form->addLabelFor('email', 'Library') . '</td><td>' .
$form->addSelectList('library', $libraryArr, true, '', 'Please choose', array('id'=>'library','onChange'=>'libDepartments(this);') ) .
' <span id="library_error" class="library_error"> -- This Field is Required</span></td></tr><tr><td>' .
$form->addLabelFor('department', 'Department') . '</td><td>' .
$form->addSelectList('department', null, true, '', 'Please choose a library first', array('id'=>'department') ) .
'<span id="department_error" class="department_error"> -- This Field is Required</span><tr><td>' .
$form->addLabelFor('type', 'User Type') . '</td><td>' .
$form->addSelectList('type', array('1'=>'Local Library Admin', '2'=>'Standard User', '3'=>'Global Consortium Admin'), true, 2, null, array('id'=>'type') ) .
' </td></tr><tr><td colspan="2" align=center>' . $form->addInput('submit', 'btnAddUser', "Add this User", array('class'=>'lq-button')) . '</td></tr>
</table>
</center><br><br>';
return $output;
}
public function add()
{
global $dbh;
if (isset($_REQUEST['btnAddUser']) && !empty($_REQUEST['btnAddUser']))
{
$query = "INSERT INTO users (user_name,password,email,department_id,library_id,permissions)" .
"VALUES ('" .
$_REQUEST['newusername'] . "',ENCODE('" . $_REQUEST['newpassword'] . "'," . SEED . ")" . ",'" . $_REQUEST['email'] . "'," . $_REQUEST['department'] . "," . $_REQUEST['library'] . "," . $_REQUEST['type'] .
")"
;
$res = $dbh->query($query);
}
}
public function homeScreen($user_id)
{
global $dbh;
$library = User::libraryId($user_id);
$department = User::department($user_id);
$objArr = array();
// For current library display a count of each departments open cases
$query = "SELECT count(*) as count, cases.department_id, department.department
FROM cases, department
WHERE cases.department_id=department.department_id
AND status = 'open'
AND department.library_id = " . $_SESSION['library_id'] .
" GROUP BY department_id ";
$res = $dbh->query($query);
debug("HOME QUERY" . $query);
while ($obj = $dbh->fetch($res,''))
$objArr[] = $obj;
return $objArr;
}
}
?>