File "mysql.class.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/unlimited-elements-for-elementor/inc_php/framework/mysql.class.php
File size: 6.07 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

defined('UNLIMITED_ELEMENTS_INC') or die('Restricted access');
class UniteMySql
{
    const TYPE_KEY = "INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT";
    const TYPE_STRING = "VARCHAR(120)";
    const TYPE_NUMBER = "INTEGER";
    const TYPE_TEXT = "TEXT";
    const TYPE_BOOLEAN = "BOOLEAN";
    const CODE_ZERO = 0;
    const CODE_TABLE_NOT_EXISTS = 1;
    private $mysql_host = "";
    private $mysql_user = "";
    private $mysql_pass = "";
    private $mysql_database = "";
    public static $handle = null;
    public $lastRowID = -1;
    public $lastArr = array();
    private $charset = null;
    function __construct()
    {
    }
    public function connect($host, $user, $pass, $database)
    {
        if (!function_exists("mysqli_connect")) {
            throw new Exception("php mysql extension doesn't activated, please activate it in php.ini");
        }
        $this->mysql_host = $host;
        $this->mysql_user = $user;
        $this->mysql_pass = $pass;
        $this->mysql_database = $database;
        $this->getCreateDatabase();
    }
    public function setCharset($charset)
    {
        $this->charset = $charset;
    }
    public function multiQueries($query)
    {
        $result = mysqli_multi_query(self::$handle, $query);
        $this->checkForErrors("Multi Query");
        return $result;
    }
    private function throwError($message, $code = null)
    {
        if ($code == null) {
            throw new Exception($message);
        } else {
            throw new Exception($message, $code);
        }
    }
    public function confirmOutput($message = "")
    {
        return array("success" => true, "message" => $message);
    }
    public function validateHandle($functionName = "")
    {
        if (self::$handle == false) {
            if ($functionName) {
                $this->exitWithMessage("{$functionName} error - no open database");
            } else {
                $this->throwError("sqlite error - no open database");
            }
        }
        return $this->confirmOutput();
    }
    public function validateTableName($tableName, $functionName = "")
    {
        if (trim($tableName) == "") {
            if ($functionName) {
                $this->throwError("{$functionName} error - no table found");
            } else {
                $this->throwError("sqlite error - no table found");
            }
        }
    }
    public function validateTable($tableName, $functionName)
    {
        $this->validateTableName($tableName, $functionName);
        if ($this->isTableExists($tableName) == false) {
            if ($functionName) {
                return $this->throwError("{$functionName} error - the table {$tableName} doesn't exists", self::CODE_TABLE_NOT_EXISTS);
            } else {
                $this->throwError("sqlite error - the table {$tableName} doesn't exists", self::CODE_TABLE_NOT_EXISTS);
            }
        }
    }
    public function validateFields($arrFields, $functionName = "")
    {
        if (gettype($arrFields) != "array") {
            $this->throwError("createTable error - the fields array isn't array type.");
        }
        if (count($arrFields) == 0) {
            $this->throwError("createTable error - the fields don't given.");
        }
    }
    public function setDbFile($dbFilepath)
    {
        $this->dbFilepath = $dbFilepath;
        $response = $this->getCreateDatabase();
        return $response;
    }
    public function setAbsolutePath($path)
    {
        $this->databaseAbsolutePath = $path;
        $this->dbFilepath = "";
    }
    public function getLastRowID()
    {
        return $this->lastRowID;
    }
    public function isTableExists($tableName)
    {
        $sql = 'select * from ' . $tableName;
        $numRows = mysqli_num_rows(mysqli_query(self::$handle, "SHOW TABLES LIKE '" . $tableName . "'"));
        $this->checkForErrors("Is table exists error", $sql);
        return $numRows != 0;
    }
    public function getCreateDatabase()
    {
        self::$handle = @mysqli_connect($this->mysql_host, $this->mysql_user, $this->mysql_pass);
        if (!self::$handle) {
            $errorNumber = mysqli_connect_errno();
            if (!empty($errorNumber)) {
                $error = mysqli_connect_error();
                $this->throwError($error);
            }
        }
        mysqli_select_db(self::$handle, $this->mysql_database);
        $this->checkForErrors("Mysql connect to database error");
        if (!empty($this->charset)) {
            mysqli_set_charset(self::$handle, $this->charset);
        }
        return $this->confirmOutput();
    }
    private function checkForErrors($prefix = "", $query = "")
    {
        if (mysqli_error(self::$handle) == false) {
            return false;
        }
        $message = mysqli_error(self::$handle);
        if ($prefix) {
            $message = $prefix . ' - ' . $message;
        }
        if ($query) {
            $message .= ' query: ' . $query;
        }
        $this->throwError($message);
    }
    public function getErrorMsg()
    {
        return mysqli_error(self::$handle);
    }
    public function getErrorNumber()
    {
        return mysql_errno(self::$handle);
    }
    public function query($query, $errorText = "")
    {
        mysqli_query(self::$handle, $query);
        $this->checkForErrors($errorText, $query);
        return mysqli_affected_rows(self::$handle);
    }
    public function getQueryArr($query, $errorText)
    {
        $rs = mysqli_query(self::$handle, $query);
        $this->checkForErrors($errorText, $query);
        $arrRows = array();
        while ($row = mysqli_fetch_assoc($rs)) {
            $arrRows[] = $row;
        }
        $this->lastArr = $arrRows;
        return $arrRows;
    }
    public function getAffectedRows()
    {
        return mysqli_affected_rows(self::$handle);
    }
    public function insertid()
    {
        $this->lastRowID = mysqli_insert_id(self::$handle);
        return $this->lastRowID;
    }
    public function escape($str)
    {
        if (function_exists("mysqli_real_escape_string")) {
            return mysqli_real_escape_string(self::$handle, $str);
        }
        return $str;
    }
}