Wedge

Public area => The Pub => Support => Topic started by: Jurien on September 18th, 2016, 11:44 AM

Title: Class-DB changes?
Post by: Jurien on September 18th, 2016, 11:44 AM
core/app/Class-DB.php and Load.php
Had to replace an older saved Class-DB.php and Load.php (see attachment) because with this new revs it is not possible to connect to forum database (host provider)
<?php
/**
 * The general database singleton with query parametrization.
 *
 * Wedge (http&#58;//wedge.org)
 * Copyright © 2010 René-Gilles Deberdt, wedge.org
 * Portions are © 2011 Simple Machines.
 * License: http&#58;//wedge.org/license/
 */

if (!defined('WEDGE'))
die('Hacking attempt...');

class 
wesql
{
protected static $instance// container for self
protected static $_db_con// store the database connection (normally)
protected static $callback_values// store the special replacements for we::$user items

// What kind of class are you, anyway? One of a kind!
private function __clone()
{
return false;
}

protected function __construct()
{
global $db_prefix;

self::$callback_values = array(
'db_prefix' => $db_prefix
);
}

// Bootstrap's bootstraps
public static function getInstance()
{
// Quero ergo sum
if (self::$instance == null)
self::$instance = new self();

return self::$instance;
}

public static function is_connected()
{
return (bool) self::$_db_con;
}

public static function connect($db_server$db_name$db_user$db_passwd$db_prefix$db_options = array())
{
global $mysql_set_mode;

// Attempt to connect. (And in non SSI mode, also select the database)
$connection mysqli_connect((!empty($db_options['persist']) ? 'p:' '') . $db_server$db_user$db_passwd, empty($db_options['dont_select_db']) ? $db_name '') or die(mysqli_connect_error());

// Ooops, couldn't connect. See whether that should be a fatal or silent error.
if (!$connection)
{
if (!empty($db_options['non_fatal']))
return null;
else
show_db_error();
}

if (isset($mysql_set_mode) && $mysql_set_mode === true)
wesql::query('SET sql_mode = \'\', AUTOCOMMIT = 1',
array(),
false
);

// Otherwise set, and return true so that we can tell we did manage a connection.
return self::$_db_con $connection;
}

public static function fix_prefix(&$db_prefix$db_name)
{
$db_prefix is_numeric($db_prefix[0]) ? $db_name '.' $db_prefix '`' $db_name '`.' $db_prefix;
self::register_replacement('db_prefix'$db_prefix);
}

public static function quote($db_string$db_values$connection null)
{
global $db_callback;

// Only bother if there's something to replace.
if (strpos($db_string'{') !== false)
{
// This is needed by the callback function.
$db_callback = array($db_values$connection == null self::$_db_con $connection);

// Do the quoting and escaping
$db_string preg_replace_callback('~{([a-z_]+)(?::([a-zA-Z0-9_-]+))?}~''wesql::replace_value'$db_string);

// Clear this global variable.
$db_callback = array();
}

return $db_string;
}

public static function query($db_string$db_values = array(), $connection null)
{
global $db_cache$db_count$db_show_debug$time_start;
global $db_unbuffered$db_callback$settings;

// Comments that are allowed in a query are preg_removed.
static $allowed_comments_from = array(
'~\s+~s',
'~/\*!40001 SQL_NO_CACHE \*/~',
'~/\*!40000 USE INDEX \([A-Za-z\_]+?\) \*/~',
'~/\*!40100 ON DUPLICATE KEY UPDATE id_msg = \d+ \*/~',
);
static $allowed_comments_to = array(
' ',
'',
'',
'',
);

// Decide which connection to use.
$connection $connection === null self::$_db_con $connection;

// One more query....
$db_count = !isset($db_count) ? $db_count 1;

if (empty($settings['disableQueryCheck']) && strpos($db_string'\'') !== false && empty($db_values['security_override']))
wesql::error_backtrace('Hacking attempt...''Illegal character (\') used in query...'true);

// Use "ORDER BY null" to prevent Mysql doing filesorts for Group By clauses without an Order By
if (strpos($db_string'GROUP BY') !== false && strpos($db_string'ORDER BY') === false && strpos($db_string'INSERT') === false && strpos($db_string'REPLACE') === false)
{
// Add before LIMIT
if ($pos strpos($db_string'LIMIT '))
$db_string substr($db_string0$pos) . "\t\t\tORDER BY null\n" substr($db_string$posstrlen($db_string));
else
// Append it.
$db_string .= "\n\t\t\tORDER BY null";
}

if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string'{db_prefix}') !== false))
{
// Pass some values to the global space for use in the callback function.
$db_callback = array($db_values$connection);

// Inject the values passed to this function.
$db_string preg_replace_callback('~{([a-z_]+)(?::([a-zA-Z0-9_-]+))?}~''wesql::replace_value'$db_string);

// This shouldn't be residing in global space any longer.
$db_callback = array();
}

