home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / htdocs / phpmyadmin / sql.php < prev    next >
PHP Script  |  2002-01-06  |  16KB  |  439 lines

  1. <?php
  2. /* $Id: sql.php,v 1.78 2001/12/18 21:52:21 loic1 Exp $ */
  3.  
  4.  
  5. /**
  6.  * Gets some core libraries
  7.  */
  8. require('./libraries/grab_globals.lib.php');
  9. require('./libraries/common.lib.php');
  10.  
  11.  
  12. /**
  13.  * Defines the url to return to in case of error in a sql statement
  14.  */
  15. if (empty($goto)) {
  16.     $goto    = (empty($table)) ? 'db_details.php' : 'tbl_properties.php';
  17. }
  18. if (!isset($err_url)) {
  19.     $err_url = $goto
  20.              . '?lang=' . $lang
  21.              . '&server=' . $server
  22.              . (isset($db) ? '&db=' . urlencode($db) : '')
  23.              . (($goto != 'db_details.php' && isset($table)) ? '&table=' . urlencode($table) : '');
  24. }
  25.  
  26.  
  27. /** 
  28.  * Check rights in case of DROP DATABASE
  29.  *
  30.  * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
  31.  * but since a malicious user may pass this variable by url/form, we don't take
  32.  * into account this case.
  33.  */
  34. if (!defined('PMA_CHK_DROP')
  35.     && !$cfgAllowUserDropDatabase
  36.     && eregi('DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE ', $sql_query)) {
  37.     // Checks if the user is a Superuser
  38.     // TODO: set a global variable with this information
  39.     // loic1: optimized query
  40.     $result = @mysql_query('USE mysql');
  41.     if (mysql_error()) {
  42.         include('./header.inc.php');
  43.         PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
  44.     } // end if
  45. } // end if
  46.  
  47.  
  48. /**
  49.  * Bookmark add
  50.  */
  51. if (isset($store_bkm)) {
  52.     if (get_magic_quotes_gpc()) {
  53.         $fields['label'] = stripslashes($fields['label']);
  54.     }
  55.     include('./libraries/bookmark.lib.php');
  56.     PMA_addBookmarks($fields, $cfgBookmark);
  57.     header('Location: ' . $cfgPmaAbsoluteUri . $goto);
  58. }
  59.  
  60.  
  61. /**
  62.  * Gets the true sql query
  63.  */
  64. // $sql_query has been urlencoded in the confirmation form for drop/delete
  65. // queries or in the navigation bar for browsing among records
  66. if (isset($btnDrop) || isset($navig)) {
  67.     $sql_query = urldecode($sql_query);
  68. }
  69.  
  70.  
  71. /**
  72.  * Sets or modifies the $goto variable if required
  73.  */
  74. if ($goto == 'sql.php') {
  75.     $goto = 'sql.php'
  76.           . '?lang=' . $lang
  77.           . '&server=' . $server
  78.           . '&db=' . urlencode($db)
  79.           . '&table=' . urlencode($table)
  80.           . '&pos=' . $pos
  81.           . '&sql_query=' . urlencode($sql_query);
  82. }
  83.  
  84.  
  85. /**
  86.  * Go back to further page if table should not be dropped
  87.  */
  88. if (isset($btnDrop) && $btnDrop == $strNo) {
  89.     if (!empty($back)) {
  90.         $goto = $back;
  91.     }
  92.     if (@file_exists('./' . $goto)) {
  93.         if ($goto == 'db_details.php' && !empty($table)) {
  94.             unset($table);
  95.         }
  96.         include('./' . ereg_replace('\.\.*', '.', $goto));
  97.     } else {
  98.         header('Location: ' . $cfgPmaAbsoluteUri . str_replace('&', '&', $goto));
  99.     }
  100.     exit();
  101. } // end if
  102.  
  103.  
  104. /**
  105.  * Displays the confirm page if required
  106.  *
  107.  * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
  108.  * with js) because possible security issue is not so important here: at most,
  109.  * the confirm message isn't displayed.
  110.  */
  111. if (!$cfgConfirm
  112.     || (isset($is_js_confirmed) && $is_js_confirmed)
  113.     || isset($btnDrop)) {
  114.     $do_confirm = FALSE;
  115. } else {
  116.     $do_confirm = (eregi('DROP[[:space:]]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)|ALTER TABLE +((`[^`]+`)|([A-Za-z0-9_$]+)) +DROP|DELETE FROM', $sql_query));
  117. }
  118.  
  119. if ($do_confirm) {
  120.     if (get_magic_quotes_gpc()) {
  121.         $stripped_sql_query = stripslashes($sql_query);
  122.     } else {
  123.         $stripped_sql_query = $sql_query;
  124.     }
  125.     include('./header.inc.php');
  126.     echo $strDoYouReally . ' :<br />' . "\n";
  127.     echo '<tt>' . htmlspecialchars($stripped_sql_query) . '</tt> ?<br/>' . "\n";
  128.     ?>
  129. <form action="sql.php" method="post">
  130.     <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
  131.     <input type="hidden" name="server" value="<?php echo $server; ?>" />
  132.     <input type="hidden" name="db" value="<?php echo $db; ?>" />
  133.     <input type="hidden" name="table" value="<?php echo isset($table) ? $table : ''; ?>" />
  134.     <input type="hidden" name="sql_query" value="<?php echo urlencode($sql_query); ?>" />
  135.     <input type="hidden" name="zero_rows" value="<?php echo isset($zero_rows) ? $zero_rows : ''; ?>" />
  136.     <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  137.     <input type="hidden" name="back" value="<?php echo isset($back) ? $back : ''; ?>" />
  138.     <input type="hidden" name="reload" value="<?php echo isset($reload) ? $reload : 0; ?>" />
  139.     <input type="hidden" name="show_query" value="<?php echo isset($show_query) ? $show_query : ''; ?>" />
  140.     <input type="submit" name="btnDrop" value="<?php echo $strYes; ?>" />
  141.     <input type="submit" name="btnDrop" value="<?php echo $strNo; ?>" />
  142. </form>
  143.     <?php
  144.     echo "\n";
  145. } // end if
  146.  
  147.  
  148. /**
  149.  * Executes the query and displays results
  150.  */
  151. else {
  152.     if (!isset($sql_query)) {
  153.         $sql_query = '';
  154.     } else if (get_magic_quotes_gpc()) {
  155.         $sql_query = stripslashes($sql_query);
  156.     }
  157.     // Defines some variables
  158.     // loic1: A table have to be created -> left frame should be reloaded
  159.     if ((!isset($reload) || $reload == 0)
  160.         && eregi('^CREATE TABLE (.*)', $sql_query)) {
  161.         $reload           = 1;
  162.     }
  163.     // Gets the number of rows per page
  164.     if (!isset($session_max_rows)) {
  165.         $session_max_rows = $cfgMaxRows;
  166.     } else if ($session_max_rows != 'all') {
  167.         $cfgMaxRows       = $session_max_rows;
  168.     }
  169.     // Defines the display mode (horizontal/vertical) and header "frequency"
  170.     if (empty($disp_direction)) {
  171.         $disp_direction   = $cfgDefaultDisplay;
  172.     }
  173.     if (empty($repeat_cells)) {
  174.         $repeat_cells     = $cfgRepeatCells;
  175.     }
  176.  
  177.     $is_explain = $is_select = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = FALSE;
  178.     if (eregi('^EXPLAIN[[:space:]]+', $sql_query)) {
  179.         $is_explain  = TRUE;
  180.     } else if (eregi('^SELECT[[:space:]]+', $sql_query)) {
  181.         $is_select   = TRUE;
  182.         $is_count    = (eregi('^SELECT COUNT\((.*\.+)?.*\)', $sql_query));
  183.         $is_export   = (eregi(' INTO OUTFILE ', $sql_query));
  184.     } else if (eregi('^DELETE[[:space:]]+', $sql_query)) {
  185.         $is_delete   = TRUE;
  186.         $is_affected = TRUE;
  187.     } else if (eregi('^(INSERT|LOAD DATA|REPLACE)[[:space:]]+', $sql_query)) {
  188.         $is_insert   = TRUE;
  189.         $is_affected = TRUE;
  190.     } else if (eregi('^UPDATE[[:space:]]+', $sql_query)) {
  191.         $is_affected = TRUE;
  192.     } else if (eregi('^SHOW[[:space:]]+', $sql_query)) {
  193.         $is_show     = TRUE;
  194.     } else if (eregi('^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+', $sql_query)) {
  195.         $is_maint    = TRUE;
  196.     }
  197.  
  198.     // Do append a "LIMIT" clause?
  199.     if (isset($pos)
  200.         && (!$cfgShowAll || $session_max_rows != 'all')
  201.         && ($is_select && !($is_count || $is_export) && eregi(' FROM ', $sql_query))
  202.         && !eregi(' LIMIT[ 0-9,]+$', $sql_query)) {
  203.         $sql_limit_to_append = " LIMIT $pos, $cfgMaxRows";
  204.         if (eregi('(.*)( PROCEDURE (.*)| FOR UPDATE| LOCK IN SHARE MODE)$', $sql_query, $regs)) {
  205.             $full_sql_query  = $regs[1] . $sql_limit_to_append . $regs[2];
  206.         } else {
  207.             $full_sql_query  = $sql_query . $sql_limit_to_append;
  208.         }
  209.     } else {
  210.         $full_sql_query      = $sql_query;
  211.     } // end if...else
  212.  
  213.     mysql_select_db($db);
  214.  
  215.     // If the query is a DELETE query with no WHERE clause, get the number of
  216.     // rows that will be deleted (mysql_affected_rows will always return 0 in
  217.     // this case)
  218.     if ($is_delete
  219.         && eregi('^DELETE( .+)?( FROM (.+))$', $sql_query, $parts)
  220.         && !eregi(' WHERE ', $parts[3])) {
  221.         $OPresult     = @mysql_query('SELECT COUNT(*) as count' .  $parts[2]);
  222.         if ($OPresult) {
  223.             $num_rows = mysql_result($OPresult, 0, 'count');
  224.         } else {
  225.             $num_rows = 0;
  226.         }
  227.         mysql_free_result($OPresult);
  228.     }
  229.  
  230.     // Executes the query
  231.     $result   = @mysql_query($full_sql_query);
  232.  
  233.     // Displays an error message if required and stop parsing the script
  234.     if (mysql_error()) {
  235.         $error        = mysql_error();
  236.         include('./header.inc.php');
  237.         $full_err_url = (ereg('^(db_details|tbl_properties)', $err_url))
  238.                       ? $err_url . '&show_query=y&sql_query=' . urlencode($sql_query)
  239.                       : $err_url;
  240.         PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
  241.     }
  242.  
  243.     // Gets the number of rows affected/returned
  244.     if (!$is_affected) {
  245.         $num_rows = @mysql_num_rows($result);
  246.     } else if (!isset($num_rows)) {
  247.         $num_rows = @mysql_affected_rows();
  248.     }
  249.  
  250.     // Counts the total number of rows for the same 'SELECT' query without the
  251.     // 'LIMIT' clause that may have been programatically added
  252.     if (empty($sql_limit_to_append)) {
  253.         $unlim_num_rows         = $num_rows;
  254.     }
  255.     else if ($is_select) {
  256.         // reads only the from-part of the query...
  257.         $array = split(' from | FROM | order | ORDER | having | HAVING | limit | LIMIT | group by | GROUP BY', $sql_query);
  258.         if (!empty($array[1])) {
  259.             // ... and makes a count(*) to count the entries
  260.             $count_query = 'SELECT COUNT(*) AS count FROM ' . $array[1];
  261.             $OPresult    = mysql_query($count_query);
  262.             if ($OPresult) {
  263.                 $unlim_num_rows = mysql_result($OPresult, 0, 'count');
  264.             }
  265.             mysql_free_result($OPresult);
  266.         } else {
  267.             $unlim_num_rows     = 0;
  268.         }
  269.     } // end rows total count
  270.  
  271.     // No rows returned -> move back to the calling page
  272.     if ($num_rows < 1 || $is_affected) {
  273.         if ($is_delete) {
  274.             $message = $strDeletedRows . ' ' . $num_rows;
  275.         } else if ($is_insert) {
  276.             $message = $strInsertedRows . ' ' . $num_rows;
  277.         } else if ($is_affected) {
  278.             $message = $strAffectedRows . ' ' . $num_rows;
  279.         } else if (!empty($zero_rows)) {
  280.             $message = $zero_rows;
  281.         } else {
  282.             $message = $strEmptyResultSet;
  283.         }
  284.  
  285.         if (@file_exists('./' . $goto)) {
  286.             $goto = ereg_replace('\.\.*', '.', $goto);
  287.             // Checks for a valid target script
  288.             if (isset($table) && $table == '') {
  289.                 unset($table);
  290.             }
  291.             if (isset($db) && $db == '') {
  292.                 unset($db);
  293.             }
  294.             $is_db = $is_table = FALSE;
  295.             if ($goto == 'tbl_properties.php') {
  296.                 if (!isset($table)) {
  297.                     $goto     = 'db_details.php';
  298.                 } else {
  299.                     $is_table = @mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
  300.                     if (!@mysql_numrows($is_table)) {
  301.                         $goto = 'db_details.php';
  302.                         unset($table);
  303.                     }
  304.                 } // end if... else...
  305.             }
  306.             if ($goto == 'db_details.php') {
  307.                 if (isset($table)) {
  308.                     unset($table);
  309.                 }
  310.                 if (!isset($db)) {
  311.                     $goto     = 'main.php';
  312.                 } else {
  313.                     $is_db    = @mysql_select_db($db);
  314.                     if (!$is_db) {
  315.                         $goto = 'main.php';
  316.                         unset($db);
  317.                     }
  318.                 } // end if... else...
  319.             }
  320.             // Loads to target script
  321.             if ($goto == 'db_details.php' || $goto == 'tbl_properties.php') {
  322.                 $js_to_run = 'functions.js';
  323.             }
  324.             if ($goto != 'main.php') {
  325.                 include('./header.inc.php');
  326.             }
  327.             include('./' . $goto);
  328.         } // end if file_exist
  329.         else {
  330.             header('Location: ' . $cfgPmaAbsoluteUri . str_replace('&', '&', $goto) . '&message=' . $message);
  331.         } // end else
  332.         exit();
  333.     } // end no rows returned
  334.  
  335.     // At least one row is returned -> displays a table with results
  336.     else {
  337.         // Displays the headers
  338.         if (isset($show_query)) {
  339.             unset($show_query);
  340.         }
  341.         $js_to_run = 'functions.js';
  342.         include('./header.inc.php');
  343.         include('./libraries/bookmark.lib.php');
  344.  
  345.         // Gets the list of fields properties
  346.         while ($field = mysql_fetch_field($result)) {
  347.             $fields_meta[] = $field;
  348.         }
  349.         $fields_cnt        = count($fields_meta);
  350.  
  351.         // Displays the results in a table
  352.         include('./libraries/display_tbl.lib.php');
  353.         if (empty($disp_mode)) {
  354.             // see the "PMA_setDisplayMode()" function in
  355.             // libraries/display_tbl.lib.php
  356.             $disp_mode = 'urdr11110';
  357.         }
  358.         PMA_displayTable($result, $disp_mode);
  359.         mysql_free_result($result);
  360.  
  361.         // Displays "Insert a new row" link if required
  362.         if ($disp_mode[6] == '1') {
  363.             $lnk_goto  = 'sql.php'
  364.                        . '?lang=' . $lang
  365.                        . '&server=' . $server
  366.                        . '&db=' . urlencode($db)
  367.                        . '&table=' . urlencode($table)
  368.                        . '&pos=' . $pos
  369.                        . '&session_max_rows=' . $session_max_rows
  370.                        . '&disp_direction=' . $disp_direction
  371.                        . '&repeat_cells=' . $repeat_cells
  372.                        . '&sql_query=' . urlencode($sql_query);
  373.             $url_query = 'lang=' . $lang
  374.                        . '&server=' . $server
  375.                        . '&db=' . urlencode($db)
  376.                        . '&table=' . urlencode($table)
  377.                        . '&pos=' . $pos
  378.                        . '&session_max_rows=' . $session_max_rows
  379.                        . '&disp_direction=' . $disp_direction
  380.                        . '&repeat_cells=' . $repeat_cells
  381.                        . '&sql_query=' . urlencode($sql_query)
  382.                        . '&goto=' . urlencode($lnk_goto);
  383.  
  384.             echo "\n\n";
  385.             echo '<!-- Insert a new row -->' . "\n";
  386.             echo '<p>' . "\n";
  387.             echo '    <a href="tbl_change.php?' . $url_query . '">' . $strInsertNewRow . '</a>' . "\n";
  388.             echo '</p>' . "\n";
  389.         } // end insert new row
  390.  
  391.         // Bookmark Support if required
  392.         if ($disp_mode[7] == '1'
  393.             && ($cfgBookmark['db'] && $cfgBookmark['table'] && empty($id_bookmark))
  394.             && !empty($sql_query)) {
  395.             echo "\n";
  396.  
  397.             $goto = 'sql.php'
  398.                   . '?lang=' . $lang
  399.                   . '&server=' . $server
  400.                   . '&db=' . urlencode($db)
  401.                   . '&table=' . urlencode($table)
  402.                   . '&pos=' . $pos
  403.                   . '&session_max_rows=' . $session_max_rows
  404.                   . '&disp_direction=' . $disp_direction
  405.                   . '&repeat_cells=' . $repeat_cells
  406.                   . '&sql_query=' . urlencode($sql_query)
  407.                   . '&id_bookmark=1';
  408.             ?>
  409. <!-- Bookmark the query -->
  410. <form action="sql.php" method="post" onsubmit="return emptyFormElements(this, 'fields[label]');">
  411.             <?php
  412.             echo "\n";
  413.             if ($disp_mode[3] == '1') {
  414.                 echo '    <i>' . $strOr . '</i>' . "\n";
  415.             }
  416.             ?>
  417.     <br /><br />
  418.     <?php echo $strBookmarkLabel; ?> :
  419.     <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  420.     <input type="hidden" name="fields[dbase]" value="<?php echo $db; ?>" />
  421.     <input type="hidden" name="fields[user]" value="<?php echo $cfgBookmark['user']; ?>" />
  422.     <input type="hidden" name="fields[query]" value="<?php echo urlencode($sql_query); ?>" />
  423.     <input type="text" name="fields[label]" value="" />
  424.     <input type="submit" name="store_bkm" value="<?php echo $strBookmarkThis; ?>" />
  425. </form>
  426.             <?php
  427.         } // end bookmark support
  428.     } // end rows returned
  429.  
  430. } // end executes the query
  431. echo "\n\n";
  432.  
  433.  
  434. /**
  435.  * Displays the footer
  436.  */
  437. require('./footer.inc.php');
  438. ?>
  439.