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 $this->db = @new mysqli($server,$user,$pass,$db);
23 if(! mysqli_connect_errno()) {
24 $this->connected = true;
33 public function getdb() {
37 public function q($sql) {
39 if((! $this->db) || (! $this->connected))
42 $result = @$this->db->query($sql);
49 logger('dba: ' . $this->db->error);
53 elseif($result === true)
56 $mesg = $result->num_rows . ' results' . EOL;
58 $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
60 logger('dba: ' . $str );
65 * If dbfail.out exists, we will write any failed calls directly to it,
66 * regardless of any logging that may or may nor be in effect.
67 * These usually indicate SQL syntax errors that need to be resolved.
70 if($result === false) {
71 logger('dba: ' . printable($sql) . ' returned false.');
72 if(file_exists('dbfail.out'))
73 file_put_contents('dbfail.out', printable($sql) . ' returned false' . "\n", FILE_APPEND);
77 if(($result === true) || ($result === false))
81 if($result->num_rows) {
82 while($x = $result->fetch_array(MYSQL_ASSOC))
84 $result->free_result();
88 logger('dba: ' . printable(print_r($r, true)), LOGGER_DATA);
92 public function dbg($dbg) {
96 public function escape($str) {
97 if($this->db && $this->connected)
98 return @$this->db->real_escape_string($str);
101 function __destruct() {
106 if(! function_exists('printable')) {
107 function printable($s) {
108 $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
109 $s = str_replace("\x00",'.',$s);
110 if(x($_SERVER,'SERVER_NAME'))
111 $s = escape_tags($s);
115 // Procedural functions
116 if(! function_exists('dbg')) {
117 function dbg($state) {
123 if(! function_exists('dbesc')) {
124 function dbesc($str) {
126 if($db && $db->connected)
127 return($db->escape($str));
129 return(str_replace("'","\\'",$str));
133 // Function: q($sql,$args);
134 // Description: execute SQL query with printf style args.
135 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
138 if(! function_exists('q')) {
142 $args = func_get_args();
145 if($db && $db->connected) {
146 $ret = $db->q(vsprintf($sql,$args));
152 * This will happen occasionally trying to store the
153 * session data after abnormal program termination
157 logger('dba: no database: ' . print_r($args,true));
164 * Raw db query, no arguments
168 if(! function_exists('dbq')) {
172 if($db && $db->connected)
180 // Caller is responsible for ensuring that any integer arguments to
181 // dbesc_array are actually integers and not malformed strings containing
182 // SQL injection vectors. All integer array elements should be specifically
183 // cast to int to avoid trouble.
186 if(! function_exists('dbesc_array_cb')) {
187 function dbesc_array_cb(&$item, $key) {
189 $item = dbesc($item);
193 if(! function_exists('dbesc_array')) {
194 function dbesc_array(&$arr) {
195 if(is_array($arr) && count($arr)) {
196 array_walk($arr,'dbesc_array_cb');