-
Notifications
You must be signed in to change notification settings - Fork 4
/
admin.php
342 lines (250 loc) · 7.51 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
if ( !defined( 'DOKU_INC' ) )
define( 'DOKU_INC', realpath( dirname( __FILE__ ) . '/../../' ) . '/' );
if ( !defined( 'DOKU_PLUGIN' ) )
define( 'DOKU_PLUGIN', DOKU_INC . 'lib/plugins/' );
require_once( DOKU_PLUGIN . 'admin.php' );
/**
* All DokuWiki plugins to extend the admin function
* need to inherit from this class
*/
class admin_plugin_database2 extends DokuWiki_Admin_Plugin
{
/**
* instance providing functionality stuff.
*
* @var Database2_Admin
*/
protected $db;
/**
* return some info
*/
public function getInfo()
{
return array(
'author' => 'Thomas Urban',
'email' => '[email protected]',
'date' => '2009-11-18',
'name' => 'database2',
'desc' => 'Provides console for querying SQL commands ' .
'to local SQLite database file',
'url' => 'http://wiki.nihilum.de/software:database2',
);
}
/**
* handle user request
*/
public function handle()
{
}
/**
* output appropriate html
*/
public function html()
{
if ( !$this->getConf( 'console' ) )
{
ptln( $this->getLang( 'consoleoff' ) );
return;
}
if ( $this->getConf( 'consoleforcehistory') )
{
@session_start();
$useHistory = true;
}
else if ( $useHistory = !headers_sent() )
session_start();
else
ptln( $this->getLang( 'consolesession' ) );
$db = $this->connect( $_REQUEST['dbfile'] );
if ( $db )
{
if ( $_GET['sectok'] != getSecurityToken() )
$query = '';
else
$query = trim( $_GET['q'] );
$queryEsc = strtr( $query, array( '<' => '<' ) );
if ( $useHistory && ( $query !== '' ) )
{
if ( !is_array( $_SESSION['DATABASE2_CONSOLE_HISTORY'] ) )
$_SESSION['DATABASE2_CONSOLE_HISTORY'] = array();
$HISTORY =& $_SESSION['DATABASE2_CONSOLE_HISTORY'];
$index = array_search( $query, $HISTORY );
if ( $index !== false )
unset( $HISTORY[$index] );
array_unshift( $HISTORY, $query );
$HISTORY = array_slice( $HISTORY, 0, 20 );
}
$btn = $this->getLang( 'consolebtn' );
$sqliteLabel = $this->getLang( 'consolesqlitedoc' );
$dbSelectorLabel = $this->getLang( 'consoledbselector' );
$helperShortcutsLabel = $this->getLang( 'consolehelpershortcuts' );
$helperKeys = $this->getLang( 'consolehelperkeys' );
$helperLocks = $this->getLang( 'consolehelperlocks' );
$helperLog = $this->getLang( 'consolehelperlog' );
$helperTables = $this->getLang( 'consolehelpertables' );
$helperVac = $this->getLang( 'consolehelpervac' );
$helperTemplatesLabel = $this->getLang( 'consolehelpertemplates' );
$helperRead = $this->getLang( 'consolehelperread' );
$helperReadSQL = $this->getLang( 'consolehelperreadsql' );
$helperEdit = $this->getLang( 'consolehelperedit' );
$helperEditSQL = $this->getLang( 'consolehelpereditsql' );
$helperDelete = $this->getLang( 'consolehelperdelete' );
$helperDeleteSQL = $this->getLang( 'consolehelperdeletesql' );
$helperAdd = $this->getLang( 'consolehelperadd' );
$helperAddSQL = $this->getLang( 'consolehelperaddsql' );
if ( $useHistory )
{
$helperHistoryLabel = $this->getLang( 'consolehelperhistory' );
$history = array();
if ( is_array( $HISTORY ) )
foreach ( $HISTORY as $q )
{
$qesc = strtr( $q, array( '<' => '<' ) );
$q = strtr( $q, array( '"' => '"' ) );
$history[] = '<option value="' . $q . '">' . $qesc . ' </option>';
}
$history = implode( "\n", $history );
$history = <<<EOT
<div>
$helperHistoryLabel
<select name="history" onchange="return db2_console_load(this.options[this.selectedIndex].value);">
$history
</select>
</div>
EOT;
}
$sectok = getSecurityToken();
echo <<<EOT
<script type="text/javascript"><!--
function db2_console_load(query)
{
with ( document.database2_console.q )
{
value = query;
focus();
}
return false;
}
//--></script>
<form action="$_SERVER[PHP_SELF]" method="GET" name="database2_console" id="database2_console">
<input type="hidden" name="do" value="$_REQUEST[do]" />
<input type="hidden" name="page" value="$_REQUEST[page]" />
<input type="hidden" name="id" value="$_REQUEST[id]" />
<input type="hidden" name="sectok" value="$sectok" />
<div>
$dbSelectorLabel <input type="text" name="dbfile" id="dbfile" value="$_REQUEST[dbfile]" size="40" />
</div>
<div>
$helperShortcutsLabel
<a href="" onclick="return db2_console_load('SELECT * FROM __keys');">$helperKeys</a>
|
<a href="" onclick="return db2_console_load('SELECT * FROM __locks');">$helperLocks</a>
|
<a href="" onclick="return db2_console_load('SELECT * FROM __log');">$helperLog</a>
|
<a href="" onclick="return db2_console_load('SELECT * FROM SQLITE_MASTER');">$helperTables</a>
|
<a href="" onclick="return db2_console_load('VACUUM');">$helperVac</a>
</div>
<div>
$helperTemplatesLabel
<a href="" onclick="return db2_console_load('$helperReadSQL');">$helperRead</a>
|
<a href="" onclick="return db2_console_load('$helperEditSQL');">$helperEdit</a>
|
<a href="" onclick="return db2_console_load('$helperDeleteSQL');">$helperDelete</a>
|
<a href="" onclick="return db2_console_load('$helperAddSQL');">$helperAdd</a>
</div>
$history
<textarea name="q" rows="3" cols="60" style="width: 100%;">$queryEsc</textarea>
<div>
<input type="submit" value="$btn" />
|
<a href="http://sqlite.org/lang.html" target="sqliteLangDoc">
$sqliteLabel
</a>
</div>
</form>
<script type="text/javascript"><!--
document.database2_console.q.select();
document.database2_console.q.focus();
//--></script>
EOT;
if ( $query !== '' )
{
echo <<<EOT
<div style="padding-top: 1em; margin-top: 1em; border-top: 1px solid #888888;">
EOT;
try
{
$result = $db->getLink()->query( $query );
if ( $result instanceof PDOStatement )
{
$rows = $result->fetchAll( PDO::FETCH_ASSOC );
if ( count( $rows ) )
{
$first = array_slice( $rows, 0, 1 );
$cols = empty( $first ) ? array() : array_keys( $first );
echo $db->__renderTable( null, $cols, $rows,
count( $rows ), count( $rows ),
0, null, array(), false,
true );
}
else
echo $this->getLang( 'consolegoodresult' );
}
else
var_dump( $result );
}
catch ( PDOException $e )
{
echo '<div class="error">' . $e->getMessage() . '</div>';
}
echo '</div>';
}
}
}
/**
* Connects to local database.
*
* @return Database2_Admin
*/
public function connect( $explicitDBPathname = null )
{
if ( !( $this->db instanceof Database2_Admin ) )
{
self::includeLib();
$db = new Database2_Admin( $this );
$dbFile = trim( $explicitDBPathname );
if ( $dbFile === '' )
$dbFile = $_REQUEST['id'];
if ( $db->connect( $dbFile ) )
$this->db = $db;
}
return $this->db;
}
public static function includeLib()
{
if ( !class_exists( 'Database2_Admin' ) )
{
$libFile = dirname( __FILE__ ) . '/database2.php';
// support working with development version if available and
// selected to enable development in a production wiki
// (as used on wiki.nihilum.de)
if ( is_file( dirname( __FILE__ ) . '/database2.dev.php' ) )
{
@session_start();
if ( $_GET['use_dev'] )
$_SESSION['useDevIP'] = $_SERVER['REMOTE_ADDR'];
if ( $_GET['use_prod'] )
unset( $_SESSION['useDevIP'] );
if ( $_SESSION['useDevIP'] )
if ( $_SESSION['useDevIP'] == $_SERVER['REMOTE_ADDR'] )
$libFile = dirname( __FILE__ ) . '/database2.dev.php';
}
{ include_once( $libFile ); }
}
}
}