forked from anuko/timetracker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuth.class.php
More file actions
131 lines (117 loc) · 4.48 KB
/
Auth.class.php
File metadata and controls
131 lines (117 loc) · 4.48 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
<?php
// +----------------------------------------------------------------------+
// | Anuko Time Tracker
// +----------------------------------------------------------------------+
// | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
// +----------------------------------------------------------------------+
// | LIBERAL FREEWARE LICENSE: This source code document may be used
// | by anyone for any purpose, and freely redistributed alone or in
// | combination with other software, provided that the license is obeyed.
// |
// | There are only two ways to violate the license:
// |
// | 1. To redistribute this code in source form, with the copyright
// | notice or license removed or altered. (Distributing in compiled
// | forms without embedded copyright notices is permitted).
// |
// | 2. To redistribute modified versions of this code in *any* form
// | that bears insufficient indications that the modifications are
// | not the work of the original author(s).
// |
// | This license applies to this document only, not any other software
// | that it may be combined with.
// |
// +----------------------------------------------------------------------+
// | Contributors:
// | https://www.anuko.com/time-tracker/credits.htm
// +----------------------------------------------------------------------+
class Auth {
// isAuthenticated - checks authentication status for user.
function isAuthenticated() {
if (isset($_SESSION['authenticated'])) {
// This check does not work properly because we are not getting here. Need to improve.
// if (!isset($_COOKIE[LOGIN_COOKIE_NAME])) {
// die ("Your browser's cookie functionality is turned off. Please turn it on.");
// }
global $smarty;
$smarty->assign('authenticated', true); // Used in header.tpl for menu display.
return true;
}
session_write_close();
global $smarty;
$smarty->assign('authenticated', false); // Used in header.tpl for menu display.
return false;
}
/**
* authenticate - main function for authentication. Returns an array with 'login' key set to login
* and other values depending on the underlying authentication module.
* Returns false if error. For actual implementation see classes in lib/auth/.
*/
function authenticate($login, $password)
{
return false;
}
// isPasswordExternal - returns true if actual password is not stored in the internal DB.
function isPasswordExternal()
{
return false;
}
// doLogin - perfoms a login procedure.
function doLogin($login, $password) {
$auth = $this->authenticate($login, $password);
if ($auth === false) {
return false;
}
$login = $auth['login'];
$mdb2 = getConnection();
$types = array('text', 'integer');
$sth = $mdb2->prepare('SELECT id FROM tt_users WHERE login=:login AND status=:status', $types);
$data = array('login' => $login, 'status' => 1);
$res = $sth->execute($data);
if (is_a($res, 'PEAR_Error')) {
return false;
}
$val = $res->fetchRow();
if (!$val['id']) {
return false;
}
$this->setAuth($val['id'], $login);
return true;
}
// doLogout - clears logon data from session.
function doLogout() {
unset($_SESSION['authenticated']);
unset($_SESSION['authenticated_user_id']);
unset($_SESSION['login']);
}
// setAuth - stores authorization data in session.
function setAuth($userid, $username) {
$_SESSION['authenticated'] = true;
$_SESSION['authenticated_user_id'] = $userid; // NOTE: using "user_id" instead of "authenticated_user_id" gets us in trouble
// with older PHP when register_globals = On. What happens is that any time we set
// $user_id variable in script, $_SESSION['user_id'] is also changed automatically.
$_SESSION['login'] = $username;
}
// getUserLogin - retrieves user login from session.
function getUserLogin() {
return $_SESSION['login'];
}
// getUserId - retrieves user ID from session.
function getUserId() {
if (isset($_SESSION['authenticated_user_id']))
return $_SESSION['authenticated_user_id'];
else
return null;
}
static function &factory($module, $params = array())
{
import('auth.Auth_'.$module);
$class = 'Auth_' . $module;
if (class_exists($class)) {
$new_class = new $class($params);
return $new_class;
} else {
die('Class '.$class.' not found');
}
}
}