more work associated with DBA and index.php shuffle

This commit is contained in:
redmatrix 2016-05-24 17:54:45 -07:00
parent 84ba6393ad
commit 516c43ba15
6 changed files with 106 additions and 140 deletions

View File

@ -12,7 +12,6 @@ namespace Zotlabs\Module;
/** /**
* @brief Initialisation for the setup module. * @brief Initialisation for the setup module.
* *
* @param[in,out] App &$a
*/ */
class Setup extends \Zotlabs\Web\Controller { class Setup extends \Zotlabs\Web\Controller {
@ -54,16 +53,15 @@ class Setup extends \Zotlabs\Web\Controller {
/** /**
* @brief Handle the actions of the different setup steps. * @brief Handle the actions of the different setup steps.
* *
* @param[in,out] App &$a
*/ */
function post() {
global $db; function post() {
switch($this->install_wizard_pass) { switch($this->install_wizard_pass) {
case 1: case 1:
case 2: case 2:
return; return;
break; // just in case return don't return :) // implied break;
case 3: case 3:
$urlpath = \App::get_path(); $urlpath = \App::get_path();
$dbhost = trim($_POST['dbhost']); $dbhost = trim($_POST['dbhost']);
@ -88,33 +86,9 @@ class Setup extends \Zotlabs\Web\Controller {
if(! \DBA::$dba->connected) { if(! \DBA::$dba->connected) {
echo 'Database Connect failed: ' . DBA::$dba->error; echo 'Database Connect failed: ' . DBA::$dba->error;
killme(); 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; return;
break; // implied break;
case 4: case 4:
$urlpath = \App::get_path(); $urlpath = \App::get_path();
$dbhost = notags(trim($_POST['dbhost'])); $dbhost = notags(trim($_POST['dbhost']));
@ -177,6 +151,8 @@ class Setup extends \Zotlabs\Web\Controller {
\App::$data['db_installed'] = true; \App::$data['db_installed'] = true;
return; return;
// implied break;
default:
break; break;
} }
} }
@ -193,11 +169,10 @@ class Setup extends \Zotlabs\Web\Controller {
* *
* Depending on the state we are currently in it returns different content. * Depending on the state we are currently in it returns different content.
* *
* @param App &$a
* @return string parsed HTML output * @return string parsed HTML output
*/ */
function get() {
global $db; function get() {
$o = ''; $o = '';
$wizard_status = ''; $wizard_status = '';

View File

@ -1,19 +1,16 @@
<?php /** @file */ <?php /** @file */
namespace Zotlabs\Web;
class WebServer { class WebServer {
public function run() { public function run() {
global $db;
/* /*
* Bootstrap the application, load configuration, load modules, load theme, etc. * Bootstrap the application, load configuration, load modules, load theme, etc.
*/ */
/*
* bootstrap the application
*/
require_once('boot.php'); require_once('boot.php');
if(file_exists('.htsite.php')) if(file_exists('.htsite.php'))
@ -54,8 +51,8 @@ class WebServer {
require_once('include/dba/dba_driver.php'); require_once('include/dba/dba_driver.php');
if(! \App::$install) { if(! \App::$install) {
$db = DBA::dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type, \App::$install); \DBA::dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type, \App::$install);
if(! $db->connected) { if(! \DBA::$dba->connected) {
system_unavailable(); system_unavailable();
} }
@ -69,7 +66,7 @@ class WebServer {
load_config('system'); load_config('system');
load_config('feature'); load_config('feature');
\App::$session = new Zotlabs\Web\Session(); \App::$session = new Session();
\App::$session->init(); \App::$session->init();
load_hooks(); load_hooks();
call_hooks('init_1'); call_hooks('init_1');
@ -150,7 +147,7 @@ class WebServer {
nav_set_selected('nothing'); nav_set_selected('nothing');
$Router = new Zotlabs\Web\Router($a); $Router = new Router($a);
/* initialise content region */ /* initialise content region */

View File

@ -6,7 +6,7 @@ require_once('boot.php');
function cli_startup() { function cli_startup() {
global $a, $db, $default_timezone; global $default_timezone;
if(is_null($a)) { if(is_null($a)) {
$a = new miniApp; $a = new miniApp;
@ -14,21 +14,19 @@ function cli_startup() {
App::init(); App::init();
if(! DBA::$connected) { @include(".htconfig.php");
@include(".htconfig.php");
$a->convert(); $a->convert();
if(! defined('UNO')) if(! defined('UNO'))
define('UNO', 0); define('UNO', 0);
App::$timezone = ((x($default_timezone)) ? $default_timezone : 'UTC'); App::$timezone = ((x($default_timezone)) ? $default_timezone : 'UTC');
date_default_timezone_set(App::$timezone); date_default_timezone_set(App::$timezone);
require_once('include/dba/dba_driver.php'); require_once('include/dba/dba_driver.php');
$db = DBA::dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type); 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); unset($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type);
};
App::$session = new Zotlabs\Web\Session(); App::$session = new Zotlabs\Web\Session();
App::$session->init(); App::$session->init();

View File

@ -1,74 +1,77 @@
<?php <?php
class DBA { class DBA {
/**
* @file dba_driver.php
* @brief some database related functions and abstract driver class.
*
* This file contains the abstract database driver class dba_driver and some
* functions for working with databases.
*/
static public $dba = null; /**
static public $dbtype = null; * @file dba_driver.php
* @brief some database related functions and abstract driver class.
*
* This file contains the abstract database driver class dba_driver and some
* functions for working with databases.
*/
static public $dba = null;
static public $dbtype = null;
/** /**
* @brief Returns the database driver object. * @brief Returns the database driver object.
* *
* If available it will use PHP's mysqli otherwise mysql driver. * If available it will use PHP's mysqli otherwise mysql driver.
* *
* @param string $server DB server name * @param string $server DB server name
* @param string $port DB port * @param string $port DB port
* @param string $user DB username * @param string $user DB username
* @param string $pass DB password * @param string $pass DB password
* @param string $db database name * @param string $db database name
* @param string $dbtype 0 for mysql, 1 for postgres * @param string $dbtype 0 for mysql, 1 for postgres
* @param bool $install Defaults to false * @param bool $install Defaults to false
* @return null|dba_driver A database driver object (dba_mysql|dba_mysqli) or null if no driver found. * @return null|dba_driver A database driver object (dba_mysql|dba_mysqli) or null if no driver found.
*/ */
static public function dba_factory($server, $port,$user,$pass,$db,$dbtype,$install = false) {
self::$dba = null; static public function dba_factory($server, $port,$user,$pass,$db,$dbtype,$install = false) {
self::$dbtype = intval($dbtype); self::$dba = null;
$set_port = $port;
if(self::$dbtype == DBTYPE_POSTGRES) { self::$dbtype = intval($dbtype);
require_once('include/dba/dba_postgres.php'); $set_port = $port;
if(is_null($port)) $set_port = 5432;
self::$dba = new dba_postgres($server, $set_port, $user, $pass, $db, $install);
} else {
// Highly experimental at the present time. if(self::$dbtype == DBTYPE_POSTGRES) {
// require_once('include/dba/dba_pdo.php'); require_once('include/dba/dba_postgres.php');
// self::$dba = new dba_pdo($server, $set_port,$user,$pass,$db,$install); if(is_null($port)) $set_port = 5432;
// } self::$dba = new dba_postgres($server, $set_port, $user, $pass, $db, $install);
if(class_exists('mysqli')) {
if (is_null($port)) $set_port = ini_get("mysqli.default_port");
require_once('include/dba/dba_mysqli.php');
self::$dba = new dba_mysqli($server, $set_port,$user,$pass,$db,$install);
} }
} else {
// Until we have a proper PDO driver, store the DB connection parameters for // Highly experimental at the present time.
// plugins/addons which use PDO natively (such as cdav). This is wasteful as // require_once('include/dba/dba_pdo.php');
// it opens a separate connection to the DB, but saves a lot of effort re-writing // self::$dba = new dba_pdo($server, $set_port,$user,$pass,$db,$install);
// third-party interfaces that are working and well tested. // }
if(class_exists('mysqli')) {
if (is_null($port)) $set_port = ini_get("mysqli.default_port");
require_once('include/dba/dba_mysqli.php');
self::$dba = new dba_mysqli($server, $set_port,$user,$pass,$db,$install);
}
}
// Until we have a proper PDO driver, store the DB connection parameters for
// plugins/addons which use PDO natively (such as cdav). This is wasteful as
// it opens a separate connection to the DB, but saves a lot of effort re-writing
// third-party interfaces that are working and well tested.
if(is_object(self::$dba) && self::$dba->connected) { if(is_object(self::$dba) && self::$dba->connected) {
$dns = ((self::$dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql') $dns = ((self::$dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql')
. ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port)
. ';dbname=' . $db; . ';dbname=' . $db;
self::$dba->pdo_set(array($dns,$user,$pass)); self::$dba->pdo_set(array($dns,$user,$pass));
} }
define('NULL_DATE', self::$dba->get_null_date()); define('NULL_DATE', self::$dba->get_null_date());
define('ACTIVE_DBTYPE', self::$dbtype); define('ACTIVE_DBTYPE', self::$dbtype);
return self::$dba; return self::$dba;
} }
} }
@ -256,7 +259,6 @@ function dbg($state) {
* @return Return an escaped string of the value to pass to a DB query. * @return Return an escaped string of the value to pass to a DB query.
*/ */
function dbesc($str) { function dbesc($str) {
global $db;
if(\DBA::$dba && \DBA::$dba->connected) if(\DBA::$dba && \DBA::$dba->connected)
return(\DBA::$dba->escape($str)); return(\DBA::$dba->escape($str));
@ -264,12 +266,10 @@ function dbesc($str) {
return(str_replace("'", "\\'", $str)); return(str_replace("'", "\\'", $str));
} }
function dbescbin($str) { function dbescbin($str) {
global $db;
return \DBA::$dba->escapebin($str); return \DBA::$dba->escapebin($str);
} }
function dbunescbin($str) { function dbunescbin($str) {
global $db;
return \DBA::$dba->unescapebin($str); return \DBA::$dba->unescapebin($str);
} }
@ -283,36 +283,25 @@ function dbescdate($date) {
} }
function db_quoteinterval($txt) { function db_quoteinterval($txt) {
global $db;
return \DBA::$dba->quote_interval($txt); return \DBA::$dba->quote_interval($txt);
} }
function dbesc_identifier($str) { function dbesc_identifier($str) {
global $db;
return \DBA::$dba->escape_identifier($str); return \DBA::$dba->escape_identifier($str);
} }
function db_utcnow() { function db_utcnow() {
global $db;
return \DBA::$dba->utcnow(); return \DBA::$dba->utcnow();
} }
function db_optimizetable($table) { function db_optimizetable($table) {
global $db;
\DBA::$dba->optimize_table($table); \DBA::$dba->optimize_table($table);
} }
function db_concat($fld, $sep) { function db_concat($fld, $sep) {
global $db;
return \DBA::$dba->concat($fld, $sep); 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. * @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 * @param string $sql The SQL query to execute
* @return bool|array * @return bool|array
*/ */
function q($sql) { function q($sql) {
global $db;
$args = func_get_args(); $args = func_get_args();
unset($args[0]); unset($args[0]);
@ -343,13 +332,14 @@ function q($sql) {
else else
db_logger('dba: vsprintf error: ' . print_r(debug_backtrace(), true),LOGGER_NORMAL,LOG_CRIT); 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 * This will happen occasionally trying to store the
* session data after abnormal program termination * session data after abnormal program termination
*/ */
db_logger('dba: no database: ' . print_r($args,true),LOGGER_NORMAL,LOG_CRIT); db_logger('dba: no database: ' . print_r($args,true),LOGGER_NORMAL,LOG_CRIT);
return false; return false;
@ -363,7 +353,6 @@ function q($sql) {
* @param string $sql The SQL query to execute * @param string $sql The SQL query to execute
*/ */
function dbq($sql) { function dbq($sql) {
global $db;
if(\DBA::$dba && \DBA::$dba->connected) if(\DBA::$dba && \DBA::$dba->connected)
$ret = \DBA::$dba->q($sql); $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. // The logger function may make DB calls internally to query the system logging parameters.
// This can cause a recursion if database debugging is enabled. // 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 // So this function preserves the current database debugging state and then turns it off
// doing the logger() call // temporarily while doing the logger() call
function db_logger($s,$level = LOGGER_NORMAL,$syslog = LOG_INFO) { function db_logger($s,$level = LOGGER_NORMAL,$syslog = LOG_INFO) {
global $db;
$saved = \DBA::$dba->debug; $saved = \DBA::$dba->debug;
\DBA::$dba->debug = false; \DBA::$dba->debug = false;
logger($s,$level,$syslog); logger($s,$level,$syslog);

View File

@ -567,21 +567,25 @@ function attribute_contains($attr, $s) {
*/ */
function logger($msg, $level = LOGGER_NORMAL, $priority = LOG_INFO) { 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))) if(App::$module == 'setup' && is_writable('install.log')) {
return; $debugging = true;
$logfile = 'install.log';
$debugging = get_config('system', 'debugging'); $loglevel = LOGGER_ALL;
$loglevel = intval(get_config('system', 'loglevel')); }
$logfile = get_config('system', 'logfile'); else {
$debugging = get_config('system', 'debugging');
$loglevel = intval(get_config('system', 'loglevel'));
$logfile = get_config('system', 'logfile');
}
if((! $debugging) || (! $logfile) || ($level > $loglevel)) if((! $debugging) || (! $logfile) || ($level > $loglevel))
return; return;
$where = ''; $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) { if(version_compare(PHP_VERSION, '5.4.0') >= 0) {
$stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); $stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$where = basename($stack[0]['file']) . ':' . $stack[0]['line'] . ':' . $stack[1]['function'] . ': '; $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; $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); $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']) if(! $pluginfo['logged'])
@file_put_contents($pluginfo['filename'], $pluginfo['message'], FILE_APPEND); @file_put_contents($pluginfo['filename'], $pluginfo['message'], FILE_APPEND);

View File

@ -1,5 +1,7 @@
<?php <?php
namespace Zotlabs\Web;
/** /**
* @file index.php * @file index.php
* *