forked from jwigal/churchinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuerySQL.php
197 lines (149 loc) · 4.45 KB
/
QuerySQL.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
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
<?php
/*******************************************************************************
*
* filename : QuerySQL.php
* last change : 2003-01-04
* website : http://www.infocentral.org
* copyright : Copyright 2001, 2002, 2003 Deane Barker, Chris Gebhardt
*
* InfoCentral is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
******************************************************************************/
//Include the function library
require "Include/Config.php";
require "Include/Functions.php";
//Set the page title
$sPageTitle = gettext("Free-Text Query");
// Security: User must be an Admin to access this page. It allows unrestricted database access!
// Otherwise, re-direct them to the main menu.
if (!$_SESSION['bAdmin'])
{
Redirect("Menu.php");
exit;
}
if (isset($_POST["SQL"]))
{
//Assign the value locally
$sSQL = stripslashes(trim($_POST["SQL"]));
}
else
{
$sSQL = "";
}
if (isset($_POST["CSV"]))
{
ExportQueryResults();
exit;
}
require "Include/Header.php";
?>
<form method="post">
<center><table><tr>
<td class="LabelColumn"> <?php echo gettext("Export Results to CSV file") ?> </td>
<td class="TextColumn"><input name="CSV" type="checkbox" id="CSV" value="1"></td>
</tr></table></center>
<p align="center">
<textarea style="font-family:courier,fixed; font-size:9pt; padding:1;" cols="60" rows="10" name="SQL"><?php echo $sSQL; ?></textarea>
</p>
<p align="center">
<input type="submit" class="icButton" name="Submit" <?php echo 'value="' . gettext("Execute SQL") . '"'; ?>>
</p>
</form>
<?php
if (isset($_POST["SQL"]))
{
if (strtolower(substr($sSQL,0,6)) == "select")
{
RunFreeQuery();
}
}
function ExportQueryResults()
{
global $cnInfoCentral;
global $aRowClass;
global $rsQueryResults;
global $sSQL;
global $iQueryID;
$sCSVstring = "";
//Run the SQL
$rsQueryResults = RunQuery($sSQL);
if (mysql_error() != "")
{
$sCSVstring = gettext("An error occured: ") . mysql_errno() . "--" . mysql_error();
}
else
{
//Loop through the fields and write the header row
for ($iCount = 0; $iCount < mysql_num_fields($rsQueryResults); $iCount++)
{
$sCSVstring .= mysql_field_name($rsQueryResults,$iCount) . ",";
}
$sCSVstring .= "\n";
//Loop through the recordsert
while($aRow =mysql_fetch_array($rsQueryResults))
{
//Loop through the fields and write each one
for ($iCount = 0; $iCount < mysql_num_fields($rsQueryResults); $iCount++)
{
$outStr = str_replace ('"', '""', $aRow[$iCount]);
$sCSVstring .= "\"" . $outStr . "\",";
}
$sCSVstring .= "\n";
}
}
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=Query-" . date("Ymd-Gis") . ".csv");
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo $sCSVstring;
exit;
}
function RunFreeQuery()
{
global $cnInfoCentral;
global $aRowClass;
global $rsQueryResults;
global $sSQL;
global $iQueryID;
//Run the SQL
$rsQueryResults = RunQuery($sSQL);
if (mysql_error() != "")
{
echo gettext("An error occured: ") . mysql_errno() . "--" . mysql_error();
}
else
{
$sRowClass = "RowColorA";
echo '<table align="center" cellpadding="5" cellspacing="0">';
echo '<tr class="' . $sRowClass . '">';
//Loop through the fields and write the header row
for ($iCount = 0; $iCount < mysql_num_fields($rsQueryResults); $iCount++)
{
echo ' <td align="center">
<b>' . mysql_field_name($rsQueryResults,$iCount) . '</b>
</td>';
}
echo '</tr>';
//Loop through the recordsert
while($aRow =mysql_fetch_array($rsQueryResults))
{
$sRowClass = AlternateRowStyle($sRowClass);
echo '<tr class="' . $sRowClass . '">';
//Loop through the fields and write each one
for ($iCount = 0; $iCount < mysql_num_fields($rsQueryResults); $iCount++)
{
echo '<td align="center">' . $aRow[$iCount] . '</td>';
}
echo '</tr>';
}
echo '</table>';
echo '<br><p class="ShadedBox" style="border-style: solid; margin-left: 50px; margin-right: 50 px; border-width: 1px;"><span class="SmallText">' . str_replace(Chr(13),"<br>",htmlspecialchars($sSQL)) . '</span></p>';
}
}
require "Include/Footer.php";
?>