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 if(class_exists('mysqli')) {
40 $this->db = @new mysqli($server,$user,$pass,$db);
41 if(! mysqli_connect_errno()) {
42 $this->connected = true;
46 $this->db = mysql_connect($server,$user,$pass);
47 if($this->db && mysql_select_db($db,$this->db)) {
48 $this->connected = true;
51 if(! $this->connected) {
58 public function getdb() {
62 public function q($sql) {
64 if((! $this->db) || (! $this->connected))
67 if(class_exists('mysqli'))
68 $result = @$this->db->query($sql);
70 $result = @mysql_query($sql,$this->db);
76 if(class_exists('mysqli') && $this->db->errno)
77 logger('dba: ' . $this->db->error);
79 logger('dba: ' . mysql_error($this->db));
83 elseif($result === true)
86 $mesg = $result->num_rows . ' results' . EOL;
88 $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
90 logger('dba: ' . $str );
95 * If dbfail.out exists, we will write any failed calls directly to it,
96 * regardless of any logging that may or may nor be in effect.
97 * These usually indicate SQL syntax errors that need to be resolved.
100 if($result === false) {
101 logger('dba: ' . printable($sql) . ' returned false.');
102 if(file_exists('dbfail.out'))
103 file_put_contents('dbfail.out', printable($sql) . ' returned false' . "\n", FILE_APPEND);
107 if(($result === true) || ($result === false))
111 if($result->num_rows) {
112 while($x = $result->fetch_array(MYSQL_ASSOC))
114 $result->free_result();
118 logger('dba: ' . printable(print_r($r, true)), LOGGER_DATA);
122 public function dbg($dbg) {
126 public function escape($str) {
127 if($this->db && $this->connected)
128 return @$this->db->real_escape_string($str);
131 function __destruct() {
136 if(! function_exists('printable')) {
137 function printable($s) {
138 $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
139 $s = str_replace("\x00",'.',$s);
140 if(x($_SERVER,'SERVER_NAME'))
141 $s = escape_tags($s);
145 // Procedural functions
146 if(! function_exists('dbg')) {
147 function dbg($state) {
153 if(! function_exists('dbesc')) {
154 function dbesc($str) {
156 if($db && $db->connected)
157 return($db->escape($str));
159 return(str_replace("'","\\'",$str));
163 // Function: q($sql,$args);
164 // Description: execute SQL query with printf style args.
165 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
168 if(! function_exists('q')) {
172 $args = func_get_args();
175 if($db && $db->connected) {
176 $ret = $db->q(vsprintf($sql,$args));
182 * This will happen occasionally trying to store the
183 * session data after abnormal program termination
187 logger('dba: no database: ' . print_r($args,true));
194 * Raw db query, no arguments
198 if(! function_exists('dbq')) {
202 if($db && $db->connected)
210 // Caller is responsible for ensuring that any integer arguments to
211 // dbesc_array are actually integers and not malformed strings containing
212 // SQL injection vectors. All integer array elements should be specifically
213 // cast to int to avoid trouble.
216 if(! function_exists('dbesc_array_cb')) {
217 function dbesc_array_cb(&$item, $key) {
219 $item = dbesc($item);
223 if(! function_exists('dbesc_array')) {
224 function dbesc_array(&$arr) {
225 if(is_array($arr) && count($arr)) {
226 array_walk($arr,'dbesc_array_cb');