Initial repo created
[timetracker.git] / WEB-INF / lib / auth / Auth_db.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/content/time_tracker/open_source/credits.htm
27 // +----------------------------------------------------------------------+
28
29 /**
30 * Auth_db class is used to authenticate users against internal DB
31 * @package TimeTracker
32 */
33 class Auth_db extends Auth {
34
35   /**
36    * Authenticate user against internal users DB
37    *
38    * @param string $login
39    * @param string $password
40    * @return mixed
41    */
42   function authenticate($login, $password)
43   {
44         $mdb2 = getConnection();
45         
46         // Try md5 password match first.
47         $sql = "SELECT id FROM tt_users 
48       WHERE login = ".$mdb2->quote($login)." AND password = md5(".$mdb2->quote($password).") AND status = 1";
49
50     $res = $mdb2->query($sql);
51     if (is_a($res, 'PEAR_Error')) {
52       die($res->getMessage());
53     }
54
55     $val = $res->fetchRow();
56     if ($val['id'] > 0) {
57       return array('login'=>$login,'id'=>$val['id']);
58     } else {
59         
60       // If the OLD_PASSWORDS option is defined - set it.
61       if (defined('OLD_PASSWORDS') && isTrue(OLD_PASSWORDS)) {
62         $sql = "SET SESSION old_passwords = 1";
63         $res = $mdb2->query($sql);
64         if (is_a($res, 'PEAR_Error')) {
65           die($res->getMessage());
66         }       
67       }
68
69       // Try legacy password match. This is needed for compatibility with older versions of TT.
70       $sql = "SELECT id FROM tt_users
71         WHERE login = ".$mdb2->quote($login)." AND password = password(".$mdb2->quote($password).") AND status = 1";
72       $res = $mdb2->query($sql);
73       if (is_a($res, 'PEAR_Error')) {
74         die($res->getMessage());
75       }
76       $val = $res->fetchRow();
77       if ($val['id'] > 0) {
78         return array('login'=>$login,'id'=>$val['id']);
79       }
80       return false;
81     }
82   }
83
84   function isPasswordExternal() {
85     return false;
86   }
87 }