1<?php
2/**
3 * DokuWiki Plugin telleveryone (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Szymon Olewniczak <it@rid.pl>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class admin_plugin_telleveryone extends DokuWiki_Admin_Plugin
15{
16
17    /**
18     * @return int sort number in admin menu
19     */
20    public function getMenuSort()
21    {
22        return 1;
23    }
24
25    /**
26     * @return bool true if only access for superuser, false is for superusers and moderators
27     */
28    public function forAdminOnly()
29    {
30        return false;
31    }
32
33    /**
34     * Should carry out any processing required by the plugin.
35     */
36    public function handle()
37    {
38        global $ID, $INPUT, $INFO;
39
40        if (!$INPUT->param('action')) return;
41        if (!$INPUT->arr('log')) return;
42        if (!checkSecurityToken()) return;
43
44        $log = $INPUT->arr('log');
45
46        try {
47            /** @var \helper_plugin_telleveryone_db $db_helper */
48            $db_helper = plugin_load('helper', 'telleveryone_db');
49            $sqlite = $db_helper->getDB();
50        } catch (Exception $e) {
51            msg($e->getMessage(), -1);
52            return;
53        }
54
55        switch ($INPUT->str('action')) {
56            case 'add':
57                if (!isset($log['message'])) return;
58                $message = $log['message'];
59                $message_html = p_render('xhtml',p_get_instructions($message), $ignore);
60
61                $sqlite->storeEntry('log', [
62                    'timestamp' => date('c'),
63                    'user' => $INFO['client'],
64                    'message' => $message,
65                    'message_html' => $message_html
66                ]);
67                break;
68            case 'delete':
69                if (!isset($log['id'])) return;
70                $sqlite->query('DELETE FROM log WHERE id=?', $log['id']);
71                break;
72            case 'update':
73                if (!isset($log['id']) || !isset($log['message'])) return;
74                $message = $log['message'];
75                $message_html = p_render('xhtml',p_get_instructions($message), $ignore);
76
77                $sqlite->query('UPDATE log SET message=?, message_html=? WHERE id=?', $message, $message_html, $log['id']);
78                break;
79        }
80        send_redirect(wl($ID, array('do' => 'admin', 'page' => 'telleveryone'), true, '&'));
81    }
82
83    /**
84     * Render HTML output, e.g. helpful text and a form
85     */
86    public function html()
87    {
88        global $ID, $INPUT;
89
90        ptln('<h1>' . $this->getLang('menu') . '</h1>');
91
92        try {
93            /** @var \helper_plugin_telleveryone_db $db_helper */
94            $db_helper = plugin_load('helper', 'telleveryone_db');
95            $sqlite = $db_helper->getDB();
96        } catch (Exception $e) {
97            msg($e->getMessage(), -1);
98            return;
99        }
100
101        $res = $sqlite->query("SELECT value FROM config WHERE key='token'");
102        $token = $sqlite->res2single($res);
103
104        $remote_url = DOKU_URL;
105        ptln('<p>API URI: <code>' . $remote_url . '</code><br>Token: <code>' . $token . '</code></p>');
106
107        $res = $sqlite->query('SELECT id, timestamp, message, message_html FROM log ORDER BY timestamp DESC');
108        $logs = $sqlite->res2arr($res);
109        ptln('<ul>');
110        foreach ($logs as $log) {
111            $id = $log['id'];
112            $timestamp = strtotime($log['timestamp']);
113            $message = $log['message'];
114            $message_html = $log['message_html'];
115
116            ptln('<li class="level1"><div class="li">');
117
118            if ($id == $INPUT->int('edit')) {
119                ptln('<div>' . dformat($timestamp) . '</div>');
120                ptln('<hr>');
121                $form = $this->createForm('btn_update', $message);
122                $form->setHiddenField('action', 'update');
123                $form->setHiddenField('log[id]', $id);
124                ptln($form->toHTML());
125            } else {
126                $edit_link = wl(
127                    $ID, [
128                        'do' => 'admin',
129                        'page' => 'telleveryone',
130                        'edit' => $id
131                    ]
132                );
133
134                $edit_link = '<a href="' . $edit_link . '">' . $this->getLang('btn_edit') . '</a>';
135
136                $delete_link = wl(
137                    $ID, [
138                        'do' => 'admin',
139                        'page' => 'telleveryone',
140                        'action' => 'delete',
141                        'sectok' => getSecurityToken(),
142                        'log[id]' => $id
143                    ]
144                );
145
146                $delete_link = '<a href="' . $delete_link . '" class="plugin__telleveryone_delete">' . $this->getLang('btn_delete') . '</a>';
147                ptln('<div>' . dformat($timestamp) . ' ' . $edit_link . ' ' . $delete_link . '</div>');
148                ptln('<hr>');
149                ptln('<div>' . $message_html . '</div>');
150            }
151
152            ptln('</div></li>');
153        }
154        ptln('</ul>');
155
156        if (!$INPUT->param('edit')) {
157            $form = $this->createForm();
158            $form->setHiddenField('action', 'add');
159            ptln($form->toHTML());
160        }
161    }
162
163    protected function createForm($btn_label='btn_add', $textareaValue='') {
164        global $ID;
165
166        $form = new dokuwiki\Form\Form();
167        $form->setHiddenField('id', '');
168        $form->addHTML('<div class="toolbar group">');
169        $form->addHTML('<div id="tool__bar" class="tool__bar" role="toolbar"></div>');
170        $form->addHTML('</div>');
171
172        $textarea = new \dokuwiki\Form\TextareaElement('log[message]', '');
173        $textarea->id('wiki__text');
174        $textarea->val($textareaValue);
175        $textarea->attr('class', "edit");
176        $textarea->attr('cols', 80);
177        $textarea->attr('rows', 10);
178
179
180        $form->addElement($textarea);
181
182
183        $form->addHTML('<div id="wiki__editbar">');
184        $form->addButton('', $this->getLang($btn_label));
185        $cancel_link = wl(
186            $ID, [
187                'do' => 'admin',
188                'page' => 'telleveryone',
189            ]
190        );
191
192        $cancel_link = '<a href="' . $cancel_link . '" style="margin-left:1em" id="plugin__telleveryone_cancel">' . $this->getLang('btn_cancel') . '</a>';
193        $form->addHTML($cancel_link);
194        $form->addHTML('</div><br>');
195        return $form;
196    }
197}
198
199