PageRenderTime 67ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/sms_email_reminder/cron_functions.php

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