2 // Copyright (C) 2011 Cassian LUP <cassi.lup@gmail.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 //starting the PHP session (also regenerating the session id to avoid session fixation attacks)
11 session_regenerate_id(true);
14 //landing page definition -- where to go if something goes wrong
15 $landingpage = "index.php?site=".$_SESSION['site_id'];
18 //checking whether the request comes from index.php
19 if (!isset($_SESSION['itsme'])) {
21 header('Location: '.$landingpage.'&w');
27 if (!isset($_POST['uname']) ||
empty($_POST['uname'])) {
29 header('Location: '.$landingpage.'&w&c');
32 if (!isset($_POST['pass']) ||
empty($_POST['pass'])) {
34 header('Location: '.$landingpage.'&w&c');
40 if (!empty($_POST['languageChoice'])) {
41 $_SESSION['language_choice'] = (int)$_POST['languageChoice'];
43 else if (empty($_SESSION['language_choice'])) {
44 // just in case both are empty, then use english
45 $_SESSION['language_choice'] = 1;
48 // keep the current session language token
51 //Settings that will override globals.php
56 require_once('../interface/globals.php');
57 require_once("$srcdir/authentication/common_operations.php");
58 $password_update=isset($_SESSION['password_update']);
59 unset($_SESSION['password_update']);
60 $plain_code= $_POST['pass'];
63 $authorizedPortal=false; //flag
64 DEFINE("TBL_PAT_ACC_ON","patient_access_onsite");
65 DEFINE("COL_PID","pid");
66 DEFINE("COL_POR_PWD","portal_pwd");
67 DEFINE("COL_POR_USER","portal_username");
68 DEFINE("COL_POR_SALT","portal_salt");
69 DEFINE("COL_POR_PWD_STAT","portal_pwd_status");
70 $sql= "SELECT ".implode(",",array(COL_ID
,COL_PID
,COL_POR_PWD
,COL_POR_SALT
,COL_POR_PWD_STAT
))
71 ." FROM ".TBL_PAT_ACC_ON
72 ." WHERE ".COL_POR_USER
."=?";
73 $auth = privQuery($sql, array($_POST['uname']));
77 header('Location: '.$landingpage.'&w');
80 if(empty($auth[COL_POR_SALT
]))
82 if(SHA1($plain_code)!=$auth[COL_POR_PWD
])
85 header('Location: '.$landingpage.'&w');
88 $new_salt=oemr_password_salt();
89 $new_hash=oemr_password_hash($plain_code,$new_salt);
90 $sqlUpdatePwd= " UPDATE " . TBL_PAT_ACC_ON
91 ." SET " .COL_POR_PWD
."=?, "
92 . COL_POR_SALT
. "=? "
93 ." WHERE ".COL_ID
."=?";
94 privStatement($sqlUpdatePwd,array($new_hash,$new_salt,$auth[COL_ID
]));
97 if(oemr_password_hash($plain_code,$auth[COL_POR_SALT
])!=$auth[COL_POR_PWD
])
100 header('Location: '.$landingpage.'&w');
106 $_SESSION['portal_username']=$_POST['uname'];
107 $sql = "SELECT * FROM `patient_data` WHERE `pid` = ?";
109 if ($userData = sqlQuery($sql, array($auth['pid']) )) { // if query gets executed
111 if (empty($userData)) {
112 // no records for this pid, so escape
114 header('Location: '.$landingpage.'&w');
118 if ($userData['allow_patient_portal'] != "YES") {
119 // Patient has not authorized portal, so escape
121 header('Location: '.$landingpage.'&w');
125 if ($auth['pid'] != $userData['pid']) {
126 // Not sure if this is even possible, but should escape if this happens
128 header('Location: '.$landingpage.'&w');
132 if ( $password_update)
134 $code_new=$_POST['pass_new'];
135 $code_new_confirm=$_POST['pass_new_confirm'];
136 if(!(empty($_POST['pass_new'])) && !(empty($_POST['pass_new_confirm'])) && ($code_new == $code_new_confirm)) {
137 $new_salt=oemr_password_salt();
138 $new_hash=oemr_password_hash($code_new,$new_salt);
140 // Update the password and continue (patient is authorized)
141 privStatement("UPDATE ".TBL_PAT_ACC_ON
142 ." SET ".COL_POR_PWD
."=?,".COL_POR_SALT
."=?,".COL_POR_PWD_STAT
."=1 WHERE id=?", array($new_hash,$new_salt,$auth['id']) );
143 $authorizedPortal = true;
146 if ($auth['portal_pwd_status'] == 0) {
147 if(!$authorizedPortal) {
148 // Need to enter a new password in the index.php script
149 $_SESSION['password_update'] = 1;
150 header('Location: '.$landingpage);
155 if ($auth['portal_pwd_status'] == 1) {
156 // continue (patient is authorized)
157 $authorizedPortal = true;
160 if ($authorizedPortal) {
161 // patient is authorized (prepare the session variables)
162 unset($_SESSION['password_update']); // just being safe
163 unset($_SESSION['itsme']); // just being safe
164 $_SESSION['pid'] = $auth['pid'];
165 $_SESSION['patient_portal_onsite'] = 1;
169 header('Location: '.$landingpage.'&w');
174 else { //problem with query
176 header('Location: '.$landingpage.'&w');
180 require_once('summary_pat_portal.php');