// Debugging.
if (!empty($db_show_debug))
{
// Get the file and line number this function was called.
list ($file$line) = self::error_backtrace('''''return');

// Initialize $db_cache if not already initialized.
if (!isset($db_cache))
$db_cache = array();

if (!empty($_SESSION['debug_redirect']))
{
$db_cache array_merge($_SESSION['debug_redirect'], $db_cache);
$db_count count($db_cache) + 1;
$_SESSION['debug_redirect'] = array();
}

$st microtime(true);
// Don't overload it.
$db_cache[$db_count]['q'] = $db_count 50 $db_string '...';
$db_cache[$db_count]['f'] = $file;
$db_cache[$db_count]['l'] = $line;
$db_cache[$db_count]['s'] = $st $time_start;
}

// First, we clean strings out of the query, reduce whitespace, lowercase, and trim - so we can check it over.
if (empty($settings['disableQueryCheck']))
{
$clean '';
$old_pos 0;
$pos = -1;
while (true)
{
$pos strpos($db_string'\''$pos 1);
if ($pos === false)
break;
$clean .= substr($db_string$old_pos$pos $old_pos);

while (true)
{
$pos1 strpos($db_string'\''$pos 1);
$pos2 strpos($db_string'\\'$pos 1);
if ($pos1 === false)
break;
elseif ($pos2 == false || $pos2 $pos1)
{
$pos $pos1;
break;
}

$pos $pos2 1;
}
$clean .= ' %s ';

$old_pos $pos 1;
}
$clean .= substr($db_string$old_pos);
$clean trim(strtolower(preg_replace($allowed_comments_from$allowed_comments_to$clean)));

// Comments? We don't use comments in our queries, we leave 'em outside!
if (strpos($clean'/*') > || strhas($clean, array('--'';')))
$fail true;
// Trying to change passwords, slow us down, or something?
elseif (strpos($clean'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s'$clean) != 0)
$fail true;
elseif (strpos($clean'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s'$clean) != 0)
$fail true;

if (!empty($fail) && function_exists('log_error'))
self::error_backtrace('Hacking attempt...''Hacking attempt...' "\n" $db_stringE_USER_ERROR);
}

$ret = @mysqli_query($connection$db_string, empty($db_unbuffered) ? MYSQLI_STORE_RESULT MYSQLI_USE_RESULT);

if ($ret === false && empty($db_values['db_error_skip']))
$ret self::serious_error($db_string$connection);

// Debugging.
if (!empty($db_show_debug))
$db_cache[$db_count]['t'] = microtime(true) - $st;

return $ret;
}

public static function affected_rows($connection null)
{
return mysqli_affected_rows($connection === null self::$_db_con $connection);
}

public static function insert_id($connection null)
{
$connection $connection === null self::$_db_con $connection;
return mysqli_insert_id($connection);
}

public static function transaction($operation 'commit'$connection null)
{
// Determine whether to use the known connection or not.
$connection $connection === null self::$_db_con $connection;

switch ($operation)
{
case 'begin':
case 'rollback':
case 'commit':
return @mysqli_query($connectionstrtoupper($operation));
default:
return false;
}
}

public static function error($connection null)
{
return mysqli_error($connection === null self::$_db_con $connection);
}

public static function serious_error($db_string$connection null)
{
global $txt$context$webmaster_email$settings$db_last_error$db_persist;
global $db_server$db_user$db_passwd$db_name$db_show_debug$ssi_db_user$ssi_db_passwd;

if (isset($txt) && !isset($txt['mysql_error_space'], $txt['file']))
loadLanguage(array('index''Errors'), ''falsetrue);

if (!isset($txt['lang_name'])) // Still nothing?
$txt = array();

// Get the file and line numbers.
list ($file$line) = self::error_backtrace('''''return');

// Decide which connection to use.
$connection $connection === null self::$_db_con $connection;

// This is the error message...
$query_error mysqli_error($connection);
$query_errno mysqli_errno($connection);

// Error numbers:
// 1016: Can't open file '....MYI'
// 1030: Got error ??? from table handler.
// 1034: Incorrect key file for table.
// 1035: Old key file for table.
// 1205: Lock wait timeout exceeded.
// 1213: Deadlock found.
// 2006: Server has gone away.
// 2013: Lost connection to server during query.

// Log the error.
if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error'))
log_error((empty($txt) ? 'Database error' $txt['database_error']) . ': ' $query_error . (!empty($settings['enableErrorQueryLogging']) ? "\n\n$db_string''), 'database'$file$line);

// Database error auto fixing ;)
if (function_exists('cache_get_data') && (!isset($settings['autoFixDatabase']) || $settings['autoFixDatabase'] == '1'))
{
// Force caching on, just for the error checking.
$old_cache = @$settings['cache_enable'];
$settings['cache_enable'] = '1';

if (($temp cache_get_data('db_last_error'600)) !== null)
$db_last_error max(@$db_last_error$temp);

if (@$db_last_error time() - 3600 24 3)
{
// We know there's a problem... but what? Try to auto detect.
if ($query_errno == 1030 && strpos($query_error' 127 ') !== false)
{
preg_match_all('~(?:[\n\r]|^)[^\']+?(?:FROM|JOIN|UPDATE|TABLE) ((?:[^\n\r(]+?(?:, )?)*)~s'$db_string$matches);

$fix_tables = array();
foreach ($matches[1] as $tables)
{
$tables array_unique(explode(','$tables));
// Now, it's still theoretically possible this could be an injection. So backtick it!
foreach ($tables as $table)
if (trim($table) != '')
$fix_tables[] = '`' strtr(trim($table), array('`' => '')) . '`';
}

$fix_tables array_unique($fix_tables);
}
// Table crashed. Let's try to fix it.
elseif ($query_errno == 1016)
{
if (preg_match('~\'([^.\']+)~'$query_error$match) != 0)
$fix_tables = array('`' $match[1] . '`');
}
// Indexes crashed. Should be easy to fix!
elseif ($query_errno == 1034 || $query_errno == 1035)
{
preg_match('~\'([^\']+?)\'~'$query_error$match);
$fix_tables = array('`' $match[1] . '`');
}
}

// Check for errors like 145... only fix it once every three days, and send an email. (can't use empty because it might not be set yet...)
if (!empty($fix_tables))
{
// Subs-Post.php for sendmail().
loadSource('Subs-Post');

// Make a note of the REPAIR...
@touch(CACHE_DIR '/error.lock');

// Attempt to find and repair the broken table.
foreach ($fix_tables as $table)
wesql::query("
REPAIR TABLE 
$table"falsefalse);

// And send off an email!
sendmail($webmaster_email, empty($txt) ? 'Database error' $txt['database_error'], empty($txt) ? 'Please try again.' $txt['tried_to_repair']);

$settings['cache_enable'] = $old_cache;

// Try the query again...?
$ret self::query($db_stringfalsefalse);
if ($ret !== false)
return $ret;
}
else
$settings['cache_enable'] = $old_cache;

// Check for the "lost connection" or "deadlock found" errors - and try it just one more time.
if (in_array($query_errno, array(1205121320062013)))
{
if (in_array($query_errno, array(20062013)) && self::$_db_con == $connection)
{
// Are we in SSI mode? If so try that username and password first
if (WEDGE == 'SSI' && !empty($ssi_db_user) && !empty($ssi_db_passwd))
self::$_db_con = @mysqli_connect((!empty($db_persist) ? 'p:' '') . $db_server$ssi_db_user$ssi_db_passwd);

// Fall back to the regular username and password if need be
if (!self::$_db_con)
self::$_db_con = @mysqli_connect((!empty($db_persist) ? 'p:' '') . $db_server$db_user$db_passwd);

if (!self::$_db_con || !@mysqli_select_db(self::$_db_con$db_name))
self::$_db_con false;
}

if (self::$_db_con)
{
// Try a deadlock more than once more.
for ($n 0$n 4$n++)
{
$ret self::query($db_stringfalsefalse);

$new_errno mysqli_errno(self::$_db_con);
if ($ret !== false || in_array($new_errno, array(12051213)))
break;
}

// If it failed again, shucks to be you... we're not trying it over and over.
if ($ret !== false)
return $ret;
}
}
// Are they out of space, perhaps?
elseif ($query_errno == 1030 && strhas($query_error, array(' -1 '' 28 '' 12 ')))
$query_error .= !isset($txt$txt['mysql_error_space']) ? ' - check database storage space.' $txt['mysql_error_space'];
}

// Nothing's defined yet... just die with it.
if (empty($context) || empty($txt))
exit($db_string '<br><br>' $query_error);

// Show an error message, if possible.
$context['error_title'] = $txt['database_error'];
if (allowedTo('admin_forum'))
$context['error_message'] = nl2br($query_errorfalse) . '<br>' $txt['file'] . ': ' $file '<br>' $txt['line'] . ': ' $line;
else
$context['error_message'] = $txt['try_again'];

if (allowedTo('admin_forum') && !empty($db_show_debug))
$context['error_message'] .= '<br><br>' nl2br($db_stringfalse);

// It's already been logged... don't log it again.
fatal_error($context['error_message'], false);
}

public static function insert($method$table$columns$data)
{
global $db_prefix;

$connection self::$_db_con;

// With nothing to insert, simply return.
if (empty($data))
return false;

// Replace the prefix holder with the actual prefix.
$table str_replace('{db_prefix}'$db_prefix$table);

// Inserting data as a single row can be done as a single array.
if (!is_array($data[array_rand($data)]))
$data = array($data);

// Should we assume the data is most likely to exist..? If yes, we'll use an UPDATE call.
// The first entry in the column list then becomes the condition.
if ($method === 'update')
{
foreach ($data as $id => $row)
{
$set '';
$where_val reset($columns);
$where_key key($columns);
foreach ($columns as $key => $val)
if ($key !== $where_key)
$set[] = $key ' = {' $val ':' $key '}';

self::query('
UPDATE ' 
$table '
SET ' 
implode(', '$set) . '
WHERE ' 
$where_key ' = {' $where_val ':' $where_key '}',
array_combine(array_keys($columns), $row)
);

if (self::affected_rows() > 0)
unset($data[$id]);
}
if (empty($data))
return true;

// Anything left to update? Do a regular insert, then.
$method 'ignore';
}

// Create the mold for a single row insert.
$insertData '(';

// Didn't we bother to specify column types?
if (isset($columns[0]))
{
$columns array_flip($columns);
foreach ($columns as $k => &$v)
$v is_int($k) ? 'int' 'string';
}
foreach ($columns as $columnName => $type)
{
// Are we restricting the length?
if (strpos($type'string-') !== false)
$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' substr($type7) . '), '$columnName);
else
$insertData .= sprintf('{%1$s:%2$s}, '$type$columnName);
}
$insertData substr($insertData0, -2) . ')';

// Create an array consisting of only the columns.
$indexed_columns array_keys($columns);

// Here's where the variables are injected to the query.
$insertRows = array();

foreach ($data as $dataRow)
$insertRows[] = self::quote($insertDataarray_combine($indexed_columns$dataRow), $connection);

// Determine the method.
$queryTitle $method === 'replace' 'REPLACE' : ($method === 'ignore' 'INSERT IGNORE' 'INSERT');

// Do the insert, and return a success bool.
return !!self::query('
$queryTitle ' INTO ' $table '(`' implode('`, `'$indexed_columns) . '`)
VALUES
implode(',
'
$insertRows),
array(
'security_override' => true,
'db_error_skip' => $table === $db_prefix 'log_errors',
),
$connection
);
}

public static function register_replacement($match$value)
{
self::$callback_values[$match] = $value;
}

public static function replace_value($matches)
{
global $db_callback;

list ($values$connection) = $db_callback;
if ($connection === null)
$connection self::$_db_con;

if (!is_object($connection))
show_db_error();

if (isset(self::$callback_values[$matches[1]]))
return self::$callback_values[$matches[1]];

if (!isset($matches[2]))
{
if (in_array($matches[1], array('literal''int''string''array_int''array_string''date''float''raw')))
self::error_backtrace('Invalid value inserted into database, {' $matches[1] . ':???}.'''E_USER_ERROR);
else
self::error_backtrace('Invalid database variable, {' $matches[1] . '}.'''E_USER_ERROR);
}

if ($matches[1] == 'literal')
return sprintf('\'%1$s\''mysqli_real_escape_string($connection$matches[2]));

if (!isset($values[$matches[2]]))
self::error_backtrace('The database value you\'re trying to insert does not exist: ' htmlspecialchars($matches[2]), ''E_USER_ERROR);

$replacement $values[$matches[2]];

switch ($matches[1])
{
case 'int':
if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement)
self::error_backtrace('Wrong value type sent to the database. Integer expected. (' $matches[2] . ')'''E_USER_ERROR);
return (string) (int) $replacement;

case 'string':
return sprintf('\'%1$s\''mysqli_real_escape_string($connection$replacement));

case 'array_int':
if (is_array($replacement))
{
if (empty($replacement))
self::error_backtrace('Database error, given array of integer values is empty. (' $matches[2] . ')'''E_USER_ERROR);

foreach ($replacement as $key => $value)
{
if (!is_numeric($value) || (string) $value !== (string) (int) $value)
self::error_backtrace('Wrong value type sent to the database. Array of integers expected. (' $matches[2] . ')'''E_USER_ERROR);

$replacement[$key] = (string) (int) $value;
}

return implode(', '$replacement);
}
else
self::error_backtrace('Wrong value type sent to the database. Array of integers expected. (' $matches[2] . ')'''E_USER_ERROR);

case 'array_string':
if (is_array($replacement))
{
if (empty($replacement))
self::error_backtrace('Database error, given array of string values is empty. (' $matches[2] . ')'''E_USER_ERROR);

foreach ($replacement as $key => $value)
$replacement[$key] = sprintf('\'%1$s\''mysqli_real_escape_string($connection$value));

return implode(', '$replacement);
}
else
self::error_backtrace('Wrong value type sent to the database. Array of strings expected. (' $matches[2] . ')'''E_USER_ERROR);

case 'date':
if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~'$replacement$date_matches) === 1)
return sprintf('\'%04d-%02d-%02d\''$date_matches[1], $date_matches[2], $date_matches[3]);
else
self::error_backtrace('Wrong value type sent to the database. Date expected. (' $matches[2] . ')'''E_USER_ERROR);

case 'float':
if (!is_numeric($replacement))
self::error_backtrace('Wrong value type sent to the database. Floating point number expected. (' $matches[2] . ')'''E_USER_ERROR);
return (string) (float) $replacement;

case 'raw':
return $replacement;

default:
self::error_backtrace('Undefined type used in the database query. (' $matches[1] . ':' $matches[2] . ')');
}
}

public static function error_backtrace($error_message$log_message ''$error_type false$file null$line null)
{
if (empty($log_message))
$log_message $error_message;

$trace_log debug_backtrace(version_compare(PHP_VERSION'5.3.6') < false DEBUG_BACKTRACE_IGNORE_ARGS);
foreach ($trace_log as $step)
{
// Found it?
if ((!isset($step['class']) || $step['class'] !== 'wesql') && strpos($step['function'], 'query') === false && (!in_array(substr($step['function'], 05), array('preg_''mysql'))))
{
$log_message .= '<br>Function: ' $step['function'];
break;
}

if (isset($step['line']))
{
$file $step['file'];
$line $step['line'];
}
}

// A special case - we want the file and line numbers for debugging.
if ($error_type == 'return')
return array($file$line);

// Is always a critical error.
if (function_exists('log_error'))
log_error($log_message'critical'$file$line);

if (function_exists('fatal_error'))
{
fatal_error($error_messagefalse);

// Cannot continue...
exit;
}
elseif ($error_type)
trigger_error($error_message . ($line !== null '<em>(' basename($file) . '-' $line ')</em>' ''), $error_type);
else
trigger_error($error_message . ($line !== null '<em>(' basename($file) . '-' $line ')</em>' ''));
}

public static function escape_wildcard_string($string$translate_human_wildcards false)
{
$replacements = array(
'%' => '\%',
'_' => '\_',
'\\' => '\\\\',
);

if ($translate_human_wildcards)
$replacements += array(
'*' => '%',
);

return strtr($string$replacements);
}

public static function fetch_assoc($result)
{
return mysqli_fetch_assoc($result);
}

public static function fetch_row($result)
{
return mysqli_fetch_row($result);
}

public static function fetch_all($result$type MYSQLI_ASSOC)
{
if ($result === false)
return array();
if (function_exists('mysqli_fetch_all')) // mysqlnd enabled, valid request?
return (array) mysqli_fetch_all($result$type);
$arr = array();
$func_name $type === MYSQLI_ASSOC 'mysqli_fetch_assoc' 'mysqli_fetch_row';
while ($row $func_name($result))
$arr[] = $row;
return $arr;
}

public static function fetch_rows($result)
{
return self::fetch_all($resultMYSQLI_NUM);
}

public static function free_result($result)
{
return mysqli_free_result($result);
}

public static function query_get($db_string$db_values = array(), $connection null$job 'assoc')
{
$request self::query($db_string$db_values$connection);
$results call_user_func('self::fetch_' $job$request);
wesql::free_result($request);

return $results;
}

public static function query_assoc($db_string$db_values = array(), $connection null)
{
return self::query_get($db_string$db_values$connection'assoc');
}

public static function query_row($db_string$db_values = array(), $connection null)
{
return self::query_get($db_string$db_values$connection'row');
}

public static function query_all($db_string$db_values = array(), $connection null)
{
return self::query_get($db_string$db_values$connection'all');
}

public static function query_rows($db_string$db_values = array(), $connection null)
{
return self::query_get($db_string$db_values$connection'rows');
}

public static function data_seek($result$row_num)
{
return mysqli_data_seek($result$row_num);
}

public static function num_fields($result)
{
return mysqli_num_fields($result);
}

public static function num_rows($result)
{
return mysqli_num_rows($result);
}

public static function escape_string($string)
{
return addslashes($string);
}

public static function unescape_string($string)
{
return stripslashes($string);
}

public static function server_info($connection null)
{
return mysqli_get_server_info($connection === null self::$_db_con $connection);
}

public static function select_db($db_name$connection null)
{
$connection $connection === null self::$_db_con $connection;
return mysqli_select_db($connection$db_name);
}
}

Edit:As soon as i upload both revisions (Class-DB.php and Load.php) try this on Main Forum and testing Forum there is no connection to both databases (no errors just not possible to connect to databases)
Check databases on MySQL Management all statuses are oké.

Edit:As soon as i upload both revisions (Class-DB.php and Load.php) try this on Main Forum and testing Forum there is no connection to both databases (no errors just not possible to connect to databases)
Check databases on MySQL Management all statuses are oké.

Edit2:Keeps getting worse. Also had to replace Subs-BoardIndex.php and Subs-Editor.php because it was not possible to reply or start an new topic
Title: Re : Class-DB changes?
Post by: Nao on September 21st, 2016, 10:59 AM
I have no idea why you're reposting a complete file... Apart from taking space in the database, I don't know what its purpose is.

It's important to update all the files at the same time (well, not at the same second lol), since they rely on changed code.
If you have a custom Class-DB file, well... You should use plugins to change code ;) (I personally don't, but that's because I know the files well, and most of the changes I make end up in Wedge itself anyway, but you get the point.)

