xref: /plugin/publish/action/mail.php (revision 8bc3733105f6d52b2ef6f187a3eb75c791fb098d)
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 = 'grosse@cosmocode.de';//
61        $receiver = $this->getConf('apr_mail_receiver');
62
63        // get mail sender
64        $sender = $data['userinfo']['mail'];
65
66        if ($sender == $receiver) {
67            dbglog('Mail not send. Sender and receiver are identical.');
68//            return true;
69        }
70
71        if ($INFO['isadmin'] == '1') {
72            dbglog('Mail not send. Sender is admin.');
73//            return true;
74        }
75
76        // get mail subject
77        //$timestamp = time(); //$data['meta']['date']['modified'];
78        $timestamp = $data['lastmod'];
79//        date_default_timezone_set("Europe/Paris");
80        $datum = date("d.m.Y",$timestamp);
81        $uhrzeit = date("H:i",$timestamp);
82        $subject = $this->getLang('apr_mail_subject') . ': ' . $ID . ' - ' . $datum . ' ' . $uhrzeit;
83        dbglog($subject);
84
85        // get mail text
86        //$body = 'apr_changemail_text';
87        $body = $this->getLang('apr_changemail_text');
88        $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body);
89        $body = str_replace('@FULLNAME@', $data['userinfo']['name'], $body);
90        $body = str_replace('@TITLE@', $conf['title'], $body);
91
92        /** @var helper_plugin_publish $helper */
93        $helper = plugin_load('helper','publish');
94        $rev = $data['lastmod'];
95
96        /**
97         * todo it seems like the diff to the previous version is not that helpful after all. Check and remove.
98        $changelog = new PageChangelog($ID);
99        $difflinkPrev = $helper->getDifflink($ID, $changelog->getRelativeRevision($rev,-1), $rev);
100        $difflinkPrev = '"' . $difflinkPrev . '"';
101        $body = str_replace('@CHANGESPREV@', $difflinkPrev, $body);
102        */
103
104        $difflinkApr = $helper->getDifflink($ID, $helper->getLatestApprovedRevision($ID), $rev);
105        $difflinkApr = '"' . $difflinkApr . '"';
106        $body = str_replace('@CHANGES@', $difflinkApr, $body);
107
108        $apprejlink = $this->apprejlink($ID, $data['lastmod']);
109        $apprejlink = '"' . $apprejlink . '"';
110        $body = str_replace('@APPREJ@', $apprejlink, $body);
111
112        dbglog('mail_send?');
113        $returnStatus = mail_send($receiver, $subject, $body, $sender);
114        dbglog($returnStatus);
115        dbglog($body);
116        return $returnStatus;
117    }
118
119    // Funktion versendet eine approve-Mail
120    public function send_approve_mail() {
121        dbglog('send_approve_mail()');
122        global $ID;
123        global $ACT;
124        global $REV;
125
126        /** @var DokuWiki_Auth_Plugin $auth */
127        global $auth;
128        global $conf;
129        $data = pageinfo();
130
131        if ($ACT != 'save') {
132           // return true;
133        }
134
135        // get mail receiver
136        $changelog = new PageChangelog($ID);
137        $revinfo = $changelog->getRevisionInfo($REV);
138        $userinfo = $auth->getUserData($revinfo['user']);
139        $receiver = $userinfo['mail'];
140        dbglog('$receiver: ' . $receiver);
141        // get mail sender
142        $sender = $data['userinfo']['mail'];
143        dbglog('$sender: ' . $sender);
144        // get mail subject
145        $subject = $this->getLang('apr_mail_app_subject');
146        dbglog('$subject: ' . $subject);
147        // get mail text
148        $body = $this->getLang('apr_approvemail_text');
149        $body = str_replace('@DOKUWIKIURL@', DOKU_URL, $body);
150        $body = str_replace('@FULLNAME@', $data['userinfo']['name'], $body);
151        $body = str_replace('@TITLE@', $conf['title'], $body);
152
153        $url = wl($ID, array('rev'=>$this->hlp->getLatestApprovedRevision($ID)), true, '&');
154        $url = '"' . $url . '"';
155        $body = str_replace('@URL@', $url, $body);
156        dbglog('$body: ' . $body);
157
158        return mail_send($receiver, $subject, $body, $sender);
159    }
160
161    /**
162     * erzeugt den Link auf die edit-Seite
163     *
164     * @param $id
165     * @param $rev
166     * @return string
167     */
168    function apprejlink($id, $rev) {
169
170        $options = array(
171             'rev'=> $rev,
172        );
173        $apprejlink = wl($id, $options, true, '&');
174
175        return $apprejlink;
176    }
177
178}
179