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;
21 public $error = false;
23 function __construct($server,$user,$pass,$db,$install = false) {
25 $server = trim($server);
30 if (!(strlen($server) && strlen($user))){
31 $this->connected = false;
37 if(strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
38 if(! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
39 $this->error = sprintf( t('Cannot locate DNS info for database server \'%s\''), $server);
40 $this->connected = false;
47 if(class_exists('mysqli')) {
48 $this->db = @new mysqli($server,$user,$pass,$db);
49 if(! mysqli_connect_errno()) {
50 $this->connected = true;
54 $this->mysqli = false;
55 $this->db = mysql_connect($server,$user,$pass);
56 if($this->db && mysql_select_db($db,$this->db)) {
57 $this->connected = true;
60 if(! $this->connected) {
67 public function getdb() {
71 public function q($sql) {
73 if((! $this->db) || (! $this->connected))
77 $result = @$this->db->query($sql);
79 $result = @mysql_query($sql,$this->db);
87 logger('dba: ' . $this->db->error);
89 elseif(mysql_errno($this->db))
90 logger('dba: ' . mysql_error($this->db));
94 elseif($result === true)
98 $mesg = $result->num_rows . ' results' . EOL;
100 $mesg = mysql_num_rows($result) . ' results' . EOL;
103 $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
105 logger('dba: ' . $str );
110 * If dbfail.out exists, we will write any failed calls directly to it,
111 * regardless of any logging that may or may nor be in effect.
112 * These usually indicate SQL syntax errors that need to be resolved.
115 if($result === false) {
116 logger('dba: ' . printable($sql) . ' returned false.');
117 if(file_exists('dbfail.out'))
118 file_put_contents('dbfail.out', printable($sql) . ' returned false' . "\n", FILE_APPEND);
122 if(($result === true) || ($result === false))
127 if($result->num_rows) {
128 while($x = $result->fetch_array(MYSQLI_ASSOC))
130 $result->free_result();
134 if(mysql_num_rows($result)) {
135 while($x = mysql_fetch_array($result, MYSQL_ASSOC))
137 mysql_free_result($result);
143 logger('dba: ' . printable(print_r($r, true)), LOGGER_DATA);
147 public function dbg($dbg) {
151 public function escape($str) {
152 if($this->db && $this->connected) {
154 return @$this->db->real_escape_string($str);
156 return @mysql_real_escape_string($str,$this->db);
160 function __destruct() {
165 mysql_close($this->db);
169 if(! function_exists('printable')) {
170 function printable($s) {
171 $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
172 $s = str_replace("\x00",'.',$s);
173 if(x($_SERVER,'SERVER_NAME'))
174 $s = escape_tags($s);
178 // Procedural functions
179 if(! function_exists('dbg')) {
180 function dbg($state) {
186 if(! function_exists('dbesc')) {
187 function dbesc($str) {
189 if($db && $db->connected)
190 return($db->escape($str));
192 return(str_replace("'","\\'",$str));
197 // Function: q($sql,$args);
198 // Description: execute SQL query with printf style args.
199 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
202 if(! function_exists('q')) {
206 $args = func_get_args();
209 if($db && $db->connected) {
210 $ret = $db->q(vsprintf($sql,$args));
216 * This will happen occasionally trying to store the
217 * session data after abnormal program termination
220 logger('dba: no database: ' . print_r($args,true));
227 * Raw db query, no arguments
231 if(! function_exists('dbq')) {
235 if($db && $db->connected)
243 // Caller is responsible for ensuring that any integer arguments to
244 // dbesc_array are actually integers and not malformed strings containing
245 // SQL injection vectors. All integer array elements should be specifically
246 // cast to int to avoid trouble.
249 if(! function_exists('dbesc_array_cb')) {
250 function dbesc_array_cb(&$item, $key) {
252 $item = dbesc($item);
256 if(! function_exists('dbesc_array')) {
257 function dbesc_array(&$arr) {
258 if(is_array($arr) && count($arr)) {
259 array_walk($arr,'dbesc_array_cb');