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