/** * Executes a query as controluser if possible, otherwise as normal user * * @param string the query to execute * @param boolean whether to display SQL error messages or not * * @return integer the result id * * @global string the URL of the page to show in case of error * @global string the name of db to come back to * @global integer the ressource id of DB connect as controluser * @global array configuration infos about the relations stuff * * @access public * * @author Mike Beck <*****@*****.**> */ function PMA_query_as_cu($sql, $show_error = TRUE) { global $err_url_0, $db, $dbh, $cfgRelation; if (isset($dbh)) { PMA_mysql_select_db($cfgRelation['db'], $dbh); $result = @PMA_mysql_query($sql, $dbh); if (!$result && $show_error == TRUE) { PMA_mysqlDie(mysql_error($dbh), $sql, '', $err_url_0); } PMA_mysql_select_db($db, $dbh); } else { PMA_mysql_select_db($cfgRelation['db']); $result = @PMA_mysql_query($sql); if ($result && $show_error == TRUE) { PMA_mysqlDie('', $sql, '', $err_url_0); } PMA_mysql_select_db($db); } // end if... else... if ($result) { return $result; } else { return FALSE; } }
/** * Returns $table's field types * * @param string the database name * @param string the table name * * @return array the field types; key of array is PMA_backquote * of the field name * * @access public * * This function exists because mysql_field_type() returns 'blob' * even for 'text' fields. */ function PMA_fieldTypes($db, $table, $use_backquotes) { PMA_mysql_select_db($db); $table_def = PMA_mysql_query('SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table)); while ($row = @PMA_mysql_fetch_array($table_def)) { $types[PMA_backquote($row['Field'], $use_backquotes)] = ereg_replace('\\(.*', '', $row['Type']); } return $types; }
$is_table = @PMA_mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\''); if (!($is_table && @mysql_numrows($is_table))) { $goto = 'db_details.php'; unset($table); } } // end if... else... } if ($goto == 'db_details.php') { if (isset($table)) { unset($table); } if (!isset($db)) { $goto = 'main.php'; } else { $is_db = @PMA_mysql_select_db($db); if (!$is_db) { $goto = 'main.php'; unset($db); } } // end if... else... } // Defines the message to be displayed if (!empty($id_bookmark) && $action_bookmark == 2) { $message = $strBookmarkDeleted; } else { if (!isset($sql_query_cpy)) { if (empty($message)) { $message = $strNoQuery; }
$pdf->Cell(30,8,$res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'],1,0,'L'); } } if ($cfgRelation['commwork']) { if (isset($comments[$field_name])) { $pdf->Cell(0,8,$comments[$field_name],1,0,'L'); } } */ } // end while $pdf->SetFont('', '', 14); mysql_free_result($result); } //end each } // end function PMA_RT_DOC /** * Main logic */ if (!isset($pdf_page_number)) { $pdf_page_number = 1; } $show_grid = isset($show_grid) && $show_grid == 'on' ? 1 : 0; $show_color = isset($show_color) && $show_color == 'on' ? 1 : 0; $show_table_dimension = isset($show_table_dimension) && $show_table_dimension == 'on' ? 1 : 0; $all_tab_same_wide = isset($all_tab_same_wide) && $all_tab_same_wide == 'on' ? 1 : 0; $with_doc = isset($with_doc) && $with_doc == 'on' ? 1 : 0; $orientation = isset($orientation) && $orientation == 'P' ? 'P' : 'L'; $paper = isset($paper) ? $paper : 'A4'; PMA_mysql_select_db($db); $rt = new PMA_RT($pdf_page_number, $show_table_dimension, $show_color, $show_grid, $all_tab_same_wide, $orientation, $paper);
function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false) { global $cfgRelation; /** * Gets fields properties */ PMA_mysql_select_db($db); $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table); $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url); $fields_cnt = mysql_num_rows($result); // Check if we can use Relations (Mike Beck) if ($do_relation && !empty($cfgRelation['relation'])) { // Find which tables are related with the current one and write it in // an array $res_rel = PMA_getForeigners($db, $table); if ($res_rel && count($res_rel) > 0) { $have_rel = TRUE; } else { $have_rel = FALSE; } } else { $have_rel = FALSE; } // end if /** * Get the unique keys in the table */ $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db); $keys_result = PMA_mysql_query($keys_query) or PMA_mysqlDie('', $keys_query, '', $error_url); $unique_keys = array(); while ($key = PMA_mysql_fetch_array($keys_result)) { if ($key['Non_unique'] == 0) { $unique_keys[] = $key['Column_name']; } } /** * Displays the table structure */ $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strStructure'] . ': ' . $table . $crlf . '%' . $crlf . ' \\begin{longtable}{'; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } $columns_cnt = 4; $alignment = '|l|c|c|c|'; if ($do_relation && $have_rel) { $columns_cnt++; $alignment .= 'l|'; } if ($do_comments && $cfgRelation['commwork']) { $columns_cnt++; $alignment .= 'l|'; } if ($do_mime && $cfgRelation['mimework']) { $columns_cnt++; $alignment .= 'l|'; } $buffer = $alignment . '} ' . $crlf; $header .= ' \\hline '; $header .= '\\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strField'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strType'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strNull'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strDefault'] . '}}'; if ($do_relation && $have_rel) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strLinksTo'] . '}}'; } if ($do_comments && $cfgRelation['commwork']) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strComments'] . '}}'; $comments = PMA_getComments($db, $table); } if ($do_mime && $cfgRelation['mimework']) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}'; $mime_map = PMA_getMIME($db, $table, true); } $local_buffer = PMA_texEscape($table); // Table caption for first page and label if (isset($GLOBALS['latex_caption'])) { $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_caption']) . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_structure_label']) . '} \\\\' . $crlf; } $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf; // Table caption on next pages if (isset($GLOBALS['latex_caption'])) { $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_continued_caption']) . '} \\\\ ' . $crlf; } $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot '; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } while ($row = PMA_mysql_fetch_array($result)) { $type = $row['Type']; // reformat mysql query output - staybyte - 9. June 2001 // loic1: set or enum types: slashes single quotes inside options if (eregi('^(set|enum)\\((.+)\\)$', $type, $tmp)) { $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1); $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')'; $type_nowrap = ''; $binary = 0; $unsigned = 0; $zerofill = 0; } else { $type_nowrap = ' nowrap="nowrap"'; $type = eregi_replace('BINARY', '', $type); $type = eregi_replace('ZEROFILL', '', $type); $type = eregi_replace('UNSIGNED', '', $type); if (empty($type)) { $type = ' '; } $binary = eregi('BINARY', $row['Type'], $test); $unsigned = eregi('UNSIGNED', $row['Type'], $test); $zerofill = eregi('ZEROFILL', $row['Type'], $test); } $strAttribute = ' '; if ($binary) { $strAttribute = 'BINARY'; } if ($unsigned) { $strAttribute = 'UNSIGNED'; } if ($zerofill) { $strAttribute = 'UNSIGNED ZEROFILL'; } if (!isset($row['Default'])) { if ($row['Null'] != '') { $row['Default'] = 'NULL'; } } else { $row['Default'] = $row['Default']; } $field_name = $row['Field']; $local_buffer = $field_name . "" . $type . "" . ($row['Null'] == '' ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . "" . (isset($row['Default']) ? $row['Default'] : ''); if ($do_relation && $have_rel) { $local_buffer .= ""; if (isset($res_rel[$field_name])) { $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')'; } } if ($do_comments && $cfgRelation['commwork']) { $local_buffer .= ""; if (isset($comments[$field_name])) { $local_buffer .= $comments[$field_name]; } } if ($do_mime && $cfgRelation['mimework']) { $local_buffer .= ""; if (isset($mime_map[$field_name])) { $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']); } } $local_buffer = PMA_texEscape($local_buffer); if ($row['Key'] == 'PRI') { $pos = strpos($local_buffer, ""); $local_buffer = '\\textit{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos); } if (in_array($field_name, $unique_keys)) { $pos = strpos($local_buffer, ""); $local_buffer = '\\textbf{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos); } $buffer = str_replace("", ' & ', $local_buffer); $buffer .= ' \\\\ \\hline ' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } } // end while mysql_free_result($result); $buffer = ' \\end{longtable}' . $crlf; return PMA_exportOutputHandler($buffer); }
/** * Get the list and number of available databases. * * @param string the url to go back to in case of error * * @return boolean always true * * @global array the list of available databases * @global integer the number of available databases */ function PMA_availableDatabases($error_url = '') { global $dblist; global $num_dbs; $num_dbs = count($dblist); // 1. A list of allowed databases has already been defined by the // authentification process -> gets the available databases list if ($num_dbs) { $true_dblist = array(); for ($i = 0; $i < $num_dbs; $i++) { $dblink = @PMA_mysql_select_db($dblist[$i]); if ($dblink) { $true_dblist[] = $dblist[$i]; } // end if } // end for $dblist = array(); $dblist = $true_dblist; unset($true_dblist); $num_dbs = count($dblist); } else { $dbs = mysql_list_dbs() or PMA_mysqlDie('', 'SHOW DATABASES;', FALSE, $error_url); $num_dbs = $dbs ? @mysql_num_rows($dbs) : 0; $real_num_dbs = 0; for ($i = 0; $i < $num_dbs; $i++) { $db_name_tmp = PMA_mysql_dbname($dbs, $i); $dblink = @PMA_mysql_select_db($db_name_tmp); if ($dblink) { $dblist[] = $db_name_tmp; $real_num_dbs++; } } // end for mysql_free_result($dbs); $num_dbs = $real_num_dbs; } // end else return TRUE; }
/** * Finds all related tables * * @param string wether to go from master to foreign or vice versa * * @return boolean always TRUE * * @global array the list of tables that we still couldn't connect * @global array the list of allready connected tables * @global string the current databse name * @global string the super user connection id * @global array the list of relation settings * * @access private */ function PMA_getRelatives($from) { global $tab_left, $tab_know, $fromclause; global $dbh, $db, $cfgRelation; if ($from == 'master') { $to = 'foreign'; } else { $to = 'master'; } $in_know = '(\'' . implode('\', \'', $tab_know) . '\')'; $in_left = '(\'' . implode('\', \'', $tab_left) . '\')'; $rel_query = 'SELECT *' . ' FROM ' . PMA_backquote($cfgRelation['relation']) . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND ' . $from . '_table IN ' . $in_know . ' AND ' . $to . '_table IN ' . $in_left; if (isset($dbh)) { PMA_mysql_select_db($cfgRelation['db'], $dbh); $relations = @PMA_mysql_query($rel_query, $dbh) or PMA_mysqlDie(PMA_mysql_error($dbh), $rel_query, '', $err_url_0); PMA_mysql_select_db($db, $dbh); } else { PMA_mysql_select_db($cfgRelation['db']); $relations = @PMA_mysql_query($rel_query) or PMA_mysqlDie('', $rel_query, '', $err_url_0); PMA_mysql_select_db($db); } while ($row = PMA_mysql_fetch_array($relations)) { $found_table = $row[$to . '_table']; if (isset($tab_left[$found_table])) { $fromclause .= "\n" . ' LEFT JOIN ' . PMA_backquote($row[$to . '_table']) . ' ON ' . PMA_backquote($row[$from . '_table']) . '.' . PMA_backquote($row[$from . '_field']) . ' = ' . PMA_backquote($row[$to . '_table']) . '.' . PMA_backquote($row[$to . '_field']) . ' '; $tab_know[$found_table] = $found_table; $tab_left = PMA_arrayShort($tab_left, $found_table); } } // end while return TRUE; }
} if ($rs_usr) { $re0 = '(^|(\\\\\\\\)+|[^\\])'; // non-escaped wildcards $re1 = '(^|[^\\])(\\\\)+'; // escaped wildcards while ($row = PMA_mysql_fetch_row($rs_usr)) { $show_grants_dbname = substr($row[0], strpos($row[0], ' ON ') + 4, strpos($row[0], '.', strpos($row[0], ' ON ')) - strpos($row[0], ' ON ') - 4); $show_grants_str = substr($row[0], 6, strpos($row[0], ' ON ') - 6); if ($show_grants_str == 'ALL' || $show_grants_str == 'ALL PRIVILEGES' || $show_grants_str == 'CREATE' || strpos($show_grants_str, 'CREATE')) { if ($show_grants_dbname == '*') { $is_create_priv = TRUE; $db_to_create = ''; break; } else { if (ereg($re0 . '%|_', $show_grants_dbname) || !PMA_mysql_select_db($show_grants_dbname, $userlink) && @mysql_errno() != 1044) { $db_to_create = ereg_replace($re0 . '%', '\\1...', ereg_replace($re0 . '_', '\\1?', $show_grants_dbname)); $db_to_create = ereg_replace($re1 . '(%|_)', '\\1\\3', $db_to_create); // and remove backquotes $db_to_create = str_replace('`', '', $db_to_create); $is_create_priv = TRUE; break; } } // end elseif } // end if } // end while unset($show_grants_dbname, $show_grants_str); mysql_free_result($rs_usr);