forked from LibreHealthIO/lh-ehr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirect_message_check.inc
485 lines (424 loc) · 15.1 KB
/
direct_message_check.inc
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
/**
* Background receive function for phiMail Direct Messaging service.
*
* This script is called by the background service manager
* at /library/ajax/execute_background_services.php
*
* Copyright (C) 2013 EMR Direct <http://www.emrdirect.com/>
*
* LICENSE: This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
*
* @package LibreEHR
* @author EMR Direct <http://www.emrdirect.com/>
* @link http://librehealth.io
*/
require_once(dirname(__FILE__) . "/log.inc");
require_once(dirname(__FILE__) . "/sql.inc");
require_once(dirname(__FILE__) . "/pnotes.inc");
require_once(dirname(__FILE__) . "/../controllers/C_Document.class.php");
require_once(dirname(__FILE__) . "/documents.php");
require_once(dirname(__FILE__) . "/gprelations.inc.php");
require_once(dirname(__FILE__) . "/classes/class.phpmailer.php");
/**
* Connect to a phiMail Direct Messaging server
*/
function phimail_connect(&$phimail_error) {
if ($GLOBALS['phimail_enable'] == false) {
$phimail_error = 'C1';
return false; //for safety
}
$phimail_server = @parse_url($GLOBALS['phimail_server_address']);
$phimail_username = $GLOBALS['phimail_username'];
$phimail_password = $GLOBALS['phimail_password'];
$phimail_cafile = dirname(__FILE__) . '/../sites/' . $_SESSION['site_id']
. '/documents/phimail_server_pem/phimail_server.pem';
if (!file_exists($phimail_cafile)) $phimail_cafile='';
$phimail_secure = true;
switch ($phimail_server['scheme']) {
case "tcp":
case "http": $server = "tcp://".$phimail_server['host'];
$phimail_secure = false;
break;
case "https": $server = "ssl://" . $phimail_server['host']
. ':' . $phimail_server['port'];
break;
case "ssl":
case "sslv3":
case "tls": $server = $GLOBALS['phimail_server_address'];
break;
default: $phimail_error = 'C2';
return false;
}
if ($phimail_secure) {
$context = stream_context_create();
if ($phimail_cafile != '' &&
(!stream_context_set_option($context, 'ssl', 'verify_peer', true) ||
!stream_context_set_option($context, 'ssl', 'cafile', $phimail_cafile))) {
$phimail_error = 'C3';
return false;
}
$socket_tries = 0;
$fp = false;
while ($socket_tries < 3 && !$fp) {
$socket_tries++;
$fp = @stream_socket_client($server, $err1, $err2, 10,
STREAM_CLIENT_CONNECT, $context);
}
if (!$fp) {
if ($err1 == '111') $err2 = xl('Server may be offline');
if ($err2 == '') $err2 = xl('Connection error');
$phimail_error = "C4 $err1 ($err2)";
}
} else {
$fp = @fsockopen($server,$phimail_server['port']);
}
return $fp;
}
/**
* Connect to a phiMail Direct Messaging server and check for any incoming status
* messages related to previously transmitted messages or any new messages received.
*/
function phimail_check() {
$fp = phimail_connect($err);
if ($fp===false) {
phimail_logit(0,xl('could not connect to server').' '.$err);
return;
}
$phimail_username = $GLOBALS['phimail_username'];
$phimail_password = $GLOBALS['phimail_password'];
$ret = phimail_write_expect_OK($fp,"AUTH $phimail_username $phimail_password\n");
if($ret!==TRUE) {
phimail_logit(0, "authentication error " . $ret);
return;
}
if(!($notifyUsername = $GLOBALS['phimail_notify'])) $notifyUsername='admin'; //fallback
while (1) {
phimail_write($fp,"CHECK\n");
$ret=fgets($fp,512);
if($ret=="NONE\n") { //nothing to process
phimail_close($fp);
phimail_logit(1,"message check completed");
return;
}
else if(substr($ret,0,6)=="STATUS") {
//Format STATUS message-id status-code [additional-information]
$val=explode(" ",trim($ret),4);
$sql='SELECT * from direct_message_log WHERE msg_id = ?';
$res = sqlStatementNoLog($sql,array($val[1]));
if ($res===FALSE) { //database problem
phimail_close($fp);
phimail_logit(0,"database problem");
return;
}
if (($msg=sqlFetchArray($res))===FALSE) {
//no match, so log it and move on (should never happen)
phimail_logit(0,"NO MATCH: ".$ret);
$ret = phimail_write_expect_OK($fp,"OK\n");
if($ret!==TRUE) return; else continue;
}
//if we get here, $msg contains the matching outgoing message record
if($val[2]=='failed') {
$success=0;
$status='F';
} else if ($val[2]=='dispatched') {
$success=1;
$status='D';
} else {
//unrecognized status, log it and move on (should never happen)
$ret = "UNKNOWN STATUS: ".$ret;
$success=0;
$status='U';
}
phimail_logit($success,$ret,$msg['patient_id']);
if (!isset($val[3])) $val[3]="";
$sql = "UPDATE direct_message_log SET status=?, status_ts=NOW(), status_info=? WHERE msg_type='S' AND msg_id=?";
$res = sqlStatementNoLog($sql,array($status,$val[3],$val[1]));
if ($res===FALSE) { //database problem
phimail_close($fp);
phimail_logit(0,"database problem updating: ".$val[1]);
return;
}
if (!$success) {
//notify local user of failure
$sql = "SELECT username FROM users WHERE id = ?";
$res2 = sqlStatementNoLog($sql, array($msg['user_id']));
$fail_user = ($res2 === FALSE || ($user_row = sqlFetchArray($res2)) === FALSE) ?
xl('unknown (see log)') : $user_row['username'];
$fail_notice = xl('Sent by:') . ' ' . $fail_user . '(' . $msg['user_id'] . ') ' . xl('on') . ' ' . $msg['create_ts']
. "\n" . xl('Sent to:') . ' ' . $msg['recipient'] . "\n" . xl('Server message:') . ' ' . $ret;
phimail_notify( xl('Direct Messaging Send Failure.'), $fail_notice);
$pnote_id = addPnote($msg['patient_id'],
xl("FAILURE NOTICE: Direct Message Send Failed.") . "\n\n$fail_notice\n",
0, 1, "Unassigned", $notifyUsername, "", "New", "phimail-service");
}
//done with this status message
$ret = phimail_write_expect_OK($fp,"OK\n");
if($ret!==TRUE) {
phimail_close($fp);
return;
}
}
else if(substr($ret,0,4)=="MAIL") {
$val = explode(" ",trim($ret),5); // MAIL recipient sender #attachments msg-id
$recipient=$val[1];
$sender=$val[2];
$att=(int)$val[3];
$msg_id=$val[4];
//request main message
$ret2 = phimail_write_expect_OK($fp,"SHOW 0\n");
if($ret2!==TRUE) {
phimail_close($fp);
return;
}
//get message headers
$hdrs="";
while (($next_hdr = fgets($fp,1024)) != "\n")
$hdrs .= $next_hdr;
$mime_type=fgets($fp,512);
$mime_info=explode(";",$mime_type);
$mime_type_main=strtolower($mime_info[0]);
//get main message body
$body_len=fgets($fp,256);
$body=phimail_read_blob($fp,$body_len);
if ($body===FALSE) {
phimail_close($fp);
return;
}
$att2=fgets($fp,256);
if($att2!=$att) { //safety for mismatch on attachments
phimail_close($fp);
return;
}
//get attachment info
if($att>0) {
for ($attnum=0;$attnum<$att;$attnum++) {
if( ($attinfo[$attnum]['name']=fgets($fp,1024)) === FALSE
|| ($attinfo[$attnum]['mime']=fgets($fp,1024)) === FALSE
|| ($attinfo[$attnum]['desc']=fgets($fp,1024)) === FALSE) {
phimail_close($fp);
return;
}
}
}
//main part gets stored as document if not plain text content
//(if plain text it will be the body of the final pnote)
$all_doc_ids = array();
$doc_id=0;
$att_detail="";
if ($mime_type_main != "text/plain") {
$name = uniqid("dm-message-") . phimail_extension($mime_type_main);
$doc_id = phimail_store($name,$mime_type_main,$body);
if (!$doc_id) {
phimail_close($fp);
return;
}
$idnum=$doc_id['doc_id'];
$all_doc_ids[] = $idnum;
$url=$doc_id['url'];
$url=substr($url,strrpos($url,"/")+1);
$att_detail = "\n" . xl ("Document") . " $idnum (\"$url\"; $mime_type_main; " .
filesize($body) . " bytes) Main message body";
}
//download and store attachments
for($attnum=0;$attnum<$att;$attnum++) {
$ret2 = phimail_write_expect_OK($fp,"SHOW " . ($attnum+1) . "\n");
if ($ret2!==TRUE) {
phimail_close($fp);
return;
}
//we can ignore next two lines (repeat of name and mime-type)
if ( ($a1=fgets($fp,512))===FALSE || ($a2=fgets($fp,512))===FALSE ) {
phimail_close($fp);
return;
}
$att_len = fgets($fp,256); //length of file
$attdata = phimail_read_blob($fp,$att_len);
if ($attdata===FALSE) {
phimail_close($fp);
return;
}
$attinfo[$attnum]['file']=$attdata;
$req_name = trim($attinfo[$attnum]['name']);
$req_name = (empty($req_name) ? $attdata : "dm-") . $req_name;
$attinfo[$attnum]['mime'] = explode(";",trim($attinfo[$attnum]['mime']));
$attmime = strtolower($attinfo[$attnum]['mime'][0]);
$att_doc_id = phimail_store($req_name, $attmime, $attdata);
if (!$att_doc_id) {
phimail_close($fp);
return;
}
$attinfo[$attnum]['doc_id']=$att_doc_id;
$idnum=$att_doc_id['doc_id'];
$all_doc_ids[] = $idnum;
$url=$att_doc_id['url'];
$url=substr($url,strrpos($url,"/")+1);
$att_detail = $att_detail . "\n" . xl ("Document") . " $idnum (\"$url\"; $attmime; " .
filesize($attdata) . " bytes) " . trim($attinfo[$attnum]['desc']);
}
if ($att_detail != "")
$att_detail = "\n\n" . xl("The following documents were attached to this Direct message:") . $att_detail;
$ret2 = phimail_write_expect_OK($fp,"DONE\n"); //we'll check for failure after logging.
//logging only after succesful download, storage, and acknowledgement of message
$sql = "INSERT INTO direct_message_log (msg_type,msg_id,sender,recipient,status,status_ts,user_id) " .
"VALUES ('R', ?, ?, ?, 'R', NOW(), ?)";
$res = sqlStatementNoLog($sql,array($msg_id,$sender,$recipient,phimail_service_userID()));
phimail_logit(1,$ret);
//alert appointed user about new message
switch($mime_type_main) {
case "text/plain":
$body_text = @file_get_contents($body); //this was not uploaded as a document
unlink($body);
$pnote_id = addPnote(0, xl("Direct Message Received.") . "\n$hdrs\n$body_text$att_detail",
0, 1, "Unassigned", $notifyUsername, "", "New", "phimail-service");
break;
default:
$note = xl("Direct Message Received.") . "\n$hdrs\n"
. xl("Message content is not plain text so it has been stored as a document.") . $att_detail;
$pnote_id = addPnote(0, $note, 0, 1, "Unassigned", $notifyUsername, "", "New", "phimail-service");
break;
}
foreach ($all_doc_ids as $doc_id) setGpRelation(1, $doc_id, 6, $pnote_id);
if ($ret2!==TRUE) {
phimail_close();
return;
}
}
else { //unrecognized or FAIL response
phimail_logit(0, "problem checking messages " . $ret);
phimail_close($fp);
return;
}
}
}
/**
* Helper functions
*/
function phimail_write($fp,$text) {
fwrite($fp,$text);
fflush($fp);
}
function phimail_write_expect_OK($fp,$text) {
phimail_write($fp,$text);
$ret = fgets($fp,256);
if($ret!="OK\n") { //unexpected error
phimail_close($fp);
return $ret;
}
return TRUE;
}
function phimail_close($fp) {
fwrite($fp,"BYE\n");
fflush($fp);
fclose($fp);
}
function phimail_logit($success,$text,$pid=0,$event="direct-message-check") {
newEvent($event,"phimail-service",0,$success,$text,$pid);
}
/**
* Read a blob of data into a local temporary file
* @param $len number of bytes to read
* @return the temp filename, or FALSE if failure
*/
function phimail_read_blob($fp,$len) {
$fpath=$GLOBALS['temporary_files_dir'];
if(!@file_exists($fpath)) {
return FALSE;
}
$name = uniqid("direct-");
$fn = $fpath . "/" . $name . ".dat";
$dup = 1;
while(file_exists($fn)) {
$fn = $fpath . "/" . $name . "." . $dup++ . ".dat";
}
$ff = @fopen ($fn, "w");
if(!$ff) return FALSE;
$bytes_left=$len;
$chunk_size=1024;
while (!feof($fp) && $bytes_left>0) {
if ($bytes_left < $chunk_size ) $chunk_size = $bytes_left;
$chunk = fread($fp,$chunk_size);
if($chunk===FALSE || @fwrite($ff,$chunk)===FALSE) {
@fclose($ff);
@unlink($fn);
return FALSE;
}
$bytes_left -= strlen($chunk);
}
@fclose($ff);
return($fn);
}
/**
* Return a suitable filename extension based on MIME-type
* (very limited, default is .dat)
*/
function phimail_extension($mime) {
$m=explode("/",$mime);
switch($mime) {
case 'text/plain':
return (".txt");
default:
}
switch($m[1]) {
case 'html':
case 'xml':
case 'pdf':
return (".".$m[1]);
default:
return (".dat");
}
}
function phimail_service_userID($name='phimail-service') {
$sql = "SELECT id FROM users WHERE username=?";
if (($r = sqlStatementNoLog($sql,array($name))) === FALSE ||
($u = sqlFetchArray($r)) === FALSE) {
$user=1; //default if we don't have a service user
} else {
$user = $u['id'];
}
return ($user);
}
/**
* Registers an attachment or non-text message file using the existing Document structure
* @return Array(doc_id,URL) of the file as stored in documents table, false = failure
*/
function phimail_store($name,$mime_type,$fn) {
// Collect phimail user id
$user = phimail_service_userID();
// Import the document
$return = addNewDocument($name,$mime_type,$fn,0,filesize($fn),$user,'direct');
// Remove the temporary file
@unlink($fn);
// Return the result
return $return;
}
/**
* Send an error notification or other alert to the notification address specified in globals.
* (notification email function modified from interface/drugs/dispense_drug.php)
* @return true if notification successfully sent, false otherwise
*/
function phimail_notify($subj,$body) {
$recipient = $GLOBALS['practice_return_email_path'];
if (empty($recipient)) return false;
$mail = new PHPMailer();
$mail->SetLanguage("en", $GLOBALS['fileroot'] . "/library/" );
$mail->From = $recipient;
$mail->FromName = 'phiMail Gateway';
$mail->isMail();
$mail->Host = "localhost";
$mail->Mailer = "mail";
$mail->Body = $body;
$mail->Subject = $subj;
$mail->AddAddress($recipient);
return ($mail->Send());
}
?>