diff options
-rw-r--r-- | Zotlabs/Module/Setup.php | 38 | ||||
-rw-r--r-- | include/cli_startup.php | 6 | ||||
-rwxr-xr-x | include/dba/dba_driver.php | 121 | ||||
-rw-r--r-- | include/network.php | 2 | ||||
-rw-r--r-- | include/text.php | 16 | ||||
-rwxr-xr-x | index.php | 2 |
6 files changed, 98 insertions, 87 deletions
diff --git a/Zotlabs/Module/Setup.php b/Zotlabs/Module/Setup.php index 5a4a87866..3ac67e1c0 100644 --- a/Zotlabs/Module/Setup.php +++ b/Zotlabs/Module/Setup.php @@ -57,7 +57,7 @@ class Setup extends \Zotlabs\Web\Controller { * @param[in,out] App &$a */ function post() { - + global $db; switch($this->install_wizard_pass) { case 1: @@ -82,14 +82,37 @@ class Setup extends \Zotlabs\Web\Controller { $siteurl = rtrim($siteurl,'/'); require_once('include/dba/dba_driver.php'); - $db = null; - $db = \DBA::dba_factory($dbhost, $dbport, $dbuser, $dbpass, $dbdata, $dbtype, true); + unset($db); + $db = dba_factory($dbhost, $dbport, $dbuser, $dbpass, $dbdata, $dbtype, true); if(! $db->connected) { echo 'Database Connect failed: ' . $db->error; killme(); \App::$data['db_conn_failed']=true; } + /*if(get_db_errno()) { + unset($db); + $db = dba_factory($dbhost, $dbport, $dbuser, $dbpass, '', true); + + if(! get_db_errno()) { + $r = q("CREATE DATABASE '%s'", + dbesc($dbdata) + ); + if($r) { + unset($db); + $db = new dba($dbhost, $dbport, $dbuser, $dbpass, $dbdata, true); + } else { + \App::$data['db_create_failed']=true; + } + } else { + \App::$data['db_conn_failed']=true; + return; + } + }*/ + //if(get_db_errno()) { + + //} + return; break; case 4: @@ -116,7 +139,7 @@ class Setup extends \Zotlabs\Web\Controller { } // connect to db - $db = \DBA::dba_factory($dbhost, $dbport, $dbuser, $dbpass, $dbdata, $dbtype, true); + $db = dba_factory($dbhost, $dbport, $dbuser, $dbpass, $dbdata, $dbtype, true); if(! $db->connected) { echo 'CRITICAL: DB not connected.'; @@ -172,6 +195,7 @@ class Setup extends \Zotlabs\Web\Controller { * @return string parsed HTML output */ function get() { + global $db; $o = ''; $wizard_status = ''; @@ -204,7 +228,7 @@ class Setup extends \Zotlabs\Web\Controller { $txt .= "<pre>".\App::$data['db_failed'] . "</pre>". EOL ; $db_return_text .= $txt; } - if(\DBA::$dba && \DBA::$dba->connected) { + if($db && $db->connected) { $r = q("SELECT COUNT(*) as `total` FROM `account`"); if($r && count($r) && $r[0]['total']) { $tpl = get_markup_template('install.tpl'); @@ -669,12 +693,12 @@ class Setup extends \Zotlabs\Web\Controller { function load_database($db) { - $str = file_get_contents(\DBA::$dba->get_install_script()); + $str = file_get_contents($db->get_install_script()); $arr = explode(';',$str); $errors = false; foreach($arr as $a) { if(strlen(trim($a))) { - $r = @\DBA::$dba->q(trim($a)); + $r = @$db->q(trim($a)); if(! $r) { $errors .= t('Errors encountered creating database tables.') . $a . EOL; } diff --git a/include/cli_startup.php b/include/cli_startup.php index a226f1345..a99164d4c 100644 --- a/include/cli_startup.php +++ b/include/cli_startup.php @@ -1,7 +1,6 @@ <?php /** @file */ require_once('boot.php'); -require_once('include/dba/dba_driver.php'); // Everything we need to boot standalone 'background' processes @@ -15,7 +14,7 @@ function cli_startup() { App::init(); - if(! DBA::$dba) { + if(is_null($db)) { @include(".htconfig.php"); $a->convert(); @@ -26,7 +25,8 @@ function cli_startup() { App::$timezone = ((x($default_timezone)) ? $default_timezone : 'UTC'); date_default_timezone_set(App::$timezone); - $db = DBA::dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type); + require_once('include/dba/dba_driver.php'); + $db = dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type); unset($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type); }; diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 4bad70323..3c5b0b67e 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -7,57 +7,44 @@ * functions for working with databases. */ - -class DBA { - - static public $dba = null; - static public $dbtype = null; - - - /** - * @brief Returns the database driver object. - * - * If available it will use PHP's mysqli otherwise mysql driver. - * - * @param string $server DB server name - * @param string $port DB port - * @param string $user DB username - * @param string $pass DB password - * @param string $db database name - * @param string $dbtype 0 for mysql, 1 for postgres - * @param bool $install Defaults to false - * @return null|dba_driver A database driver object (dba_mysql|dba_mysqli) or null if no driver found. - */ - - function dba_factory($server, $port,$user,$pass,$db,$dbtype,$install = false) { - - self::$dba = null; - - self::$dbtype = intval($dbtype); - - if(self::$dbtype == DBTYPE_POSTGRES) { - require_once('include/dba/dba_postgres.php'); - if(is_null($port)) $port = 5432; - self::$dba = new dba_postgres($server, $port, $user, $pass, $db, $install); - } - else { - if(class_exists('mysqli')) { - if (is_null($port)) $port = ini_get("mysqli.default_port"); - require_once('include/dba/dba_mysqli.php'); - self::$dba = new dba_mysqli($server, $port,$user,$pass,$db,$install); - } - else { - // UNSUPPORTED, OBSOLETE - if (is_null($port)) $port = "3306"; - require_once('include/dba/dba_mysql.php'); - self::$dba = new dba_mysql($server, $port,$user,$pass,$db,$install); - } +/** + * @brief Returns the database driver object. + * + * If available it will use PHP's mysqli otherwise mysql driver. + * + * @param string $server DB server name + * @param string $port DB port + * @param string $user DB username + * @param string $pass DB password + * @param string $db database name + * @param string $dbtype 0 for mysql, 1 for postgres + * @param bool $install Defaults to false + * @return null|dba_driver A database driver object (dba_mysql|dba_mysqli) or null if no driver found. + */ +function dba_factory($server, $port,$user,$pass,$db,$dbtype,$install = false) { + $dba = null; + + $dbtype = intval($dbtype); + + if($dbtype == DBTYPE_POSTGRES) { + require_once('include/dba/dba_postgres.php'); + if(is_null($port)) $port = 5432; + $dba = new dba_postgres($server, $port, $user, $pass, $db, $install); + } else { + if(class_exists('mysqli')) { + if (is_null($port)) $port = ini_get("mysqli.default_port"); + require_once('include/dba/dba_mysqli.php'); + $dba = new dba_mysqli($server, $port,$user,$pass,$db,$install); + } else { + if (is_null($port)) $port = "3306"; + require_once('include/dba/dba_mysql.php'); + $dba = new dba_mysql($server, $port,$user,$pass,$db,$install); } - - define('NULL_DATE', self::$dba->get_null_date()); - define('ACTIVE_DBTYPE', self::$dbtype); - return self::$dba; } + + define('NULL_DATE', $dba->get_null_date()); + define('ACTIVE_DBTYPE', $dbtype); + return $dba; } /** @@ -66,7 +53,6 @@ class DBA { * This class gets extended by the real database driver classes, e.g. dba_mysql, * dba_mysqli. */ - abstract class dba_driver { // legacy behavior const INSTALL_SCRIPT='install/schema_mysql.sql'; @@ -217,10 +203,10 @@ function printable($s) { * @param int $state 0 to disable debugging */ function dbg($state) { -// global $db; + global $db; - if(DBA::$dba) - DBA::$dba->dbg($state); + if($db) + $db->dbg($state); } /** @@ -234,20 +220,21 @@ function dbg($state) { * @return Return an escaped string of the value to pass to a DB query. */ function dbesc($str) { - if(DBA::$dba && DBA::$dba->connected) - return(DBA::$dba->escape($str)); + global $db; + + if($db && $db->connected) + return($db->escape($str)); else return(str_replace("'", "\\'", $str)); } - function dbescbin($str) { global $db; - return DBA::$dba->escapebin($str); + return $db->escapebin($str); } function dbunescbin($str) { global $db; - return DBA::$dba->unescapebin($str); + return $db->unescapebin($str); } function dbescdate($date) { @@ -261,27 +248,27 @@ function dbescdate($date) { function db_quoteinterval($txt) { global $db; - return DBA::$dba->quote_interval($txt); + return $db->quote_interval($txt); } function dbesc_identifier($str) { global $db; - return DBA::$dba->escape_identifier($str); + return $db->escape_identifier($str); } function db_utcnow() { global $db; - return DBA::$dba->utcnow(); + return $db->utcnow(); } function db_optimizetable($table) { global $db; - DBA::$dba->optimize_table($table); + $db->optimize_table($table); } function db_concat($fld, $sep) { global $db; - return DBA::$dba->concat($fld, $sep); + return $db->concat($fld, $sep); } // Function: q($sql,$args); @@ -311,7 +298,7 @@ function q($sql) { $args = func_get_args(); unset($args[0]); - if(DBA::$dba && DBA::$dba->connected) { + if($db && $db->connected) { $stmt = vsprintf($sql, $args); if($stmt === false) { if(version_compare(PHP_VERSION, '5.4.0') >= 0) @@ -320,7 +307,7 @@ function q($sql) { else logger('dba: vsprintf error: ' . print_r(debug_backtrace(), true),LOGGER_NORMAL,LOG_CRIT); } - return DBA::$dba->q($stmt); + return $db->q($stmt); } /* @@ -342,8 +329,8 @@ function q($sql) { function dbq($sql) { global $db; - if(DBA::$dba && DBA::$dba->connected) - $ret = DBA::$dba->q($sql); + if($db && $db->connected) + $ret = $db->q($sql); else $ret = false; diff --git a/include/network.php b/include/network.php index f822b644d..ec255581d 100644 --- a/include/network.php +++ b/include/network.php @@ -2035,7 +2035,7 @@ function get_site_info() { 'admin' => $admin, 'site_name' => (($site_name) ? $site_name : ''), 'platform' => Zotlabs\Project\System::get_platform_name(), - 'dbdriver' => \DBA::$dba->getdriver(), + 'dbdriver' => $db->getdriver(), 'lastpoll' => get_config('system','lastpoll'), 'info' => (($site_info) ? $site_info : ''), 'channels_total' => $channels_total_stat, diff --git a/include/text.php b/include/text.php index f27abf80b..0a7f84b01 100644 --- a/include/text.php +++ b/include/text.php @@ -540,11 +540,11 @@ function attribute_contains($attr, $s) { */ function logger($msg, $level = LOGGER_NORMAL, $priority = LOG_INFO) { - - require_once('include/dba/dba_driver.php'); - // turn off logger in install mode - if((App::$module == 'install') || (! (DBA::$dba && DBA::$dba->connected))) + global $a; + global $db; + + if((App::$module == 'install') || (! ($db && $db->connected))) return; $debugging = get_config('system', 'debugging'); @@ -621,11 +621,11 @@ function log_priority_str($priority) { * @param int $level A log level. */ function dlogger($msg, $level = 0) { - - require_once('include/dba/dba_driver.php'); - // turn off logger in install mode - if((App::$module == 'install') || (! (DBA::$dba && DBA::$dba->connected))) + global $a; + global $db; + + if((App::$module == 'install') || (! ($db && $db->connected))) return; $debugging = get_config('system','debugging'); @@ -47,7 +47,7 @@ date_default_timezone_set(App::$timezone); require_once('include/dba/dba_driver.php'); if(! App::$install) { - $db = DBA::dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type, App::$install); + $db = dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type, App::$install); if(! $db->connected) { system_unavailable(); } |