xref: /plugin/publish/action/mail.php (revision e620622258d1b05d03156c7b8741cbcf3d79f7c3)
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    // Funktion versendet eine Änderungsmail
32    function send_change_mail(&$event, $param) {
33        global $ID;
34        global $ACT;
35        global $REV;
36        global $INFO;
37        global $conf;
38        $data = pageinfo();
39
40        if ($ACT != 'save') {
41            return true;
42        }
43
44        // IO_WIKIPAGE_WRITE is always called twice when saving a page. This makes sure to only send the mail once.
45        if (!$event->data[3]) {
46            return true;
47        }
48
49        // Does the publish plugin apply to this page?
50        if (!$this->hlp->isActive($ID)) {
51            return true;
52        }
53
54        //are we supposed to send change-mails at all?
55        if (!$this->getConf('send_mail_on_change')) {
56            return true;
57        }
58
59        // get mail receiver
60        $receiver = $this->getConf('apr_mail_receiver');
61
62        // get mail sender
63        $sender = $data['userinfo']['mail'];
64
65        if ($sender == $receiver) {
66            dbglog('[publish plugin]: Mail not send. Sender and receiver are identical.');
67            return true;
68        }
69
70        if ($INFO['isadmin'] == '1') {
71            dbglog('[publish plugin]: Mail not send. Sender is admin.');
72            return true;
73        }
74
75        // get mail subject
76        $timestamp = $data['lastmod'];
77        $datum = date("d.m.Y",$timestamp);
78        $uhrzeit = date("H:i",$timestamp);
79        $subject = $this->getLang('apr_mail_subject') . ': ' . $ID . ' - ' . $datum . ' ' . $uhrzeit;
80        dbglog($subject);
81
82        $body = $this->create_mail_body('change');
83
84        dbglog('mail_send?');
85        $returnStatus = mail_send($receiver, $subject, $body, $sender);
86        dbglog($returnStatus);
87        dbglog($body);
88        return $returnStatus;
89    }
90
91    /**
92     * Create the body of mails to inform about a changed or an approved page
93     *
94     * @param string $action Must either be "change" or "approve"
95     * @return bool|string
96     * @internal param $pageinfo
97     */
98    public function create_mail_body($action) {
99        global $ID;
100        global $conf;
101        $pageinfo = pageinfo();
102
103        // get mail text
104        $body = $this->getLang('mail_greeting') . "\n";
105        $rev = $pageinfo['lastmod'];
106
107        if ($action === 'change') {
108            $body .= $this->getLang('mail_new_suggestiopns') . "\n\n";
109
110            //If there is no approved revision show the diff to the revision before. Otherwise show the diff to the last approved revision.
111            if($this->hlp->hasApprovals($pageinfo['meta'])) {
112                $body .= $this->getLang('mail_changes_to_approved_rev') . "\n\n";
113                $difflink = $this->hlp->getDifflink($ID, $this->hlp->getLatestApprovedRevision($ID), $rev);
114            } else {
115                $body .= $this->getLang('mail_changes_to_previous_rev') . "\n\n";
116                $changelog = new PageChangelog($ID);
117                $prevrev = $changelog->getRelativeRevision($rev, -1);
118                $difflink = $this->hlp->getDifflink($ID, $prevrev, $rev);
119            }
120            $body = str_replace('@CHANGES@', $difflink, $body);
121            $apprejlink = $this->apprejlink($ID, $rev);
122            $body = str_replace('@URL@', $apprejlink, $body);
123        } elseif ($action === 'approve') {
124            $body .= $this->getLang('mail_approved') . "\n\n";
125            $apprejlink = $this->apprejlink($ID, $rev);
126            $body = str_replace('@URL@', $apprejlink, $body);
127        } else {
128            return false;
129        }
130
131        $body .= $this->getLang('mail_dw_signature');
132
133        $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body);
134        $body = str_replace('@FULLNAME@', $pageinfo['userinfo']['name'], $body);
135        $body = str_replace('@TITLE@', $conf['title'], $body);
136
137        return $body;
138    }
139
140
141
142    /**
143     * Send approve-mail to editor of the now approved revision
144     *
145     * @return mixed
146     */
147    public function send_approve_mail() {
148        dbglog('send_approve_mail()');
149        global $ID;
150        global $ACT;
151        global $REV;
152
153        /** @var DokuWiki_Auth_Plugin $auth */
154        global $auth;
155        global $conf;
156        $data = pageinfo();
157
158        if ($ACT != 'save') {
159           // return true;
160        }
161
162        // get mail receiver
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        $sender = $data['userinfo']['mail'];
170
171        // get mail subject
172        $subject = $this->getLang('apr_mail_app_subject');
173
174        // get mail text
175        $body = $this->create_mail_body('approve');
176
177        return mail_send($receiver, $subject, $body, $sender);
178    }
179
180    /**
181     * erzeugt den Link auf die edit-Seite
182     *
183     * @param $id
184     * @param $rev
185     * @return string
186     */
187    function apprejlink($id, $rev) {
188
189        $options = array(
190             'rev'=> $rev,
191        );
192        $apprejlink = wl($id, $options, true, '&');
193
194        return $apprejlink;
195    }
196
197}
198