xref: /plugin/publish/action/mail.php (revision 0d6459be1125fe04395acfdb7272ec689fd41e83)
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 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('send_mail_on_change')) {
61            return true;
62        }
63
64        // get mail receiver
65        $receiver = $this->getConf('apr_mail_receiver');
66
67        // get mail sender
68        $ReplyTo = $data['userinfo']['mail'];
69
70        if ($ReplyTo == $receiver) {
71            dbglog('[publish plugin]: Mail not send. Sender and receiver are identical.');
72            return true;
73        }
74
75        if ($INFO['isadmin'] == '1') {
76            dbglog('[publish plugin]: Mail not send. Sender is admin.');
77            return true;
78        }
79
80        // get mail subject
81        $timestamp = dformat($data['lastmod'], $conf['dformat']);
82        $subject = $this->getLang('apr_mail_subject') . ': ' . $ID . ' - ' . $timestamp;
83        dbglog($subject);
84
85        $body = $this->create_mail_body('change');
86
87        $mail = new Mailer();
88        $mail->to($receiver);
89        $mail->subject($subject);
90        $mail->setBody($body);
91        $mail->setHeader("Reply-To", $ReplyTo);
92        $returnStatus = $mail->send();
93        return $returnStatus;
94    }
95
96    /**
97     * Create the body of mails to inform about a changed or an approved page
98     *
99     * @param string $action Must either be "change" or "approve"
100     * @return bool|string
101     * @internal param $pageinfo
102     */
103    public function create_mail_body($action) {
104        global $ID;
105        global $conf;
106        $pageinfo = pageinfo();
107
108        // get mail text
109        $body = $this->getLang('mail_greeting') . "\n";
110        $rev = $pageinfo['lastmod'];
111
112        if ($action === 'change') {
113            $body .= $this->getLang('mail_new_suggestiopns') . "\n\n";
114
115            //If there is no approved revision show the diff to the revision before. Otherwise show the diff to the last approved revision.
116            if($this->hlp->hasApprovals($pageinfo['meta'])) {
117                $body .= $this->getLang('mail_changes_to_approved_rev') . "\n\n";
118                $difflink = $this->hlp->getDifflink($ID, $this->hlp->getLatestApprovedRevision($ID), $rev);
119            } else {
120                $body .= $this->getLang('mail_changes_to_previous_rev') . "\n\n";
121                $changelog = new PageChangelog($ID);
122                $prevrev = $changelog->getRelativeRevision($rev, -1);
123                $difflink = $this->hlp->getDifflink($ID, $prevrev, $rev);
124            }
125            $body = str_replace('@CHANGES@', $difflink, $body);
126            $apprejlink = $this->revlink($ID, $rev);
127            $body = str_replace('@URL@', $apprejlink, $body);
128        } elseif ($action === 'approve') {
129            $body .= $this->getLang('mail_approved') . "\n\n";
130            $apprejlink = $this->revlink($ID, $rev);
131            $body = str_replace('@URL@', $apprejlink, $body);
132        } else {
133            return false;
134        }
135
136        $body .= $this->getLang('mail_dw_signature');
137
138        $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body);
139        $body = str_replace('@FULLNAME@', $pageinfo['userinfo']['name'], $body);
140        $body = str_replace('@TITLE@', $conf['title'], $body);
141
142        return $body;
143    }
144
145
146
147    /**
148     * Send approve-mail to editor of the now approved revision
149     *
150     * @return bool false if there was an error passing the mail to the MTA
151     */
152    public function send_approve_mail() {
153        global $ID;
154        global $REV;
155
156        /** @var DokuWiki_Auth_Plugin $auth */
157        global $auth;
158        $data = pageinfo();
159
160        // get mail receiver
161        if (!$REV) {
162        $rev = $data['lastmod'];
163        } else {
164            $rev=$REV;
165        }
166        $changelog = new PageChangelog($ID);
167        $revinfo = $changelog->getRevisionInfo($rev);
168        $userinfo = $auth->getUserData($revinfo['user']);
169        $receiver = $userinfo['mail'];
170
171        // get mail sender
172        $ReplyTo = $data['userinfo']['mail'];
173
174        if ($ReplyTo == $receiver) {
175            return true;
176        }
177
178        // get mail subject
179        $subject = $this->getLang('apr_mail_app_subject');
180
181        // get mail text
182        $body = $this->create_mail_body('approve');
183
184        $mail = new Mailer();
185        $mail->to($receiver);
186        $mail->subject($subject);
187        $mail->setBody($body);
188        $mail->setHeader("Reply-To", $ReplyTo);
189        $returnStatus = $mail->send();
190
191        return $returnStatus;
192    }
193
194    /**
195     * create link to the specified revision
196     *
197     * @param string $id
198     * @param string $rev The timestamp of the revision
199     * @return string
200     */
201    function revlink($id, $rev) {
202
203        $options = array(
204             'rev'=> $rev,
205        );
206        $revlink = wl($id, $options, true, '&');
207
208        return $revlink;
209    }
210
211}
212