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) {
23 $server = trim($server);
29 if(strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
30 if(! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
31 notice( sprintf( t('Cannot locate DNS info for database server \'%s\''), $server));
32 $this->connected = false;
39 $this->db = @new mysqli($server,$user,$pass,$db);
40 if(! mysqli_connect_errno()) {
41 $this->connected = true;
50 public function getdb() {
54 public function q($sql) {
56 if((! $this->db) || (! $this->connected))
59 $result = @$this->db->query($sql);
66 logger('dba: ' . $this->db->error);
70 elseif($result === true)
73 $mesg = $result->num_rows . ' results' . EOL;
75 $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
77 logger('dba: ' . $str );
82 * If dbfail.out exists, we will write any failed calls directly to it,
83 * regardless of any logging that may or may nor be in effect.
84 * These usually indicate SQL syntax errors that need to be resolved.
87 if($result === false) {
88 logger('dba: ' . printable($sql) . ' returned false.');
89 if(file_exists('dbfail.out'))
90 file_put_contents('dbfail.out', printable($sql) . ' returned false' . "\n", FILE_APPEND);
94 if(($result === true) || ($result === false))
98 if($result->num_rows) {
99 while($x = $result->fetch_array(MYSQL_ASSOC))
101 $result->free_result();
105 logger('dba: ' . printable(print_r($r, true)), LOGGER_DATA);
109 public function dbg($dbg) {
113 public function escape($str) {
114 if($this->db && $this->connected)
115 return @$this->db->real_escape_string($str);
118 function __destruct() {
123 if(! function_exists('printable')) {
124 function printable($s) {
125 $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
126 $s = str_replace("\x00",'.',$s);
127 if(x($_SERVER,'SERVER_NAME'))
128 $s = escape_tags($s);
132 // Procedural functions
133 if(! function_exists('dbg')) {
134 function dbg($state) {
140 if(! function_exists('dbesc')) {
141 function dbesc($str) {
143 if($db && $db->connected)
144 return($db->escape($str));
146 return(str_replace("'","\\'",$str));
150 // Function: q($sql,$args);
151 // Description: execute SQL query with printf style args.
152 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
155 if(! function_exists('q')) {
159 $args = func_get_args();
162 if($db && $db->connected) {
163 $ret = $db->q(vsprintf($sql,$args));
169 * This will happen occasionally trying to store the
170 * session data after abnormal program termination
174 logger('dba: no database: ' . print_r($args,true));
181 * Raw db query, no arguments
185 if(! function_exists('dbq')) {
189 if($db && $db->connected)
197 // Caller is responsible for ensuring that any integer arguments to
198 // dbesc_array are actually integers and not malformed strings containing
199 // SQL injection vectors. All integer array elements should be specifically
200 // cast to int to avoid trouble.
203 if(! function_exists('dbesc_array_cb')) {
204 function dbesc_array_cb(&$item, $key) {
206 $item = dbesc($item);
210 if(! function_exists('dbesc_array')) {
211 function dbesc_array(&$arr) {
212 if(is_array($arr) && count($arr)) {
213 array_walk($arr,'dbesc_array_cb');