Updated translations (#1517)
[openemr.git] / modules / sms_email_reminder / cron_functions.php
blobd655e83d1850f24dc61f4c21d69f1f9b25b93ffc
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'] == '') {
25 // larry :: debug
26 //echo "\nDEBUG :: use mail method\n";
28 // larry :: add cc/bcc - bot used ?
29 $cc = "";
30 $bcc = "";
31 $format = ""; // mdsupport - replaces 0 which causes gmail formatting / display problems.
33 //echo "function called";exit;
34 if (strlen($format)==0) {
35 $format="text/html";
38 $headers = "MIME-Version: 1.0\r\n";
39 $headers .= "Content-type: ". $format ."; charset=iso-8859-1\r\n";
41 // additional headers
42 $headers .= "From: $from\r\n";
43 if (strlen($cc)>5) {
44 $headers .= "Cc: $cc\r\n";
47 if (strlen($bcc)>5) {
48 $headers .= "Bcc: $bcc\r\n";
51 $cnt = "";
52 $cnt .= "\nHeaders : ".$headers;
53 $cnt .= "\nDate Time :". date("d M, Y h:i:s");
54 $cnt .= "\nTo : ".$to;
55 $cnt .= "\nSubject : ".$subject;
56 $cnt .= "\nBody : \n".$vBody."\n";
58 if (1) {
59 //WriteLog($cnt);
62 $mstatus = true;
63 $mstatus = @mail($to, $subject, $vBody, $headers);
64 // larry :: debug
65 //echo "\nDEBUG :email: send email from=".$from." to=".$to." sbj=".$subject." body=".$vBody." head=".$headers."\n";
66 //echo "\nDEBUG :email: send status=".$mstatus."\n";
67 } else {
68 // larry :: debug
69 //echo "\nDEBUG :: use smtp method\n";
71 if (!class_exists("smtp_class")) {
72 include("../../library/classes/smtp/smtp.php");
73 include("../../library/classes/smtp/sasl.php");
76 $strFrom = $from;
77 $sender_line=__LINE__;
78 $strTo = $to;
79 $recipient_line=__LINE__;
80 if (strlen($strFrom) == 0) {
81 return( false );
84 if (strlen($strTo) == 0) {
85 return( false );
88 //if( !$smtp )
89 $smtp=new smtp_class;
91 $smtp->host_name = $GLOBALS['smtp_host_name'];
92 $smtp->host_port = $GLOBALS['smtp_host_port'];
93 $smtp->ssl = $GLOBALS['smtp_use_ssl'];
94 $smtp->localhost = $GLOBALS['smtp_localhost'];
95 $smtp->direct_delivery = 0;
96 $smtp->timeout = 10;
97 $smtp->data_timeout = 0;
99 $smtp->debug = 1;
100 $smtp->html_debug = 0;
101 $smtp->pop3_auth_host = "";
103 $smtp->user = $GLOBALS['smtp_auth_user'];
104 $smtp->password = $GLOBALS['smtp_auth_pass'];
106 $smtp->realm = "";
107 // Workstation name for NTLM authentication
108 $smtp->workstation = "";
109 // Specify a SASL authentication method like LOGIN, PLAIN, CRAM-MD5, NTLM, etc..
110 // Leave it empty to make the class negotiate if necessary
111 $smtp->authentication_mechanism = "";
113 // If you need to use the direct delivery mode and this is running under
114 // Windows or any other platform
115 if ($smtp->direct_delivery) {
116 if (!function_exists("GetMXRR")) {
117 $_NAMESERVERS=array();
118 include("getmxrr.php");
122 if ($smtp->SendMessage(
123 $strFrom,
124 array( $strTo ),
125 array(
126 "From: $strFrom",
127 "To: $strTo",
128 "Subject: $subject",
129 "Date Time :". date("d M, Y h:i:s")
131 $vBody
132 ) ) {
133 echo "Message sent to $to OK.\n";
134 $mstatus = true;
135 } else {
136 echo "Cound not send the message to $to.\nError: ".$smtp->error."\n";
137 $mstatus = false;
140 unset($smtp);
143 return $mstatus;
146 ////////////////////////////////////////////////////////////////////
147 // Function: WriteLog
148 // Purpose: written log into file
149 ////////////////////////////////////////////////////////////////////
150 function WriteLog($data)
152 global $log_folder_path;
154 $filename = $log_folder_path . "/cronlog_".date("Ymd").".html";
155 //echo $filename;exit;
156 if (!$fp = fopen($filename, 'a')) {
157 print "Cannot open file ($filename)";
158 exit;
161 $sdata = "\n====================================================================\n";
163 if (!fwrite($fp, $sdata.$data.$sdata)) {
164 print "Cannot write to file ($filename)";
165 exit;
168 fclose($fp);
171 ////////////////////////////////////////////////////////////////////
172 // define my_print_r - used for debuging - if not defined
173 ////////////////////////////////////////////////////////////////////
174 if (!function_exists('my_print_r')) {
175 function my_print_r($data)
177 echo "<pre>";
178 print_r($data);
179 echo "</pre>";
183 ////////////////////////////////////////////////////////////////////
184 // Function: cron_SendSMS
185 // Purpose: send sms
186 ////////////////////////////////////////////////////////////////////
187 function cron_SendSMS($to, $subject, $vBody, $from)
189 global $mysms;
190 $cnt = "";
191 $cnt .= "\nDate Time :". date("d M, Y h:i:s");
192 $cnt .= "\nTo : ".$to;
193 $cnt .= "\From : ".$from;
194 $cnt .= "\nSubject : ".$subject;
195 $cnt .= "\nBody : \n".$vBody."\n";
196 if (1) {
197 //WriteLog($cnt);
200 $mstatus = true;
201 // larry :: todo - find out about the billing inclusion ?
202 // $mysms->getbalance();
203 // $mysms->token_pay("1234567890123456"); //spend voucher with SMS credits
204 $mysms->send($to, $from, $vBody);
205 return $mstatus;
208 ////////////////////////////////////////////////////////////////////
209 // Function: cron_updateentry
210 // Purpose: update status yes if alert send to patient
211 ////////////////////////////////////////////////////////////////////
212 function cron_updateentry($type, $pid, $pc_eid)
214 // larry :: this was commented - i remove comment - what it means * in this field ?
215 //$set = " pc_apptstatus='*',"; - in this prev version there was a comma - somthing to follow ?
216 //$set = " pc_apptstatus='*' ";
218 //$query="update openemr_postcalendar_events set $set ";
219 $query = "update openemr_postcalendar_events set ";
221 // larry :: and here again same story - this time for sms pc_sendalertsms - no such field in the table
222 if ($type=='SMS') {
223 $query.=" pc_sendalertsms='YES' ";
224 } else {
225 $query.=" pc_sendalertemail='YES' ";
228 $query .=" where pc_pid='$pid' and pc_eid='$pc_eid' ";
229 //echo "<br>".$query;
230 $db_sql = (sqlStatement($query));
233 ////////////////////////////////////////////////////////////////////
234 // Function: cron_getAlertpatientData
235 // Purpose: get patient data for send to alert
236 ////////////////////////////////////////////////////////////////////
237 function cron_getAlertpatientData($type)
239 // larry :: move this at the top - not in the function body
240 global $SMS_NOTIFICATION_HOUR,$EMAIL_NOTIFICATION_HOUR;
241 // larry :: end commment
244 //$ssql .= " and ((ope.pc_eventDate='$check_date') OR ('$check_date' BETWEEN ope.pc_eventDate AND ope.pc_endDate)) ";
245 if ($type=='SMS') {
246 // larry :: remove ope.pc_sendalertemail='No' - nothing like it in the calendar
247 $ssql = " and pd.hipaa_allowsms='YES' and pd.phone_cell<>'' and ope.pc_sendalertsms='NO' ";
248 // $ssql = " and pd.hipaa_allowsms='YES' and pd.phone_cell<>'' ";
250 $check_date = date("Y-m-d", mktime(date("h")+$SMS_NOTIFICATION_HOUR, 0, 0, date("m"), date("d"), date("Y")));
251 } else {
252 // larry :: remove ope.pc_sendalertemail='No' - nothing like it in the calendar
253 $ssql = " and pd.hipaa_allowemail='YES' and pd.email<>'' and ope.pc_sendalertemail='NO' ";
254 //$ssql = " and pd.hipaa_allowemail='YES' and pd.email<>'' ";
256 $check_date = date("Y-m-d", mktime(date("h")+$EMAIL_NOTIFICATION_HOUR, 0, 0, date("m"), date("d"), date("Y")));
259 $patient_field = "pd.pid,pd.title,pd.fname,pd.lname,pd.mname,pd.phone_cell,pd.email,pd.hipaa_allowsms,pd.hipaa_allowemail,";
260 $ssql .= " and (ope.pc_eventDate='$check_date')";
261 // larry :: add condition if remnder was already sent
262 // $ssql .= " and (ope.pc_apptstatus != '*' ) ";
264 $query = "select $patient_field pd.pid,ope.pc_eid,ope.pc_pid,ope.pc_title,
265 ope.pc_hometext,ope.pc_eventDate,ope.pc_endDate,
266 ope.pc_duration,ope.pc_alldayevent,ope.pc_startTime,ope.pc_endTime
267 from
268 openemr_postcalendar_events as ope ,patient_data as pd
269 where
270 ope.pc_pid=pd.pid $ssql
271 order by
272 ope.pc_eventDate,ope.pc_endDate,pd.pid";
274 //echo "<br>".$query;
276 $db_patient = (sqlStatement($query));
277 $patient_array = array();
278 $cnt=0;
279 while ($prow = sqlFetchArray($db_patient)) {
280 $patient_array[$cnt] = $prow;
281 $cnt++;
284 return $patient_array;
287 ////////////////////////////////////////////////////////////////////
288 // Function: cron_getNotificationData
289 // Purpose: get alert notification data
290 ////////////////////////////////////////////////////////////////////
291 function cron_getNotificationData($type)
293 // larry :: pre populate array fields
294 //$db_email_msg['notification_id'] = '';
295 //$db_email_msg['sms_gateway_type'] = '';
297 $query = "select * from automatic_notification where type='$type' ";
298 //echo "<br>".$query;
299 $db_email_msg = sqlFetchArray(sqlStatement($query));
300 return $db_email_msg;
303 ////////////////////////////////////////////////////////////////////
304 // Function: cron_InsertNotificationLogEntry
305 // Purpose: insert log entry in table
306 ////////////////////////////////////////////////////////////////////
307 function cron_InsertNotificationLogEntry($type, $prow, $db_email_msg)
309 global $SMS_GATEWAY_USENAME,$SMS_GATEWAY_PASSWORD,$SMS_GATEWAY_APIKEY;
310 if ($type=='SMS') {
311 $smsgateway_info = $db_email_msg['sms_gateway_type']."|||".$SMS_GATEWAY_USENAME."|||".$SMS_GATEWAY_PASSWORD."|||".$SMS_GATEWAY_APIKEY;
312 } else {
313 $smsgateway_info = $db_email_msg['email_sender']."|||".$db_email_msg['email_subject'];
316 $patient_info = $prow['title']." ".$prow['fname']." ".$prow['mname']." ".$prow['lname']."|||".$prow['phone_cell']."|||".$prow['email'];
317 $data_info = $prow['pc_eventDate']."|||".$prow['pc_endDate']."|||".$prow['pc_startTime']."|||".$prow['pc_endTime'];
319 $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 ";
320 $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")."')";
321 $db_loginsert = ( sqlStatement($sql_loginsert) );
324 ////////////////////////////////////////////////////////////////////
325 // Function: cron_setmessage
326 // Purpose: set the message
327 ////////////////////////////////////////////////////////////////////
328 function cron_setmessage($prow, $db_email_msg)
330 // larry :: debug
331 //echo "\nDEBUG :cron_setmessage: set message ".$prow['title']." ".$prow['fname']." ".$prow['mname']." ".$prow['lname']."\n";
333 $NAME = $prow['title']." ".$prow['fname']." ".$prow['mname']." ".$prow['lname'];
334 //echo "DEBUG :1: name=".$NAME."\n";
336 $PROVIDER = $db_email_msg['provider_name'];
337 $dtWrk = strtotime($prow['pc_eventDate'].' '.$prow['pc_startTime']);
338 $DATE = date('l F j, Y', $dtWrk);
339 $STARTTIME = date('g:i A', $dtWrk);
340 $ENDTIME = $prow['pc_endTime'];
341 $find_array = array("***NAME***","***PROVIDER***","***DATE***","***STARTTIME***","***ENDTIME***");
342 $replare_array = array($NAME,$PROVIDER,$DATE,$STARTTIME,$ENDTIME);
343 $message = str_replace($find_array, $replare_array, $db_email_msg['message']);
344 // larry :: debug
345 //echo "DEBUG :2: msg=".$message."\n";
347 return $message;
350 ////////////////////////////////////////////////////////////////////
351 // Function: cron_GetNotificationSettings
352 // Purpose: get notification settings
353 ////////////////////////////////////////////////////////////////////
354 function cron_GetNotificationSettings()
356 $strQuery = "select * from notification_settings where type='SMS/Email Settings'";
357 $vectNotificationSettings = sqlFetchArray(sqlStatement($strQuery));
359 return( $vectNotificationSettings );