7 * For debugging, insert 'dbg(1);' anywhere in the program flow.
8 * dbg(0); will turn it off. Logging is performed at LOGGER_DATA level.
9 * When logging, all binary info is converted to text and html entities are escaped so that
10 * the debugging stream is safe to view within both terminals and web pages.
14 if(! class_exists('dba')) {
19 public $connected = false;
21 function __construct($server,$user,$pass,$db,$install = false) {
22 $server = trim($server);
28 if(strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
29 if(! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
30 notice( sprintf( t('Cannot locate DNS info for database server \'%s\'',$server)));
31 $this->connected = false;
38 $this->db = @new mysqli($server,$user,$pass,$db);
39 if(! mysqli_connect_errno()) {
40 $this->connected = true;
49 public function getdb() {
53 public function q($sql) {
55 if((! $this->db) || (! $this->connected))
58 $result = @$this->db->query($sql);
65 logger('dba: ' . $this->db->error);
69 elseif($result === true)
72 $mesg = $result->num_rows . ' results' . EOL;
74 $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
76 logger('dba: ' . $str );
81 * If dbfail.out exists, we will write any failed calls directly to it,
82 * regardless of any logging that may or may nor be in effect.
83 * These usually indicate SQL syntax errors that need to be resolved.
86 if($result === false) {
87 logger('dba: ' . printable($sql) . ' returned false.');
88 if(file_exists('dbfail.out'))
89 file_put_contents('dbfail.out', printable($sql) . ' returned false' . "\n", FILE_APPEND);
93 if(($result === true) || ($result === false))
97 if($result->num_rows) {
98 while($x = $result->fetch_array(MYSQL_ASSOC))
100 $result->free_result();
104 logger('dba: ' . printable(print_r($r, true)), LOGGER_DATA);
108 public function dbg($dbg) {
112 public function escape($str) {
113 if($this->db && $this->connected)
114 return @$this->db->real_escape_string($str);
117 function __destruct() {
122 if(! function_exists('printable')) {
123 function printable($s) {
124 $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
125 $s = str_replace("\x00",'.',$s);
126 if(x($_SERVER,'SERVER_NAME'))
127 $s = escape_tags($s);
131 // Procedural functions
132 if(! function_exists('dbg')) {
133 function dbg($state) {
139 if(! function_exists('dbesc')) {
140 function dbesc($str) {
142 if($db && $db->connected)
143 return($db->escape($str));
145 return(str_replace("'","\\'",$str));
149 // Function: q($sql,$args);
150 // Description: execute SQL query with printf style args.
151 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
154 if(! function_exists('q')) {
158 $args = func_get_args();
161 if($db && $db->connected) {
162 $ret = $db->q(vsprintf($sql,$args));
168 * This will happen occasionally trying to store the
169 * session data after abnormal program termination
173 logger('dba: no database: ' . print_r($args,true));
180 * Raw db query, no arguments
184 if(! function_exists('dbq')) {
188 if($db && $db->connected)
196 // Caller is responsible for ensuring that any integer arguments to
197 // dbesc_array are actually integers and not malformed strings containing
198 // SQL injection vectors. All integer array elements should be specifically
199 // cast to int to avoid trouble.
202 if(! function_exists('dbesc_array_cb')) {
203 function dbesc_array_cb(&$item, $key) {
205 $item = dbesc($item);
209 if(! function_exists('dbesc_array')) {
210 function dbesc_array(&$arr) {
211 if(is_array($arr) && count($arr)) {
212 array_walk($arr,'dbesc_array_cb');