// Debug on/off
define("DEBUG",0);
// SQL counter
$query_counter=0;
/**
* MySQL database working class
*
*/
class MySQLDB {
private static $instance;
private $db_config;
public $connection;
public static function connect() {
if (!isset(self::$instance)) {
$c =__CLASS__;
self::$instance=new $c;
}
return self::$instance;
}
public function __clone() {
trigger_error('Cloning not allowed.', E_USER_ERROR);
}
public function __wakeup() {
trigger_error('Cloning not allowed.', E_USER_ERROR);
}
private function __construct() {
try {
if(!($this->db_config = parse_ini_file('config/mysql.ini'))) {
throw new Exception('Cannot load mysqli configuration: ' . __FILE__ . ' ' . __LINE__);
}
$this->connection=new mysqli($this->db_config['server'], $this->db_config['username'], $this->db_config['password'], $this->db_config['db']);
if (mysqli_connect_error()) {
throw new Exception('Database error: ' . mysqli_connect_error());
}
}
catch( Exception $e ) {
print $e->getMessage().'<br />';
}
$this->connection->set_charset('utf8');
}
function query($query='') {
global $query_counter;
if(DEBUG) print '<br /> Query : '.$query.'<br/>';
$result = mysqli_query($this->connection,$query) or die('Error [query]: <br />'.mysqli_error($mysqldb->connection).'<br /> Query :<br/>'.$query);
$query_counter++;
return $result;
}
function __destruct() {
$this->connection->close();
}
}
/**
* Returns cutted down entry data
*
* @param string $data
* @param int $mode
* @return string
*/
function vf($data,$mode=0) {
switch ($mode) {
case 1:
return preg_replace("/[^a-z0-9A-Z]+/",'',$data); // числа, буквы
break;
case 2:
return preg_replace("/[^a-zA-Z]+/",'',$data); // буквы
break;
case 3:
return preg_replace("/[^0-9]+/",'',$data); // числа
break;
case 4:
return preg_replace("/[^a-z0-9A-Z\-_\.]+/",'',$data); // числа, буквы, тире, прочерк, точка
break;
case 5:
return preg_replace("/[^ [:punct:]".('a-zA-Z')."0-9]+/",'',$data); // соотв. текущему языку алфавит + цифры и знаки препинания
break;
case 'in':
return preg_replace("~\A[^0-9]+|[^0-9,]+|,[^0-9]*(?=,)|[^0-9]+\Z~",'',$data); // числа, запятые для "SELECT * FROM `table` WHERE `key` IN ($data)"
break;
default:
return preg_replace("/[~@\+\?\%\/\;=\*\>\<\"\'\-]+/",'',$data); // блек-лист в крайнем случае.
break;
}
}