Class-DB only updates the ::get() method to be more useful, i.e. allow the webmaster to get a single variable from the database without having to go through the query/fetch_assoc/free_result crap.

PS: don't post bug reports in the New Revs topic... It's updated automatically when I push new commits, meaning I don't expect to see someone else posting on it.
Title: Re: Class-DB changes?
Post by: Jurien on September 21st, 2016, 09:56 PM
What should I do with this answer as I point it out revs in Class-DB.php and Load.php once úploaded immediately thereafter database has become unreachable.
Title: Re: Class-DB changes?
Post by: Nao on September 22nd, 2016, 03:10 PM
I don't see what your problem is, really.
It's all working fine on my site...
Title: Re: Class-DB changes?
Post by: CerealGuy on September 22nd, 2016, 06:30 PM
@Freñiçh did you do any custom changes to those files on your own?
This is the commit which most likely makes trouble for you. I didn't roll it out on my installs, no time in the moment.
https://github.com/Wedge/wedge/commit/14c4ea60a02c04787834ad73ccaa86e793b3cc18
Title: Re: Class-DB changes?
Post by: Jurien on September 22nd, 2016, 08:42 PM
Quote from CerealGuy on September 22nd, 2016, 06:30 PM
@Freñiçh did you do any custom changes to those files on your own?
No i do not have made any custom changes.
Quote from CerealGuy on September 22nd, 2016, 06:30 PM
This is the commit which most likely makes trouble for you. I didn't roll it out on my installs, no time in the moment.
https://github.com/Wedge/wedge/commit/14c4ea60a02c04787834ad73ccaa86e793b3cc18
Yeah that comment is known by me
Title: Re: Class-DB changes?
Post by: CerealGuy on September 23rd, 2016, 10:34 AM
Quote from Freñiçh on September 22nd, 2016, 08:42 PM
Quote from CerealGuy on September 22nd, 2016, 06:30 PM
@Freñiçh did you do any custom changes to those files on your own?
No i do not have made any custom changes.
Quote from CerealGuy on September 22nd, 2016, 06:30 PM
This is the commit which most likely makes trouble for you. I didn't roll it out on my installs, no time in the moment.
https://github.com/Wedge/wedge/commit/14c4ea60a02c04787834ad73ccaa86e793b3cc18
Yeah that comment is known by me
What you could do is, doing all the changes on your own and figuring out what makes the trouble.
Most changes are just replacing '$db_string' with '$query'.  This change should not do any trouble, but who nows?
The next thing is renaming 'query_get' to just 'get'.
This can be bad, normally there should be a 'depreacated dummy function' for this, maybe some plugins are using this function and now don't work anymore.

