<?php
/**
 * Project:     astat - simple site engine
 * File:        /core/database/mysql.class.php
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.astat.org SVN: $URL: http://svn.astat.org/astat/trunk/core/database/mysql.class.php $
 * @copyright 2025 becast.at
 * @author Bernhard Jaud <bernhard at becast dot at>
 * @package astat core
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version $Id: mysql.class.php 104 2010-02-20 19:16:12Z genuineparts $
*/
 
$module["db"]["name"]="Database Class (mysql)";
$module["db"]["ver"]="1.0.1";
class db {
	var $host="";
	var $user="root";
	var $password="";
	var $db="becast";
	var $encoding;
	var $logging;
	var $abfragen=0;
	var $exception;
	private $conid=FALSE;
	public const ASSOC = 1;
	public const NUM = 2;
	public const BOTH = 3;
 
	function __construct($host,$user,$password,$db,$encoding="utf8",$logging=false,$exception=false) {
		global $log,$config;
		$this->host=$host;
		$this->user=$user;
		$this->password=$password;
		$this->db=$db;
		$this->logging=$logging;
		$this->exception=$exception;

		if($this->logging==FALSE && DEBUG!=FALSE){
			$this->logger=new logger("file",dirname(dirname(dirname(__FILE__))).'/logs/mysql_debug.log',5);
		}elseif($this->logging){
			$this->logger=new logger("file",dirname(dirname(dirname(__FILE__))).'/logs/mysql.log');
		}
		
		$this->encoding=$encoding;
		$this->conid;

		if($this->logging)
			$this->logger->write("mySQL Klasse instanziert", 5);
			if(!$this->conid){
				if($this->logging)
					$this->logger->write("Connection zu mySQL Server besteht nicht.", 5,__LINE__,__FILE__);
					$this->conid=$this->connect($this->host,$this->user,$this->password,$this->db,$this->encoding);
				}else{
					if($this->logging)
					$this->logger->write("Connection zu mySQL Server besteht. ID ".$this->conid, 5,__LINE__,__FILE__);
				}
			if($this->encoding)
			$this->query("SET NAMES '".$this->encoding."'");
	}

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

	function connect($host, $user, $password, $db, $encoding){
	    $conn = @mysql_connect($host, $user, $password);
		if($this->logging)
			$this->logger->write("Connect to Mysql Server", 5,__LINE__,__FILE__);

	   	if(!$conn){
			if($this->exception){
				 throw new Exception('mySQLi Connect failed');
			}else{
				trigger_error("mySQL Connect failed",E_USER_ERROR);

				if($this->logging)
					$this->logger->write("Connect Failed ". $this->error(), 5,__LINE__,__FILE__);
					die();
			}
		}else{
			if($this->logging)
				$this->logger->write("Selecting Database ".$db, 5,__LINE__,__FILE__);
		}

	    		$conn2 = @mysql_select_db($db,$conn);
			if($this->logging)
				$this->logger->write("Connect to Mysql Server", 5,__LINE__,__FILE__);

	  		if(!$conn2){
				include('templates/general_error.tpl');

				if($this->logging)
					$this->logger->write("Select failed ". $this->error(), 5,__LINE__,__FILE__);
					die();
				}else{
					if($this->logging)
						$this->logger->write("Database ".$db." selected", 5,__LINE__,__FILE__);
				}

		return $conn;	
	  }


	function disconnect(){
		if($this->logging)
			$this->logger->write("Closing Mysql Connection.", 5,__LINE__,__FILE__);

   		@mysql_close($this->conid);
   	}

	function query($query){
		global $abfragen;

		if($this->logging)
   		$this->logger->write($query." ". $this->conid, 5,__LINE__,__FILE__);

	    $this->abfragen++;
	    $res = mysql_query($query, $this->conid);
	    return $res;
    	}

	function querys(){
	    return $this->abfragen;
	    }
	
	function num_rows($result){
	    $rows = mysql_num_rows($result);
	    return $rows;
	    }

	function fetch_row($result){
	    $row = mysql_fetch_row($result);
	    return $row;
	    }

	function fetch_array($result){
	    $row = array();
	    $row = mysql_fetch_array($result, MYSQL_ASSOC);
	    return $row;
	}
	
	function escape($string){
	    $return = mysql_real_escape_string($string);
	    return $return;
	}
	
	function escape_binary($string){
		return "X'".$this->escape(bin2hex($string))."'";
	}
	
	function fetch_object($result){
	    $row = mysql_fetch_object($result);
	    return $row;
	}

	function error(){
	    $error = mysql_error($this->conid);
	    return $error;
	}
	
	function errno(){
	    $errno = mysql_errno($this->conid);
	    return $errno ;
	}

	function free_result($result){
	    mysql_free_result($result);
	}
	
	function last_id(){
	    $id = mysql_insert_id($this->conid);
	    return $id;
	}
}
?>
