mailposts.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2002-2004 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. To explicitly enable this feature, execute
  13. include_once("scripts/mailposts.php");
  14. from config.php somewhere.
  15. Several variables control the functioning of this script:
  16. $MailPostsTo - comma separated list of email recipients
  17. $MailPostsFrom - return email address
  18. $MailPostsDelay - number of seconds to wait before sending mail after
  19. the first post. Useful so that lots of small edits result are
  20. batched together in a single email message. However, mail
  21. won't be sent until the first execution of pmwiki.php after the
  22. delay has expired, which could be much longer than the delay period
  23. itself depending on how active your site is.
  24. $MailPostsSquelch - minimum number of seconds between sending mail
  25. messages. Useful when $MailPostsDelay is set to a small value
  26. to prevent large numbers of mail notifications to be sent.
  27. $MailPostsFile - scratch file used to keep track of recent posts
  28. $MailPostsMessage - body of message to be sent. The sequence
  29. '$MailPostsList' is replaced with the list of changes.
  30. $MailPostTimeFmt - the format for dates and times in $PostTime
  31. $MailPostItemFmt - the text to be sent for each changed item in the post.
  32. The string $PostTime contains the time of the post formatted
  33. according to $MailPostTimeFmt.
  34. $MailPostsSubject - subject line for mail to be sent
  35. $MailPostsHeaders - string of extra headers for mail (passed to PHP
  36. mail() function, some headers may not work for PHP < 4.3).
  37. $MailPostsFunction - If the default PHP mail function isn't working
  38. for you, then you can define your own mail function here or you
  39. can try "MailPostsSendmail".
  40. */
  41. SDV($MailPostsDelay,0);
  42. SDV($MailPostsSquelch,7200);
  43. SDV($MailPostsFile,"$WikiDir/.mailposts");
  44. SDV($MailPostsMessage,"Recent wiki posts:\n"
  45. ." ($ScriptUrl/Main/AllRecentChanges)\n\n\$MailPostsList\n");
  46. SDV($MailPostsSubject,"$WikiTitle recent wiki posts");
  47. SDV($MailPostsFunction,"mail");
  48. SDV($MailPostsTimeFmt,$TimeFmt);
  49. SDV($MailPostsItemFmt,' * $PageName . . . $PostTime by $Author');
  50. SDV($MailPostsHeaders,'');
  51. if (@$MailPostsFrom)
  52. $MailPostsHeaders = "From: $MailPostsFrom\r\n$MailPostsHeaders";
  53. if ($action=='post' || @$_POST['post']) {
  54. Lock(2);
  55. $fp = @fopen($MailPostsFile,"a");
  56. if ($fp) {
  57. $PostTime = strftime($MailPostsTimeFmt,$Now);
  58. fputs($fp,str_replace("\n",$Newline,
  59. FmtPageName("$Now $MailPostsItemFmt",$pagename))."\n");
  60. fclose($fp);
  61. }
  62. Lock(0);
  63. }
  64. if (@$MailPostsTo=="") return;
  65. $fp = @fopen($MailPostsFile,"r");
  66. if (!$fp) return;
  67. $oldestpost = $Now+1; $mailpost=array();
  68. while (!feof($fp)) {
  69. @(list($t,$p) = explode(' ',rtrim(fgets($fp,1024)),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,str_replace($Newline,"\n",$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. }
  94. ?>