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')) {
20 function __construct($server,$user,$pass,$db,$install = false) {
21 $this->db = @new mysqli($server,$user,$pass,$db);
22 if((mysqli_connect_errno()) && (! $install))
26 public function getdb() {
30 public function q($sql) {
35 $result = @$this->db->query($sql);
42 logger('dba: ' . $this->db->error);
46 elseif($result === true)
49 $mesg = $result->num_rows . ' results' . EOL;
51 $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
53 logger('dba: ' . $str );
58 * If dbfail.out exists, we will write any failed calls directly to it,
59 * regardless of any logging that may or may nor be in effect.
60 * These usually indicate SQL syntax errors that need to be resolved.
63 if($result === false) {
64 logger('dba: ' . printable($sql) . ' returned false.');
65 if(file_exists('dbfail.out'))
66 file_put_contents('dbfail.out', printable($sql) . ' returned false' . "\n", FILE_APPEND);
70 if(($result === true) || ($result === false))
74 if($result->num_rows) {
75 while($x = $result->fetch_array(MYSQL_ASSOC))
77 $result->free_result();
81 logger('dba: ' . printable(print_r($r, true)), LOGGER_DATA);
85 public function dbg($dbg) {
89 public function escape($str) {
90 return @$this->db->real_escape_string($str);
93 function __destruct() {
98 if(! function_exists('printable')) {
99 function printable($s) {
100 $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
101 $s = str_replace("\x00",'.',$s);
102 if(x($_SERVER,'SERVER_NAME'))
103 $s = escape_tags($s);
107 // Procedural functions
108 if(! function_exists('dbg')) {
109 function dbg($state) {
114 if(! function_exists('dbesc')) {
115 function dbesc($str) {
118 return($db->escape($str));
120 return(str_replace("'","\\'",$str));
124 // Function: q($sql,$args);
125 // Description: execute SQL query with printf style args.
126 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
129 if(! function_exists('q')) {
133 $args = func_get_args();
137 $ret = $db->q(vsprintf($sql,$args));
143 * This will happen occasionally trying to store the
144 * session data after abnormal program termination
148 logger('dba: no database: ' . print_r($args,true));
155 * Raw db query, no arguments
159 if(! function_exists('dbq')) {
168 // Caller is responsible for ensuring that any integer arguments to
169 // dbesc_array are actually integers and not malformed strings containing
170 // SQL injection vectors. All integer array elements should be specifically
171 // cast to int to avoid trouble.
174 if(! function_exists('dbesc_array_cb')) {
175 function dbesc_array_cb(&$item, $key) {
177 $item = dbesc($item);
181 if(! function_exists('dbesc_array')) {
182 function dbesc_array(&$arr) {
183 if(is_array($arr) && count($arr)) {
184 array_walk($arr,'dbesc_array_cb');