What really makes me wonder is that you don't get any errors. Can't believe that you just can't connect to your *sql server and that's it. The changes are on the Class-DB file, but not in the connection parts.
Title: Re: Class-DB changes?
Post by: Nao on September 23rd, 2016, 11:10 AM
I only changed $db_string to $query because I wanted to make the source code more readable. Really no big deal. And I don't think any mod would change anything in that file anyway, so it was a moot point to me...

As for get(), the function was added VERY late in the development process, like, a month before I left full-time development, and I never got around to replacing more DB calls with it. So I figured it was also pretty much a safe change. I don't know of any plugins that use query_get(), either. At least nowhere in the official plugin repo...
Title: Re: Class-DB changes?
Post by: CerealGuy on September 23rd, 2016, 12:10 PM
I don't think that I used that function ever, seems to be a more internal used function. But we have no idea what @Freñiçh is doing ^^
Title: Re: Class-DB changes?
Post by: Jurien on September 24th, 2016, 09:26 AM
Quote from CerealGuy
What really makes me wonder is that you don't get any errors. Can't believe that you just can't connect to your *sql server and that's it. The changes are on the Class-DB file, but not in the connection parts.
Well it is no errors,been login at forum immediately when the Class-DB file is uploaded, and the website page is refreshed.

But still searching, problem not been found until then I avoid the new revisions made in Class-DB
Title: Re: Class-DB changes?
Post by: Nao on September 24th, 2016, 11:55 PM
Are you sure you uploaded all of the files in the new revision..? If you missed one, it may have triggered an incompatibility.
Title: Re: Class-DB changes?
Post by: Jurien on September 25th, 2016, 08:17 AM
Quote from Nao on September 24th, 2016, 11:55 PM
Are you sure you uploaded all of the files in the new revision..? If you missed one, it may have triggered an incompatibility.
Yes I uploaded all the files at once;
with a waiting period to give the servers an opportunity to process them,no luck at all, this happen as well on hosted server and local server.

 :edit: Thought could this be an PHP script issue PHP 7. versus PHP 5.
