Created ttDebugTracer class to hopefully help to address blank screen issues.
[timetracker.git] / WEB-INF / lib / Auth.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 class Auth {
30
31   // isAuthenticated - checks authentication status for user.
32   function isAuthenticated() {
33     if (isset($_SESSION['authenticated'])) {
34 // This check does not work properly because we are not getting here. Need to improve.
35 //        if (!isset($_COOKIE['tt_login'])) {
36 //          die ("Your browser's cookie functionality is turned off. Please turn it on.");
37 //        }
38
39       global $smarty;
40       $smarty->assign('authenticated', true); // Used in header.tpl for menu display.
41       return true;
42     }
43     session_write_close();
44     return false;
45   }
46
47   /**
48    * authenticate - main function for authentication. Returns an array with 'login' key set to login
49    * and other values depending on the underlying authentication module.
50    * Returns false if error. For actual implementation see classes in WEB-INF/lib/auth/.
51    */
52   function authenticate($login, $password)
53   {
54     return false;
55   }
56
57   // isPasswordExternal - returns true if actual password is not stored in the internal DB.
58   function isPasswordExternal()
59   {
60     return false;
61   }
62
63   // doLogin - perfoms a login procedure.
64   function doLogin($login, $password) {
65     $auth = $this->authenticate($login, $password);
66
67     if (isTrue('DEBUG')) {
68       echo '<br>'; var_dump($auth); echo '<br />';
69     }
70
71     if ($auth === false)
72       return false;
73
74     $login = $auth['login'];
75
76     $mdb2 = getConnection();
77     $sql = "SELECT id FROM tt_users WHERE login = ".$mdb2->quote($login)." AND status = 1";
78     $res = $mdb2->query($sql);
79     if (is_a($res, 'PEAR_Error')) {
80       if (isTrue('DEBUG'))
81         echo 'db error!<br />';
82       return false;
83     }
84     $val = $res->fetchRow();
85     if (!$val['id']) {
86       if (isTrue('DEBUG'))
87         echo 'login "'.$login.'" does not exist in Time Tracker database.<br />';
88       return false;
89     }
90
91     $this->setAuth($val['id'], $login);
92     return true;
93   }
94
95   // doLogout - clears logon data from session.
96   function doLogout() {
97     unset($_SESSION['authenticated']);
98     unset($_SESSION['authenticated_user_id']);
99     unset($_SESSION['login']);
100   }
101
102   // setAuth - stores authorization data in session.
103   function setAuth($userid, $username) {
104     $_SESSION['authenticated'] = true;
105     $_SESSION['authenticated_user_id'] = $userid; // NOTE: using "user_id" instead of "authenticated_user_id" gets us in trouble
106                                                   // with older PHP when register_globals = On. What happens is that any time we set
107                                                   // $user_id variable in script, $_SESSION['user_id'] is also changed automatically. 
108     $_SESSION['login'] = $username;
109   }
110
111   // getUserLogin - retrieves user login from session.
112   function getUserLogin() {
113     return $_SESSION['login'];
114   }
115
116   // getUserId - retrieves user ID from session.
117   function getUserId() {
118     if (isset($_SESSION['authenticated_user_id']))
119       return $_SESSION['authenticated_user_id'];
120     else
121       return null;
122   }
123
124   static function &factory($module, $params = array())
125   {
126     import('auth.Auth_'.$module);
127     $class = 'Auth_' . $module;
128     if (class_exists($class)) {
129       $new_class = new $class($params);
130       return $new_class;
131     } else {
132       die('Class '.$class.' not found');
133     }
134   }
135 }