mailposts.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2002-2005 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. This script enables email notifications to be sent whenever posts
  8. are made. It is included by default from the stdconfig.php
  9. script if $EnableMailPosts is set to non-zero. Be sure to set
  10. the $MailPostsTo variable or the scratch file will grow without
  11. limit.
  12. Several variables control the functioning of this script:
  13. $MailPostsTo - comma separated list of email recipients
  14. $MailPostsFrom - return email address
  15. $MailPostsDelay - number of seconds to wait before sending mail after
  16. the first post. Useful so that lots of small edits result are
  17. batched together in a single email message. However, mail
  18. won't be sent until the first execution of pmwiki.php after the
  19. delay has expired, which could be much longer than the delay period
  20. itself depending on how active your site is.
  21. $MailPostsSquelch - minimum number of seconds between sending mail
  22. messages. Useful when $MailPostsDelay is set to a small value
  23. to prevent large numbers of mail notifications to be sent.
  24. $MailPostsFile - scratch file used to keep track of recent posts
  25. $MailPostsMessage - body of message to be sent. The sequence
  26. '$MailPostsList' is replaced with the list of changes.
  27. $MailPostTimeFmt - the format for dates and times in $PostTime
  28. $MailPostItemFmt - the text to be sent for each changed item in the post.
  29. The string $PostTime contains the time of the post formatted
  30. according to $MailPostTimeFmt.
  31. $MailPostsSubject - subject line for mail to be sent
  32. $MailPostsHeaders - string of extra headers for mail (passed to PHP
  33. mail() function, some headers may not work for PHP < 4.3).
  34. $MailPostsFunction - If the default PHP mail function isn't working
  35. for you, then you can define your own mail function here or you
  36. can try "MailPostsSendmail".
  37. */
  38. SDV($MailPostsDelay,0);
  39. SDV($MailPostsSquelch,7200);
  40. SDV($MailPostsFile,"$WorkDir/.mailposts");
  41. SDV($MailPostsMessage,"Recent wiki posts:\n"
  42. ." ($ScriptUrl/$SiteGroup/AllRecentChanges)\n\n\$MailPostsList\n");
  43. SDV($MailPostsSubject,"$WikiTitle recent wiki posts");
  44. SDV($MailPostsFunction,"mail");
  45. SDV($MailPostsTimeFmt,$TimeFmt);
  46. SDV($MailPostsItemFmt,' * $FullName . . . $PostTime by $Author');
  47. SDV($MailPostsHeaders,'');
  48. if (@$MailPostsFrom)
  49. $MailPostsHeaders = "From: $MailPostsFrom\r\n$MailPostsHeaders";
  50. array_push($EditFunctions,'MailPosts');
  51. function MailPosts($pagename, &$page, &$new) {
  52. global $IsPagePosted, $MailPostsFile, $MailPostsTimeFmt, $Now,
  53. $MailPostsItemFmt, $PostTime;
  54. if (!$IsPagePosted) return;
  55. $fp = @fopen($MailPostsFile, "a");
  56. if ($fp) {
  57. $PostTime = strftime($MailPostsTimeFmt, $Now);
  58. fputs($fp,
  59. urlencode(FmtPageName("$Now $MailPostsItemFmt", $pagename))."\n");
  60. fclose($fp);
  61. }
  62. }
  63. if (@$MailPostsTo == "") return;
  64. $fp = @fopen($MailPostsFile, "r");
  65. if (!$fp) return;
  66. $oldestpost = $Now+1; $mailpost=array();
  67. while (!feof($fp)) {
  68. $x = urldecode(rtrim(fgets($fp, 1024)));
  69. @(list($t,$p) = explode(' ', $x, 2));
  70. if (!$t) continue;
  71. if ($p=='#lastmailed') {
  72. if ($t > $Now-$MailPostsSquelch) { fclose($fp); return; }
  73. continue;
  74. }
  75. Lock(2);
  76. array_push($mailpost, $p."\n");
  77. if ($t<$oldestpost) $oldestpost=$t;
  78. }
  79. fclose($fp);
  80. if ($oldestpost > $Now-$MailPostsDelay) { Lock(0); return; }
  81. $MailPostsFunction($MailPostsTo,$MailPostsSubject,
  82. str_replace('$MailPostsList',join('',$mailpost),$MailPostsMessage),
  83. $MailPostsHeaders);
  84. $fp = @fopen($MailPostsFile,"w");
  85. if ($fp) { fputs($fp,"$Now #lastmailed\n"); fclose($fp); }
  86. Lock(0);
  87. function MailPostsSendmail($to,$subject,$msg,$headers) {
  88. if (preg_match('/From: .*?([-.\w]+@[-.\w]+)/',$headers,$match))
  89. $from="-f".$match[1];
  90. $mailer = popen("/usr/lib/sendmail -t -i $from","w");
  91. fwrite($mailer,"To: $to\nSubject: $subject\n$headers\n\n$msg");
  92. pclose($mailer);
  93. }