Title: Re: Class-DB changes?
Post by: CerealGuy on September 25th, 2016, 09:54 AM
What version(s) of php are you using?
Title: Re: Class-DB changes?
Post by: Jurien on September 25th, 2016, 10:11 AM
PHP 5.6.23-0+deb.8 Local server
PHP 5.5.37 Hosted server

Just a thought i believe :at: Nao is using PHP 7 is it possible that the new revs are written in or for PHP 7,because i never have any issues on this scale when i üploaded new revs
Title: Re: Class-DB changes?
Post by: Jurien on September 25th, 2016, 06:26 PM
Just üpload newer revs,and finally knowing what is going wrong,thanks to
[Commit revision d41f147]
Quote
! Some PHP installs prefer, by default, to give you blank pages or Apache errors instead of showing you an unfinished page when errors are met. I never realized that. Putting things back to normal. At least it'll make debugging easier now... (index.php
Parse error: syntax error, unexpected 'handle_utf8mb4' (T_STRING), expecting variable (T_VARIABLE) in /home/........./domains/......../public_html/......../wedge-master/gz/app/Class-DB.php on line 621
Title: Re: Class-DB changes?
Post by: Nao on September 25th, 2016, 08:02 PM
All these years wasted trying to debug things when I could have added that command in index.php... ^^

