ACC SHELL

Path : /srv/www/vhosts/marevva/nadacekrizovatka/
File Upload :
Current File : /srv/www/vhosts/marevva/nadacekrizovatka/class.db.php

<?php
	class db
	{
		private $_db;
		private $_collation = "utf8";
		private $_prefix;
		private $_lastID;
		
		// --------------------------------------------------------------------------------
	   
		public function __construct( $dbServer, $dbUser, $dbPass, $dbDatabase, $dbPrefix = '' ) 
		{
			$this->_prefix = $dbPrefix;

			$this->_db = new mysqli( $dbServer, $dbUser, $dbPass, $dbDatabase );
			
			/*
			 * This is the "official" OO way to do it,
			 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
			 *
			if ($mysqli->connect_error) 
			{
				die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
			}
			*/
			
			/*
			 * Use this instead of $connect_error if you need to ensure
			 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
			 */
			if (mysqli_connect_error()) 
			{
				die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
			}    
			
			$this->query( " SET sql_mode = '' " );
			
		}
		
		public function __destruct()
		{
			$this->_db->close();
		}
		
		// --------------------------------------------------------------------------------
		
		public function error($echo = true)
		{
			if ( mysql_error() != '' )
			{
				echo '<b>MySQL Error</b>: ' . mysql_error() . '<br/>';
				
				if ( mysql_error() == 'Too many connections' )
				{
					echo '	<script type="text/javascript">
						
							window.location.href = \'http://' .$_SERVER['HTTP_HOST']. '/temp-down.html\';
						
					</script>';
				}
			}
		}
		
		public function getDBlink()
	  	{
		  	return $this->_db;
	  	}
		
		public function getLastID()
		{
			return  $this->_lastID;
		}
		
		public function getRealLastID()
		{
			return mysqli_insert_id( $this->_db );
		}

		
		public function getCollation()
	  	{
		  	return $this->_collation;
	  	}
		public function setCollation( $_collation )
	  	{
		  	$this->_collation = $_collation;
	  	}
		
		public function getPrefix()
	  	{
		  	return $this->_prefix;
	  	}
		public function setPrefix( $_prefix )
	  	{
		  	$this->_prefix = $_prefix;
	  	}
		
		// --------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------
		
		public function getItem( $table, $ID, $fieldName = 'ID' )
		{				
			$this->_db->set_charset( $this->_collation );
			
			$query = "SELECT * FROM `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "` WHERE `" . $fieldName . "` = '" . $this->_db->real_escape_string( $ID ) . "' LIMIT 1";									
			$sql = $this->_db->query( $query );
			
			//echo '<!-- SQL query:' . $query . ' -->';
			
			if ( !$sql )
			{
				return $this->error();
			}
			else
			{
				$result = $sql->fetch_array( MYSQLI_ASSOC );
				$sql->close();
				
				return $result;
			}
		}
		
		// --------------------------------------------------------------------------------
		
		public function getItemWhere( $table, $where )
		{				
			$this->_db->set_charset( $this->_collation );
			
			$query = "SELECT * FROM `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "` " . $where . " LIMIT 1";						
			
			//echo $query;
			
			$sql = $this->_db->query( $query );
			
			if ( !$sql )
			{
				return $this->error();
			}
			else
			{
				$result = $sql->fetch_array( MYSQLI_ASSOC );
				$sql->close();
				
				return $result;
			}
		}
		
		// --------------------------------------------------------------------------------
		
		public function getAllItems( $table, $where = '' )
		{
			$this->_db->set_charset( $this->_collation );
			
			$query = "SELECT * FROM `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "` " . $where;
			
			//echo '<!-- SQL query:' . $query . ' -->';
			//echo '<br/>' . $query . '<br/>';
			
			$sql = $this->_db->query( $query );
			
			if (!$sql)
			{
				return $this->error();
			}
			else
			{
				$result = array();
				
				while ( $row = $sql->fetch_array( MYSQLI_ASSOC ) )
					$result[] = $row;
				
				$sql->close();
					
				return $result;
			}
		}
		
		// --------------------------------------------------------------------------------
		
		public function getAllItemsEx( $table, $where = '', $columns = '' )
		{
			$this->_db->set_charset( $this->_collation );
			
			$query = "SELECT ".$columns." FROM `" . $this->_db->real_escape_string( $table ) . "` " . $where;
			
			//echo '<!-- SQL query:' . $query . ' -->';
			
			$sql = $this->_db->query( $query );
			
			if (!$sql)
			{
				return $this->error();
			}
			else
			{
				$result = array();
				
				while ( $row = $sql->fetch_array( MYSQLI_ASSOC ) )
					$result[] = $row;
				
				$sql->close();
					
				return $result;
			}
		}
		
		// --------------------------------------------------------------------------------
		
		public function setItem( $table, $ID, $tbFields )
        {
            $this->_db->set_charset( $this->_collation );
			
			$query = "UPDATE `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "` SET ";
			
			$i = 0;
			foreach ( $tbFields as $name => $value )
			{
				if ( $i > 0 )
					$query .= ", ";
				
				if ( $value == 'CURRENT_TIMESTAMP' )
				{
					$query .= "`" . $this->_db->real_escape_string( $name ) . "` = CURRENT_TIMESTAMP ";
				}
				else
				{
					$query .= "`" . $this->_db->real_escape_string( $name ) . "` = '" . $this->_db->real_escape_string( $value ) . "' ";
				}

				$i++;
			}
			
			$query .= "WHERE `ID` = " . $this->_db->real_escape_string( $ID );
			
			//echo 'setItem: ' . $query . '<br/>';
			
            $result = $this->_db->query( $query );
			
			$this->_db->query( "OPTIMIZE TABLE `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "`" );
			
			return $result;
        }
		
		// --------------------------------------------------------------------------------
		
		public function setItemUnescaped( $table, $ID, $tbFields )
        {
            $this->_db->set_charset( $this->_collation );
			
			$query = "UPDATE `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "` SET ";
			
			$i = 0;
			foreach ( $tbFields as $name => $value )
			{
				if ( $i > 0 )
					$query .= ", ";
				
				$query .= "`" . ( $name ) . "` = '" . ( $value ) . "' ";
				
				$i++;
			}
			
			$query .= "WHERE `ID` = " . ( $ID );
			
			//echo 'setItemUnescaped: ' . $query . '<br/>';
			
            $result = $this->_db->query( $query );
			
			$this->_db->query( "OPTIMIZE TABLE `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "`" );
			
			return $result;
        }
		
		// --------------------------------------------------------------------------------
		
		public function setItemUnescapedEx( $table, $ID, $tbFields )
        {
            $this->_db->set_charset( $this->_collation );
			
			$query = "UPDATE `" . $this->_prefix . ( $table ) . "` SET ";
			
			$i = 0;
			foreach ( $tbFields as $name => $value )
			{
				if ( $i > 0 )
					$query .= ", ";
				
				$query .= "`" . ( $name ) . "` = '" . ( $value ) . "' ";
				
				$i++;
			}
			
			$query .= "WHERE `ID` = " . ( $ID );
			
            $result = $this->_db->query( $query );
			
			$this->_db->query( "OPTIMIZE TABLE `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "`" );
			
			return $result;
        }
		
		// --------------------------------------------------------------------------------
		
		public function insertItem( $table, $tbFields )
		{			
			$this->_db->set_charset( $this->_collation );
			
			$query = "INSERT INTO `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "` (";
			
			$i = 0;
			foreach ( $tbFields as $name => $value )
			{
				if ( $i > 0 )
					$query .= ", ";
				
				$query .= "`" . $this->_db->real_escape_string( $name ) . "`";
				
				$i++;
			}
			
			$query .= " ) VALUES ( ";
			
			$i = 0;
			foreach ( $tbFields as $name => $value )
			{
				if ( $i > 0 )
					$query .= ", ";
				
				$query .= "'" . $this->_db->real_escape_string( $value ) . "' ";
				
				$i++;
			}
			
			$query .= ")";
			
			//echo $query;
			
			$result = $this->_db->query( $query );
			
			$this->_lastID = $this->_db->insert_id;		
			
			$this->_db->query( "OPTIMIZE TABLE `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "`" );
			
			return $result;
		}
		
		// --------------------------------------------------------------------------------
		
		public function insertItemUnescaped( $table, $tbFields )
		{			
			$this->_db->set_charset( $this->_collation );
			
			$query = "INSERT INTO `" . $this->_prefix . ( $table ) . "` (";
			
			$i = 0;
			foreach ( $tbFields as $name => $value )
			{
				if ( $i > 0 )
					$query .= ", ";
				
				$query .= "`" . ( $name ) . "`";
				
				$i++;
			}
			
			$query .= " ) VALUES ( ";
			
			$i = 0;
			foreach ( $tbFields as $name => $value )
			{
				if ( $i > 0 )
					$query .= ", ";
				
				$query .= "'" . ( $value ) . "' ";
				
				$i++;
			}
			
			$query .= ")";
			
			//echo $query;
			
			$result = $this->_db->query( $query );	
			
			$this->_lastID = $this->_db->insert_id;			
			
			$this->_db->query( "OPTIMIZE TABLE `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "`" );
			
			return $result;
		}	
		
		// --------------------------------------------------------------------------------
		
		public function deleteItem( $table, $ID )
		{
			$query = "DELETE FROM `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "` WHERE `ID` = " . $this->_db->real_escape_string($ID);
			$result = $this->_db->query( $query );
			
			$this->_db->query( "OPTIMIZE TABLE `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "`" );
			
			return $result;
		}
		
		// --------------------------------------------------------------------------------
		
		public function deleteItemWhere( $table, $where )
		{
			$query = "DELETE FROM `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "` " . $where;
			$result = $this->_db->query( $query );
			
			$this->_db->query( "OPTIMIZE TABLE `" . $this->_prefix . $this->_db->real_escape_string( $table ) . "`" );
			
			return $result;
		}
		
		// --------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------
		
		function getSearchResults( $serachQuery, $table, $where, $limit )
		{
			$numargs = func_num_args();
			
			if ( $numargs < 5 ) 
			{
				return array();				
			}
			else
			{
				$this->_db->set_charset( $this->_collation );
				
				$serachQuery = $this->_db->real_escape_string( $serachQuery );
				$serachQueryHTML = $this->_db->real_escape_string( htmlentities($serachQuery, ENT_COMPAT, "UTF-8") );
				
				if ( $where != '' )
					$where .= " AND ";
				
				$searchIn = '';
				$arg_list = func_get_args();
				
				for ( $i = 4; $i < $numargs; $i++ )
				{
					$searchIn .= "`" . $arg_list[$i] . "` LIKE '%" . $serachQuery . "%'";
					$searchIn .= " OR `" . $arg_list[$i] . "` LIKE '%" . $serachQueryHTML . "%'";
					if ( $i < $numargs - 1 )
						$searchIn .= " OR ";
				}
				
				if ( $limit > 0 )
					$limit = " LIMIT " . $limit;
				else
					$limit = '';
				
				$query = "SELECT * FROM `" . $this->_prefix . $table . "` WHERE " . $where . " (" . $searchIn . ")" . $limit;
				
				//echo $query . '<br/><br/>';
				
				$sql = $this->_db->query( $query );
				
				if ( $sql )
				{
					$result = array();
					
					while ( $row = $sql->fetch_array( MYSQLI_ASSOC ) )
						$result[] = $row;
					
					$sql->close();
				
					return $result;
				}
				
				return array();
			}
		}
		
		// --------------------------------------------------------------------------------
		
		function getSearchResultsEx( $serachQuery, $table, $where, $limit, $fields, $fieldsReturn )
		{
			$this->_db->set_charset( $this->_collation );
			
			$serachQuery = $this->_db->real_escape_string( htmlentities($serachQuery, ENT_COMPAT, "UTF-8") );
			
			if ( $where != '' )
				$where .= " AND ";
			
			$searchIn = '';
			for ( $i = 0; $i < count($fields); $i++ )
			{
				$searchIn .= "`" . $fields[$i] . "` LIKE '%" . $serachQuery . "%'";
				if ( $i < count($fields) - 1 )
					$searchIn .= " OR ";
			}
			
			$searchWhat = '';
			for ( $i = 0; $i < count($fieldsReturn); $i++ )
			{
				$searchWhat .= "`" . $fieldsReturn[$i] . "`";
				if ( $i < count($fieldsReturn) - 1 )
					$searchWhat .= ", ";
			}
			
			if ( $limit > 0 )
				$limit = " LIMIT " . $limit;
			else
				$limit = '';
			
			$query = "SELECT " . $searchWhat . " FROM `" . $this->_prefix . $table . "` WHERE " . $where . " (" . $searchIn . ")" . $limit;
			
			//echo $query . '<br/><br/>';
			
			$sql = $this->_db->query( $query );
			
			if ( $sql )
			{
				$result = array();
				
				while ( $row = $sql->fetch_array( MYSQLI_ASSOC ) )
					$result[] = $row;
				
				$sql->close();
			
				return $result;
			}
			
			return array();
		}
		
		// --------------------------------------------------------------------------------
		
		public function query( $query )
		{
			//echo $query . '<br/><br/>';
			if ( gettype( $query ) != 'string' )
				return false;
			
			$result = $this->_db->query( $query );
			
			return $result;
		}
		
	}
?>

ACC SHELL 2018