7 * @link http://www.open-emr.org
8 * @author Jerry Padgett <sjpadgett@gmail.com>
9 * @copyright Copyright (c) 2016-2019 Jerry Padgett <sjpadgett@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 require_once(dirname(__FILE__
) . '/../../interface/globals.php');
15 use OpenEMR\Common\Crypto\CryptoGen
;
16 use OpenEMR\Common\Logging\EventAuditLogger
;
18 class ApplicationTable
20 public function __construct()
26 * All DB Transactions take place
30 * @param array $params
33 * Logging True / False
34 * @param boolean $error
35 * Error Display True / False
38 public function zQuery($sql, $params = '', $log = false, $error = true)
44 $return = sqlStatement($sql, $params);
46 } catch (Exception
$e) {
48 $this->errorHandler($e, $sql, $params);
53 EventAuditLogger
::instance()->auditSQLEvent($sql, $result, $params);
59 public function getPortalAuditRec($recid)
64 $sql = "Select * From onsite_portal_activity Where id = ?";
65 $return = sqlStatementNoLog($sql, $recid);
67 } catch (Exception
$e) {
68 $this->errorHandler($e, $sql);
70 if ($result === true) {
71 return sqlFetchArray($return);
77 public function getPortalAudit($patientid, $action = 'review', $activity = 'profile', $status = 'waiting', $auditflg = 1, $rtn = 'last', $oelog = true, $error = true)
89 $sql = "Select * From onsite_portal_activity As pa Where pa.patient_id = ? And pa.activity = ? And pa.require_audit = ? " .
90 "And pa.status = ? And pa.pending_action = ? ORDER BY pa.date ASC LIMIT 1";
91 $return = sqlStatementNoLog($sql, $audit);
93 } catch (Exception
$e) {
95 $this->errorHandler($e, $sql, $audit);
100 EventAuditLogger
::instance()->auditSQLEvent($sql, $result, $audit);
103 if ($rtn == 'last') {
104 return sqlFetchArray($return);
110 * Function portalAudit
111 * All Portal audit Transactions log
112 * Hoping to work both ends, patient and user, from one or most two tables
115 * SQL Query Statment for actions will execute sql as normal for cases
116 * user auth is not required.
117 * @param array $params
118 * Parameters for actions
119 * @param array $auditvals
120 * Parameters of audit
121 * @param boolean $log
122 * openemr Logging True / False
123 * @param boolean $error
124 * Error Display True / False
125 * @param type audit array params for portal audits
127 * $audit['patient_id']="";
128 * $audit['activity']="";
129 * $audit['require_audit']="";
130 * $audit['pending_action']="";
131 * $audit['action_taken']="";
132 * $audit['status']="";
133 * $audit['narrative']="";
134 * $audit['table_action']=""; //auth user action sql to run after review
135 * $audit['table_args']=""; //auth user action data to run after review
136 * $audit['action_user']="";
137 * $audit['action_taken_time']="";
138 * $audit['checksum']="";
140 public function portalAudit(string $type = null, string $rec = null, array $auditvals, $oelog = true, $error = true)
148 if ($type != 'insert') {
149 $audit['date'] = $auditvals['date'] ?
$auditvals['date'] : date("Y-m-d H:i:s");
152 $audit['patient_id'] = $auditvals['patient_id'] ?
$auditvals['patient_id'] : $_SESSION['pid'];
153 $audit['activity'] = $auditvals['activity'] ?
$auditvals['activity'] : "";
154 $audit['require_audit'] = $auditvals['require_audit'] ?
$auditvals['require_audit'] : "";
155 $audit['pending_action'] = $auditvals['pending_action'] ?
$auditvals['pending_action'] : "";
156 $audit['action_taken'] = $auditvals['action_taken'] ?
$auditvals['action_taken'] : "";
157 $audit['status'] = $auditvals['status'] ?
$auditvals['status'] : "new";
158 $audit['narrative'] = $auditvals['narrative'] ?
$auditvals['narrative'] : "";
159 $audit['table_action'] = $auditvals['table_action'] ?
$auditvals['table_action'] : "";
160 if ($auditvals['activity'] == 'profile') {
161 $audit['table_args'] = serialize($auditvals['table_args']);
163 $audit['table_args'] = $auditvals['table_args'];
166 $audit['action_user'] = $auditvals['action_user'] ?
$auditvals['action_user'] : "";
167 $audit['action_taken_time'] = $auditvals['action_taken_time'] ?
$auditvals['action_taken_time'] : "";
168 $audit['checksum'] = $auditvals['checksum'] ?
$auditvals['checksum'] : "";
171 if ($type != 'update') {
172 $logsql = "INSERT INTO onsite_portal_activity" .
173 "( date, patient_id, activity, require_audit, pending_action, action_taken, status, narrative," .
174 "table_action, table_args, action_user, action_taken_time, checksum) " .
175 "VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
177 $logsql = "update onsite_portal_activity set date=?, patient_id=?, activity=?, require_audit=?," .
178 " pending_action=?, action_taken=?,status=?, narrative=?, table_action=?, table_args=?," .
179 "action_user=?, action_taken_time=?, checksum=? ";
180 $logsql .= "where id='" . add_escape_custom($rec) . "' And patient_id='" . add_escape_custom($audit['patient_id']) . "'";
183 $return = sqlStatementNoLog($logsql, $audit);
185 } catch (Exception
$e) {
187 $this->errorHandler($e, $logsql, $audit);
192 $this->portalLog('profile audit transaction', $audit['patient_id'], $logsql, $audit, $result, 'See portal audit activity');
193 //auditSQLEvent( $logsql, $result, $audit );
199 public function portalLog($event = '', $patient_id = null, $comments = "", $binds = '', $success = '1', $user_notes = '', $ccda_doc_id = 0)
201 $groupname = $GLOBALS['groupname'] ??
'none';
202 $user = $_SESSION['portal_username'] ??
$_SESSION['authUser'] ??
null;
203 $log_from = isset($_SESSION['portal_username']) ?
'onsite-portal' : 'portal-dashboard';
204 if (!isset($_SESSION['portal_username']) && !isset($_SESSION['authUser'])) {
205 $log_from = 'portal-login';
208 $user_notes .= isset($_SESSION['whereto']) ?
(' Module:' . $_SESSION['whereto']) : "";
210 $processed_binds = "";
211 if (is_array($binds)) {
213 foreach ($binds as $value_bind) {
215 $processed_binds .= "'" . add_escape_custom($value_bind) . "'";
218 $processed_binds .= ",'" . add_escape_custom($value_bind) . "'";
222 if (! empty($processed_binds)) {
223 $processed_binds = "(" . $processed_binds . ")";
224 $comments .= " " . $processed_binds;
228 $this->portalNewEvent($event, $user, $groupname, $success, $comments, $patient_id, $log_from, $user_notes, $ccda_doc_id);
231 * Function errorHandler
232 * All error display and log
233 * Display the Error, Line and File
234 * Same behavior of HelpfulDie fuction in OpenEMR
235 * Path /library/sql.inc.php
239 * @param array $binds
241 public function errorHandler($e, $sql, $binds = '')
243 $trace = $e->getTraceAsString();
244 $nLast = strpos($trace, '[internal function]');
245 $trace = substr($trace, 0, ( $nLast - 3 ));
248 $logMsg .= "\r Exception: " . self
::escapeHtml($e->getMessage());
249 } while ($e = $e->getPrevious());
253 $processedBinds = "";
254 if (is_array($binds)) {
256 foreach ($binds as $valueBind) {
258 $processedBinds .= "'" . $valueBind . "'";
261 $processedBinds .= ",'" . $valueBind . "'";
265 if (! empty($processedBinds)) {
266 $processedBinds = "(" . $processedBinds . ")";
270 echo '<pre><span style="color: red;">';
271 echo 'ERROR : ' . $logMsg;
273 echo 'SQL statement : ' . self
::escapeHtml($sql);
274 echo self
::escapeHtml($processedBinds);
275 echo '</span></pre>';
282 $logMsg .= "\n SQL statement : $sql" . $processedBinds;
283 $logMsg .= "\n $trace";
284 error_log("ERROR: " . htmlspecialchars($logMsg, ENT_QUOTES
), 0);
286 public function escapeHtml($string)
288 return htmlspecialchars($string, ENT_QUOTES
);
291 * Retrive the data format from GLOBALS
293 * @param Date format set in GLOBALS
294 * @return Date format in PHP
296 public function dateFormat($format)
298 if ($format == "0") {
299 $date_format = 'yyyy/mm/dd';
300 } elseif ($format == 1) {
301 $date_format = 'mm/dd/yyyy';
302 } elseif ($format == 2) {
303 $date_format = 'dd/mm/yyyy';
305 $date_format = $format;
311 * fixDate - Date Conversion Between Different Formats
313 * @param String $input_date
314 * Date to be converted
315 * @param String $date_format
318 public function fixDate($input_date, $output_format = null, $input_format = null)
324 $input_date = preg_replace('/T|Z/', ' ', $input_date);
326 $temp = explode(' ', $input_date); // split using space and consider the first portion, in case of date with time
327 $input_date = $temp[0];
329 $output_format = ApplicationTable
::dateFormat($output_format);
330 $input_format = ApplicationTable
::dateFormat($input_format);
332 preg_match("/[^ymd]/", $output_format, $date_seperator_output);
333 $seperator_output = $date_seperator_output[0];
334 $output_date_arr = explode($seperator_output, $output_format);
336 preg_match("/[^ymd]/", $input_format, $date_seperator_input);
337 $seperator_input = $date_seperator_input[0];
338 $input_date_array = explode($seperator_input, $input_format);
340 preg_match("/[^1234567890]/", $input_date, $date_seperator_input);
341 $seperator_input = $date_seperator_input[0];
342 $input_date_arr = explode($seperator_input, $input_date);
344 foreach ($output_date_arr as $key => $format) {
345 $index = array_search($format, $input_date_array);
346 $output_date_arr[$key] = $input_date_arr[$index];
349 $output_date = implode($seperator_output, $output_date_arr);
351 $output_date = $temp[1] ?
$output_date . " " . $temp[1] : $output_date; // append the time, if exists, with the new formatted date
356 * Using generate id function from OpenEMR sql.inc.php library file
357 * @param string $seqname table name containing sequence (default is adodbseq)
358 * @param integer $startID id to start with for a new sequence (default is 1)
359 * @return integer returns the sequence integer
361 public function generateSequenceID()
363 return generate_id();
366 public function portalNewEvent($event, $user, $groupname, $success, $comments = "", $patient_id = null, $log_from = '', $user_notes = "", $ccda_doc_id = 0)
368 EventAuditLogger
::instance()->recordLogItem($success, $event, $user, $groupname, $comments, $patient_id, null, $log_from, null, $ccda_doc_id, $user_notes);