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 $body = $this->getLang('mail_greeting') . "\n"; 113 $rev = $pageinfo['lastmod']; 114 115 if ($action === 'change') { 116 $body .= $this->getLang('mail_new_suggestiopns') . "\n\n"; 117 118 //If there is no approved revision show the diff to the revision before. Otherwise show the diff to the last approved revision. 119 if($this->hlp->hasApprovals($pageinfo['meta'])) { 120 $body .= $this->getLang('mail_changes_to_approved_rev') . "\n\n"; 121 $difflink = $this->hlp->getDifflink($ID, $this->hlp->getLatestApprovedRevision($ID), $rev); 122 } else { 123 $body .= $this->getLang('mail_changes_to_previous_rev') . "\n\n"; 124 $changelog = new PageChangelog($ID); 125 $prevrev = $changelog->getRelativeRevision($rev, -1); 126 $difflink = $this->hlp->getDifflink($ID, $prevrev, $rev); 127 } 128 $body = str_replace('@CHANGES@', $difflink, $body); 129 $apprejlink = $this->revlink($ID, $rev); 130 $body = str_replace('@URL@', $apprejlink, $body); 131 } elseif ($action === 'approve') { 132 $body .= $this->getLang('mail_approved') . "\n\n"; 133 $apprejlink = $this->revlink($ID, $rev); 134 $body = str_replace('@URL@', $apprejlink, $body); 135 } else { 136 return false; 137 } 138 139 $body .= $this->getLang('mail_dw_signature'); 140 141 $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body); 142 $body = str_replace('@FULLNAME@', $pageinfo['userinfo']['name'], $body); 143 $body = str_replace('@TITLE@', $conf['title'], $body); 144 145 return $body; 146 } 147 148 149 150 /** 151 * Send approve-mail to editor of the now approved revision 152 * 153 * @return bool false if there was an error passing the mail to the MTA 154 */ 155 public function send_approve_mail() { 156 global $ID; 157 global $REV; 158 159 /** @var DokuWiki_Auth_Plugin $auth */ 160 global $auth; 161 $data = pageinfo(); 162 163 // get mail receiver 164 if (!$REV) { 165 $rev = $data['lastmod']; 166 } else { 167 $rev=$REV; 168 } 169 $changelog = new PageChangelog($ID); 170 $revinfo = $changelog->getRevisionInfo($rev); 171 $userinfo = $auth->getUserData($revinfo['user']); 172 $receiver = $userinfo['mail']; 173 174 // get mail sender 175 $ReplyTo = $data['userinfo']['mail']; 176 177 if ($ReplyTo == $receiver) { 178 return true; 179 } 180 181 // get mail subject 182 $subject = $this->getLang('apr_mail_app_subject'); 183 184 // get mail text 185 $body = $this->create_mail_body('approve'); 186 187 $mail = new Mailer(); 188 $mail->to($receiver); 189 $mail->subject($subject); 190 $mail->setBody($body); 191 $mail->setHeader("Reply-To", $ReplyTo); 192 $returnStatus = $mail->send(); 193 194 return $returnStatus; 195 } 196 197 /** 198 * create link to the specified revision 199 * 200 * @param string $id 201 * @param string $rev The timestamp of the revision 202 * @return string 203 */ 204 function revlink($id, $rev) { 205 206 $options = array( 207 'rev'=> $rev, 208 ); 209 $revlink = wl($id, $options, true, '&'); 210 211 return $revlink; 212 } 213 214} 215