summaryrefslogtreecommitdiff
path: root/modules/headline.module
blob: 1c6b417433c57e353dcf72b9334b015dbbf13aa3 (plain)
1
2
3
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php

include_once "modules/backend.class";

function headline_help() {
 ?>
  <P>Drupal's headline module both imports and exports RDF/RSS headlines.</P>
  <P>A lot of news-oriented websites are now publishing news (headlines) and making their content available through XML, RSS and RDF backend files.  They syndicate free content and allow retrieval and further transmission, aggregation, and online publication. In its current state, drupal's headline module supports RDF and RSS backends.</P>
  <P>RSS was originally developed by Netscape to allow adding news channels to "My Netscape" sites, but it has since become adopted as the <I>de facto</I> net standard for distributing headlines and brief dynamic texts.</P>
  <P>The headline module goes out to a list of configured news sites once an hour or so (driven by cron), downloads new RSS/RDF data and makes it available to your visitors.  In addition, your headlines are exported as well and can be retrieved by other sites from <CODE><?php echo path_uri(); ?>export/headlines.rdf</CODE>.</P>
 <?php
}

function headline_conf_options() {
  $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));
  $output .= form_select(t("Update interval"), "headline_cron_time" , variable_get("headline_cron_time", 86400), $period, t("The update interval indicating how often you want to update your headline channels.  Requires crontab."));
  return $output;
}


function headline_cron() {
  if (time() - variable_get("headline_cron_last", 0) > variable_get("headline_cron_time", time())) {
    variable_set("headline_cron_last", time());

    $result = db_query("SELECT * FROM channel");
    while ($channel = db_fetch_object($result)) {
      $backend = new Backend($channel->id);
    }
  }
}

function headline_blocks() {
  global $theme;

  // Get channel info:
  $result = db_query("SELECT * FROM channel ORDER BY id");

  $theme->header();

  print "<TABLE BORDER=\"0\">\n";
  while ($channel = db_fetch_object($result)) {
    if ($state % 3 == 0) print " <TR>\n";

    print "  <TD ALIGN=\"center\" VALIGN=\"top\" WIDTH=\"33%\">\n";

    // Load backend from database:
    $backend = new backend($channel->id);

    if ($backend->headlines) {
      unset($content);
      foreach ($backend->headlines as $headline) $content .= "<LI>$headline</LI>\n";
    }
    else {
      $content = "no headlines available\n";
    }

    // Print backend box to screen:
    $theme->box($backend->site, $content);
    print " </TD>\n";

    if ($state % 3 == 2) print " </TR>\n";

    $state += 1;
  }
  print "</TABLE>\n";

  $theme->footer();
}

function headline_page() {
  global $type;

  switch($type) {
    case "rdf":
      headline_rdf();
      break;
    default:
      headline_blocks();
  }
}


function headline_block() {
  $result = db_query("SELECT * FROM channel");
  while ($channel = db_fetch_object($result)) {
    $backend = new Backend($channel->id);

    if ($backend->headlines) {
      unset($content);
      foreach ($backend->headlines as $headline) {
        $content .= "<LI>$headline</LI>\n";
      }
    }
    else {
      $content = "no headlines available";
    }

    $blocks[$channel->id]["subject"] = $backend->site;
    $blocks[$channel->id]["content"] = $content;
    $blocks[$channel->id]["info"] = "$backend->site headlines";
    $blocks[$channel->id]["link"] = $backend->url;
  }
  return $blocks;
}

