Simple Groupware PHP Cross Reference Groupware Applications

Source: /src/lib/pmwiki/scripts/notify.php - 195 lines - 7456 bytes - Summary - Text - Print

   1  <?php if (!defined('PmWiki')) exit();
   2  /*  Copyright 2006-2009 Patrick R. Michaud (pmichaud@pobox.com)
   3      This file is part of PmWiki; you can redistribute it and/or modify
   4      it under the terms of the GNU General Public License as published
   5      by the Free Software Foundation; either version 2 of the License, or
   6      (at your option) any later version.  See pmwiki.php for full details.
   7  
   8      This script enables email notifications to be sent when posts
   9      are made.  It is included by default from the stdconfig.php 
  10      script if $EnableNotify is set to non-zero.
  11  
  12      Once enabled, the addresses to receive messages are configured
  13      via the Site.NotifyList page.  A simple line in that page
  14      such as 
  15  
  16          notify=somebody@example.com
  17  
  18      will cause messages to be periodically sent to "somebody@example.com"
  19      listing the pages that have changed on the site since the previous
  20      message was sent.  Multiple notify lines can be placed in the page,
  21      and there are options to restrict the types of notifications
  22      desired.  For more details, see the PmWiki.Notify page in
  23      the documentation.
  24  
  25      Several variables set defaults for this script:
  26  
  27      $NotifyFrom - return email address to use in message.
  28      $NotifyDelay - number of seconds to wait before sending mail
  29          after the first post.
  30      $NotifySquelch - minimum number of seconds between sending email
  31          messages to each address.  Individual "notify=" lines in
  32          Site.NotifyList can override this value via a custom "squelch="
  33          parameter.
  34      $NotifyFile - scratchpad file used to keep track of pending emails.
  35      $NotifyListPageFmt - name of the NotifyList configuration page.
  36      $NotifySubjectFmt - subject line for sent messages.
  37      $NotifyBodyFmt - body of message to be sent.  The string '$NotifyItems'
  38          is replaced with the list of posts in the email.
  39      $NotifyItemFmt - the format for each post to be included in a notification.
  40      $NotifyTimeFmt - the format for dates and times ($PostTime) 
  41          in notification messages.
  42      $NotifyHeaders - any additional message headers to be sent.
  43      $NotifyParameters - any additional parameters to be passed to PHP's
  44          mail() function.
  45  */
  46  
  47  SDV($NotifyDelay, 0);
  48  SDV($NotifySquelch, 10800);
  49  SDV($NotifyFile, "$WorkDir/.notifylist");
  50  SDV($NotifyListPageFmt, '$SiteAdminGroup.NotifyList');
  51  SDV($NotifySubjectFmt, '[$WikiTitle] recent notify posts');
  52  SDV($NotifyBodyFmt, 
  53    "Recent \$WikiTitle posts:\n" 
  54    . "  \$ScriptUrl/$[{\$SiteGroup}/AllRecentChanges]\n\n\$NotifyItems\n");
  55  SDV($NotifyTimeFmt, $TimeFmt);
  56  SDV($NotifyItemFmt, 
  57    ' * {$FullName} . . . $PostTime by {$LastModifiedBy}');
  58  SDV($NotifyHeaders, '');
  59  SDV($NotifyParameters, '');
  60  
  61  if (@$NotifyFrom)
  62    $NotifyHeaders = "From: $NotifyFrom\r\n$NotifyHeaders";
  63  
  64  $EditFunctions[] = 'PostNotify';
  65  
  66  ##   check if we need to do notifications
  67  if ($action != 'edit' && $action != 'postupload') NotifyCheck($pagename);
  68  
  69  function NotifyCheck($pagename) {
  70    global $NotifyFile, $Now, $LastModTime;
  71    $nfp = @fopen($NotifyFile, 'r');
  72    if (!$nfp) return;
  73    $nextevent = fgets($nfp);
  74    fclose($nfp);
  75    if ($Now < $nextevent && $LastModTime < filemtime($NotifyFile)) return;
  76    register_shutdown_function('NotifyUpdate', $pagename, getcwd());
  77  }
  78  
  79      
  80  function PostNotify($pagename, &$page, &$new) {
  81    global $IsPagePosted;
  82    if ($IsPagePosted) 
  83      register_shutdown_function('NotifyUpdate', $pagename, getcwd());
  84  }
  85  
  86  function NotifyUpdate($pagename, $dir='') {
  87    global $NotifyList, $NotifyListPageFmt, $NotifyFile, $IsPagePosted, $IsUploadPosted,
  88      $FmtV, $NotifyTimeFmt, $NotifyItemFmt, $SearchPatterns,
  89      $NotifySquelch, $NotifyDelay, $Now, $Charset, $EnableNotifySubjectEncode,
  90      $NotifySubjectFmt, $NotifyBodyFmt, $NotifyHeaders, $NotifyParameters;
  91  
  92    $abort = ignore_user_abort(true);
  93    if ($dir) { flush(); chdir($dir); }
  94  
  95    $GLOBALS['EnableRedirect'] = 0;
  96  
  97    ##   Read in the current notify configuration
  98    $pn = FmtPageName($NotifyListPageFmt, $pagename);
  99    $npage = ReadPage($pn, READPAGE_CURRENT);
 100    preg_match_all('/^[\s*:#->]*(notify[:=].*)/m', $npage['text'], $nlist);
 101    $nlist = array_merge((array)@$NotifyList, (array)@$nlist[1]);
 102    if (!$nlist) return;
 103  
 104    ##   make sure other processes are locked out
 105    Lock(2);
 106  
 107    ##   let's load the current .notifylist table
 108    $nfile = FmtPageName($NotifyFile, $pagename);
 109    $nfp = @fopen($nfile, 'r');
 110    if ($nfp) {
 111      ##   get our current squelch and delay timestamps
 112      clearstatcache();
 113      $sz = filesize($nfile);
 114      list($nextevent, $firstpost) = explode(' ', rtrim(fgets($nfp, $sz)));
 115      ##   restore our notify array
 116      $notify = unserialize(fgets($nfp, $sz));
 117      fclose($nfp);
 118    }
 119    if (!is_array($notify)) $notify = array();
 120  
 121    ##   if this is for a newly posted page, get its information
 122    if ($IsPagePosted || $IsUploadPosted) {
 123      $page = ReadPage($pagename, READPAGE_CURRENT);
 124      $FmtV['$PostTime'] = strftime($NotifyTimeFmt, $Now);
 125      $item = urlencode(FmtPageName($NotifyItemFmt, $pagename));
 126      if ($firstpost < 1) $firstpost = $Now;
 127    }
 128  
 129    foreach($nlist as $n) {
 130      $opt = ParseArgs($n);
 131      $mailto = preg_split('/[\s,]+/', $opt['notify']);
 132      if (!$mailto) continue;
 133      if ($opt['squelch']) 
 134        foreach($mailto as $m) $squelch[$m] = $opt['squelch'];
 135      if (!$IsPagePosted) continue;
 136      if ($opt['link']) {
 137        $link = MakePageName($pagename, $opt['link']);
 138        if (!preg_match("/(^|,)$link(,|$)/i", $page['targets'])) continue;
 139      }
 140      $pats = @(array)$SearchPatterns[$opt['list']];
 141      if ($opt['group']) $pats[] = FixGlob($opt['group'], '$1$2.*');
 142      if ($opt['name']) $pats[] = FixGlob($opt['name'], '$1*.$2');
 143      if ($pats && !MatchPageNames($pagename, $pats)) continue;
 144      if ($opt['trail']) {
 145        $trail = ReadTrail($pagename, $opt['trail']);
 146        for ($i=0; $i<count($trail); $i++) 
 147          if ($trail[$i]['pagename'] == $pagename) break;
 148        if ($i >= count($trail)) continue;
 149      }
 150      foreach($mailto as $m) { $notify[$m][] = $item; }
 151    }
 152  
 153    $nnow = time();
 154    if ($nnow < $firstpost + $NotifyDelay) 
 155      $nextevent = $firstpost + $NotifyDelay;
 156    else {
 157      $firstpost = 0;
 158      $nextevent = $nnow + 86400;
 159      $mailto = array_keys($notify);
 160      $subject = FmtPageName($NotifySubjectFmt, $pagename);
 161      if(IsEnabled($EnableNotifySubjectEncode, 0)
 162        && preg_match("/[^\x20-\x7E]/", $subject))
 163          $subject = strtoupper("=?$Charset?B?"). base64_encode($subject)."?=";
 164      $body = FmtPageName($NotifyBodyFmt, $pagename);
 165      foreach ($mailto as $m) {
 166        $msquelch = @$notify[$m]['lastmail'] +
 167                      ((@$squelch[$m]) ? $squelch[$m] : $NotifySquelch);
 168        if ($nnow < $msquelch) {
 169          if ($msquelch < $nextevent && count($notify[$m])>1)
 170            $nextevent = $msquelch;
 171          continue;
 172        }
 173        unset($notify[$m]['lastmail']);
 174        if (!$notify[$m]) { unset($notify[$m]); continue; }
 175        $mbody = str_replace('$NotifyItems',   
 176                             urldecode(implode("\n", $notify[$m])), $body);
 177        if ($NotifyParameters && !@ini_get('safe_mode'))
 178          mail($m, $subject, $mbody, $NotifyHeaders, $NotifyParameters);
 179        else 
 180          mail($m, $subject, $mbody, $NotifyHeaders);
 181        $notify[$m] = array('lastmail' => $nnow);
 182      }
 183    }
 184  
 185    ##   save the updated notify status
 186    $nfp = @fopen($nfile, "w");
 187    if ($nfp) {
 188      fputs($nfp, "$nextevent $firstpost\n");
 189      fputs($nfp, serialize($notify) . "\n");
 190      fclose($nfp);
 191    }
 192    Lock(0);
 193    return true;
 194  }
 195  

title

Description

title

Description

title

Description

title

title

Body