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