Okay, I may have forgotten to add "self::" in front of all handle_utf8mb4 calls! Can you check?
Title: Re: Class-DB changes?
Post by: CerealGuy on September 27th, 2016, 12:51 PM
Just applied latest commit to my test install ::)
Code: [Select]
Parse error: syntax error, unexpected 'handle_utf8mb4' (T_STRING), expecting variable (T_VARIABLE) in /usr/share/nginx/www/wedge/gz/app/Class-DB.php on line 621

EDIT: Going to fix it step by step:
Line 567:
Code: [Select]
return sprintf('\'%1$s\'', handle_utf8mb4(mysqli_real_escape_string($connection, $replacement)));
should be
Code: [Select]
return sprintf('\'%1$s\'', self::handle_utf8mb4(mysqli_real_escape_string($connection, $replacement)));
Line 621:
Code: [Select]
public static handle_utf8mb4($str)
should be
Code: [Select]
public static function handle_utf8mb4($str)

PR: https://github.com/Wedge/wedge/pull/38
Title: Re: Class-DB changes?
Post by: Nao on September 27th, 2016, 01:32 PM
Yeah, I should have split the three different changes to that file into three commits (variable renaming, function renaming and adding a placeholder for it)... sorry about that.

I'll commit it.

Btw what's that css edit in your pull request..?
Title: Re: Class-DB changes?
Post by: CerealGuy on September 27th, 2016, 01:57 PM
Firefox fix for Overflow bug:
http://wedge.org/pub/bugs/8819/overflow-bug/msg297352/#msg297352

Don't ask me why this got committed too, normally I seperate all changes in different branches and let the master branch untouched. Maybe i committed this stuff once to the master branch, i don't know. Can reset my master, create a new branch and do a new pull request if you want, or you just change those two things on your own :D
Title: Re: Class-DB changes?
Post by: CerealGuy on September 27th, 2016, 02:40 PM
Quote
Fixed untested utf8mb4 placeholder code. Note, this still doesn't support 4-byte UTF8... I still don't know exactly what I should do with those. (Class-DB.php)
@Nao, Can you explain what's the problem with 4-byte UTF8?
Title: Re: Class-DB changes?
Post by: Jurien on September 27th, 2016, 09:22 PM
Quote from Nao on September 27th, 2016, 01:32 PM
Yeah, I should have split the three different changes to that file into three commits (variable renaming, function renaming and adding a placeholder for it)... sorry about that.

