xref: /plugin/publish/action/mail.php (revision c447490b56c035ff17100913ab06648ed8f1f6b6)
1698cf656SMichael Große<?php
2698cf656SMichael Große/**
3698cf656SMichael Große * @license GNU General Public License, version 2
4698cf656SMichael Große */
5698cf656SMichael Große
6698cf656SMichael Große
7698cf656SMichael Großeif (!defined('DOKU_INC')) die();
8698cf656SMichael Große
9698cf656SMichael Große
10698cf656SMichael Große/**
11698cf656SMichael Große * Class action_plugin_publish_mail
12698cf656SMichael Große *
13698cf656SMichael Große * @author Michael Große <grosse@cosmocode.de>
14698cf656SMichael Große */
15698cf656SMichael Großeclass action_plugin_publish_mail extends DokuWiki_Action_Plugin {
16698cf656SMichael Große
17a9071e04SMichael Große    /**
18a9071e04SMichael Große     * @var helper_plugin_publish
19a9071e04SMichael Große     */
20a9071e04SMichael Große    private $hlp;
21a9071e04SMichael Große
22a9071e04SMichael Große    function __construct() {
23a9071e04SMichael Große        $this->hlp = plugin_load('helper','publish');
24a9071e04SMichael Große    }
25a9071e04SMichael Große
26698cf656SMichael Große    public function register(Doku_Event_Handler $controller) {
27698cf656SMichael Große
28698cf656SMichael Große        $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'send_change_mail', array());
29698cf656SMichael Große    }
30698cf656SMichael Große
314f5c41dbSMichael Große    /**
324f5c41dbSMichael Große     * send an email to inform about a changed page
334f5c41dbSMichael Große     *
344f5c41dbSMichael Große     * @param $event
354f5c41dbSMichael Große     * @param $param
36d5f6523fSMichael Große     * @return bool false if the receiver is invalid or there was an error passing the mail to the MTA
374f5c41dbSMichael Große     */
38698cf656SMichael Große    function send_change_mail(&$event, $param) {
39698cf656SMichael Große        global $ID;
40698cf656SMichael Große        global $ACT;
41698cf656SMichael Große        global $INFO;
420d6459beSMichael Große        global $conf;
43698cf656SMichael Große        $data = pageinfo();
44698cf656SMichael Große
45698cf656SMichael Große        if ($ACT != 'save') {
46698cf656SMichael Große            return true;
47698cf656SMichael Große        }
488bc37331SMichael Große
498bc37331SMichael Große        // IO_WIKIPAGE_WRITE is always called twice when saving a page. This makes sure to only send the mail once.
50698cf656SMichael Große        if (!$event->data[3]) {
51698cf656SMichael Große            return true;
52698cf656SMichael Große        }
53698cf656SMichael Große
548bc37331SMichael Große        // Does the publish plugin apply to this page?
558bc37331SMichael Große        if (!$this->hlp->isActive($ID)) {
568bc37331SMichael Große            return true;
578bc37331SMichael Große        }
588bc37331SMichael Große
598bc37331SMichael Große        //are we supposed to send change-mails at all?
60d5f6523fSMichael Große        if ($this->getConf('apr_mail_receiver') === '') {
618bc37331SMichael Große            return true;
628bc37331SMichael Große        }
638bc37331SMichael Große
64698cf656SMichael Große        // get mail receiver
65698cf656SMichael Große        $receiver = $this->getConf('apr_mail_receiver');
66d5f6523fSMichael Große        $validator                      = new EmailAddressValidator();
67d5f6523fSMichael Große        $validator->allowLocalAddresses = true;
68d5f6523fSMichael Große        if(!$validator->check_email_address($receiver)) {
69d5f6523fSMichael Große            dbglog(sprintf($this->getLang('mail_invalid'),htmlspecialchars($receiver)));
70d5f6523fSMichael Große            return false;
71d5f6523fSMichael Große        }
72698cf656SMichael Große
73698cf656SMichael Große        // get mail sender
742583d55dSMichael Große        $ReplyTo = $data['userinfo']['mail'];
75698cf656SMichael Große
762583d55dSMichael Große        if ($ReplyTo == $receiver) {
779e614880SMichael Große            return true;
78698cf656SMichael Große        }
79698cf656SMichael Große
80698cf656SMichael Große        if ($INFO['isadmin'] == '1') {
819e614880SMichael Große            return true;
82698cf656SMichael Große        }
83698cf656SMichael Große
84698cf656SMichael Große        // get mail subject
850d6459beSMichael Große        $timestamp = dformat($data['lastmod'], $conf['dformat']);
860d6459beSMichael Große        $subject = $this->getLang('apr_mail_subject') . ': ' . $ID . ' - ' . $timestamp;
87698cf656SMichael Große
88e6206222SMichael Große        $body = $this->create_mail_body('change');
89698cf656SMichael Große
902583d55dSMichael Große        $mail = new Mailer();
912583d55dSMichael Große        $mail->to($receiver);
922583d55dSMichael Große        $mail->subject($subject);
932583d55dSMichael Große        $mail->setBody($body);
942583d55dSMichael Große        $mail->setHeader("Reply-To", $ReplyTo);
952583d55dSMichael Große        $returnStatus = $mail->send();
9612699d35SMichael Große        return $returnStatus;
97698cf656SMichael Große    }
98698cf656SMichael Große
99e6206222SMichael Große    /**
100e6206222SMichael Große     * Create the body of mails to inform about a changed or an approved page
101e6206222SMichael Große     *
102e6206222SMichael Große     * @param string $action Must either be "change" or "approve"
103e6206222SMichael Große     * @return bool|string
104e6206222SMichael Große     * @internal param $pageinfo
105e6206222SMichael Große     */
106e6206222SMichael Große    public function create_mail_body($action) {
107e6206222SMichael Große        global $ID;
1088ff23e7fSMichael Große        global $conf;
109e6206222SMichael Große        $pageinfo = pageinfo();
110bc4555caSMichael Große
1118ff23e7fSMichael Große        // get mail text
1128ff23e7fSMichael Große        $rev = $pageinfo['lastmod'];
1138ff23e7fSMichael Große
114bc4555caSMichael Große        if ($action === 'change') {
115*c447490bSMichael Große            $body = io_readFile($this->localFN('mailchangetext'));
116bc4555caSMichael Große
1178ff23e7fSMichael Große            //If there is no approved revision show the diff to the revision before. Otherwise show the diff to the last approved revision.
1188ff23e7fSMichael Große            if($this->hlp->hasApprovals($pageinfo['meta'])) {
119*c447490bSMichael Große                $aprpre = 'Aproved';
120*c447490bSMichael Große                $oldrev = $this->hlp->getLatestApprovedRevision($ID);
121*c447490bSMichael Große                $difflink = $this->hlp->getDifflink($ID, $oldrev, $rev);
1228ff23e7fSMichael Große            } else {
123*c447490bSMichael Große                $aprpre = 'Previous';
124e6206222SMichael Große                $changelog = new PageChangelog($ID);
125*c447490bSMichael Große                $oldrev = $changelog->getRelativeRevision($rev, -1);
126*c447490bSMichael Große                $difflink = $this->hlp->getDifflink($ID, $oldrev, $rev);
1278ff23e7fSMichael Große            }
128*c447490bSMichael Große
129*c447490bSMichael Große            $body = str_replace('@DIFF@', $difflink, $body);
130*c447490bSMichael Große            $body = str_replace('@APRPRE@', $aprpre, $body);
131*c447490bSMichael Große            $summary = $pageinfo['meta']['last_change']['sum'];
132*c447490bSMichael Große            $body = str_replace('@SUMMARY@', $summary, $body);
133*c447490bSMichael Große            if ($oldrev === false ) {
134*c447490bSMichael Große                $oldlink = '---';
135*c447490bSMichael Große            } else {
136*c447490bSMichael Große                $oldlink = $this->revlink($ID, $oldrev);
137*c447490bSMichael Große            }
138*c447490bSMichael Große            $body = str_replace('@OLDPAGE@', $oldlink, $body);
139*c447490bSMichael Große            $newlink = $this->revlink($ID, $rev);
140*c447490bSMichael Große            $body = str_replace('@NEWPAGE@', $newlink, $body);
141bc4555caSMichael Große        } elseif ($action === 'approve') {
142*c447490bSMichael Große            $body = io_readFile($this->localFN('mailapprovetext'));
143*c447490bSMichael Große            $newlink = $this->revlink($ID, $rev);
144*c447490bSMichael Große            $body = str_replace('@URL@', $newlink, $body);
145*c447490bSMichael Große            $body = str_replace('@FULLNAME@', $pageinfo['userinfo']['name'], $body);
146bc4555caSMichael Große        } else {
147bc4555caSMichael Große            return false;
148bc4555caSMichael Große        }
149bc4555caSMichael Große
1508ff23e7fSMichael Große        $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body);
1518ff23e7fSMichael Große        $body = str_replace('@TITLE@', $conf['title'], $body);
1528ff23e7fSMichael Große
1538ff23e7fSMichael Große        return $body;
1548ff23e7fSMichael Große    }
1558ff23e7fSMichael Große
1568ff23e7fSMichael Große
1579e614880SMichael Große
1589e614880SMichael Große    /**
1599e614880SMichael Große     * Send approve-mail to editor of the now approved revision
1609e614880SMichael Große     *
1612583d55dSMichael Große     * @return bool false if there was an error passing the mail to the MTA
1629e614880SMichael Große     */
16319a54b91SMichael Große    public function send_approve_mail() {
16419a54b91SMichael Große        global $ID;
16519a54b91SMichael Große        global $REV;
1668cd8852aSMichael Große
1678cd8852aSMichael Große        /** @var DokuWiki_Auth_Plugin $auth */
1688cd8852aSMichael Große        global $auth;
169698cf656SMichael Große        $data = pageinfo();
17019a54b91SMichael Große
17119a54b91SMichael Große        // get mail receiver
1723c21cb49SMichael Große        if (!$REV) {
1733c21cb49SMichael Große            $rev = $data['lastmod'];
1743c21cb49SMichael Große        } else {
1753c21cb49SMichael Große            $rev=$REV;
1763c21cb49SMichael Große        }
1778cd8852aSMichael Große        $changelog = new PageChangelog($ID);
1783c21cb49SMichael Große        $revinfo = $changelog->getRevisionInfo($rev);
1798cd8852aSMichael Große        $userinfo = $auth->getUserData($revinfo['user']);
1808cd8852aSMichael Große        $receiver = $userinfo['mail'];
181bc4555caSMichael Große
18219a54b91SMichael Große        // get mail sender
1832583d55dSMichael Große        $ReplyTo = $data['userinfo']['mail'];
1842583d55dSMichael Große
1852583d55dSMichael Große        if ($ReplyTo == $receiver) {
1862583d55dSMichael Große            return true;
1872583d55dSMichael Große        }
188bc4555caSMichael Große
18919a54b91SMichael Große        // get mail subject
19019a54b91SMichael Große        $subject = $this->getLang('apr_mail_app_subject');
191698cf656SMichael Große
192bc4555caSMichael Große        // get mail text
193e6206222SMichael Große        $body = $this->create_mail_body('approve');
19419a54b91SMichael Große
1952583d55dSMichael Große        $mail = new Mailer();
1962583d55dSMichael Große        $mail->to($receiver);
1972583d55dSMichael Große        $mail->subject($subject);
1982583d55dSMichael Große        $mail->setBody($body);
1992583d55dSMichael Große        $mail->setHeader("Reply-To", $ReplyTo);
2002583d55dSMichael Große        $returnStatus = $mail->send();
2013c21cb49SMichael Große
2022583d55dSMichael Große        return $returnStatus;
203698cf656SMichael Große    }
204698cf656SMichael Große
205698cf656SMichael Große    /**
206f31c6595SMichael Große     * create link to the specified revision
207698cf656SMichael Große     *
208f31c6595SMichael Große     * @param string $id
209f31c6595SMichael Große     * @param string $rev The timestamp of the revision
210846d3071SMichael Große     * @return string
211698cf656SMichael Große     */
212f31c6595SMichael Große    function revlink($id, $rev) {
213698cf656SMichael Große
214698cf656SMichael Große        $options = array(
215698cf656SMichael Große             'rev'=> $rev,
216698cf656SMichael Große        );
217f31c6595SMichael Große        $revlink = wl($id, $options, true, '&');
218698cf656SMichael Große
219f31c6595SMichael Große        return $revlink;
220698cf656SMichael Große    }
221698cf656SMichael Große
222698cf656SMichael Große}
223