Implement direct message receive and background services manager, take 1.
[openemr.git] / modules / sms_email_reminder / cron_functions.php
blob6a2a32de7ce517ebdde7a24c0c8c46a5e650f2e3
1 <?php
2 ////////////////////////////////////////////////////////////////////
3 // CRON FUNCTIONS - to use with cron_sms and cron_email backend
4 // scripts to notify events
5 ////////////////////////////////////////////////////////////////////
7 // larry :: somne global to be defined here
8 global $smsgateway_info;
9 global $patient_info;
10 global $data_info;
12 global $SMS_NOTIFICATION_HOUR;
13 global $EMAIL_NOTIFICATION_HOUR;
15 ////////////////////////////////////////////////////////////////////
16 // Function: cron_SendMail
17 // Purpose: send mail
18 // Input: to, subject, email body and from
19 // Output: status - if sent or not
20 ////////////////////////////////////////////////////////////////////
21 function cron_SendMail( $to, $subject, $vBody, $from )
23 // check if smtp globals set
24 if( $GLOBALS['smtp_host_name'] == '' )
26 // larry :: debug
27 //echo "\nDEBUG :: use mail method\n";
29 // larry :: add cc/bcc - bot used ?
30 $cc = "";
31 $bcc = "";
32 $format = 0;
34 //echo "function called";exit;
35 if( strlen( $format )==0 ) $format="text/html";
36 $headers = "MIME-Version: 1.0\r\n";
37 $headers .= "Content-type: ". $format ."; charset=iso-8859-1\r\n";
39 // additional headers
40 $headers .= "From: $from\r\n";
41 if( strlen($cc)>5 ) $headers .= "Cc: $cc\r\n";
42 if( strlen($bcc)>5 ) $headers .= "Bcc: $bcc\r\n";
43 $cnt = "";
44 $cnt .= "\nHeaders : ".$headers;
45 $cnt .= "\nDate Time :". date("d M, Y h:i:s");
46 $cnt .= "\nTo : ".$to;
47 $cnt .= "\nSubject : ".$subject;
48 $cnt .= "\nBody : \n".$vBody."\n";
50 if(1)
52 //WriteLog($cnt);
54 $mstatus = true;
55 $mstatus = @mail( $to, $subject, $vBody, $headers );
56 // larry :: debug
57 //echo "\nDEBUG :email: send email from=".$from." to=".$to." sbj=".$subject." body=".$vBody." head=".$headers."\n";
58 //echo "\nDEBUG :email: send status=".$mstatus."\n";
59 } else
61 // larry :: debug
62 //echo "\nDEBUG :: use smtp method\n";
64 if( !class_exists( "smtp_class" ) )
66 include("../../library/classes/smtp/smtp.php");
67 include("../../library/classes/smtp/sasl.php");
70 $strFrom = $from;
71 $sender_line=__LINE__;
72 $strTo = $to;
73 $recipient_line=__LINE__;
74 if( strlen( $strFrom ) == 0 ) return( false );
75 if( strlen( $strTo ) == 0 ) return( false );
77 //if( !$smtp )
78 $smtp=new smtp_class;
80 $smtp->host_name = $GLOBALS['smtp_host_name'];
81 $smtp->host_port = $GLOBALS['smtp_host_port'];
82 $smtp->ssl = $GLOBALS['smtp_use_ssl'];
83 $smtp->localhost = $GLOBALS['smtp_localhost'];
84 $smtp->direct_delivery = 0;
85 $smtp->timeout = 10;
86 $smtp->data_timeout = 0;
88 $smtp->debug = 1;
89 $smtp->html_debug = 0;
90 $smtp->pop3_auth_host = "";
92 $smtp->user = $GLOBALS['smtp_auth_user'];
93 $smtp->password = $GLOBALS['smtp_auth_pass'];
95 $smtp->realm = "";
96 // Workstation name for NTLM authentication
97 $smtp->workstation = "";
98 // Specify a SASL authentication method like LOGIN, PLAIN, CRAM-MD5, NTLM, etc..
99 // Leave it empty to make the class negotiate if necessary
100 $smtp->authentication_mechanism = "";
102 // If you need to use the direct delivery mode and this is running under
103 // Windows or any other platform
104 if($smtp->direct_delivery)
106 if(!function_exists("GetMXRR"))
108 $_NAMESERVERS=array();
109 include("getmxrr.php");
113 if( $smtp->SendMessage(
114 $strFrom,
115 array( $strTo ),
116 array(
117 "From: $strFrom",
118 "To: $strTo",
119 "Subject: $subject",
120 "Date Time :". date("d M, Y h:i:s")
122 $vBody ) )
124 echo "Message sent to $to OK.\n";
125 $mstatus = true;
126 } else
128 echo "Cound not send the message to $to.\nError: ".$smtp->error."\n";
129 $mstatus = false;
132 unset( $smtp );
135 return $mstatus;
138 ////////////////////////////////////////////////////////////////////
139 // Function: WriteLog
140 // Purpose: written log into file
141 ////////////////////////////////////////////////////////////////////
142 function WriteLog( $data )
144 global $log_folder_path;
146 $filename = $log_folder_path . "/cronlog_".date("Ymd").".html";
147 //echo $filename;exit;
148 if (!$fp = fopen($filename, 'a'))
150 print "Cannot open file ($filename)";
151 exit;
154 $sdata = "\n====================================================================\n";
156 if (!fwrite($fp, $sdata.$data.$sdata))
158 print "Cannot write to file ($filename)";
159 exit;
162 fclose($fp);
165 ////////////////////////////////////////////////////////////////////
166 // define my_print_r - used for debuging - if not defined
167 ////////////////////////////////////////////////////////////////////
168 if( !function_exists( 'my_print_r' ) )
170 function my_print_r($data)
172 echo "<pre>";print_r($data);echo "</pre>";
176 ////////////////////////////////////////////////////////////////////
177 // Function: cron_SendSMS
178 // Purpose: send sms
179 ////////////////////////////////////////////////////////////////////
180 function cron_SendSMS( $to, $subject, $vBody, $from )
182 global $mysms;
183 $cnt = "";
184 $cnt .= "\nDate Time :". date("d M, Y h:i:s");
185 $cnt .= "\nTo : ".$to;
186 $cnt .= "\From : ".$from;
187 $cnt .= "\nSubject : ".$subject;
188 $cnt .= "\nBody : \n".$vBody."\n";
189 if(1)
191 //WriteLog($cnt);
193 $mstatus = true;
194 // larry :: todo - find out about the billing inclusion ?
195 // $mysms->getbalance();
196 // $mysms->token_pay("1234567890123456"); //spend voucher with SMS credits
197 $mysms->send( $to, $from, $vBody );
198 return $mstatus;
201 ////////////////////////////////////////////////////////////////////
202 // Function: cron_updateentry
203 // Purpose: update status yes if alert send to patient
204 ////////////////////////////////////////////////////////////////////
205 function cron_updateentry($type,$pid,$pc_eid)
207 // larry :: this was commented - i remove comment - what it means * in this field ?
208 //$set = " pc_apptstatus='*',"; - in this prev version there was a comma - somthing to follow ?
209 //$set = " pc_apptstatus='*' ";
211 //$query="update openemr_postcalendar_events set $set ";
212 $query = "update openemr_postcalendar_events set ";
214 // larry :: and here again same story - this time for sms pc_sendalertsms - no such field in the table
215 if($type=='SMS')
216 $query.=" pc_sendalertsms='YES' ";
217 else
218 $query.=" pc_sendalertemail='YES' ";
220 $query .=" where pc_pid='$pid' and pc_eid='$pc_eid' ";
221 //echo "<br>".$query;
222 $db_sql = (sqlStatement($query));
225 ////////////////////////////////////////////////////////////////////
226 // Function: cron_getAlertpatientData
227 // Purpose: get patient data for send to alert
228 ////////////////////////////////////////////////////////////////////
229 function cron_getAlertpatientData( $type )
231 // larry :: move this at the top - not in the function body
232 global $SMS_NOTIFICATION_HOUR,$EMAIL_NOTIFICATION_HOUR;
233 // larry :: end commment
236 //$ssql .= " and ((ope.pc_eventDate='$check_date') OR ('$check_date' BETWEEN ope.pc_eventDate AND ope.pc_endDate)) ";
237 if($type=='SMS')
239 // larry :: remove ope.pc_sendalertemail='No' - nothing like it in the calendar
240 $ssql = " and pd.hipaa_allowsms='YES' and pd.phone_cell<>'' and ope.pc_sendalertsms='NO' ";
241 // $ssql = " and pd.hipaa_allowsms='YES' and pd.phone_cell<>'' ";
243 $check_date = date("Y-m-d", mktime(date("h")+$SMS_NOTIFICATION_HOUR, 0, 0, date("m"), date("d"), date("Y")));
244 }else
246 // larry :: remove ope.pc_sendalertemail='No' - nothing like it in the calendar
247 $ssql = " and pd.hipaa_allowemail='YES' and pd.email<>'' and ope.pc_sendalertemail='NO' ";
248 //$ssql = " and pd.hipaa_allowemail='YES' and pd.email<>'' ";
250 $check_date = date("Y-m-d", mktime(date("h")+$EMAIL_NOTIFICATION_HOUR, 0, 0, date("m"), date("d"), date("Y")));
253 $patient_field = "pd.pid,pd.title,pd.fname,pd.lname,pd.mname,pd.phone_cell,pd.email,pd.hipaa_allowsms,pd.hipaa_allowemail,";
254 $ssql .= " and (ope.pc_eventDate='$check_date')";
255 // larry :: add condition if remnder was already sent
256 // $ssql .= " and (ope.pc_apptstatus != '*' ) ";
258 $query = "select $patient_field pd.pid,ope.pc_eid,ope.pc_pid,ope.pc_title,
259 ope.pc_hometext,ope.pc_eventDate,ope.pc_endDate,
260 ope.pc_duration,ope.pc_alldayevent,ope.pc_startTime,ope.pc_endTime
261 from
262 openemr_postcalendar_events as ope ,patient_data as pd
263 where
264 ope.pc_pid=pd.pid $ssql
265 order by
266 ope.pc_eventDate,ope.pc_endDate,pd.pid";
268 //echo "<br>".$query;
270 $db_patient = (sqlStatement($query));
271 $patient_array = array();
272 $cnt=0;
273 while ($prow = sqlFetchArray($db_patient))
275 $patient_array[$cnt] = $prow;
276 $cnt++;
278 return $patient_array;
281 ////////////////////////////////////////////////////////////////////
282 // Function: cron_getNotificationData
283 // Purpose: get alert notification data
284 ////////////////////////////////////////////////////////////////////
285 function cron_getNotificationData($type)
287 // larry :: pre populate array fields
288 //$db_email_msg['notification_id'] = '';
289 //$db_email_msg['sms_gateway_type'] = '';
291 $query = "select * from automatic_notification where type='$type' ";
292 //echo "<br>".$query;
293 $db_email_msg = sqlFetchArray(sqlStatement($query));
294 return $db_email_msg;
297 ////////////////////////////////////////////////////////////////////
298 // Function: cron_InsertNotificationLogEntry
299 // Purpose: insert log entry in table
300 ////////////////////////////////////////////////////////////////////
301 function cron_InsertNotificationLogEntry($type,$prow,$db_email_msg)
303 global $SMS_GATEWAY_USENAME,$SMS_GATEWAY_PASSWORD,$SMS_GATEWAY_APIKEY;
304 if( $type=='SMS' )
305 $smsgateway_info = $db_email_msg['sms_gateway_type']."|||".$SMS_GATEWAY_USENAME."|||".$SMS_GATEWAY_PASSWORD."|||".$SMS_GATEWAY_APIKEY;
306 else
307 $smsgateway_info = $db_email_msg['email_sender']."|||".$db_email_msg['email_subject'];
309 $patient_info = $prow['title']." ".$prow['fname']." ".$prow['mname']." ".$prow['lname']."|||".$prow['phone_cell']."|||".$prow['email'];
310 $data_info = $prow['pc_eventDate']."|||".$prow['pc_endDate']."|||".$prow['pc_startTime']."|||".$prow['pc_endTime'];
312 $sql_loginsert = "INSERT INTO `notification_log` ( `iLogId` , `pid` , `pc_eid` , `sms_gateway_type` , `message` , `email_sender` , `email_subject` , `type` , `patient_info` , `smsgateway_info` , `pc_eventDate` , `pc_endDate` , `pc_startTime` , `pc_endTime` , `dSentDateTime` ) VALUES ";
313 $sql_loginsert .= "(NULL , '$prow[pid]', '$prow[pc_eid]', '$db_email_msg[sms_gateway_type]', '$db_email_msg[message]', '$db_email_msg[email_sender]', '$db_email_msg[email_subject]', '$db_email_msg[type]', '$patient_info', '$smsgateway_info', '$prow[pc_eventDate]', '$prow[pc_endDate]', '$prow[pc_startTime]', '$prow[pc_endTime]', '".date("Y-m-d H:i:s")."')";
314 $db_loginsert = ( sqlStatement( $sql_loginsert ) );
317 ////////////////////////////////////////////////////////////////////
318 // Function: cron_setmessage
319 // Purpose: set the message
320 ////////////////////////////////////////////////////////////////////
321 function cron_setmessage($prow,$db_email_msg)
323 // larry :: debug
324 //echo "\nDEBUG :cron_setmessage: set message ".$prow['title']." ".$prow['fname']." ".$prow['mname']." ".$prow['lname']."\n";
326 $NAME = $prow['title']." ".$prow['fname']." ".$prow['mname']." ".$prow['lname'];
327 //echo "DEBUG :1: name=".$NAME."\n";
329 $PROVIDER = $db_email_msg['provider_name'];
330 $DATE = $prow['pc_eventDate'];
331 $STARTTIME = $prow['pc_startTime'];
332 $ENDTIME = $prow['pc_endTime'];
333 $find_array = array("***NAME***","***PROVIDER***","***DATE***","***STARTTIME***","***ENDTIME***");
334 $replare_array = array($NAME,$PROVIDER,$DATE,$STARTTIME,$ENDTIME);
335 $message = str_replace($find_array,$replare_array,$db_email_msg['message']);
336 // larry :: debug
337 //echo "DEBUG :2: msg=".$message."\n";
339 return $message;
342 ////////////////////////////////////////////////////////////////////
343 // Function: cron_GetNotificationSettings
344 // Purpose: get notification settings
345 ////////////////////////////////////////////////////////////////////
346 function cron_GetNotificationSettings( )
348 $strQuery = "select * from notification_settings where type='SMS/Email Settings'";
349 $vectNotificationSettings = sqlFetchArray( sqlStatement( $strQuery ) );
351 return( $vectNotificationSettings );