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

Dries's avatar
 
Dries committed
4
function cloud_help($section = "admin/help#cloud") {
Dries's avatar
 
Dries committed
5 6 7
  $output = "";

  switch ($section) {
Dries's avatar
 
Dries committed
8
    case 'admin/help#cloud':
Dries's avatar
 
Dries committed
9 10
      $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:";
      $output .= "<ul>";
Dries's avatar
 
Dries committed
11
      $output .= "<li>The site administrator enters names and URLs of the relevant pages on the %cloud-add page.</li>";
Dries's avatar
 
Dries committed
12 13 14
      $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 an <i>x</i>-byte difference since the last time it checked, where <i>x</i> is a configuration option.)</li>";
      $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
15
      $output = t($output, array("%cloud-add" => l(t("content") ." &raquo; ". t("syndication") ." &raquo; ". t("site cloud") ." &raquo; ". t("add new site"), "admin/node/syndication/cloud/add")));
Dries's avatar
 
Dries committed
16 17
      break;
    case 'cloud':
Dries's avatar
 
Dries committed
18
      $output .= t("<p>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 the cloud.</p>");
Dries's avatar
 
Dries committed
19
      break;
Dries's avatar
 
Dries committed
20 21
    case 'admin/system/modules#description':
      $output = t("Tracks other sites and displays last date changed.");
Dries's avatar
 
Dries committed
22
      break;
Dries's avatar
 
Dries committed
23
    case 'admin/node/syndication/cloud':
Dries's avatar
 
Dries committed
24
      $output = t("The cloud monitor tracks or crawls other interesting web sites and displays their last modification dates. Visitors to this site learn about other relevant sites and can easily see if there is new content. To get this working you <b>must</b> setup cron support. To get the site cloud block to display you must turn on the <i>site cloud</i> block in %block. To go to a monitored site, click on the site name, to immediately load and/or update the record for a site, click on \"update site\", to delete a site go to \"edit site\". ", array("%block" => l(t("administer") ." &raquo; ". t("configuration") ." &raquo; ". t("blocks"), "admin/block")));
Dries's avatar
 
Dries committed
25
      break;
Dries's avatar
 
Dries committed
26
    case 'admin/node/syndication/cloud/add':
Dries's avatar
 
Dries committed
27
      $output = t("Add a site to the site cloud. Don't forget the \"http://\" for your URLs.");
Dries's avatar
 
Dries committed
28 29 30 31
      break;
  }

  return $output;
32 33
}

Dries's avatar
 
Dries committed
34
function cloud_cron() {
Dries's avatar
 
Dries committed
35
  $result = db_query("SELECT * FROM {site} WHERE checked = 0 OR checked + refresh < %d", time());
Dries's avatar
 
Dries committed
36 37 38 39 40 41 42 43 44 45 46

  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
47 48
  $links = array();

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

Dries's avatar
 
Dries committed
53 54
  if ($type == "system") {
    if (user_access("administer site cloud")) {
Dries's avatar
 
Dries committed
55 56 57
      menu("admin/node/syndication/cloud", t("site cloud"), "cloud_admin");
      menu("admin/node/syndication/cloud/add", t("add new site"), "cloud_admin");
      menu("admin/node/syndication/cloud/help", t("help"), "cloud_help", 9);
Dries's avatar
 
Dries committed
58
    }
Dries's avatar
 
Dries committed
59 60
  }

Dries's avatar
 
Dries committed
61
  return $links;
Dries's avatar
 
Dries committed
62 63 64
}

function cloud_update($site) {
Dries's avatar
 
Dries committed
65 66 67 68 69

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

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

Kjartan's avatar
Kjartan committed
74
  if (!ereg("^http://|https://|ftp://", $site["feed"])) {
Dries's avatar
 
Dries committed
75
    watchdog("warning", "cloud: invalid or missing URL to monitor for '". $site["name"] ."'", l(t("edit site"), "admin/node/syndication/cloud/edit/". $site["sid"]));
Dries's avatar
 
Dries committed
76 77 78 79 80 81
  }

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

Dries's avatar
 
Dries committed
82 83
  if ($fp = @fopen($site["feed"], "r")) {
    while (!feof($fp)) {
Dries's avatar
 
Dries committed
84 85
      $data .= fgets($fp, 128);
    }
Dries's avatar
 
Dries committed
86

87
    if (abs($site["size"] - strlen($data)) >= $site["threshold"]) {
Dries's avatar
 
Dries committed
88
      db_query("UPDATE {site} SET size = %d, changed = %d, checked = %d WHERE link = '%s'", strlen($data), time(), time(), $site["link"]);
Dries's avatar
 
Dries committed
89 90
    }
    else {
Dries's avatar
 
Dries committed
91
      db_query("UPDATE {site} SET checked = %d WHERE link = '%s'", time(), $site["link"]);
Dries's avatar
 
Dries committed
92
    }
Dries's avatar
Dries committed
93 94

    fclose($fp);
Dries's avatar
 
Dries committed
95 96
  }
  else {
Dries's avatar
 
Dries committed
97
    watchdog("warning", "cloud: failed to syndicate from '". $site["name"] ."'". ($errstr ? ": $errstr" : ""));
Dries's avatar
 
Dries committed
98 99 100 101 102
  }
}


function cloud_form($edit = array()) {
Dries's avatar
 
Dries committed
103
  $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));
104
  $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
105

Dries's avatar
 
Dries committed
106 107
  $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
108
  $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
109
  $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
110
  $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
111 112 113 114 115 116 117 118

  $form .= form_submit("Submit");

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

