Current File : /home/users/barii/public_html/finansenl.com.pl/wodki/admin/classes/the_mysql.class.php
<?php

// Klasa MySQLi / PDO Mysql
// By Mariusz (THE)


define("SQL_HOST"           , "67973.m.tld.pl");
define("SQL_NAME"           , "baza67973_etykiety");
define("SQL_USER"           , "admin67973_etykiety");
define("SQL_PASS"           , '5BmF1MXif0');


class MySQL {

	public $value = array();
	public $query = null;
	public $row = array();
	public $logs = false;

    function __construct()
	{
		global $conn;

      	if(!$conn) $conn = new mysqli(SQL_HOST,SQL_USER,SQL_PASS,SQL_NAME);

		if($conn->connect_error): 
			$this->mysql(1000);
		endif;

		if(!$conn->query("SET NAMES UTF8")): 
			$this->mysql(1002);
		endif;

		$conn->set_charset('utf8mb4');



    }
    
    public function doQuery($query)
    {

		global $conn, $dbcounter;

		if(!$conn) return false;
		//if($_GET['dblog']) $starttime = microtime(true);
		$this->query = $conn->query($query);
		//if($_GET['dblog']) $endtime = microtime(true);
		//if($_GET['dblog']) $duration = $endtime - $starttime;

		if(isset($_GET['dblog'])) {

			if($dbcounter) $dbcounter++;
			else $dbcounter = 1;
			$_SESSION['dbcounter'] = $dbcounter;

		
		}

		return $this->query;
    	
    }



	
/**
 * Zapytanie: $sql->sqlQuery
 * @return 
 */

	public function sqlQuery($query)
	{
		global $conn;
		
		$db = null;

		$doQuery = $this->doQuery($query);

		if($doQuery):
			while($this->row = mysqli_fetch_assoc($doQuery)) { 
                $db[] = $this->row; 
            }
            return $db; 
		else:
			$error = $conn->errno;
		endif;
		if(!isset($db)): $error = $conn->errno; else:  endif;
	}
	
/**
 * Zapytanie: $sql->sqlExec
 * @return 
 */

	public function sqlExec($select, $table, $option)
	{
        $query = $this->doQuery("SELECT ".$select." FROM ".$table." ".$option);
        $db = null;

		if(is_object($query)):
            while($row = mysqli_fetch_assoc($query)) { 
                $db[] = $row;
            }
            return $db;
        endif;
	}
	
	public function sqlRow($select, $table, $option)
	{
		$queryText = "SELECT ".$select." FROM `".$table."` ".$option;
		if(strpos($queryText, "LIMIT") === false) $queryText .= " LIMIT 1";
		$this->query = $this->doQuery($queryText);
		$this->row = mysqli_fetch_assoc($this->query);
		return $this->row;
	}
	




	public function sqlInsert($table,$values)
	{

		global $conn;
		if(is_array($values)) {
			$queryText = "INSERT INTO $table VALUES ";
			$vals = array();
			foreach($values as $v) $vals[] = "(".$v.")";
			$queryText .= implode(', ', $vals);
			$this->query = $this->doQuery($queryText);
		} 
		else {
			$queryText = "INSERT INTO $table VALUES($values)";
			$this->query = $this->doQuery($queryText);
		}

		if($conn->error) {
			die("class.mysql.php : " . $conn->errno . " - " . $conn->error . $queryText);
		}
		return true;

	}

	public function sqlInsertArray($table,$array, $return_id = false)
	{

		global $conn;
		if(is_array($array)) {
			$keys = '';
			$values = '';
			$n = 0;
			foreach($array as $k => $v) {
				$n++;
				if($n > 1) {
					$keys .= ',';
					$values .= ',';
				}
				$keys .= "`".$this->escape($k)."`";
				if(is_null($v)) $values .= "null";
				else $values .= "'".$this->escape($v)."'";
			}
			$queryText = "INSERT INTO $table ($keys) VALUES ($values)";
			$this->query = $this->doQuery($queryText);
		}

		if($conn->error) {
    error_log("MySQL ERROR: " . $conn->errno . " - " . $conn->error . " | Zapytanie: $queryText");
    throw new Exception("Błąd bazy danych");
}
		if ($return_id) {
			return $conn->insert_id;
		} else {
			return true;
		}
		
	}
	
	public function sqlReplace($table,$values)
	{
		global $conn;
		$this->query = $this->doQuery("REPLACE INTO $table VALUES($values)");
		if($conn->error) {
			die("class.mysql.php : " . $conn->errno . " - " . $conn->error . "REPLACE INTO $table VALUES($values)");
		}
		return true;
	}

	public function sqlReplaceArray($table,$array)
	{

		global $conn;
		if(is_array($array)) {
			$keys = '';
			$values = '';
			$n = 0;
			foreach($array as $k => $v) {
				$n++;
				if($n > 1) {
					$keys .= ',';
					$values .= ',';
				}
				$keys .= "`".$this->escape($k)."`";
				if(is_null($v)) $values .= "null";
				else $values .= "'".$this->escape($v)."'";
			}
			$queryText = "REPLACE INTO $table ($keys) VALUES ($values)";
			$this->query = $this->doQuery($queryText);
		}

		if($conn->error) {
			die("class.mysql.php : " . $conn->errno . " - " . $conn->error . $queryText);
		}
		return true;

	}
	