I'll commit it.
Took me some time to get things right,but it seems to be oké now,suppose you have not incorporated the final addition of :at: Cereal Guy ?,because i had to add this lines manually into the newest revs of Class-DB.PHP.

B.t.w Thanks Cereal Guy for your efforts :cool:
Title: Re: Class-DB changes?
Post by: Nao on September 28th, 2016, 12:39 AM
Quote from Freñiçh on September 27th, 2016, 09:22 PM
Took me some time to get things right,but it seems to be oké now,suppose you have not incorporated the final addition of :at: Cereal Guy ?,because i had to add this lines manually into the newest revs of Class-DB.PHP.
No, I fixed the files but didn't use his code. The fix is similar, though!

https://github.com/Wedge/wedge/commit/9f59d4284378079b4840623185a3c87b763a21c6
Title: Re: Class-DB changes?
Post by: Nao on September 28th, 2016, 12:43 AM
Quote from CerealGuy on September 27th, 2016, 02:40 PM
@Nao, Can you explain what's the problem with 4-byte UTF8?
Well, MySQL added support for 4-byte UTF8, which is used mostly by new smileys. If you try to insert such a character into a regular UTF8 database, it'll be inserted as a ? (question mark). Which isn't great. Of course, it's only for those smileys.
There are two ways to fix that:
- Either catch all utf8mb4 characters at MySQL insert time, and turn them into HTML entities (which is what Elk does, so that's emanuelle's recommended solution),
- Or ensure that the database uses utf8mb4 from the start, if supported. That way, the character will only take 4 bytes in the database, rather than the length of an entity... Which, okay, is barely twice that number.

I'm not a big fan of parsing all messages for character recognition. I don't know if the array_split followed with an ord() call on all array items would be really fast. (Probably faster than a regexp, but still...)
Title: Re: Class-DB changes?
Post by: Nao on September 28th, 2016, 12:46 AM
Quote from CerealGuy on September 27th, 2016, 01:57 PM
Firefox fix for Overflow bug:
http://wedge.org/pub/bugs/8819/overflow-bug/msg297352/#msg297352

