xref: /plugin/publish/action/mail.php (revision 8ff23e7f553f3f4bcd3d86b025466004908914a2)
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_approve_mail_body($ID, $data);
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_change_mail_body($id, $pageinfo) {
93        global $conf;
94        // get mail text
95        $body = $this->getLang('mail_greeting') . "\n";
96        $body .= $this->getLang('mail_new_suggestiopns') . "\n\n";
97
98        $rev = $pageinfo['lastmod'];
99
100        //If there is no approved revision show the diff to the revision before. Otherwise show the diff to the last approved revision.
101        if ($this->hlp->hasApprovals($pageinfo['meta'])) {
102            $body .= $this->getLang('mail_changes_to_approved_rev') . "\n\n";
103            $difflink = $this->hlp->getDifflink($id, $this->hlp->getLatestApprovedRevision($id), $rev);
104        } else {
105            $body .= $this->getLang('mail_changes_to_previous_rev') . "\n\n";
106            $changelog = new PageChangelog($id);
107            $prevrev = $changelog->getRelativeRevision($rev,-1);
108            $difflink = $this->hlp->getDifflink($id, $prevrev, $rev);
109        }
110
111        $body .= $this->getLang('mail_dw_signature');
112
113        $body = str_replace('@CHANGES@', $difflink, $body);
114        $apprejlink = $this->apprejlink($id, $rev);
115        $body = str_replace('@APPREJ@', $apprejlink, $body);
116
117        $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body);
118        $body = str_replace('@FULLNAME@', $pageinfo['userinfo']['name'], $body);
119        $body = str_replace('@TITLE@', $conf['title'], $body);
120
121        return $body;
122    }
123
124
125
126    /**
127     * Send approve-mail to editor of the now approved revision
128     *
129     * @return mixed
130     */
131    public function send_approve_mail() {
132        dbglog('send_approve_mail()');
133        global $ID;
134        global $ACT;
135        global $REV;
136
137        /** @var DokuWiki_Auth_Plugin $auth */
138        global $auth;
139        global $conf;
140        $data = pageinfo();
141
142        if ($ACT != 'save') {
143           // return true;
144        }
145
146        // get mail receiver
147        $changelog = new PageChangelog($ID);
148        $revinfo = $changelog->getRevisionInfo($REV);
149        $userinfo = $auth->getUserData($revinfo['user']);
150        $receiver = $userinfo['mail'];
151        dbglog('$receiver: ' . $receiver);
152        // get mail sender
153        $sender = $data['userinfo']['mail'];
154        dbglog('$sender: ' . $sender);
155        // get mail subject
156        $subject = $this->getLang('apr_mail_app_subject');
157        dbglog('$subject: ' . $subject);
158        // get mail text
159        $body = $this->getLang('apr_approvemail_text');
160        $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body);
161        $body = str_replace('@FULLNAME@', $data['userinfo']['name'], $body);
162        $body = str_replace('@TITLE@', $conf['title'], $body);
163
164        $url = wl($ID, array('rev'=>$this->hlp->getLatestApprovedRevision($ID)), true, '&');
165        $url = '"' . $url . '"';
166        $body = str_replace('@URL@', $url, $body);
167        dbglog('$body: ' . $body);
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