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
|
<?php
// File sql.php / ibWebAdmin
// Purpose perform sql commands/scripts on the selected database
// Author Lutz Brueckner <irie@gmx.de>
// Copyright (c) 2000, 2001, 2002, 2003, 2004 by Lutz Brueckner,
// published under the terms of the GNU General Public Licence v.2,
// see file LICENCE for details
// Created <00/09/12 08:59:22 lb>
//
// $Id: sql.php,v 1.29 2004/04/22 19:12:52 lbrueckner Exp $
require('./inc/script_start.inc.php');
require('./inc/handle_watchtable.inc.php');
if (isset($s_edit_where) && count($s_edit_where) > 0) {
include('./inc/handle_editdata.inc.php');
}
//
// if the sql_enter-panel is open, get the content for the textarea
//
$idx = get_panel_index($s_sql_panels, 'sql_enter');
if ($s_sql_panels[$idx][2] == 'open') {
$sql_script = isset($s_sql_buffer[$s_sql_pointer]) ? $s_sql_buffer[$s_sql_pointer] : '';
if (isset($HTTP_POST_VARS['sql_script'])) {
$sql_script = get_request_data('sql_script');
if (SQL_MAXSAVE == 0 || substr_count($sql_script, "\n") <= SQL_MAXSAVE) {
$s_sql_buffer[$s_sql_pointer] = $sql_script;
}
else {
unset($s_sql_buffer[$s_sql_pointer]);
}
}
// load file into the textarea
if (isset($HTTP_POST_VARS['sql_load']) &&
is_uploaded_file($HTTP_POST_FILES['sql_file']['tmp_name'])) {
$sql_script = implode('',file($HTTP_POST_FILES['sql_file']['tmp_name']));
if (SQL_MAXSAVE == 0 || substr_count($sql_script, "\n") <= SQL_MAXSAVE) {
$s_sql_buffer[$s_sql_pointer] = $sql_script;
}
else {
unset($s_sql_buffer[$s_sql_pointer]);
}
}
// read and execute a sql file
if (isset($HTTP_POST_VARS['sql_execute']) &&
is_uploaded_file($HTTP_POST_FILES['sql_file']['tmp_name'])) {
$sql_script = implode('',file($HTTP_POST_FILES['sql_file']['tmp_name']));
}
// get the query plan
if (isset($HTTP_POST_VARS['sql_plan'])) {
$sql = "SET PLAN ON;\n".$sql_script.";\n";
list($binary_output, $binary_error) = isql_execute($sql, $s_login['user'], $s_login['password'], $s_login['database'], $s_login['host']);
if (isset($binary_output[1]) && substr($binary_output[1], 0, 4) == 'PLAN') {
$binary_output = array_slice($binary_output, 0, 2);
}
$plan_flag = TRUE;
}
if (isset($HTTP_GET_VARS['buf'])) {
if ($HTTP_GET_VARS['buf'] == 'next') {
$s_sql_pointer = ($s_sql_pointer < SQL_HISTORY_SIZE -1) ? $s_sql_pointer +1 : 0;
}
elseif ($HTTP_GET_VARS['buf'] == 'prev') {
$s_sql_pointer = ($s_sql_pointer == 0) ? SQL_HISTORY_SIZE -1 : $s_sql_pointer -1;
}
$s_sql['buffer'] = '';
$s_sql['more'] = FALSE;
$sql_script = isset($s_sql_buffer[$s_sql_pointer]) ? $s_sql_buffer[$s_sql_pointer] : '';
}
if (isset($HTTP_POST_VARS['sql_go'])) {
if (isset($HTTP_POST_VARS['sql_pointer'])
&& abs($HTTP_POST_VARS['sql_pointer'] < SQL_HISTORY_SIZE)) {
$s_sql_pointer = abs($HTTP_POST_VARS['sql_pointer']);
}
else {
$s_sql_pointer = 0;
}
$s_sql['buffer'] = '';
$sql_script = isset($s_sql_buffer[$s_sql_pointer]) ? $s_sql_buffer[$s_sql_pointer] : '';
}
// include the javascript for jsrs requests
if ($s_use_jsrs == TRUE) {
$js_stack .= js_jsrs_sql_buffer();
}
}
//
// script is called from the enter command form
//
if (isset($HTTP_POST_VARS['sql_run']) ||
isset($HTTP_POST_VARS['sql_display_all']) ||
isset($HTTP_POST_VARS['sql_execute'])) {
// remove empty lines from userinput and put the statements into $lines[]
if (isset($HTTP_POST_VARS['sql_run']) ||
isset($HTTP_POST_VARS['sql_execute'])) {
$lines = explode(';', $sql_script);
//remove whitespace and empty lines
foreach($lines as $idx => $cmd){
$cmd = trim($cmd);
if ($cmd == '') {
array_splice($lines, $idx, 1);
continue;
}
$lines[$idx] = $cmd;
// execute the whole script through isql, if it contains a CREATE DATABASE
if (preg_match('/^CREATE(\s)+(DATABASE|SCHEMA)/i', $cmd)) {
$isql_flag = TRUE;
if ($cmd{(strlen($cmd) -1)} != ';') {
$lines[$idx] .= ';';
}
}
// empty the sql buffer if the script contains one or more select statements
if (strncasecmp('select', $cmd, 6) == 0) {
$s_sql['buffer'] = '';
}
}
// make sure that there are no disabled commands in the script
if (is_array($SQL_DISABLE) && count($SQL_DISABLE) > 0
&& ($s_login['user'] != 'SYSDBA' || SYSDBA_GET_ALL === FALSE)) {
foreach ($SQL_DISABLE as $disable) {
$len = strlen($disable);
foreach ($lines as $line) {
if (strncasecmp($disable, $line, $len) == 0) {
$error = sprintf($ERRORS['DISABLED_CMD'], $disable);
break 2;
}
}
}
}
$s_sql['queries'] = $lines;
}
// 'display all'
else {
$lines = $s_sql['queries'];
}
// execute command/script by isql
if (isset($isql_flag) && empty($error)) {
list($binary_output, $binary_error) = isql_execute($sql_script);
$s_sql['buffer'] = '';
array_shift($binary_output); // discard the first line
foreach($binary_output as $line) {
$s_sql['buffer'] .= nl2br(str_replace(' ', ' ', $line)) . "<br>\n";
}
}
// perform the query(s) by ibase_query()
elseif ($s_connected == TRUE && empty($error)) {
$s_sql['more'] = FALSE;
foreach ($lines as $lnr => $cmd) {
$cnt = 0;
$trans = ibase_trans(TRANS_WRITE, $dbhandle);
$res = @ibase_query($trans, $cmd)
or $ib_error = ibase_errmsg();
// if sql_output-panel is open
$idx = get_panel_index($s_sql_panels, 'sql_output');
if ($s_sql_panels[$idx][2] == 'open') {
// if the query have result rows
if (is_resource($res) && @ibase_num_fields($res) > 0) {
$fieldinfo[$lnr] = get_field_info($res);
// save the rows for the output in the sql_output panel
while ($row = @ibase_fetch_object($res)) {
$results[$lnr][] = get_object_vars ($row);
$cnt++;
if ($cnt >= SHOW_OUTPUT_ROWS
&& !isset($HTTP_POST_VARS['sql_display_all'])) {
$s_sql['more'] = TRUE;
break;
}
}
}
}
ibase_commit($trans);
}
}
// cleanup the watchtable output buffer
$s_watch_buffer = '';
}
//
// process the export buttons from the sql_enter panel
//
if ($HTTP_SERVER_VARS['REQUEST_METHOD'] == 'POST' && !empty($HTTP_POST_VARS)) {
foreach (array_keys($HTTP_POST_VARS) as $name) {
if (preg_match('/sql_export_([0-9]+)/', $name, $matches)
&& isset($s_sql['queries'][$matches[1]])) {
$trans = ibase_trans(TRANS_WRITE, $dbhandle);
$res = @ibase_query($trans, $s_sql['queries'][$matches[1]]);
if ($res === FALSE) {
$ib_error = ibase_errmsg();
break;
}
// send headers for user-download
send_export_headers('application/octet-stream', 'export.csv');
// build one line for the csv file from every result row
while ($row = @ibase_fetch_row($res)) {
$line = '';
foreach ($row as $val) {
$line .= '"'.str_replace('"', '""', rtrim($val)).'",';
}
$line = substr($line, 0, -1) . "\n";
// send line to client
echo $line;
}
ibase_free_result($res);
ibase_commit($trans);
exit();
}
}
}
//
// print out all the panels
//
$active = 'SQL';
$panels = &$s_sql_panels;
require('./inc/script_end.inc.php');
//
// build a result table for an 'select' from the sql_enter panel
//
function &get_result_table($result, &$fieldinfo, $idx) {
$table = '<table id="resulttable_'.$idx."\" border=\"1\" cellspacing=\"0\" onselectstart=\"return false\" style=\"-moz-user-select: none\">\n"
." <tr align=\"left\">\n"
.' <th>'.implode('</th><th>', array_keys($result[0]))."</th>\n"
." </tr>\n";
$cnt = count($result[0]);
foreach ($result as $row) {
$table .= " <tr>\n";
$nr = 0;
foreach ($row as $val) {
if ($val === NULL) {
$val = '<i>NULL</i>';
}
elseif ($fieldinfo[$nr]['type'] == 'BLOB' && !empty($val)) {
$val = '<i>BLOB</i>';
}
else {
$val = trim($val);
}
$table .= " <td> ".$val."</td>\n";
$nr++;
}
$table .= " </tr>\n";
}
$table .= "</table>\n"
.js_selectableelements()
.js_selectabletablerows_listener('resulttable_'.$idx);
return $table;
}
//
// build an export button for the sql_output panel
//
function sql_export_button($idx) {
global $button_strings;
$name = 'sql_export_'.$idx;
return sprintf('<input type="submit" name="%s" value="%s"><br>', $name, $button_strings['Export'])."\n";
}
//
// get informations for the fields of a result set
//
function &get_field_info($res) {
$info = array();
$num = ibase_num_fields($res);
for ($i=0; $i < $num; $i++) {
$info[] = ibase_field_info($res, $i);
}
return $info;
}
?>
|