rename password functions for PHP 5.5 compatibility
[openemr.git] / interface / patient_file / summary / create_portallogin.php
blobe80791211a026175d347e6a6adf0245b91e2782c
1 <?php
2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2011 Z&H Consultancy Services Private Limited <sam@zhservices.com>
4 //
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
18 // A copy of the GNU General Public License is included along with this program:
19 // openemr/interface/login/GnuGPL.html
20 // For more information write to the Free Software
21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 //
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Jacob T Paul <jacob@zhservices.com>
25 // Paul Simon <paul@zhservices.com>
27 // +------------------------------------------------------------------------------+
29 //SANITIZE ALL ESCAPES
30 $sanitize_all_escapes=true;
33 //STOP FAKE REGISTER GLOBALS
34 $fake_register_globals=false;
36 require_once("../../globals.php");
37 require_once("$srcdir/sql.inc");
38 require_once("$srcdir/formdata.inc.php");
39 require_once("$srcdir/classes/postmaster.php");
41 // Collect portalsite parameter (either off for offsite or on for onsite); only allow off or on
42 $portalsite = isset($_GET['portalsite']) ? $_GET['portalsite'] : $portalsite = "off";
43 if ($portalsite != "off" && $portalsite != "on") $portalsite = "off";
45 $row = sqlQuery("SELECT pd.*,pao.portal_username,pao.portal_pwd,pao.portal_pwd_status FROM patient_data AS pd LEFT OUTER JOIN patient_access_" . add_escape_custom($portalsite) . "site AS pao ON pd.pid=pao.pid WHERE pd.pid=?",array($pid));
47 function generatePassword($length=6, $strength=1) {
48 $consonants = 'bdghjmnpqrstvzacefiklowxy';
49 $numbers = '0234561789';
50 $specials = '@#$%';
53 $password = '';
54 $alt = time() % 2;
55 for ($i = 0; $i < $length/3; $i++) {
56 if ($alt == 1) {
57 $password .= $consonants[(rand() % strlen($consonants))].$numbers[(rand() % strlen($numbers))].$specials[(rand() % strlen($specials))];
58 $alt = 0;
59 } else {
60 $password .= $numbers[(rand() % strlen($numbers))].$specials[(rand() % strlen($specials))].$consonants[(rand() % strlen($consonants))];
61 $alt = 1;
64 return $password;
67 function validEmail($email){
68 if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
69 return true;
71 return false;
74 function messageCreate($uname,$pass,$site){
75 $message = htmlspecialchars( xl("Patient Portal Web Address"),ENT_NOQUOTES) . ":<br>";
76 if ($site == "on") {
77 $message .= "<a href='" . htmlspecialchars($GLOBALS['portal_onsite_address'],ENT_QUOTES) . "'>" .
78 htmlspecialchars($GLOBALS['portal_onsite_address'],ENT_NOQUOTES) . "</a><br><br>";
79 } // $site == "off"
80 else {
81 $offsite_portal_patient_link = $GLOBALS['portal_offsite_address_patient_link'] ? htmlspecialchars($GLOBALS['portal_offsite_address_patient_link'],ENT_QUOTES) : htmlspecialchars("https://mydocsportal.com",ENT_QUOTES);
82 $message .= "<a href='" . $offsite_portal_patient_link . "'>" .
83 $offsite_portal_patient_link . "</a><br><br>";
84 $message .= htmlspecialchars(xl("Provider Id"),ENT_NOQUOTES) . ": " .
85 htmlspecialchars($GLOBALS['portal_offsite_providerid'],ENT_NOQUOTES) . "<br><br>";
88 $message .= htmlspecialchars(xl("User Name"),ENT_NOQUOTES) . ": " .
89 htmlspecialchars($uname,ENT_NOQUOTES) . "<br><br>" .
90 htmlspecialchars(xl("Password"),ENT_NOQUOTES) . ": " .
91 htmlspecialchars($pass,ENT_NOQUOTES) . "<br><br>";
92 return $message;
95 function emailLogin($patient_id,$message){
96 $patientData = sqlQuery("SELECT * FROM `patient_data` WHERE `pid`=?", array($patient_id) );
97 if ( $patientData['hipaa_allowemail'] != "YES" || empty($patientData['email']) || empty($GLOBALS['patient_reminder_sender_email']) ) {
98 return false;
100 if (!(validEmail($patientData['email']))) {
101 return false;
103 if (!(validEmail($GLOBALS['patient_reminder_sender_email']))) {
104 return false;
107 $mail = new MyMailer();
108 $pt_name=$patientData['fname'].' '.$patientData['lname'];
109 $pt_email=$patientData['email'];
110 $email_subject=xl('Access Your Patient Portal');
111 $email_sender=$GLOBALS['patient_reminder_sender_email'];
112 $mail->AddReplyTo($email_sender, $email_sender);
113 $mail->SetFrom($email_sender, $email_sender);
114 $mail->AddAddress($pt_email, $pt_name);
115 $mail->Subject = $email_subject;
116 $mail->MsgHTML("<html><body><div class='wrapper'>".$message."</div></body></html>");
117 $mail->IsHTML(true);
118 $mail->AltBody = $message;
120 if ($mail->Send()) {
121 return true;
122 } else {
123 $email_status = $mail->ErrorInfo;
124 error_log("EMAIL ERROR: ".$email_status,0);
125 return false;
129 function displayLogin($patient_id,$message,$emailFlag){
130 $patientData = sqlQuery("SELECT * FROM `patient_data` WHERE `pid`=?", array($patient_id) );
131 if ($emailFlag) {
132 $message = "<br><br>" .
133 htmlspecialchars(xl("Email was sent to following address"),ENT_NOQUOTES) . ": " .
134 htmlspecialchars($patientData['email'],ENT_NOQUOTES) . "<br><br>" .
135 $message;
137 echo "<html><body onload='window.print();'>" . $message . "</body></html>";
140 if(isset($_REQUEST['form_save']) && $_REQUEST['form_save']=='SUBMIT'){
141 require_once("$srcdir/authentication/common_operations.php");
143 $clear_pass=$_REQUEST['pwd'];
145 $res = sqlStatement("SELECT * FROM patient_access_" . add_escape_custom($portalsite) . "site WHERE pid=?",array($pid));
146 $query_parameters=array($_REQUEST['uname']);
147 $salt_clause="";
148 if($portalsite=='on')
150 // For onsite portal create a blowfish based hash and salt.
151 $new_salt = oemr_password_salt();
152 $salt_clause = ",portal_salt=? ";
153 array_push($query_parameters,oemr_password_hash($clear_pass,$new_salt),$new_salt);
155 else
157 // For offsite portal still create and SHA1 hashed password
158 // When offsite portal is updated to handle blowfish, then both portals can use the same execution path.
159 array_push($query_parameters,SHA1($clear_pass));
161 array_push($query_parameters,$pid);
162 if(sqlNumRows($res)){
163 sqlStatement("UPDATE patient_access_" . add_escape_custom($portalsite) . "site SET portal_username=?,portal_pwd=?,portal_pwd_status=0 " . $salt_clause . " WHERE pid=?",$query_parameters);
165 else{
166 sqlStatement("INSERT INTO patient_access_" . add_escape_custom($portalsite) . "site SET portal_username=?,portal_pwd=?,portal_pwd_status=0" . $salt_clause . " ,pid=?",$query_parameters);
169 // Create the message
170 $message = messageCreate($_REQUEST['uname'],$clear_pass,$portalsite);
171 // Email and display/print the message
172 if ( emailLogin($pid,$message) ) {
173 // email was sent
174 displayLogin($pid,$message,true);
176 else {
177 // email wasn't sent
178 displayLogin($pid,$message,false);
180 exit;
181 } ?>
183 <html>
184 <head>
185 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
187 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.6.4.min.js"></script>
188 <script type="text/javascript">
189 function transmit(){
191 // get a public key to encrypt the password info and send
192 document.getElementById('form_save').value='SUBMIT';
193 document.forms[0].submit();
195 </script>
196 </head>
197 <body class="body_top">
198 <form name="portallogin" action="" method="POST">
199 <table align="center" style="margin-top:10px">
200 <tr class="text">
201 <th colspan="5" align="center"><?php echo htmlspecialchars(xl("Generate Username And Password For")." ".$row['fname'],ENT_QUOTES);?></th>
202 </tr>
203 <?php
204 if($portalsite == 'off'){
206 <tr class="text">
207 <td><?php echo htmlspecialchars(xl('Provider Id').':',ENT_QUOTES);?></td>
208 <td><span><?php echo htmlspecialchars($GLOBALS['portal_offsite_providerid'],ENT_QUOTES);?></span></td>
209 </tr>
210 <?php
213 <tr class="text">
214 <td><?php echo htmlspecialchars(xl('User Name').':',ENT_QUOTES);?></td>
215 <td><input type="text" name="uname" value="<?php if($row['portal_username']) echo htmlspecialchars($row['portal_username'],ENT_QUOTES); else echo htmlspecialchars($row['fname'].$row['id'],ENT_QUOTES);?>" size="10" readonly></td>
216 </tr>
217 <tr class="text">
218 <td><?php echo htmlspecialchars(xl('Password').':',ENT_QUOTES);?></td>
219 <?php
220 $pwd = generatePassword();
222 <td><input type="text" name="pwd" id="pwd" value="<?php echo htmlspecialchars($pwd,ENT_QUOTES);?>" size="10"/>
223 </td>
224 <td><a href="#" class="css_button" onclick="top.restoreSession(); javascript:document.location.reload()"><span><?php echo htmlspecialchars(xl('Change'),ENT_QUOTES);?></span></a></td>
225 </tr>
226 <tr class="text">
227 <td><input type="hidden" name="form_save" id="form_save"></td>
228 <td colspan="5" align="center">
229 <a href="#" class="css_button" onclick="return transmit()"><span><?php echo htmlspecialchars(xl('Save'),ENT_QUOTES);?></span></a>
230 <input type="hidden" name="form_cancel" id="form_cancel">
231 <a href="#" class="css_button" onclick="top.restoreSession(); parent.$.fn.fancybox.close();"><span><?php echo htmlspecialchars(xl('Cancel'),ENT_QUOTES);?></span></a>
232 </td>
233 </tr>
234 </table>
235 </form>
236 </body>