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()) && (! $install)) {
28 $this->connected = true;
31 public function getdb() {
35 public function q($sql) {
40 $result = @$this->db->query($sql);
47 logger('dba: ' . $this->db->error);
51 elseif($result === true)
54 $mesg = $result->num_rows . ' results' . EOL;
56 $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
58 logger('dba: ' . $str );
63 * If dbfail.out exists, we will write any failed calls directly to it,
64 * regardless of any logging that may or may nor be in effect.
65 * These usually indicate SQL syntax errors that need to be resolved.
68 if($result === false) {
69 logger('dba: ' . printable($sql) . ' returned false.');
70 if(file_exists('dbfail.out'))
71 file_put_contents('dbfail.out', printable($sql) . ' returned false' . "\n", FILE_APPEND);
75 if(($result === true) || ($result === false))
79 if($result->num_rows) {
80 while($x = $result->fetch_array(MYSQL_ASSOC))
82 $result->free_result();
86 logger('dba: ' . printable(print_r($r, true)), LOGGER_DATA);
90 public function dbg($dbg) {
94 public function escape($str) {
95 return @$this->db->real_escape_string($str);
98 function __destruct() {
103 if(! function_exists('printable')) {
104 function printable($s) {
105 $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
106 $s = str_replace("\x00",'.',$s);
107 if(x($_SERVER,'SERVER_NAME'))
108 $s = escape_tags($s);
112 // Procedural functions
113 if(! function_exists('dbg')) {
114 function dbg($state) {
119 if(! function_exists('dbesc')) {
120 function dbesc($str) {
123 return($db->escape($str));
125 return(str_replace("'","\\'",$str));
129 // Function: q($sql,$args);
130 // Description: execute SQL query with printf style args.
131 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
134 if(! function_exists('q')) {
138 $args = func_get_args();
142 $ret = $db->q(vsprintf($sql,$args));
148 * This will happen occasionally trying to store the
149 * session data after abnormal program termination
153 logger('dba: no database: ' . print_r($args,true));
160 * Raw db query, no arguments
164 if(! function_exists('dbq')) {
176 // Caller is responsible for ensuring that any integer arguments to
177 // dbesc_array are actually integers and not malformed strings containing
178 // SQL injection vectors. All integer array elements should be specifically
179 // cast to int to avoid trouble.
182 if(! function_exists('dbesc_array_cb')) {
183 function dbesc_array_cb(&$item, $key) {
185 $item = dbesc($item);
189 if(! function_exists('dbesc_array')) {
190 function dbesc_array(&$arr) {
191 if(is_array($arr) && count($arr)) {
192 array_walk($arr,'dbesc_array_cb');