<?php
/* vim: set ts=2 sw=2: */
// +----------------------------------------------------------------------+
// | PHP version 4.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997 - 2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | hide@address.com so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Jiri Kocman <hide@address.com> |
// +----------------------------------------------------------------------+
/**
* class mysql_db_class
*
* Trida urcena pro pristup k MySQL databazi. Obsahuje vsechny zname PHP
* funkce pro praci s MySQL + mnoho dalsich uzitecnych funkci, ktere jsou
* bud kopii jinych PHP funkci, nebo jsou vytvoreny z existujicich funkci
* a slouzi k ulehceni programatorske prace. Posledni aktualni verzi tridy
* naleznete na {@link http://www.komplikator.cz/db_class.php homepage tridy}.
*
* @package db_class
* @author Jiri Kocman - hide@address.com
* @version $Id: mysql_db_class.php,v 1.16 2003/02/24 05:09:50 cvsuser Exp $
*/
class db
{
/* ----------------------------------------------------------------------------------------- */
/* ----------------------------------- v a r i a b l e s ----------------------------------- */
/* ----------------------------------------------------------------------------------------- */
/**
* identifikator pripojeni k databazi
* @access private
* @var ResourceID
*/
var $CONN;
/**
* identifikator vysledku sql dotazu
* @access private
* @var ResourceID
*/
var $SQL;
/**
* DB server
* @access private
* @var string
*/
var $SERVER;
/**
* DB uzivatel
* @access private
* @var string
*/
var $USER;
/**
* DB heslo
* @access private
* @var string
*/
var $PASS;
/**
* DB jmeno
* @access private
* @var string
*/
var $DB;
/**
* vytvaret nove spojeni
* @access private
* @var bool
*/
var $NEW;
/**
* logovat chyby
* @access private
* @var bool
*/
var $LOG_ERROR;
/**
* logovat dotazy
* @access private
* @var bool
*/
var $ULOG;
/**
* debug mod
* @access private
* @var bool
*/
var $DEBUG;
/**
* zobrazovat chyby
* @access private
* @var bool
*/
var $SHOW_ERROR;
/**
* vypisovat userlog
* @access private
* @var bool
*/
var $SHOW_MESSAGE;
/**
* zapisovat userlog
* @access private
* @var bool
*/
var $LOG_MESSAGE;
/**
* pocet desetinnych mist pro cas v userlogu
* @access private
* @var integer
*/
var $PRECISION;
/**
* nazev errorlogu
* @access private
* @var string
*/
var $ERRORLOG;
/**
* nazev userlogu
* @access private
* @var string
*/
var $USERLOG;
/**
* dir pro userlog
* @access private
* @var string
*/
var $ULOGDIR;
/**
* dir pro errorlog
* @access private
* @var string
*/
var $LOGDIR;
/**
* ciselna varianta verze PHP
* @access private
* @var int
*/
var $PHPVersion;
/**
* pole se strankovanim
* @access public
* @var array
*/
var $PAGING;
/**
* cislo dle ktereho se bude radit - 1 = DESC, jinak ASC
* @access public
* @var string $ORD
*/
var $ORD;
/**
* v promenne bude vytvoren SQL dump
* @access public
* @var string $OUTPUT
*/
var $OUTPUT;
/* ----------------------------------------------------------------------------------------- */
/* ---------------------------- p r i v a t e m e t h o d s ------------------------------ */
/* ----------------------------------------------------------------------------------------- */
/**
* Interni funkce zapisuje chybovou hláku do error logu
*
* @access private
* @param string $_errorNo cislo SQL chyby
* @param string $_errorStr popis SQL chyby
* @param string $_query SQL dotaz
* @return false
*/
function _error($_errorNo, $_errorStr, $_query = null)
{
if ($this->LOG_ERROR == 1 && $this->ERRORLOG != null)
{
$mess .= "\r\nDolo k chybě dne " . date('d.m.Y v H:i:s') . "\r\n" . $_errorNo . ': ' . $_errorStr . "\r\n";
if ($_query != null)
$mess .= "Dotaz:\r\n" . $_query . "\r\n\r\n";
else
$mess .= "\r\n";
$f = fopen($this->LOGDIR . '/' . $this->ERRORLOG, 'a');
@fwrite($f, $mess);
@fclose($f);
}
if ($this->SHOW_ERROR == 1)
{
echo '<hr><b><font color=red><pre>Dolo k chybě dne ' . date('d.m.Y v H:i:s') . '</font><br>' . $_errorNo . ': ' . $_errorStr . '<br>';
if ($_query != null)
echo 'Dotaz: <br>' . str_replace("\t", ' ', $_query) . '</pre></b><hr><br>';
else
echo '</pre><br>';
echo '</b><hr>';
}
return false;
}
/**
* Interni funkce vytvari pole pro strankovani
*
* Navratove pole je dvourozmerne. Pole bude mit vzdy minimalne 5 indexu (pocet zavisi na poctu
* zobrazovanych odkazu).
* Index 1: obsahuje pole s peti indexy. 1. kus SQL dotazy s LIMIT, 2. pocatecni radek, 3. pocet radku na strane
* Ctvrty obsahuje celkovy pocet zaznamu paty pak lokalizovanou (ceskou) koncovku, pro slovo zaznam. Paty a sesty
* index pole znamena cisla zobrazenych polozek, respektive cislo prvni zobrazene a cislo posledne zobrazene polozky
* jinak se to da rict slovy od - do.
* Zbyle indexy pole obsahuji stejne pararametry - prvni index obsahuje text odkazu, druhy pak cast, kterou je nutno
* pridat do odkazu. Pokud je druhy index prazdny, pak v pripade sipkovych odakzu, neni odkaz aktivni. U jednotlivych
* odkazu s cisly pak prazdna polozka znamena aktualni stranku.
* Index 2: skok o x stran zpet
* Index 3: skok o jednu stranu zpet
* Index 4: skok o jednu stranu dale
* Index 5: skok o x stran dopredu
* Index 6 - x: jednotlive ciselne odkazy
*
* @access private
* @param int $PAGENUMBER cislo aktualni stranky
* @param int $ROWNUM celkovy pocet zaznamu
* @param int $PERPAGE pocet zaznamu na strance
* @param int $PERPAGELINKS pocet strankovacich odkazu
* @param int $JUMP o kolik stranek se posova pri kliknuti na dvojitou sipku
* @param string $VARNAME obsahuje nazev promenne pouzite v odkazech
* @param string $SEPARATOR obsahuje cast url pred PGN=cislo - muze to byt cokoli, takze i cela url :-)
* @return void
*/
function _make_paging(&$PAGENUMBER, &$ROWNUM, &$PERPAGE, &$PERPAGELINKS, &$JUMP, &$VARNAME, &$SEPARATOR)
{
$START = $PERPAGE *($PAGENUMBER - 1);
$OUTPUT = array();
$OUTPUT[] = array(' LIMIT ' . $START . ', ' . $PERPAGE, $START, $PERPAGE);
$TOTALPAGE = ceil($ROWNUM / $PERPAGE);
if ($PAGENUMBER > $TOTALPAGE) $PAGENUMBER = $TOTALPAGE;
if ($PAGENUMBER < 1) $PAGENUMBER=1;
$FLOOR = (floor(($PAGENUMBER - 1) / $JUMP) * $JUMP) + 1;
$PERPAGELINKS = ($FLOOR + $PERPAGELINKS);
if ($PERPAGELINKS > $TOTALPAGE) $PERPAGELINKS = $TOTALPAGE + 1;
if ($PAGENUMBER > $JUMP)
{
$OUTPUT[] = array('<<', $SEPARATOR . $VARNAME . '=' . ($FLOOR - $JUMP));
}
else
{
$OUTPUT[] = array('<<', '');
}
if ($PAGENUMBER > 1)
{
$OUTPUT[] = array('<', $SEPARATOR . $VARNAME . '=' . ($PAGENUMBER - 1));
}
else
{
$OUTPUT[] = array('<', '');
}
if ($PAGENUMBER < $TOTALPAGE)
{
$OUTPUT[] = array('>', $SEPARATOR . $VARNAME . '=' . ($PAGENUMBER + 1));
}
else
{
$OUTPUT[] = array('>', '');
}
if (($FLOOR + $JUMP) <= $TOTALPAGE)
{
$OUTPUT[] = array('>>', $SEPARATOR . $VARNAME . '=' . ($FLOOR + $JUMP));
}
else
{
$OUTPUT[] = array('>>', '');
}
for ($i = $FLOOR; $i < $PERPAGELINKS; $i++)
{
if ($i == $PAGENUMBER) $OUTPUT[] = array($i, '');
else $OUTPUT[] = array($i, $SEPARATOR . $VARNAME . '=' . $i);
}
/* pouze ceska lokalizace, casem to predelam na moznost to nejak rozumne menit */
if ($ROWNUM > 1 && $ROWNUM < 5) $OUTPUT[0][4] = 'y';
elseif ($ROWNUM == 1) $OUTPUT[0][4] = '';
else $OUTPUT[0][4] = 'ů';
if ($PERPAGE + $START > $ROWNUM) $OUTPUT[0][6] = $ROWNUM;
else $OUTPUT[0][6] = $START + $PERPAGE;
$OUTPUT[0][3] = $ROWNUM;
$OUTPUT[0][5] = $START + 1;
$this->PAGING = &$OUTPUT;
return true;
}
/**
* Interni funkce strada output dumpu
*
* @access private
* @param string $s SQL dotaz
* @return false
*/
function _out($s)
{
$this->OUTPUT .= $s;
return false;
}
/**
* Interni funkce zapisuje hláku do user logu
*
* @access private
* @param string $_query SQL dotaz
* @param string $_time cas provadeni dotazu
* @return false
*/
function _writeUserLog($_query, $_time)
{
$_time = round($_time, $this->PRECISION);
if ($this->LOG_MESSAGE == 1 && $this->USERLOG != null)
{
$mess = 'Dotaz z ' . date('d.m.Y v H:i:s') . ' trval: ' . $_time . "s\r\n" . $_query . "\r\n\r\n";
$f = fopen($this->ULOGDIR . '/' . $this->USERLOG, 'a');
@fwrite($f, $mess);
@fclose($f);
}
if ($this->SHOW_MESSAGE == 1)
{
$mess = 'Dotaz z ' . date('d.m.Y v H:i:s') . ' trval: ' . $_time . "s\r\n" . $_query . "\r\n\r\n";
echo '<hr><b><pre>Dotaz z ' . date('d.m.Y v H:i:s') . ' trval: ' . $_time . 's<br>';
if ($_query != null)
echo '<font color=blue>' . str_replace("\t", ' ', $_query) . '</font></pre></b><hr><br>';
else
echo '</b></pre><br>';
}
return false;
}
/* ----------------------------------------------------------------------------------------- */
/* ---------------------- p u b l i c d a t a b a s e m e t h o d s -------------------- */
/* ----------------------------------------------------------------------------------------- */
/**
* Konstruktor tridy
*
* Konstruktor tridy vytvori pripojeni k databazi a pripadne zvoli zadanou databazi
*
* @access public
* @param string $server databazovy server
* @param string $user databazovy user
* @param string $pass databazove heslo
* @param bool $new vytvaret nove spojeni (pouze v PHP 4.2+)
* @param string $db nazev databaze
* @return class link
*/
function db($server = '', $user = '', $pass = '', $new = false, $db = '')
{
if ($server)
{
$this->db_Connect($server, $user, $pass, $new);
if ($db)
{
$this->db_Select_Db($db);
}
}
return $this->CONN;
}
/**
* Ekvivalent mysql_affected_rows()
* @access public
* @param resource $conn identifikator pripojeni k databazi
* @return integer
*/
function db_Affected_Rows($conn = null)
{
if ($conn != null) $this->CONN = $conn;
$tmp = @mysql_affected_rows($this->CONN);
if ($tmp) return $tmp;
else return 0;
}
/**
* Ekvivalent mysql_close()
* @access public
* @param resource $conn identifikator databazoveho spojeni
* @return void
*/
function db_Close($conn = null)
{
if ($conn != null) $this->CONN = $conn;
$tmp = @mysql_close($this->CONN);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_connect()
* @access public
* @param string $server SQL server
* @param string $user SQL user
* @param string $pass SQL heslo
* @param bool $new vytvaret novy link
* @return ResourceID
*/
function &db_Connect($server = null, $user = null, $pass = null, $new = false)
{
if ($server != null) $this->SERVER = $server;
if ($user != null) $this->USER = $user;
if ($pass != null) $this->PASS = $pass;
if ($new != null) $this->NEW = $new;
if (!$this->PHPVersion) $this->PHPVersion = $this->getPHPVersion();
if ($this->NEW && $this->PHPVersion >= 40200)
$this->CONN = @mysql_connect($this->SERVER, $this->USER, $this->PASS, $this->NEW);
else
$this->CONN = @mysql_connect($this->SERVER, $this->USER, $this->PASS);
if ($this->CONN)
{
return $this->CONN;
}
else
{
if ($this->DEBUG == 1) $this->_error(@mysql_errno(), @mysql_error());
return false;
}
}
/**
* Ekvivalent mysql_data_seek()
* @access public
* @param integer $row cislo radku
* @param resource $sql identifikator SQL dotazu
* @return bool
*/
function db_Data_Seek($row = 0, $sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_data_seek($this->SQL,$row);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_create_db()
* @access public
* @param string $database nazev databaze
* @param resource $conn identifikator pripojeni k databazi
* @return string
*/
function db_Create_Db($database,$conn = null)
{
if ($conn != null) $this->CONN = $conn;
$tmp = @mysql_create_db($database, $this->CONN);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_db_query()
* @access public
* @param string $query SQL dotaz
* @param string $db nazev databaze
* @param resource $conn identifikator pripojeni
* @return resource
*/
function &db_Db_Query($query, $db = null, $conn = null)
{
if ($db != null) $this->DB = $db;
if ($conn != null) $this->CONN = $conn;
if ($this->ULOG == 1)
{
$_timeStart = microtime();
$this->SQL = @mysql_db_query($this->DB, $query, $this->CONN);
$_timeStop = microtime();
$_tmpStart = explode(" ", $_timeStart);
$_tmpStop = explode(" ", $_timeStop);
$_tmpSec = $_tmpStop[1] - $_tmpStart[1];
if ($_tmpSec == 0)
$_tmpMS = $_tmpStop[0] - $_tmpStart[0];
else
$_tmpMS = 1 - $tmpStart[0] + $tmpStop[0];
$_time = $_tmpSec + $_tmpMS;
$this->_writeUserLog($query, $_time);
}
else
{
$this->SQL = @mysql_db_query($this->DB, $query, $this->CONN);
}
if ($this->SQL)
{
return $this->SQL;
}
else
{
if ($this->DEBUG == 1) $this->_error(@mysql_errno(), @mysql_error(), &$query);
return false;
}
}
/**
* Ekvivalent mysql_dbname()
* @access public
* @param integer $row poradi ve vysledku
* @param resource $sql identifikator SQL dotazu
* @return string
*/
function db_Dbname($row, $sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_dbname($this->SQL, $row);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_drop_db()
* @access public
* @param string $database nazev databaze
* @param resource $conn identifikator databazoveho spojeni
* @return bool
*/
function db_Drop_Db($database, $conn = null)
{
if ($conn != null) $this->CONN = $conn;
$tmp = @mysql_drop_db($database, $this->CONN);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_escape_string()
* @access public
* @param string $tmp retezec ktery bude opatren escape sekvencemi
* @return string
*/
function db_Escape_String($tmp)
{
$tmp = @mysql_escape_string($tmp);
return $tmp;
}
/**
* Obdoba db_fetch_into().
*
* Obdoba db_fetch_into(). Funkce vlozi do pole vsechny hodnoty z dotazu.
*
* @access public
* @param array $pole jmeno pole do ktereho se naliji data
* @param resource $sql identifikator SQL dotazu
* @param integer $count pocet radku ktere se maji do pole ulozit
* @param integer $start cislo radku od ktereho se zacnou vkladat data
* @return integer
*/
function db_Fetch_All_Into(&$pole, $sql = null, $count = 0, $start = 0)
{
$pole = array(); // reset pole
if ($sql != null) $this->SQL = $sql;
$radku = $this->db_num_rows();
$this->db_Data_Seek($start);
if ($count == 0 || ($start + $count) > $radku)
{
while($row = $this->db_fetch_assoc())
{
$pole[] = $row;
}
}
else
{
for ($i = $start; $i < $start + $count; $i++)
{
$pole[] = $this->db_Fetch_Assoc();
}
}
return $radku;
}
/**
* Ekvivalent mysql_fetch_array()
* @access public
* @param resource $sql identifikator SQL dotazu
* @return array
*/
function db_Fetch_Array($sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_fetch_array($this->SQL);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_fetch_assoc()
* @access public
* @param resource $sql identifikator SQL dotazu
* @return array
*/
function db_Fetch_Assoc($sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_fetch_assoc($this->SQL);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_fetch_field()
* @access public
* @param integer $item cislo polozky vysledku
* @param resource $sql identifikator SQL dotazu
* @return object
*/
function db_Fetch_Field($item, $sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_fetch_field($this->SQL, $item);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent odbc_fetch_into()
*
* Ekvivalent odbc_fetch_into(). Funkce vlozi do pole vsechny
* hodnoty z definovaneho sloupce.
*
* @access public
* @param mixed $col nazev nebo poradi sloupce ve vysledku
* @param array $pole jmeno pole do ktereho se naliji data
* @param resource $sql identifikator SQL dotazu
* @return integer
*/
function db_Fetch_Into($col, &$pole, $sql = null)
{
$pole = array(); // reset pole
if ($sql != null) $this->SQL = $sql;
$radku = $this->db_num_rows();
for($i = 0; $i < $radku; $i++)
{
$pole[$i] = $this->db_result($i, $col);
}
return $radku;
}
/**
* Ekvivalent mysql_fetch_lengths()
* @access public
* @param resource $sql identifikator SQL dotazu
* @return integer
*/
function db_Fetch_Lengths($sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_fetch_lengths($this->SQL);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_fetch_object()
* @access public
* @param resource $sql identifikator SQL dotazu
* @return object
*/
function db_Fetch_Object($sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_fetch_object($this->SQL);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_fetch_row()
* @access public
* @param resource $sql identifikator SQL dotazu
* @return array
*/
function db_Fetch_Row($sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_fetch_row($this->SQL);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_field_flags()
* @access public
* @param integer $item cislo sloupce
* @param resource $sql identifikator SQL dotazu
* @return string
*/
function db_Field_Flags($item, $sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_field_flags($this->SQL, $item);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_field_len()
* @access public
* @param integer $item cislo sloupce
* @param resource $sql identifikator SQL dotazu
* @return string
*/
function db_Field_Len($item, $sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_field_len($this->SQL, $item);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_field_name()
* @access public
* @param integer $col cislo sloupce
* @param resource $sql identifikator SQL dotazu
* @return string
*/
function db_Field_Name($col = 0, $sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_field_name($this->SQL, $col);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_field_seek()
* @access public
* @param integer $item cislo sloupce
* @param resource $sql identifikator SQL dotazu
* @return bool
*/
function db_Field_Seek($item, $sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_field_seek($this->SQL, $item);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_field_table()
* @access public
* @param integer $item cislo sloupce
* @param resource $sql identifikator SQL dotazu
* @return string
*/
function db_Field_Table($item, $sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_field_table($this->SQL, $item);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_field_type()
* @access public
* @param integer $item cislo sloupce
* @param resource $sql identifikator SQL dotazu
* @return string
*/
function db_Field_Type($item, $sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_field_type($this->SQL, $item);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_free_result()
* @access public
* @param resource $sql identifikator SQL dotazu
* @return bool
*/
function db_Free_Result($sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_free_result($this->SQL);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_get_client_info()
* @access public
* @param void
* @return string
*/
function db_Get_Client_Info()
{
$tmp = @mysql_get_client_info();
return $tmp;
}
/**
* Ekvivalent mysql_get_host_info()
* @access public
* @param resource $conn identifikator databazoveho spojeni
* @return string
*/
function db_Get_Host_Info($conn = null)
{
if ($conn != null) $this->CONN = $conn;
$tmp = @mysql_get_host_info($this->CONN);
return $tmp;
}
/**
* Ekvivalent mysql_get_proto_info()
* @access public
* @param resource $conn identifikator databazoveho spojeni
* @return string
*/
function db_Get_Proto_Info($conn = null)
{
if ($conn != null) $this->CONN = $conn;
$tmp = @mysql_get_proto_info($this->CONN);
return $tmp;
}
/**
* Ekvivalent mysql_get_server_info()
* @access public
* @param resource $conn identifikator databazoveho spojeni
* @return string
*/
function db_Get_Server_Info($conn = null)
{
if ($conn != null) $this->CONN = $conn;
$tmp = @mysql_get_server_info($this->CONN);
return $tmp;
}
/**
* Ekvivalent mysql_insert_id()
* @access public
* @param void
* @return integer
*/
function db_Insert_Id()
{
$tmp = @mysql_insert_id();
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_num_fields()
* @access public
* @param resource $sql identifikator SQL dotazu
* @return integer
*/
function db_Num_Fields($sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_num_fields($this->SQL);
if ($tmp) return $tmp;
else return 0;
}
/**
* Ekvivalent mysql_list_dbs()
* @access public
* @param resource $conn identifikator databazoveho spojeni
* @return string
*/
function db_List_Dbs($conn = null)
{
if ($conn != null) $this->CONN = $conn;
$tmp = @mysql_list_dbs($this->CONN);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_list_fields()
* @access public
* @param string $table nazev tabulky
* @param string $db nazev databaze
* @param resource $conn identifikator pripojeni k databazi
* @return resource
*/
function db_List_Fields($table, $db = null, $conn = null)
{
if ($conn != null) $this->CONN = $conn;
if ($db != null) $this->DB = $db;
$tmp = @mysql_list_fields($this->DB, $table, $this->CONN);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_list_tables()
* @access public
* @param $db string nazev databaze
* @param resource $conn identifikator databazoveho spojeni
* @return bool
*/
function db_List_Tables($db = null, $conn = null)
{
if ($conn != null) $this->CONN = $conn;
if ($db != null) $this->DB = $db;
$tmp = @mysql_list_tables($this->DB, $this->CONN);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_num_rows()
* @access public
* @param resource $sql identifikator SQL dotazu
* @return integer
*/
function db_Num_Rows($sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_num_rows($this->SQL);
if ($tmp) return $tmp;
else return 0;
}
/**
* Ekvivalent mysql_pconnect()
* @access public
* @param string $server SQL server
* @param string $user SQL user
* @param string $pass SQL heslo
* @return ResourceID
*/
function &db_pConnect($server = null, $user = null, $pass = null)
{
if ($server != null) $this->SERVER = $server;
if ($user != null) $this->USER = $user;
if ($pass != null) $this->PASS = $pass;
$this->CONN = @mysql_pconnect($this->SERVER, $this->USER, $this->PASS);
if ($this->CONN)
{
return $this->CONN;
}
else
{
if ($this->DEBUG == 1) $this->_error(@mysql_errno(), @mysql_error());
return false;
}
}
/**
* Ekvivalent mysql_query()
* @access public
* @param string $query SQL dotaz
* @param resource $conn identifikator pripojeni
* @return resource
*/
function &db_Query($query, $conn = null)
{
if ($conn != null) $this->CONN = $conn;
if ($this->ULOG == 1)
{
$_timeStart = microtime();
$this->SQL = @mysql_query($query, $this->CONN);
$_timeStop = microtime();
$_tmpStart = explode(' ', $_timeStart);
$_tmpStop = explode(' ', $_timeStop);
$_tmpSec = $_tmpStop[1] - $_tmpStart[1];
if ($_tmpSec == 0)
$_tmpMS = $_tmpStop[0] - $_tmpStart[0];
else
$_tmpMS = 1 - $tmpStart[0] + $tmpStop[0];
$_time = $_tmpSec + $_tmpMS;
$this->_writeUserLog($query, $_time);
}
else
{
$this->SQL = @mysql_query($query, $this->CONN);
}
if ($this->SQL)
{
return $this->SQL;
}
else
{
if ($this->DEBUG == 1) $this->_error(@mysql_errno(), @mysql_error(), &$query);
return false;
}
}
/**
* Ekvivalent mysql_result()
* @access public
* @param integer $row cislo radku
* @param mixed $col cislo nebo nazev sloupce
* @param resource $sql identifikator SQL dotazu
* @return string
*/
function db_Result($row, $col, $sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_result($this->SQL, $row, $col);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_select_db()
* @access public
* @param string $db nazev databaze
* @param resource $conn identifikator pripojeni
* @return bool
*/
function &db_Select_DB($db = null, $conn = null)
{
if ($db != null) $this->DB = $db;
if ($conn != null) $this->CONN = $conn;
$tmp = @mysql_select_db($this->DB,$this->CONN);
if ($tmp)
{
return true;
}
else
{
if ($this->DEBUG == 1) $this->_error(@mysql_errno(), @mysql_error());
return false;
}
}
/**
* Ekvivalent mysql_tablename()
* @access public
* @param integer $row cislo radku
* @param resource $sql identifikator SQL dotazu
* @return string
*/
function db_Tablename($row, $sql = null)
{
if ($sql != null) $this->SQL = $sql;
$tmp = @mysql_tablename($this->SQL, $row);
if ($tmp) return $tmp;
else return false;
}
/**
* Ekvivalent mysql_unbuffered_query()
* @access public
* @param string $query SQL dotaz
* @param resource $conn identifikator pripojeni
* @return resource
*/
function &db_Unbuffered_Query($query, $conn = null)
{
if ($conn != null) $this->CONN = $conn;
if ($this->ULOG == 1)
{
$_timeStart = microtime();
$this->SQL = @mysql_unbuffered_query($query, $this->CONN);
$_timeStop = microtime();
$_tmpStart = explode(' ', $_timeStart);
$_tmpStop = explode(' ', $_timeStop);
$_tmpSec = $_tmpStop[1] - $_tmpStart[1];
if ($_tmpSec == 0)
$_tmpMS = $_tmpStop[0] - $_tmpStart[0];
else
$_tmpMS = 1 - $tmpStart[0] + $tmpStop[0];
$_time = $_tmpSec + $_tmpMS;
$this->_writeUserLog($query, $_time);
}
else
{
$this->SQL = @mysql_unbuffered_query($query, $this->CONN);
}
if ($this->SQL)
{
return $this->SQL;
}
else
{
if ($this->DEBUG == 1) $this->_error(@mysql_errno(), @mysql_error(), &$query);
return false;
}
}
/* ----------------------------------------------------------------------------------------- */
/* -------------------------- o t h e r p u b l i c m e t h o d s ---------------------- */
/* ----------------------------------------------------------------------------------------- */
/**
* Zapinani / vypinani debug modu
*
* Zapinani / vypinani debug modu. Zapne, vypne a nastavi moznost logovani a zobrazovani chyb.
*
* @access public
* @param bool $DEBUG 1 = zapnuty debug mod, 0 = vypnuty
* @param bool $SHOW_ERROR 1 = zobrazuhe chyby na strance
* @param bool $LOG_ERROR 1 = loguje chyby do souboru
* @param string $ERRORLOG cesta k errorlogu. Neni li zadana, pouzije se defaultni. Vhodne uvadet absolutni cestu.
* @return void
*/
function debug($DEBUG = 1, $SHOW_ERROR = 1, $LOG_ERROR = 0, $ERRORLOG = null)
{
if ($DEBUG == 1)
{
$this->DEBUG = 1;
if ($ERRORLOG != null)
{
$this->ERRORLOG = basename($ERRORLOG);
$this->LOGDIR = dirname($ERRORLOG);
}
else
{
$this->ERRORLOG = 'mysql_db_class_error.log';
$this->LOGDIR = dirname($GLOBALS['SCRIPT_FILENAME']) . '/logs';
}
}
else
{
$this->DEBUG = 0;
}
$this->SHOW_ERROR = $SHOW_ERROR;
$this->LOG_ERROR = $LOG_ERROR;
}
/**
* Funkce provede EXPLAIN SQL dotazu a zobrazi jej v HTML tabulce
* @access public
* @param string $query SQL dotaz
* @return void
*/
function explain($query)
{
$tmp = $this->SQL;
echo $this->show_data($this->db_query('EXPLAIN ' . $query));
$this->SQL = $tmp;
return false;
}
/**
* Vraci ciselne vyjadreni verze PHP
* @access public
* @return double
*/
function getPHPVersion()
{
$phpver = phpversion();
if (!ereg('([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})', $phpver, $match))
{
$result = ereg('([0-9]{1,2}).([0-9]{1,2})', $phpver, $match);
}
if (isset($match) && !empty($match[1]))
{
if (!isset($match[2]))
{
$match[2] = 0;
}
if (!isset($match[3]))
{
$match[3] = 0;
}
$phpver = (int)sprintf('%d%02d%02d', $match[1], $match[2], $match[3]);
unset($match);
}
else
{
$phpver = 0;
}
return $phpver;
}
/**
* Vytvori dump dane databaze
*
* db_dump using phpMyDump v 1.0
* check for new version
* http://szewo.com/php/mydump/eng
* some functions are adapted from the phpMyAdmin
*
* @access public
* @param string $dbname nazev databaze
* @param bool $structure_only FALSE = struktura i data, TRUE = pouze struktura
* @param string $crlf oddelovac radku
* @return string
*/
function db_dump($dbname, $structure_only = FALSE, $crlf = "\r\n")
{
$this->OUTPUT = '';
$con = $this->CONN;
// here we check MySQL Version
$result=@mysql_query("SELECT VERSION() AS version");
if ($result != FALSE && @mysql_num_rows($result) > 0)
{
$row = @mysql_fetch_array($result);
$match = explode('.', $row['version']);
}
else
{
$result=@mysql_query("SHOW VARIABLES LIKE \'version\'");
if ($result != FALSE && @mysql_num_rows($result) > 0)
{
$row = @mysql_fetch_row($result);
$match = explode('.', $row[1]);
}
}
if (!isset($match) || !isset($match[0]))
{
$match[0] = 3;
}
if (!isset($match[1]))
{
$match[1] = 21;
}
if (!isset($match[2]))
{
$match[2] = 0;
}
if(!isset($row))
{
$row = '3.21.0';
}
define('MYSQL_INT_VERSION', (int)sprintf('%d%02d%02d', $match[0], $match[1], intval($match[2])));
define('MYSQL_STR_VERSION', $row['version']);
unset($match);
$sql = "# MySQL dump by phpMyDump".$crlf;
$sql.= "# Host: $this->SERVER Database: $dbname".$crlf;
$sql.= "----------------------------".$crlf;
$sql.= "# Server version: ".MYSQL_STR_VERSION.$crlf;
$sql.= $crlf.$crlf.$crlf;
$this->_out($sql);
$res=@mysql_list_tables($dbname);
$nt=@mysql_num_rows($res);
for ($a=0;$a<$nt;$a++)
{
$row=mysql_fetch_row($res);
$tablename=$row[0];
$sql=$crlf."# ----------------------------------------".$crlf."# table structure for table '$tablename' ".$crlf;
// For MySQL < 3.23.20
if (MYSQL_INT_VERSION >= 32321)
{
$result=mysql_query("SHOW CREATE TABLE $tablename");
if ($result != FALSE && mysql_num_rows($result) > 0)
{
$tmpres = mysql_fetch_array($result);
$pos = strpos($tmpres[1], ' (');
$tmpres[1] = substr($tmpres[1], 0, 13) . $tmpres[0] . substr($tmpres[1], $pos);
$sql .= $tmpres[1].";".$crlf.$crlf;
}
mysql_free_result($result);
}
else
{
$sql.="CREATE TABLE $tablename(".$crlf;
$result=mysql_query("show fields from $tablename",$con);
while ($row = mysql_fetch_array($result))
{
$sql .= " ".$row['Field'];
$sql .= ' ' . $row['Type'];
if (isset($row['Default']) && $row['Default'] != '')
{
$sql .= ' DEFAULT \'' . $row['Default'] . '\'';
}
if ($row['Null'] != 'YES')
{
$sql .= ' NOT NULL';
}
if ($row['Extra'] != '')
{
$sql .= ' ' . $row['Extra'];
}
$sql .= ",".$crlf;
}
mysql_free_result($result);
$sql = ereg_replace(',' . $crlf . '$', '', $sql);
$result = mysql_query("SHOW KEYS FROM $tablename");
while ($row = mysql_fetch_array($result))
{
$ISkeyname = $row['Key_name'];
$IScomment = (isset($row['Comment'])) ? $row['Comment'] : '';
$ISsub_part = (isset($row['Sub_part'])) ? $row['Sub_part'] : '';
if ($ISkeyname != 'PRIMARY' && $row['Non_unique'] == 0)
{
$ISkeyname = "UNIQUE|$kname";
}
if ($IScomment == 'FULLTEXT')
{
$ISkeyname = 'FULLTEXT|$kname';
}
if (!isset($index[$ISkeyname]))
{
$index[$ISkeyname] = array();
}
if ($ISsub_part > 1)
{
$index[$ISkeyname][] = $row['Column_name'] . '(' . $ISsub_part . ')';
}
else
{
$index[$ISkeyname][] = $row['Column_name'];
}
}
mysql_free_result($result);
while (list($x, $columns) = @each($index))
{
$sql .= ",".$crlf;
if ($x == 'PRIMARY')
{
$sql .= ' PRIMARY KEY (';
}
else if (substr($x, 0, 6) == 'UNIQUE')
{
$sql .= ' UNIQUE ' . substr($x, 7) . ' (';
}
else if (substr($x, 0, 8) == 'FULLTEXT')
{
$sql .= ' FULLTEXT ' . substr($x, 9) . ' (';
}
else
{
$sql .= ' KEY ' . $x . ' (';
}
$sql .= implode($columns, ', ') . ')';
}
$sql .= $crlf.");".$crlf.$crlf;
}
$this->_out($sql);
if ($structure_only == FALSE)
{
// here we get table content
$result = mysql_query("SELECT * FROM $tablename");
$fields_cnt = mysql_num_fields($result);
while ($row = mysql_fetch_row($result))
{
$table_list = '(';
for ($j = 0; $j < $fields_cnt; $j++)
{
$table_list .= mysql_field_name($result, $j) . ', ';
}
$table_list = substr($table_list, 0, -2);
$table_list .= ')';
$sql = 'INSERT INTO ' . $tablename . ' VALUES (';
for ($j = 0; $j < $fields_cnt; $j++)
{
if (!isset($row[$j]))
{
$sql .= ' NULL, ';
}
else if ($row[$j] == '0' || $row[$j] != '')
{
$type = mysql_field_type($result, $j);
// a number
if ($type == 'tinyint' || $type == 'smallint' || $type == 'mediumint' || $type == 'int' || $type == 'bigint' ||$type == 'timestamp')
{
$sql .= $row[$j] . ', ';
}
// a string
else
{
$dummy = '';
$srcstr = $row[$j];
for ($xx = 0; $xx < strlen($srcstr); $xx++)
{
$yy = strlen($dummy);
if ($srcstr[$xx] == '\\') $dummy .= '\\\\';
if ($srcstr[$xx] == '\'') $dummy .= '\\\'';
if ($srcstr[$xx] == "\x00") $dummy .= '\0';
if ($srcstr[$xx] == "\x0a") $dummy .= '\n';
if ($srcstr[$xx] == "\x0d") $dummy .= '\r';
if ($srcstr[$xx] == "\x1a") $dummy .= '\Z';
if (strlen($dummy) == $yy) $dummy .= $srcstr[$xx];
}
$sql .= "'" . $dummy . "', ";
}
}
else
{
$sql .= "'', ";
} // end if
} // end for
$sql = ereg_replace(', $', '', $sql);
$sql .= ");".$crlf;
$this->_out($sql);
}
mysql_free_result($result);
}
}
return $this->OUTPUT;
}
/**
* Vytvori pole pro paging - strankovani
*
* Vytvori pole pro paging - strankovani. Z tohoto pole pak lze zobrazit ruzne druhy
* strankovani. Fukce se chova tremi odlisnymi zpusoby dle zadaneho argumentu type.
* Vice v prikladu
*
*
* Pole args obsahuje nekolik parametru. Priklad definice:
*
* $args = array(
* PAGENUMBER => ($_REQUEST[PGN]) ? $_REQUEST[PGN] : 1, // aktivni stranka
* PERPAGE => 10, // zaznamu na strance
* PERPAGELINKS => 10, // strankovacich odkazu
* JUMP => 10, //
* VARNAME => 'PGN', // nazev promenne s cislem aktivni stranky
* );
*
* $type = COUNT_QUERY
* v tomto pripade je parametr query SQL dotaz. napr. SELECT COUNT(*) FROM user
*
* $type = FULL_QUERY
* v tomto pripade je parametr query SQL dotaz. napr. SELECT * FROM user
*
* $type = INTEGER
* v tomto pripade je parametr query cislo s poctem zaznamu
*
* Funkce vraci string obsahujici LIMIT klauzuli pouzitelnou do SQL dotazu.
* Pole s definici strankovacich odkazu je mimo tridu dostupne pres $db->PAGING.
*
* Priklad pouziti (funkce show_paging je v adresari stuff)
*
* $db->db_Query('SELECT * FROM user' . $db->paging(&$args,COUNT_QUERY,'SELECT COUNT(*) FROM user'));
* echo $db->show_data();
* show_paging(&$db);
*
* @access public
* @param array $args pole s parametry strankovani
* @param string $type typ zpracovani
* @param mixed $query bud SQL dotaz nebo cislo s poctem zaznamu
* @param resource $conn link na pripojeni k databazi
* @return string
*/
function Paging(&$args, $type, $query, $conn = null)
{
if ($conn != null) $this->CONN = $conn;
switch ($type)
{
case 'COUNT_QUERY':
$this->SQL = $this->db_Query(&$query);
$ROWNUM = $this->db_Result(0,0);
break;
case 'FULL_QUERY':
$this->SQL = $this->db_Query(&$query);
$ROWNUM = $this->db_Num_Rows();
break;
case 'INTEGER':
$ROWNUM = &$query;
break;
default:
}
if (!isset($args[SEPARATOR])) $args[SEPARATOR] = '';
$this->_make_paging($args[PAGENUMBER], $ROWNUM, $args[PERPAGE], $args[PERPAGELINKS], $args[JUMP], $args[VARNAME], $args[SEPARATOR]);
return $this->PAGING[0][0];
}
/**
* Funkce pripravi data ziskane SQL dotazem. Data lze nafirmatovat bud do jednoduche HTML tabulky
* nebo do CSV formatu. Pripadne lze definovat vlastni separator a CRLF - prechod na dalsi radek.
* Pripravena data budou v navratove hodnote.
* @access public
* @param ResourceID $result identifikator SQL vysledku
* @param integer $from radek od ktereho zacne zpracovani dat
* @param integer $to radek na kterem konci zobrazeni dat. Je-li rovny 0 budou se data zpracovavat az po posledni radek vysledku
* @param bool $csv FALSE pro HTML, TRUE pro CSV
* @param string $separator separator sloupcu ve vyslednem CSV
* @param string $crlf oddelovac radku
* @return string
*/
function show_data($result = null, $from = 0, $to = 0, $csv = FALSE, $separator = ';', $crlf = "\r\n")
{
if ($result != null) $this->SQL = $result;
if ($to == 0) $to = $this->db_num_rows();
$this->db_data_seek($from);
if ($csv == FALSE)
{
$out = '<table border=1 cellspacing=0, cellpadding=4><tr bgcolor=#D0D0D0><tr>';
for($i = 0; $i < $this->db_num_fields(); $i++)
{
$out .= '<td>' . $this->db_field_name($i) . '</td>';
}
$out .= '</tr>';
$i = 1;
while ($row = $this->db_fetch_assoc())
{
$out .= '<tr>';
foreach ($row as $value)
{
if ($value == '') $value = ' ';
$out .= '<td>' . $value . '</td>';
}
$out .= '</tr>';
if ($i >= $to) break;
$i++;
}
$out .= '</table>';
}
else
{
for($i = 0; $i < $this->db_num_fields(); $i++)
{
$out .= $this->db_field_name($i) . $separator;
}
$out .= $crlf;
$i = 1;
while ($row = $this->db_fetch_assoc())
{
foreach ($row as $value)
{
$value = str_replace("\n", ' ', $value);
$value = str_replace("\r", '', $value);
$out .= $value . $separator;
}
$out .= $crlf;
if ($i >= $to) break;
$i++;
}
}
$this->db_data_seek(0);
return $out;
}
/**
* Zapinani / vypinani userlogu
*
* Zapinani / vypinani userlogu. Zapne, vypne a nastavi moznost logovani a zobrazovani SQL dotazu vcetne casu provadeni.
*
* @access public
* @param bool $ULOG 1 = zapnuty log mod, 0 = vypnuty
* @param bool $SHOW_MESSAGE 1 = zobrazuhe dotazy a casy provadeni ve strance
* @param bool $LOG_MESSAGE 1 = loguje dotazy a casy provadeni ve strance do souboru
* @param string $USERLOG cesta k userlogu. Neni li zadana, pouzije se defaultni. Vhodne uvadet absolutni cestu
* @param int $PRECISION zaokrouhleni casu vykonavani na $PRECISION desetinnych mist
* @return void
*/
function userLog($ULOG = 0, $SHOW_MESSAGE = 0, $LOG_MESSAGE = 0, $USERLOG = null, $PRECISION = 5)
{
$this->SHOW_MESSAGE = $SHOW_MESSAGE;
$this->LOG_MESSAGE = $LOG_MESSAGE;
$this->ULOG = $ULOG;
if ($ULOG == 1)
{
if ($USERLOG != null)
{
$this->USERLOG = basename($USERLOG);
$this->ULOGDIR = dirname($USERLOG);
}
else
{
$this->USERLOG = 'mysql_db_class_user.log';
$this->ULOGDIR = dirname($GLOBALS['SCRIPT_FILENAME']) . '/logs';
}
$this->PRECISION = $PRECISION;
}
}
/**
* funkce pro tvorbu ORDER BY pro pouziti v SQL dotazu a v URL
*/
function orderby(&$ORDERBY, $by, $ord)
{
if (isset($_REQUEST[$ORDERBY[ORD]])) $ord = ($_REQUEST[$ORDERBY[ORD]] == 1) ? 1 : 0;
if (isset($_REQUEST[$ORDERBY[BY]])) $by = $_REQUEST[$ORDERBY[BY]];
$this->ORD = ($ord == 1) ? 0 : 1; // dalsi order bude opacne
return ' ORDER BY ' . $by . ' ' . (($ord == 1) ? 'DESC' : 'ASC') . ' ';
}
}
?>