From 516c43ba154dc43ec0ef6135fb0a9732d0185a6f Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 24 May 2016 17:54:45 -0700 Subject: more work associated with DBA and index.php shuffle --- include/cli_startup.php | 22 ++++--- include/dba/dba_driver.php | 139 +++++++++++++++++++++------------------------ include/text.php | 25 ++++---- 3 files changed, 89 insertions(+), 97 deletions(-) (limited to 'include') diff --git a/include/cli_startup.php b/include/cli_startup.php index b89d5f1bb..1436fe1ef 100644 --- a/include/cli_startup.php +++ b/include/cli_startup.php @@ -6,7 +6,7 @@ require_once('boot.php'); function cli_startup() { - global $a, $db, $default_timezone; + global $default_timezone; if(is_null($a)) { $a = new miniApp; @@ -14,21 +14,19 @@ function cli_startup() { App::init(); - if(! DBA::$connected) { - @include(".htconfig.php"); + @include(".htconfig.php"); - $a->convert(); + $a->convert(); - if(! defined('UNO')) - define('UNO', 0); + if(! defined('UNO')) + define('UNO', 0); - App::$timezone = ((x($default_timezone)) ? $default_timezone : 'UTC'); - date_default_timezone_set(App::$timezone); + App::$timezone = ((x($default_timezone)) ? $default_timezone : 'UTC'); + date_default_timezone_set(App::$timezone); - require_once('include/dba/dba_driver.php'); - $db = DBA::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); - }; + require_once('include/dba/dba_driver.php'); + DBA::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); App::$session = new Zotlabs\Web\Session(); App::$session->init(); diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 52613a45e..0ab5830ed 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -1,74 +1,77 @@ connected) { - $dns = ((self::$dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql') - . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) - . ';dbname=' . $db; - self::$dba->pdo_set(array($dns,$user,$pass)); - } + if(is_object(self::$dba) && self::$dba->connected) { + $dns = ((self::$dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql') + . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) + . ';dbname=' . $db; + self::$dba->pdo_set(array($dns,$user,$pass)); + } - define('NULL_DATE', self::$dba->get_null_date()); - define('ACTIVE_DBTYPE', self::$dbtype); - return self::$dba; -} + define('NULL_DATE', self::$dba->get_null_date()); + define('ACTIVE_DBTYPE', self::$dbtype); + return self::$dba; + } } @@ -256,7 +259,6 @@ function dbg($state) { * @return Return an escaped string of the value to pass to a DB query. */ function dbesc($str) { - global $db; if(\DBA::$dba && \DBA::$dba->connected) return(\DBA::$dba->escape($str)); @@ -264,12 +266,10 @@ function dbesc($str) { return(str_replace("'", "\\'", $str)); } function dbescbin($str) { - global $db; return \DBA::$dba->escapebin($str); } function dbunescbin($str) { - global $db; return \DBA::$dba->unescapebin($str); } @@ -283,36 +283,25 @@ function dbescdate($date) { } function db_quoteinterval($txt) { - global $db; return \DBA::$dba->quote_interval($txt); } function dbesc_identifier($str) { - global $db; return \DBA::$dba->escape_identifier($str); } function db_utcnow() { - global $db; return \DBA::$dba->utcnow(); } function db_optimizetable($table) { - global $db; \DBA::$dba->optimize_table($table); } function db_concat($fld, $sep) { - global $db; return \DBA::$dba->concat($fld, $sep); } -// Function: q($sql,$args); -// Description: execute SQL query with printf style args. -// Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d", -// 'user', 1); - - /** * @brief Execute a SQL query with printf style args. * @@ -328,8 +317,8 @@ function db_concat($fld, $sep) { * @param string $sql The SQL query to execute * @return bool|array */ + function q($sql) { - global $db; $args = func_get_args(); unset($args[0]); @@ -343,13 +332,14 @@ function q($sql) { else db_logger('dba: vsprintf error: ' . print_r(debug_backtrace(), true),LOGGER_NORMAL,LOG_CRIT); } - return $db->q($stmt); + return \DBA::$dba->q($stmt); } /* * This will happen occasionally trying to store the * session data after abnormal program termination */ + db_logger('dba: no database: ' . print_r($args,true),LOGGER_NORMAL,LOG_CRIT); return false; @@ -363,7 +353,6 @@ function q($sql) { * @param string $sql The SQL query to execute */ function dbq($sql) { - global $db; if(\DBA::$dba && \DBA::$dba->connected) $ret = \DBA::$dba->q($sql); @@ -427,11 +416,11 @@ function db_getfunc($f) { // The logger function may make DB calls internally to query the system logging parameters. // This can cause a recursion if database debugging is enabled. -// So this function preserves the current database debugging state and then turns it off while -// doing the logger() call +// So this function preserves the current database debugging state and then turns it off +// temporarily while doing the logger() call function db_logger($s,$level = LOGGER_NORMAL,$syslog = LOG_INFO) { - global $db; + $saved = \DBA::$dba->debug; \DBA::$dba->debug = false; logger($s,$level,$syslog); diff --git a/include/text.php b/include/text.php index 21692e74f..caaef664f 100644 --- a/include/text.php +++ b/include/text.php @@ -567,21 +567,25 @@ function attribute_contains($attr, $s) { */ function logger($msg, $level = LOGGER_NORMAL, $priority = LOG_INFO) { - // turn off logger in install mode - global $a; - global $db; - - if((App::$module == 'install') || (! (DBA::$dba && DBA::$dba->connected))) - return; - $debugging = get_config('system', 'debugging'); - $loglevel = intval(get_config('system', 'loglevel')); - $logfile = get_config('system', 'logfile'); + if(App::$module == 'setup' && is_writable('install.log')) { + $debugging = true; + $logfile = 'install.log'; + $loglevel = LOGGER_ALL; + } + else { + $debugging = get_config('system', 'debugging'); + $loglevel = intval(get_config('system', 'loglevel')); + $logfile = get_config('system', 'logfile'); + } if((! $debugging) || (! $logfile) || ($level > $loglevel)) return; $where = ''; + + // We require > 5.4 but leave the version check so that install issues (including version) can be logged + if(version_compare(PHP_VERSION, '5.4.0') >= 0) { $stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); $where = basename($stack[0]['file']) . ':' . $stack[0]['line'] . ':' . $stack[1]['function'] . ': '; @@ -590,7 +594,8 @@ function logger($msg, $level = LOGGER_NORMAL, $priority = LOG_INFO) { $s = datetime_convert() . ':' . log_priority_str($priority) . ':' . session_id() . ':' . $where . $msg . PHP_EOL; $pluginfo = array('filename' => $logfile, 'loglevel' => $level, 'message' => $s,'priority' => $priority, 'logged' => false); - call_hooks('logger',$pluginfo); + if(! (App::$module == 'setup')) + call_hooks('logger',$pluginfo); if(! $pluginfo['logged']) @file_put_contents($pluginfo['filename'], $pluginfo['message'], FILE_APPEND); -- cgit v1.2.3