<?php

/**
 * Driver for MySQL Improved Extension
 *
 * @package OXO
 * @version 1, 26/08/16
 * @since 1
 * @author Filip Golewski <zoltarx@o2.pl>
 *
 */

$CLASSNAME='mysqli_db';
$SUPPORTS=array('mysql');

if (class_exists($CLASSNAME, false)) {
    return;
}

class mysqli_db
{
    var $host='localhost', $user, $password, $name, $prefix;
    var $charset='UTF-8', $persistent;
    var $security="BASE64";
    var $link, $result, $queries, $last, $error;
    
    function mysqli_db($Database=null)
    {
        global $Errors;
        if (!function_exists('mysqli_real_connect')) { $Errors[] = "Extension <b>mysqli</b> is missing"; unset($this); return; }
        if (!is_array($Database)) return;
        $this->user = @$Database['user'];
        $this->password = @$Database['password'];
        $this->name = @$Database['name'];
        $this->prefix = @$Database['prefix'];
        if (isset($Database['host'])) $this->host = $Database['host'];
        if (isset($Database['port'])) $this->host .= ':'.$Database['port'];
        if (isset($Database['persistent'])) $this->persistent=$Database['persistent'];
        if (isset($Database['security'])) $this->security=strtoupper($Database['security']);
        if (isset($Database['charset'])) $this->charset=$Database['charset'];
    }
    
    function connect()
    {
        if (!function_exists('mysqli_real_connect')) return false;
        global $Errors;
        $secret = function_exists('secure_decode') ? secure_decode($this->password, $this->security) : $this->password;
        $this->link = mysqli_init();
        if (!$this->link) {
            $Errors[] = $this->error = "Function <b>mysqli_init</b> failed";
            return false;
        }
        $persistent = $this->persistent ? "p:" : "";
        if (!@mysqli_real_connect($this->link, $persistent . $this->host, $this->user, $secret, $this->name)) {
            $this->error = mysqli_connect_error();
            $Errors[] = $this->error;
            return false;
        }
        $this->set_charset();
        return $this->link;
    }
    
    function escape($value)
    {
        return mysqli_real_escape_string($this->link, $value);
    }
    
    function query($sql)
    {
        if (!$this->link) return false;
        $this->free();
        $this->queries++;
        $this->result = mysqli_query($this->link, $this->last = str_replace('#__', $this->prefix, $sql));
        if ($this->result) {
            $this->error = "";
        } else {
            $this->error = $this->get_error();
            global $Log;
            if (is_object($Log)) {
                $Log->write($this->error . " ... " . $this->last, "error");
            }
        }
        return $this->result;
    }
    
    function scalar($sql)
    {
        if (!$this->query($sql)) {
            return false;
        }
        $array = mysqli_fetch_array($this->result, MYSQLI_NUM);
        $this->free();
        return is_array($array) ? $array[0] : "";
    }
    
    function fetch_row()
    {
        $result = mysqli_fetch_assoc($this->result);
        return $result;
    }
    
    function fetch_all()
    {
        $result = array();
        while ($row = @mysqli_fetch_assoc($this->result)) {
            $result[] = $row;
        }
        $this->free();
        return $result;
    }
    
    function free()
    {
        if ($this->result) {
            if (is_object($this->result)) {
                mysqli_free_result($this->result);
            }
            $this->result = null;
        }
    }

    function table_rows($table)
    {
        $stored = $this->result;
        $result = ($this->query("SHOW COUNT(*) FROM `$table` AS count") and $row = $this->fetchrow()) ? $row['count'] : false;
        $this->free();
        $this->result = $stored;
        return $result;
    }
    
    function num_rows()
    {
        return @mysqli_num_rows($this->result);
    }
    
    function affected_rows()
    {
        return @mysqli_affected_rows($this->result);
    }
    
    function get_error()
    {
        return mysqli_error($this->link);
    }
    
    function close()
    {
        $this->free();
        if ($this->link) {
            mysqli_close($this->link);
        }
    }
    
    function set_charset($charset=null)
    {
        if (is_null($charset)) $charset=$this->charset;
        if (!$charset) return true;
        switch ($charset=strtoupper($charset)) {
            case 'UTF-8':
                $charset='UTF8';
                break;
            case 'ISO-8859-2':
                $charset='LATIN2';
                break;
        }
        return !!mysqli_set_charset($this->link, $charset);
    }

    function __destruct()
    {
        $this->close();
    }

    function fetchrow() { return $this->fetch_row(); }
    function fetchall() { return $this->fetch_all(); }
    function numrows() { return $this->num_rows(); }
    function rows($table) { return $this->table_rows($table); }
    function affectedrows() { return $this->affected_rows(); }
    function setcharset($charset='') { return $this->set_charset($charset); }
    function safe($str) { return $this->escape($str); }
    function fetch() { return $this->fetch_all(); }
    function row() { return $this->fetch_row(); }

}
