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 * @internal param $pageinfo 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 $body = str_replace('@FULLNAME@', $pageinfo['userinfo']['name'], $body); 146 } else { 147 return false; 148 } 149 150 $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body); 151 $body = str_replace('@TITLE@', $conf['title'], $body); 152 153 return $body; 154 } 155 156 157 158 /** 159 * Send approve-mail to editor of the now approved revision 160 * 161 * @return bool false if there was an error passing the mail to the MTA 162 */ 163 public function send_approve_mail() { 164 global $ID; 165 global $REV; 166 167 /** @var DokuWiki_Auth_Plugin $auth */ 168 global $auth; 169 $data = pageinfo(); 170 171 // get mail receiver 172 if (!$REV) { 173 $rev = $data['lastmod']; 174 } else { 175 $rev=$REV; 176 } 177 $changelog = new PageChangelog($ID); 178 $revinfo = $changelog->getRevisionInfo($rev); 179 $userinfo = $auth->getUserData($revinfo['user']); 180 $receiver = $userinfo['mail']; 181 182 // get mail sender 183 $ReplyTo = $data['userinfo']['mail']; 184 185 if ($ReplyTo == $receiver) { 186 return true; 187 } 188 189 // get mail subject 190 $subject = $this->getLang('apr_mail_app_subject'); 191 192 // get mail text 193 $body = $this->create_mail_body('approve'); 194 195 $mail = new Mailer(); 196 $mail->to($receiver); 197 $mail->subject($subject); 198 $mail->setBody($body); 199 $mail->setHeader("Reply-To", $ReplyTo); 200 $returnStatus = $mail->send(); 201 202 return $returnStatus; 203 } 204 205 /** 206 * create link to the specified revision 207 * 208 * @param string $id 209 * @param string $rev The timestamp of the revision 210 * @return string 211 */ 212 function revlink($id, $rev) { 213 214 $options = array( 215 'rev'=> $rev, 216 ); 217 $revlink = wl($id, $options, true, '&'); 218 219 return $revlink; 220 } 221 222} 223