|
- <?php
- if (!defined('DEDEINC')) exit('dedebiz');
-
- @set_time_limit(0);
-
-
- $dsql = $dsqlitete = $db = new DedeSqlite(FALSE);
-
- if (!defined('MYSQL_BOTH')) {
- define('MYSQL_BOTH', MYSQLI_BOTH);
- }
- if (!defined('MYSQL_ASSOC')) {
- define('MYSQL_ASSOC', SQLITE3_ASSOC);
- }
- class DedeSqlite
- {
- var $linkID;
- var $dbHost;
- var $dbUser;
- var $dbPwd;
- var $dbName;
- var $dbPrefix;
- var $result;
- var $queryString;
- var $parameters;
- var $isClose;
- var $safeCheck;
- var $showError = false;
- var $recordLog = false;
- var $isInit = false;
- var $pconnect = false;
- var $_fixObject;
-
-
- function __construct($pconnect = FALSE, $nconnect = FALSE)
- {
- $this->isClose = FALSE;
- $this->safeCheck = TRUE;
- $this->pconnect = $pconnect;
- if ($nconnect) {
- $this->Init($pconnect);
- }
- }
-
- function DedeSql($pconnect = FALSE, $nconnect = TRUE)
- {
- $this->__construct($pconnect, $nconnect);
- }
-
- function Init($pconnect = FALSE)
- {
- $this->linkID = 0;
-
-
- $this->dbHost = $GLOBALS['cfg_dbhost'];
- $this->dbUser = $GLOBALS['cfg_dbuser'];
- $this->dbPwd = $GLOBALS['cfg_dbpwd'];
- $this->dbName = $GLOBALS['cfg_dbname'];
- $this->dbPrefix = $GLOBALS['cfg_dbprefix'];
- $this->result["me"] = 0;
- $this->Open($pconnect);
- }
-
-
- function SetSource($host, $username, $pwd, $dbname, $dbprefix = "dede_")
- {
- $this->dbHost = $host;
- $this->dbUser = $username;
- $this->dbPwd = $pwd;
- $this->dbName = $dbname;
- $this->dbPrefix = $dbprefix;
- $this->result["me"] = 0;
- }
-
-
- function SetParameter($key, $value)
- {
- $this->parameters[$key] = $value;
- }
-
-
- function Open($pconnect = FALSE)
- {
- global $dsqlite;
-
- if ($dsqlite && !$dsqlite->isClose && $dsqlite->isInit) {
- $this->linkID = $dsqlite->linkID;
- } else {
-
- $this->linkID = new SQLite3(DEDEDATA.'/'.$this->dbName.'.db');
-
-
- CopySQLiPoint($this);
- }
-
-
- if (!$this->linkID) {
- $this->DisplayError("DedeBIZ错误警告:<span style='color:#dc3545'>连接数据库失败,可能数据库密码不对或数据库服务器出错</span>");
- exit();
- }
- $this->isInit = TRUE;
- return TRUE;
- }
-
-
- function SetLongLink()
- {
- @mysqli_query("SET interactive_timeout=3600, wait_timeout=3600 ;", $this->linkID);
- }
-
-
- function GetError()
- {
- $str = mysqli_error($this->linkID);
- return $str;
- }
-
-
-
-
- function Close($isok = FALSE)
- {
- $this->FreeResultAll();
- if ($isok) {
- $this->linkID->close();
- $this->isClose = TRUE;
- $GLOBALS['dsql'] = NULL;
- }
- }
-
-
- function ClearErrLink()
- {
- }
-
-
- function CloseLink($dblink)
- {
- }
-
- function Esc($_str)
- {
- return addslashes($_str);
- }
-
-
- function ExecuteNoneQuery($sql = '')
- {
- global $dsqlite;
- if (!$dsqlite->isInit) {
- $this->Init($this->pconnect);
- }
- if ($dsqlite->isClose) {
- $this->Open(FALSE);
- $dsqlite->isClose = FALSE;
- }
- if (!empty($sql)) {
- $this->SetQuery($sql);
- } else {
- return FALSE;
- }
- if (is_array($this->parameters)) {
- foreach ($this->parameters as $key => $value) {
- $this->queryString = str_replace("@".$key, "'$value'", $this->queryString);
- }
- }
-
- if ($this->safeCheck) CheckSql($this->queryString, 'update');
-
- $t1 = ExecTime();
-
- $rs = $this->linkID->exec($this->queryString);
-
-
-
- if ($this->recordLog) {
- $queryTime = ExecTime() - $t1;
- $this->RecordLog($queryTime);
-
- }
- return $rs;
- }
-
-
-
- function ExecuteNoneQuery2($sql = '')
- {
- global $dsqlite;
- if (!$dsqlite->isInit) {
- $this->Init($this->pconnect);
- }
- if ($dsqlite->isClose) {
- $this->Open(FALSE);
- $dsqlite->isClose = FALSE;
- }
-
- if (!empty($sql)) {
- $this->SetQuery($sql);
- }
- if (is_array($this->parameters)) {
- foreach ($this->parameters as $key => $value) {
- $this->queryString = str_replace("@".$key, "'$value'", $this->queryString);
- }
- }
- $t1 = ExecTime();
- $this->linkID->exec($this->queryString);
-
-
- if ($this->recordLog) {
- $queryTime = ExecTime() - $t1;
- $this->RecordLog($queryTime);
-
- }
-
- return $this->linkID->changes();
- }
-
- function ExecNoneQuery($sql = '')
- {
- return $this->ExecuteNoneQuery($sql);
- }
-
- function GetFetchRow($id = 'me')
- {
- return $this->result[$id]->numColumns();
- }
-
- function GetAffectedRows()
- {
- return $this->linkID->changes();
- }
-
-
- function Execute($id = "me", $sql = '')
- {
- global $dsqlite;
- if (!$dsqlite->isInit) {
- $this->Init($this->pconnect);
- }
- if ($dsqlite->isClose) {
- $this->Open(FALSE);
- $dsqlite->isClose = FALSE;
- }
- if (!empty($sql)) {
- $this->SetQuery($sql);
- }
-
- if ($this->safeCheck) {
- CheckSql($this->queryString);
- }
-
- $t1 = ExecTime();
-
-
- $this->result[$id] = $this->linkID->query($this->queryString);
-
-
-
-
- if ($this->recordLog) {
- $queryTime = ExecTime() - $t1;
- $this->RecordLog($queryTime);
-
- }
-
- if ($this->result[$id] === FALSE) {
- $this->DisplayError($this->linkID->lastErrorMsg()." <br>Error sql: <span style='color:#dc3545'>".$this->queryString."</span>");
- }
- }
-
- function Query($id = "me", $sql = '')
- {
- $this->Execute($id, $sql);
- }
-
-
- function GetOne($sql = '', $acctype = MYSQLI_ASSOC)
- {
- global $dsqlite;
- if (!$dsqlite->isInit) {
- $this->Init($this->pconnect);
- }
- if ($dsqlite->isClose) {
- $this->Open(FALSE);
- $dsqlite->isClose = FALSE;
- }
- if (!empty($sql)) {
- if (!preg_match("/LIMIT/i", $sql)) $this->SetQuery(preg_replace("/[,;]$/i", '', trim($sql))." LIMIT 0,1;");
- else $this->SetQuery($sql);
- }
- $this->Execute("one");
- $arr = $this->GetArray("one", $acctype);
- if (!is_array($arr)) {
- return '';
- } else {
- $this->result["one"]->reset();
- return ($arr);
- }
- }
-
-
- function ExecuteSafeQuery($sql, $id = "me")
- {
- global $dsqlite;
- if (!$dsqlite->isInit) {
- $this->Init($this->pconnect);
- }
- if ($dsqlite->isClose) {
- $this->Open(FALSE);
- $dsqlite->isClose = FALSE;
- }
- $this->result[$id] = $this->linkID->query($sql);
- }
-
-
-
- function GetArray($id = "me", $acctype = SQLITE3_ASSOC)
- {
- switch ($acctype) {
- case MYSQL_ASSOC:
- $acctype = SQLITE3_ASSOC;
- break;
- case MYSQL_NUM:
- $acctype = SQLITE3_NUM;
- break;
- default:
- $acctype = SQLITE3_BOTH;
- break;
- }
-
- if ($this->result[$id] === 0) {
- return FALSE;
- } else {
- $rs = $this->result[$id]->fetchArray($acctype);
- if (!$rs) {
- $this->result[$id] = 0;
- return false;
- }
- return $rs;
- }
- }
-
- function GetObject($id = "me")
- {
- if (!isset($this->_fixObject[$id])) {
- $this->_fixObject[$id] = array();
- while ($row = $this->result[$id]->fetchArray(SQLITE3_ASSOC)) {
- $this->_fixObject[$id][] = (object)$row;
- }
- $this->result[$id]->reset();
- }
- return array_shift($this->_fixObject[$id]);
- }
-
-
- function IsTable($tbname)
- {
- global $dsqlite;
- if (!$dsqlite->isInit) {
- $this->Init($this->pconnect);
- }
- $prefix = "#@__";
- $tbname = str_replace($prefix, $GLOBALS['cfg_dbprefix'], $tbname);
-
- $row = $this->linkID->querySingle("PRAGMA table_info({$tbname});");
-
- if ($row !== null) {
- return TRUE;
- }
- return FALSE;
- }
-
-
- function GetVersion($isformat = TRUE)
- {
- global $dsqlite;
- if (!$dsqlite->isInit) {
- $this->Init($this->pconnect);
- }
- if ($dsqlite->isClose) {
- $this->Open(FALSE);
- $dsqlite->isClose = FALSE;
- }
- $rs = $this->linkID->querySingle("select sqlite_version();");
- $sqlite_version = $rs;
- if ($isformat) {
- $sqlite_versions = explode(".", trim($sqlite_version));
- $sqlite_version = number_format($sqlite_versions[0].".".$sqlite_versions[1], 2);
- }
- return $sqlite_version;
- }
-
-
- function GetTableFields($tbname, $id = "me")
- {
- global $dsqlite;
- if (!$dsqlite->isInit) {
- $this->Init($this->pconnect);
- }
- $prefix = "#@__";
- $tbname = str_replace($prefix, $GLOBALS['cfg_dbprefix'], $tbname);
- $query = "SELECT * FROM {$tbname} LIMIT 0,1";
- $this->result[$id] = $this->linkID->query($query);
- }
-
-
- function GetFieldObject($id = "me")
- {
- $cols = $this->result[$id]->numColumns();
- $fields = array();
- while ($row = $this->result[$id]->fetchArray()) {
- for ($i = 1; $i < $cols; $i++) {
- $fields[] = $this->result[$id]->columnName($i);
- }
- }
-
- return (object)$fields;
- }
-
-
- function GetTotalRow($id = "me")
- {
- $queryString = preg_replace("/SELECT(.*)FROM/isU", 'SELECT count(*) as dd FROM', $this->queryString);
- $rs = $this->linkID->query($queryString);
- $row = $rs->fetchArray();
- return $row['dd'];
- }
-
-
- function GetLastID()
- {
-
-
-
-
-
- return $this->linkID->lastInsertRowID();
- }
-
-
- function FreeResult($id = "me")
- {
- if ($this->result[$id]) {
- @$this->result[$id]->reset();
- }
- }
- function FreeResultAll()
- {
- if (!is_array($this->result)) {
- return '';
- }
- foreach ($this->result as $kk => $vv) {
- if ($vv) {
- @$vv->reset();
- }
- }
- }
-
-
- function SetQuery($sql)
- {
- $prefix = "#@__";
- $sql = str_replace($prefix, $GLOBALS['cfg_dbprefix'], $sql);
- $this->queryString = $sql;
-
- if (preg_match("/CONCAT\(([^\)]*?)\)/i", $this->queryString, $matches)) {
- $this->queryString = preg_replace("/CONCAT\(([^\)]*?)\)/i", str_replace(",", "||", $matches[1]), $this->queryString);
- $this->queryString = str_replace("'||'", "','", $this->queryString);
- }
-
- $this->queryString = preg_replace("/FIND_IN_SET\('([\w]+)', arc.flag\)>0/i", "(',' || arc.flag || ',') LIKE '%,\\1,%'", $this->queryString);
- $this->queryString = preg_replace("/FIND_IN_SET\('([\w]+)', arc.flag\)<1/i", "(',' || arc.flag || ',') NOT LIKE '%,\\1,%'", $this->queryString);
- if (preg_match("/CREATE TABLE/i", $this->queryString)) {
- $this->queryString = preg_replace("/[\r\n]/", '', $this->queryString);
- $this->queryString = preg_replace('/character set (.*?) /i', '', $this->queryString);
- $this->queryString = preg_replace('/unsigned/i', '', $this->queryString);
- $this->queryString = str_replace('TYPE=MyISAM', '', $this->queryString);
-
- $this->queryString = preg_replace('/TINYINT\(([\d]+)\)/i', 'INTEGER', $this->queryString);
- $this->queryString = preg_replace('/mediumint\(([\d]+)\)/i', 'INTEGER', $this->queryString);
- $this->queryString = preg_replace('/smallint\(([\d]+)\)/i', 'INTEGER', $this->queryString);
- $this->queryString = preg_replace('/int\(([\d]+)\)/i', 'INTEGER', $this->queryString);
- $this->queryString = preg_replace('/auto_increment/i', 'PRIMARY KEY AUTOINCREMENT', $this->queryString);
- $this->queryString = preg_replace('/, KEY(.*?)MyISAM;/i', '', $this->queryString);
- $this->queryString = preg_replace('/, KEY(.*?);/i', ');', $this->queryString);
- $this->queryString = preg_replace('/, UNIQUE KEY(.*?);/i', ');', $this->queryString);
- $this->queryString = preg_replace('/set\(([^\)]*?)\)/', 'varchar', $this->queryString);
- $this->queryString = preg_replace('/enum\(([^\)]*?)\)/', 'varchar', $this->queryString);
- if (preg_match("/PRIMARY KEY AUTOINCREMENT/", $this->queryString)) {
- $this->queryString = preg_replace('/,([\t\s ]+)PRIMARY KEY \(`([0-9a-zA-Z]+)`\)/i', '', $this->queryString);
- $this->queryString = str_replace(', PRIMARY KEY (`id`)', '', $this->queryString);
- }
- }
- $this->queryString = preg_replace("/SHOW fields FROM `([\w]+)`/i", "PRAGMA table_info('\\1') ", $this->queryString);
- $this->queryString = preg_replace("/SHOW CREATE TABLE .([\w]+)/i", "SELECT 0,sql FROM sqlite_master WHERE name='\\1'; ", $this->queryString);
-
- $this->queryString = preg_replace("/Show Tables/i", "SELECT name FROM sqlite_master WHERE type = \"table\"", $this->queryString);
- $this->queryString = str_replace("\'", "\"", $this->queryString);
- $this->queryString = str_replace('\t\n', "", $this->queryString);
-
- }
-
- function SetSql($sql)
- {
- $this->SetQuery($sql);
- }
-
- function RecordLog($runtime = 0)
- {
- $RecordLogFile = dirname(__FILE__).'/../data/mysqli_record_log.inc';
- $url = $this->GetCurUrl();
- $savemsg = <<<EOT
-
- ------------------------------------------
- SQL:{$this->queryString}
- Page:$url
- Runtime:$runtime
- EOT;
- $fp = @fopen($RecordLogFile, 'a');
- @fwrite($fp, $savemsg);
- @fclose($fp);
- }
-
-
- function DisplayError($msg)
- {
- $errorTrackFile = dirname(__FILE__).'/../data/mysqli_error_trace.inc';
- if (file_exists(dirname(__FILE__).'/../data/mysqli_error_trace.php')) {
- @unlink(dirname(__FILE__).'/../data/mysqli_error_trace.php');
- }
- if ($this->showError) {
- $emsg = '';
- $emsg .= "<div><h3>DedeBIZ Error Warning!</h3>\r\n";
- $emsg .= "<div><a href='https://www.dedebiz.com' target='_blank' style='color:red'>Technical Support: https://www.dedebiz.com</a></div>";
- $emsg .= "<div style='line-helght:160%;font-size:14px;color:green'>\r\n";
- $emsg .= "<div style='color:blue'><br>Error page: <span style='color:#dc3545'>".$this->GetCurUrl()."</span></div>\r\n";
- $emsg .= "<div>Error infos: {$msg}</div>\r\n";
- $emsg .= "<br></div></div>\r\n";
-
- echo $emsg;
- }
-
- $savemsg = 'Page: '.$this->GetCurUrl()."\r\nError: ".$msg."\r\nTime".date('Y-m-d H:i:s');
-
- $fp = @fopen($errorTrackFile, 'a');
- @fwrite($fp, '<'.'?php exit();'."\r\n/*\r\n{$savemsg}\r\n*/\r\n?".">\r\n");
- @fclose($fp);
- }
-
-
- function GetCurUrl()
- {
- if (!empty($_SERVER["REQUEST_URI"])) {
- $scriptName = $_SERVER["REQUEST_URI"];
- $nowurl = $scriptName;
- } else {
- $scriptName = $_SERVER["PHP_SELF"];
- if (empty($_SERVER["QUERY_STRING"])) {
- $nowurl = $scriptName;
- } else {
- $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
- }
- }
- return $nowurl;
- }
- }
-
-
- function CopySQLiPoint(&$ndsql)
- {
- $GLOBALS['dsqlite'] = $ndsql;
- }
-
-
- if (!function_exists('CheckSql')) {
- function CheckSql($db_string, $querytype = 'select')
- {
- global $cfg_cookie_encode;
- $clean = '';
- $error = '';
- $old_pos = 0;
- $pos = -1;
- $log_file = DEDEINC.'/../data/'.md5($cfg_cookie_encode).'_safe.txt';
- $userIP = GetIP();
- $getUrl = GetCurUrl();
-
-
- if ($querytype == 'select') {
- $notallow1 = "[^0-9a-z@\._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@\.-]{1,}";
-
-
- if (preg_match("/".$notallow1."/i", $db_string)) {
- fputs(fopen($log_file, 'a+'), "$userIP||$getUrl||$db_string||SelectBreak\r\n");
- exit("<span>Safe Alert: Request Error step 1 !</span>");
- }
- }
-
-
- 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(array('~\s+~s'), array(' '), $clean)));
-
- if (
- strpos($clean, '@') !== FALSE or strpos($clean, 'char(') !== FALSE or strpos($clean, '"') !== FALSE
- or strpos($clean, '$s$$s$') !== FALSE
- ) {
- $fail = TRUE;
- if (preg_match("#^create table#i", $clean)) $fail = FALSE;
- $error = "unusual character";
- }
-
-
- if (strpos($clean, 'union') !== FALSE && preg_match('~(^|[^a-z])union($|[^[a-z])~s', $clean) != 0) {
- $fail = TRUE;
- $error = "union detect";
- }
-
-
- elseif (strpos($clean, '/*') > 2 || strpos($clean, '--') !== FALSE || strpos($clean, '#') !== FALSE) {
- $fail = TRUE;
- $error = "comment detect";
- }
-
-
- elseif (strpos($clean, 'sleep') !== FALSE && preg_match('~(^|[^a-z])sleep($|[^[a-z])~s', $clean) != 0) {
- $fail = TRUE;
- $error = "slown down detect";
- } elseif (strpos($clean, 'benchmark') !== FALSE && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
- $fail = TRUE;
- $error = "slown down detect";
- } elseif (strpos($clean, 'load_file') !== FALSE && preg_match('~(^|[^a-z])load_file($|[^[a-z])~s', $clean) != 0) {
- $fail = TRUE;
- $error = "file fun detect";
- } elseif (strpos($clean, 'into outfile') !== FALSE && preg_match('~(^|[^a-z])into\s+outfile($|[^[a-z])~s', $clean) != 0) {
- $fail = TRUE;
- $error = "file fun detect";
- }
-
-
- elseif (preg_match('~\([^)]*?select~s', $clean) != 0) {
- $fail = TRUE;
- $error = "sub select detect";
- }
- if (!empty($fail)) {
- fputs(fopen($log_file, 'a+'), "$userIP||$getUrl||$db_string||$error\r\n");
- exit("<span>Safe Alert: Request Error step 2!</span>");
- } else {
- return $db_string;
- }
- }
- }
|