Dries's avatar
 
Dries committed
119
  return form($form);
Dries's avatar
 
Dries committed
120 121 122
}

function cloud_get_site($sid) {
Dries's avatar
 
Dries committed
123
  return db_fetch_array(db_query("SELECT * FROM {site} WHERE sid = %d", $sid));
Dries's avatar
 
Dries committed
124 125 126
}

function cloud_save($edit) {
Dries's avatar
 
Dries committed
127
  if ($edit["sid"] && $edit["name"]) {
Dries's avatar
 
Dries committed
128
    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
129 130
  }
  else if ($edit["sid"]) {
Dries's avatar
 
Dries committed
131
    db_query("DELETE FROM {site} WHERE sid = %d", $edit["sid"]);
Dries's avatar
 
Dries committed
132 133
  }
  else {
Dries's avatar
 
Dries committed
134
    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
135 136 137 138
  }
}

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

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

Dries's avatar
 
Dries committed
146
  return table($header, $rows);
Dries's avatar
 
Dries committed
147 148
}

Dries's avatar
 
Dries committed
149
function cloud_list($limit = 10) {
Dries's avatar
 
Dries committed
150
  $result = db_query_range("SELECT * FROM {site} WHERE changed > ". (time() - 604800) ." ORDER BY changed DESC", 0, $limit);
Dries's avatar
 
Dries committed
151

Dries's avatar
 
Dries committed
152
  $hour = -1;
Dries's avatar
Dries committed
153
  $list = -1;
Dries's avatar
 
Dries committed
154 155
  $inlist = false;
  $output .= "<div class=\"item-list\">";
Dries's avatar
 
Dries committed
156
  while ($site = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
157 158
    if ($hour != floor((time() - $site->changed) / 3600)) {
      $hour = floor((time() - $site->changed) / 3600);
Dries's avatar
Dries committed
159
      if ($hour < 12) {
Dries's avatar
 
Dries committed
160 161 162 163
        if ($inlist) {
          $output .= "</ul>";
          $inlist = false;
        }
Kjartan's avatar
Kjartan committed
164
        if ($hour == 0) {
Dries's avatar
 
Dries committed
165 166 167 168
          $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
169
        }
Dries's avatar
Dries committed
170 171
      }
      else if ($list) {
Dries's avatar
 
Dries committed
172 173 174 175 176
        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
177 178
        $list = 0;
      }
Dries's avatar
 
Dries committed
179
    }
Dries's avatar
 
Dries committed
180 181 182 183 184
    if (!$inlist) {
      $output .= "<ul>";
      $inlist = true;
    }
    $output .= "<li><a href=\"$site->link\">$site->name</a></li>";
Dries's avatar
 
Dries committed
185
  }
Dries's avatar
 
Dries committed
186 187
  if ($inlist) $output .= "</ul>";
  $output .= "</div>";
Dries's avatar
 
Dries committed
188 189 190 191
  return $output;
}

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

Dries's avatar
 
Dries committed
193 194

  if (user_access("access site cloud")) {
Dries's avatar
 
Dries committed
195 196 197
    print theme("header");
    print theme("box", t("Site cloud"), cloud_help("cloud") . cloud_list(100));
    print theme("footer");
Dries's avatar
 
Dries committed
198 199 200
  }
}

Dries's avatar
 
Dries committed
201 202 203 204 205 206 207
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
208
    $block["content"] = cloud_list(20) ."<div class=\"more-link\">". l(t("more"), "cloud", array("title" => t("Monitor other sites in the cloud."))) ."</div>";
Dries's avatar
 
Dries committed
209 210
    return $block;
  }
Dries's avatar
 
Dries committed
211 212 213
}

function cloud_admin() {
Dries's avatar
 
Dries committed
214 215
  $op = $_POST["op"];
  $edit = $_POST["edit"];
Dries's avatar
 
Dries committed
216 217

  if (empty($op)) {
Dries's avatar
 
Dries committed
218
    $op = arg(4);
Dries's avatar
 
Dries committed
219
  }
Dries's avatar
 
Dries committed
220 221 222 223

  if (user_access("administer site cloud")) {
    switch ($op) {
      case "add":
Dries's avatar
 
Dries committed
224
        $output = cloud_form();
Dries's avatar
 
Dries committed
225 226
        break;
      case "edit":
Dries's avatar
 
Dries committed
227
        $output = cloud_form(cloud_get_site(arg(5)));
Dries's avatar
 
Dries committed
228
        break;
Dries's avatar
 
Dries committed
229
      case "update":
Dries's avatar
 
Dries committed
230
        $output = status(cloud_update(cloud_get_site(arg(5))));
Dries's avatar
 
Dries committed
231
        $output .= cloud_display();
Dries's avatar
 
Dries committed
232
        break;
Dries's avatar
 
Dries committed
233
     case "Delete":
Dries's avatar
 
Dries committed
234
        $edit["name"] = 0;
Dries's avatar
 
Dries committed
235 236
        // fall through:
      case "Submit":
Dries's avatar
 
Dries committed
237
        $output = status(cloud_save($edit));
Dries's avatar
 
Dries committed
238 239
        // fall through:
      default:
Dries's avatar
 
Dries committed
240
        $output .= cloud_display();
Dries's avatar
 
Dries committed
241
    }
Dries's avatar
 
Dries committed
242
    return $output;
Dries's avatar
 
Dries committed
243 244
  }
  else {
Dries's avatar
 
Dries committed
245
    return message_access();
Dries's avatar
 
Dries committed
246 247 248
  }
}

Dries's avatar
 
Dries committed
249
?>