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 $mysqli = true;
20 public $connected = false;
23 function __construct($server,$user,$pass,$db,$install = false) {
25 $server = trim($server);
31 if(strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
32 if(! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
33 notice( sprintf( t('Cannot locate DNS info for database server \'%s\''), $server));
34 $this->connected = false;
41 if(class_exists('mysqli')) {
42 $this->db = @new mysqli($server,$user,$pass,$db);
43 if(! mysqli_connect_errno()) {
44 $this->connected = true;
48 $this->mysqli = false;
49 $this->db = mysql_connect($server,$user,$pass);
50 if($this->db && mysql_select_db($db,$this->db)) {
51 $this->connected = true;
54 if(! $this->connected) {
61 public function getdb() {
65 public function q($sql) {
67 if((! $this->db) || (! $this->connected))
71 $result = @$this->db->query($sql);
73 $result = @mysql_query($sql,$this->db);
81 logger('dba: ' . $this->db->error);
83 elseif(mysql_errno($this->db))
84 logger('dba: ' . mysql_error($this->db));
88 elseif($result === true)
92 $mesg = $result->num_rows . ' results' . EOL;
94 $mesg = mysql_num_rows($result) . ' results' . EOL;
97 $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
99 logger('dba: ' . $str );
104 * If dbfail.out exists, we will write any failed calls directly to it,
105 * regardless of any logging that may or may nor be in effect.
106 * These usually indicate SQL syntax errors that need to be resolved.
109 if($result === false) {
110 logger('dba: ' . printable($sql) . ' returned false.');
111 if(file_exists('dbfail.out'))
112 file_put_contents('dbfail.out', printable($sql) . ' returned false' . "\n", FILE_APPEND);
116 if(($result === true) || ($result === false))
121 if($result->num_rows) {
122 while($x = $result->fetch_array(MYSQL_ASSOC))
124 $result->free_result();
128 if(mysql_num_rows($result)) {
129 while($x = mysql_fetch_array($result, MYSQL_ASSOC))
131 mysql_free_result($result);
137 logger('dba: ' . printable(print_r($r, true)), LOGGER_DATA);
141 public function dbg($dbg) {
145 public function escape($str) {
146 if($this->db && $this->connected) {
148 return @$this->db->real_escape_string($str);
150 return @mysql_real_escape_string($str,$this->db);
154 function __destruct() {
158 @mysql_close($this->db);
162 if(! function_exists('printable')) {
163 function printable($s) {
164 $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
165 $s = str_replace("\x00",'.',$s);
166 if(x($_SERVER,'SERVER_NAME'))
167 $s = escape_tags($s);
171 // Procedural functions
172 if(! function_exists('dbg')) {
173 function dbg($state) {
179 if(! function_exists('dbesc')) {
180 function dbesc($str) {
182 if($db && $db->connected)
183 return($db->escape($str));
185 return(str_replace("'","\\'",$str));
189 // Function: q($sql,$args);
190 // Description: execute SQL query with printf style args.
191 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
194 if(! function_exists('q')) {
198 $args = func_get_args();
201 if($db && $db->connected) {
202 $ret = $db->q(vsprintf($sql,$args));
208 * This will happen occasionally trying to store the
209 * session data after abnormal program termination
213 logger('dba: no database: ' . print_r($args,true));
220 * Raw db query, no arguments
224 if(! function_exists('dbq')) {
228 if($db && $db->connected)
236 // Caller is responsible for ensuring that any integer arguments to
237 // dbesc_array are actually integers and not malformed strings containing
238 // SQL injection vectors. All integer array elements should be specifically
239 // cast to int to avoid trouble.
242 if(! function_exists('dbesc_array_cb')) {
243 function dbesc_array_cb(&$item, $key) {
245 $item = dbesc($item);
249 if(! function_exists('dbesc_array')) {
250 function dbesc_array(&$arr) {
251 if(is_array($arr) && count($arr)) {
252 array_walk($arr,'dbesc_array_cb');