3
* $Horde: horde/admin/sqlshell.php,v 1.18.10.1 2005/01/03 12:25:29 jan Exp $
5
* Copyright 1999-2005 Chuck Hagenbuch <chuck@horde.org>
7
* See the enclosed file COPYING for license information (LGPL). If you
8
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
11
@define('HORDE_BASE', dirname(__FILE__) . '/..');
12
require_once HORDE_BASE . '/lib/base.php';
13
require_once 'Horde/Menu.php';
14
require_once 'Horde/Help.php';
15
require_once 'DB.php';
17
if (!Auth::isAdmin()) {
18
Horde::fatal('Forbidden.', __FILE__, __LINE__);
21
$title = _("SQL Shell");
22
require HORDE_TEMPLATES . '/common-header.inc';
23
require HORDE_TEMPLATES . '/admin/common-header.inc';
26
<form name="sqlshell" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
27
<?php Util::pformInput() ?>
30
if (Util::getFormData('list-tables')) {
31
$description = 'LIST TABLES';
32
$dbh = &DB::connect($conf['sql']);
33
if (is_a($dbh, 'PEAR_Error')) {
36
$result = $dbh->getListOf('tables');
38
} elseif (Util::getFormData('list-dbs')) {
39
$description = 'LIST DATABASES';
40
$dbh = &DB::connect($conf['sql']);
41
if (is_a($dbh, 'PEAR_Error')) {
44
$result = $dbh->getListOf('databases');
46
} elseif ($command = trim(Util::getFormData('sql'))) {
47
// Keep a cache of prior queries for convenience.
48
if (!isset($_SESSION['_sql_query_cache'])) {
49
$_SESSION['_sql_query_cache'] = array();
51
if (($key = array_search($command, $_SESSION['_sql_query_cache'])) !== false) {
52
unset($_SESSION['_sql_query_cache'][$key]);
54
array_unshift($_SESSION['_sql_query_cache'], $command);
55
while (count($_SESSION['_sql_query_cache']) > 20) {
56
array_pop($_SESSION['_sql_query_cache']);
59
// Parse out the query results.
60
$dbh = &DB::connect($conf['sql']);
61
if (is_a($dbh, 'PEAR_Error')) {
64
$result = $dbh->query(String::convertCharset($command, NLS::getCharset(), $conf['sql']['charset']));
69
if (isset($command)) {
70
echo '<table cellpadding="2" cellspacing="0" border="0" width="100%"><tr><td class="header">' . _("Query") . '</td></tr><tr><td class="text"><pre>' . htmlspecialchars($command) . '</pre></td></tr></table>';
73
echo '<table width="100%" cellpadding="2" cellspacing="0" border="0"><tr><td class="header">' . _("Results") . '</td></tr><tr><td>';
75
if (is_a($result, 'PEAR_Error')) {
76
echo '<pre>'; var_dump($result); echo '</pre>';
78
if (is_object($result)) {
79
echo '<table border="0" cellpadding="1" cellspacing="1" class="item">';
82
while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
85
foreach ($row as $key => $val) {
86
echo '<th align="left">' . (empty($key) ? ' ' : htmlspecialchars(String::convertCharset($key, $conf['sql']['charset']))) . '</th>';
91
echo '<tr class="item' . ($i % 2) . '">';
92
foreach ($row as $val) {
93
echo '<td class="fixed">' . (empty($val) ? ' ' : htmlspecialchars(String::convertCharset($val, $conf['sql']['charset']))) . '</td>';
99
} elseif (is_array($result)) {
100
echo '<table border="0" cellpadding="1" cellspacing="1" class="item">';
102
foreach ($result as $i => $val) {
104
echo '<tr><th align="left">' . (isset($description) ? htmlspecialchars($description) : ' ') . '</th></tr>';
107
echo '<tr class="item' . ($i % 2) . '">';
108
echo '<td class="fixed">' . (empty($val) ? ' ' : htmlspecialchars(String::convertCharset($val, $conf['sql']['charset']))) . '</td>';
113
echo '<b>' . _("Success") . '</b>';
117
echo '</td></tr></table><br />';
121
<?php if (isset($_SESSION['_sql_query_cache']) &&
122
count($_SESSION['_sql_query_cache'])): ?>
123
<select name="query_cache" onchange="document.sqlshell.sql.value = document.sqlshell.query_cache[document.sqlshell.query_cache.selectedIndex].value;">
124
<?php foreach ($_SESSION['_sql_query_cache'] as $query): ?>
125
<option value="<?php echo htmlspecialchars($query) ?>"><?php echo htmlspecialchars($query) ?></option>
128
<input type="button" value="<?php echo _("Paste") ?>" class="button" onclick="document.sqlshell.sql.value = document.sqlshell.query_cache[document.sqlshell.query_cache.selectedIndex].value;" />
129
<input type="button" value="<?php echo _("Run") ?>" class="button" onclick="document.sqlshell.sql.value = document.sqlshell.query_cache[document.sqlshell.query_cache.selectedIndex].value; document.sqlshell.submit();" />
133
<textarea class="fixed" name="sql" rows="10" cols="60" wrap="hard">
134
<?php if (!empty($command)) echo htmlspecialchars($command) ?></textarea>
136
<input type="submit" class="button" value="<?php echo _("Execute") ?>">
137
<input type="submit" class="button" name="list-tables" value="<?php echo _("List Tables") ?>">
138
<input type="submit" class="button" name="list-dbs" value="<?php echo _("List Databases") ?>">
139
<?php echo Help::link('admin', 'admin-sqlshell') ?>
144
require HORDE_TEMPLATES . '/common-footer.inc';