xref: /plugin/publish/action/mail.php (revision 43aa3594b8d7ee24eee5d135f4ba00d4eaafe720)
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
44698cf656SMichael Große        if ($ACT != 'save') {
45698cf656SMichael Große            return true;
46698cf656SMichael Große        }
478bc37331SMichael Große
488bc37331SMichael Große        // IO_WIKIPAGE_WRITE is always called twice when saving a page. This makes sure to only send the mail once.
49698cf656SMichael Große        if (!$event->data[3]) {
50698cf656SMichael Große            return true;
51698cf656SMichael Große        }
52698cf656SMichael Große
538bc37331SMichael Große        // Does the publish plugin apply to this page?
548bc37331SMichael Große        if (!$this->hlp->isActive($ID)) {
558bc37331SMichael Große            return true;
568bc37331SMichael Große        }
578bc37331SMichael Große
588bc37331SMichael Große        //are we supposed to send change-mails at all?
59d5f6523fSMichael Große        if ($this->getConf('apr_mail_receiver') === '') {
608bc37331SMichael Große            return true;
618bc37331SMichael Große        }
628bc37331SMichael Große
63698cf656SMichael Große        // get mail receiver
64698cf656SMichael Große        $receiver = $this->getConf('apr_mail_receiver');
65*43aa3594SPhy        if(!mail_isvalid($receiver)) {
66d5f6523fSMichael Große            dbglog(sprintf($this->getLang('mail_invalid'), htmlspecialchars($receiver)));
67d5f6523fSMichael Große            return false;
68d5f6523fSMichael Große        }
69698cf656SMichael Große
70698cf656SMichael Große        // get mail sender
71faf1789aSMichael Große        $ReplyTo = $INFO['userinfo']['mail'];
72698cf656SMichael Große
732583d55dSMichael Große        if ($ReplyTo == $receiver) {
749e614880SMichael Große            return true;
75698cf656SMichael Große        }
76698cf656SMichael Große
77698cf656SMichael Große        if ($INFO['isadmin'] == '1') {
789e614880SMichael Große            return true;
79698cf656SMichael Große        }
80698cf656SMichael Große
81698cf656SMichael Große        // get mail subject
82faf1789aSMichael Große        $timestamp = dformat(filemtime(wikiFN($ID)), $conf['dformat']);
830d6459beSMichael Große        $subject = $this->getLang('apr_mail_subject') . ': ' . $ID . ' - ' . $timestamp;
84698cf656SMichael Große
85e6206222SMichael Große        $body = $this->create_mail_body('change');
86698cf656SMichael Große
872583d55dSMichael Große        $mail = new Mailer();
882583d55dSMichael Große        $mail->to($receiver);
892583d55dSMichael Große        $mail->subject($subject);
902583d55dSMichael Große        $mail->setBody($body);
912583d55dSMichael Große        $mail->setHeader("Reply-To", $ReplyTo);
922583d55dSMichael Große        $returnStatus = $mail->send();
9312699d35SMichael Große        return $returnStatus;
94698cf656SMichael Große    }
95698cf656SMichael Große
96e6206222SMichael Große    /**
97e6206222SMichael Große     * Create the body of mails to inform about a changed or an approved page
98e6206222SMichael Große     *
99e6206222SMichael Große     * @param string $action Must either be "change" or "approve"
100e6206222SMichael Große     * @return bool|string
101eb718fbbSMichael Große     *
102e6206222SMichael Große     */
103e6206222SMichael Große    public function create_mail_body($action) {
104e6206222SMichael Große        global $ID;
1058ff23e7fSMichael Große        global $conf;
106faf1789aSMichael Große        global $INFO;
107bc4555caSMichael Große
1088ff23e7fSMichael Große        // get mail text
109faf1789aSMichael Große        $rev = filemtime(wikiFN($ID));
1108ff23e7fSMichael Große
111bc4555caSMichael Große        if ($action === 'change') {
112c447490bSMichael Große            $body = io_readFile($this->localFN('mailchangetext'));
113bc4555caSMichael Große
1148ff23e7fSMichael Große            //If there is no approved revision show the diff to the revision before. Otherwise show the diff to the last approved revision.
115faf1789aSMichael Große            if($this->hlp->hasApprovals($INFO['meta'])) {
116c447490bSMichael Große                $aprpre = 'Aproved';
117c447490bSMichael Große                $oldrev = $this->hlp->getLatestApprovedRevision($ID);
118c447490bSMichael Große                $difflink = $this->hlp->getDifflink($ID, $oldrev, $rev);
1198ff23e7fSMichael Große            } else {
120c447490bSMichael Große                $aprpre = 'Previous';
121e6206222SMichael Große                $changelog = new PageChangelog($ID);
122c447490bSMichael Große                $oldrev = $changelog->getRelativeRevision($rev, -1);
123c447490bSMichael Große                $difflink = $this->hlp->getDifflink($ID, $oldrev, $rev);
1248ff23e7fSMichael Große            }
125c447490bSMichael Große
126c447490bSMichael Große            $body = str_replace('@DIFF@', $difflink, $body);
127c447490bSMichael Große            $body = str_replace('@APRPRE@', $aprpre, $body);
128faf1789aSMichael Große            $summary = $INFO['meta']['last_change']['sum'];
129c447490bSMichael Große            $body = str_replace('@SUMMARY@', $summary, $body);
130c447490bSMichael Große            if ($oldrev === false ) {
131c447490bSMichael Große                $oldlink = '---';
132c447490bSMichael Große            } else {
133c447490bSMichael Große                $oldlink = $this->revlink($ID, $oldrev);
134c447490bSMichael Große            }
135c447490bSMichael Große            $body = str_replace('@OLDPAGE@', $oldlink, $body);
136c447490bSMichael Große            $newlink = $this->revlink($ID, $rev);
137c447490bSMichael Große            $body = str_replace('@NEWPAGE@', $newlink, $body);
138bc4555caSMichael Große        } elseif ($action === 'approve') {
139c447490bSMichael Große            $body = io_readFile($this->localFN('mailapprovetext'));
140c447490bSMichael Große            $newlink = $this->revlink($ID, $rev);
141c447490bSMichael Große            $body = str_replace('@URL@', $newlink, $body);
142eb718fbbSMichael Große
143eb718fbbSMichael Große            $changelog = new PageChangelog($ID);
144eb718fbbSMichael Große            $revinfo = $changelog->getRevisionInfo($rev);
145eb718fbbSMichael Große            /** @var DokuWiki_Auth_Plugin $auth */
146eb718fbbSMichael Große            global $auth;
147eb718fbbSMichael Große            $userinfo = $auth->getUserData($revinfo['user']);
148eb718fbbSMichael Große            $body = str_replace('@FULLNAME@', $userinfo['name'], $body);
149bc4555caSMichael Große        } else {
150bc4555caSMichael Große            return false;
151bc4555caSMichael Große        }
152bc4555caSMichael Große
1538ff23e7fSMichael Große        $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body);
1548ff23e7fSMichael Große        $body = str_replace('@TITLE@', $conf['title'], $body);
1558ff23e7fSMichael Große
1568ff23e7fSMichael Große        return $body;
1578ff23e7fSMichael Große    }
1588ff23e7fSMichael Große
1598ff23e7fSMichael Große
1609e614880SMichael Große
1619e614880SMichael Große    /**
1629e614880SMichael Große     * Send approve-mail to editor of the now approved revision
1639e614880SMichael Große     *
1642583d55dSMichael Große     * @return bool false if there was an error passing the mail to the MTA
1659e614880SMichael Große     */
16619a54b91SMichael Große    public function send_approve_mail() {
16719a54b91SMichael Große        global $ID;
16819a54b91SMichael Große        global $REV;
1698cd8852aSMichael Große
1708cd8852aSMichael Große        /** @var DokuWiki_Auth_Plugin $auth */
1718cd8852aSMichael Große        global $auth;
172698cf656SMichael Große        $data = pageinfo();
17319a54b91SMichael Große
17419a54b91SMichael Große        // get mail receiver
1753c21cb49SMichael Große        if (!$REV) {
1763c21cb49SMichael Große            $rev = $data['lastmod'];
1773c21cb49SMichael Große        } else {
1783c21cb49SMichael Große            $rev=$REV;
1793c21cb49SMichael Große        }
1808cd8852aSMichael Große        $changelog = new PageChangelog($ID);
1813c21cb49SMichael Große        $revinfo = $changelog->getRevisionInfo($rev);
1828cd8852aSMichael Große        $userinfo = $auth->getUserData($revinfo['user']);
1838cd8852aSMichael Große        $receiver = $userinfo['mail'];
184bc4555caSMichael Große
18519a54b91SMichael Große        // get mail sender
1862583d55dSMichael Große        $ReplyTo = $data['userinfo']['mail'];
1872583d55dSMichael Große
1882583d55dSMichael Große        if ($ReplyTo == $receiver) {
1892583d55dSMichael Große            return true;
1902583d55dSMichael Große        }
191bc4555caSMichael Große
19219a54b91SMichael Große        // get mail subject
19319a54b91SMichael Große        $subject = $this->getLang('apr_mail_app_subject');
194698cf656SMichael Große
195bc4555caSMichael Große        // get mail text
196e6206222SMichael Große        $body = $this->create_mail_body('approve');
19719a54b91SMichael Große
1982583d55dSMichael Große        $mail = new Mailer();
1992583d55dSMichael Große        $mail->to($receiver);
2002583d55dSMichael Große        $mail->subject($subject);
2012583d55dSMichael Große        $mail->setBody($body);
2022583d55dSMichael Große        $mail->setHeader("Reply-To", $ReplyTo);
2032583d55dSMichael Große        $returnStatus = $mail->send();
2043c21cb49SMichael Große
2052583d55dSMichael Große        return $returnStatus;
206698cf656SMichael Große    }
207698cf656SMichael Große
208698cf656SMichael Große    /**
209f31c6595SMichael Große     * create link to the specified revision
210698cf656SMichael Große     *
211f31c6595SMichael Große     * @param string $id
212f31c6595SMichael Große     * @param string $rev The timestamp of the revision
213846d3071SMichael Große     * @return string
214698cf656SMichael Große     */
215f31c6595SMichael Große    function revlink($id, $rev) {
216698cf656SMichael Große
217698cf656SMichael Große        $options = array(
218698cf656SMichael Große             'rev'=> $rev,
219698cf656SMichael Große        );
220f31c6595SMichael Große        $revlink = wl($id, $options, true, '&');
221698cf656SMichael Große
222f31c6595SMichael Große        return $revlink;
223698cf656SMichael Große    }
224698cf656SMichael Große
225698cf656SMichael Große}
226