/
sql.php
125 lines (109 loc) · 3.17 KB
/
sql.php
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
<?php
/*
* LMS version 1.11-git
*
* (C) Copyright 2001-2012 LMS Developers
*
* Please, see the doc/AUTHORS for more information about authors!
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* $Id$
*/
include('sqllang.php');
$layout['pagetitle'] = trans('SQL');
if($query = $_POST['query'])
{
$pagelimit = ( $CONFIG['phpui']['sqlpanel_pagelimit'] ? $CONFIG['phpui']['sqlpanel_pagelimit'] : 50 );
$page = (! $_GET['page'] ? 1 : $_GET['page']);
$start = ($page - 1) * $pagelimit;
$words = array('SELECT','EXPLAIN','SHOW','DESCRIBE','ANALYZE','CHECK','OPTIMIZE','REPAIR','VACUUM');
$t = getmicrotime();
$rows = $LMS->DB->Execute($query);
$duration = getmicrotime() - $t;
if(sizeof($DB->errors))
{
$error['query'] = trans('Query is not correct!');
$SMARTY->assign('error', $error);
$SMARTY->assign('query', $query);
$SMARTY->display('sql.html');
die;
}
list($firstword) = explode(' ', trim($query));
if( ! in_array(strtoupper($firstword), $words) )
{
$nrows = $rows;
}
else
{
unset($result);
switch($CONFIG['database']['type'])
{
case 'postgres':
$cols = pg_num_fields($DB->_result);
for($i=0; $i < $cols; $i++)
$colnames[] = pg_field_name($DB->_result, $i);
break;
case 'mysql':
$cols = mysql_num_fields($DB->_result);
for($i=0; $i < $cols; $i++)
$colnames[] = mysql_field_name($DB->_result, $i);
break;
case 'mysqli':
$cols = mysqli_num_fields($DB->_result);
for($i=0; $i < $cols; $i++)
{
mysqli_field_seek($DB->_result, $i);
$finfo = mysqli_fetch_field($DB->_result);
$colnames[] = $finfo->name;
}
break;
}
if($_GET['print'])
$classes = array(0 => 'grey', 1 => 'white');
else
$classes = array(0 => 'light', 1 => 'lucid');
$i = 0;
while($row = $DB->_driver_fetchrow_assoc())
{
$i++;
if ( $i > $start && $i < ($start+$pagelimit+1) )
{
$result .= '<TR CLASS="'.$classes[$i%2].'"><TD CLASS="BLEND">'.$i.'</TD>';
foreach($colnames as $column)
{
$result .= '<TD>'.nl2br(htmlspecialchars($row[$column])).'</TD>';
}
$result .= '</TR>';
}
}
$nrows = $i;
}
$SMARTY->assign('start', $start);
$SMARTY->assign('page', $page);
$SMARTY->assign('pagelimit', $pagelimit);
$SMARTY->assign('nrows', $nrows);
$SMARTY->assign('ncols', $cols+1);
$SMARTY->assign('colnames', $colnames);
$SMARTY->assign('executetime', $duration);
$SMARTY->assign('result', $result);
$layout['pagetitle'] = trans('SQL - Query Results');
}
$SMARTY->assign('query', $query);
if($_GET['print'])
$SMARTY->display('sqlprint.html');
else
$SMARTY->display('sql.html');
?>