1<?php 2/** 3 * @license GNU General Public License, version 2 4 */ 5 6 7if (!defined('DOKU_INC')) die(); 8 9 10/** 11 * Class action_plugin_publish_mail 12 * 13 * @author Michael Große <grosse@cosmocode.de> 14 */ 15class action_plugin_publish_mail extends DokuWiki_Action_Plugin { 16 17 /** 18 * @var helper_plugin_publish 19 */ 20 private $hlp; 21 22 function __construct() { 23 $this->hlp = plugin_load('helper','publish'); 24 } 25 26 public function register(Doku_Event_Handler $controller) { 27 28 $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'send_change_mail', array()); 29 } 30 31 /** 32 * send an email to inform about a changed page 33 * 34 * @param $event 35 * @param $param 36 * @return bool false if the receiver is invalid or there was an error passing the mail to the MTA 37 */ 38 function send_change_mail(&$event, $param) { 39 global $ID; 40 global $ACT; 41 global $INFO; 42 global $conf; 43 $data = pageinfo(); 44 45 if ($ACT != 'save') { 46 return true; 47 } 48 49 // IO_WIKIPAGE_WRITE is always called twice when saving a page. This makes sure to only send the mail once. 50 if (!$event->data[3]) { 51 return true; 52 } 53 54 // Does the publish plugin apply to this page? 55 if (!$this->hlp->isActive($ID)) { 56 return true; 57 } 58 59 //are we supposed to send change-mails at all? 60 if ($this->getConf('apr_mail_receiver') === '') { 61 return true; 62 } 63 64 // get mail receiver 65 $receiver = $this->getConf('apr_mail_receiver'); 66 $validator = new EmailAddressValidator(); 67 $validator->allowLocalAddresses = true; 68 if(!$validator->check_email_address($receiver)) { 69 dbglog(sprintf($this->getLang('mail_invalid'),htmlspecialchars($receiver))); 70 return false; 71 } 72 73 // get mail sender 74 $ReplyTo = $data['userinfo']['mail']; 75 76 if ($ReplyTo == $receiver) { 77 return true; 78 } 79 80 if ($INFO['isadmin'] == '1') { 81 return true; 82 } 83 84 // get mail subject 85 $timestamp = dformat($data['lastmod'], $conf['dformat']); 86 $subject = $this->getLang('apr_mail_subject') . ': ' . $ID . ' - ' . $timestamp; 87 88 $body = $this->create_mail_body('change'); 89 90 $mail = new Mailer(); 91 $mail->to($receiver); 92 $mail->subject($subject); 93 $mail->setBody($body); 94 $mail->setHeader("Reply-To", $ReplyTo); 95 $returnStatus = $mail->send(); 96 return $returnStatus; 97 } 98 99 /** 100 * Create the body of mails to inform about a changed or an approved page 101 * 102 * @param string $action Must either be "change" or "approve" 103 * @return bool|string 104 * 105 */ 106 public function create_mail_body($action) { 107 global $ID; 108 global $conf; 109 $pageinfo = pageinfo(); 110 111 // get mail text 112 $rev = $pageinfo['lastmod']; 113 114 if ($action === 'change') { 115 $body = io_readFile($this->localFN('mailchangetext')); 116 117 //If there is no approved revision show the diff to the revision before. Otherwise show the diff to the last approved revision. 118 if($this->hlp->hasApprovals($pageinfo['meta'])) { 119 $aprpre = 'Aproved'; 120 $oldrev = $this->hlp->getLatestApprovedRevision($ID); 121 $difflink = $this->hlp->getDifflink($ID, $oldrev, $rev); 122 } else { 123 $aprpre = 'Previous'; 124 $changelog = new PageChangelog($ID); 125 $oldrev = $changelog->getRelativeRevision($rev, -1); 126 $difflink = $this->hlp->getDifflink($ID, $oldrev, $rev); 127 } 128 129 $body = str_replace('@DIFF@', $difflink, $body); 130 $body = str_replace('@APRPRE@', $aprpre, $body); 131 $summary = $pageinfo['meta']['last_change']['sum']; 132 $body = str_replace('@SUMMARY@', $summary, $body); 133 if ($oldrev === false ) { 134 $oldlink = '---'; 135 } else { 136 $oldlink = $this->revlink($ID, $oldrev); 137 } 138 $body = str_replace('@OLDPAGE@', $oldlink, $body); 139 $newlink = $this->revlink($ID, $rev); 140 $body = str_replace('@NEWPAGE@', $newlink, $body); 141 } elseif ($action === 'approve') { 142 $body = io_readFile($this->localFN('mailapprovetext')); 143 $newlink = $this->revlink($ID, $rev); 144 $body = str_replace('@URL@', $newlink, $body); 145 146 $changelog = new PageChangelog($ID); 147 $revinfo = $changelog->getRevisionInfo($rev); 148 /** @var DokuWiki_Auth_Plugin $auth */ 149 global $auth; 150 $userinfo = $auth->getUserData($revinfo['user']); 151 $body = str_replace('@FULLNAME@', $userinfo['name'], $body); 152 } else { 153 return false; 154 } 155 156 $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body); 157 $body = str_replace('@TITLE@', $conf['title'], $body); 158 159 return $body; 160 } 161 162 163 164 /** 165 * Send approve-mail to editor of the now approved revision 166 * 167 * @return bool false if there was an error passing the mail to the MTA 168 */ 169 public function send_approve_mail() { 170 global $ID; 171 global $REV; 172 173 /** @var DokuWiki_Auth_Plugin $auth */ 174 global $auth; 175 $data = pageinfo(); 176 177 // get mail receiver 178 if (!$REV) { 179 $rev = $data['lastmod']; 180 } else { 181 $rev=$REV; 182 } 183 $changelog = new PageChangelog($ID); 184 $revinfo = $changelog->getRevisionInfo($rev); 185 $userinfo = $auth->getUserData($revinfo['user']); 186 $receiver = $userinfo['mail']; 187 188 // get mail sender 189 $ReplyTo = $data['userinfo']['mail']; 190 191 if ($ReplyTo == $receiver) { 192 return true; 193 } 194 195 // get mail subject 196 $subject = $this->getLang('apr_mail_app_subject'); 197 198 // get mail text 199 $body = $this->create_mail_body('approve'); 200 201 $mail = new Mailer(); 202 $mail->to($receiver); 203 $mail->subject($subject); 204 $mail->setBody($body); 205 $mail->setHeader("Reply-To", $ReplyTo); 206 $returnStatus = $mail->send(); 207 208 return $returnStatus; 209 } 210 211 /** 212 * create link to the specified revision 213 * 214 * @param string $id 215 * @param string $rev The timestamp of the revision 216 * @return string 217 */ 218 function revlink($id, $rev) { 219 220 $options = array( 221 'rev'=> $rev, 222 ); 223 $revlink = wl($id, $options, true, '&'); 224 225 return $revlink; 226 } 227 228} 229