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

Dries's avatar
 
Dries committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
function cloud_help($section = "admin/syndication/cloud/help") {
  $output = "";

  switch ($section) {
    case 'admin/help':
    case 'admin/cloud/help':
    case 'admin/syndication/cloud/help':
      $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>";
      $output .= strtr("<li>The site administrator enters names and URLs of the relevant pages on the %cloud-add page.</li>", array("%cloud-add" => l(t("content syndication &raquo; site cloud &raquo; add new site"), "admin/syndication/cloud/add") ));
      $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>";
      break;
    case 'cloud':
      $output .= "<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>";
      break;
    case 'admin/system/modules':
      $output = "Tracks other sites and displays last date changed.";
      break;
    case 'admin/syndication/cloud':
      $output = strtr("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 <B>Site bloud block</b> 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("block management"), "admin/block") ));
      break;
    case 'admin/syndication/cloud/add':
      $output = "Add a site to the site cloud. Don't forget the \"http://\" for your URLs.";
      break;
  }

  return t($output);
Dries's avatar
 
Dries committed
33 34
}

35
function cloud_system($field){
Dries's avatar
 
Dries committed
36 37 38 39
  $output = "";

  if ($field == "description") {$output = cloud_help("admin/system/modules");};
  return $output;
40 41
}

Dries's avatar
 
Dries committed
42
function cloud_cron() {
Dries's avatar
 
Dries committed
43
  $result = db_query("SELECT * FROM {site} WHERE checked = 0 OR checked + refresh < %d", time());
Dries's avatar
 
Dries committed
44 45 46 47 48 49 50 51 52 53 54

  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
55 56
  $links = array();

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

  if ($type == "admin" && user_access("administer site cloud")) {
Dries's avatar
 
Dries committed
62
    menu("admin/syndication", "content syndication", NULL, NULL, 5);
Dries's avatar
 
Dries committed
63 64
    menu("admin/syndication/cloud", "site cloud", "cloud_admin", cloud_help("admin/syndication/cloud"));
    menu("admin/syndication/cloud/add", "add new site", "cloud_admin", cloud_help("admin/syndication/cloud/add"));
Dries's avatar
 
Dries committed
65
    menu("admin/syndication/cloud/help", "help", "cloud_help", NULL, 9);
Dries's avatar
 
Dries committed
66 67
  }

Dries's avatar
 
Dries committed
68
  return $links;
Dries's avatar
 
Dries committed
69 70 71
}

function cloud_update($site) {
Dries's avatar
 
Dries committed
72 73 74 75 76

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

Kjartan's avatar
Kjartan committed
77
  if (!ereg("^http://|https://|ftp://", $site["link"])) {
Dries's avatar
 
Dries committed
78
    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
79 80
  }

Kjartan's avatar
Kjartan committed
81
  if (!ereg("^http://|https://|ftp://", $site["feed"])) {
Dries's avatar
 
Dries committed
82
    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
83 84 85 86 87 88
  }

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

Dries's avatar
 
Dries committed
89 90
  if ($fp = @fopen($site["feed"], "r")) {
    while (!feof($fp)) {
Dries's avatar
 
Dries committed
91 92
      $data .= fgets($fp, 128);
    }
Dries's avatar
 
Dries committed
93

94
    if (abs($site["size"] - strlen($data)) >= $site["threshold"]) {
Dries's avatar
 
Dries committed
95
      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
96 97
    }
    else {
Dries's avatar
 
Dries committed
98
      db_query("UPDATE {site} SET checked = %d WHERE link = '%s'", time(), $site["link"]);
Dries's avatar
 
Dries committed
99
    }
Dries's avatar
Dries committed
100 101

    fclose($fp);
Dries's avatar
 
Dries committed
102 103
  }
  else {
Dries's avatar
 
Dries committed
104
    watchdog("warning", "cloud: failed to syndicate from '". $site["name"] ."'". ($errstr ? ": $errstr" : ""));
Dries's avatar
 
Dries committed
105 106 107 108 109
  }
}


function cloud_form($edit = array()) {
Dries's avatar
 
Dries committed
110
  $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));