	public function sqlInsertedId()
	{
		global $conn;
		return $conn->insert_id;
	}

	public function sqlDelete($table,$where)
	{
		global $conn;
		$this->query = $this->doQuery("DELETE FROM $table WHERE $where");
		if($conn->error) {
			die("class.mysql.php : " . $conn->errno . " - " . $conn->error . "DELETE FROM $table WHERE $where");
		}
	}

	public function sqlUpdate($table,$values,$where,$limit = 1)
	{
		global $conn;
		if(!isset($table)): die('class.mysql.php : nie podano wymaganych parametrów'); endif;
		if(!isset($values)): die('class.mysql.php : nie podano wymaganych parametrów'); endif;
		if(!isset($where)): die('class.mysql.php : nie podano wymaganych parametrów'); endif;

		$query = "UPDATE $table SET $values WHERE $where";
		if($limit > 0) $query .= " LIMIT ".intval($limit);

		$this->query = $this->doQuery($query);
		if($conn->error) {
			die("class.mysql.php : " . $conn->errno . " - " . $conn->error . $query);
		}
		return true;
	}
	
/**
 * Sprawdzanie czy wybrany rekord znajduje się w bazie danych
 * @param object $table - nazwa tabeli
 * @param object $where - warunek where
 * @return true lub false
 */
	public function sqlCheck($table, $where)
	{
		$this->query	=	$this->doQuery("SELECT * FROM $table WHERE $where");
		@$this->numrows	=	mysqli_num_rows($this->query);
		
		if($this->numrows==0): 
			return false;
		elseif($this->numrows>0): 
			return true; 
		endif;
	}
	
	public function sqlGetLastId($table, $order = 'id')
	{
		$this->query = $this->doQuery("SELECT ".$order." FROM ".$table." ORDER BY ".$order." DESC LIMIT 1");
		$this->row = mysqli_fetch_array($this->query);
		return $this->row[$order];
	}

	public function sqlHigherIdInSet( $query, $col = 'id' )
	{
		$rows = $this->sqlQuery($query." ORDER BY `".$this->escape($col)."` DESC LIMIT 1");
		return $rows[0][$col];
	}
	
	public function sqlCountSimple($table, $col = '', $options = '')
	{

		if(!$col && !$options)
			$query = $table;
		else
			$query = "SELECT COUNT(`".$this->escape($col)."`) FROM ".$this->escape($table)." ".$options;

		$rows = $this->sqlQuery($query);

		return (is_array($rows)) ? intval(current($rows[0])) : 0;
		
	}
	
	public function sqlCount( $table, $where = '' )
	{
		global $conn;

		if(empty($where)):
			$query = $conn->query($table);
		else:
			$query = $conn->query("SELECT * FROM ".$table." WHERE ".$where);
		endif;

		if($query)
			$this->row = @mysqli_num_rows($query);
		else
			return 0;

        if($this->row >= 1):
			return $this->row;
		else:
			return 0;
		endif;
	}

	public function sqlTableExist( $table )
	{
		$tables = $this->doQuery("SHOW TABLES LIKE '".$this->escape($table)."'");
		if($tables) {
			while (list ($temp) = @mysqli_fetch_array ($tables)) {
			if ($temp == $table) {
				return true;
				}
			}
		}
		return false;
	}

	public function sqlTableCopy( $from, $to )
	{
		$this->doQuery("CREATE TABLE `".$to."` LIKE `".$from."`"); 
		$this->doQuery("INSERT `".$to."` SELECT * FROM `".$from."`");
		return false;
	}
    
    public function generateIdent( $length, $checkInTable = false,  $tableCol = 'ident')
    {

        $pw = '';
        for($i=0;$i<$length;$i++) {
            switch(rand(1,3)):
            case 1: 
                $pw.=chr(rand(48,57));
                break; //0-9
            case 2:
                $pw.=chr(rand(65,90));
                break; //A-Z
            case 3:
                $pw.=chr(rand(97,122));
                break; //a-z
            endswitch;
        }

		if($checkInTable) {
			if($this->sqlCheck($checkInTable, "`".$this->escape($tableCol)."` = '".$this->escape($pw)."'")) {
				return $this->generateIdent($length, $checkInTable, $tableCol);
			}
		}

        return $pw;

    }
    
    public function show($numer, $opis)
	{

	}

	public function mysql($numer)
	{
		$this->show($numer, "MySQL Error");
		die();
	}
    
    public function system($numer)
    {
        $this->show($numer, "Fatal Error");
        die();
    }
    
    public function escape($text) {
    	global $conn;
    	return ($conn) ? $conn->real_escape_string($text) : addslashes($text);
    }

	public function sqlPassword( $input ) {
		$pass = strtoupper(
				sha1(
						sha1($input, true)
				)
		);
		$pass = '*' . $pass;
		return $pass;
	}


}