cloud.module 9.48 KB
Newer Older
1 2
<?php
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
 
Dries committed
4 5
function cloud_help($type = "administrator") {
  if ($type == "user") {
Dries's avatar
 
Dries committed
6
    $output .= "<p>". t("The cloud monitor tracks or crawls other interesting web sites and displays their latest modification dates.  It acts as a link watcher such that you can keep an eye on the other sites in our cloud.") ."</p>";
Dries's avatar
 
Dries committed
7 8
  }
  else {
Dries's avatar
 
Dries committed
9
    $output .= "The cloud monitor tracks or crawls other interesting web sites and displays their last modification dates.  Visitors to the host site learn about relevant sites and can easily see if there is new content.  Here is how it works:";
Dries's avatar
 
Dries committed
10 11
    $output .= "<ul>";
    $output .= " <li>The site administrator enters names and URLs of the relevant pages on the cloud monitor administration page.</li>";
Dries's avatar
 
Dries committed
12
    $output .= " <li>Drupal's cron function, triggers the cloud module to check all the registered web sites for recent changes or updates.  (A page is updated when there is a <i>x</i>-byte difference since the last time it checked, where <i>x</i> is a configuration option.)</li>";
Dries's avatar
 
Dries committed
13 14 15
    $output .= " <li>The module exports both a page and a block that display the registered sites ordered by their last modification date.</li>";
    $output .= "</ul>";
  }
Dries's avatar
 
Dries committed
16 17 18 19

  return $output;
}

20 21 22 23 24
function cloud_system($field){
  $system["description"] = t("Tracks other sites and displays last date changed.");
  return $system[$field];
}

Dries's avatar
 
Dries committed
25
function cloud_cron() {
Dries's avatar
 
Dries committed
26
  $result = db_query("SELECT * FROM site WHERE timestamp = 0 OR timestamp + refresh < ". time());
Dries's avatar
 
Dries committed
27 28 29 30 31 32 33 34 35 36 37

  while ($site = db_fetch_array($result)) {
    cloud_update($site);
  }
}

function cloud_perm() {
  return array("access site cloud", "administer site cloud");
}

function cloud_link($type) {
Dries's avatar
 
Dries committed
38 39
  $links = array();

Dries's avatar
 
Dries committed
40
  if ($type == "page" && user_access("access site cloud")) {
Dries's avatar
 
Dries committed
41
    $links[] = l(t("site cloud"), "cloud", array("title" => t("Monitor other sites in the cloud.")));
Dries's avatar
 
Dries committed
42 43 44
  }

  if ($type == "admin" && user_access("administer site cloud")) {
Dries's avatar
 
Dries committed
45 46
    $help["general"] = t("The cloud monitor tracks or crawls other interesting web sites and displays their last modification dates. Visitors to the host site learn about relevant sites and can easily see if there is new content. To get the blogroll to work you <b>must</b> setup cron support. To get the blogroll block to display you must turn on the <a href=\"%block\">Site cloud block</a>. To go to a monitored site, click on the site name, to load and/or update the blogroll for that site immediately click on \"update site\", to delete the site go to \"edit site\". ", array("%block" => url("admin/block")));
    $help["add"] = t("Add a site too the blogroll. Don't forget the \"http://\" for your URLs.");
Dries's avatar
 
Dries committed
47

Dries's avatar
 
Dries committed
48 49
    menu("admin/syndication", "content syndication", NULL, NULL, 5);
    menu("admin/syndication/cloud", "blogrolling", "cloud_admin", $help["general"]);
Dries's avatar
 
Dries committed
50
    menu("admin/syndication/cloud/add", "add new site", "cloud_admin", $help["add"]);
Dries's avatar
 
Dries committed
51
    menu("admin/syndication/cloud/help", "help", "cloud_help", NULL, 9);
Dries's avatar
 
Dries committed
52 53
  }

Dries's avatar
 
Dries committed
54
  return $links;
Dries's avatar
 
Dries committed
55 56 57
}

function cloud_update($site) {
Dries's avatar
 
Dries committed
58 59 60 61 62

  /*
  ** Check whether the site is properly configured:
  */

Kjartan's avatar
Kjartan committed
63
  if (!ereg("^http://|https://|ftp://", $site["link"])) {
Dries's avatar
 
Dries committed
64
    watchdog("warning", "cloud: invalid or missing URL for '". $site["name"] ."'", l(t("edit site"), "admin/syndication/cloud/edit/". $site["sid"]));
Dries's avatar
 
Dries committed
65 66
  }

Kjartan's avatar
Kjartan committed
67
  if (!ereg("^http://|https://|ftp://", $site["feed"])) {
Dries's avatar
 
Dries committed
68
    watchdog("warning", "cloud: invalid or missing URL to monitor for '". $site["name"] ."'", l(t("edit site"), "admin/syndication/cloud/edit/". $site["sid"]));
Dries's avatar
 
Dries committed
69 70 71 72 73 74
  }

  /*
  ** Grab the page and update the database if required:
  */

Dries's avatar
 
Dries committed
75 76
  if ($fp = @fopen($site["feed"], "r")) {
    while (!feof($fp)) {
Dries's avatar
 
Dries committed
77 78
      $data .= fgets($fp, 128);
    }
Dries's avatar
 
Dries committed
79

80
    if (abs($site["size"] - strlen($data)) >= $site["threshold"]) {
Dries's avatar
 
Dries committed
81
      db_query("UPDATE site SET size = '". strlen($data) ."', timestamp = '". time() ."' WHERE link = '%s'", $site["link"]);
Dries's avatar
 
Dries committed
82
    }
Dries's avatar
Dries committed
83 84

    fclose($fp);
Dries's avatar
 
Dries committed
85 86
  }
  else {
Dries's avatar
 
Dries committed
87
    watchdog("warning", "cloud: failed to syndicate from '". $site["name"] ."'". ($errstr ? ": $errstr" : ""));
Dries's avatar
 
Dries committed
88 89 90 91 92
  }
}


