Newer
Older
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
<?
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) {
// open socket:
$url = parse_url($site[url]);
$fp = fsockopen($url[host], ($url[port] ? $url[port] : 80), $errno, $errstr, 15);
if ($fp) {
// fetch data:
fputs($fp, "GET $url[path]?$url[query] HTTP/1.0\nUser-Agent: ". variable_get(site_name, "drupal") ."\nHost: $url[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 url = '". check_input($site[url]) ."'");
}
}
}
else {
watchdog("error", "cloud: failed to syndicate from '$site[title]'");
}
}
function cloud_form($edit = array()) {
global $REQUEST_URI;
$form .= form_textfield("Title", "title", $edit["title"], 50, 64);
$form .= form_textfield("URL", "url", $edit["url"], 50, 64);
$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["title"]) {
db_query("UPDATE site SET title = '". check_input($edit["title"]) ."', url = '". check_input($edit["url"]) ."' 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 (title, url) VALUES ('". check_input($edit["title"]) ."', '". check_input($edit["url"]) ."')");
}
}
function cloud_display() {
$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";
$output .= " <tr><td><a href=\"". check_output($site->url) ."\">". check_output($site->title) ."</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";
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
}
$output .= "</table>\n";
return $output;
}
function cloud_list() {
$result = db_query("SELECT * FROM site ORDER BY timestamp DESC LIMIT 100");
while ($site = db_fetch_object($result)) {
if ($date != date("g A", $site->timestamp)) {
$date = date("g A", $site->timestamp);
$output .= "<p /><b>$date:</b>";
}
$output .= "<br />". format_url($site->url, $site->title);
}
return $output;
}
function cloud_page() {
global $theme;
if (user_access("access site cloud")) {
$theme->header();
$theme->box(t("Updated sites"), cloud_list());
$theme->footer();
}
}
function cloud_block() {
$block[0]["subject"] = t("Sites");
$block[0]["content"] = cloud_list();
$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[title] = 0;
// fall through:
case "Submit":
print status(cloud_save($edit));
// fall through:
default:
print cloud_display();
}
}
else {
print message_access();
}
}
?>