Browse Source

"dba" is now a static class

tags/3.5.4
Michael 1 year ago
parent
commit
a29b7e7153

+ 6
- 10
include/auth_ejabberd.php View File

@@ -47,18 +47,16 @@ $directory = realpath($directory."/..");
47 47
 chdir($directory);
48 48
 require_once("boot.php");
49 49
 
50
-global $a, $db;
50
+global $a;
51 51
 
52 52
 if (is_null($a)) {
53 53
 	$a = new App(dirname(__DIR__));
54 54
 }
55 55
 
56
-if (is_null($db)) {
57
-	@include(".htconfig.php");
58
-	require_once("include/dba.php");
59
-	$db = new dba($db_host, $db_user, $db_pass, $db_data);
60
-	unset($db_host, $db_user, $db_pass, $db_data);
61
-};
56
+@include(".htconfig.php");
57
+require_once("include/dba.php");
58
+dba::connect($db_host, $db_user, $db_pass, $db_data);
59
+unset($db_host, $db_user, $db_pass, $db_data);
62 60
 
63 61
 // the logfile to which to write, should be writeable by the user which is running the server
64 62
 $sLogFile = get_config('jabber','logfile');
@@ -81,8 +79,6 @@ class exAuth {
81 79
 	 * @param boolean $bDebug Debug mode
82 80
 	 */
83 81
 	public function __construct($sLogFile, $bDebug) {
84
-		global $db;
85
-
86 82
 		// setter
87 83
 		$this->sLogFile 	= $sLogFile;
88 84
 		$this->bDebug		= $bDebug;
@@ -96,7 +92,7 @@ class exAuth {
96 92
 		// We are connected to the SQL server and are having a log file.
97 93
 		do {
98 94
 			// Quit if the database connection went down
99
-			if (!$db->connected()) {
95
+			if (!dba::connected()) {
100 96
 				$this->writeDebugLog("[debug] the database connection went down");
101 97
 				return;
102 98
 			}

+ 5
- 9
include/cli_startup.php View File

@@ -8,19 +8,16 @@ require_once('boot.php');
8 8
 // Everything we need to boot standalone 'background' processes
9 9
 
10 10
 function cli_startup() {
11
-
12
-	global $a, $db;
11
+	global $a;
13 12
 
14 13
 	if (is_null($a)) {
15 14
 		$a = new App(dirname(__DIR__));
16 15
 	}
17 16
 
18
-	if (is_null($db)) {
19
-		@include(".htconfig.php");
20
-		require_once("dba.php");
21
-		$db = new dba($db_host, $db_user, $db_pass, $db_data);
22
-		unset($db_host, $db_user, $db_pass, $db_data);
23
-	};
17
+	@include(".htconfig.php");
18
+	require_once("dba.php");
19
+	dba::connect($db_host, $db_user, $db_pass, $db_data);
20
+	unset($db_host, $db_user, $db_pass, $db_data);
24 21
 
25 22
 	require_once('include/session.php');
26 23
 
@@ -29,5 +26,4 @@ function cli_startup() {
29 26
 	$a->set_baseurl(get_config('system','url'));
30 27
 
31 28
 	load_hooks();
32
-
33 29
 }

+ 141
- 250
include/dba.php View File

@@ -11,21 +11,22 @@ require_once('include/datetime.php');
11 11
  */
12 12
 
13 13
 class dba {
14
-
15
-	private $debug = 0;
16
-	private $db;
17
-	private $result;
18
-	private $driver;
19
-	public  $connected = false;
20
-	public  $error = false;
21
-	public  $errorno = 0;
22
-	public  $affected_rows = 0;
23
-	private $_server_info = '';
14
+	public static $connected = true;
15
+
16
+	private static $_server_info = '';
17
+	private static $db;
18
+	private static $driver;
19
+	private static $error = false;
20
+	private static $errorno = 0;
21
+	private static $affected_rows = 0;
24 22
 	private static $in_transaction = false;
25
-	private static $dbo;
26 23
 	private static $relation = array();
27 24
 
28
-	function __construct($serveraddr, $user, $pass, $db, $install = false) {
25
+	public static function connect($serveraddr, $user, $pass, $db, $install = false) {
26
+		if (!is_null(self::$db)) {
27
+			return true;
28
+		}
29
+
29 30
 		$a = get_app();
30 31
 
31 32
 		$stamp1 = microtime(true);
@@ -45,24 +46,24 @@ class dba {
45 46
 		$db = trim($db);
46 47
 
47 48
 		if (!(strlen($server) && strlen($user))) {
48
-			$this->connected = false;
49
-			$this->db = null;
50
-			return;
49
+			self::$connected = false;
50
+			self::$db = null;
51
+			return false;
51 52
 		}
52 53
 
53 54
 		if ($install) {
54 55
 			if (strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
55 56
 				if (! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
56
-					$this->error = sprintf(t('Cannot locate DNS info for database server \'%s\''), $server);
57
-					$this->connected = false;
58
-					$this->db = null;
59
-					return;
57
+					self::$error = sprintf(t('Cannot locate DNS info for database server \'%s\''), $server);
58
+					self::$connected = false;
59
+					self::$db = null;
60
+					return false;
60 61
 				}
61 62
 			}
62 63
 		}
63 64
 
64 65
 		if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
65
-			$this->driver = 'pdo';
66
+			self::$driver = 'pdo';
66 67
 			$connect = "mysql:host=".$server.";dbname=".$db;
67 68
 
68 69
 			if (isset($port)) {
@@ -73,63 +74,47 @@ class dba {
73 74
 				$connect .= ";charset=".$a->config["system"]["db_charset"];
74 75
 			}
75 76
 			try {
76
-				$this->db = @new PDO($connect, $user, $pass);
77
-				$this->connected = true;
77
+				self::$db = @new PDO($connect, $user, $pass);
78
+				self::$connected = true;
78 79
 			} catch (PDOException $e) {
79
-				$this->connected = false;
80
+				self::$connected = false;
80 81
 			}
81 82
 		}
82 83
 
83
-		if (!$this->connected && class_exists('mysqli')) {
84
-			$this->driver = 'mysqli';
85
-			$this->db = @new mysqli($server, $user, $pass, $db, $port);
84
+		if (!self::$connected && class_exists('mysqli')) {
85
+			self::$driver = 'mysqli';
86
+			self::$db = @new mysqli($server, $user, $pass, $db, $port);
86 87
 			if (!mysqli_connect_errno()) {
87
-				$this->connected = true;
88
+				self::$connected = true;
88 89
 
89 90
 				if (isset($a->config["system"]["db_charset"])) {
90
-					$this->db->set_charset($a->config["system"]["db_charset"]);
91
+					self::$db->set_charset($a->config["system"]["db_charset"]);
91 92
 				}
92 93
 			}
93 94
 		}
94 95
 
95
-		if (!$this->connected && function_exists('mysql_connect')) {
96
-			$this->driver = 'mysql';
97
-			$this->db = mysql_connect($serveraddr, $user, $pass);
98
-			if ($this->db && mysql_select_db($db, $this->db)) {
99
-				$this->connected = true;
96
+		if (!self::$connected && function_exists('mysql_connect')) {
97
+			self::$driver = 'mysql';
98
+			self::$db = mysql_connect($serveraddr, $user, $pass);
99
+			if (self::$db && mysql_select_db($db, self::$db)) {
100
+				self::$connected = true;
100 101
 
101 102
 				if (isset($a->config["system"]["db_charset"])) {
102
-					mysql_set_charset($a->config["system"]["db_charset"], $this->db);
103
+					mysql_set_charset($a->config["system"]["db_charset"], self::$db);
103 104
 				}
104 105
 			}
105 106
 		}
106 107
 
107 108
 		// No suitable SQL driver was found.
108
-		if (!$this->connected) {
109
-			$this->db = null;
109
+		if (!self::$connected) {
110
+			self::$db = null;
110 111
 			if (!$install) {
111 112
 				system_unavailable();
112 113
 			}
113 114
 		}
114 115
 		$a->save_timestamp($stamp1, "network");
115 116
 
116
-		self::$dbo = $this;
117
-	}
118
-
119
-	/**
120
-	 * @brief Checks if the database object is initialized
121
-	 *
122
-	 * This is a possible bugfix for something that doesn't occur for me.
123
-	 * There seems to be situations, where the object isn't initialized.
124
-	 */
125
-	private static function initialize() {
126
-		if (!is_object(self::$dbo)) {
127
-			global $db;
128
-			self::$dbo = $db;
129
-			if (!is_object(self::$dbo)) {
130
-				die('Database is uninitialized!');
131
-			}
132
-		}
117
+		return true;
133 118
 	}
134 119
 
135 120
 	/**
@@ -140,21 +125,21 @@ class dba {
140 125
 	 *
141 126
 	 * @return string
142 127
 	 */
143
-	public function server_info() {
144
-		if ($this->_server_info == '') {
145
-			switch ($this->driver) {
128
+	public static function server_info() {
129
+		if (self::$_server_info == '') {
130
+			switch (self::$driver) {
146 131
 				case 'pdo':
147
-					$this->_server_info = $this->db->getAttribute(PDO::ATTR_SERVER_VERSION);
132
+					self::$_server_info = self::$db->getAttribute(PDO::ATTR_SERVER_VERSION);
148 133
 					break;
149 134
 				case 'mysqli':
150
-					$this->_server_info = $this->db->server_info;
135
+					self::$_server_info = self::$db->server_info;
151 136
 					break;
152 137
 				case 'mysql':
153
-					$this->_server_info = mysql_get_server_info($this->db);
138
+					self::$_server_info = mysql_get_server_info(self::$db);
154 139
 					break;
155 140
 			}
156 141
 		}
157
-		return $this->_server_info;
142
+		return self::$_server_info;
158 143
 	}
159 144
 
160 145
 	/**
@@ -162,10 +147,10 @@ class dba {
162 147
 	 *
163 148
 	 * @return string
164 149
 	 */
165
-	public function database_name() {
166
-		$r = $this->q("SELECT DATABASE() AS `db`");
167
-
168
-		return $r[0]['db'];
150
+	public static function database_name() {
151
+		$ret = self::p("SELECT DATABASE() AS `db`");
152
+                $data = self::inArray($ret);
153
+		return $data[0]['db'];
169 154
 	}
170 155
 
171 156
 	/**
@@ -173,7 +158,7 @@ class dba {
173 158
 	 *
174 159
 	 * @param string $query The database query that will be analyzed
175 160
 	 */
176
-	public function log_index($query) {
161
+	private static function log_index($query) {
177 162
 		$a = get_app();
178 163
 
179 164
 		if (empty($a->config["system"]["db_log_index"])) {
@@ -190,7 +175,7 @@ class dba {
190 175
 			return;
191 176
 		}
192 177
 
193
-		$r = $this->q("EXPLAIN ".$query);
178
+		$r = self::p("EXPLAIN ".$query);
194 179
 		if (!dbm::is_result($r)) {
195 180
 			return;
196 181
 		}
@@ -198,7 +183,7 @@ class dba {
198 183
 		$watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
199 184
 		$blacklist = explode(',', $a->config["system"]["db_log_index_blacklist"]);
200 185
 
201
-		foreach ($r AS $row) {
186
+		while ($row = dba::fetch($r)) {
202 187
 			if ((intval($a->config["system"]["db_loglimit_index"]) > 0)) {
203 188
 				$log = (in_array($row['key'], $watchlist) &&
204 189
 					($row['rows'] >= intval($a->config["system"]["db_loglimit_index"])));
@@ -225,79 +210,33 @@ class dba {
225 210
 		}
226 211
 	}
227 212
 
228
-	/**
229
-	 * @brief execute SQL query - deprecated
230
-	 *
231
-	 * Please use the dba:: functions instead:
232
-	 * dba::select, dba::exists, dba::insert
233
-	 * dba::delete, dba::update, dba::p, dba::e
234
-	 *
235
-	 * @param string $sql SQL query
236
-	 * @return array Query array
237
-	 */
238
-	public function q($sql) {
239
-		$ret = self::p($sql);
240
-
241
-		if (is_bool($ret)) {
242
-			return $ret;
243
-		}
244
-
245
-		$columns = self::columnCount($ret);
246
-
247
-		$data = self::inArray($ret);
248
-
249
-		if ((count($data) == 0) && ($columns == 0)) {
250
-			return true;
251
-		}
252
-
253
-		return $data;
254
-	}
255
-
256
-	public function escape($str) {
257
-		if ($this->db && $this->connected) {
258
-			switch ($this->driver) {
259
-				case 'pdo':
260
-					return substr(@$this->db->quote($str, PDO::PARAM_STR), 1, -1);
261
-				case 'mysqli':
262
-					return @$this->db->real_escape_string($str);
263
-				case 'mysql':
264
-					return @mysql_real_escape_string($str,$this->db);
265
-			}
213
+	public static function escape($str) {
214
+		switch (self::$driver) {
215
+			case 'pdo':
216
+				return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1);
217
+			case 'mysqli':
218
+				return @self::$db->real_escape_string($str);
219
+			case 'mysql':
220
+				return @mysql_real_escape_string($str,self::$db);
266 221
 		}
267 222
 	}
268 223
 
269
-	function connected() {
270
-		switch ($this->driver) {
224
+	public static function connected() {
225
+		switch (self::$driver) {
271 226
 			case 'pdo':
272 227
 				// Not sure if this really is working like expected
273
-				$connected = ($this->db->getAttribute(PDO::ATTR_CONNECTION_STATUS) != "");
228
+				$connected = (self::$db->getAttribute(PDO::ATTR_CONNECTION_STATUS) != "");
274 229
 				break;
275 230
 			case 'mysqli':
276
-				$connected = $this->db->ping();
231
+				$connected = self::$db->ping();
277 232
 				break;
278 233
 			case 'mysql':
279
-				$connected = mysql_ping($this->db);
234
+				$connected = mysql_ping(self::$db);
280 235
 				break;
281 236
 		}
282 237
 		return $connected;
283 238
 	}
284 239
 
285
-	function __destruct() {
286
-		if ($this->db) {
287
-			switch ($this->driver) {
288
-				case 'pdo':
289
-					$this->db = null;
290
-					break;
291
-				case 'mysqli':
292
-					$this->db->close();
293
-					break;
294
-				case 'mysql':
295
-					mysql_close($this->db);
296
-					break;
297
-			}
298
-		}
299
-	}
300
-
301 240
 	/**
302 241
 	 * @brief Replaces ANY_VALUE() function by MIN() function,
303 242
 	 *  if the database server does not support ANY_VALUE().
@@ -309,8 +248,8 @@ class dba {
309 248
 	 * @param string $sql An SQL string without the values
310 249
 	 * @return string The input SQL string modified if necessary.
311 250
 	 */
312
-	public function any_value_fallback($sql) {
313
-		$server_info = $this->server_info();
251
+	public static function any_value_fallback($sql) {
252
+		$server_info = self::server_info();
314 253
 		if (version_compare($server_info, '5.7.5', '<') ||
315 254
 			(stripos($server_info, 'MariaDB') !== false)) {
316 255
 			$sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
@@ -327,7 +266,7 @@ class dba {
327 266
 	 * @param string $sql An SQL string without the values
328 267
 	 * @return string The input SQL string modified if necessary.
329 268
 	 */
330
-	public function clean_query($sql) {
269
+	public static function clean_query($sql) {
331 270
 		$search = array("\t", "\n", "\r", "  ");
332 271
 		$replace = array(' ', ' ', ' ', ' ');
333 272
 		do {
@@ -352,7 +291,7 @@ class dba {
352 291
 			if (is_int($args[$param]) || is_float($args[$param])) {
353 292
 				$replace = intval($args[$param]);
354 293
 			} else {
355
-				$replace = "'".self::$dbo->escape($args[$param])."'";
294
+				$replace = "'".self::escape($args[$param])."'";
356 295
 			}
357 296
 
358 297
 			$pos = strpos($sql, '?', $offset);
@@ -391,8 +330,6 @@ class dba {
391 330
 	 * @return object statement object
392 331
 	 */
393 332
 	public static function p($sql) {
394
-		self::initialize();
395
-
396 333
 		$a = get_app();
397 334
 
398 335
 		$stamp1 = microtime(true);
@@ -410,7 +347,7 @@ class dba {
410 347
 			$args[++$i] = $param;
411 348
 		}
412 349
 
413
-		if (!self::$dbo || !self::$dbo->connected) {
350
+		if (!self::$connected) {
414 351
 			return false;
415 352
 		}
416 353
 
@@ -419,8 +356,8 @@ class dba {
419 356
 			logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
420 357
 		}
421 358
 
422
-		$sql = self::$dbo->clean_query($sql);
423
-		$sql = self::$dbo->any_value_fallback($sql);
359
+		$sql = self::clean_query($sql);
360
+		$sql = self::any_value_fallback($sql);
424 361
 
425 362
 		$orig_sql = $sql;
426 363
 
@@ -428,9 +365,9 @@ class dba {
428 365
 			$sql = "/*".System::callstack()." */ ".$sql;
429 366
 		}
430 367
 
431
-		self::$dbo->error = '';
432
-		self::$dbo->errorno = 0;
433
-		self::$dbo->affected_rows = 0;
368
+		self::$error = '';
369
+		self::$errorno = 0;
370
+		self::$affected_rows = 0;
434 371
 
435 372
 		// We have to make some things different if this function is called from "e"
436 373
 		$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
@@ -444,25 +381,25 @@ class dba {
444 381
 		// We are having an own error logging in the function "e"
445 382
 		$called_from_e = ($called_from['function'] == 'e');
446 383
 
447
-		switch (self::$dbo->driver) {
384
+		switch (self::$driver) {
448 385
 			case 'pdo':
449 386
 				// If there are no arguments we use "query"
450 387
 				if (count($args) == 0) {
451
-					if (!$retval = self::$dbo->db->query($sql)) {
452
-						$errorInfo = self::$dbo->db->errorInfo();
453
-						self::$dbo->error = $errorInfo[2];
454
-						self::$dbo->errorno = $errorInfo[1];
388
+					if (!$retval = self::$db->query($sql)) {
389
+						$errorInfo = self::$db->errorInfo();
390
+						self::$error = $errorInfo[2];
391
+						self::$errorno = $errorInfo[1];
455 392
 						$retval = false;
456 393
 						break;
457 394
 					}
458
-					self::$dbo->affected_rows = $retval->rowCount();
395
+					self::$affected_rows = $retval->rowCount();
459 396
 					break;
460 397
 				}
461 398
 
462
-				if (!$stmt = self::$dbo->db->prepare($sql)) {
463
-					$errorInfo = self::$dbo->db->errorInfo();
464
-					self::$dbo->error = $errorInfo[2];
465
-					self::$dbo->errorno = $errorInfo[1];
399
+				if (!$stmt = self::$db->prepare($sql)) {
400
+					$errorInfo = self::$db->errorInfo();
401
+					self::$error = $errorInfo[2];
402
+					self::$errorno = $errorInfo[1];
466 403
 					$retval = false;
467 404
 					break;
468 405
 				}
@@ -473,12 +410,12 @@ class dba {
473 410
 
474 411
 				if (!$stmt->execute()) {
475 412
 					$errorInfo = $stmt->errorInfo();
476
-					self::$dbo->error = $errorInfo[2];
477
-					self::$dbo->errorno = $errorInfo[1];
413
+					self::$error = $errorInfo[2];
414
+					self::$errorno = $errorInfo[1];
478 415
 					$retval = false;
479 416
 				} else {
480 417
 					$retval = $stmt;
481
-					self::$dbo->affected_rows = $retval->rowCount();
418
+					self::$affected_rows = $retval->rowCount();
482 419
 				}
483 420
 				break;
484 421
 			case 'mysqli':
@@ -489,26 +426,26 @@ class dba {
489 426
 
490 427
 				// The fallback routine is called as well when there are no arguments
491 428
 				if (!$can_be_prepared || (count($args) == 0)) {
492
-					$retval = self::$dbo->db->query(self::replace_parameters($sql, $args));
493
-					if (self::$dbo->db->errno) {
494
-						self::$dbo->error = self::$dbo->db->error;
495
-						self::$dbo->errorno = self::$dbo->db->errno;
429
+					$retval = self::$db->query(self::replace_parameters($sql, $args));
430
+					if (self::$db->errno) {
431
+						self::$error = self::$db->error;
432
+						self::$errorno = self::$db->errno;
496 433
 						$retval = false;
497 434
 					} else {
498 435
 						if (isset($retval->num_rows)) {
499
-							self::$dbo->affected_rows = $retval->num_rows;
436
+							self::$affected_rows = $retval->num_rows;
500 437
 						} else {
501
-							self::$dbo->affected_rows = self::$dbo->db->affected_rows;
438
+							self::$affected_rows = self::$db->affected_rows;
502 439
 						}
503 440
 					}
504 441
 					break;
505 442
 				}
506 443
 
507
-				$stmt = self::$dbo->db->stmt_init();
444
+				$stmt = self::$db->stmt_init();
508 445
 
509 446
 				if (!$stmt->prepare($sql)) {
510
-					self::$dbo->error = $stmt->error;
511
-					self::$dbo->errorno = $stmt->errno;
447
+					self::$error = $stmt->error;
448
+					self::$errorno = $stmt->errno;
512 449
 					$retval = false;
513 450
 					break;
514 451
 				}
@@ -534,44 +471,44 @@ class dba {
534 471
 				}
535 472
 
536 473
 				if (!$stmt->execute()) {
537
-					self::$dbo->error = self::$dbo->db->error;
538
-					self::$dbo->errorno = self::$dbo->db->errno;
474
+					self::$error = self::$db->error;
475
+					self::$errorno = self::$db->errno;
539 476
 					$retval = false;
540 477
 				} else {
541 478
 					$stmt->store_result();
542 479
 					$retval = $stmt;
543
-					self::$dbo->affected_rows = $retval->affected_rows;
480
+					self::$affected_rows = $retval->affected_rows;
544 481
 				}
545 482
 				break;
546 483
 			case 'mysql':
547 484
 				// For the old "mysql" functions we cannot use prepared statements
548
-				$retval = mysql_query(self::replace_parameters($sql, $args), self::$dbo->db);
549
-				if (mysql_errno(self::$dbo->db)) {
550
-					self::$dbo->error = mysql_error(self::$dbo->db);
551
-					self::$dbo->errorno = mysql_errno(self::$dbo->db);
485
+				$retval = mysql_query(self::replace_parameters($sql, $args), self::$db);
486
+				if (mysql_errno(self::$db)) {
487
+					self::$error = mysql_error(self::$db);
488
+					self::$errorno = mysql_errno(self::$db);
552 489
 				} else {
553
-					self::$dbo->affected_rows = mysql_affected_rows($retval);
490
+					self::$affected_rows = mysql_affected_rows($retval);
554 491
 
555 492
 					// Due to missing mysql_* support this here wasn't tested at all
556 493
 					// See here: http://php.net/manual/en/function.mysql-num-rows.php
557
-					if (self::$dbo->affected_rows <= 0) {
558
-						self::$dbo->affected_rows = mysql_num_rows($retval);
494
+					if (self::$affected_rows <= 0) {
495
+						self::$affected_rows = mysql_num_rows($retval);
559 496
 					}
560 497
 				}
561 498
 				break;
562 499
 		}
563 500
 
564 501
 		// We are having an own error logging in the function "e"
565
-		if ((self::$dbo->errorno != 0) && !$called_from_e) {
502
+		if ((self::$errorno != 0) && !$called_from_e) {
566 503
 			// We have to preserve the error code, somewhere in the logging it get lost
567
-			$error = self::$dbo->error;
568
-			$errorno = self::$dbo->errorno;
504
+			$error = self::$error;
505
+			$errorno = self::$errorno;
569 506
 
570
-			logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
507
+			logger('DB Error '.self::$errorno.': '.self::$error."\n".
571 508
 				System::callstack(8)."\n".self::replace_parameters($sql, $params));
572 509
 
573
-			self::$dbo->error = $error;
574
-			self::$dbo->errorno = $errorno;
510
+			self::$error = $error;
511
+			self::$errorno = $errorno;
575 512
 		}
576 513
 
577 514
 		$a->save_timestamp($stamp1, 'database');
@@ -603,8 +540,6 @@ class dba {
603 540
 	 * @return boolean Was the query successfull? False is returned only if an error occurred
604 541
 	 */
605 542
 	public static function e($sql) {
606
-		self::initialize();
607
-
608 543
 		$a = get_app();
609 544
 
610 545
 		$stamp = microtime(true);
@@ -627,18 +562,18 @@ class dba {
627 562
 
628 563
 			self::close($stmt);
629 564
 
630
-		} while ((self::$dbo->errorno == 1213) && (--$timeout > 0));
565
+		} while ((self::$errorno == 1213) && (--$timeout > 0));
631 566
 
632
-		if (self::$dbo->errorno != 0) {
567
+		if (self::$errorno != 0) {
633 568
 			// We have to preserve the error code, somewhere in the logging it get lost
634
-			$error = self::$dbo->error;
635
-			$errorno = self::$dbo->errorno;
569
+			$error = self::$error;
570
+			$errorno = self::$errorno;
636 571
 
637
-			logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
572
+			logger('DB Error '.self::$errorno.': '.self::$error."\n".
638 573
 				System::callstack(8)."\n".self::replace_parameters($sql, $params));
639 574
 
640
-			self::$dbo->error = $error;
641
-			self::$dbo->errorno = $errorno;
575
+			self::$error = $error;
576
+			self::$errorno = $errorno;
642 577
 		}
643 578
 
644 579
 		$a->save_timestamp($stamp, "database_write");
@@ -655,8 +590,6 @@ class dba {
655 590
 	 * @return boolean Are there rows for that condition?
656 591
 	 */
657 592
 	public static function exists($table, $condition) {
658
-		self::initialize();
659
-
660 593
 		if (empty($table)) {
661 594
 			return false;
662 595
 		}
@@ -691,8 +624,6 @@ class dba {
691 624
 	 * @return array first row of query
692 625
 	 */
693 626
 	public static function fetch_first($sql) {
694
-		self::initialize();
695
-
696 627
 		$params = self::getParam(func_get_args());
697 628
 
698 629
 		$stmt = self::p($sql, $params);
@@ -714,9 +645,7 @@ class dba {
714 645
 	 * @return int Number of rows
715 646
 	 */
716 647
 	public static function affected_rows() {
717
-		self::initialize();
718
-
719
-		return self::$dbo->affected_rows;
648
+		return self::$affected_rows;
720 649
 	}
721 650
 
722 651
 	/**
@@ -726,12 +655,10 @@ class dba {
726 655
 	 * @return int Number of columns
727 656
 	 */
728 657
 	public static function columnCount($stmt) {
729
-		self::initialize();
730
-
731 658
 		if (!is_object($stmt)) {
732 659
 			return 0;
733 660
 		}
734
-		switch (self::$dbo->driver) {
661
+		switch (self::$driver) {
735 662
 			case 'pdo':
736 663
 				return $stmt->columnCount();
737 664
 			case 'mysqli':
@@ -748,12 +675,10 @@ class dba {
748 675
 	 * @return int Number of rows
749 676
 	 */
750 677
 	public static function num_rows($stmt) {
751
-		self::initialize();
752
-
753 678
 		if (!is_object($stmt)) {
754 679
 			return 0;
755 680
 		}
756
-		switch (self::$dbo->driver) {
681
+		switch (self::$driver) {
757 682
 			case 'pdo':
758 683
 				return $stmt->rowCount();
759 684
 			case 'mysqli':
@@ -771,13 +696,11 @@ class dba {
771 696
 	 * @return array current row
772 697
 	 */
773 698
 	public static function fetch($stmt) {
774
-		self::initialize();
775
-
776 699
 		if (!is_object($stmt)) {
777 700
 			return false;
778 701
 		}
779 702
 
780
-		switch (self::$dbo->driver) {
703
+		switch (self::$driver) {
781 704
 			case 'pdo':
782 705
 				return $stmt->fetch(PDO::FETCH_ASSOC);
783 706
 			case 'mysqli':
@@ -813,7 +736,7 @@ class dba {
813 736
 				}
814 737
 				return $columns;
815 738
 			case 'mysql':
816
-				return mysql_fetch_array(self::$dbo->result, MYSQL_ASSOC);
739
+				return mysql_fetch_array($stmt, MYSQL_ASSOC);
817 740
 		}
818 741
 	}
819 742
 
@@ -827,9 +750,7 @@ class dba {
827 750
 	 * @return boolean was the insert successfull?
828 751
 	 */
829 752
 	public static function insert($table, $param, $on_duplicate_update = false) {
830
-		self::initialize();
831
-
832
-		$sql = "INSERT INTO `".self::$dbo->escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
753
+		$sql = "INSERT INTO `".self::escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
833 754
 			substr(str_repeat("?, ", count($param)), 0, -2).")";
834 755
 
835 756
 		if ($on_duplicate_update) {
@@ -848,17 +769,15 @@ class dba {
848 769
 	 * @return integer Last inserted id
849 770
 	 */
850 771
 	public static function lastInsertId() {
851
-		self::initialize();
852
-
853
-		switch (self::$dbo->driver) {
772
+		switch (self::$driver) {
854 773
 			case 'pdo':
855
-				$id = self::$dbo->db->lastInsertId();
774
+				$id = self::$db->lastInsertId();
856 775
 				break;
857 776
 			case 'mysqli':
858
-				$id = self::$dbo->db->insert_id;
777
+				$id = self::$db->insert_id;
859 778
 				break;
860 779
 			case 'mysql':
861
-				$id = mysql_insert_id(self::$dbo);
780
+				$id = mysql_insert_id(self::$db);
862 781
 				break;
863 782
 		}
864 783
 		return $id;
@@ -874,11 +793,9 @@ class dba {
874 793
 	 * @return boolean was the lock successful?
875 794
 	 */
876 795
 	public static function lock($table) {
877
-		self::initialize();
878
-
879 796
 		// See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
880 797
 		self::e("SET autocommit=0");
881
-		$success = self::e("LOCK TABLES `".self::$dbo->escape($table)."` WRITE");
798
+		$success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
882 799
 		if (!$success) {
883 800
 			self::e("SET autocommit=1");
884 801
 		} else {
@@ -893,8 +810,6 @@ class dba {
893 810
 	 * @return boolean was the unlock successful?
894 811
 	 */
895 812
 	public static function unlock() {
896
-		self::initialize();
897
-
898 813
 		// See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
899 814
 		self::e("COMMIT");
900 815
 		$success = self::e("UNLOCK TABLES");
@@ -909,8 +824,6 @@ class dba {
909 824
 	 * @return boolean Was the command executed successfully?
910 825
 	 */
911 826
 	public static function transaction() {
912
-		self::initialize();
913
-
914 827
 		if (!self::e('COMMIT')) {
915 828
 			return false;
916 829
 		}
@@ -927,8 +840,6 @@ class dba {
927 840
 	 * @return boolean Was the command executed successfully?
928 841
 	 */
929 842
 	public static function commit() {
930
-		self::initialize();
931
-
932 843
 		if (!self::e('COMMIT')) {
933 844
 			return false;
934 845
 		}
@@ -942,8 +853,6 @@ class dba {
942 853
 	 * @return boolean Was the command executed successfully?
943 854
 	 */
944 855
 	public static function rollback() {
945
-		self::initialize();
946
-
947 856
 		if (!self::e('ROLLBACK')) {
948 857
 			return false;
949 858
 		}
@@ -983,8 +892,6 @@ class dba {
983 892
 	 * @return boolean|array was the delete successfull? When $in_process is set: deletion data
984 893
 	 */
985 894
 	public static function delete($table, $param, $in_process = false, &$callstack = array()) {
986
-		self::initialize();
987
-
988 895
 		$commands = array();
989 896
 
990 897
 		// Create a key for the loop prevention
@@ -997,7 +904,7 @@ class dba {
997 904
 
998 905
 		$callstack[$key] = true;
999 906
 
1000
-		$table = self::$dbo->escape($table);
907
+		$table = self::escape($table);
1001 908
 
1002 909
 		$commands[$key] = array('table' => $table, 'param' => $param);
1003 910
 
@@ -1147,9 +1054,7 @@ class dba {
1147 1054
 	 * @return boolean was the update successfull?
1148 1055
 	 */
1149 1056
 	public static function update($table, $fields, $condition, $old_fields = array()) {
1150
-		self::initialize();
1151
-
1152
-		$table = self::$dbo->escape($table);
1057
+		$table = self::escape($table);
1153 1058
 
1154 1059
 		if (count($condition) > 0) {
1155 1060
 			$array_element = each($condition);
@@ -1226,8 +1131,6 @@ class dba {
1226 1131
 	 * $data = dba::select($table, $fields, $condition, $params);
1227 1132
 	 */
1228 1133
 	public static function select($table, $fields = array(), $condition = array(), $params = array()) {
1229
-		self::initialize();
1230
-
1231 1134
 		if ($table == '') {
1232 1135
 			return false;
1233 1136
 		}
@@ -1295,8 +1198,6 @@ class dba {
1295 1198
 	 * @return array Data array
1296 1199
 	 */
1297 1200
 	public static function inArray($stmt, $do_close = true) {
1298
-		self::initialize();
1299
-
1300 1201
 		if (is_bool($stmt)) {
1301 1202
 			return $stmt;
1302 1203
 		}
@@ -1317,9 +1218,7 @@ class dba {
1317 1218
 	 * @return string Error number (0 if no error)
1318 1219
 	 */
1319 1220
 	public static function errorNo() {
1320
-		self::initialize();
1321
-
1322
-		return self::$dbo->errorno;
1221
+		return self::$errorno;
1323 1222
 	}
1324 1223
 
1325 1224
 	/**
@@ -1328,9 +1227,7 @@ class dba {
1328 1227
 	 * @return string Error message ('' if no error)
1329 1228
 	 */
1330 1229
 	public static function errorMessage() {
1331
-		self::initialize();
1332
-
1333
-		return self::$dbo->error;
1230
+		return self::$error;
1334 1231
 	}
1335 1232
 
1336 1233
 	/**
@@ -1340,13 +1237,11 @@ class dba {
1340 1237
 	 * @return boolean was the close successfull?
1341 1238
 	 */
1342 1239
 	public static function close($stmt) {
1343
-		self::initialize();
1344
-
1345 1240
 		if (!is_object($stmt)) {
1346 1241
 			return false;
1347 1242
 		}
1348 1243
 
1349
-		switch (self::$dbo->driver) {
1244
+		switch (self::$driver) {
1350 1245
 			case 'pdo':
1351 1246
 				return $stmt->closeCursor();
1352 1247
 			case 'mysqli':
@@ -1359,10 +1254,8 @@ class dba {
1359 1254
 }
1360 1255
 
1361 1256
 function dbesc($str) {
1362
-	global $db;
1363
-
1364
-	if ($db && $db->connected) {
1365
-		return($db->escape($str));
1257
+	if (dba::$connected) {
1258
+		return(dba::escape($str));
1366 1259
 	} else {
1367 1260
 		return(str_replace("'","\\'",$str));
1368 1261
 	}
@@ -1379,17 +1272,15 @@ function dbesc($str) {
1379 1272
  * @return array Query array
1380 1273
  */
1381 1274
 function q($sql) {
1382
-	global $db;
1383
-
1384 1275
 	$args = func_get_args();
1385 1276
 	unset($args[0]);
1386 1277
 
1387
-	if (!$db || !$db->connected) {
1278
+	if (!dba::$connected) {
1388 1279
 		return false;
1389 1280
 	}
1390 1281
 
1391
-	$sql = $db->clean_query($sql);
1392
-	$sql = $db->any_value_fallback($sql);
1282
+	$sql = dba::clean_query($sql);
1283
+	$sql = dba::any_value_fallback($sql);
1393 1284
 
1394 1285
 	$stmt = @vsprintf($sql, $args);
1395 1286
 

+ 19
- 24
include/dbstructure.php View File

@@ -17,10 +17,8 @@ const DB_UPDATE_FAILED = 2;      // Database check failed
17 17
  * Converts all tables from MyISAM to InnoDB
18 18
  */
19 19
 function convert_to_innodb() {
20
-	global $db;
21
-
22 20
 	$r = q("SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `engine` = 'MyISAM' AND `table_schema` = '%s'",
23
-		dbesc($db->database_name()));
21
+		dbesc(dba::database_name()));
24 22
 
25 23
 	if (!dbm::is_result($r)) {
26 24
 		echo t('There are no tables on MyISAM.')."\n";
@@ -33,7 +31,7 @@ function convert_to_innodb() {
33 31
 
34 32
 		$result = dba::e($sql);
35 33
 		if (!dbm::is_result($result)) {
36
-			print_update_error($db, $sql);
34
+			print_update_error($sql);
37 35
 		}
38 36
 	}
39 37
 }
@@ -188,20 +186,19 @@ function print_structure($database) {
188 186
 /**
189 187
  * @brief Print out database error messages
190 188
  *
191
- * @param object $db Database object
192 189
  * @param string $message Message to be added to the error message
193 190
  *
194 191
  * @return string Error message
195 192
  */
196
-function print_update_error($db, $message) {
193
+function print_update_error($message) {
197 194
 	echo sprintf(t("\nError %d occurred during database update:\n%s\n"),
198
-		$db->errorno, $db->error);
195
+		dba::errorNo(), dba::errorMessage());
199 196
 
200 197
 	return t('Errors encountered performing database changes: ').$message.EOL;
201 198
 }
202 199
 
203 200
 function update_structure($verbose, $action, $tables=null, $definition=null) {
204
-	global $a, $db;
201
+	global $a;
205 202
 
206 203
 	if ($action) {
207 204
 		Config::set('system', 'maintenance', 1);
@@ -234,8 +231,8 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
234 231
 	}
235 232
 
236 233
 	// MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements
237
-	if ((version_compare($db->server_info(), '5.7.4') >= 0) &&
238
-		!(strpos($db->server_info(), 'MariaDB') !== false)) {
234
+	if ((version_compare(dba::server_info(), '5.7.4') >= 0) &&
235
+		!(strpos(dba::server_info(), 'MariaDB') !== false)) {
239 236
 		$ignore = '';
240 237
 	} else {
241 238
 		$ignore = ' IGNORE';
@@ -249,7 +246,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
249 246
 		if (!isset($database[$name])) {
250 247
 			$r = db_create_table($name, $structure["fields"], $verbose, $action, $structure['indexes']);
251 248
 			if (!dbm::is_result($r)) {
252
-				$errors .= print_update_error($db, $name);
249
+				$errors .= print_update_error($name);
253 250
 			}
254 251
 			$is_new_table = True;
255 252
 		} else {
@@ -446,7 +443,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
446 443
 					} else {
447 444
 						$r = dba::e("CREATE TABLE `".$temp_name."` LIKE `".$name."`;");
448 445
 						if (!dbm::is_result($r)) {
449
-							$errors .= print_update_error($db, $sql3);
446
+							$errors .= print_update_error($sql3);
450 447
 							return $errors;
451 448
 						}
452 449
 					}
@@ -454,7 +451,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
454 451
 
455 452
 				$r = @dba::e($sql3);
456 453
 				if (!dbm::is_result($r)) {
457
-					$errors .= print_update_error($db, $sql3);
454
+					$errors .= print_update_error($sql3);
458 455
 				}
459 456
 				if ($is_unique) {
460 457
 					if ($ignore != "") {
@@ -462,17 +459,17 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
462 459
 					} else {
463 460
 						$r = dba::e("INSERT INTO `".$temp_name."` SELECT ".$field_list." FROM `".$name."`".$group_by.";");
464 461
 						if (!dbm::is_result($r)) {
465
-							$errors .= print_update_error($db, $sql3);
462
+							$errors .= print_update_error($sql3);
466 463
 							return $errors;
467 464
 						}
468 465
 						$r = dba::e("DROP TABLE `".$name."`;");
469 466
 						if (!dbm::is_result($r)) {
470
-							$errors .= print_update_error($db, $sql3);
467
+							$errors .= print_update_error($sql3);
471 468
 							return $errors;
472 469
 						}
473 470
 						$r = dba::e("RENAME TABLE `".$temp_name."` TO `".$name."`;");
474 471
 						if (!dbm::is_result($r)) {
475
-							$errors .= print_update_error($db, $sql3);
472
+							$errors .= print_update_error($sql3);
476 473
 							return $errors;
477 474
 						}
478 475
 					}
@@ -522,7 +519,7 @@ function db_field_command($parameters, $create = true) {
522 519
 }
523 520
 
524 521
 function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
525
-	global $a, $db;
522
+	global $a;
526 523
 
527 524
 	$r = true;
528 525
 
@@ -1761,18 +1758,16 @@ function db_definition() {
1761 1758
  * run from command line
1762 1759
  */
1763 1760
 function dbstructure_run(&$argv, &$argc) {
1764
-	global $a, $db;
1761
+	global $a;
1765 1762
 
1766 1763
 	if (is_null($a)) {
1767 1764
 		$a = new App(dirname(__DIR__));
1768 1765
 	}
1769 1766
 
1770
-	if (is_null($db)) {
1771
-		@include ".htconfig.php";
1772
-		require_once "include/dba.php";
1773
-		$db = new dba($db_host, $db_user, $db_pass, $db_data);
1774
-		unset($db_host, $db_user, $db_pass, $db_data);
1775
-	}
1767
+	@include ".htconfig.php";
1768
+	require_once "include/dba.php";
1769
+	dba::connect($db_host, $db_user, $db_pass, $db_data);
1770
+	unset($db_host, $db_user, $db_pass, $db_data);
1776 1771
 
1777 1772
 	if ($argc == 2) {
1778 1773
 		switch ($argv[1]) {

+ 4
- 4
include/poller.php View File

@@ -19,15 +19,15 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
19 19
 require_once("boot.php");
20 20
 
21 21
 function poller_run($argv, $argc){
22
-	global $a, $db, $poller_up_start, $poller_db_duration;
22
+	global $a, $poller_up_start, $poller_db_duration;
23 23
 
24 24
 	$poller_up_start = microtime(true);
25 25
 
26 26
 	$a = new App(dirname(__DIR__));
27 27
 
28
-	@include(".htconfig.php");
29
-	require_once("include/dba.php");
30
-	$db = new dba($db_host, $db_user, $db_pass, $db_data);
28
+	require_once ".htconfig.php";
29
+	require_once "include/dba.php";
30
+	dba::connect($db_host, $db_user, $db_pass, $db_data);
31 31
 	unset($db_host, $db_user, $db_pass, $db_data);
32 32
 
33 33
 	Config::load();

+ 5
- 7
include/shadowupdate.php View File

@@ -7,18 +7,16 @@ require_once("boot.php");
7 7
 require_once("include/threads.php");
8 8
 
9 9
 function shadowupdate_run(&$argv, &$argc){
10
-	global $a, $db;
10
+	global $a;
11 11
 
12 12
 	if (is_null($a)) {
13 13
 		$a = new App(dirname(__DIR__));
14 14
 	}
15 15
 
16
-	if (is_null($db)) {
17
-		@include(".htconfig.php");
18
-		require_once("include/dba.php");
19
-		$db = new dba($db_host, $db_user, $db_pass, $db_data);
20
-		unset($db_host, $db_user, $db_pass, $db_data);
21
-	}
16
+	@include(".htconfig.php");
17
+	require_once("include/dba.php");
18
+	dba::connect($db_host, $db_user, $db_pass, $db_data);
19
+	unset($db_host, $db_user, $db_pass, $db_data);
22 20
 
23 21
 	Config::load();
24 22
 

+ 2
- 6
include/text.php View File

@@ -702,20 +702,18 @@ $LOGGER_LEVELS = array();
702 702
  * LOGGER_ALL
703 703
  *
704 704
  * @global App $a
705
- * @global dba $db
706 705
  * @global array $LOGGER_LEVELS
707 706
  * @param string $msg
708 707
  * @param int $level
709 708
  */
710 709
 function logger($msg, $level = 0) {
711 710
 	$a = get_app();
712
-	global $db;
713 711
 	global $LOGGER_LEVELS;
714 712
 
715 713
 	// turn off logger in install mode
716 714
 	if (
717 715
 		$a->module == 'install'
718
-		|| ! ($db && $db->connected)
716
+		|| !dba::$connected
719 717
 	) {
720 718
 		return;
721 719
 	}
@@ -776,7 +774,6 @@ function logger($msg, $level = 0) {
776 774
  * LOGGER_ALL
777 775
  *
778 776
  * @global App $a
779
- * @global dba $db
780 777
  * @global array $LOGGER_LEVELS
781 778
  * @param string $msg
782 779
  * @param int $level
@@ -784,12 +781,11 @@ function logger($msg, $level = 0) {
784 781
 
785 782
 function dlogger($msg, $level = 0) {
786 783
 	$a = get_app();
787
-	global $db;
788 784
 
789 785
 	// turn off logger in install mode
790 786
 	if (
791 787
 		$a->module == 'install'
792
-		|| ! ($db && $db->connected)
788
+		|| !dba::$connected
793 789
 	) {
794 790
 		return;
795 791
 	}

+ 9
- 14
include/uimport.php View File

@@ -14,11 +14,6 @@ function last_insert_id() {
14 14
 	return dba::lastInsertId();
15 15
 }
16 16
 
17
-function last_error() {
18
-	global $db;
19
-	return $db->error;
20
-}
21
-
22 17
 /**
23 18
  * Remove columns from array $arr that aren't in table $table
24 19
  *
@@ -103,7 +98,7 @@ function import_account(App $a, $file) {
103 98
 	// check for username
104 99
 	$r = q("SELECT uid FROM user WHERE nickname='%s'", $account['user']['nickname']);
105 100
 	if ($r === false) {
106
-		logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
101
+		logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
107 102
 		notice(t('Error! Cannot check nickname'));
108 103
 		return;
109 104
 	}
@@ -114,7 +109,7 @@ function import_account(App $a, $file) {
114 109
 	// check if username matches deleted account
115 110
 	$r = q("SELECT id FROM userd WHERE username='%s'", $account['user']['nickname']);
116 111
 	if ($r === false) {
117
-		logger("uimport:check nickname : ERROR : " . last_error(), LOGGER_NORMAL);
112
+		logger("uimport:check nickname : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
118 113
 		notice(t('Error! Cannot check nickname'));
119 114
 		return;
120 115
 	}
@@ -144,7 +139,7 @@ function import_account(App $a, $file) {
144 139
 	$r = db_import_assoc('user', $account['user']);
145 140
 	if ($r === false) {
146 141
 		//echo "<pre>"; var_dump($r, $query, mysql_error()); killme();
147
-		logger("uimport:insert user : ERROR : " . last_error(), LOGGER_NORMAL);
142
+		logger("uimport:insert user : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
148 143
 		notice(t("User creation error"));
149 144
 		return;
150 145
 	}
@@ -165,7 +160,7 @@ function import_account(App $a, $file) {
165 160
 		$profile['uid'] = $newuid;
166 161
 		$r = db_import_assoc('profile', $profile);
167 162
 		if ($r === false) {
168
-			logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
163
+			logger("uimport:insert profile " . $profile['profile-name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
169 164
 			info(t("User profile creation error"));
170 165
 			dba::delete('user', array('uid' => $newuid));
171 166
 			return;
@@ -208,7 +203,7 @@ function import_account(App $a, $file) {
208 203
 		$contact['uid'] = $newuid;
209 204
 		$r = db_import_assoc('contact', $contact);
210 205
 		if ($r === false) {
211
-			logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
206
+			logger("uimport:insert contact " . $contact['nick'] . "," . $contact['network'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
212 207
 			$errorcount++;
213 208
 		} else {
214 209
 			$contact['newid'] = last_insert_id();
@@ -222,7 +217,7 @@ function import_account(App $a, $file) {
222 217
 		$group['uid'] = $newuid;
223 218
 		$r = db_import_assoc('group', $group);
224 219
 		if ($r === false) {
225
-			logger("uimport:insert group " . $group['name'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
220
+			logger("uimport:insert group " . $group['name'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
226 221
 		} else {
227 222
 			$group['newid'] = last_insert_id();
228 223
 		}
@@ -249,7 +244,7 @@ function import_account(App $a, $file) {
249 244
 		if ($import == 2) {
250 245
 			$r = db_import_assoc('group_member', $group_member);
251 246
 			if ($r === false) {
252
-				logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
247
+				logger("uimport:insert group member " . $group_member['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
253 248
 			}
254 249
 		}
255 250
 	}
@@ -266,7 +261,7 @@ function import_account(App $a, $file) {
266 261
 		);
267 262
 
268 263
 		if ($r === false) {
269
-			logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
264
+			logger("uimport:insert photo " . $photo['resource-id'] . "," . $photo['scale'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
270 265
 		}
271 266
 	}
272 267
 
@@ -274,7 +269,7 @@ function import_account(App $a, $file) {
274 269
 		$pconfig['uid'] = $newuid;
275 270
 		$r = db_import_assoc('pconfig', $pconfig);
276 271
 		if ($r === false) {
277
-			logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . last_error(), LOGGER_NORMAL);
272
+			logger("uimport:insert pconfig " . $pconfig['id'] . " : ERROR : " . dba::errorMessage(), LOGGER_NORMAL);
278 273
 		}
279 274
 	}
280 275
 

+ 1
- 1
index.php View File

@@ -51,7 +51,7 @@ if (!$install) {
51 51
 require_once "include/dba.php";
52 52
 
53 53
 if (!$install) {
54
-	$db = new dba($db_host, $db_user, $db_pass, $db_data, $install);
54
+	dba::connect($db_host, $db_user, $db_pass, $db_data, $install);
55 55
 	unset($db_host, $db_user, $db_pass, $db_data);
56 56
 
57 57
 	/**

+ 3
- 4
mod/admin.php View File

@@ -606,10 +606,9 @@ function admin_page_queue(App $a) {
606 606
  * @return string
607 607
  */
608 608
 function admin_page_summary(App $a) {
609
-	global $db;
610 609
 	// are there MyISAM tables in the DB? If so, trigger a warning message
611 610
 	$r = q("SELECT `engine` FROM `information_schema`.`tables` WHERE `engine` = 'myisam' AND `table_schema` = '%s' LIMIT 1",
612
-		dbesc($db->database_name()));
611
+		dbesc(dba::database_name()));
613 612
 	$showwarning = false;
614 613
 	$warningtext = array();
615 614
 	if (dbm::is_result($r)) {
@@ -726,7 +725,7 @@ function admin_page_site_post(App $a) {
726 725
 		$old_host = str_replace("http://", "@", normalise_link($old_url));
727 726
 
728 727
 		function update_table($table_name, $fields, $old_url, $new_url) {
729
-			global $db, $a;
728
+			global $a;
730 729
 
731 730
 			$dbold = dbesc($old_url);
732 731
 			$dbnew = dbesc($new_url);
@@ -743,7 +742,7 @@ function admin_page_site_post(App $a) {
743 742
 			$q = sprintf("UPDATE %s SET %s;", $table_name, $upds);
744 743
 			$r = q($q);
745 744
 			if (!$r) {
746
-				notice("Failed updating '$table_name': ".$db->error);
745
+				notice("Failed updating '$table_name': ".dba::errorMessage());
747 746
 				goaway('admin/site');
748 747
 			}
749 748
 		}

+ 0
- 2
mod/directory.php View File

@@ -24,8 +24,6 @@ function directory_post(App $a) {
24 24
 }
25 25
 
26 26
 function directory_content(App $a) {
27
-	global $db;
28
-
29 27
 	require_once("mod/proxy.php");
30 28
 
31 29
 	if((get_config('system','block_public')) && (! local_user()) && (! remote_user()) ||

+ 7
- 9
mod/install.php View File

@@ -28,7 +28,7 @@ function install_init(App $a) {
28 28
 }
29 29
 
30 30
 function install_post(App $a) {
31
-	global $install_wizard_pass, $db;
31
+	global $install_wizard_pass;
32 32
 
33 33
 	switch($install_wizard_pass) {
34 34
 		case 1:
@@ -44,9 +44,7 @@ function install_post(App $a) {
44 44
 			$phpath = notags(trim($_POST['phpath']));
45 45
 
46 46
 			require_once("include/dba.php");
47
-			unset($db);
48
-			$db = new dba($dbhost, $dbuser, $dbpass, $dbdata, true);
49
-			if (!$db->connected) {
47
+			if (!dba::connect($dbhost, $dbuser, $dbpass, $dbdata, true)) {
50 48
 				$a->data['db_conn_failed'] = true;
51 49
 			}
52 50
 
@@ -65,7 +63,7 @@ function install_post(App $a) {
65 63
 			$rino = 2;
66 64
 
67 65
 			// connect to db
68
-			$db = new dba($dbhost, $dbuser, $dbpass, $dbdata, true);
66
+			dba::connect($dbhost, $dbuser, $dbpass, $dbdata, true);
69 67
 
70 68
 			$tpl = get_markup_template('htconfig.tpl');
71 69
 			$txt = replace_macros($tpl,array(
@@ -87,7 +85,7 @@ function install_post(App $a) {
87 85
 				$a->data['txt'] = $txt;
88 86
 			}
89 87
 
90
-			$errors = load_database($db);
88
+			$errors = load_database();
91 89
 
92 90
 
93 91
 			if ($errors) {
@@ -103,7 +101,7 @@ function install_post(App $a) {
103 101
 
104 102
 function install_content(App $a) {
105 103
 
106
-	global $install_wizard_pass, $db;
104
+	global $install_wizard_pass;
107 105
 	$o = '';
108 106
 	$wizard_status = "";
109 107
 	$install_title = t('Friendica Communications Server - Setup');
@@ -133,7 +131,7 @@ function install_content(App $a) {
133 131
 		$db_return_text .= $txt;
134 132
 	}
135 133
 
136
-	if ($db && $db->connected) {
134
+	if (dba::$connected) {
137 135
 		$r = q("SELECT COUNT(*) as `total` FROM `user`");
138 136
 		if (dbm::is_result($r) && $r[0]['total']) {
139 137
 			$tpl = get_markup_template('install.tpl');
@@ -534,7 +532,7 @@ function load_database_rem($v, $i) {
534 532
 	}
535 533
 }
536 534
 
537
-function load_database($db) {
535
+function load_database() {
538 536
 
539 537
 	require_once("include/dbstructure.php");
540 538
 	$errors = update_structure(false, true);

+ 1
- 1
util/db_update.php View File

@@ -18,7 +18,7 @@ $lang = get_browser_language();
18 18
 load_translation_table($lang);
19 19
 
20 20
 require_once("include/dba.php");
21
-$db = new dba($db_host, $db_user, $db_pass, $db_data, false);
21
+dba::connect($db_host, $db_user, $db_pass, $db_data, false);
22 22
 unset($db_host, $db_user, $db_pass, $db_data);
23 23
 
24 24
 $build = get_config('system', 'build');

+ 1
- 1
util/maintenance.php View File

@@ -12,7 +12,7 @@ $lang = get_browser_language();
12 12
 load_translation_table($lang);
13 13
 
14 14
 require_once("include/dba.php");
15
-$db = new dba($db_host, $db_user, $db_pass, $db_data, false);
15
+dba::connect($db_host, $db_user, $db_pass, $db_data, false);
16 16
 unset($db_host, $db_user, $db_pass, $db_data);
17 17
 
18 18
 Config::load();

Loading…
Cancel
Save