-
Notifications
You must be signed in to change notification settings - Fork 5
/
tools.php
216 lines (184 loc) · 6.96 KB
/
tools.php
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
<?php
/**
* Internal helper functions for use with the DokuWiki Linkback Plugin.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Gina Haeussge <[email protected]>
* @link http://wiki.foosel.net/snippets/dokuwiki/linkback
*/
require_once (DOKU_PLUGIN . 'linkback/http.php');
class tools_plugin_linkback extends DokuWiki_Plugin {
/**
* Retrieves a favicon for the given page.
*/
function getFavicon($url, $page) {
$urlparts = parse_url($url);
$regex = '!<link rel="(shortcut )?icon" href="([^"]+)" ?/?>!';
if (preg_match($regex, $page, $match)) {
$icon = $match[2];
if (!preg_match("#^(http://)?[^/]+#i", $icon)) {
$icon = $urlparts['scheme'] . '://' . $urlparts['host'] . (($icon[0]) == '/' ? '' : '/') . $icon;
}
return $icon;
}
$icon = $urlparts['scheme'] . '://' . $urlparts['host'] . '/favicon.ico';
$http_client = new LinkbackHTTPClient();
$http_client->sendRequest($icon, array (), 'HEAD');
if ($http_client->status == 200)
return $icon;
else
return $this->getConf('favicon_default');
}
/**
* Retrieves $conf['range']kB of $url and returns headers and retrieved body.
*/
function getPage($url) {
$range = $this->getConf('range') * 1024;
$http_client = new LinkbackHTTPClient();
$http_client->headers['Range'] = 'bytes=0-' . $range;
$http_client->max_bodysize = $range;
$http_client->max_bodysize_limit = true;
$retval = $http_client->get($url, true);
return array (
'success' => $retval,
'headers' => $http_client->resp_headers,
'body' => $http_client->resp_body,
'error' => $http_client->error,
'status' => $http_client->status,
);
}
/**
* Sends a notify mail on new linkback
*
* @param string $ID id of the wiki page for which the
* linkback was received
* @param array $linkback data array of the new linkback
*
* @author Andreas Gohr <[email protected]>
* @author Esther Brunner <[email protected]>
* @author Gina Haeussge <[email protected]>
*/
function notify($ID, $linkback) {
global $conf;
if (!$conf['notify'])
return;
$to = $conf['notify'];
$text = io_readFile($this->localFN('subscribermail'));
$search = array (
'@PAGE@',
'@TITLE@',
'@DATE@',
'@URL@',
'@TEXT@',
'@UNSUBSCRIBE@',
'@DOKUWIKIURL@',
'@PAGEURL@',
);
$replace = array (
$ID,
$conf['title'],
strftime($conf['dformat'], $linkback['received']),
$linkback['url'],
$linkback['excerpt'],
wl($ID, 'do=unsubscribe', true, '&'),
DOKU_URL,
wl($ID, '', true),
);
$text = str_replace($search, $replace, $text);
$subject = '[' . $conf['title'] . '] ' . $this->getLang('mail_newlinkback');
$mail = new Mailer();
$mail->to($to);
$mail->subject($subject);
$mail->setBody($text);
$mail->send();
}
/**
* Adds an entry to the linkbacks changelog
*
* @author Esther Brunner <[email protected]>
* @author Ben Coburn <[email protected]>
* @author Gina Haeussge <[email protected]>
*/
function addLogEntry($date, $id, $type = 'cl', $summary = '', $extra = '') {
global $conf;
$changelog = $conf['metadir'] . '/_linkbacks.changes';
if (!$date) {
$date = time(); //use current time if none supplied
}
$remote = $_SERVER['REMOTE_ADDR'];
$user = $_SERVER['REMOTE_USER'];
$strip = array (
"\t",
"\n"
);
$logline = array (
'date' => $date,
'ip' => $remote,
'type' => str_replace($strip,
'',
$type
), 'id' => $id, 'user' => $user, 'sum' => str_replace($strip, '', $summary), 'extra' => str_replace($strip, '', $extra));
// add changelog line
$logline = implode("\t", $logline) . "\n";
io_saveFile($changelog, $logline, true); //global changelog cache
$this->_trimRecentCommentsLog($changelog);
}
/**
* Trims the recent comments cache to the last $conf['changes_days'] recent
* changes or $conf['recent'] items, which ever is larger.
* The trimming is only done once a day.
*
* @author Ben Coburn <[email protected]>
*/
function _trimRecentCommentsLog($changelog) {
global $conf;
if (@ file_exists($changelog) && (filectime($changelog) + 86400) < time() && !@ file_exists($changelog .
'_tmp')) {
io_lock($changelog);
$lines = file($changelog);
if (count($lines) < $conf['recent']) {
// nothing to trim
io_unlock($changelog);
return true;
}
io_saveFile($changelog . '_tmp', ''); // presave tmp as 2nd lock
$trim_time = time() - $conf['recent_days'] * 86400;
$out_lines = array ();
$linecount = count($lines);
for ($i = 0; $i < $linecount; $i++) {
$log = parseChangelogLine($lines[$i]);
if ($log === false)
continue; // discard junk
if ($log['date'] < $trim_time) {
$old_lines[$log['date'] . ".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions)
} else {
$out_lines[$log['date'] . ".$i"] = $lines[$i]; // definitely keep these lines
}
}
// sort the final result, it shouldn't be necessary,
// however the extra robustness in making the changelog cache self-correcting is worth it
ksort($out_lines);
$extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum
if ($extra > 0) {
ksort($old_lines);
$out_lines = array_merge(array_slice($old_lines, - $extra), $out_lines);
}
// save trimmed changelog
io_saveFile($changelog . '_tmp', implode('', $out_lines));
@ unlink($changelog);
if (!rename($changelog . '_tmp', $changelog)) {
// rename failed so try another way...
io_unlock($changelog);
io_saveFile($changelog, implode('', $out_lines));
@ unlink($changelog . '_tmp');
} else {
io_unlock($changelog);
}
return true;
}
}
function addProcessLogEntry($data) {
global $conf;
io_saveFile($conf['cachedir'].'/linkback.log',join("\n",$data)."\n\n",true);
}
}