| | |
| | | |
| | | if($iPos2 !== false && ($iPos === false || $iPos2 <= $iPos)) { |
| | | $sTxt = $this->escape($sValue); |
| | | |
| | | |
| | | $sTxt = str_replace('`', '', $sTxt); |
| | | if(strpos($sTxt, '.') !== false) $sTxt = preg_replace('/^(.+)\.(.+)$/', '`$1`.`$2`', $sTxt); |
| | | else $sTxt = '`' . $sTxt . '`'; |
| | | |
| | |
| | | mysqli_query($this->_iConnId, 'SET NAMES '.$this->dbCharset); |
| | | mysqli_query($this->_iConnId, "SET character_set_results = '".$this->dbCharset."', character_set_client = '".$this->dbCharset."', character_set_connection = '".$this->dbCharset."', character_set_database = '".$this->dbCharset."', character_set_server = '".$this->dbCharset."'"); |
| | | } |
| | | |
| | | private function securityScan($string) { |
| | | global $app, $conf; |
| | | |
| | | // get security config |
| | | if(isset($app)) { |
| | | $app->uses('getconf'); |
| | | $ids_config = $app->getconf->get_security_config('ids'); |
| | | |
| | | if($ids_config['sql_scan_enabled'] == 'yes') { |
| | | |
| | | // Remove whitespace |
| | | $string = trim($string); |
| | | if(substr($string,-1) == ';') $string = substr($string,0,-1); |
| | | |
| | | // Save original string |
| | | $string_orig = $string; |
| | | |
| | | //echo $string; |
| | | $chars = array(';', '#', '/*', '*/', '--', '\\\'', '\\"'); |
| | | |
| | | $string = str_replace('\\\\', '', $string); |
| | | $string = preg_replace('/(^|[^\\\])([\'"])\\2/is', '$1', $string); |
| | | $string = preg_replace('/(^|[^\\\])([\'"])(.*?[^\\\])\\2/is', '$1', $string); |
| | | $ok = true; |
| | | |
| | | if(substr_count($string, "`") % 2 != 0 || substr_count($string, "'") % 2 != 0 || substr_count($string, '"') % 2 != 0) { |
| | | $app->log("SQL injection warning (" . $string_orig . ")",2); |
| | | $ok = false; |
| | | } else { |
| | | foreach($chars as $char) { |
| | | if(strpos($string, $char) !== false) { |
| | | $ok = false; |
| | | $app->log("SQL injection warning (" . $string_orig . ")",2); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | if($ok == true) { |
| | | return true; |
| | | } else { |
| | | if($ids_config['sql_scan_action'] == 'warn') { |
| | | // we return false in warning level. |
| | | return false; |
| | | } else { |
| | | // if sql action = 'block' or anything else then stop here. |
| | | $app->error('Possible SQL injection. All actions have been logged.'); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private function _query($sQuery = '') { |
| | | global $app; |
| | |
| | | |
| | | $aArgs = func_get_args(); |
| | | $sQuery = call_user_func_array(array(&$this, '_build_query_string'), $aArgs); |
| | | $this->securityScan($sQuery); |
| | | |
| | | $this->_iQueryId = mysqli_query($this->_iConnId, $sQuery); |
| | | if (!$this->_iQueryId) { |
| | |
| | | } |
| | | |
| | | public function queryOne($sQuery = '') { |
| | | return $this->query_one($sQuery); |
| | | return call_user_func_array(array(&$this, 'queryOneRecord'), func_get_args()); |
| | | } |
| | | |
| | | public function query_one($sQuery = '') { |
| | | return $this->queryOneRecord($sQuery); |
| | | return call_user_func_array(array(&$this, 'queryOneRecord'), func_get_args()); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | public function queryAll($sQuery = '') { |
| | | return $this->queryAllRecords($sQuery); |
| | | return call_user_func_array(array(&$this, 'queryAllRecords'), func_get_args()); |
| | | } |
| | | |
| | | public function query_all($sQuery = '') { |
| | | return $this->queryAllRecords($sQuery); |
| | | return call_user_func_array(array(&$this, 'queryAllRecords'), func_get_args()); |
| | | } |
| | | |
| | | /** |
| | |
| | | global $app; |
| | | if(!is_string($sString) && !is_numeric($sString)) { |
| | | $app->log('NON-String given in escape function! (' . gettype($sString) . ')', LOGLEVEL_INFO); |
| | | $sAddMsg = getDebugBacktrace(); |
| | | //$sAddMsg = getDebugBacktrace(); |
| | | $app->log($sAddMsg, LOGLEVEL_DEBUG); |
| | | $sString = ''; |
| | | } |
| | |
| | | $mysql_error = (is_object($this->_iConnId) ? mysqli_error($this->_iConnId) : mysqli_connect_error()); |
| | | $mysql_errno = (is_object($this->_iConnId) ? mysqli_errno($this->_iConnId) : mysqli_connect_errno()); |
| | | |
| | | $sAddMsg .= getDebugBacktrace(); |
| | | //$sAddMsg .= getDebugBacktrace(); |
| | | |
| | | if($this->show_error_messages && $conf['demo_mode'] === false) { |
| | | echo $sErrormsg . $sAddMsg; |
| | | } else if(is_object($app) && method_exists($app, 'log')) { |
| | | $app->log($sErrormsg . $sAddMsg, LOGLEVEL_WARN); |
| | | $app->log($sErrormsg . $sAddMsg . ' -> ' . $mysql_errno . ' (' . $mysql_error . ')', LOGLEVEL_WARN); |
| | | } |
| | | } |
| | | |
| | |
| | | public function datalogSave($db_table, $action, $primary_field, $primary_id, $record_old, $record_new, $force_update = false) { |
| | | global $app, $conf; |
| | | |
| | | // Insert backticks only for incomplete table names. |
| | | if(stristr($db_table, '.')) { |
| | | $escape = ''; |
| | | } else { |
| | | $escape = '`'; |
| | | } |
| | | // Check fields |
| | | if(!preg_match('/^[a-zA-Z0-9\-\_\.]{1,64}$/',$db_table)) $app->error('Invalid table name '.$db_table); |
| | | if(!preg_match('/^[a-zA-Z0-9\-\_]{1,64}$/',$primary_field)) $app->error('Invalid primary field '.$primary_field.' in table '.$db_table); |
| | | |
| | | $primary_field = $this->quote($primary_field); |
| | | $primary_id = intval($primary_id); |
| | | |
| | | if($force_update == true) { |
| | | //* We force a update even if no record has changed |
| | |
| | | //** Inserts a record and saves the changes into the datalog |
| | | public function datalogInsert($tablename, $insert_data, $index_field) { |
| | | global $app; |
| | | |
| | | |
| | | // Check fields |
| | | if(!preg_match('/^[a-zA-Z0-9\-\_\.]{1,64}$/',$tablename)) $app->error('Invalid table name '.$tablename); |
| | | if(!preg_match('/^[a-zA-Z0-9\-\_]{1,64}$/',$index_field)) $app->error('Invalid index field '.$index_field.' in table '.$tablename); |
| | | |
| | | if(is_array($insert_data)) { |
| | | $key_str = ''; |
| | | $val_str = ''; |
| | |
| | | public function datalogUpdate($tablename, $update_data, $index_field, $index_value, $force_update = false) { |
| | | global $app; |
| | | |
| | | // Check fields |
| | | if(!preg_match('/^[a-zA-Z0-9\-\_\.]{1,64}$/',$tablename)) $app->error('Invalid table name '.$tablename); |
| | | if(!preg_match('/^[a-zA-Z0-9\-\_]{1,64}$/',$index_field)) $app->error('Invalid index field '.$index_field.' in table '.$tablename); |
| | | |
| | | $old_rec = $this->queryOneRecord("SELECT * FROM ?? WHERE ?? = ?", $tablename, $index_field, $index_value); |
| | | |
| | | if(is_array($update_data)) { |
| | |
| | | public function datalogDelete($tablename, $index_field, $index_value) { |
| | | global $app; |
| | | |
| | | // Check fields |
| | | if(!preg_match('/^[a-zA-Z0-9\-\_\.]{1,64}$/',$tablename)) $app->error('Invalid table name '.$tablename); |
| | | if(!preg_match('/^[a-zA-Z0-9\-\_]{1,64}$/',$index_field)) $app->error('Invalid index field '.$index_field.' in table '.$tablename); |
| | | |
| | | $old_rec = $this->queryOneRecord("SELECT * FROM ?? WHERE ?? = ?", $tablename, $index_field, $index_value); |
| | | $this->query("DELETE FROM ?? WHERE ?? = ?", $tablename, $index_field, $index_value); |
| | | $new_rec = array(); |
| | |
| | | case 'blob': |
| | | return 'blob'; |
| | | break; |
| | | case 'date': |
| | | return 'date'; |
| | | break; |
| | | } |
| | | } |
| | | |