/
admin.php
292 lines (247 loc) · 10.3 KB
/
admin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
*
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Myron Turner <turnermm02@shaw.ca>
*/
if(!defined('DOKU_INC')) die();
define('REPLACE_DIR', DOKU_INC . 'data/meta/macros/');
/**
* All DokuWiki plugins to extend the admin function
* need to inherit from this class
*/
class admin_plugin_textinsert extends DokuWiki_Admin_Plugin {
var $output = false;
var $macros_file;
var $macros_data; //used for html listings
/**
* handle user request
*/
function __construct() {
if(!$this->getConf('farm')) {
define('MACROS_FILE', REPLACE_DIR . 'macros.ser');
}
else {
define('MACROS_FILE', metaFN('macros','.ser'));
}
}
function handle() {
$this->macros_file=MACROS_FILE;
if (!isset($_REQUEST['cmd'])) return; // first time - nothing to do
$this->output = '';
if (!checkSecurityToken()) return;
if (!is_array($_REQUEST['cmd'])) return;
$action = "";
// verify valid values
switch (key($_REQUEST['cmd'])) {
case 'add' :
$action = 'add';
$a = $this->add();
break;
case 'delete' :
$a = $this->del();
break;
case 'edit':
$a = $this->edit();
break;
}
// $this->output = print_r($a,true);
// $this->output .= print_r($_REQUEST,true);
}
function add() {
$a = $this->get_macros();
$macros = $_REQUEST['macro'];
$words = $_REQUEST['word'];
foreach ($macros AS $key=>$value) {
if(isset($value) && trim($value)) {
if(isset($words[$key]) && trim($words[$key])) {
$value = utf8_deaccent($value);
$a[$value] = htmlspecialchars (($words[$key]),ENT_NOQUOTES, 'UTF-8');
}
}
}
io_saveFile(MACROS_FILE,serialize($a));
return $a;
}
function del() {
$macros = $this->get_macros();
$deletions = $_REQUEST['delete'];
$keys = array_keys($deletions);
foreach ($keys AS $_key) {
unset($macros[$_key]);
}
io_saveFile(MACROS_FILE,serialize($macros));
return $macros;
}
function edit() {
$macros = $this->get_macros();
$encoded = $_REQUEST['encoded'];
$encoded = array_map('urldecode',$encoded);
foreach($encoded AS $k=>$val) {
$macros[$k] = htmlspecialchars ($val,ENT_NOQUOTES, 'UTF-8', false);
}
io_saveFile(MACROS_FILE,serialize($macros));
return $macros;
}
function get_macros() {
if(file_exists(MACROS_FILE)) {
$a = unserialize(file_get_contents(MACROS_FILE));
if(!is_array($a)) return array();
ksort($a);
return $a;
}
return array();
}
function get_delete_list() {
$macros = $this->macros_data;
ptln('<table cellspacing="4px" width="90%">');
foreach($macros as $macro=>$subst) {
ptln("<tr><td><input type='checkbox' name='delete[$macro]' value='$subst'>");
ptln( "<td style='padding:4px;'>$macro<td>$subst</td>");
}
ptln('</table>');
}
function get_edit_list() {
$macros = $this->macros_data;
ptln('<table cellspacing="4"><tr><th align="center">Macro</th><th align="center">' . $this->getLang('col_subst') .'</th></tr>');
foreach($macros as $macro=>$subst) {
ptln("<tr><td align='center'>$macro </td><td>");
$encoded = urlencode($subst);
if($subst != $encoded) {
ptln("<input type = 'hidden' name='encoded[$macro]' value='$encoded'>");
}
if(strlen($subst) > 80) {
ptln ("<textarea cols='55' rows='3' name='edit[$macro]' onchange='replace_encode(this)'>$subst</textarea></td></tr>");
}
else {
ptln ("<input type='text' size='80' name='edit[$macro]' onchange='replace_encode(this)' value='$subst'></td></tr>");
}
}
ptln('</table>');
}
function view_entries() {
$macros = $this->macros_data;
ptln('<table cellpadding="8px" width="90%">');
foreach($macros as $macro=>$subst) {
ptln( "<tr><td align='center'>$macro<td style='padding: 4px; border-bottom: 1px solid black;'>$subst</tr>");
}
ptln('</table>');
}
function js() {
echo <<<JSFN
<script type="text/javascript">
//<![CDATA[
var replace_divs= new Array('macro_add','macro_del','macro_edit','ti_info','macro_list');
/**
* Edit onChange handler
* @param el input element which has been changed
* @desc if an encode hidden input already exists, its value
* is re-encoded from the text input's value
* If not, a new encoded hidden input is created with the encoded
* value. The encode input value is used to substitute the new edit values
* in the php edit() function
*/
function replace_encode (el) {
var matches = el.name.match(/\[(.*)\]/);
if(matches[1]) {
var name = 'encoded['+matches[1]+']';
var val = el.value;
val = val.replace(/>/g,">");
val = val.replace(/</g,"<");
if(!el.form[name]) {
var encoder = document.createElement('input');
encoder.type = 'hidden';
encoder.name = name;
encoder.value = encodeURIComponent(val);
el.form.appendChild(encoder);
}
else if(el.form[name]) {
el.form[name].value = encodeURIComponent(val);
}
}
}
function ti_getEL(n) {
return document.getElementById(n);
}
function replace_show(which) {
for(var i in replace_divs) {
ti_getEL(replace_divs[i]).style.display='none';
}
ti_getEL(which).style.display='block';
ti_getEL('ti_info_btn').style.display='inline';
}
//]]>
</script>
JSFN;
}
/**
* output appropriate html
*/
function html() {
$this->macros_data = $this->get_macros();
$this->js();
if($this->output) {
ptln('<pre>' . $this->output . '</pre>');
}
ptln('<div style="padding:4px" id="ti_info">');
ptln('<div style="text-align:right;">');
ptln('<button class="button" style="padding:0px;margin:0px;" onclick="replace_show(\'ti_info_btn\');">');
ptln($this->getLang('hide_info') .'</button> ');
ptln('</div>');
ptln('<h2>Info</h2>');
ptln( $this->locale_xhtml('intro') . '</div>');
ptln('<div style="padding-bottom:8px;">');
ptln('<button class="button" onclick="replace_show(\'macro_add\'); ">');
ptln($this->getLang('add_macros') .'</button> ');
ptln('<button class="button" onclick="replace_show(\'macro_del\'); ">');
ptln($this->getLang('delete_macros') .'</button> ');
ptln('<button class="button" onclick="replace_show(\'macro_edit\'); ">');
ptln($this->getLang('edit_macros') .'</button> ');
ptln('<button class="button" onclick="ti_getEL(\'macro_list\').style.display=\'block\';ti_getEL(\'macro_list\').scrollIntoView();">');
ptln($this->getLang('view_macros') .'</button> ');
ptln('<button class="button" onclick="ti_getEL(\'macro_list\').style.display=\'none\';">');
ptln($this->getLang('hide_macros') .'</button> ');
ptln('<button class="button" id="ti_info_btn" style="display:none" onclick="ti_getEL(\'ti_info\').style.display=\'block\';">');
ptln($this->getLang('show_info') .'</button>');
ptln('</div>');
ptln('<form action="'.wl($ID).'" method="post">');
// output hidden values to ensure dokuwiki will return back to this plugin
ptln(' <input type="hidden" name="do" value="admin" />');
ptln(' <input type="hidden" name="page" value="'.$this->getPluginName().'" />');
formSecurityToken();
ptln('<div id="macro_add" style="display:none">');
ptln('<h2>' . $this->getLang('label_add') . '</h2>');
ptln( '<table cellspacing="8px"><tr><th>Macro</th><th>' . $this->getLang('col_subst') . '</th></tr>');
ptln('<tr><td> <input type="text" name="macro[A]" id="m_A" value="" /></td>');
ptln('<td> <input type="text" name="word[A]" size="80" id="w_A" value="" /></td></tr>');
ptln('<tr><td> <input type="text" name="macro[B]" id="m_B" value="" /></td>');
ptln('<td> <input type="text" name="word[B]" size="80" id="w_B" value="" /></td></tr>');
ptln('<tr><td> <input type="text" name="macro[C]" id="m_C" value="" /></td>');
ptln('<td> <input type="text" name="word[C]" size="80" id="w_C" value="" /></td></tr>');
ptln('<tr><td> <input type="text" name="macro[D]" id="m_D" value="" /></td>');
ptln('<td> <input type="text" name="word[D]" size="80" id="w_C" value="" /></td></tr>');
ptln('<tr><td> <input type="text" name="macro[E]" id="m_E" value="" /></td>');
ptln('<td> <input type="text" name="word[E]" size="80" id="w_E" value="" /></td>');
ptln('<tr><td> <input type="text" name="macro[F]" id="m_F" value="" /></td>');
ptln('<td> <textarea cols="45" name="word[F]" rows="4" id="w_F"></textarea></td>');
ptln('</table>');
ptln(' <input type="submit" name="cmd[add]" value="'.$this->getLang('btn_add').'" />');
ptln('</div><br />');
ptln('<div id="macro_del" style="display:none">');
ptln('<h2>' . $this->getLang('label_del') . '</h2>');
$this->get_delete_list();
ptln('<br /><input type="submit" name="cmd[delete]" value="'.$this->getLang('btn_del').'" />');
ptln('</div>');
ptln('<div id="macro_edit" style="display:none; padding: 8px;">');
ptln('<h2>' . $this->getLang('label_edit') . '</h2>');
$this->get_edit_list();
ptln('<br /><input type="submit" name="cmd[edit]" value="'.$this->getLang('btn_edit').'" />');
ptln('</div>');
ptln('</form>');
ptln('<br /><div id="macro_list" style="overflow:auto;display:block;">');
ptln('<h2>' . $this->getLang('label_list') . '</h2>');
$this->view_entries();
ptln('</div>');
}
}