mirror of
https://github.com/torrentpier/torrentpier
synced 2025-08-14 10:37:30 -07:00
Ошибка подключения классов кеша
Исправление ошибки + замена sqlite_escape_string костыльного.
This commit is contained in:
parent
740eb64a51
commit
92172c833b
4 changed files with 204 additions and 217 deletions
44
common.php
44
common.php
|
@ -36,6 +36,11 @@ define('BB_BT_USERS', 'bb_bt_users');
|
|||
|
||||
define('BT_AUTH_KEY_LENGTH', 10);
|
||||
|
||||
define('PEER_HASH_PREFIX', 'peer_');
|
||||
define('PEERS_LIST_PREFIX', 'peers_list_');
|
||||
define('PEER_HASH_EXPIRE', round($bb_cfg['announce_interval'] * (0.85 * $tr_cfg['expire_factor']))); // sec
|
||||
define('PEERS_LIST_EXPIRE', round($bb_cfg['announce_interval'] * 0.7)); // sec
|
||||
|
||||
define('DL_STATUS_RELEASER', -1);
|
||||
define('DL_STATUS_DOWN', 0);
|
||||
define('DL_STATUS_COMPLETE', 1);
|
||||
|
@ -51,8 +56,8 @@ define('BOT_UID', -746);
|
|||
/**
|
||||
* Database
|
||||
*/
|
||||
// Core DB class
|
||||
require(CORE_DIR . 'dbs.php');
|
||||
|
||||
$DBS = new DBS($bb_cfg);
|
||||
|
||||
function DB ($db_alias = 'db1')
|
||||
|
@ -64,21 +69,13 @@ function DB ($db_alias = 'db1')
|
|||
/**
|
||||
* Cache
|
||||
*/
|
||||
define('PEER_HASH_PREFIX', 'peer_');
|
||||
define('PEERS_LIST_PREFIX', 'peers_list_');
|
||||
define('PEER_HASH_EXPIRE', round($bb_cfg['announce_interval'] * (0.85 * $tr_cfg['expire_factor']))); // sec
|
||||
define('PEERS_LIST_EXPIRE', round($bb_cfg['announce_interval'] * 0.7)); // sec
|
||||
|
||||
if (!function_exists('sqlite_escape_string'))
|
||||
{
|
||||
function sqlite_escape_string($string)
|
||||
{
|
||||
return SQLite3::escapeString($string);
|
||||
}
|
||||
}
|
||||
// Main cache class
|
||||
require(INC_DIR . 'cache/common.php');
|
||||
// Main datastore class
|
||||
require(INC_DIR . 'datastore/common.php');
|
||||
|
||||
// Core CACHE class
|
||||
require(CORE_DIR . 'caches.php');
|
||||
|
||||
$CACHES = new CACHES($bb_cfg);
|
||||
|
||||
function CACHE ($cache_name)
|
||||
|
@ -87,13 +84,9 @@ function CACHE ($cache_name)
|
|||
return $CACHES->get_cache_obj($cache_name);
|
||||
}
|
||||
|
||||
// Main cache class
|
||||
require(INC_DIR . 'cache/common.php');
|
||||
|
||||
// Common cache classes
|
||||
require(INC_DIR . 'cache/memcache.php');
|
||||
require(INC_DIR . 'cache/sqlite.php');
|
||||
require(INC_DIR . 'cache/sqlite_common.php');
|
||||
require(INC_DIR . 'cache/redis.php');
|
||||
require(INC_DIR . 'cache/apc.php');
|
||||
require(INC_DIR . 'cache/xcache.php');
|
||||
|
@ -102,18 +95,15 @@ require(INC_DIR . 'cache/file.php');
|
|||
/**
|
||||
* Datastore
|
||||
*/
|
||||
// Main datastore class
|
||||
require(INC_DIR . 'datastore/common.php');
|
||||
|
||||
// Common datastore classes
|
||||
require(INC_DIR . 'datastore/memcache.php');
|
||||
require(INC_DIR . 'datastore/sqlite.php');
|
||||
require(INC_DIR . 'datastore/redis.php');
|
||||
require(INC_DIR . 'datastore/xcache.php');
|
||||
require(INC_DIR . 'datastore/apc.php');
|
||||
require(INC_DIR . 'datastore/xcache.php');
|
||||
require(INC_DIR . 'datastore/file.php');
|
||||
|
||||
// Initialize Datastore
|
||||
// Initialize datastore
|
||||
switch ($bb_cfg['datastore_type'])
|
||||
{
|
||||
case 'memcache':
|
||||
|
@ -133,14 +123,14 @@ switch ($bb_cfg['datastore_type'])
|
|||
$datastore = new datastore_redis($bb_cfg['cache']['redis'], $bb_cfg['cache']['prefix']);
|
||||
break;
|
||||
|
||||
case 'xcache':
|
||||
$datastore = new datastore_xcache($bb_cfg['cache']['prefix']);
|
||||
break;
|
||||
|
||||
case 'apc':
|
||||
$datastore = new datastore_apc($bb_cfg['cache']['prefix']);
|
||||
break;
|
||||
|
||||
case 'xcache':
|
||||
$datastore = new datastore_xcache($bb_cfg['cache']['prefix']);
|
||||
break;
|
||||
|
||||
case 'filecache':
|
||||
default: $datastore = new datastore_file($bb_cfg['cache']['db_dir'] . 'datastore/', $bb_cfg['cache']['prefix']);
|
||||
}
|
||||
|
|
188
library/includes/cache/sqlite.php
vendored
188
library/includes/cache/sqlite.php
vendored
|
@ -37,10 +37,10 @@ class cache_sqlite extends cache_common
|
|||
$this->db->shard($name);
|
||||
$cached_items = array();
|
||||
$this->prefix_len = strlen($this->prefix);
|
||||
$this->prefix_sql = sqlite_escape_string($this->prefix);
|
||||
$this->prefix_sql = SQLite3::escapeString($this->prefix);
|
||||
|
||||
$name_ary = $name_sql = (array) $name;
|
||||
array_deep($name_sql, 'sqlite_escape_string');
|
||||
array_deep($name_sql, 'SQLite3::escapeString');
|
||||
|
||||
// get available items
|
||||
$rowset = $this->db->fetch_rowset("
|
||||
|
@ -80,9 +80,9 @@ class cache_sqlite extends cache_common
|
|||
function set ($name, $value, $ttl = 604800)
|
||||
{
|
||||
$this->db->shard($this->prefix . $name);
|
||||
$name_sql = sqlite_escape_string($this->prefix . $name);
|
||||
$name_sql = SQLite3::escapeString($this->prefix . $name);
|
||||
$expire = TIMENOW + $ttl;
|
||||
$value_sql = sqlite_escape_string(serialize($value));
|
||||
$value_sql = SQLite3::escapeString(serialize($value));
|
||||
|
||||
$result = $this->db->query("REPLACE INTO ". $this->cfg['table_name'] ." (cache_name, cache_expire_time, cache_value) VALUES ('$name_sql', $expire, '$value_sql')");
|
||||
return (bool) $result;
|
||||
|
@ -93,7 +93,7 @@ class cache_sqlite extends cache_common
|
|||
if ($name)
|
||||
{
|
||||
$this->db->shard($this->prefix . $name);
|
||||
$result = $this->db->query("DELETE FROM ". $this->cfg['table_name'] ." WHERE cache_name = '". sqlite_escape_string($this->prefix . $name) ."'");
|
||||
$result = $this->db->query("DELETE FROM ". $this->cfg['table_name'] ." WHERE cache_name = '". SQLite3::escapeString($this->prefix . $name) ."'");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -107,4 +107,182 @@ class cache_sqlite extends cache_common
|
|||
$result = $this->db->query("DELETE FROM ". $this->cfg['table_name'] ." WHERE cache_expire_time < $expire_time");
|
||||
return ($result) ? $this->db->changes() : 0;
|
||||
}
|
||||
}
|
||||
|
||||
class sqlite_common extends cache_common
|
||||
{
|
||||
var $cfg = array(
|
||||
'db_file_path' => 'sqlite.db',
|
||||
'table_name' => 'table_name',
|
||||
'table_schema' => 'CREATE TABLE table_name (...)',
|
||||
'pconnect' => true,
|
||||
'con_required' => true,
|
||||
'log_name' => 'SQLite',
|
||||
'shard_type' => 'none', # none, string, int (тип перевичного ключа для шардинга)
|
||||
'shard_val' => 0, # для string - кол. начальных символов, для int - делитель (будет использован остаток от деления)
|
||||
);
|
||||
var $engine = 'SQLite';
|
||||
var $dbh = null;
|
||||
var $connected = false;
|
||||
var $shard_val = false;
|
||||
|
||||
var $table_create_attempts = 0;
|
||||
|
||||
function sqlite_common ($cfg)
|
||||
{
|
||||
$this->cfg = array_merge($this->cfg, $cfg);
|
||||
$this->dbg_enabled = sql_dbg_enabled();
|
||||
}
|
||||
|
||||
function connect ()
|
||||
{
|
||||
$this->cur_query = ($this->dbg_enabled) ? 'connect to: '. $this->cfg['db_file_path'] : 'connect';
|
||||
$this->debug('start');
|
||||
|
||||
if (@$this->dbh = new SQLite3($this->cfg['db_file_path']))
|
||||
{
|
||||
$this->connected = true;
|
||||
}
|
||||
|
||||
if (DBG_LOG) dbg_log(' ', $this->cfg['log_name'] .'-connect'. ($this->connected ? '' : '-FAIL'));
|
||||
|
||||
if (!$this->connected && $this->cfg['con_required'])
|
||||
{
|
||||
trigger_error('SQLite not connected', E_USER_ERROR);
|
||||
}
|
||||
|
||||
$this->debug('stop');
|
||||
$this->cur_query = null;
|
||||
}
|
||||
|
||||
function create_table ()
|
||||
{
|
||||
$this->table_create_attempts++;
|
||||
return $this->dbh->query($this->cfg['table_schema']);
|
||||
}
|
||||
|
||||
function shard ($name)
|
||||
{
|
||||
$type = $this->cfg['shard_type'];
|
||||
|
||||
if ($type == 'none') return;
|
||||
if (is_array($name)) trigger_error('cannot shard: $name is array', E_USER_ERROR);
|
||||
|
||||
// define shard_val
|
||||
if ($type == 'string')
|
||||
{
|
||||
$shard_val = substr($name, 0, $this->cfg['shard_val']);
|
||||
}
|
||||
else
|
||||
{
|
||||
$shard_val = $name % $this->cfg['shard_val'];
|
||||
}
|
||||
// все запросы должны быть к одному и тому же шарду
|
||||
if ($this->shard_val !== false)
|
||||
{
|
||||
if ($shard_val != $this->shard_val)
|
||||
{
|
||||
trigger_error("shard cannot be reassigned. [{$this->shard_val}, $shard_val, $name]", E_USER_ERROR);
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
$this->shard_val = $shard_val;
|
||||
$this->cfg['db_file_path'] = str_replace('*', $shard_val, $this->cfg['db_file_path']);
|
||||
}
|
||||
|
||||
function query ($query)
|
||||
{
|
||||
if (!$this->connected) $this->connect();
|
||||
|
||||
$this->cur_query = $query;
|
||||
$this->debug('start');
|
||||
|
||||
if (!$result = @$this->dbh->query($query))
|
||||
{
|
||||
$rowsresult = $this->dbh->query("PRAGMA table_info({$this->cfg['table_name']})");
|
||||
$rowscount = 0;
|
||||
while ($row = $rowsresult->fetchArray(SQLITE3_ASSOC))
|
||||
{
|
||||
$rowscount++;
|
||||
}
|
||||
if (!$this->table_create_attempts && !$rowscount)
|
||||
{
|
||||
if ($this->create_table())
|
||||
{
|
||||
$result = $this->dbh->query($query);
|
||||
}
|
||||
}
|
||||
if (!$result)
|
||||
{
|
||||
$this->trigger_error($this->get_error_msg());
|
||||
}
|
||||
}
|
||||
|
||||
$this->debug('stop');
|
||||
$this->cur_query = null;
|
||||
|
||||
$this->num_queries++;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function fetch_row ($query)
|
||||
{
|
||||
$result = $this->query($query);
|
||||
return is_resource($result) ? $result->fetchArray(SQLITE3_ASSOC) : false;
|
||||
}
|
||||
|
||||
function fetch_rowset ($query)
|
||||
{
|
||||
$result = $this->query($query);
|
||||
$rowset = array();
|
||||
while ($row = $result->fetchArray(SQLITE3_ASSOC))
|
||||
{
|
||||
$rowset[] = $row;
|
||||
}
|
||||
return $rowset;
|
||||
}
|
||||
|
||||
function changes ()
|
||||
{
|
||||
return is_resource($this->dbh) ? $this->dbh->changes() : 0;
|
||||
}
|
||||
|
||||
function escape ($str)
|
||||
{
|
||||
return SQLite3::escapeString($str);
|
||||
}
|
||||
|
||||
function get_error_msg ()
|
||||
{
|
||||
return 'SQLite error #'. ($err_code = $this->dbh->lastErrorCode()) .': '. $this->dbh->lastErrorMsg();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function trigger_error ($msg = 'DB Error')
|
||||
{
|
||||
if (error_reporting()) trigger_error($msg, E_USER_ERROR);
|
||||
}
|
||||
}
|
181
library/includes/cache/sqlite_common.php
vendored
181
library/includes/cache/sqlite_common.php
vendored
|
@ -1,181 +0,0 @@
|
|||
<?php
|
||||
|
||||
if (!defined('BB_ROOT')) die(basename(__FILE__));
|
||||
|
||||
class sqlite_common extends cache_common
|
||||
{
|
||||
var $cfg = array(
|
||||
'db_file_path' => 'sqlite.db',
|
||||
'table_name' => 'table_name',
|
||||
'table_schema' => 'CREATE TABLE table_name (...)',
|
||||
'pconnect' => true,
|
||||
'con_required' => true,
|
||||
'log_name' => 'SQLite',
|
||||
'shard_type' => 'none', # none, string, int (тип перевичного ключа для шардинга)
|
||||
'shard_val' => 0, # для string - кол. начальных символов, для int - делитель (будет использован остаток от деления)
|
||||
);
|
||||
var $engine = 'SQLite';
|
||||
var $dbh = null;
|
||||
var $connected = false;
|
||||
var $shard_val = false;
|
||||
|
||||
var $table_create_attempts = 0;
|
||||
|
||||
function sqlite_common ($cfg)
|
||||
{
|
||||
$this->cfg = array_merge($this->cfg, $cfg);
|
||||
$this->dbg_enabled = sql_dbg_enabled();
|
||||
}
|
||||
|
||||
function connect ()
|
||||
{
|
||||
$this->cur_query = ($this->dbg_enabled) ? 'connect to: '. $this->cfg['db_file_path'] : 'connect';
|
||||
$this->debug('start');
|
||||
|
||||
if (@$this->dbh = new SQLite3($this->cfg['db_file_path']))
|
||||
{
|
||||
$this->connected = true;
|
||||
}
|
||||
|
||||
if (DBG_LOG) dbg_log(' ', $this->cfg['log_name'] .'-connect'. ($this->connected ? '' : '-FAIL'));
|
||||
|
||||
if (!$this->connected && $this->cfg['con_required'])
|
||||
{
|
||||
trigger_error('SQLite not connected', E_USER_ERROR);
|
||||
}
|
||||
|
||||
$this->debug('stop');
|
||||
$this->cur_query = null;
|
||||
}
|
||||
|
||||
function create_table ()
|
||||
{
|
||||
$this->table_create_attempts++;
|
||||
return $this->dbh->query($this->cfg['table_schema']);
|
||||
}
|
||||
|
||||
function shard ($name)
|
||||
{
|
||||
$type = $this->cfg['shard_type'];
|
||||
|
||||
if ($type == 'none') return;
|
||||
if (is_array($name)) trigger_error('cannot shard: $name is array', E_USER_ERROR);
|
||||
|
||||
// define shard_val
|
||||
if ($type == 'string')
|
||||
{
|
||||
$shard_val = substr($name, 0, $this->cfg['shard_val']);
|
||||
}
|
||||
else
|
||||
{
|
||||
$shard_val = $name % $this->cfg['shard_val'];
|
||||
}
|
||||
// все запросы должны быть к одному и тому же шарду
|
||||
if ($this->shard_val !== false)
|
||||
{
|
||||
if ($shard_val != $this->shard_val)
|
||||
{
|
||||
trigger_error("shard cannot be reassigned. [{$this->shard_val}, $shard_val, $name]", E_USER_ERROR);
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
$this->shard_val = $shard_val;
|
||||
$this->cfg['db_file_path'] = str_replace('*', $shard_val, $this->cfg['db_file_path']);
|
||||
}
|
||||
|
||||
function query ($query)
|
||||
{
|
||||
if (!$this->connected) $this->connect();
|
||||
|
||||
$this->cur_query = $query;
|
||||
$this->debug('start');
|
||||
|
||||
if (!$result = @$this->dbh->query($query))
|
||||
{
|
||||
$rowsresult = $this->dbh->query("PRAGMA table_info({$this->cfg['table_name']})");
|
||||
$rowscount = 0;
|
||||
while ($row = $rowsresult->fetchArray(SQLITE3_ASSOC))
|
||||
{
|
||||
$rowscount++;
|
||||
}
|
||||
if (!$this->table_create_attempts && !$rowscount)
|
||||
{
|
||||
if ($this->create_table())
|
||||
{
|
||||
$result = $this->dbh->query($query);
|
||||
}
|
||||
}
|
||||
if (!$result)
|
||||
{
|
||||
$this->trigger_error($this->get_error_msg());
|
||||
}
|
||||
}
|
||||
|
||||
$this->debug('stop');
|
||||
$this->cur_query = null;
|
||||
|
||||
$this->num_queries++;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function fetch_row ($query)
|
||||
{
|
||||
$result = $this->query($query);
|
||||
return is_resource($result) ? $result->fetchArray(SQLITE3_ASSOC) : false;
|
||||
}
|
||||
|
||||
function fetch_rowset ($query)
|
||||
{
|
||||
$result = $this->query($query);
|
||||
$rowset = array();
|
||||
while ($row = $result->fetchArray(SQLITE3_ASSOC))
|
||||
{
|
||||
$rowset[] = $row;
|
||||
}
|
||||
return $rowset;
|
||||
}
|
||||
|
||||
function changes ()
|
||||
{
|
||||
return is_resource($this->dbh) ? $this->dbh->changes() : 0;
|
||||
}
|
||||
|
||||
function escape ($str)
|
||||
{
|
||||
return sqlite_escape_string($str);
|
||||
}
|
||||
|
||||
function get_error_msg ()
|
||||
{
|
||||
return 'SQLite error #'. ($err_code = $this->dbh->lastErrorCode()) .': '. $this->dbh->lastErrorMsg();
|
||||
}
|
||||
|
||||
function rm ($name = '')
|
||||
{
|
||||
if ($name)
|
||||
{
|
||||
$this->db->shard($this->prefix . $name);
|
||||
$result = $this->db->query("DELETE FROM ". $this->cfg['table_name'] ." WHERE cache_name = '". sqlite_escape_string($this->prefix . $name) ."'");
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = $this->db->query("DELETE FROM ". $this->cfg['table_name']);
|
||||
}
|
||||
return (bool) $result;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function trigger_error ($msg = 'DB Error')
|
||||
{
|
||||
if (error_reporting()) trigger_error($msg, E_USER_ERROR);
|
||||
}
|
||||
}
|
|
@ -31,8 +31,8 @@ class datastore_sqlite extends datastore_common
|
|||
{
|
||||
$this->data[$item_name] = $item_data;
|
||||
|
||||
$ds_title = sqlite_escape_string($this->prefix . $item_name);
|
||||
$ds_data = sqlite_escape_string(serialize($item_data));
|
||||
$ds_title = SQLite3::escapeString($this->prefix . $item_name);
|
||||
$ds_data = SQLite3::escapeString(serialize($item_data));
|
||||
|
||||
$result = $this->db->query("REPLACE INTO ". $this->cfg['table_name'] ." (ds_title, ds_data) VALUES ('$ds_title', '$ds_data')");
|
||||
|
||||
|
@ -49,9 +49,9 @@ class datastore_sqlite extends datastore_common
|
|||
if (!$items = $this->queued_items) return;
|
||||
|
||||
$prefix_len = strlen($this->prefix);
|
||||
$prefix_sql = sqlite_escape_string($this->prefix);
|
||||
$prefix_sql = SQLite3::escapeString($this->prefix);
|
||||
|
||||
array_deep($items, 'sqlite_escape_string');
|
||||
array_deep($items, 'SQLite3::escapeString');
|
||||
$items_list = $prefix_sql . join("','$prefix_sql", $items);
|
||||
|
||||
$rowset = $this->db->fetch_rowset("SELECT ds_title, ds_data FROM ". $this->cfg['table_name'] ." WHERE ds_title IN ('$items_list')");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue