<?
/*
HPE - News Portal Engine
Copyright (C) 2000-2001 Mike Krus
This program is free software; it 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.
READ LICENSE.TXT IN THE BASE DIRECTORY FOR INFORMATION
ABOUT REDISTRIBUTING THIS SOURCE CODE
*/
class HPEDBAccess
{
var $dberr = "";
var $db = 0;
var $dbconfurl;
var $dbtnewscats = "HPENewsCats";
var $dbtnewssites = "HPENewsSites";
var $dbtnewsbundles = "HPENewsBundles";
var $dbtnewsclicks = "HPENewsClicks";
var $dbtnewsclicksref = "HPENewsClicksRef";
var $dbtnewsitems = "HPENewsItems";
var $dbtnewsindex = "HPENewsIndex";
var $dbtnewscats = "HPENewsCats";
var $dbtnewsupdates = "HPENewsUpdates";
var $dbtnewsupdateseq = "HPENewsUpdateSeq";
var $dbtnewsupdatecatseq = "HPENewsUpdateCatSeq";
var $dbtnewsaccess = "HPENewsAccess";
var $dbtnewspublisher = "HPENewsPublishers";
var $dbtnewspoll = "HPENewsPollData";
var $dbtnewssearch = "HPENewsSearch";
var $dbtnewsprospects = "HPENewsProspects";
var $dbtsyndic8 = "HPESyndic8";
var $dbtusers = "HPEUsers";
var $dbtsessions = "HPESessions";
var $dbtwebmasters = "HPEUserWebmasters";
var $dbtuseraccess = "HPEUserAccess";
var $dbtwebmastersaccess = "HPEUserAccessWebmasters";
var $dbtmailpagejobs = "HPEMailPageJobs";
var $dbtusersexport = "HPEUsersExport";
var $dbtnewsagents = "HPENewsAgents";
var $dbtnewsalerts = "HPENewsAlerts";
var $dbtwatchdog = "HPEWatchdog";
function HPEDBAccess()
{
global $HPEtheConfig;
global $HPEtheThemeMaker;
$this->dbconfurl = $HPEtheConfig->dbconfurl;
if($this->dbconfurl)
{
if(preg_match("%(.+)://(.+):(.*)@(.+)/(.+)%", $this->dbconfurl, $matches))
{
$protocole = $matches[1];
$login = $matches[2];
$passwd = $matches[3];
$host = $matches[4];
$db = $matches[5];
if($protocole != "mysql")
die("Can't work with database type '$protocole'");
$this->db_handle = mysql_connect($host, $login, $passwd);
if (!$this->db_handle)
{
if(!isset($GLOBALS["HPENOSHUTDOWN"]))
{
$data = array(
"message" => $HPEtheConfig->shutdownmsg
);
echo $HPEtheThemeMaker->TPLRun("shutdown", $data);
$this->dberr = mysql_error();
HPEWatchDog("dberror", $this->dberr);
}
return;
}
mysql_select_db($db) or die("Could not select database '$db'");
$this->db = $db;
}
else die("Failed to match db host config");
}
$this->dberr = "";
$this->dbtnewscats = $HPEtheConfig->dbnprefix . $this->dbtnewscats;
$this->dbtnewssites = $HPEtheConfig->dbnprefix . $this->dbtnewssites;
$this->dbtnewsbundles = $HPEtheConfig->dbnprefix . $this->dbtnewsbundles;
$this->dbtnewsclicks = $HPEtheConfig->dbnprefix . $this->dbtnewsclicks;
$this->dbtnewsclicksref = $HPEtheConfig->dbnprefix . $this->dbtnewsclicksref;
$this->dbtnewsitems = $HPEtheConfig->dbnprefix . $this->dbtnewsitems;
$this->dbtnewsindex = $HPEtheConfig->dbnprefix . $this->dbtnewsindex;
$this->dbtnewscats = $HPEtheConfig->dbnprefix . $this->dbtnewscats;
$this->dbtnewsupdates = $HPEtheConfig->dbnprefix . $this->dbtnewsupdates;
$this->dbtnewsupdateseq = $HPEtheConfig->dbnprefix . $this->dbtnewsupdateseq;
$this->dbtnewsupdatecatseq = $HPEtheConfig->dbnprefix . $this->dbtnewsupdatecatseq;
$this->dbtnewsaccess = $HPEtheConfig->dbnprefix . $this->dbtnewsaccess;
$this->dbtnewspublisher = $HPEtheConfig->dbnprefix . $this->dbtnewspublisher;
$this->dbtnewspoll = $HPEtheConfig->dbnprefix . $this->dbtnewspoll;
$this->dbtnewssearch = $HPEtheConfig->dbnprefix . $this->dbtnewssearch;
$this->dbtnewsprospects = $HPEtheConfig->dbnprefix . $this->dbtnewsprospects;
$this->dbtsyndic8 = $HPEtheConfig->dbnprefix . $this->dbtsyndic8;
$this->dbtusers = $HPEtheConfig->dbuprefix . $this->dbtusers;
$this->dbtsessions = $HPEtheConfig->dbuprefix . $this->dbtsessions;
$this->dbtwebmasters = $HPEtheConfig->dbuprefix . $this->dbtwebmasters;
$this->dbtuseraccess = $HPEtheConfig->dbuprefix . $this->dbtuseraccess;
$this->dbtwebmastersaccess = $HPEtheConfig->dbuprefix . $this->dbtwebmastersaccess;
$this->dbtmailpagejobs = $HPEtheConfig->dbuprefix . $this->dbtmailpagejobs;
$this->dbtusersexport = $HPEtheConfig->dbuprefix . $this->dbtusersexport;
$this->dbtnewsagents = $HPEtheConfig->dbuprefix . $this->dbtnewsagents;
$this->dbtnewsalerts = $HPEtheConfig->dbuprefix . $this->dbtnewsalerts;
return;
}
function close()
{
$this->dberr = "";
return $this->db_handle->disconnect();
}
function query($query)
{
$this->dberr = "";
$query = ereg_replace(";\$", "", $query);
$result = mysql_query($query, $this->db_handle);
if($result)
return $result;
$this->dberr = mysql_error() . ": " . $query;
HPEWatchDog("dberror", $this->dberr);
}
function iserror($result)
{
return (!$result);
}
function getrowcount($result)
{
$this->dberr = "";
if (!$result)
return 0;
else
return mysql_num_rows($result);
}
function getaffectedrowcount()
{
$this->dberr = "";
return mysql_affected_rows($this->db_handle);
}
function getrow($result)
{
$this->dberr = "";
if ($result)
return mysql_fetch_array($result, MYSQL_ASSOC);
else
return false;
}
function getobject($result)
{
$this->dberr = "";
if ($result)
return mysql_fetch_object($result);
else
return false;
}
function one_data($query)
{
$this->dberr = "";
$one = $this->query($query);
if($this->iserror($one))
return 0;
$row = mysql_fetch_array($one);
if(!isset($row[0]))
return 0;
return $row[0];
}
function one_array($query)
{
$this->dberr = "";
$one = $this->query($query);
if($this->iserror($one))
return false;
return $this->getrow($one);
}
function next_id($sequence)
{
$this->dberr = "";
$this->query("LOCK TABLES ${sequence}_seq");
$result = $this->query("UPDATE ${sequence}_seq SET id=id+1");
if($result)
{
$this->query("UNLOCK TABLES ${sequence}_seq");
return $this->one_data("SELECT MAX(id) FROM ${sequence}_seq");
}
else
{
die(mysql_error());
}
}
function insert($table, $params, $values, $spec = "")
{
$n = count($params);
if($n != count($values))
die("size of param and value arrays don't match");
$query = "INSERT $spec INTO $table (";
for($i=0; $i<$n; $i++)
{
$query .= $params[$i];
if($i != $n-1)
$query .= ",";
}
$query .= ") VALUES (";
for($i=0; $i<$n; $i++)
{
$query .= $values[$i];
if($i != $n-1)
$query .= ",";
}
$query .= ")";
// echo "<pre>" . htmlentities($query) . "</pre>\n";
return $this->query($query);
}
function replace($table, $params, $values, $spec = "")
{
$n = count($params);
if($n != count($values))
die("size of param and value arrays don't match");
$query = "REPLACE $spec INTO $table (";
for($i=0; $i<$n; $i++)
{
$query .= $params[$i];
if($i != $n-1)
$query .= ",";
}
$query .= ") VALUES (";
for($i=0; $i<$n; $i++)
{
$query .= $values[$i];
if($i != $n-1)
$query .= ",";
}
$query .= ")";
// echo "<pre>" . htmlentities($query) . "</pre>\n";
return $this->query($query);
}
function update($table, $where, $params, $values, $spec = "")
{
$n = count($params);
if($n != count($values))
die("size of param and value arrays don't match");
$query = "UPDATE $spec $table SET ";
for($i=0; $i<$n; $i++)
{
$query .= $params[$i] . "=" . $values[$i];
if($i != $n-1)
$query .= ", ";
}
$query .= " WHERE $where";
// echo "<pre>" . htmlentities($query) . "</pre>\n";
return $this->query($query);
}
function escapeString($str)
{
$nstr = "'" . mysql_escape_string($str) . "'";
return $nstr;
}
};
?>