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