<?php
/*
Daniel Afonso Heisler
CPD - UNIVATES
daniel@fates.tche.br
*/
class Connection
{
var $filename;
var $fp;
var $port;
function Connection()
{
GLOBAL $REMOTE_ADDR;
GLOBAL $DOCUMENT_ROOT;
$this->filename = "/tmp/Login.$REMOTE_ADDR";
$this->log = "$DOCUMENT_ROOT/../logs/sql.log";
$this->result = "/tmp/Result.$REMOTE_ADDR";
$this->port = '5432';
}
function SQLFields($table,$LoginDB=null)
{
if ( $this->fp = fopen($this->filename, "r") )
{
$contents = fread($this->fp, filesize($this->filename));
fclose ($this->fp);
list($LoginDB,$this->port,$LoginUID,$LoginPSW) = split(":",$contents);
$LoginPSW = base64_decode($LoginPSW);
if ( $LoginUID && $LoginPSW )
{
$sql = "select * from $table where id = 0";
system("echo -e '{$LoginUID}\n{$LoginPSW}\n{$sql};' | /usr/local/pgsql/bin/psql $LoginDB -u > $this->result");
$this->fp = fopen($this->result, "r");
$contents = fread($this->fp, filesize($this->result));
$cont = explode("\n",$contents);
$contents = $cont[0];
$result = str_replace(' ','',$contents);
$contents = explode('|',$result);
fclose ($this->fp);
unlink($this->result);
return($contents);
}
else
return null;
}
else
return null;
}
function SQLData($sql,$LoginDB=null,$uid=null,$pwd=null,$host=null,$port=null)
{
if ( $tab = $this->SQLExec($sql,$LoginDB,$uid,$pwd,$host,$port) )
{
if ( pg_fetch_array($tab,0) )
{
for ( $x=0; $x<pg_numrows($tab); $x++ )
{
$z = pg_fetch_array($tab,$x);
$temp[$x] = $z;
for ( $y=0; $y<((count($z)+1)/2); $y++ )
$dados[$x][$y] = $temp[$x][$y];
}
}
else
return null;
return $dados;
}
else
return null;
}
function SQLExec($sql,$LoginDB=null,$uid=null,$pwd=null,$host=null,$port=null)
{
if ( $this->fp = fopen($this->filename, "r") )
{
$contents = fread($this->fp, filesize($this->filename));
fclose ($this->fp);
if ( ! $LoginDB )
list($LoginDB,$this->port,$LoginUID,$LoginPSW) = split(":",$contents);
else
list($temp,$this->port,$LoginUID,$LoginPSW) = split(":",$contents);
$LoginPSW = base64_decode($LoginPSW);
$parametros = "dbname=$LoginDB port=$this->port user=$LoginUID password=$LoginPSW";
if ( $LoginDB && $uid && $pwd )
{
if ( $host )
$parametros = "dbname=$LoginDB host=$host user=$uid password=$pwd";
else
$parametros = "dbname=$LoginDB user=$uid password=$pwd";
if ( $port )
$parametros .= " port=$port";
else
$parametros .= " port=$this->port";
}
$con = pg_connect ($parametros);
if ( empty($sql) or !($con) )
return null;
else
{
if ( $res = pg_exec($con,$sql) )
$this->SQLLogs($sql,$LoginUID);
return $res;
}
}
else
return null;
}
function SQLLogs($sql,$LoginUID)
{
GLOBAL $DOCUMENT_ROOT;
$this->log = "$DOCUMENT_ROOT/../logs/sql.log";
if ( $this->fp = fopen($this->log, "a+") )
{
$sql_sel = stristr($sql,'select');
if ( ! $sql_sel )
{
$contents = date("d/m/Y - H:i");
$contents .= (" -> $LoginUID\n -> $sql\n");
fputs($this->fp, $contents);
}
fclose ($this->fp);
}
}
}
?> |