3 require_once('include/datetime.php');
9 * For debugging, insert 'dbg(1);' anywhere in the program flow.
10 * dbg(0); will turn it off. Logging is performed at LOGGER_DATA level.
11 * When logging, all binary info is converted to text and html entities are escaped so that
12 * the debugging stream is safe to view within both terminals and web pages.
16 if(! class_exists('dba')) {
21 public $mysqli = true;
22 public $connected = false;
23 public $error = false;
25 function __construct($server,$user,$pass,$db,$install = false) {
27 $server = trim($server);
32 if (!(strlen($server) && strlen($user))){
33 $this->connected = false;
39 if(strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
40 if(! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
41 $this->error = sprintf( t('Cannot locate DNS info for database server \'%s\''), $server);
42 $this->connected = false;
49 if(class_exists('mysqli')) {
50 $this->db = @new mysqli($server,$user,$pass,$db);
51 if(! mysqli_connect_errno()) {
52 $this->connected = true;
56 $this->mysqli = false;
57 $this->db = mysql_connect($server,$user,$pass);
58 if($this->db && mysql_select_db($db,$this->db)) {
59 $this->connected = true;
62 if(! $this->connected) {
69 public function getdb() {
73 public function q($sql) {
75 if((! $this->db) || (! $this->connected))
79 $result = @$this->db->query($sql);
81 $result = @mysql_query($sql,$this->db);
87 <<<<<<< HEAD:include/dba.php
88 // If dbfail.out exists, we will write any failed calls directly to it,
89 // regardless of any logging that may or may nor be in effect.
90 // These usually indicate SQL syntax errors that need to be resolved.
91 if(file_exists('dbfail.out')) {
92 file_put_contents('dbfail.out', datetime_convert() . "\n" . $str . "\n", FILE_APPEND);
94 logger('dba: ' . $str );
96 $this->throwOrLog(new RuntimeException('dba: ' . $str));
102 logger('dba: ' . $this->db->error);
103 >>>>>>> upstream/master:include/dba.php
105 elseif(mysql_errno($this->db))
106 logger('dba: ' . mysql_error($this->db));
108 if($result === false)
110 elseif($result === true)
114 $mesg = $result->num_rows . ' results' . EOL;
116 $mesg = mysql_num_rows($result) . ' results' . EOL;
119 $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
121 logger('dba: ' . $str );
125 * If dbfail.out exists, we will write any failed calls directly to it,
126 * regardless of any logging that may or may nor be in effect.
127 * These usually indicate SQL syntax errors that need to be resolved.
130 if($result === false) {
131 logger('dba: ' . printable($sql) . ' returned false.');
132 if(file_exists('dbfail.out'))
133 file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n", FILE_APPEND);
136 if(($result === true) || ($result === false))
141 if($result->num_rows) {
142 while($x = $result->fetch_array(MYSQLI_ASSOC))
144 $result->free_result();
147 <<<<<<< HEAD:include/dba.php
150 * starts a transaction. Transactions need to be finished with
151 * commit() or rollback(). Please mind that the db table engine may
154 public function beginTransaction() {
156 return $this->db->autocommit(false);
158 //no transaction support in mysql module...
159 mysql_query('SET AUTOCOMMIT = 0;', $db);
164 * rollback a transaction. So, rollback anything that was done since the last call
165 * to beginTransaction().
167 public function rollback() {
169 return $this->db->rollback();
171 //no transaction support in mysql module...
172 mysql_query('ROLLBACK;', $db);
174 $this->stopTransaction();
178 * commit a transaction. So, write any query to the database.
180 public function commit() {
182 return $this->db->commit();
184 //no transaction support in mysql module...
185 mysql_query('COMMIT;', $db);
187 $this->stopTransaction();
190 private function stopTransaction() {
192 return $this->db->autocommit(true);
194 //no transaction support in mysql module...
195 mysql_query('SET AUTOCOMMIT = 1;', $db);
199 public function dbg($dbg) {
204 if(mysql_num_rows($result)) {
205 while($x = mysql_fetch_array($result, MYSQL_ASSOC))
207 mysql_free_result($result);
213 logger('dba: ' . printable(print_r($r, true)));
216 >>>>>>> upstream/master:include/dba.php
218 public function dbg($dbg) {
222 public function escape($str) {
223 if($this->db && $this->connected) {
225 return @$this->db->real_escape_string($str);
227 return @mysql_real_escape_string($str,$this->db);
231 function __destruct() {
236 mysql_close($this->db);
240 if(! function_exists('printable')) {
241 function printable($s) {
242 $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
243 $s = str_replace("\x00",'.',$s);
244 if(x($_SERVER,'SERVER_NAME'))
245 $s = escape_tags($s);
249 // Procedural functions
250 if(! function_exists('dbg')) {
251 function dbg($state) {
257 if(! function_exists('dbesc')) {
258 function dbesc($str) {
260 if($db && $db->connected)
261 return($db->escape($str));
263 return(str_replace("'","\\'",$str));
268 // Function: q($sql,$args);
269 // Description: execute SQL query with printf style args.
270 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
273 if(! function_exists('q')) {
277 $args = func_get_args();
280 if($db && $db->connected) {
281 $stmt = vsprintf($sql,$args);
283 logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true));
284 return $db->q($stmt);
289 * This will happen occasionally trying to store the
290 * session data after abnormal program termination
293 logger('dba: no database: ' . print_r($args,true));
300 * Raw db query, no arguments
304 if(! function_exists('dbq')) {
308 if($db && $db->connected)
316 // Caller is responsible for ensuring that any integer arguments to
317 // dbesc_array are actually integers and not malformed strings containing
318 // SQL injection vectors. All integer array elements should be specifically
319 // cast to int to avoid trouble.
322 if(! function_exists('dbesc_array_cb')) {
323 function dbesc_array_cb(&$item, $key) {
325 $item = dbesc($item);
329 if(! function_exists('dbesc_array')) {
330 function dbesc_array(&$arr) {
331 if(is_array($arr) && count($arr)) {
332 array_walk($arr,'dbesc_array_cb');