fixes to prior commit
[openemr.git] / library / authentication / common_operations.php
blob962080f4b63396677d0c726dd0103ace644580b1
1 <?php
2 /**
3 * This is a library of commonly used functions for managing data for authentication
4 *
5 * Copyright (C) 2013 Kevin Yeh <kevin.y@integralemr.com> and OEMR <www.oemr.org>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
18 * @package OpenEMR
19 * @author Kevin Yeh <kevin.y@integralemr.com>
20 * @link http://www.open-emr.org
23 require_once("$srcdir/authentication/privDB.php");
24 require_once("$srcdir/authentication/password_hashing.php");
25 define("TBL_USERS_SECURE","users_secure");
26 define("TBL_USERS","users");
28 define("COL_PWD","password");
29 define("COL_UNM","username");
30 define("COL_ID","id");
31 define("COL_SALT","salt");
32 define("COL_LU","last_update");
33 define("COL_PWD_H1","password_history1");
34 define("COL_SALT_H1","salt_history1");
35 define("COL_ACTIVE","active");
37 define("COL_PWD_H2","password_history2");
38 define("COL_SALT_H2","salt_history2");
41 /**
42 * create a new password entry in the users_secure table
44 * @param type $username
45 * @param type $password Passing by reference so additional copy is not created in memory
47 function initializePassword($username,$userid,&$password)
50 $salt=oemr_password_salt();
51 $hash=oemr_password_hash($password,$salt);
52 $passwordSQL= "INSERT INTO ".TBL_USERS_SECURE.
53 " (".implode(",",array(COL_ID,COL_UNM,COL_PWD,COL_SALT,COL_LU)).")".
54 " VALUES (?,?,?,?,NOW()) ";
56 $params=array(
57 $userid,
58 $username,
59 $hash,
60 $salt
62 privStatement($passwordSQL,$params);
63 return $hash;
67 /**
68 * After a user's password has been updated to use the new hashing strategy wipe out the old hash value.
71 * @param type $username
72 * @param type $userid
74 function purgeCompatabilityPassword($username,$userid)
76 $purgeSQL = " UPDATE " . TBL_USERS
77 ." SET ". COL_PWD . "='NoLongerUsed' "
78 ." WHERE ".COL_UNM. "=? "
79 ." AND ".COL_ID. "=?";
80 privStatement($purgeSQL,array($username,$userid));
84 /**
86 * @param type $username
87 * @param type $password
88 * @return boolean returns true if the password for the given user is correct, false otherwise.
90 function confirm_user_password($username,&$password)
92 $getUserSecureSQL= " SELECT " . implode(",",array(COL_ID,COL_PWD,COL_SALT))
93 ." FROM ".TBL_USERS_SECURE
94 ." WHERE BINARY ".COL_UNM."=?";
95 // Use binary keyword to require case sensitive username match
96 $userSecure=privQuery($getUserSecureSQL,array($username));
97 if(is_array($userSecure))
99 $phash=oemr_password_hash($password,$userSecure[COL_SALT]);
100 if($phash==$userSecure[COL_PWD])
103 return true;
106 return false;