123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php if (!defined('PmWiki')) exit();
- SDV($NotifyDelay, 0);
- SDV($NotifySquelch, 10800);
- SDV($NotifyFile, "$WorkDir/.notifylist");
- SDV($NotifyListPageFmt, '{$SiteGroup}.NotifyList');
- SDV($NotifySubjectFmt, '[$WikiTitle] recent notify posts');
- SDV($NotifyBodyFmt,
- "Recent \$WikiTitle posts:\n"
- . " \$ScriptUrl/$[{\$SiteGroup}/AllRecentChanges]\n\n\$NotifyItems\n");
- SDV($NotifyTimeFmt, $TimeFmt);
- SDV($NotifyItemFmt,
- ' * {$FullName} . . . $PostTime by {$LastModifiedBy}');
- SDV($NotifyHeaders, '');
- SDV($NotifyParameters, '');
- if (@$NotifyFrom)
- $NotifyHeaders = "From: $NotifyFrom\r\n$NotifyHeaders";
- $EditFunctions[] = 'PostNotify';
- if ($action != 'edit') NotifyCheck($pagename);
- function NotifyCheck($pagename) {
- global $NotifyFile, $Now, $LastModTime;
- $nfp = @fopen($NotifyFile, 'r');
- if (!$nfp) return;
- $nextevent = fgets($nfp);
- fclose($nfp);
- if ($Now < $nextevent && $LastModTime < filemtime($NotifyFile)) return;
- register_shutdown_function('flush');
- register_shutdown_function('NotifyUpdate', $pagename, getcwd());
- }
-
- function PostNotify($pagename, &$page, &$new) {
- global $IsPagePosted;
- if ($IsPagePosted) {
- register_shutdown_function('flush');
- register_shutdown_function('NotifyUpdate', $pagename, getcwd());
- }
- }
- function NotifyUpdate($pagename, $dir='') {
- global $NotifyList, $NotifyListPageFmt, $NotifyFile, $IsPagePosted,
- $FmtV, $NotifyTimeFmt, $NotifyItemFmt, $SearchPatterns,
- $NotifySquelch, $NotifyDelay, $Now,
- $NotifySubjectFmt, $NotifyBodyFmt, $NotifyHeaders, $NotifyParameters;
- $abort = ignore_user_abort(true);
- if ($dir) chdir($dir);
- $GLOBALS['EnableRedirect'] = 0;
-
- $pn = FmtPageName($NotifyListPageFmt, $pagename);
- $npage = ReadPage($pn, READPAGE_CURRENT);
- preg_match_all('/^[\s*:#->]*(notify[:=].*)/m', $npage['text'], $nlist);
- $nlist = array_merge((array)@$NotifyList, (array)@$nlist[1]);
- if (!$nlist) return;
-
- Lock(2);
-
- $nfile = FmtPageName($NotifyFile, $pagename);
- $nfp = @fopen($nfile, 'r');
- if ($nfp) {
-
- clearstatcache();
- $sz = filesize($nfile);
- list($nextevent, $firstpost) = explode(' ', rtrim(fgets($nfp, $sz)));
-
- $notify = unserialize(fgets($nfp, $sz));
- fclose($nfp);
- }
- if (!is_array($notify)) $notify = array();
-
- if ($IsPagePosted) {
- $page = ReadPage($pagename, READPAGE_CURRENT);
- $FmtV['$PostTime'] = strftime($NotifyTimeFmt, $Now);
- $item = urlencode(FmtPageName($NotifyItemFmt, $pagename));
- if ($firstpost < 1) $firstpost = $Now;
- }
- foreach($nlist as $n) {
- $opt = ParseArgs($n);
- $mailto = preg_split('/[\s,]+/', $opt['notify']);
- if (!$mailto) continue;
- if ($opt['squelch'])
- foreach($mailto as $m) $squelch[$m] = $opt['squelch'];
- if (!$IsPagePosted) continue;
- if ($opt['link']) {
- $link = MakePageName($pagename, $opt['link']);
- if (!preg_match("/(^|,)$link(,|$)/i", $page['targets'])) continue;
- }
- $pats = @(array)$SearchPatterns[$opt['list']];
- if ($opt['group']) $pats[] = FixGlob($opt['group'], '$1$2.*');
- if ($opt['name']) $pats[] = FixGlob($opt['name'], '$1*.$2');
- if ($pats && !MatchPageNames($pagename, $pats)) continue;
- if ($opt['trail']) {
- $trail = ReadTrail($pagename, $opt['trail']);
- for ($i=0; $i<count($trail); $i++)
- if ($trail[$i]['pagename'] == $pagename) break;
- if ($i >= count($trail)) continue;
- }
- foreach($mailto as $m) { $notify[$m][] = $item; }
- }
- $nnow = time();
- if ($nnow < $firstpost + $NotifyDelay)
- $nextevent = $firstpost + $NotifyDelay;
- else {
- $firstpost = 0;
- $nextevent = $nnow + 86400;
- $mailto = array_keys($notify);
- $subject = FmtPageName($NotifySubjectFmt, $pagename);
- $body = FmtPageName($NotifyBodyFmt, $pagename);
- foreach ($mailto as $m) {
- $msquelch = @$notify[$m]['lastmail'] +
- ((@$squelch[$m]) ? $squelch[$m] : $NotifySquelch);
- if ($nnow < $msquelch) {
- if ($msquelch < $nextevent && count($notify[$m])>1)
- $nextevent = $msquelch;
- continue;
- }
- unset($notify[$m]['lastmail']);
- if (!$notify[$m]) { unset($notify[$m]); continue; }
- $mbody = str_replace('$NotifyItems',
- urldecode(implode("\n", $notify[$m])), $body);
- mail($m, $subject, $mbody, $NotifyHeaders, $NotifyParameters);
- $notify[$m] = array('lastmail' => $nnow);
- }
- }
-
- $nfp = @fopen($nfile, "w");
- if ($nfp) {
- fputs($nfp, "$nextevent $firstpost\n");
- fputs($nfp, serialize($notify) . "\n");
- fclose($nfp);
- }
- Lock(0);
- return true;
- }
|