Some cleanup (#1003)

This commit is contained in:
Roman Kelesidis 2023-10-28 17:15:41 +07:00 committed by GitHub
commit 459e5e161f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 17 additions and 72 deletions

View file

@ -70,7 +70,7 @@ if (!isset($info_hash)) {
$info_hash_hex = bin2hex($info_hash);
// Store peer id
$peer_id_sql = rtrim(DB()->escape(htmlspecialchars($peer_id, ENT_QUOTES, 'UTF-8', false)), ' ');
$peer_id_sql = rtrim(DB()->escape(htmlCHR($peer_id)), ' ');
// Check info_hash version
if (strlen($info_hash) === 32) {

View file

@ -20,9 +20,7 @@ if (!$mode = (string)$this->request['mode']) {
switch ($mode) {
case 'clear_cache':
foreach ($bb_cfg['cache']['engines'] as $cache_name => $cache_val) {
if (!in_array('db_sqlite', $cache_val)) {
CACHE($cache_name)->rm();
}
CACHE($cache_name)->rm();
}
$this->response['cache_html'] = '<span class="seed bold">' . $lang['ALL_CACHE_CLEARED'] . '</span>';

View file

@ -787,6 +787,20 @@ function wbr($text, $max_word_length = HTML_WBR_LENGTH)
return preg_replace("/([\w\->;:.,~!?(){}@#$%^*\/\\\\]{" . $max_word_length . "})/ui", '$1<wbr>', $text);
}
/**
* Convert special characters to HTML entities
*
* @param $txt
* @param bool $double_encode
* @param int $quote_style
* @param ?string $charset
* @return string
*/
function htmlCHR($txt, bool $double_encode = false, int $quote_style = ENT_QUOTES, ?string $charset = 'UTF-8'): string
{
return (string)htmlspecialchars($txt ?? '', $quote_style, $charset, $double_encode);
}
function generate_user_info($row, bool $have_auth = IS_ADMIN): array
{
global $userdata, $lang, $images, $bb_cfg;

View file

@ -321,20 +321,6 @@ function send_no_cache_headers()
header('Pragma: no-cache');
}
/**
* Convert special characters to HTML entities
*
* @param $txt
* @param bool $double_encode
* @param int $quote_style
* @param ?string $charset
* @return string
*/
function htmlCHR($txt, bool $double_encode = false, int $quote_style = ENT_QUOTES, ?string $charset = 'UTF-8'): string
{
return (string)htmlspecialchars($txt ?? '', $quote_style, $charset, $double_encode);
}
/**
* Adds commas between every group of thousands
*

View file

@ -20,6 +20,7 @@ class Sqlite extends Common
public $used = true;
public $db;
public $prefix;
public $engine = 'SQLite';
public $cfg = [
'db_file_path' => '/path/to/cache.db.sqlite',
'table_name' => 'cache',

View file

@ -157,23 +157,6 @@ class SqliteCommon extends Common
return 'SQLite error #' . $this->dbh->lastErrorCode() . ': ' . $this->dbh->lastErrorMsg();
}
public function rm($name = '')
{
if ($name) {
$this->db->shard($this->prefix . $name);
$result = $this->db->query("DELETE FROM " . $this->cfg['table_name'] . " WHERE cache_name = '" . SQLite3::escapeString($this->prefix . $name) . "'");
} else {
$result = $this->db->query("DELETE FROM " . $this->cfg['table_name']);
}
return (bool)$result;
}
public function gc($expire_time = TIMENOW)
{
$result = $this->db->query("DELETE FROM " . $this->cfg['table_name'] . " WHERE cache_expire_time < $expire_time");
return $result ? sqlite_changes($this->db->dbh) : 0;
}
public function trigger_error($msg = 'DB Error')
{
if (error_reporting()) {

View file

@ -41,7 +41,6 @@ class Caches
}
$this->ref[$cache_name] =& $this->obj[$cache_name];
break;
case 'sqlite':
if (!isset($this->obj[$cache_name])) {
$cache_cfg['pconnect'] = $this->cfg['pconnect'];
@ -51,26 +50,12 @@ class Caches
}
$this->ref[$cache_name] =& $this->obj[$cache_name];
break;
case 'db_sqlite':
if (!isset($this->obj[$cache_name])) {
$cache_cfg['pconnect'] = $this->cfg['pconnect'];
$cache_cfg['db_file_path'] = $this->get_db_path($cache_name, $cache_cfg, '.sqlite.db');
$cache_cfg['table_name'] = $cache_name;
$cache_cfg['table_schema'] = $this->get_table_schema($cache_cfg);
$this->obj[$cache_name] = new Cache\SqliteCommon($cache_cfg);
}
$this->ref[$cache_name] =& $this->obj[$cache_name];
break;
case 'redis':
if (!isset($this->obj[$cache_name])) {
$this->obj[$cache_name] = new Cache\Redis($this->cfg['redis'], $this->cfg['prefix']);
}
$this->ref[$cache_name] =& $this->obj[$cache_name];
break;
case 'filecache':
default:
if (!isset($this->obj[$cache_name])) {
@ -93,9 +78,4 @@ class Caches
return $this->cfg['db_dir'] . $name . $ext;
}
public function get_table_schema($cfg)
{
return "CREATE TABLE {$cfg['table_name']} ( {$cfg['columns']} )";
}
}

View file

@ -157,23 +157,6 @@ class SqliteCommon extends Common
return 'SQLite error #' . $this->dbh->lastErrorCode() . ': ' . $this->dbh->lastErrorMsg();
}
public function rm($name = '')
{
if ($name) {
$this->db->shard($this->prefix . $name);
$result = $this->db->query("DELETE FROM " . $this->cfg['table_name'] . " WHERE cache_name = '" . SQLite3::escapeString($this->prefix . $name) . "'");
} else {
$result = $this->db->query("DELETE FROM " . $this->cfg['table_name']);
}
return (bool)$result;
}
public function gc($expire_time = TIMENOW)
{
$result = $this->db->query("DELETE FROM " . $this->cfg['table_name'] . " WHERE cache_expire_time < $expire_time");
return $result ? sqlite_changes($this->db->dbh) : 0;
}
public function trigger_error($msg = 'DB Error')
{
if (error_reporting()) {