3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Gets some core libraries
8 require('./libraries/read_dump.lib.php3');
9 require('./libraries/grab_globals.lib.php3');
10 require('./libraries/common.lib.php3');
14 * Increases the max. allowed time to run a script
16 @set_time_limit
($cfg['ExecTimeLimit']);
20 * Defines the url to return to in case of error in a sql statement
22 if (!isset($goto) ||
!eregi('^(db_details|tbl_properties)(_[a-z]*)?\.php3$', $goto)) {
23 $goto = 'db_details.php3';
26 . '?' . PMA_generate_common_url($db)
27 . (($goto == 'tbl_properties.php3') ?
'&table=' . urlencode($table) : '');
31 * Set up default values for some variables
34 $sql_bookmark = isset($sql_bookmark) ?
$sql_bookmark : '';
35 $sql_query = isset($sql_query) ?
$sql_query : '';
36 if (!empty($sql_localfile) && $cfg['UploadDir'] != '') {
37 $sql_file = $cfg['UploadDir'] . $sql_localfile;
38 } else if (empty($sql_file)) {
44 * Bookmark Support: get a query back from bookmark if required
46 if (!empty($id_bookmark)) {
47 include('./libraries/bookmark.lib.php3');
48 switch ($action_bookmark) {
49 case 0: // bookmarked query that have to be run
50 $sql_query = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
51 if (isset($bookmark_variable) && !empty($bookmark_variable)) {
52 $sql_query = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $sql_query);
55 case 1: // bookmarked query that have to be displayed
56 $sql_query = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
59 case 2: // bookmarked query that have to be deleted
60 $sql_query = PMA_deleteBookmarks($db, $cfg['Bookmark'], $id_bookmark);
67 * Prepares the sql query
69 // Gets the query from a file if required
70 if ($sql_file != 'none') {
71 // loic1 : fixed a security issue
72 // if ((file_exists($sql_file) && is_uploaded_file($sql_file))
73 // || file_exists($cfg['UploadDir'] . $sql_localfile)) {
74 if (file_exists($sql_file)
75 && ((isset($sql_localfile) && $sql_file == $cfg['UploadDir'] . $sql_localfile) ||
is_uploaded_file($sql_file))) {
77 if (PMA_PHP_INT_VERSION
>= 40000) {
78 $open_basedir = @ini_get
('open_basedir');
80 if (empty($open_basedir)) {
81 $open_basedir = @get_cfg_var
('open_basedir');
84 // If we are on a server with open_basedir, we must move the file
85 // before opening it. The doc explains how to create the "./tmp"
88 if (!empty($open_basedir)) {
90 $tmp_subdir = (PMA_IS_WINDOWS ?
'.\\tmp\\' : './tmp/');
92 // function is_writeable() is valid on PHP3 and 4
93 if (!is_writeable($tmp_subdir)) {
94 // if we cannot move the file, let PHP report the error
95 error_reporting(E_ALL
);
96 $sql_query = PMA_readFile($sql_file, $sql_file_compression);
99 $sql_file_new = $tmp_subdir . basename($sql_file);
100 if (PMA_PHP_INT_VERSION
< 40003) {
101 copy($sql_file, $sql_file_new);
103 move_uploaded_file($sql_file, $sql_file_new);
105 $sql_query = PMA_readFile($sql_file_new, $sql_file_compression);
106 unlink($sql_file_new);
110 // read from the normal upload dir
111 $sql_query = PMA_readFile($sql_file, $sql_file_compression);
114 // Convert the file's charset if necessary
115 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
116 && isset($charset_of_file) && $charset_of_file != $charset) {
117 $sql_query = PMA_convert_string($charset_of_file, $charset, $sql_query);
119 } // end uploaded file stuff
122 // Kanji convert SQL textfile 2002/1/4 by Y.Kawada
123 if (@function_exists
('PMA_kanji_str_conv')) {
124 $sql_tmp = trim($sql_query);
125 PMA_change_enc_order();
126 $sql_query = PMA_kanji_str_conv($sql_tmp, $knjenc, isset($xkana) ?
$xkana : '');
127 PMA_change_enc_order();
129 $sql_query = trim($sql_query);
132 // $sql_query come from the query textarea, if it's a reposted query gets its
134 if (!empty($prev_sql_query)) {
135 $prev_sql_query = urldecode($prev_sql_query);
136 if ($sql_query == trim(htmlspecialchars($prev_sql_query))) {
137 $sql_query = $prev_sql_query;
141 // Drop database is not allowed -> ensure the query can be run
142 if (!$cfg['AllowUserDropDatabase']
143 && eregi('DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE ', $sql_query)) {
144 // Checks if the user is a Superuser
145 // TODO: set a global variable with this information
146 // loic1: optimized query
147 $result = @PMA_mysql_query
('USE mysql');
148 if (PMA_mysql_error()) {
149 include('./header.inc.php3');
150 PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
153 define('PMA_CHK_DROP', 1);
158 if ($sql_query != '') {
160 PMA_splitSqlFile($pieces, $sql_query, PMA_MYSQL_INT_VERSION
);
161 $pieces_count = count($pieces);
162 if ($pieces_count > 1) {
166 // Copy of the cleaned sql statement for display purpose only (see near the
167 // beginning of "db_details.php3" & "tbl_properties.php3")
168 if ($sql_file != 'none' && $pieces_count > 10) {
169 // Be nice with bandwidth...
170 $sql_query_cpy = $sql_query = '';
172 $sql_query_cpy = implode(";\n", $pieces) . ';';
173 // Be nice with bandwidth... for now, an arbitrary limit of 500,
174 // could be made configurable but probably not necessary
175 if (strlen($sql_query_cpy) > 500) {
176 $sql_query_cpy = $sql_query = '';
180 // really run the query?
181 if ($view_bookmark == 0) {
182 // Only one query to run
183 if ($pieces_count == 1 && !empty($pieces[0])) {
184 $sql_query = $pieces[0];
185 if (eregi('^(DROP|CREATE)[[:space:]]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)', $sql_query)) {
188 include('./sql.php3');
192 // Runs multiple queries
193 else if (PMA_mysql_select_db($db)) {
195 for ($i = 0; $i < $pieces_count; $i++
) {
196 $a_sql_query = $pieces[$i];
197 if ($i == $pieces_count - 1 && eregi('^SELECT', $a_sql_query)) {
198 $complete_query = $sql_query;
199 $sql_query = $a_sql_query;
200 include('./sql.php3');
203 $result = PMA_mysql_query($a_sql_query);
204 if ($result == FALSE) { // readdump failed
205 $my_die = $a_sql_query;
208 if (!isset($reload) && eregi('^(DROP|CREATE)[[:space:]]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)', $a_sql_query)) {
213 } // end if (really run the query)
222 if (isset($my_die)) {
223 $js_to_run = 'functions.js';
224 include('./header.inc.php3');
225 PMA_mysqlDie('', $my_die, '', $err_url);
230 * Go back to the calling script
232 // Checks for a valid target script
233 if (isset($table) && $table == '') {
236 if (isset($db) && $db == '') {
239 $is_db = $is_table = FALSE;
240 if ($goto == 'tbl_properties.php3') {
241 if (!isset($table)) {
242 $goto = 'db_details.php3';
244 PMA_mysql_select_db($db);
245 $is_table = @PMA_mysql_query
('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
246 if (!($is_table && @mysql_numrows
($is_table))) {
247 $goto = 'db_details.php3';
250 } // end if... else...
252 if ($goto == 'db_details.php3') {
259 $is_db = @PMA_mysql_select_db
($db);
264 } // end if... else...
266 // Defines the message to be displayed
267 if (!empty($id_bookmark) && $action_bookmark == 2) {
268 $message = $strBookmarkDeleted;
269 } else if (!isset($sql_query_cpy)) {
270 $message = $strNoQuery;
271 } else if ($sql_query_cpy == '') {
272 $message = "$strSuccess :<br />$strTheContent ($pieces_count $strInstructions) ";
274 $message = $strSuccess;
276 // Loads to target script
277 if ($goto == 'db_details.php3' ||
$goto == 'tbl_properties.php3') {
278 $js_to_run = 'functions.js';
280 if ($goto != 'main.php3') {
281 include('./header.inc.php3');
283 require('./' . $goto);