File: sqlshell.php

package info (click to toggle)
horde3 3.0.4-4sarge7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 15,980 kB
  • ctags: 16,295
  • sloc: php: 68,726; xml: 2,382; sql: 498; makefile: 74; sh: 63; pascal: 6
file content (144 lines) | stat: -rw-r--r-- 5,842 bytes parent folder | download
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
<?php
/**
 * $Horde: horde/admin/sqlshell.php,v 1.18.10.1 2005/01/03 12:25:29 jan Exp $
 *
 * Copyright 1999-2005 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");
require HORDE_TEMPLATES . '/common-header.inc';
require HORDE_TEMPLATES . '/admin/common-header.inc';

?>
<form name="sqlshell" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<?php Util::pformInput() ?>

<?php
if (Util::getFormData('list-tables')) {
    $description = 'LIST TABLES';
    $dbh = &DB::connect($conf['sql']);
    if (is_a($dbh, 'PEAR_Error')) {
        $result = $dbh;
    } else {
        $result = $dbh->getListOf('tables');
    }
} elseif (Util::getFormData('list-dbs')) {
    $description = 'LIST DATABASES';
    $dbh = &DB::connect($conf['sql']);
    if (is_a($dbh, 'PEAR_Error')) {
        $result = $dbh;
    } else {
        $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.
    $dbh = &DB::connect($conf['sql']);
    if (is_a($dbh, 'PEAR_Error')) {
        $result = $dbh;
    } else {
        $result = $dbh->query(String::convertCharset($command, NLS::getCharset(), $conf['sql']['charset']));
    }
}

if (isset($result)) {
    if (isset($command)) {
        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>';
    }

    echo '<table width="100%" cellpadding="2" cellspacing="0" border="0"><tr><td class="header">' . _("Results") . '</td></tr><tr><td>';

    if (is_a($result, 'PEAR_Error')) {
        echo '<pre>'; var_dump($result); echo '</pre>';
    } else {
        if (is_object($result)) {
            echo '<table border="0" cellpadding="1" cellspacing="1" class="item">';
            $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) ? '&nbsp;' : htmlspecialchars(String::convertCharset($key, $conf['sql']['charset']))) . '</th>';
                    }
                    echo '</tr>';
                    $first = false;
                }
                echo '<tr class="item' . ($i % 2) . '">';
                foreach ($row as $val) {
                    echo '<td class="fixed">' . (empty($val) ? '&nbsp;' : htmlspecialchars(String::convertCharset($val, $conf['sql']['charset']))) . '</td>';
                }
                echo '</tr>';
                $i++;
            }
            echo '</table>';
        } elseif (is_array($result)) {
            echo '<table border="0" cellpadding="1" cellspacing="1" class="item">';
            $first = true;
            foreach ($result as $i => $val) {
                if ($first) {
                    echo '<tr><th align="left">' . (isset($description) ? htmlspecialchars($description) : '&nbsp;') . '</th></tr>';
                    $first = false;
                }
                echo '<tr class="item' . ($i % 2) . '">';
                echo '<td class="fixed">' . (empty($val) ? '&nbsp;' : htmlspecialchars(String::convertCharset($val, $conf['sql']['charset']))) . '</td>';
                echo '</tr>';
            }
            echo '</table>';
        } else {
            echo '<b>' . _("Success") . '</b>';
        }
    }

    echo '</td></tr></table><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" wrap="hard">
<?php if (!empty($command)) echo htmlspecialchars($command) ?></textarea>
<br />
<input type="submit" class="button" value="<?php echo _("Execute") ?>">
<input type="submit" class="button" name="list-tables" value="<?php echo _("List Tables") ?>">
<input type="submit" class="button" name="list-dbs" value="<?php echo _("List Databases") ?>">
<?php echo Help::link('admin', 'admin-sqlshell') ?>

</form>
<?php

require HORDE_TEMPLATES . '/common-footer.inc';