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