function cloud_form($edit = array()) {
Dries's avatar
 
Dries committed
93
  $period = array(900 => format_interval(900), 1800 => format_interval(1800), 3600 => format_interval(3600), 7200 => format_interval(7200), 10800 => format_interval(10800), 21600 => format_interval(21600), 32400 => format_interval(32400), 43200 => format_interval(43200), 64800 => format_interval(64800), 86400 => format_interval(86400), 172800 => format_interval(172800), 259200 => format_interval(259200), 604800 => format_interval(604800), 1209600 => format_interval(1209600), 2419200 => format_interval(2419200));
94
  $threshold = array(1 => "1 byte", 10 => "10 bytes", 20 => "20 bytes", 40 => "40 bytes", 60 => "60 bytes", 80 => "80 bytes", 100 => "100 bytes", 120 => "120 bytes", 140 => "140 bytes", 160 => "160 bytes", 320 => "320 bytes", 640 => "640 bytes");
Dries's avatar
 
Dries committed
95

Dries's avatar
 
Dries committed
96 97
  $form .= form_textfield("Site name", "name", $edit["name"], 50, 128, "The name of the web site you want to monitor for updates.");
  $form .= form_textfield("Site URL", "link", $edit["link"], 50, 255, "The URL of the web site you want to monitor for updates.");
natrak's avatar
natrak committed
98
  $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.");
Dries's avatar
 
Dries committed
99
  $form .= form_select("Update interval", "refresh", ($edit["refresh"] ? $edit["refresh"] : 3600), $period, "The refresh interval indicating how often you want to check this site for updates.  Requires crontab.");
Dries's avatar
 
Dries committed
100
  $form .= form_select("Change threshold", "threshold", ($edit["threshold"] ? $edit["threshold"] : 40), $threshold, "The number of bytes the site must have been modified before considered changed.");
Dries's avatar
 
Dries committed
101 102 103 104 105 106 107 108

  $form .= form_submit("Submit");

  if ($edit["sid"]) {
    $form .= form_submit("Delete");
    $form .= form_hidden("sid", $edit["sid"]);
  }

Dries's avatar
 
Dries committed
109
  return form($form);
Dries's avatar
 
Dries committed
110 111 112
}

function cloud_get_site($sid) {
Dries's avatar
 
Dries committed
113
  return db_fetch_array(db_query("SELECT * FROM site WHERE sid = %d", $sid));
Dries's avatar
 
Dries committed
114 115 116
}

function cloud_save($edit) {
Dries's avatar
 
Dries committed
117
  if ($edit["sid"] && $edit["name"]) {
Dries's avatar
 
Dries committed
118
    db_query("UPDATE site SET name = '%s', link = '%s', feed = '%s', refresh = %d, threshold = %d WHERE sid = %d", $edit["name"], $edit["link"], $edit["feed"], $edit["refresh"], $edit["threshold"], $edit["sid"]);
Dries's avatar
 
Dries committed
119 120
  }
  else if ($edit["sid"]) {
Dries's avatar
 
Dries committed
121
    db_query("DELETE FROM site WHERE sid = %d", $edit["sid"]);
Dries's avatar
 
Dries committed
122 123
  }
  else {
Dries's avatar
 
Dries committed
124
    db_query("INSERT INTO site (name, link, feed, refresh, threshold) VALUES  ('%s', '%s', '%s', %d, %d)", $edit["name"], $edit["link"], $edit["feed"], $edit["refresh"], $edit["threshold"]);
Dries's avatar
 
Dries committed
125 126 127 128
  }
}

function cloud_display() {
Dries's avatar
 
Dries committed
129
  $result = db_query("SELECT * FROM site ORDER BY name");
Dries's avatar
 
Dries committed
130

Dries's avatar
 
Dries committed
131
  $header = array(t("site"), t("last update"), array("data" => t("operations"), "colspan" => 2));
Dries's avatar
 
Dries committed
132
  while ($site = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
133
    $rows[] = array("<a href=\"$site->link\">$site->name</a>", ($site->timestamp ? format_interval(time() - $site->timestamp) ." ago" : "never"), l(t("edit site"), "admin/syndication/cloud/edit/$site->sid"), l(t("update site"), "admin/syndication/cloud/update/$site->sid"));
Dries's avatar
 
Dries committed
134 135
  }

Dries's avatar
 
Dries committed
136
  return table($header, $rows);
Dries's avatar
 
Dries committed
137 138
}