111
  $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
112

Dries's avatar
 
Dries committed
113 114
  $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
115
  $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
116
  $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
117
  $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
118 119 120 121 122 123 124 125

  $form .= form_submit("Submit");

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

Dries's avatar
 
Dries committed
126
  return form($form);
Dries's avatar
 
Dries committed
127 128 129
}

function cloud_get_site($sid) {
Dries's avatar
 
Dries committed
130
  return db_fetch_array(db_query("SELECT * FROM {site} WHERE sid = %d", $sid));
Dries's avatar
 
Dries committed
131 132 133
}

function cloud_save($edit) {
Dries's avatar
 
Dries committed
134
  if ($edit["sid"] && $edit["name"]) {
Dries's avatar
 
Dries committed
135
    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
136 137
  }
  else if ($edit["sid"]) {
Dries's avatar
 
Dries committed
138
    db_query("DELETE FROM {site} WHERE sid = %d", $edit["sid"]);
Dries's avatar
 
Dries committed
139 140
  }
  else {
Dries's avatar
 
Dries committed
141
    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
142 143 144 145
  }
}

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

Dries's avatar
 
Dries committed
148
  $header = array(t("site"), t("last update"), array("data" => t("operations"), "colspan" => 2));
Dries's avatar
 
Dries committed
149
  while ($site = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
150
    $rows[] = array("<a href=\"$site->link\">$site->name</a>", ($site->changed ? format_interval(time() - $site->changed) ." 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
151 152
  }

Dries's avatar
 
Dries committed
153
  return table($header, $rows);
Dries's avatar
 
Dries committed
154 155
}

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

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

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

Dries's avatar
 
Dries committed
200 201

  if (user_access("access site cloud")) {
Dries's avatar
 
Dries committed
202
    theme("header");
Dries's avatar
 
Dries committed
203
    theme("box", t("Site cloud"), cloud_help("cloud") . cloud_list(100));
Dries's avatar
 
Dries committed
204
    theme("footer");
Dries's avatar
 
Dries committed
205 206 207
  }
}

Dries's avatar
 
Dries committed
208 209 210 211 212 213 214
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
215
    $block["content"] = cloud_list(20) ."<div style=\"text-align: right;\">". l(t("more"), "cloud", array("title" => t("Monitor other sites in the cloud."))) ."</div>";
Dries's avatar
 
Dries committed
216 217
    return $block;
  }
Dries's avatar
 
Dries committed
218 219 220
}

function cloud_admin() {
Dries's avatar
 
Dries committed
221 222
  $op = $_POST["op"];
  $edit = $_POST["edit"];
Dries's avatar
 
Dries committed
223 224

  if (empty($op)) {
Dries's avatar
 
Dries committed
225
    $op = arg(3);
Dries's avatar
 
Dries committed
226
  }
Dries's avatar
 
Dries committed
227 228 229 230 231 232 233

  if (user_access("administer site cloud")) {
    switch ($op) {
      case "add":
        print cloud_form();
        break;
      case "edit":
Dries's avatar
 
Dries committed
234
        print cloud_form(cloud_get_site(arg(4)));
Dries's avatar
 
Dries committed
235
        break;
Dries's avatar
 
Dries committed
236
      case "update":
Dries's avatar
 
Dries committed
237
        print status(cloud_update(cloud_get_site(arg(4))));
Dries's avatar
 
Dries committed
238 239
        print cloud_display();
        break;
Dries's avatar
 
Dries committed
240
     case "Delete":
Dries's avatar
 
Dries committed
241
        $edit["name"] = 0;
Dries's avatar
 
Dries committed
242 243 244 245 246 247 248 249 250 251 252 253 254
        // fall through:
      case "Submit":
        print status(cloud_save($edit));
        // fall through:
      default:
        print cloud_display();
    }
  }
  else {
    print message_access();
  }
}

Dries's avatar
 
Dries committed
255
?>