xref: /plugin/publish/action/mail.php (revision 0d6459be1125fe04395acfdb7272ec689fd41e83)
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
362583d55dSMichael Große     * @return bool false if 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;
42*0d6459beSMichael 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?
608bc37331SMichael Große        if (!$this->getConf('send_mail_on_change')) {
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');
66698cf656SMichael Große
67698cf656SMichael Große        // get mail sender
682583d55dSMichael Große        $ReplyTo = $data['userinfo']['mail'];
69698cf656SMichael Große
702583d55dSMichael Große        if ($ReplyTo == $receiver) {
719e614880SMichael Große            dbglog('[publish plugin]: Mail not send. Sender and receiver are identical.');
729e614880SMichael Große            return true;
73698cf656SMichael Große        }
74698cf656SMichael Große
75698cf656SMichael Große        if ($INFO['isadmin'] == '1') {
769e614880SMichael Große            dbglog('[publish plugin]: Mail not send. Sender is admin.');
779e614880SMichael Große            return true;
78698cf656SMichael Große        }
79698cf656SMichael Große
80698cf656SMichael Große        // get mail subject
81*0d6459beSMichael Große        $timestamp = dformat($data['lastmod'], $conf['dformat']);
82*0d6459beSMichael Große        $subject = $this->getLang('apr_mail_subject') . ': ' . $ID . ' - ' . $timestamp;
83698cf656SMichael Große        dbglog($subject);
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
101e6206222SMichael Große     * @internal param $pageinfo
102e6206222SMichael Große     */
103e6206222SMichael Große    public function create_mail_body($action) {
104e6206222SMichael Große        global $ID;
1058ff23e7fSMichael Große        global $conf;
106e6206222SMichael Große        $pageinfo = pageinfo();
107bc4555caSMichael Große
1088ff23e7fSMichael Große        // get mail text
1098ff23e7fSMichael Große        $body = $this->getLang('mail_greeting') . "\n";
1108ff23e7fSMichael Große        $rev = $pageinfo['lastmod'];
1118ff23e7fSMichael Große
112bc4555caSMichael Große        if ($action === 'change') {
113bc4555caSMichael Große            $body .= $this->getLang('mail_new_suggestiopns') . "\n\n";
114bc4555caSMichael Große
1158ff23e7fSMichael Große            //If there is no approved revision show the diff to the revision before. Otherwise show the diff to the last approved revision.
1168ff23e7fSMichael Große            if($this->hlp->hasApprovals($pageinfo['meta'])) {
1178ff23e7fSMichael Große                $body .= $this->getLang('mail_changes_to_approved_rev') . "\n\n";
118e6206222SMichael Große                $difflink = $this->hlp->getDifflink($ID, $this->hlp->getLatestApprovedRevision($ID), $rev);
1198ff23e7fSMichael Große            } else {
1208ff23e7fSMichael Große                $body .= $this->getLang('mail_changes_to_previous_rev') . "\n\n";
121e6206222SMichael Große                $changelog = new PageChangelog($ID);
1228ff23e7fSMichael Große                $prevrev = $changelog->getRelativeRevision($rev, -1);
123e6206222SMichael Große                $difflink = $this->hlp->getDifflink($ID, $prevrev, $rev);
1248ff23e7fSMichael Große            }
1258ff23e7fSMichael Große            $body = str_replace('@CHANGES@', $difflink, $body);
126f31c6595SMichael Große            $apprejlink = $this->revlink($ID, $rev);
127bc4555caSMichael Große            $body = str_replace('@URL@', $apprejlink, $body);
128bc4555caSMichael Große        } elseif ($action === 'approve') {
129bc4555caSMichael Große            $body .= $this->getLang('mail_approved') . "\n\n";
130f31c6595SMichael Große            $apprejlink = $this->revlink($ID, $rev);
131bc4555caSMichael Große            $body = str_replace('@URL@', $apprejlink, $body);
132bc4555caSMichael Große        } else {
133bc4555caSMichael Große            return false;
134bc4555caSMichael Große        }
135bc4555caSMichael Große
136bc4555caSMichael Große        $body .= $this->getLang('mail_dw_signature');
1378ff23e7fSMichael Große
1388ff23e7fSMichael Große        $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body);
1398ff23e7fSMichael Große        $body = str_replace('@FULLNAME@', $pageinfo['userinfo']['name'], $body);
1408ff23e7fSMichael Große        $body = str_replace('@TITLE@', $conf['title'], $body);
1418ff23e7fSMichael Große
1428ff23e7fSMichael Große        return $body;
1438ff23e7fSMichael Große    }
1448ff23e7fSMichael Große
1458ff23e7fSMichael Große
1469e614880SMichael Große
1479e614880SMichael Große    /**
1489e614880SMichael Große     * Send approve-mail to editor of the now approved revision
1499e614880SMichael Große     *
1502583d55dSMichael Große     * @return bool false if there was an error passing the mail to the MTA
1519e614880SMichael Große     */
15219a54b91SMichael Große    public function send_approve_mail() {
15319a54b91SMichael Große        global $ID;
15419a54b91SMichael Große        global $REV;
1558cd8852aSMichael Große
1568cd8852aSMichael Große        /** @var DokuWiki_Auth_Plugin $auth */
1578cd8852aSMichael Große        global $auth;
158698cf656SMichael Große        $data = pageinfo();
15919a54b91SMichael Große
16019a54b91SMichael Große        // get mail receiver
1613c21cb49SMichael Große        if (!$REV) {
1623c21cb49SMichael Große        $rev = $data['lastmod'];
1633c21cb49SMichael Große        } else {
1643c21cb49SMichael Große            $rev=$REV;
1653c21cb49SMichael Große        }
1668cd8852aSMichael Große        $changelog = new PageChangelog($ID);
1673c21cb49SMichael Große        $revinfo = $changelog->getRevisionInfo($rev);
1688cd8852aSMichael Große        $userinfo = $auth->getUserData($revinfo['user']);
1698cd8852aSMichael Große        $receiver = $userinfo['mail'];
170bc4555caSMichael Große
17119a54b91SMichael Große        // get mail sender
1722583d55dSMichael Große        $ReplyTo = $data['userinfo']['mail'];
1732583d55dSMichael Große
1742583d55dSMichael Große        if ($ReplyTo == $receiver) {
1752583d55dSMichael Große            return true;
1762583d55dSMichael Große        }
177bc4555caSMichael Große
17819a54b91SMichael Große        // get mail subject
17919a54b91SMichael Große        $subject = $this->getLang('apr_mail_app_subject');
180698cf656SMichael Große
181bc4555caSMichael Große        // get mail text
182e6206222SMichael Große        $body = $this->create_mail_body('approve');
18319a54b91SMichael Große
1842583d55dSMichael Große        $mail = new Mailer();
1852583d55dSMichael Große        $mail->to($receiver);
1862583d55dSMichael Große        $mail->subject($subject);
1872583d55dSMichael Große        $mail->setBody($body);
1882583d55dSMichael Große        $mail->setHeader("Reply-To", $ReplyTo);
1892583d55dSMichael Große        $returnStatus = $mail->send();
1903c21cb49SMichael Große
1912583d55dSMichael Große        return $returnStatus;
192698cf656SMichael Große    }
193698cf656SMichael Große
194698cf656SMichael Große    /**
195f31c6595SMichael Große     * create link to the specified revision
196698cf656SMichael Große     *
197f31c6595SMichael Große     * @param string $id
198f31c6595SMichael Große     * @param string $rev The timestamp of the revision
199846d3071SMichael Große     * @return string
200698cf656SMichael Große     */
201f31c6595SMichael Große    function revlink($id, $rev) {
202698cf656SMichael Große
203698cf656SMichael Große        $options = array(
204698cf656SMichael Große             'rev'=> $rev,
205698cf656SMichael Große        );
206f31c6595SMichael Große        $revlink = wl($id, $options, true, '&');
207698cf656SMichael Große
208f31c6595SMichael Große        return $revlink;
209698cf656SMichael Große    }
210698cf656SMichael Große
211698cf656SMichael Große}
212