~ubuntu-branches/ubuntu/saucy/horde3/saucy

« back to all changes in this revision

Viewing changes to admin/sqlshell.php

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-05-04 23:08:08 UTC
  • Revision ID: james.westby@ubuntu.com-20050504230808-p4hf3hk28o3v7wir
Tags: upstream-3.0.4
ImportĀ upstreamĀ versionĀ 3.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * $Horde: horde/admin/sqlshell.php,v 1.18.10.1 2005/01/03 12:25:29 jan Exp $
 
4
 *
 
5
 * Copyright 1999-2005 Chuck Hagenbuch <chuck@horde.org>
 
6
 *
 
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.
 
9
 */
 
10
 
 
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';
 
16
 
 
17
if (!Auth::isAdmin()) {
 
18
    Horde::fatal('Forbidden.', __FILE__, __LINE__);
 
19
}
 
20
 
 
21
$title = _("SQL Shell");
 
22
require HORDE_TEMPLATES . '/common-header.inc';
 
23
require HORDE_TEMPLATES . '/admin/common-header.inc';
 
24
 
 
25
?>
 
26
<form name="sqlshell" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
 
27
<?php Util::pformInput() ?>
 
28
 
 
29
<?php
 
30
if (Util::getFormData('list-tables')) {
 
31
    $description = 'LIST TABLES';
 
32
    $dbh = &DB::connect($conf['sql']);
 
33
    if (is_a($dbh, 'PEAR_Error')) {
 
34
        $result = $dbh;
 
35
    } else {
 
36
        $result = $dbh->getListOf('tables');
 
37
    }
 
38
} elseif (Util::getFormData('list-dbs')) {
 
39
    $description = 'LIST DATABASES';
 
40
    $dbh = &DB::connect($conf['sql']);
 
41
    if (is_a($dbh, 'PEAR_Error')) {
 
42
        $result = $dbh;
 
43
    } else {
 
44
        $result = $dbh->getListOf('databases');
 
45
    }
 
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();
 
50
    }
 
51
    if (($key = array_search($command, $_SESSION['_sql_query_cache'])) !== false) {
 
52
        unset($_SESSION['_sql_query_cache'][$key]);
 
53
    }
 
54
    array_unshift($_SESSION['_sql_query_cache'], $command);
 
55
    while (count($_SESSION['_sql_query_cache']) > 20) {
 
56
        array_pop($_SESSION['_sql_query_cache']);
 
57
    }
 
58
 
 
59
    // Parse out the query results.
 
60
    $dbh = &DB::connect($conf['sql']);
 
61
    if (is_a($dbh, 'PEAR_Error')) {
 
62
        $result = $dbh;
 
63
    } else {
 
64
        $result = $dbh->query(String::convertCharset($command, NLS::getCharset(), $conf['sql']['charset']));
 
65
    }
 
66
}
 
67
 
 
68
if (isset($result)) {
 
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>';
 
71
    }
 
72
 
 
73
    echo '<table width="100%" cellpadding="2" cellspacing="0" border="0"><tr><td class="header">' . _("Results") . '</td></tr><tr><td>';
 
74
 
 
75
    if (is_a($result, 'PEAR_Error')) {
 
76
        echo '<pre>'; var_dump($result); echo '</pre>';
 
77
    } else {
 
78
        if (is_object($result)) {
 
79
            echo '<table border="0" cellpadding="1" cellspacing="1" class="item">';
 
80
            $first = true;
 
81
            $i = 0;
 
82
            while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
 
83
                if ($first) {
 
84
                    echo '<tr>';
 
85
                    foreach ($row as $key => $val) {
 
86
                        echo '<th align="left">' . (empty($key) ? '&nbsp;' : htmlspecialchars(String::convertCharset($key, $conf['sql']['charset']))) . '</th>';
 
87
                    }
 
88
                    echo '</tr>';
 
89
                    $first = false;
 
90
                }
 
91
                echo '<tr class="item' . ($i % 2) . '">';
 
92
                foreach ($row as $val) {
 
93
                    echo '<td class="fixed">' . (empty($val) ? '&nbsp;' : htmlspecialchars(String::convertCharset($val, $conf['sql']['charset']))) . '</td>';
 
94
                }
 
95
                echo '</tr>';
 
96
                $i++;
 
97
            }
 
98
            echo '</table>';
 
99
        } elseif (is_array($result)) {
 
100
            echo '<table border="0" cellpadding="1" cellspacing="1" class="item">';
 
101
            $first = true;
 
102
            foreach ($result as $i => $val) {
 
103
                if ($first) {
 
104
                    echo '<tr><th align="left">' . (isset($description) ? htmlspecialchars($description) : '&nbsp;') . '</th></tr>';
 
105
                    $first = false;
 
106
                }
 
107
                echo '<tr class="item' . ($i % 2) . '">';
 
108
                echo '<td class="fixed">' . (empty($val) ? '&nbsp;' : htmlspecialchars(String::convertCharset($val, $conf['sql']['charset']))) . '</td>';
 
109
                echo '</tr>';
 
110
            }
 
111
            echo '</table>';
 
112
        } else {
 
113
            echo '<b>' . _("Success") . '</b>';
 
114
        }
 
115
    }
 
116
 
 
117
    echo '</td></tr></table><br />';
 
118
}
 
119
?>
 
120
 
 
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>
 
126
  <?php endforeach; ?>
 
127
  </select>
 
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();" />
 
130
  <br />
 
131
<?php endif; ?>
 
132
 
 
133
<textarea class="fixed" name="sql" rows="10" cols="60" wrap="hard">
 
134
<?php if (!empty($command)) echo htmlspecialchars($command) ?></textarea>
 
135
<br />
 
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') ?>
 
140
 
 
141
</form>
 
142
<?php
 
143
 
 
144
require HORDE_TEMPLATES . '/common-footer.inc';