From c3b0c0f32a17649503db67f208cce6f9e0cdc322 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Mon, 25 Apr 2016 16:55:33 -0700 Subject: remove global db variable --- include/cli_startup.php | 6 +-- include/dba/dba_driver.php | 121 +++++++++++++++++++++++++-------------------- include/network.php | 2 +- include/text.php | 16 +++--- 4 files changed, 79 insertions(+), 66 deletions(-) (limited to 'include') diff --git a/include/cli_startup.php b/include/cli_startup.php index a99164d4c..a226f1345 100644 --- a/include/cli_startup.php +++ b/include/cli_startup.php @@ -1,6 +1,7 @@ convert(); @@ -25,8 +26,7 @@ function cli_startup() { App::$timezone = ((x($default_timezone)) ? $default_timezone : 'UTC'); date_default_timezone_set(App::$timezone); - require_once('include/dba/dba_driver.php'); - $db = dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type); + $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); }; diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 3c5b0b67e..4bad70323 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -7,44 +7,57 @@ * functions for working with databases. */ -/** - * @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); + +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); + } } - } - define('NULL_DATE', $dba->get_null_date()); - define('ACTIVE_DBTYPE', $dbtype); - return $dba; + define('NULL_DATE', self::$dba->get_null_date()); + define('ACTIVE_DBTYPE', self::$dbtype); + return self::$dba; + } } /** @@ -53,6 +66,7 @@ function dba_factory($server, $port,$user,$pass,$db,$dbtype,$install = false) { * 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'; @@ -203,10 +217,10 @@ function printable($s) { * @param int $state 0 to disable debugging */ function dbg($state) { - global $db; +// global $db; - if($db) - $db->dbg($state); + if(DBA::$dba) + DBA::$dba->dbg($state); } /** @@ -220,21 +234,20 @@ function dbg($state) { * @return Return an escaped string of the value to pass to a DB query. */ function dbesc($str) { - global $db; - - if($db && $db->connected) - return($db->escape($str)); + if(DBA::$dba && DBA::$dba->connected) + return(DBA::$dba->escape($str)); else return(str_replace("'", "\\'", $str)); } + function dbescbin($str) { global $db; - return $db->escapebin($str); + return DBA::$dba->escapebin($str); } function dbunescbin($str) { global $db; - return $db->unescapebin($str); + return DBA::$dba->unescapebin($str); } function dbescdate($date) { @@ -248,27 +261,27 @@ function dbescdate($date) { function db_quoteinterval($txt) { global $db; - return $db->quote_interval($txt); + return DBA::$dba->quote_interval($txt); } function dbesc_identifier($str) { global $db; - return $db->escape_identifier($str); + return DBA::$dba->escape_identifier($str); } function db_utcnow() { global $db; - return $db->utcnow(); + return DBA::$dba->utcnow(); } function db_optimizetable($table) { global $db; - $db->optimize_table($table); + DBA::$dba->optimize_table($table); } function db_concat($fld, $sep) { global $db; - return $db->concat($fld, $sep); + return DBA::$dba->concat($fld, $sep); } // Function: q($sql,$args); @@ -298,7 +311,7 @@ function q($sql) { $args = func_get_args(); unset($args[0]); - if($db && $db->connected) { + if(DBA::$dba && DBA::$dba->connected) { $stmt = vsprintf($sql, $args); if($stmt === false) { if(version_compare(PHP_VERSION, '5.4.0') >= 0) @@ -307,7 +320,7 @@ function q($sql) { else logger('dba: vsprintf error: ' . print_r(debug_backtrace(), true),LOGGER_NORMAL,LOG_CRIT); } - return $db->q($stmt); + return DBA::$dba->q($stmt); } /* @@ -329,8 +342,8 @@ function q($sql) { function dbq($sql) { global $db; - if($db && $db->connected) - $ret = $db->q($sql); + if(DBA::$dba && DBA::$dba->connected) + $ret = DBA::$dba->q($sql); else $ret = false; diff --git a/include/network.php b/include/network.php index ec255581d..f822b644d 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' => $db->getdriver(), + 'dbdriver' => \DBA::$dba->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 0a7f84b01..f27abf80b 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) { - // turn off logger in install mode - global $a; - global $db; - if((App::$module == 'install') || (! ($db && $db->connected))) + require_once('include/dba/dba_driver.php'); + + // turn off logger in install mode + if((App::$module == 'install') || (! (DBA::$dba && DBA::$dba->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) { - // turn off logger in install mode - global $a; - global $db; - if((App::$module == 'install') || (! ($db && $db->connected))) + require_once('include/dba/dba_driver.php'); + + // turn off logger in install mode + if((App::$module == 'install') || (! (DBA::$dba && DBA::$dba->connected))) return; $debugging = get_config('system','debugging'); -- cgit v1.2.3 From d62f4908146f2bdfb396bff06f74afb62995e4a3 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Mon, 25 Apr 2016 20:12:36 -0700 Subject: Setup was horked after this commit and I couldn't easily make it right so reverting - will try again at a future date Revert "remove global db variable" This reverts commit c3b0c0f32a17649503db67f208cce6f9e0cdc322. --- include/cli_startup.php | 6 +-- include/dba/dba_driver.php | 121 ++++++++++++++++++++------------------------- include/network.php | 2 +- include/text.php | 16 +++--- 4 files changed, 66 insertions(+), 79 deletions(-) (limited to 'include') 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 @@ 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'); -- cgit v1.2.3 From 2e7028c976023ebf5c03bfaaad9f91be8a01e932 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Mon, 25 Apr 2016 22:03:02 -0700 Subject: some cleanup on the mysqli driver --- include/dba/dba_mysqli.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'include') diff --git a/include/dba/dba_mysqli.php b/include/dba/dba_mysqli.php index 6986d4586..57a7559a1 100755 --- a/include/dba/dba_mysqli.php +++ b/include/dba/dba_mysqli.php @@ -4,20 +4,26 @@ require_once('include/dba/dba_driver.php'); class dba_mysqli extends dba_driver { - function connect($server, $port, $user,$pass,$db) { + function connect($server,$port,$user,$pass,$db) { if($port) $this->db = new mysqli($server,$user,$pass,$db, $port); else $this->db = new mysqli($server,$user,$pass,$db); - if(! mysqli_connect_errno()) { - $this->connected = true; + if($this->db->connect_error) { + $this->connected = false; + $this->error = $this->db->connect_error; + + if(file_exists('dbfail.out')) { + file_put_contents('dbfail.out', datetime_convert() . "\nConnect: " . $this->error . "\n", FILE_APPEND); + } + + return false; } - if($this->connected) { + else { + $this->connected = true; return true; } - $this->error = $this->db->connect_error; - return false; } function q($sql) { -- cgit v1.2.3 From 3446e68ac2aa3f970307b748fee5ee2da1af4ca4 Mon Sep 17 00:00:00 2001 From: Mario Vavti Date: Tue, 26 Apr 2016 11:41:08 +0200 Subject: move spoiler= and quote= bbcode handling from prepare_body() to bbcode() and add open tag to bbco_autocomplete --- include/bbcode.php | 44 ++++++++++++++++++++++++++++---------------- include/text.php | 29 ----------------------------- 2 files changed, 28 insertions(+), 45 deletions(-) (limited to 'include') diff --git a/include/bbcode.php b/include/bbcode.php index 78a2759c1..b8cd23f59 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -330,8 +330,19 @@ function bb_map_location($match) { } function bb_opentag($match) { + $openclose = (($match[2]) ? '' . $match[1] . '' : t('Click to open/close')); + $text = (($match[2]) ? $match[2] : $match[1]); $rnd = mt_rand(); - return "
" . $match[1] . "
" . $match[2] . "
"; + + return ''; +} + +function bb_spoilertag($match) { + $openclose = (($match[2]) ? '' . $match[1] . ' ' . t('spoiler') . '' : t('Click to open/close')); + $text = (($match[2]) ? $match[2] : $match[1]); + $rnd = mt_rand(); + + return ''; } /** @@ -748,33 +759,34 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $cache = false) $Text = preg_replace("/\[code\](.*?)\[\/code\]/ism", "$CodeLayout", $Text); } - // Declare the format for [spoiler] layout - $SpoilerLayout = '
$1
'; - // Check for [spoiler] text - // handle nested quotes $endlessloop = 0; - while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20)) - $Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism", "$SpoilerLayout", $Text); + while ((strpos($Text, "[/spoiler]")!== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20)) { + $Text = preg_replace_callback("/\[spoiler\](.*?)\[\/spoiler\]/ism", 'bb_spoilertag', $Text); + } // Check for [spoiler=Author] text - - $t_wrote = t('$1 spoiler'); - - // handle nested quotes $endlessloop = 0; - while ((strpos($Text, "[/spoiler]")!== false) and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20)) - $Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism", - "
" . $t_wrote . "
$2
", - $Text); + while ((strpos($Text, "[/spoiler]")!== false) and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20)) { + $Text = preg_replace_callback("/\[spoiler=(.*?)\](.*?)\[\/spoiler\]/ism", 'bb_spoilertag', $Text); + } + // Check for [open] text + $endlessloop = 0; + while ((strpos($Text, "[/open]")!== false) and (strpos($Text, "[open]") !== false) and (++$endlessloop < 20)) { + $Text = preg_replace_callback("/\[open\](.*?)\[\/open\]/ism", 'bb_opentag', $Text); + } + // Check for [open=Title] text $endlessloop = 0; while ((strpos($Text, "[/open]")!== false) and (strpos($Text, "[open=") !== false) and (++$endlessloop < 20)) { $Text = preg_replace_callback("/\[open=(.*?)\](.*?)\[\/open\]/ism", 'bb_opentag', $Text); } + + + // Declare the format for [quote] layout $QuoteLayout = '
$1
'; @@ -792,7 +804,7 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $cache = false) $endlessloop = 0; while ((strpos($Text, "[/quote]")!== false) and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20)) $Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism", - "
" . $t_wrote . "
$2
", + "" . $t_wrote . "
$2
", $Text); // Images diff --git a/include/text.php b/include/text.php index 0a7f84b01..926e2eed6 100644 --- a/include/text.php +++ b/include/text.php @@ -1532,35 +1532,6 @@ function prepare_body(&$item,$attach = false) { $s = sslify($s); - // Look for spoiler - $spoilersearch = '
'; - - // Remove line breaks before the spoiler - while ((strpos($s, "\n".$spoilersearch) !== false)) - $s = str_replace("\n".$spoilersearch, $spoilersearch, $s); - while ((strpos($s, "
".$spoilersearch) !== false)) - $s = str_replace("
".$spoilersearch, $spoilersearch, $s); - - while ((strpos($s, $spoilersearch) !== false)) { - - $pos = strpos($s, $spoilersearch); - $rnd = random_string(8); - $spoilerreplace = '
'.sprintf(t('Click to open/close')).''. - '