<?
/*
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
*/
require_once "$HPEinc/newsaccess.inc";
class HPENewsSource
{
var $id = 0;
var $name = "";
var $url = "";
var $host = "";
var $port = 80;
var $path = "";
var $header = "";
var $source = "";
var $language = "en";
var $maxitems = 0;
var $timeout = 21600;
var $level = 0;
var $type = 0;
var $typendx = 0;
var $isxml = 0;
var $flags = 0;
var $param1 = "";
var $param2 = "";
var $param3 = "";
var $description = "";
var $icon = "";
var $limitdesc = 0;
var $editor = 0;
var $categorie = 0;
var $reqcomment = "";
var $branding = "";
var $adddate = "NOW()";
var $moddate = "NOW()";
var $cachefile = ""; // where to cache the data
var $thetitle; // parsed title for the source
var $thelink; // parsed link
var $filedate = 0; // date of parse
var $itemcount = 0; // number of newly parsed items
var $data; // array of parsed data
var $urating = 0; // user ratting
var $uratingvotes = 0; // number of votes
function HPENewsSource()
{
}
function init()
{
$this->id = 0;
$this->syndic8id = 0;
$this->name = "";
$this->url = "";
$this->host = "";
$this->port = 80;
$this->path = "";
$this->header = "";
$this->source = "";
$this->language = "en";
$this->type = 0;
$this->typendx = 0;
$this->isxml = 0;
$this->maxitems = 0;
$this->timeout = 1800;
$this->flags = 0;
$this->param1 = "";
$this->param2 = "";
$this->param3 = "";
$this->description = "";
$this->icon = "";
$this->level = 0;
$this->limitdesc = 0;
$this->editor = 0;
$this->categorie = 0;
$this->branding = "";
$this->template = "";
$this->reqcomment = "";
$this->cachefile = "";
$this->thetitle = "";
$this->thelink = "";
$this->itemcount = 0;
$this->data = array();
$this->urating = 0;
$this->uratingvotes = 0;
}
function read($id)
{
if($id == $this->id)
return HPENPidle;
$this->id = 0;
global $HPEtheDB;
global $HPEinc;
global $HPEtheConfig;
$this->init();
$cache = $this->makecachepath($id, "inc");
if(file_exists($cache))
{
include $cache;
}
else
{
$query = "SELECT * FROM $HPEtheDB->dbtnewssites WHERE siteid=$id";
$row = $HPEtheDB->one_array($query);
if($row)
{
$this->id = $id;
$this->name = stripslashes($row["sitename"]);
$this->source = $row["source"];
$this->url = $row["siteurl"];
$this->host = $row["sitehost"];
$this->port = $row["siteport"];
$this->path = $row["newspath"];
$this->header = $row["header"];
$this->language = strtolower($row["language"]);
$this->type = $row["newstype"];
$n = count($HPEtheConfig->newsparsers);
for($i=0; $i<$n; $i++)
if($HPEtheConfig->newsparsers[$i]["id"] == $this->type)
break;
if($i != $n) $this->typendx = $i;
else $this->typendx = -1;
$this->categorie = $row["categorie"];
$this->maxitems = $row["maxitems"];
$this->timeout = $row["timeout"];
$this->param1 = $row["param1"];
$this->param2 = $row["param2"];
$this->param3 = $row["param3"];
$this->description = $row["description"];
$this->icon = $row["icon"];
$this->flags = $row["flags"];
$this->level = $row["level"];
$this->limitdesc = $row["limitdesc"];
$this->reqcomment = $row["reqcomment"];
$this->branding = $row["branding"];
$this->adddate = $row["adddate"];
$this->moddate = $row["moddate"];
$this->urating = $row["urating"];
$this->computeUserRating();
$this->editor = $row["editor"];
$this->editoricon = "";
$this->editorlink = "";
$this->editorname = "";
if($this->editor)
{
if($row = $HPEtheDB->one_array("SELECT * FROM $HPEtheDB->dbtnewspublisher WHERE pubid=$this->editor"))
{
$this->editoricon = $row["icon"];
$this->editorlink = $row["url"];
$this->editorname = $row["name"];
}
else $this->editor = 0;
}
if($this->type == 0 || $this->type == 9) $this->isxml = 1;
elseif(stristr($this->path, "rss") || stristr($this->path, "xml")) $this->isxml = 1;
else $this->isxml = 0;
$this->syndic8id = $HPEtheDB->one_data("SELECT syndic8id FROM $HPEtheDB->dbtsyndic8 WHERE hpeid=$id");
$cf = @fopen($cache, "w");
if($cf)
{
flock($cf, 2);
fputs($cf, "<?\n\n" );
fputs($cf, "\$this->name = '" . escquotes($this->name) . "';\n" );
fputs($cf, "\$this->url = '$this->url';\n" );
fputs($cf, "\$this->source = '$this->source';\n" );
fputs($cf, "\$this->host = '$this->host';\n" );
fputs($cf, "\$this->port = $this->port;\n" );
fputs($cf, "\$this->path = '$this->path';\n" );
fputs($cf, "\$this->header = '" . addslashes($this->header) . "';\n" );
fputs($cf, "\$this->language = '$this->language';\n" );
fputs($cf, "\$this->id = $this->id;\n" );
fputs($cf, "\$this->level = $this->level;\n" );
fputs($cf, "\$this->limitdesc = $this->limitdesc;\n" );
fputs($cf, "\$this->type = $this->type;\n" );
fputs($cf, "\$this->typendx = $this->typendx;\n" );
fputs($cf, "\$this->isxml = $this->isxml;\n" );
fputs($cf, "\$this->maxitems = $this->maxitems;\n" );
fputs($cf, "\$this->timeout = $this->timeout;\n" );
fputs($cf, "\$this->flags = $this->flags;\n" );
fputs($cf, "\$this->editor = $this->editor;\n" );
fputs($cf, "\$this->editoricon = '" . addslashes($this->editoricon) . "';\n" );
fputs($cf, "\$this->editorlink = '" . addslashes($this->editorlink) . "';\n" );
fputs($cf, "\$this->editorname = '" . addslashes($this->editorname) . "';\n" );
fputs($cf, "\$this->categorie = $this->categorie;\n" );
fputs($cf, "\$this->branding = '$this->branding';\n" );
fputs($cf, "\$this->param1 = '" . escquotes($this->param1) . "';\n" );
fputs($cf, "\$this->param2 = '" . escquotes($this->param2) . "';\n" );
fputs($cf, "\$this->param3 = '" . escquotes($this->param3) . "';\n" );
fputs($cf, "\$this->description = '" . addslashes($this->description) . "';\n" );
fputs($cf, "\$this->reqcomment = '" . addslashes($this->reqcomment) . "';\n" );
fputs($cf, "\$this->icon = '$this->icon';\n" );
fputs($cf, "\$this->urating = $this->urating;\n" );
fputs($cf, "\$this->uratingvotes = $this->uratingvotes;\n" );
fputs($cf, "\$this->syndic8id = $this->syndic8id;\n" );
fputs($cf, "\n?>\n" );
flock($cf, 3);
fclose($cf);
}
}
else
{
if($HPEtheConfig->errorall)
echo "$HPEtheDB->dberr<BR>\n";
return HPENPnodbinfo;
}
}
if($this->flags & HPENewsDisable && !$HPEtheConfig->errorall)
{
$this->init();
return HPENPnodbinfo;
}
$this->cachefile = $this->makecachepath($id, "spd");
return HPENPidle;
}
function makecachepath($id, $ext="spd")
{
global $HPEinc;
global $HPEtheConfig;
global $HPEtheDB;
$cache = "$HPEtheConfig->cachepath/$HPEtheDB->dbtnewssites/";
$cache .= sprintf("%03d", ($id % 100));
if(!@is_dir($cache)) mkdir($cache, 0777);
if(!@is_writable($cache)) die("Can't write to dir $cache");
$cache .= "/$id.$ext";
return $cache;
}
function loadnews($id, $maxitems = 0)
{
if($this->id != $id)
$res = $this->read($id);
if($this->id) {
return $this->loadCache();
} else return $res;
}
// if the cache is valid, this loads the cache file
function loadCache()
{
if(file_exists($this->cachefile) == false)
return HPENPnocache;
if(HPELoadSingleNews($data, $this->id) == 0)
return HPENPemptycache;
$this->data = $data["items"];
$this->thetitle = $data["title"];
$this->thelink = $data["link"];
$this->filedate = $data["lastupdate"];
$this->itemcount = $data["itemcount"];
unset($data);
$this->status = HPENPfromcache;
return HPENPidle;
}
function updateDB()
{
global $HPEtheDB;
$today = date("Ymdhis");
$query = "UPDATE $HPEtheDB->dbtnewssites SET " .
"sitename=" . $HPEtheDB->escapeString($this->name) . ", " .
"moddate=$today, " .
"source='$this->source', " .
"sitehost='$this->host', " .
"siteport=$this->port, " .
"newspath='$this->path', " .
"header='$this->header', " .
"newstype=$this->type, " .
"description=" . $HPEtheDB->escapeString($this->desc) . ", " .
"param1='$this->param1', " .
"param2='$this->param2', " .
"param3='$this->param3', " .
"siteurl='$this->url', " .
"categorie=$this->categorie, " .
"language='$this->language', " .
"icon='$this->icon', " .
"timeout=$this->timeout, " .
"flags=$this->flags, " .
"editor=$this->editor, " .
"branding='$this->branding', " .
"level=$this->level, " .
"limitdesc=$this->limitdesc, " .
"maxitems=$this->maxitems " .
"WHERE siteid=$this->id";
$res = $HPEtheDB->query($query);
return !$HPEtheDB->iserror($res);
}
function createDB()
{
global $HPEtheDB;
if($this->id == 0)
die("must give source a siteid");
$today = date("Ymdhis");
$query = "INSERT INTO $HPEtheDB->dbtnewssites " .
"(siteid, adddate, sitename, moddate, source, sitehost, siteport, newspath, " .
"header, newstype, description, param1, param2, param3, siteurl, categorie, language, " .
"icon, timeout, flags, editor, maxitems, branding) VALUES " .
"($this->id, $today, " . $HPEtheDB->escapeString($this->name) . ", $today, '$this->source', " .
"'$this->host', $this->port, '$this->path', '$this->header', $this->type, " .
$HPEtheDB->escapeString($this->desc) . ", '$this->param1', '$this->param2', '$this->param3', " .
"'$this->url', $this->categorie, '$this->language', '$this->icon', $this->timeout, " .
"$this->flags, $this->editor, $this->maxitems, '$this->branding')";
$res = $HPEtheDB->query($query);
return !$HPEtheDB->iserror($res);
}
function computeUserRating()
{
global $HPEtheDB;
$total = 0;
$this->uratingvotes = 0;
$this->urating = 0;
$result = $HPEtheDB->query("SELECT * FROM $HPEtheDB->dbtnewspoll WHERE siteid=$this->id");
if(!$HPEtheDB->iserror($result) && $HPEtheDB->getrowcount($result))
{
while($row = $HPEtheDB->getrow($result))
{
$this->uratingvotes += $row["optionCount"];
$total += $row["voteID"] * $row["optionCount"];
}
$this->urating = (int) ($total / $this->uratingvotes);
}
}
function uvote($vote)
{
global $HPEtheDB;
global $HPEinc;
global $HPEtheConfig;
$query = "UPDATE $HPEtheDB->dbtnewspoll SET optionCount=optionCount+1 WHERE siteid=$this->id AND voteID=$vote";
$res = $HPEtheDB->query($query);
if($HPEtheDB->iserror($res) || $HPEtheDB->getaffectedrowcount() == 0)
{
$query = "INSERT INTO $HPEtheDB->dbtnewspoll (siteid, voteID, optionCount) VALUES " .
"($this->id, $vote, 1)";
$HPEtheDB->query($query);
}
$this->computeUserRating();
$cache = $this->makecachepath($this->id, "inc");
@unlink($cache);
}
function GetItemTableCode()
{
return date("w");
}
};
?>