function headline_admin_display() {
  global $theme;

  // Get channel info:
  $result = db_query("SELECT * FROM channel ORDER BY id");

  $output .= "<TABLE BORDER=\"1\" CELLSPADDING=\"2\" CELLSPACING=\"2\">\n";
  $output .= " <TH>site</TH><TH>contact</TH><TH>last update</TH><TH COLSPAN=\"3\">operations</TH></TR>\n";

  while ($channel = db_fetch_object($result)) {
    // Load backend from database:
    $backend = new backend($channel->id);

    $output .= "<TR>\n";
    $output .= " <TD>". format_url($backend->url, $backend->site) ."</TD>\n";
    $output .= " <TD>". format_email($backend->contact) ."</TD>\n";
    $output .= " <TD ALIGN=\"center\">". ($backend->timestamp == 1 ? "failed" : format_interval(time() - $backend->timestamp) ." ago") ."</TD>\n";
    $output .= " <TD ALIGN=\"center\"><A HREF=\"admin.php?mod=headline&op=refresh&id=$backend->id\">refresh</A></TD>\n";
    $output .= " <TD ALIGN=\"center\"><A HREF=\"admin.php?mod=headline&op=edit&id=$backend->id\">edit</A></TD>\n";
    $output .= " <TD ALIGN=\"center\"><A HREF=\"admin.php?mod=headline&op=delete&id=$backend->id\">delete</A></TD>\n";
    $output .= "</TR>\n";
  }

  $output .= "</TABLE>\n";

  print $output;
}

function headline_admin_add() {
  $output .= " <FORM ACTION=\"admin.php?mod=headline\" METHOD=\"post\">\n";
  $output .= "  <P>\n";
  $output .= "   <B>Site name:</B><BR>\n";
  $output .= "   <INPUT TYPE=\"text\" NAME=\"site\" SIZE=\"50\">\n";
  $output .= "  </P>\n";
  $output .= "  <P>\n";
  $output .= "   <B>URL:</B><BR>\n";
  $output .= "   <INPUT TYPE=\"text\" NAME=\"url\" SIZE=\"50\">\n";
  $output .= "  </P>\n";
  $output .= "  <P>\n";
  $output .= "   <B>Backend file:</B><BR>\n";
  $output .= "   <INPUT TYPE=\"text\" NAME=\"backend\" SIZE=\"50\">\n";
  $output .= "  </P>\n";
  $output .= "  <P>\n";
  $output .= "   <B>Contact information:</B><BR>\n";
  $output .= "   <INPUT TYPE=\"text\" NAME=\"contact\" SIZE=\"50\">\n";
  $output .= "  </P>\n";
  $output .= "  <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Add backend\">\n";
  $output .= " </FORM>\n";

  print $output;
}

function headline_admin_edit($id) {
  $result = db_query("SELECT * FROM channel WHERE id='$id' ORDER BY id");

  if ($channel = db_fetch_object($result)) {
    $output .= " <FORM ACTION=\"admin.php?mod=headline\" METHOD=\"post\">\n";
    $output .= "  <INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\">\n";
    $output .= "  <P>\n";
    $output .= "   <B>Site name:</B><BR>\n";
    $output .= "   <INPUT TYPE=\"text\" NAME=\"site\" SIZE=\"50\" VALUE=\"$channel->site\">\n";
    $output .= "  </P>\n";
    $output .= "  <P>\n";
    $output .= "   <B>URL:</B><BR>\n";
    $output .= "   <INPUT TYPE=\"text\" NAME=\"url\" SIZE=\"50\" VALUE=\"$channel->url\">\n";
    $output .= "  </P>\n";
    $output .= "  <P>\n";
    $output .= "   <B>Backend file:</B><BR>\n";
    $output .= "   <INPUT TYPE=\"text\" NAME=\"backend\" SIZE=\"50\" VALUE=\"$channel->file\">\n";
    $output .= "  </P>\n";
    $output .= "  <P>\n";
    $output .= "   <B>Contact information:</B><BR>\n";
    $output .= "   <INPUT TYPE=\"text\" NAME=\"contact\" SIZE=\"50\" VALUE=\"$channel->contact\">\n";
    $output .= "  </P>\n";
    $output .= "  <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Save backend\">\n";
    $output .= " </FORM>\n";
  } else {
    $output .= "Invalid Channel ID.\n";
  }
  print $output;
}

function headline_admin_rehash() {
  module_rehash_blocks("headline");
}