Dries's avatar
 
Dries committed
139
function cloud_list($limit = 10) {
Dries's avatar
 
Dries committed
140
  $result = db_query_range("SELECT * FROM site WHERE timestamp > ". (time() - 604800) ." ORDER BY timestamp DESC", 0, $limit);
Dries's avatar
 
Dries committed
141

Dries's avatar
 
Dries committed
142
  $hour = -1;
Dries's avatar
Dries committed
143
  $list = -1;
Dries's avatar
 
Dries committed
144 145
  $inlist = false;
  $output .= "<div class=\"item-list\">";
Dries's avatar
 
Dries committed
146
  while ($site = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
147 148
    if ($hour != floor((time() - $site->timestamp) / 3600)) {
      $hour = floor((time() - $site->timestamp) / 3600);
Dries's avatar
Dries committed
149
      if ($hour < 12) {
Dries's avatar
 
Dries committed
150 151 152 153
        if ($inlist) {
          $output .= "</ul>";
          $inlist = false;
        }
Kjartan's avatar
Kjartan committed
154
        if ($hour == 0) {
Dries's avatar
 
Dries committed
155 156 157 158
          $output .= t("Updated less than one hour ago:");
        }
        else {
          $output .= format_plural($hour, "Updated an hour ago:", "Updated %count hours ago:");
Kjartan's avatar
Kjartan committed
159
        }
Dries's avatar
Dries committed
160 161
      }
      else if ($list) {
Dries's avatar
 
Dries committed
162 163 164 165 166
        if ($inlist) {
          $output .= "</ul>";
          $inlist = false;
        }
        $output .= format_plural($hour, "Updated more than an hour ago:", "Updated more than %count hours ago:");
Dries's avatar
Dries committed
167 168
        $list = 0;
      }
Dries's avatar
 
Dries committed
169
    }
Dries's avatar
 
Dries committed
170 171 172 173 174
    if (!$inlist) {
      $output .= "<ul>";
      $inlist = true;
    }
    $output .= "<li><a href=\"$site->link\">$site->name</a></li>";
Dries's avatar
 
Dries committed
175
  }
Dries's avatar
 
Dries committed
176 177
  if ($inlist) $output .= "</ul>";
  $output .= "</div>";
Dries's avatar
 
Dries committed
178 179 180 181
  return $output;
}

function cloud_page() {
Dries's avatar
 
Dries committed
182

Dries's avatar
 
Dries committed
183 184

  if (user_access("access site cloud")) {
Dries's avatar
 
Dries committed
185 186 187
    theme("header");
    theme("box", t("Site cloud"), cloud_help("user") . cloud_list(100));
    theme("footer");
Dries's avatar
 
Dries committed
188 189 190
  }
}

Dries's avatar
 
Dries committed
191 192 193 194 195 196 197
function cloud_block($op = "list", $delta = 0) {
  if ($op == "list") {
    $blocks[0]["info"] = t("Site cloud");
    return $blocks;
  }
  else {
    $block["subject"] = t("Site cloud");
Dries's avatar
 
Dries committed
198
    $block["content"] = cloud_list(20) ."<div align=\"right\">". l(t("more"), "cloud", array("title" => t("Monitor other sites in the cloud."))) ."</div>";
Dries's avatar
 
Dries committed
199 200
    return $block;
  }
Dries's avatar
 
Dries committed
201 202 203
}

function cloud_admin() {
Dries's avatar
 
Dries committed
204 205
  $op = $_POST["op"];
  $edit = $_POST["edit"];
Dries's avatar
 
Dries committed
206 207

  if (empty($op)) {
Dries's avatar
 
Dries committed
208
    $op = arg(3);
Dries's avatar
 
Dries committed
209
  }
Dries's avatar
 
Dries committed
210 211 212 213 214 215 216

  if (user_access("administer site cloud")) {
    switch ($op) {
      case "add":
        print cloud_form();
        break;
      case "edit":
Dries's avatar
 
Dries committed
217
        print cloud_form(cloud_get_site(arg(4)));
Dries's avatar
 
Dries committed
218
        break;
Dries's avatar
 
Dries committed
219
      case "update":
Dries's avatar
 
Dries committed
220
        print status(cloud_update(cloud_get_site(arg(4))));
Dries's avatar
 
Dries committed
221 222
        print cloud_display();
        break;
Dries's avatar
 
Dries committed
223
     case "Delete":
Dries's avatar
 
Dries committed
224
        $edit["name"] = 0;
Dries's avatar
 
Dries committed
225 226 227 228 229 230 231 232 233 234 235 236 237
        // fall through:
      case "Submit":
        print status(cloud_save($edit));
        // fall through:
      default:
        print cloud_display();
    }
  }
  else {
    print message_access();
  }
}

Dries's avatar
 
Dries committed
238
?>