drupal/modules/backend.class

155 lines
5.2 KiB
Plaintext

<?php
class backend {
// Channel properties:
var $id;
var $url;
var $site;
var $file;
var $contact;
var $timestamp;
// Contains the raw rdf/rss/xml file:
var $data;
// Contains the parsed rdf/rss/xml file:
var $headlines = array(); // latest headlines
function backend($id, $site = "", $url = "", $file = "", $contact = "", $timout = 10800) {
// Get channel info:
$result = db_query("SELECT * FROM channel WHERE id = '$id' OR site = '$site'");
if ($channel = db_fetch_object($result)) {
// Initialize internal variables:
$this->id = $channel->id;
$this->site = $channel->site;
$this->file = $channel->file;
$this->url = $channel->url;
$this->contact = $channel->contact;
$this->timestamp = $channel->timestamp;
// Check to see whether we have to update our headlines first:
if (time() - $this->timestamp > $timout) $this->url2sql();
// Read headlines:
$result = db_query("SELECT * FROM headlines WHERE id = '$this->id' ORDER BY number");
while ($headline = db_fetch_object($result)) {
array_push($this->headlines, "<A HREF=\"$headline->link\">$headline->title</A>");
}
}
else {
$this->site = $site;
$this->url = $url;
$this->file = $file;
$this->contact = $contact;
}
}
function url2sql($timout = 10) {
if ($this->file) {
// Decode URL:
$url = parse_url($this->file);
// Retrieve data from website:
$fp = fsockopen($url[host], ($url[port] ? $url[port] : 80), $errno, $errstr, $timout);
if ($fp) {
// Request data via URL:
fputs($fp, "GET $url[path]?$url[query] HTTP/1.0\nUser-Agent: ". variable_get(site_name, "drupal") ."\nHost: $url[host]\nAccept: */*\n\n");
// Read data from socket:
while(!feof($fp)) $data .= fgets($fp, 128);
if (strstr($data, "200 OK")) {
// Remove existing entries:
$result = db_query("DELETE FROM headlines WHERE id = '$this->id'");
// Strip all 'junk':
$data = ereg_replace("<?phpxml.*/image>", "", $data);
$data = ereg_replace("</rdf.*", "", $data);
$data = chop($data);
// Iterating through our data processing each entry/item:
$items = explode("</item>", $data);
$number = 0;
for (reset($items); $item = current($items); next($items)) {
// Extract data:
$link = ereg_replace(".*<link>", "", $item);
$link = ereg_replace("</link>.*", "", $link);
$title = ereg_replace(".*<title>", "", $item);
$title = ereg_replace("</title>.*", "", $title);
// Increase the number of headlines:
$number += 1;
// Insert item in database:
$result = db_query("INSERT INTO headlines (id, title, link, number) VALUES('". check_input($this->id) ."', '". check_input($title) ."', '". check_input($link) ."', '". check_input($number) ."')");
}
// Mark channels as being updated:
$result = db_query("UPDATE channel SET timestamp = '". time() ."' WHERE id = '$this->id'");
$this->timestamp = time();
}
else {
watchdog("error", "failed to grab headlines from '$this->site': <PRE>$data</PRE>");
}
}
}
}
function displayHeadlines($timout = 1800) {
global $theme;
// Get channel info:
$result = db_query("SELECT * FROM channel WHERE site = '$this->site'");
if ($this->id) {
// Check to see whether we have to update our headlines first:
if (time() - $this->timestamp > $timout) $this->url2sql();
// Grab headlines from database:
$result = db_query("SELECT * FROM headlines WHERE id = '$this->id' ORDER BY number");
while ($headline = db_fetch_object($result)) {
$content .= "<LI><A HREF=\"$headline->link\">$headline->title</A></LI>";
}
// Add timestamp:
$update = round((time() - $this->timestamp) / 60);
// Display box:
$theme->box($this->site, $content);
}
else print "<P>Warning: something funky happened: specified channel could not be found in database.</P>";
}
function add() {
// Add channel:
$result = db_query("INSERT INTO channel (site, file, url, contact, timestamp) VALUES ('". check_input($this->site) ."', '". check_input($this->file) ."', '". check_input($this->url) ."', '". check_input($this->contact) ."', 1)");
}
function save() {
// Save channel:
$result = db_query("UPDATE channel SET site='". check_input($this->site) ."', file='". check_input($this->file) ."', url='". check_input($this->url) ."', contact='". check_input($this->contact) ."' WHERE id='". check_input($this->id) ."'");
}
function delete() {
// Delete channel:
$result = db_query("DELETE FROM channel WHERE id = '$this->id'");
// Delete headlines:
$result = db_query("DELETE FROM headlines WHERE id = '$this->id'");
}
function refresh() {
// Delete headlines:
$result = db_query("DELETE FROM headlines WHERE id = '$this->id'");
// Mark channel as invalid to enforce an update:
$result = db_query("UPDATE channel SET timestamp = 1 WHERE id = '$this->id'");
}
}
?>