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
44        if ($ACT != 'save') {
45            return true;
46        }
47
48        // IO_WIKIPAGE_WRITE is always called twice when saving a page. This makes sure to only send the mail once.
49        if (!$event->data[3]) {
50            return true;
51        }
52
53        // Does the publish plugin apply to this page?
54        if (!$this->hlp->isActive($ID)) {
55            return true;
56        }
57
58        //are we supposed to send change-mails at all?
59        if ($this->getConf('apr_mail_receiver') === '') {
60            return true;
61        }
62
63        // get mail receiver
64        $receiver = $this->getConf('apr_mail_receiver');
65        if(!mail_isvalid($receiver)) {
66            dbglog(sprintf($this->getLang('mail_invalid'), htmlspecialchars($receiver)));
67            return false;
68        }
69
70        // get mail sender
71        $ReplyTo = $INFO['userinfo']['mail'];
72
73        if ($ReplyTo == $receiver) {
74            return true;
75        }
76
77        if ($INFO['isadmin'] == '1') {
78            return true;
79        }
80
81        // get mail subject
82        $timestamp = dformat(filemtime(wikiFN($ID)), $conf['dformat']);
83        $subject = $this->getLang('apr_mail_subject') . ': ' . $ID . ' - ' . $timestamp;
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     *
102     */
103    public function create_mail_body($action) {
104        global $ID;
105        global $conf;
106        global $INFO;
107
108        // get mail text
109        $rev = filemtime(wikiFN($ID));
110
111        if ($action === 'change') {
112            $body = io_readFile($this->localFN('mailchangetext'));
113
114            //If there is no approved revision show the diff to the revision before. Otherwise show the diff to the last approved revision.
115            if($this->hlp->hasApprovals($INFO['meta'])) {
116                $aprpre = 'Aproved';
117                $oldrev = $this->hlp->getLatestApprovedRevision($ID);
118                $difflink = $this->hlp->getDifflink($ID, $oldrev, $rev);
119            } else {
120                $aprpre = 'Previous';
121                $changelog = new PageChangelog($ID);
122                $oldrev = $changelog->getRelativeRevision($rev, -1);
123                $difflink = $this->hlp->getDifflink($ID, $oldrev, $rev);
124            }
125
126            $body = str_replace('@DIFF@', $difflink, $body);
127            $body = str_replace('@APRPRE@', $aprpre, $body);
128            $summary = $INFO['meta']['last_change']['sum'];
129            $body = str_replace('@SUMMARY@', $summary, $body);
130            if ($oldrev === false ) {
131                $oldlink = '---';
132            } else {
133                $oldlink = $this->revlink($ID, $oldrev);
134            }
135            $body = str_replace('@OLDPAGE@', $oldlink, $body);
136            $newlink = $this->revlink($ID, $rev);
137            $body = str_replace('@NEWPAGE@', $newlink, $body);
138        } elseif ($action === 'approve') {
139            $body = io_readFile($this->localFN('mailapprovetext'));
140            $newlink = $this->revlink($ID, $rev);
141            $body = str_replace('@URL@', $newlink, $body);
142
143            $changelog = new PageChangelog($ID);
144            $revinfo = $changelog->getRevisionInfo($rev);
145            /** @var DokuWiki_Auth_Plugin $auth */
146            global $auth;
147            $userinfo = $auth->getUserData($revinfo['user']);
148            $body = str_replace('@FULLNAME@', $userinfo['name'], $body);
149        } else {
150            return false;
151        }
152
153        $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body);
154        $body = str_replace('@TITLE@', $conf['title'], $body);
155
156        return $body;
157    }
158
159
160
161    /**
162     * Send approve-mail to editor of the now approved revision
163     *
164     * @return bool false if there was an error passing the mail to the MTA
165     */
166    public function send_approve_mail() {
167        global $ID;
168        global $REV;
169
170        /** @var DokuWiki_Auth_Plugin $auth */
171        global $auth;
172        $data = pageinfo();
173
174        // get mail receiver
175        if (!$REV) {
176            $rev = $data['lastmod'];
177        } else {
178            $rev=$REV;
179        }
180        $changelog = new PageChangelog($ID);
181        $revinfo = $changelog->getRevisionInfo($rev);
182        $userinfo = $auth->getUserData($revinfo['user']);
183        $receiver = $userinfo['mail'];
184
185        // get mail sender
186        $ReplyTo = $data['userinfo']['mail'];
187
188        if ($ReplyTo == $receiver) {
189            return true;
190        }
191
192        // get mail subject
193        $subject = $this->getLang('apr_mail_app_subject');
194
195        // get mail text
196        $body = $this->create_mail_body('approve');
197
198        $mail = new Mailer();
199        $mail->to($receiver);
200        $mail->subject($subject);
201        $mail->setBody($body);
202        $mail->setHeader("Reply-To", $ReplyTo);
203        $returnStatus = $mail->send();
204
205        return $returnStatus;
206    }
207
208    /**
209     * create link to the specified revision
210     *
211     * @param string $id
212     * @param string $rev The timestamp of the revision
213     * @return string
214     */
215    function revlink($id, $rev) {
216
217        $options = array(
218             'rev'=> $rev,
219        );
220        $revlink = wl($id, $options, true, '&');
221
222        return $revlink;
223    }
224
225}
226