192 lines
5.9 KiB
Plaintext
192 lines
5.9 KiB
Plaintext
<?
|
|
|
|
function cloud_help() {
|
|
}
|
|
|
|
function cloud_cron() {
|
|
if (time() % 250 == 0) {
|
|
$result = db_query("SELECT * FROM site");
|
|
}
|
|
else {
|
|
$result = db_query("SELECT * FROM site WHERE timestamp > ". (time() - 604800));
|
|
}
|
|
|
|
while ($site = db_fetch_array($result)) {
|
|
cloud_update($site);
|
|
}
|
|
}
|
|
|
|
function cloud_perm() {
|
|
return array("access site cloud", "administer site cloud");
|
|
}
|
|
|
|
function cloud_link($type) {
|
|
if ($type == "page" && user_access("access site cloud")) {
|
|
$links[] = "<a href=\"module.php?mod=cloud\">". t("site cloud") ."</a>";
|
|
}
|
|
|
|
if ($type == "admin" && user_access("administer site cloud")) {
|
|
$links[] = "<a href=\"admin.php?mod=cloud\">". t("site cloud") ."</a>";
|
|
}
|
|
|
|
return $links ? $links : array();
|
|
}
|
|
|
|
function cloud_update($site) {
|
|
|
|
/*
|
|
** Check whether the site is properly configured:
|
|
*/
|
|
|
|
if (!ereg("^http://|ftp://", $site[link])) {
|
|
watchdog("warning", "cloud: invalid or missing URL for '$site[name]'");
|
|
}
|
|
|
|
if (!ereg("^http://|ftp://", $site[feed])) {
|
|
watchdog("warning", "cloud: invalid or missing URL to monitor for '$site[name]'");
|
|
}
|
|
|
|
/*
|
|
** Grab the page and update the database if required:
|
|
*/
|
|
|
|
$link = parse_url($site[feed]);
|
|
$fp = fsockopen($link[host], ($link[port] ? $link[port] : 80), &$errno, &$errstr, 15);
|
|
|
|
if ($fp) {
|
|
// fetch data:
|
|
fputs($fp, "GET $link[path]?$link[query] HTTP/1.0\nUser-Agent: ". variable_get(site_name, "drupal") ."\nHost: $link[host]\nAccept: */*\n\n");
|
|
while(!feof($fp)) $data .= fgets($fp, 128);
|
|
|
|
if (strstr($data, "200 OK")) {
|
|
if (abs($site[size] - strlen($data)) > 50) {
|
|
db_query("UPDATE site SET size = '". strlen($data) ."', timestamp = '". time() ."' WHERE link = '". check_input($site[link]) ."'");
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
watchdog("warning", "cloud: failed to syndicate from '$site[name]'". ($errstr ? ": $errstr" : ""));
|
|
}
|
|
}
|
|
|
|
|
|
function cloud_form($edit = array()) {
|
|
global $REQUEST_URI;
|
|
|
|
$form .= form_textfield("Site name", "name", $edit["name"], 50, 128, "The name of the website you want to monitor for updates.");
|
|
$form .= form_textfield("Site URL", "link", $edit["link"], 50, 255, "The URL of the website you want to monitor for updates.");
|
|
$form .= form_textfield("URL to monitor", "feed", $edit["feed"], 50, 255, "The URL of the page you want to monitor for updates. Likely to be same as the site's URL but useful to monitor framed pages and more accurate when pointed to a XML/RSS/RDF feed.");
|
|
|
|
$form .= form_submit("Submit");
|
|
|
|
if ($edit["sid"]) {
|
|
$form .= form_submit("Delete");
|
|
$form .= form_hidden("sid", $edit["sid"]);
|
|
}
|
|
|
|
return form($REQUEST_URI, $form);
|
|
}
|
|
|
|
function cloud_get_site($sid) {
|
|
return db_fetch_array(db_query("SELECT * FROM site WHERE sid = '". check_input($sid) ."'"));
|
|
}
|
|
|
|
function cloud_save($edit) {
|
|
if ($edit["sid"] && $edit["name"]) {
|
|
db_query("UPDATE site SET name = '". check_input($edit["name"]) ."', link = '". check_input($edit["link"]) ."', feed = '". check_input($edit["feed"]) ."' WHERE sid = '". check_input($edit["sid"]) ."'");
|
|
}
|
|
else if ($edit["sid"]) {
|
|
db_query("DELETE FROM site WHERE sid = '". check_input($edit["sid"]) ."'");
|
|
}
|
|
else {
|
|
db_query("INSERT INTO site (name, link, feed) VALUES ('". check_input($edit["name"]) ."', '". check_input($edit["link"]) ."', '". check_input($edit["feed"]) ."')");
|
|
}
|
|
}
|
|
|
|
function cloud_display() {
|
|
$result = db_query("SELECT * FROM site ORDER BY name");
|
|
|
|
$output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
|
|
$output .= " <tr><th>site</th><th>last update</th><th colspan=\"2\">operations</th></tr>\n";
|
|
while ($site = db_fetch_object($result)) {
|
|
$output .= " <tr><td><a href=\"". check_output($site->link) ."\">". check_output($site->name) ."</a></td><td>". ($site->timestamp ? format_interval(time() - $site->timestamp) ." ago" : "never") ."</td><td><a href=\"admin.php?mod=cloud&op=edit&id=$site->sid\">edit site</a></td><td><a href=\"admin.php?mod=cloud&op=update&id=$site->sid\">update site</a></td></tr>\n";
|
|
}
|
|
$output .= "</table>\n";
|
|
|
|
return $output;
|
|
}
|
|
|
|
function cloud_list($limit = 10) {
|
|
$result = db_query("SELECT * FROM site WHERE timestamp > ". (time() - 604800) ." ORDER BY timestamp DESC LIMIT $limit");
|
|
|
|
$hour = -1;
|
|
$list = -1;
|
|
while ($site = db_fetch_object($result)) {
|
|
if ($hour != floor((time() - $site->timestamp) / 3600)) {
|
|
$hour = floor((time() - $site->timestamp) / 3600);
|
|
if ($hour < 12) {
|
|
$output .= "<p />Updated ". format_plural($hour, "hour", "hours") ." ago:";
|
|
}
|
|
else if ($list) {
|
|
$output .= "<p />Updated ". format_plural($hour, "+ hour", "+ hours") ." ago:";
|
|
$list = 0;
|
|
}
|
|
}
|
|
$output .= "<br /> ". format_url($site->link, $site->name);
|
|
}
|
|
return $output;
|
|
}
|
|
|
|
function cloud_page() {
|
|
global $theme;
|
|
|
|
if (user_access("access site cloud")) {
|
|
$theme->header();
|
|
$theme->box(t("Updated sites"), cloud_list(100));
|
|
$theme->footer();
|
|
}
|
|
}
|
|
|
|
function cloud_block() {
|
|
$block[0]["subject"] = t("Sites");
|
|
$block[0]["content"] = cloud_list(20);
|
|
$block[0]["info"] = t("Sites");
|
|
return $block;
|
|
}
|
|
|
|
function cloud_admin() {
|
|
global $op, $id, $edit;
|
|
|
|
if (user_access("administer site cloud")) {
|
|
print "<SMALL><A HREF=\"admin.php?mod=cloud&op=add\">add new site</A> | <A HREF=\"admin.php?mod=cloud\">overview</A> | <A HREF=\"admin.php?mod=cloud&op=help\">help</A></SMALL><HR>\n";
|
|
|
|
switch ($op) {
|
|
case "add":
|
|
print cloud_form();
|
|
break;
|
|
case "edit":
|
|
print cloud_form(cloud_get_site($id));
|
|
break;
|
|
case "update":
|
|
print status(cloud_update(cloud_get_site($id)));
|
|
print cloud_display();
|
|
break;
|
|
case "help":
|
|
cloud_help();
|
|
break;
|
|
case "Delete":
|
|
$edit[name] = 0;
|
|
// fall through:
|
|
case "Submit":
|
|
print status(cloud_save($edit));
|
|
// fall through:
|
|
default:
|
|
print cloud_display();
|
|
}
|
|
}
|
|
else {
|
|
print message_access();
|
|
}
|
|
}
|
|
|
|
?> |