Location: PHPKode > projects > MJGUEST > mjguest/database/sqlite2/driver.php
<?php
	
	/*
	+----------------------------------------------------------------
	|
	|	MJGUEST
	|	=============================================================
	|	Copyright (c) 2002-2008 Giacomo "mdsjack" Menni
	|	Terms of agreement and support at www.mdsjack.bo.it
	|
	+----------------------------------------------------------------
	|	[ DATABASE MODULE ]
	|	Database driver (SQLite 2.8+)
	+----------------------------------------------------------------
	*/
	
	class db_driver
	{
		private $id;
		private $query;
		private $lastquestion;
		private $path;
		
		public $driver		= 'SQLite'; # const
		public $db_file		= '';
		public $answer		= '';
		public $tot_queries	= 0;
		public $error		= false;
		
		public $tables		= array();
		
		private $questions = array
		(	'truncate'			=> 'DELETE FROM %s'
		,	'optimize'			=> 'VACUUM %s'
		,	'row_delete'		=> 'DELETE FROM %s WHERE %s=\'%s\''
		,	'row_count'			=> 'SELECT COUNT(*) FROM %s'
		,	'row_add'			=> 'INSERT INTO %s VALUES()'
		,	'settings_load'		=> 'SELECT * FROM [TBL=settings]'
		,	'settings_update'	=> 'UPDATE [TBL=settings] SET adminpass=\'%s\', adminmail=\'%s\', timezone=%d, dateformat=\'%s\', theme=\'%s\', entriesxpage=%u, wordfilter=%b, title=\'%s\', namelen=%u, msglen=%u, emoticons=%u, avatars_maxkilobytes=%u, redirectscreen=%b, notifynewentry=%b, captcha=%b, messenger=%b, entry_approval=%b, mod_account=%b, modpass=\'%s\', modmail=\'%s\', rating=%b, banned=\'%s\', name_admin=\'%s\', name_mod=\'%s\' '
		,	'settings_restore'	=> 'UPDATE [TBL=settings] SET adminpass=\'password\', adminmail=\'hide@address.com\', timezone=0, dateformat=\'d.m.Y h:ia\', theme=\'theme-hera\', entriesxpage=10, wordfilter=1, title=\'Website name\', namelen=15, msglen=500, emoticons=5, avatars_maxkilobytes=0, redirectscreen=1, notifynewentry=0, captcha=0, messenger=1, entry_approval=0, mod_account=0, modpass=\'moderator\', modmail=\'hide@address.com\', rating=1, banned=\'\', name_admin=\'\', name_mod=\'\' '
		,	'entry_add'			=> 'INSERT INTO [TBL=entries] VALUES(NULL, \'%s\', %b, \'%s\',\'%s\', %u, \'%s\', \'%s\', \'%s\', \'%s\',\'%s\',\'%s\',\'%s\', \'\', %b, %u)'
		,	'entry_get_set'		=> 'SELECT id, ip, pvt, name, country, datetime, message, dir, email, web, chat, uin, LENGTH(avatar) AS avatar, reply, approved, rating FROM [TBL=entries] %§ ORDER BY id DESC LIMIT %u,%u'
		,	'entry_get_one'		=> 'SELECT pvt, name, country, message, dir, email, web, chat, uin, LENGTH(avatar) AS avatar, reply, rating FROM [TBL=entries] WHERE id=%u'
		,	'entry_edit'		=> 'UPDATE [TBL=entries] SET pvt=\'%b\', name=\'%s\', country=\'%s\', message=\'%s\', dir=\'%s\', email=\'%s\', web=\'%s\', chat=\'%s\', uin=\'%s\', rating=%u %§ WHERE id=%u'
		,	'get_avatar'		=> 'SELECT avatar FROM [TBL=entries] WHERE id=%u'
		,	'check_flood'		=> 'SELECT MAX(datetime) FROM [TBL=entries] WHERE ip=\'%s\''
		,	'reply_save'		=> 'UPDATE [TBL=entries] SET reply=\'%s\' WHERE id=%u'
		,	'approve'			=> 'UPDATE [TBL=entries] SET approved=1 WHERE id=%u'
		,	'ban_guest'			=> 'UPDATE [TBL=settings] SET banned=\'%s\''
		,	'rating_stats'		=> 'SELECT AVG(rating) AS avg, COUNT(rating) AS tot FROM [TBL=entries] WHERE rating != 0'
		);
		
		function __construct($path = '')
		{
			@ini_set('sqlite.assoc_case', 2);
			
			$this->tables = array
			(	'[TBL=entries]'		=> db_flag.'entries'
			,	'[TBL=settings]'	=> db_flag.'settings'
			);
			
			$this->path = $path;
			$this->open();
		}
		
		function __destruct()
		{
		#	$this->close();
		}
		
		function ask($question, $sql = null, $params = array())
		{
			$this->lastquestion = $question;
			
			$this->query(strtr(vsprintf(str_replace('%§', $sql, $this->questions[$this->lastquestion]), $params), $this->tables));
			
			return true;
		}
		
		public function get_field($num = 0)
		{
			$this->answer = $this->query->fetch(SQLITE_NUM);
			
			return ($this->answer ? $this->answer[$num] : $this->error());
		}
		
		public function get_row()
		{
			$this->answer = $this->query->fetch(SQLITE_ASSOC);
			
			return ($this->answer ? true : $this->error());
		}
		
		private function open($persistance = 1)
		{
			$this->db_file = $this->path.db_host.(!db_name ? 'database.db' : db_name.'.db');
			
			$this->id = new SQLiteDatabase($this->db_file, 0666, $this->error);
			
			return ($this->id ? true : $this->error);
		}
		
		private function error()
		{
			if($this->error == true) return false;
			
			$errno = $this->id->lastError();
			
			if ($errno <> 0):
				echo "
				<h3>Database Error!</h3>
				<strong>Module:</strong> ".db_type."<br />
				<strong>Error (#{$errno} - ".$this->lastquestion."):</strong> ".sqlite_error_string($errno);
				
				$this->error = true;
				return false;
			endif;
			
			return true;
		}
		
		public function query($sql)
		{
			$this->query = $this->id->unbufferedQuery($sql);
		#	$this->query = $this->id->query($sql);
			
			$this->tot_queries++;
			
			return ($this->query ? true : $this->error());
		}
		
		public function escape($raw_data)
		{
		#	$raw_data = utf8_decode($raw_data);
			
			return !empty($raw_data) ? sqlite_escape_string($raw_data) : $raw_data;
		}
		
		public function sql($sqlfile)
		{
			$this->id->queryExec(str_replace('mjguest_', db_flag, file_get_contents($sqlfile)));
			
			$this->error();
		}
		
		public function free_memory()
		{
			unset($this->query);
		}
	}
?>
Return current item: MJGUEST