some portal work
[openemr.git] / library / ADODB_mysqli_log.php
blobc46a9a51aec071b97f7cc115d77121379d099b00
1 <?php
2 /**
3 * ADODB custom wrapper class to support ssl option in main.
5 * @package OpenEMR
6 * @link https://www.open-emr.org
7 * @author Kevin Yeh <kevin.y@integralemr.com>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 use OpenEMR\Common\Logging\EventAuditLogger;
15 class ADODB_mysqli_log extends ADODB_mysqli
17 /**
18 * ADODB Execute function wrapper to ensure proper auditing in OpenEMR.
20 * @param string $sql query
21 * @param array $inputarr binded variables array (optional)
22 * @return boolean returns false if error
24 function Execute($sql, $inputarr = false, $insertNeedReturn = false)
26 $retval= parent::Execute($sql, $inputarr);
27 if ($retval === false) {
28 $outcome = false;
29 // Stash the error into last_mysql_error so it doesn't get clobbered when
30 // we insert into the audit log.
31 $GLOBALS['last_mysql_error']=$this->ErrorMsg();
33 // Last error no
34 $GLOBALS['last_mysql_error_no']=$this->ErrorNo();
35 } else {
36 $outcome = true;
39 // Stash the insert ID into lastidado so it doesn't get clobbered when
40 // we insert into the audit log.
41 if ($insertNeedReturn) {
42 $GLOBALS['lastidado'] = $this->Insert_ID();
44 EventAuditLogger::instance()->auditSQLEvent($sql, $outcome, $inputarr);
45 return $retval;
48 /**
49 * ADODB Execute function wrapper to skip auditing in OpenEMR.
51 * Bypasses the OpenEMR auditing engine.
53 * @param string $sql query
54 * @param array $inputarr binded variables array (optional)
55 * @return boolean returns false if error
57 function ExecuteNoLog($sql, $inputarr = false)
59 return parent::Execute($sql, $inputarr);
63 * ADODB GenID function wrapper to work with OpenEMR.
65 * Need to override to fix a bug where call to GenID was updating
66 * sequences table but always returning a zero with the OpenEMR audit
67 * engine both on and off. Note this bug only appears to occur in recent
68 * php versions on windows. The fix is to use the ExecuteNoLog() function
69 * rather than the Execute() functions within this function (otherwise,
70 * there are no other changes from the original ADODB GenID function).
72 * @param string $seqname table name containing sequence (default is adodbseq)
73 * @param integer $startID id to start with for a new sequence (default is 1)
74 * @return integer returns the sequence integer
76 function GenID($seqname = 'adodbseq', $startID = 1)
78 // post-nuke sets hasGenID to false
79 if (!$this->hasGenID) {
80 return false;
83 $getnext = sprintf($this->_genIDSQL, $seqname);
84 $holdtransOK = $this->_transOK; // save the current status
85 $rs = @$this->ExecuteNoLog($getnext);
86 if (!$rs) {
87 if ($holdtransOK) {
88 $this->_transOK = true; //if the status was ok before reset
91 $u = strtoupper($seqname);
92 $this->ExecuteNoLog(sprintf($this->_genSeqSQL, $seqname));
93 $cnt = $this->GetOne(sprintf($this->_genSeqCountSQL, $seqname));
94 if (!$cnt) {
95 $this->ExecuteNoLog(sprintf($this->_genSeq2SQL, $seqname, $startID-1));
98 $rs = $this->ExecuteNoLog($getnext);
101 if ($rs) {
102 $this->genID = mysqli_insert_id($this->_connectionID);
103 $rs->Close();
104 } else {
105 $this->genID = 0;
108 return $this->genID;
111 // ADODB _connect function wrapper to work with OpenEMR
112 // Needed to do this to add support for mysql ssl.
113 // (so only have added the mysqli_ssl_set stuff)
114 // returns true or false
115 // To add: parameter int $port,
116 // parameter string $socket
117 function _connect(
118 $argHostname = null,
119 $argUsername = null,
120 $argPassword = null,
121 $argDatabasename = null,
122 $persist = false
124 if (!extension_loaded("mysqli")) {
125 return null;
127 $this->_connectionID = @mysqli_init();
129 if (is_null($this->_connectionID)) {
130 // mysqli_init only fails if insufficient memory
131 if ($this->debug) {
132 ADOConnection::outp("mysqli_init() failed : " . $this->ErrorMsg());
134 return false;
137 I suggest a simple fix which would enable adodb and mysqli driver to
138 read connection options from the standard mysql configuration file
139 /etc/my.cnf - "Bastien Duclaux" <bduclaux#yahoo.com>
141 foreach ($this->optionFlags as $arr) {
142 mysqli_options($this->_connectionID, $arr[0], $arr[1]);
145 //http ://php.net/manual/en/mysqli.persistconns.php
146 if ($persist && PHP_VERSION > 5.2 && strncmp($argHostname, 'p:', 2) != 0) {
147 $argHostname = 'p:'.$argHostname;
150 //Below was added by OpenEMR to support mysql ssl
151 // Note there is really weird behavior where the paths to certificates do not work if within a variable.
152 // (super odd which is why have 2 different mysqli_ssl_set commands as a work around)
153 if (defined('MYSQLI_CLIENT_SSL') && $this->clientFlags == MYSQLI_CLIENT_SSL) {
154 if (file_exists($GLOBALS['OE_SITE_DIR'] . "/documents/certificates/mysql-key") &&
155 file_exists($GLOBALS['OE_SITE_DIR'] . "/documents/certificates/mysql-cert")) {
156 // with client side certificate/key
157 mysqli_ssl_set(
158 $this->_connectionID,
159 "${GLOBALS['OE_SITE_DIR']}/documents/certificates/mysql-key",
160 "${GLOBALS['OE_SITE_DIR']}/documents/certificates/mysql-cert",
161 "${GLOBALS['OE_SITE_DIR']}/documents/certificates/mysql-ca",
162 null,
163 null
165 } else {
166 // without client side certificate/key
167 mysqli_ssl_set(
168 $this->_connectionID,
169 null,
170 null,
171 "${GLOBALS['OE_SITE_DIR']}/documents/certificates/mysql-ca",
172 null,
173 null
178 #if (!empty($this->port)) $argHostname .= ":".$this->port;
179 $ok = mysqli_real_connect(
180 $this->_connectionID,
181 $argHostname,
182 $argUsername,
183 $argPassword,
184 $argDatabasename,
185 # PHP7 compat: port must be int. Use default port if cast yields zero
186 (int)$this->port != 0 ? (int)$this->port : 3306,
187 $this->socket,
188 $this->clientFlags
191 if ($ok) {
192 if ($argDatabasename) {
193 return $this->SelectDB($argDatabasename);
195 return true;
196 } else {
197 if ($this->debug) {
198 ADOConnection::outp("Could't connect : " . $this->ErrorMsg());
200 $this->_connectionID = null;
201 return false;