Don't ask me why this got committed too, normally I seperate all changes in different branches and let the master branch untouched. Maybe i committed this stuff once to the master branch, i don't know. Can reset my master, create a new branch and do a new pull request if you want, or you just change those two things on your own :D
Yeah, it's not the first time you're having problems with commits, but honestly I probably wouldn't fare better with pull requests myself.
I'd simply recommend posting a diff file, it's probably gonna be cleaner & faster for everybody. (Except I can't do a full merge from the github website, of course.)

Regarding Firefox, I'm on the fence. You're using hardcoded values (min-width: 49em, another one like that, and a final 'padding: 0 15px' that wasn't in the original css), it's usually bad practice for future browser versions & screen resolutions.
I'd need to test and see if I can find something better, but I'm having so many problems with Firefox in general, I'm not in a hurry.
Title: Re: Class-DB changes?
Post by: CerealGuy on September 28th, 2016, 09:22 AM
Quote from Nao on September 28th, 2016, 12:43 AM
Well, MySQL added support for 4-byte UTF8, which is used mostly by new smileys. If you try to insert such a character into a regular UTF8 database, it'll be inserted as a ? (question mark). Which isn't great. Of course, it's only for those smileys.
There are two ways to fix that:
- Either catch all utf8mb4 characters at MySQL insert time, and turn them into HTML entities (which is what Elk does, so that's emanuelle's recommended solution),
- Or ensure that the database uses utf8mb4 from the start, if supported. That way, the character will only take 4 bytes in the database, rather than the length of an entity... Which, okay, is barely twice that number.

I'm not a big fan of parsing all messages for character recognition. I don't know if the array_split followed with an ord() call on all array items would be really fast. (Probably faster than a regexp, but still...)
Thanks for clarification, got it. How about deciding once if the sql server supports (and is using uft8mb8 as charset on the important tables) and if so use the faster first approach, if not second?
On the other hand, utf8mb4 was introduced in 2010, so legacy support is not really needed.
Anyone who still uses a pre ut8mb4 mysql version should have other problems as character encoding.
Quote from Nao on September 28th, 2016, 12:46 AM
Regarding Firefox, I'm on the fence. You're using hardcoded values (min-width: 49em, another one like that, and a final 'padding: 0 15px' that wasn't in the original css), it's usually bad practice for future browser versions & screen resolutions.
I'd need to test and see if I can find something better, but I'm having so many problems with Firefox in general, I'm not in a hurry.
I'm totally fine with this. It's not a fix, it's more of a hack. Don't use firefox (except for tor browser) on my own, but people on our forum do and they reported this issue. So I tried, and fixed it, even if it's not the proper way. So totally fine with not merging it. People who have this problem can wait for a good fix or use this hack. I have nearly no idea of CSS, it's always playing around with browsers dev tools :whistle:.
Title: Re: Class-DB changes?
Post by: Jurien on September 29th, 2016, 09:22 AM
Warning (level 2): mysqli_real_escape_string() expects exactly 2 parameters, 1 given Class-DB.php Line 775

Title: Re: Class-DB changes?
Post by: CerealGuy on September 29th, 2016, 02:20 PM
Didn't try it yet, but something like this should work.
Code: [Select]
public static function escape_string_replacement($str, $connection = null)
{
                $connection = $connection === null ? self::$_db_con : $connection;
return mysqli_real_escape_string($connection, $str);
}
Title: Re: Class-DB changes?
Post by: Jurien on September 30th, 2016, 08:58 AM
Quote from CerealGuy on September 29th, 2016, 02:20 PM
Didn't try it yet, but something like this should work.
Adding that line's gives me another warning on Line 776.(Happens when Run Tasks Forum Maintenance<=>Routine<=>Optimize all tables.

Looks like a snowball effect remains a struggle with Class-DB.php,
Title: Re: Class-DB changes?
Post by: Nao on October 1st, 2016, 10:38 AM
Sorry. Only need to add the connection variable before the str variable in the new function definition.

And yes I'm not using that code on my current websites sorry.
Title: Re: Class-DB changes?
Post by: Jurien on October 1st, 2016, 12:06 PM
Well i'm done with it,deleted the last revs and get back to a working forum before the new revs.....even media gallery give's me all kind of errors and troubles.

I'm sorry but I can not afford to have a malfunctioning forum.
Title: Re: Class-DB changes?
Post by: CerealGuy on October 1st, 2016, 02:15 PM
Quote from Freñiçh on October 1st, 2016, 12:06 PM
Well i'm done with it,deleted the last revs and get back to a working forum before the new revs.....even media gallery give's me all kind of errors and troubles.

I'm sorry but I can not afford to have a malfunctioning forum.
Argument order was also wrong.

https://github.com/Wedge/wedge/pull/39/commits/58da2c6abf56a9db1d0147c69506449e0637978a

But still, it's some changes which aren't really tested yet. So i'm sure there are some more things to fix, but all changes @Nao made are definetly worth it. It just needs some time and commits :lol:.

I'm now getting `The database value you're trying to insert does not exist: current_topic` :eheh:
Title: Re: Class-DB changes?
Post by: Nao on October 1st, 2016, 07:40 PM
Merged your pr, thanks.
Can you debug that new thing? ;)

I promise I'll get to it next week. This has just bern the shittiest week ever... :(
Title: Re: Class-DB changes?
Post by: CerealGuy on October 2nd, 2016, 11:36 AM
I'm on it but it needs some time. The sql stuff is not throwing an error but echoing it. So no line which is doing bad stuff. But I guess something with the assoc stuff is doing that.
Title: Re: Class-DB changes?
Post by: Nao on October 2nd, 2016, 03:53 PM
Did you enable everything in the admin area, debugging section?
Posted: October 2nd, 2016, 01:01 PM

Testing...
Nope, no errors posting for me.
Anywhere else..? I need to know where to reproduce. ;)
Posted: October 2nd, 2016, 03:21 PM

I've double-checked the error_backtrace function, and it doesn't seem to be incompatible with ::get_* functions, and I've even forced a fake error (setting an int to empty string before I pass it), and the error shows up in the error log, complete with file & line number... :-/
Title: Re: Class-DB changes?
Post by: CerealGuy on October 2nd, 2016, 07:56 PM
Quote from Nao on October 2nd, 2016, 03:53 PM
Did you enable everything in the admin area, debugging section?
Have to check that, could be the problem. Too many wedge installs :D
Title: Re: Class-DB changes?
Post by: Nao on October 2nd, 2016, 11:12 PM
I'd really really like to know on WHAT page/action you're getting the error...
Title: Re: Class-DB changes?
Post by: CerealGuy on October 3rd, 2016, 10:15 AM
Got it on every ?topic request.

EDIT: Can't reproduce it with the last commit.
Title: Re: Class-DB changes?
Post by: Nao on October 3rd, 2016, 08:25 PM
Possibly a caching issue then.

Btw Wedge.org is synced to the official repo, for the first time in years... I decided that if I never committed its changes, then maybe they weren't worth it. (I'm keeping them on the side though, just in case...)
Title: Re: Class-DB changes?
Post by: CerealGuy on October 5th, 2016, 01:36 PM
Yeah definetly possible. Didn't look into the wedge update mechanism, but does it force clean the cache? :hmm:
Title: Re: Class-DB changes?
Post by: Nao on October 7th, 2016, 08:58 PM
Yes, unless your file dates weren't modified.
Title: Re: Class-DB changes?
Post by: Jurien on October 8th, 2016, 10:57 AM
Quote from Nao on October 1st, 2016, 07:40 PM
I promise I'll get to it next week. This has just bern the shittiest week ever... :(
You have recovered yourself,thank god for that :)....well done.I am back on the track just üploaded again all files of the latest Wedge source code repository,and guess what all went smooth.

Except for this guess it had to do something with my Dutch language (translation).

Check Detailed version.
Errors Manage Maintenance.php on line 894,896 and 897 & Admin.template.php
Title: Re: Class-DB changes?
Post by: Nao on October 8th, 2016, 01:49 PM
It doesn't say what error it is..?
And I don't think there should be an error due to missing language strings. Wedge always defaults to English strings in that case.
Title: Re: Class-DB changes?
Post by: Jurien on October 8th, 2016, 02:06 PM
Error log tracks register warnings and notice
Title: Re: Class-DB changes?
Post by: Nao on October 8th, 2016, 06:45 PM
Probably something that can be fixed by fiddling with language settings in the admin area and then resetting them. ^^

Wow, that quick reply focus hack I added works wonders on mobile :)