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