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
|
<?php
/**
* $Horde: horde/admin/sqlshell.php,v 1.18.10.9 2006/02/24 22:48:29 chuck Exp $
*
* Copyright 1999-2006 Chuck Hagenbuch <chuck@horde.org>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*/
@define('HORDE_BASE', dirname(__FILE__) . '/..');
require_once HORDE_BASE . '/lib/base.php';
require_once 'Horde/Menu.php';
require_once 'Horde/Help.php';
require_once 'DB.php';
if (!Auth::isAdmin()) {
Horde::fatal('Forbidden.', __FILE__, __LINE__);
}
$title = _("SQL Shell");
Horde::addScriptFile('stripe.js', 'horde', true);
require HORDE_TEMPLATES . '/common-header.inc';
require HORDE_TEMPLATES . '/admin/common-header.inc';
?>
<div style="padding:10px">
<form name="sqlshell" action="sqlshell.php" method="post">
<?php Util::pformInput() ?>
<?php
$dbh = &DB::connect($conf['sql']);
if (is_a($dbh, 'PEAR_Error')) {
Horde::fatal($dbh, __FILE__, __LINE__);
}
$dbh->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
if (Util::getFormData('list-tables')) {
$description = 'LIST TABLES';
$result = $dbh->getListOf('tables');
} elseif (Util::getFormData('list-dbs')) {
$description = 'LIST DATABASES';
$result = $dbh->getListOf('databases');
} elseif ($command = trim(Util::getFormData('sql'))) {
// Keep a cache of prior queries for convenience.
if (!isset($_SESSION['_sql_query_cache'])) {
$_SESSION['_sql_query_cache'] = array();
}
if (($key = array_search($command, $_SESSION['_sql_query_cache'])) !== false) {
unset($_SESSION['_sql_query_cache'][$key]);
}
array_unshift($_SESSION['_sql_query_cache'], $command);
while (count($_SESSION['_sql_query_cache']) > 20) {
array_pop($_SESSION['_sql_query_cache']);
}
// Parse out the query results.
$result = $dbh->query(String::convertCharset($command, NLS::getCharset(), $conf['sql']['charset']));
}
if (isset($result)) {
if (isset($command)) {
echo '<h1 class="header">' . _("Query") . '</h1><br /><pre class="text">' . htmlspecialchars($command) . '</pre>';
}
echo '<h1 class="header">' . _("Results") . '</h1><br />';
if (is_a($result, 'PEAR_Error')) {
echo '<pre class="text">'; var_dump($result); echo '</pre>';
} else {
if (is_object($result)) {
echo '<table cellspacing="1" class="item striped">';
$first = true;
$i = 0;
while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
if ($first) {
echo '<tr>';
foreach ($row as $key => $val) {
echo '<th align="left">' . (empty($key) ? ' ' : htmlspecialchars(String::convertCharset($key, $conf['sql']['charset']))) . '</th>';
}
echo '</tr>';
$first = false;
}
echo '<tr>';
foreach ($row as $val) {
echo '<td class="fixed">' . (empty($val) ? ' ' : htmlspecialchars(String::convertCharset($val, $conf['sql']['charset']))) . '</td>';
}
echo '</tr>';
}
echo '</table>';
} elseif (is_array($result)) {
echo '<table cellspacing="1" class="item striped">';
$first = true;
$i = 0;
foreach ($result as $val) {
if ($first) {
echo '<tr><th align="left">' . (isset($description) ? htmlspecialchars($description) : ' ') . '</th></tr>';
$first = false;
}
echo '<tr><td class="fixed">' . (empty($val) ? ' ' : htmlspecialchars(String::convertCharset($val, $conf['sql']['charset']))) . '</td></tr>';
}
echo '</table>';
} else {
echo '<strong>' . _("Success") . '</strong>';
}
}
echo '<br />';
}
?>
<?php if (isset($_SESSION['_sql_query_cache']) &&
count($_SESSION['_sql_query_cache'])): ?>
<select name="query_cache" onchange="document.sqlshell.sql.value = document.sqlshell.query_cache[document.sqlshell.query_cache.selectedIndex].value;">
<?php foreach ($_SESSION['_sql_query_cache'] as $query): ?>
<option value="<?php echo htmlspecialchars($query) ?>"><?php echo htmlspecialchars($query) ?></option>
<?php endforeach; ?>
</select>
<input type="button" value="<?php echo _("Paste") ?>" class="button" onclick="document.sqlshell.sql.value = document.sqlshell.query_cache[document.sqlshell.query_cache.selectedIndex].value;" />
<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();" />
<br />
<?php endif; ?>
<textarea class="fixed" name="sql" rows="10" cols="60">
<?php if (!empty($command)) echo htmlspecialchars($command) ?></textarea>
<br />
<input type="submit" class="button" value="<?php echo _("Execute") ?>" />
<input type="button" class="button" value="<?php echo _("Clear Query") ?>" onclick="document.sqlshell.sql.value=''" />
<?php if (!empty($command)): ?>
<input type="reset" class="button" value="<?php echo _("Restore Last Query") ?>" />
<?php endif; ?>
<?php if ($dbh->getSpecialQuery('tables') !== null): ?><input type="submit" class="button" name="list-tables" value="<?php echo _("List Tables") ?>" /> <?php endif; ?>
<?php if ($dbh->getSpecialQuery('databases') !== null): ?><input type="submit" class="button" name="list-dbs" value="<?php echo _("List Databases") ?>" /> <?php endif; ?>
<?php echo Help::link('admin', 'admin-sqlshell') ?>
</form>
</div>
<?php
require HORDE_TEMPLATES . '/common-footer.inc';
|