Blame
Date:
Sun Oct 2 04:00:24 2022 UTC
Message:
Daily backup
001
2021-12-17
jrmu
<?php if (!defined('PmWiki')) exit();
002
2021-12-17
jrmu
/* Copyright 2006-2018 Patrick R. Michaud (pmichaud@pobox.com)
003
2021-12-17
jrmu
This file is part of PmWiki; you can redistribute it and/or modify
004
2021-12-17
jrmu
it under the terms of the GNU General Public License as published
005
2021-12-17
jrmu
by the Free Software Foundation; either version 2 of the License, or
006
2021-12-17
jrmu
(at your option) any later version. See pmwiki.php for full details.
007
2021-12-17
jrmu
008
2021-12-17
jrmu
This script enables email notifications to be sent when posts
009
2021-12-17
jrmu
are made. It is included by default from the stdconfig.php
010
2021-12-17
jrmu
script if $EnableNotify is set to non-zero.
011
2021-12-17
jrmu
012
2021-12-17
jrmu
Once enabled, the addresses to receive messages are configured
013
2021-12-17
jrmu
via the Site.NotifyList page. A simple line in that page
014
2021-12-17
jrmu
such as
015
2021-12-17
jrmu
016
2021-12-17
jrmu
notify=somebody@example.com
017
2021-12-17
jrmu
018
2021-12-17
jrmu
will cause messages to be periodically sent to "somebody@example.com"
019
2021-12-17
jrmu
listing the pages that have changed on the site since the previous
020
2021-12-17
jrmu
message was sent. Multiple notify lines can be placed in the page,
021
2021-12-17
jrmu
and there are options to restrict the types of notifications
022
2021-12-17
jrmu
desired. For more details, see the PmWiki.Notify page in
023
2021-12-17
jrmu
the documentation.
024
2021-12-17
jrmu
025
2021-12-17
jrmu
Several variables set defaults for this script:
026
2021-12-17
jrmu
027
2021-12-17
jrmu
$NotifyFrom - return email address to use in message.
028
2021-12-17
jrmu
$NotifyDelay - number of seconds to wait before sending mail
029
2021-12-17
jrmu
after the first post.
030
2021-12-17
jrmu
$NotifySquelch - minimum number of seconds between sending email
031
2021-12-17
jrmu
messages to each address. Individual "notify=" lines in
032
2021-12-17
jrmu
Site.NotifyList can override this value via a custom "squelch="
033
2021-12-17
jrmu
parameter.
034
2021-12-17
jrmu
$NotifyFile - scratchpad file used to keep track of pending emails.
035
2021-12-17
jrmu
$NotifyListPageFmt - name of the NotifyList configuration page.
036
2021-12-17
jrmu
$NotifySubjectFmt - subject line for sent messages.
037
2021-12-17
jrmu
$NotifyBodyFmt - body of message to be sent. The string '$NotifyItems'
038
2021-12-17
jrmu
is replaced with the list of posts in the email.
039
2021-12-17
jrmu
$NotifyItemFmt - the format for each post to be included in a notification.
040
2021-12-17
jrmu
$NotifyTimeFmt - the format for dates and times ($PostTime)
041
2021-12-17
jrmu
in notification messages.
042
2021-12-17
jrmu
$NotifyHeaders - any additional message headers to be sent.
043
2021-12-17
jrmu
$NotifyParameters - any additional parameters to be passed to PHP's
044
2021-12-17
jrmu
mail() function.
045
2021-12-17
jrmu
046
2021-12-17
jrmu
Script maintained by Petko YOTOV www.pmwiki.org/petko
047
2021-12-17
jrmu
*/
048
2021-12-17
jrmu
049
2021-12-17
jrmu
SDV($NotifyDelay, 0);
050
2021-12-17
jrmu
SDV($NotifySquelch, 10800);
051
2021-12-17
jrmu
SDV($NotifyFile, "$WorkDir/.notifylist");
052
2021-12-17
jrmu
SDV($NotifyListPageFmt, '$SiteAdminGroup.NotifyList');
053
2021-12-17
jrmu
SDV($NotifySubjectFmt, '[$WikiTitle] recent notify posts');
054
2021-12-17
jrmu
SDV($NotifyBodyFmt,
055
2021-12-17
jrmu
"Recent \$WikiTitle posts:\n"
056
2021-12-17
jrmu
. " \$ScriptUrl/$[{\$SiteGroup}/AllRecentChanges]\n\n\$NotifyItems\n");
057
2021-12-17
jrmu
SDV($NotifyTimeFmt, $TimeFmt);
058
2021-12-17
jrmu
SDV($NotifyItemFmt,
059
2021-12-17
jrmu
' * {$FullName} . . . $PostTime by {$Author}');
060
2021-12-17
jrmu
SDV($NotifyHeaders, '');
061
2021-12-17
jrmu
SDV($NotifyParameters, '');
062
2021-12-17
jrmu
063
2021-12-17
jrmu
if (@$NotifyFrom)
064
2021-12-17
jrmu
$NotifyHeaders = "From: $NotifyFrom\r\n$NotifyHeaders";
065
2021-12-17
jrmu
066
2021-12-17
jrmu
$EditFunctions[] = 'PostNotify';
067
2021-12-17
jrmu
068
2021-12-17
jrmu
## check if we need to do notifications
069
2021-12-17
jrmu
if ($action != 'edit' && $action != 'postupload') NotifyCheck($pagename);
070
2021-12-17
jrmu
071
2021-12-17
jrmu
072
2021-12-17
jrmu
function NotifyCheck($pagename) {
073
2021-12-17
jrmu
global $NotifyFile, $Now, $LastModTime;
074
2021-12-17
jrmu
$nfp = @fopen($NotifyFile, 'r');
075
2021-12-17
jrmu
if (!$nfp) return;
076
2021-12-17
jrmu
$nextevent = fgets($nfp);
077
2021-12-17
jrmu
fclose($nfp);
078
2021-12-17
jrmu
if ($Now < $nextevent && $LastModTime < filemtime($NotifyFile)) return;
079
2021-12-17
jrmu
register_shutdown_function('NotifyUpdate', $pagename, getcwd());
080
2021-12-17
jrmu
}
081
2021-12-17
jrmu
082
2021-12-17
jrmu
083
2021-12-17
jrmu
function PostNotify($pagename, &$page, &$new) {
084
2021-12-17
jrmu
global $IsPagePosted;
085
2021-12-17
jrmu
if ($IsPagePosted)
086
2021-12-17
jrmu
register_shutdown_function('NotifyUpdate', $pagename, getcwd());
087
2021-12-17
jrmu
}
088
2021-12-17
jrmu
089
2021-12-17
jrmu
090
2021-12-17
jrmu
function NotifyUpdate($pagename, $dir='') {
091
2021-12-17
jrmu
global $NotifyList, $NotifyListPageFmt, $NotifyFile, $IsPagePosted, $IsUploadPosted,
092
2021-12-17
jrmu
$FmtV, $NotifyTimeFmt, $NotifyItemFmt, $SearchPatterns, $MailFunction,
093
2021-12-17
jrmu
$NotifySquelch, $NotifyDelay, $Now, $Charset, $EnableNotifySubjectEncode,
094
2021-12-17
jrmu
$NotifySubjectFmt, $NotifyBodyFmt, $NotifyHeaders, $NotifyParameters;
095
2021-12-17
jrmu
096
2021-12-17
jrmu
$abort = ignore_user_abort(true);
097
2021-12-17
jrmu
if ($dir) { flush(); chdir($dir); }
098
2021-12-17
jrmu
099
2021-12-17
jrmu
$GLOBALS['EnableRedirect'] = 0;
100
2021-12-17
jrmu
101
2021-12-17
jrmu
## Read in the current notify configuration
102
2021-12-17
jrmu
$pn = FmtPageName($NotifyListPageFmt, $pagename);
103
2021-12-17
jrmu
$npage = ReadPage($pn, READPAGE_CURRENT);
104
2021-12-17
jrmu
preg_match_all('/^[\s*:#->]*(notify[:=].*)/m', $npage['text'], $nlist);
105
2021-12-17
jrmu
$nlist = array_merge((array)@$NotifyList, (array)@$nlist[1]);
106
2021-12-17
jrmu
if (!$nlist) return;
107
2021-12-17
jrmu
108
2021-12-17
jrmu
## make sure other processes are locked out
109
2021-12-17
jrmu
Lock(2);
110
2021-12-17
jrmu
111
2021-12-17
jrmu
## let's load the current .notifylist table
112
2021-12-17
jrmu
$nfile = FmtPageName($NotifyFile, $pagename);
113
2021-12-17
jrmu
$nfp = @fopen($nfile, 'r');
114
2021-12-17
jrmu
if ($nfp) {
115
2021-12-17
jrmu
## get our current squelch and delay timestamps
116
2021-12-17
jrmu
clearstatcache();
117
2021-12-17
jrmu
$sz = filesize($nfile);
118
2021-12-17
jrmu
list($nextevent, $firstpost) = explode(' ', rtrim(fgets($nfp, $sz)));
119
2021-12-17
jrmu
## restore our notify array
120
2021-12-17
jrmu
$notify = unserialize(fgets($nfp, $sz));
121
2021-12-17
jrmu
fclose($nfp);
122
2021-12-17
jrmu
}
123
2021-12-17
jrmu
if (!is_array($notify)) $notify = array();
124
2021-12-17
jrmu
125
2021-12-17
jrmu
## if this is for a newly posted page, get its information
126
2021-12-17
jrmu
if ($IsPagePosted || $IsUploadPosted) {
127
2021-12-17
jrmu
$page = ReadPage($pagename, READPAGE_CURRENT);
128
2021-12-17
jrmu
$FmtV['$PostTime'] = strftime($NotifyTimeFmt, $Now);
129
2021-12-17
jrmu
$item = urlencode(FmtPageName($NotifyItemFmt, $pagename));
130
2021-12-17
jrmu
if ($firstpost < 1) $firstpost = $Now;
131
2021-12-17
jrmu
}
132
2021-12-17
jrmu
133
2021-12-17
jrmu
foreach($nlist as $n) {
134
2021-12-17
jrmu
$opt = ParseArgs($n);
135
2021-12-17
jrmu
$mailto = preg_split('/[\s,]+/', $opt['notify']);
136
2021-12-17
jrmu
if (!$mailto) continue;
137
2021-12-17
jrmu
if ($opt['squelch'])
138
2021-12-17
jrmu
foreach($mailto as $m) $squelch[$m] = $opt['squelch'];
139
2021-12-17
jrmu
if (!$IsPagePosted) continue;
140
2021-12-17
jrmu
if ($opt['link']) {
141
2021-12-17
jrmu
$link = MakePageName($pagename, $opt['link']);
142
2021-12-17
jrmu
if (!preg_match("/(^|,)$link(,|$)/i", $page['targets'])) continue;
143
2021-12-17
jrmu
}
144
2021-12-17
jrmu
$pats = @(array)$SearchPatterns[$opt['list']];
145
2021-12-17
jrmu
if ($opt['group']) $pats[] = FixGlob($opt['group'], '$1$2.*');
146
2021-12-17
jrmu
if ($opt['name']) $pats[] = FixGlob($opt['name'], '$1*.$2');
147
2021-12-17
jrmu
if ($pats && !MatchPageNames($pagename, $pats)) continue;
148
2021-12-17
jrmu
if ($opt['trail']) {
149
2021-12-17
jrmu
$trail = ReadTrail($pagename, $opt['trail']);
150
2021-12-17
jrmu
for ($i=0; $i<count($trail); $i++)
151
2021-12-17
jrmu
if ($trail[$i]['pagename'] == $pagename) break;
152
2021-12-17
jrmu
if ($i >= count($trail)) continue;
153
2021-12-17
jrmu
}
154
2021-12-17
jrmu
foreach($mailto as $m) { $notify[$m][] = $item; }
155
2021-12-17
jrmu
}
156
2021-12-17
jrmu
157
2021-12-17
jrmu
$nnow = time();
158
2021-12-17
jrmu
if ($nnow < $firstpost + $NotifyDelay)
159
2021-12-17
jrmu
$nextevent = $firstpost + $NotifyDelay;
160
2021-12-17
jrmu
else {
161
2021-12-17
jrmu
$firstpost = 0;
162
2021-12-17
jrmu
$nextevent = $nnow + 86400;
163
2021-12-17
jrmu
$mailto = array_keys($notify);
164
2021-12-17
jrmu
$subject = FmtPageName($NotifySubjectFmt, $pagename);
165
2021-12-17
jrmu
if(IsEnabled($EnableNotifySubjectEncode, 0)
166
2021-12-17
jrmu
&& preg_match("/[^\x20-\x7E]/", $subject))
167
2021-12-17
jrmu
$subject = strtoupper("=?$Charset?B?"). base64_encode($subject)."?=";
168
2021-12-17
jrmu
$body = FmtPageName($NotifyBodyFmt, $pagename);
169
2021-12-17
jrmu
foreach ($mailto as $m) {
170
2021-12-17
jrmu
$msquelch = @$notify[$m]['lastmail'] +
171
2021-12-17
jrmu
((@$squelch[$m]) ? $squelch[$m] : $NotifySquelch);
172
2021-12-17
jrmu
if ($nnow < $msquelch) {
173
2021-12-17
jrmu
if ($msquelch < $nextevent && count($notify[$m])>1)
174
2021-12-17
jrmu
$nextevent = $msquelch;
175
2021-12-17
jrmu
continue;
176
2021-12-17
jrmu
}
177
2021-12-17
jrmu
unset($notify[$m]['lastmail']);
178
2021-12-17
jrmu
if (!$notify[$m]) { unset($notify[$m]); continue; }
179
2021-12-17
jrmu
$mbody = str_replace('$NotifyItems',
180
2021-12-17
jrmu
urldecode(implode("\n", $notify[$m])), $body);
181
2021-12-17
jrmu
SDV($MailFunction, 'mail');
182
2021-12-17
jrmu
if ($NotifyParameters && !@ini_get('safe_mode'))
183
2021-12-17
jrmu
$MailFunction($m, $subject, $mbody, $NotifyHeaders, $NotifyParameters);
184
2021-12-17
jrmu
else
185
2021-12-17
jrmu
$MailFunction($m, $subject, $mbody, $NotifyHeaders);
186
2021-12-17
jrmu
$notify[$m] = array('lastmail' => $nnow);
187
2021-12-17
jrmu
}
188
2021-12-17
jrmu
}
189
2021-12-17
jrmu
190
2021-12-17
jrmu
## save the updated notify status
191
2021-12-17
jrmu
$nfp = @fopen($nfile, "w");
192
2021-12-17
jrmu
if ($nfp) {
193
2021-12-17
jrmu
fputs($nfp, "$nextevent $firstpost\n");
194
2021-12-17
jrmu
fputs($nfp, serialize($notify) . "\n");
195
2021-12-17
jrmu
fclose($nfp);
196
2021-12-17
jrmu
}
197
2021-12-17
jrmu
Lock(0);
198
2021-12-17
jrmu
return true;
199
2021-12-17
jrmu
}
200
2021-12-17
jrmu
IRCNow