/ www / html / programs / sqlbyline /
/www/html/programs/sqlbyline/index.php
1 <?php
2 /* Copyright (C) 2012, 2013 The Meme Factory, Inc., http://www.meme.com/
3 * Copyright (C) 2006-2009, Karl O. Pinc
4 *
5 * This file is part of Gombe-MI.
6 *
7 * Gombe-MI is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Gombe-MI. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * Execute sql, one statement or more per line.
21 *
22 * Karl O. Pinc <kop@meme.com>
23 *
24 * Remarks:
25 * SQL statements must appear on a single line.
26 * Upon error, remining statements on the line are ignored and
27 * subsqeuent lines are executed.
28 *
29 * Shows errors, but no results.
30 */
31
32 // Includes
33 require('constants.php.inc');
34 require('lib.php.inc');
35 require('lib_upload.php.inc');
36
37 //
38 // Constants
39 //
40
41 // Delimiter separating sql statements.
42 define('DELIM', "\r\n");
43
44 //
45 // Functions.
46 //
47
48
49 function execute_sql(&$upload_form, &$dbconn, $form_sql) {
50 /*
51 * Execute the sql and show the result.
52 *
53 * Input:
54 * $upload_form An instance of the Upload_form class.
55 * $dbconn An instance of the Upload_connection class.
56 * $form_sql Sql code to execute if no file content is uploaded.
57 *
58 * Returns: n/a
59 *
60 * Side Effects:
61 * Outputs html.
62 *
63 * Remarks:
64 * One of the main pages of the program.
65 */
66
67 // Get the uploaded data.
68 $upload_object = new Uploaded_data($upload_form);
69 if ($upload_object->filename == '') {
70 $sql = explode(DELIM, $form_sql);
71 } else {
72 if (!$upload_object->upload()) {
73 return;
74 }
75 $sql = $upload_object->data;
76 }
77
78 //
79 // Connect to the db.
80 //
81 if (!$dbconn->connect()) {
82 return;
83 }
84
85 //
86 // Go through all the data and load it.
87 //
88 $faulting = FALSE;
89 $lines = count($sql);
90
91 for ($line = 1; $line <= $lines; $line++) {
92
93 $this_sql = $sql[$line - 1];
94 if ($this_sql !== '' && !$dbconn->sql($this_sql)) {
95 // Record the error.
96 // Let the user try again.
97 $upload_form->error($line
98 , 'PostgreSQL will not execute your statement, it reports the error: '
99 . htmlentities($dbconn->last_error())
100 , $faulting
101 , 'The sql statement is: '
102 . htmlentities($sql[$line - 1]));
103 $faulting = TRUE;
104 }
105 }
106
107 $upload_object->success(); // Remember uploaded file.
108 if ($faulting) {
109 $upload_form->show('<p><em class="error">'
110 . 'Errors occurred</em> during the execution of '
111 . $lines
112 . ' lines of sql.</p>');
113 } else {
114 //
115 // Everything seems to have worked.
116 // Allow the user to do it again.
117 //
118 $upload_form->show('<p><em class="success">'
119 . 'Successfully executed</em> '
120 . $lines
121 . ' lines of sql<em class="success">!</em></p>');
122 }
123
124 return;
125 }
126
127 //
128 // Body of code
129 //
130
131 maintain_login_session();
132
133 show_html_start('sqlbyline -- Execute multiple lines of sql');
134
135 ?>
136
137 <h1>Execute multiple lines of SQL</h1>
138
139 <?php
140
141 //
142 // Get the form data.
143 //
144
145 // $action
146 // What page we're in.
147 if (array_key_exists('action', $_REQUEST)) {
148 $action = $_REQUEST['action'];
149 } else {
150 $action = ACTION_NONE;
151 }
152
153 // $db
154 $db = get_session_var('db', TRUE);
155
156 // $user
157 $user = get_session_var('user', TRUE);
158
159 // $password
160 $password = get_session_var('password', FALSE);
161
162 // $sql
163 $sql = get_session_var('sql', FALSE);
164
165 //error_log('p: ' . crypt($password, substr('12345678901234',0, CRYPT_SALT_LENGTH)) . ': ' . ($password === ''));
166
167 // Instantiate the sql form.
168 $upload_form =& new Upload_form($db, $user, $password, 'SQL to execute');
169
170 $upload_form->extra_input
171 = array('4'
172 => array('label'
173 => '<label for="sql_id">SQL:</label>'
174 , 'data'
175 => '<textarea name="sql" id="sql_id" tabindex="4" rows="20" cols="72">' . htmlentities($sql, ENT_QUOTES) . '</textarea>'));
176
177 //
178 // Process input values
179 //
180
181 //
182 // Dispatch on action
183 //
184 switch ($action) {
185 case ACTION_NONE:
186 $upload_form->show();
187 break;
188
189 case ACTION_UPLOAD:
190 // Make a database connection.
191 $dbconn =& new Upload_connection($db
192 , $user
193 , $password
194 , $upload_form);
195 // Do the deed.
196 execute_sql($upload_form, $dbconn, $sql);
197 break;
198 }
199
200 // Finish the stock page.
201
202 show_html_finish();
203
204 return; // End of program.