function headline_admin() {
  global $op, $id, $site, $url, $backend, $contact;

  print "<SMALL><A HREF=\"admin.php?mod=headline&op=add\">add new channel</A> | <A HREF=\"admin.php?mod=headline\">overview</A> | <A HREF=\"admin.php?mod=headline&op=help\">help</A></SMALL><HR>";

  switch($op) {
    case "add":
      headline_admin_add();
      break;
    case "edit":
      headline_admin_edit(check_input($id));
      break;
    case "delete":
      $channel = new backend($id);
      $channel->delete();
      headline_admin_rehash();
      headline_admin_display();
      break;
    case "help":
      headline_help();
      break;
    case "refresh":
      $channel = new backend($id);
      $channel->refresh();
      headline_admin_display();
      break;
    case "Add backend":
      $channel = new backend(check_input($id), check_input($site), check_input($url), check_input($backend), check_input($contact));
      $channel->add();
      headline_admin_rehash();
      headline_admin_display();
      break;
    case "Save backend":
      $channel = new backend(check_input($id));
      $channel->site = $site;
      $channel->url = $url;
      $channel->file = $backend;
      $channel->contact = $contact;
      $channel->save();
      headline_admin_rehash();
      // fall through:
    default:
      headline_admin_display();
  }
}

function headline_export_rdf() {
  global $status;

  $uri = substr(path_uri(), 0, strlen(path_uri()) - strlen("export/"));

  header("Content-Type: text/plain");

  print "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
  print "<rdf:RDF\n";
  print " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n";
  print " xmlns=\"http://my.netscape.com/rdf/simple/0.9/\">\n";

  print "<channel>\n";
  print " <title>". variable_get(site_name, "drupal") ."</title>\n";
  print " <link>". $uri ."</link>\n";
  print " <description>". variable_get(site_name, "drupal") ."</description>\n";
  print "</channel>\n";

  $result = db_query("SELECT * FROM node WHERE promote = '1' AND status = '$status[posted]' ORDER BY timestamp DESC LIMIT 10");

  while ($node = db_fetch_object($result)) {
    print "<item>\n";
    print " <title>". check_export($node->title) ."</title>\n";
    print " <link>". $uri ."node.php?id=$node->nid</link>\n";
    print "</item>\n";
  }

  print "</rdf:RDF>\n";
}

function headline_export_rss() {
  global $status;

  $uri = substr(path_uri(), 0, strlen(path_uri()) - strlen("export/"));

  header("Content-Type: text/plain");

  print "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
  print "<rdf:RDF\n";

  print "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n";
  print "xmlns=\"http://purl.org/rss/1.0/\">\n\n";

  print "<channel rdf:about=\"". $uri ."export/headlinesRSS10.rdf\">\n";
  print "   <title>". variable_get(site_name, "drupal") ."</title>\n";
  print "   <link>". $uri ."</link>\n";
  print "   <description>". variable_get(site_name, "drupal") ."</description>\n";

  print "  <items>\n";
  print "    <rdf:Seq>\n";

  $result = db_query("SELECT * FROM node WHERE promote = '1' AND status = '$status[posted]' ORDER BY timestamp DESC LIMIT 10");

  while ($node = db_fetch_object($result)) {
    print "      <rdf:li resource=\"". $uri ."node.php?id=$node->nid\" />\n";
  }

  print "    </rdf:Seq>\n";
  print "  </items>\n";
  print "</channel>\n\n";

  $result = db_query("SELECT * FROM node WHERE promote = '1' AND status = '$status[posted]' ORDER BY timestamp DESC LIMIT 10");

  while ($node = db_fetch_object($result)) {
    print "<item rdf:about=\"". $uri ."node.php?id=$node->nid\">\n";
    print "  <title>". check_export($node->title) ."</title>\n";
    print "  <link>". $uri ."node.php?id=$node->nid</link>\n";

    if ($node->abstract) print "  <description>". check_output($node->abstract, 1) ."</description>\n";
    if ($node->body) print "  <description>". check_output($node->body, 1) ."</description>\n";

    print "</item>\n";
  }

  print "</rdf:RDF>\n";
}

function headline_export($uri) {
  switch ($uri[2]) {
    case "headlines.rss":
      headline_export_rss();
      break;
    case "headlines.rdf":
    case "default":
      headline_export_rdf();
  }
}

?>