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