<?
/* Inicio da classe  */
class cMysql {
	var $host;  
	var $db;
	var $user;
	var $pass;
	var $conexao;
	var $sSql;
	var $arrRecordset;
	
	function connect($host, $db, $user, $pass) { 
		$this->host = $host; 
		$this->db  = $db; 
		$this->user = $user; 
		$this->pass = $pass; 
		$this->conexao = mysql_connect($this->host,$this->user,$this->pass);
		if (!$this->conexao) { 
			echo "N�o foi poss�vel conectar-se ao Bando de Dados MySQL"; 
		}else{
			if (!mysql_select_db($this->db,$this->conexao)) { 
				echo "Banco de dados n�o encontrado"; 
			}
		}
	}
	
	function cMysql(){
		$this->connect('localhost', 'banco', 'root', 'senhadoroot');
	}
	
	function disconnect(){
		mysql_close($this->conexao);
	}
	
	function free_page_result($recordset) {
		mysql_free_result($recordset);
	}
	
	function get_num_rows() {
		$this->num_rows = mysql_num_rows($this->arrRecordset);
		return $this->num_rows;
	}
	
	function query ($sTipo, $sSql) { 
		$this->sSql = $sSql;
		switch ($sTipo) {
		    case "select":{
				$this->arrRecordset = mysql_query($this->sSql, $this->conexao) or die(mysql_error('Erro ao executar o Select:'.$this->sSql)); 
			}
		    break;    
		    case "insert":{
				$this->arrRecordset = mysql_query($this->sSql, $this->conexao) or die(mysql_error('Erro ao executar o Insert:'.$this->sSql)); 
		    }
		    break;
		    case "delete":{
				$this->arrRecordset = mysql_query($this->sSql, $this->conexao) or die(mysql_error('Erro ao executar o Delete:'.$this->sSql)); 
			}
			break;
		    case "update":{
				$this->arrRecordset = mysql_query($this->sSql, $this->conexao) or die(mysql_error('Erro ao executar o Update:'.$this->sSql)); 
			}
			break;
		}
		return $this->arrRecordset;
	}
}

Class XML{
	var $Content="";
	var $RootNode="";
	var $ParentNode="";
	var $CRLF="\r\n";
	var $End="";
	
	Function XML($Version="1.0",$Encoding="utf-8"){
		$this->Content.="<?xml version=\"{$Version}\" encoding=\"{$Encoding}\"?>{$this->CRLF}";
	}

	Function CreateNode($NodeName="root",$Attribute=""){
		$NodeName=$this->Filter($NodeName);
		$this->RootNode=$NodeName;
		$Attribute=$this->ParseAttribute($Attribute);
		return $this->Content.="<{$NodeName}{$Attribute}>{$this->CRLF}";
	}

	Function AppendNode($NodeName,$Attribute,$Data="",$CDate=true){
		$NodeName=$this->Filter($NodeName);
		if(empty($Data)){
			if(!empty($this->ParentNode)){
				$this->Content.="</{$this->ParentNode}>{$this->CRLF}";
			}
			$this->ParentNode=$NodeName;
			$Attribute=$this->ParseAttribute($Attribute);
			return $this->Content.="<{$NodeName}{$Attribute}>{$this->CRLF}";
		}else{
			$Attribute=$this->ParseAttribute($Attribute);
			return $this->Content.=$CDate?"<{$NodeName}{$Attribute}>{$this->CRLF}<![CDATA[{$Data}]]>{$this->CRLF}</{$NodeName}>{$this->CRLF}":"<{$NodeName}{$Attribute}>{$Data}</{$NodeName}>{$this->CRLF}";
		}
	}
	
	Function End(){
		if($this->End){
			return $this->Content;
		}else{
			$this->End=true;
			return $this->Content=$this->ParentNode==""?$this->Content."</{$this->RootNode}>":$this->Content."</{$this->ParentNode}>{$this->CRLF}</{$this->RootNode}>";
		}
	}

	Function Display(){
		ob_start();
		header("Content-type: text/xml");
		echo $this->End();
		ob_end_flush();
	}

	Function Save($Filename){
		if(!$Handle=fopen($Filename,'wb+')){
			$this->Error("Couldn't Write File,Make Sure Your Access");
		}
		flock($Handle,LOCK_EX);
		fwrite($Handle,$this->End());
		return fclose($Handle);
	}

	Function Error($ErrorStr='',$ErrorNo='',$Stop=true){
		exit($ErrorStr);
	}

	Function ParseAttribute($Argv){
		$Attribute='';
		if(is_array($Argv)){
			foreach($Argv as $Key=>$Value){
				$Value=$this->Filter($Value);
				$Attribute.=" $Key=\"$Value\"";
			}
		}
		return $Attribute;
	}
	
	Function Filter($Argv){
		$Argv=trim($Argv);
		$Search=array("<",">","\"");
		$Replace=array("","","'");
		return str_replace($Search,$Replace,$Argv);
	}
}
/* Fim da classe  */

/* Forma de Uso */
	$xml = new xml;
	$xml->CreateNode('redirecionamentos');
	$qRedirecionamento = new cMysql;
	$rsRedirecionamento = $qRedirecionamento->query('select','SELECT * from jwt_redirect ORDER BY redirect_time ASC');
	$registros = $qRedirecionamento->get_num_rows();
	for ($i = 0; $i < $registros; $i++){
		$atributos = array("url"=>mysql_result($rsRedirecionamento, $i, "redirect_url"), "time"=>mysql_result($rsRedirecionamento, $i, "redirect_time"), "ip"=>mysql_result($rsRedirecionamento, $i, "redirect_ip"));
		$xml->AppendNode('url',$atributos,'',false);
	}
	$qRedirecionamento->free_page_result($rsRedirecionamento);
	$arquivo = $xml->save('teste.xml');
	//$xml->Display();
?>