home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 April (DVD) / PCWorld_2008-04_DVD.iso / temadvd / phpbb / phpBB-2.0.22.exe / phpBB2 / admin / admin_db_utilities.php < prev    next >
Encoding:
PHP Script  |  2006-12-19  |  25.7 KB  |  1,009 lines

  1. <?php
  2. /***************************************************************************
  3. *                             admin_db_utilities.php
  4. *                              -------------------
  5. *     begin                : Thu May 31, 2001
  6. *     copyright            : (C) 2001 The phpBB Group
  7. *     email                : support@phpbb.com
  8. *
  9. *     $Id: admin_db_utilities.php,v 1.42.2.14 2006/02/10 20:35:40 grahamje Exp $
  10. *
  11. ****************************************************************************/
  12.  
  13. /***************************************************************************
  14.  *
  15.  *   This program is free software; you can redistribute it and/or modify
  16.  *   it under the terms of the GNU General Public License as published by
  17.  *   the Free Software Foundation; either version 2 of the License, or
  18.  *   (at your option) any later version.
  19.  *
  20.  ***************************************************************************/
  21.  
  22. /***************************************************************************
  23. *    We will attempt to create a file based backup of all of the data in the
  24. *    users phpBB database.  The resulting file should be able to be imported by
  25. *    the db_restore.php function, or by using the mysql command_line
  26. *
  27. *    Some functions are adapted from the upgrade_20.php script and others
  28. *    adapted from the unoficial phpMyAdmin 2.2.0.
  29. ***************************************************************************/
  30.  
  31. define('IN_PHPBB', 1);
  32.  
  33. if( !empty($setmodules) )
  34. {
  35.     $filename = basename(__FILE__);
  36.     $module['General']['Backup_DB'] = $filename . "?perform=backup";
  37.  
  38.     $file_uploads = (@phpversion() >= '4.0.0') ? @ini_get('file_uploads') : @get_cfg_var('file_uploads');
  39.  
  40.     if( (empty($file_uploads) || $file_uploads != 0) && (strtolower($file_uploads) != 'off') && (@phpversion() != '4.0.4pl1') )
  41.     {
  42.         $module['General']['Restore_DB'] = $filename . "?perform=restore";
  43.     }
  44.  
  45.     return;
  46. }
  47.  
  48. //
  49. // Load default header
  50. //
  51. $no_page_header = TRUE;
  52. $phpbb_root_path = "./../";
  53. require($phpbb_root_path . 'extension.inc');
  54. require('./pagestart.' . $phpEx);
  55. include($phpbb_root_path . 'includes/sql_parse.'.$phpEx);
  56.  
  57. //
  58. // Set VERBOSE to 1  for debugging info..
  59. //
  60. define("VERBOSE", 0);
  61.  
  62. //
  63. // Increase maximum execution time, but don't complain about it if it isn't
  64. // allowed.
  65. //
  66. @set_time_limit(1200);
  67.  
  68. // -----------------------
  69. // The following functions are adapted from phpMyAdmin and upgrade_20.php
  70. //
  71. function gzip_PrintFourChars($Val)
  72. {
  73.     for ($i = 0; $i < 4; $i ++)
  74.     {
  75.         $return .= chr($Val % 256);
  76.         $Val = floor($Val / 256);
  77.     }
  78.     return $return;
  79.  
  80.  
  81.  
  82. //
  83. // This function is used for grabbing the sequences for postgres...
  84. //
  85. function pg_get_sequences($crlf, $backup_type)
  86. {
  87.     global $db;
  88.  
  89.     $get_seq_sql = "SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*'
  90.         AND relkind = 'S' ORDER BY relname";
  91.  
  92.     $seq = $db->sql_query($get_seq_sql);
  93.  
  94.     if( !$num_seq = $db->sql_numrows($seq) )
  95.     {
  96.  
  97.         $return_val = "# No Sequences Found $crlf";
  98.  
  99.     }
  100.     else
  101.     {
  102.         $return_val = "# Sequences $crlf";
  103.         $i_seq = 0;
  104.  
  105.         while($i_seq < $num_seq)
  106.         {
  107.             $row = $db->sql_fetchrow($seq);
  108.             $sequence = $row['relname'];
  109.  
  110.             $get_props_sql = "SELECT * FROM $sequence";
  111.             $seq_props = $db->sql_query($get_props_sql);
  112.  
  113.             if($db->sql_numrows($seq_props) > 0)
  114.             {
  115.                 $row1 = $db->sql_fetchrow($seq_props);
  116.  
  117.                 if($backup_type == 'structure')
  118.                 {
  119.                     $row['last_value'] = 1;
  120.                 }
  121.  
  122.                 $return_val .= "CREATE SEQUENCE $sequence start " . $row['last_value'] . ' increment ' . $row['increment_by'] . ' maxvalue ' . $row['max_value'] . ' minvalue ' . $row['min_value'] . ' cache ' . $row['cache_value'] . "; $crlf";
  123.  
  124.             }  // End if numrows > 0
  125.  
  126.             if(($row['last_value'] > 1) && ($backup_type != 'structure'))
  127.             {
  128.                 $return_val .= "SELECT NEXTVALE('$sequence'); $crlf";
  129.                 unset($row['last_value']);
  130.             }
  131.  
  132.             $i_seq++;
  133.  
  134.         } // End while..
  135.  
  136.     } // End else...
  137.  
  138.     return $returnval;
  139.  
  140. } // End function...
  141.  
  142. //
  143. // The following functions will return the "CREATE TABLE syntax for the
  144. // varying DBMS's
  145. //
  146. // This function returns, will return the table def's for postgres...
  147. //
  148. function get_table_def_postgresql($table, $crlf)
  149. {
  150.     global $drop, $db;
  151.  
  152.     $schema_create = "";
  153.     //
  154.     // Get a listing of the fields, with their associated types, etc.
  155.     //
  156.  
  157.     $field_query = "SELECT a.attnum, a.attname AS field, t.typname as type, a.attlen AS length, a.atttypmod as lengthvar, a.attnotnull as notnull
  158.         FROM pg_class c, pg_attribute a, pg_type t
  159.         WHERE c.relname = '$table'
  160.             AND a.attnum > 0
  161.             AND a.attrelid = c.oid
  162.             AND a.atttypid = t.oid
  163.         ORDER BY a.attnum";
  164.     $result = $db->sql_query($field_query);
  165.  
  166.     if(!$result)
  167.     {
  168.         message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $field_query);
  169.     } // end if..
  170.  
  171.     if ($drop == 1)
  172.     {
  173.         $schema_create .= "DROP TABLE $table;$crlf";
  174.     } // end if
  175.  
  176.     //
  177.     // Ok now we actually start building the SQL statements to restore the tables
  178.     //
  179.  
  180.     $schema_create .= "CREATE TABLE $table($crlf";
  181.  
  182.     while ($row = $db->sql_fetchrow($result))
  183.     {
  184.         //
  185.         // Get the data from the table
  186.         //
  187.         $sql_get_default = "SELECT d.adsrc AS rowdefault
  188.             FROM pg_attrdef d, pg_class c
  189.             WHERE (c.relname = '$table')
  190.                 AND (c.oid = d.adrelid)
  191.                 AND d.adnum = " . $row['attnum'];
  192.         $def_res = $db->sql_query($sql_get_default);
  193.  
  194.         if (!$def_res)
  195.         {
  196.             unset($row['rowdefault']);
  197.         }
  198.         else
  199.         {
  200.             $row['rowdefault'] = @pg_result($def_res, 0, 'rowdefault');
  201.         }
  202.  
  203.         if ($row['type'] == 'bpchar')
  204.         {
  205.             // Internally stored as bpchar, but isn't accepted in a CREATE TABLE statement.
  206.             $row['type'] = 'char';
  207.         }
  208.  
  209.         $schema_create .= '    ' . $row['field'] . ' ' . $row['type'];
  210.  
  211.         if (eregi('char', $row['type']))
  212.         {
  213.             if ($row['lengthvar'] > 0)
  214.             {
  215.                 $schema_create .= '(' . ($row['lengthvar'] -4) . ')';
  216.             }
  217.         }
  218.  
  219.         if (eregi('numeric', $row['type']))
  220.         {
  221.             $schema_create .= '(';
  222.             $schema_create .= sprintf("%s,%s", (($row['lengthvar'] >> 16) & 0xffff), (($row['lengthvar'] - 4) & 0xffff));
  223.             $schema_create .= ')';
  224.         }
  225.  
  226.         if (!empty($row['rowdefault']))
  227.         {
  228.             $schema_create .= ' DEFAULT ' . $row['rowdefault'];
  229.         }
  230.  
  231.         if ($row['notnull'] == 't')
  232.         {
  233.             $schema_create .= ' NOT NULL';
  234.         }
  235.  
  236.         $schema_create .= ",$crlf";
  237.  
  238.     }
  239.     //
  240.     // Get the listing of primary keys.
  241.     //
  242.  
  243.     $sql_pri_keys = "SELECT ic.relname AS index_name, bc.relname AS tab_name, ta.attname AS column_name, i.indisunique AS unique_key, i.indisprimary AS primary_key
  244.         FROM pg_class bc, pg_class ic, pg_index i, pg_attribute ta, pg_attribute ia
  245.         WHERE (bc.oid = i.indrelid)
  246.             AND (ic.oid = i.indexrelid)
  247.             AND (ia.attrelid = i.indexrelid)
  248.             AND    (ta.attrelid = bc.oid)
  249.             AND (bc.relname = '$table')
  250.             AND (ta.attrelid = i.indrelid)
  251.             AND (ta.attnum = i.indkey[ia.attnum-1])
  252.         ORDER BY index_name, tab_name, column_name ";
  253.     $result = $db->sql_query($sql_pri_keys);
  254.  
  255.     if(!$result)
  256.     {
  257.         message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $sql_pri_keys);
  258.     }
  259.  
  260.     while ( $row = $db->sql_fetchrow($result))
  261.     {
  262.         if ($row['primary_key'] == 't')
  263.         {
  264.             if (!empty($primary_key))
  265.             {
  266.                 $primary_key .= ', ';
  267.             }
  268.  
  269.             $primary_key .= $row['column_name'];
  270.             $primary_key_name = $row['index_name'];
  271.  
  272.         }
  273.         else
  274.         {
  275.             //
  276.             // We have to store this all this info because it is possible to have a multi-column key...
  277.             // we can loop through it again and build the statement
  278.             //
  279.             $index_rows[$row['index_name']]['table'] = $table;
  280.             $index_rows[$row['index_name']]['unique'] = ($row['unique_key'] == 't') ? ' UNIQUE ' : '';
  281.             $index_rows[$row['index_name']]['column_names'] .= $row['column_name'] . ', ';
  282.         }
  283.     }
  284.  
  285.     if (!empty($index_rows))
  286.     {
  287.         while(list($idx_name, $props) = each($index_rows))
  288.         {
  289.             $props['column_names'] = ereg_replace(", $", "" , $props['column_names']);
  290.             $index_create .= 'CREATE ' . $props['unique'] . " INDEX $idx_name ON $table (" . $props['column_names'] . ");$crlf";
  291.         }
  292.     }
  293.  
  294.     if (!empty($primary_key))
  295.     {
  296.         $schema_create .= "    CONSTRAINT $primary_key_name PRIMARY KEY ($primary_key),$crlf";
  297.     }
  298.  
  299.     //
  300.     // Generate constraint clauses for CHECK constraints
  301.     //
  302.     $sql_checks = "SELECT rcname as index_name, rcsrc
  303.         FROM pg_relcheck, pg_class bc
  304.         WHERE rcrelid = bc.oid
  305.             AND bc.relname = '$table'
  306.             AND NOT EXISTS (
  307.                 SELECT *
  308.                     FROM pg_relcheck as c, pg_inherits as i
  309.                     WHERE i.inhrelid = pg_relcheck.rcrelid
  310.                         AND c.rcname = pg_relcheck.rcname
  311.                         AND c.rcsrc = pg_relcheck.rcsrc
  312.                         AND c.rcrelid = i.inhparent
  313.             )";
  314.     $result = $db->sql_query($sql_checks);
  315.  
  316.     if (!$result)
  317.     {
  318.         message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $sql_checks);
  319.     }
  320.  
  321.     //
  322.     // Add the constraints to the sql file.
  323.     //
  324.     while ($row = $db->sql_fetchrow($result))
  325.     {
  326.         $schema_create .= '    CONSTRAINT ' . $row['index_name'] . ' CHECK ' . $row['rcsrc'] . ",$crlf";
  327.     }
  328.  
  329.     $schema_create = ereg_replace(',' . $crlf . '$', '', $schema_create);
  330.     $index_create = ereg_replace(',' . $crlf . '$', '', $index_create);
  331.  
  332.     $schema_create .= "$crlf);$crlf";
  333.  
  334.     if (!empty($index_create))
  335.     {
  336.         $schema_create .= $index_create;
  337.     }
  338.  
  339.     //
  340.     // Ok now we've built all the sql return it to the calling function.
  341.     //
  342.     return (stripslashes($schema_create));
  343.  
  344. }
  345.  
  346. //
  347. // This function returns the "CREATE TABLE" syntax for mysql dbms...
  348. //
  349. function get_table_def_mysql($table, $crlf)
  350. {
  351.     global $drop, $db;
  352.  
  353.     $schema_create = "";
  354.     $field_query = "SHOW FIELDS FROM $table";
  355.     $key_query = "SHOW KEYS FROM $table";
  356.  
  357.     //
  358.     // If the user has selected to drop existing tables when doing a restore.
  359.     // Then we add the statement to drop the tables....
  360.     //
  361.     if ($drop == 1)
  362.     {
  363.         $schema_create .= "DROP TABLE IF EXISTS $table;$crlf";
  364.     }
  365.  
  366.     $schema_create .= "CREATE TABLE $table($crlf";
  367.  
  368.     //
  369.     // Ok lets grab the fields...
  370.     //
  371.     $result = $db->sql_query($field_query);
  372.     if(!$result)
  373.     {
  374.         message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $field_query);
  375.     }
  376.  
  377.     while ($row = $db->sql_fetchrow($result))
  378.     {
  379.         $schema_create .= '    ' . $row['Field'] . ' ' . $row['Type'];
  380.  
  381.         if(!empty($row['Default']))
  382.         {
  383.             $schema_create .= ' DEFAULT \'' . $row['Default'] . '\'';
  384.         }
  385.  
  386.         if($row['Null'] != "YES")
  387.         {
  388.             $schema_create .= ' NOT NULL';
  389.         }
  390.  
  391.         if($row['Extra'] != "")
  392.         {
  393.             $schema_create .= ' ' . $row['Extra'];
  394.         }
  395.  
  396.         $schema_create .= ",$crlf";
  397.     }
  398.     //
  399.     // Drop the last ',$crlf' off ;)
  400.     //
  401.     $schema_create = ereg_replace(',' . $crlf . '$', "", $schema_create);
  402.  
  403.     //
  404.     // Get any Indexed fields from the database...
  405.     //
  406.     $result = $db->sql_query($key_query);
  407.     if(!$result)
  408.     {
  409.         message_die(GENERAL_ERROR, "FAILED IN get_table_def (show keys)", "", __LINE__, __FILE__, $key_query);
  410.     }
  411.  
  412.     while($row = $db->sql_fetchrow($result))
  413.     {
  414.         $kname = $row['Key_name'];
  415.  
  416.         if(($kname != 'PRIMARY') && ($row['Non_unique'] == 0))
  417.         {
  418.             $kname = "UNIQUE|$kname";
  419.         }
  420.  
  421.         if(!is_array($index[$kname]))
  422.         {
  423.             $index[$kname] = array();
  424.         }
  425.  
  426.         $index[$kname][] = $row['Column_name'];
  427.     }
  428.  
  429.     while(list($x, $columns) = @each($index))
  430.     {
  431.         $schema_create .= ", $crlf";
  432.  
  433.         if($x == 'PRIMARY')
  434.         {
  435.             $schema_create .= '    PRIMARY KEY (' . implode($columns, ', ') . ')';
  436.         }
  437.         elseif (substr($x,0,6) == 'UNIQUE')
  438.         {
  439.             $schema_create .= '    UNIQUE ' . substr($x,7) . ' (' . implode($columns, ', ') . ')';
  440.         }
  441.         else
  442.         {
  443.             $schema_create .= "    KEY $x (" . implode($columns, ', ') . ')';
  444.         }
  445.     }
  446.  
  447.     $schema_create .= "$crlf);";
  448.  
  449.     if(get_magic_quotes_runtime())
  450.     {
  451.         return(stripslashes($schema_create));
  452.     }
  453.     else
  454.     {
  455.         return($schema_create);
  456.     }
  457.  
  458. } // End get_table_def_mysql
  459.  
  460.  
  461. //
  462. // This fuction will return a tables create definition to be used as an sql
  463. // statement.
  464. //
  465. //
  466. // The following functions Get the data from the tables and format it as a
  467. // series of INSERT statements, for each different DBMS...
  468. // After every row a custom callback function $handler gets called.
  469. // $handler must accept one parameter ($sql_insert);
  470. //
  471. //
  472. // Here is the function for postgres...
  473. //
  474. function get_table_content_postgresql($table, $handler)
  475. {
  476.     global $db;
  477.  
  478.     //
  479.     // Grab all of the data from current table.
  480.     //
  481.  
  482.     $result = $db->sql_query("SELECT * FROM $table");
  483.  
  484.     if (!$result)
  485.     {
  486.         message_die(GENERAL_ERROR, "Failed in get_table_content (select *)", "", __LINE__, __FILE__, "SELECT * FROM $table");
  487.     }
  488.  
  489.     $i_num_fields = $db->sql_numfields($result);
  490.  
  491.     for ($i = 0; $i < $i_num_fields; $i++)
  492.     {
  493.         $aryType[] = $db->sql_fieldtype($i, $result);
  494.         $aryName[] = $db->sql_fieldname($i, $result);
  495.     }
  496.  
  497.     $iRec = 0;
  498.  
  499.     while($row = $db->sql_fetchrow($result))
  500.     {
  501.         $schema_vals = '';
  502.         $schema_fields = '';
  503.         $schema_insert = '';
  504.         //
  505.         // Build the SQL statement to recreate the data.
  506.         //
  507.         for($i = 0; $i < $i_num_fields; $i++)
  508.         {
  509.             $strVal = $row[$aryName[$i]];
  510.             if (eregi("char|text|bool", $aryType[$i]))
  511.             {
  512.                 $strQuote = "'";
  513.                 $strEmpty = "";
  514.                 $strVal = addslashes($strVal);
  515.             }
  516.             elseif (eregi("date|timestamp", $aryType[$i]))
  517.             {
  518.                 if (empty($strVal))
  519.                 {
  520.                     $strQuote = "";
  521.                 }
  522.                 else
  523.                 {
  524.                     $strQuote = "'";
  525.                 }
  526.             }
  527.             else
  528.             {
  529.                 $strQuote = "";
  530.                 $strEmpty = "NULL";
  531.             }
  532.  
  533.             if (empty($strVal) && $strVal != "0")
  534.             {
  535.                 $strVal = $strEmpty;
  536.             }
  537.  
  538.             $schema_vals .= " $strQuote$strVal$strQuote,";
  539.             $schema_fields .= " $aryName[$i],";
  540.  
  541.         }
  542.  
  543.         $schema_vals = ereg_replace(",$", "", $schema_vals);
  544.         $schema_vals = ereg_replace("^ ", "", $schema_vals);
  545.         $schema_fields = ereg_replace(",$", "", $schema_fields);
  546.         $schema_fields = ereg_replace("^ ", "", $schema_fields);
  547.  
  548.         //
  549.         // Take the ordered fields and their associated data and build it
  550.         // into a valid sql statement to recreate that field in the data.
  551.         //
  552.         $schema_insert = "INSERT INTO $table ($schema_fields) VALUES($schema_vals);";
  553.  
  554.         $handler(trim($schema_insert));
  555.     }
  556.  
  557.     return(true);
  558.  
  559. }// end function get_table_content_postgres...
  560.  
  561. //
  562. // This function is for getting the data from a mysql table.
  563. //
  564.  
  565. function get_table_content_mysql($table, $handler)
  566. {
  567.     global $db;
  568.  
  569.     // Grab the data from the table.
  570.     if (!($result = $db->sql_query("SELECT * FROM $table")))
  571.     {
  572.         message_die(GENERAL_ERROR, "Failed in get_table_content (select *)", "", __LINE__, __FILE__, "SELECT * FROM $table");
  573.     }
  574.  
  575.     // Loop through the resulting rows and build the sql statement.
  576.     if ($row = $db->sql_fetchrow($result))
  577.     {
  578.         $handler("\n#\n# Table Data for $table\n#\n");
  579.         $field_names = array();
  580.  
  581.         // Grab the list of field names.
  582.         $num_fields = $db->sql_numfields($result);
  583.         $table_list = '(';
  584.         for ($j = 0; $j < $num_fields; $j++)
  585.         {
  586.             $field_names[$j] = $db->sql_fieldname($j, $result);
  587.             $table_list .= (($j > 0) ? ', ' : '') . $field_names[$j];
  588.             
  589.         }
  590.         $table_list .= ')';
  591.  
  592.         do
  593.         {
  594.             // Start building the SQL statement.
  595.             $schema_insert = "INSERT INTO $table $table_list VALUES(";
  596.  
  597.             // Loop through the rows and fill in data for each column
  598.             for ($j = 0; $j < $num_fields; $j++)
  599.             {
  600.                 $schema_insert .= ($j > 0) ? ', ' : '';
  601.  
  602.                 if(!isset($row[$field_names[$j]]))
  603.                 {
  604.                     //
  605.                     // If there is no data for the column set it to null.
  606.                     // There was a problem here with an extra space causing the
  607.                     // sql file not to reimport if the last column was null in
  608.                     // any table.  Should be fixed now :) JLH
  609.                     //
  610.                     $schema_insert .= 'NULL';
  611.                 }
  612.                 elseif ($row[$field_names[$j]] != '')
  613.                 {
  614.                     $schema_insert .= '\'' . addslashes($row[$field_names[$j]]) . '\'';
  615.                 }
  616.                 else
  617.                 {
  618.                     $schema_insert .= '\'\'';
  619.                 }
  620.             }
  621.  
  622.             $schema_insert .= ');';
  623.  
  624.             // Go ahead and send the insert statement to the handler function.
  625.             $handler(trim($schema_insert));
  626.  
  627.         }
  628.         while ($row = $db->sql_fetchrow($result));
  629.     }
  630.  
  631.     return(true);
  632. }
  633.  
  634. function output_table_content($content)
  635. {
  636.     global $tempfile;
  637.  
  638.     //fwrite($tempfile, $content . "\n");
  639.     //$backup_sql .= $content . "\n";
  640.     echo $content ."\n";
  641.     return;
  642. }
  643. //
  644. // End Functions
  645. // -------------
  646.  
  647.  
  648. //
  649. // Begin program proper
  650. //
  651. if( isset($HTTP_GET_VARS['perform']) || isset($HTTP_POST_VARS['perform']) )
  652. {
  653.     $perform = (isset($HTTP_POST_VARS['perform'])) ? $HTTP_POST_VARS['perform'] : $HTTP_GET_VARS['perform'];
  654.  
  655.     switch($perform)
  656.     {
  657.         case 'backup':
  658.  
  659.             $error = false;
  660.             switch(SQL_LAYER)
  661.             {
  662.                 case 'oracle':
  663.                     $error = true;
  664.                     break;
  665.                 case 'db2':
  666.                     $error = true;
  667.                     break;
  668.                 case 'msaccess':
  669.                     $error = true;
  670.                     break;
  671.                 case 'mssql':
  672.                 case 'mssql-odbc':
  673.                     $error = true;
  674.                     break;
  675.             }
  676.  
  677.             if ($error)
  678.             {
  679.                 include('./page_header_admin.'.$phpEx);
  680.  
  681.                 $template->set_filenames(array(
  682.                     "body" => "admin/admin_message_body.tpl")
  683.                 );
  684.  
  685.                 $template->assign_vars(array(
  686.                     "MESSAGE_TITLE" => $lang['Information'],
  687.                     "MESSAGE_TEXT" => $lang['Backups_not_supported'])
  688.                 );
  689.  
  690.                 $template->pparse("body");
  691.  
  692.                 include('./page_footer_admin.'.$phpEx);
  693.             }
  694.  
  695.             $tables = array('auth_access', 'banlist', 'categories', 'config', 'disallow', 'forums', 'forum_prune', 'groups', 'posts', 'posts_text', 'privmsgs', 'privmsgs_text', 'ranks', 'search_results', 'search_wordlist', 'search_wordmatch', 'sessions', 'smilies', 'themes', 'themes_name', 'topics', 'topics_watch', 'user_group', 'users', 'vote_desc', 'vote_results', 'vote_voters', 'words', 'confirm', 'sessions_keys');
  696.  
  697.             $additional_tables = (isset($HTTP_POST_VARS['additional_tables'])) ? $HTTP_POST_VARS['additional_tables'] : ( (isset($HTTP_GET_VARS['additional_tables'])) ? $HTTP_GET_VARS['additional_tables'] : "" );
  698.  
  699.             $backup_type = (isset($HTTP_POST_VARS['backup_type'])) ? $HTTP_POST_VARS['backup_type'] : ( (isset($HTTP_GET_VARS['backup_type'])) ? $HTTP_GET_VARS['backup_type'] : "" );
  700.  
  701.             $gzipcompress = (!empty($HTTP_POST_VARS['gzipcompress'])) ? $HTTP_POST_VARS['gzipcompress'] : ( (!empty($HTTP_GET_VARS['gzipcompress'])) ? $HTTP_GET_VARS['gzipcompress'] : 0 );
  702.  
  703.             $drop = (!empty($HTTP_POST_VARS['drop'])) ? intval($HTTP_POST_VARS['drop']) : ( (!empty($HTTP_GET_VARS['drop'])) ? intval($HTTP_GET_VARS['drop']) : 0 );
  704.  
  705.             if(!empty($additional_tables))
  706.             {
  707.                 if(ereg(",", $additional_tables))
  708.                 {
  709.                     $additional_tables = split(",", $additional_tables);
  710.  
  711.                     for($i = 0; $i < count($additional_tables); $i++)
  712.                     {
  713.                         $tables[] = trim($additional_tables[$i]);
  714.                     }
  715.  
  716.                 }
  717.                 else
  718.                 {
  719.                     $tables[] = trim($additional_tables);
  720.                 }
  721.             }
  722.  
  723.             if( !isset($HTTP_POST_VARS['backupstart']) && !isset($HTTP_GET_VARS['backupstart']))
  724.             {
  725.                 include('./page_header_admin.'.$phpEx);
  726.  
  727.                 $template->set_filenames(array(
  728.                     "body" => "admin/db_utils_backup_body.tpl")
  729.                 );    
  730.                 $s_hidden_fields = "<input type=\"hidden\" name=\"perform\" value=\"backup\" /><input type=\"hidden\" name=\"drop\" value=\"1\" /><input type=\"hidden\" name=\"perform\" value=\"$perform\" />";
  731.  
  732.                 $template->assign_vars(array(
  733.                     "L_DATABASE_BACKUP" => $lang['Database_Utilities'] . " : " . $lang['Backup'],
  734.                     "L_BACKUP_EXPLAIN" => $lang['Backup_explain'],
  735.                     "L_FULL_BACKUP" => $lang['Full_backup'],
  736.                     "L_STRUCTURE_BACKUP" => $lang['Structure_backup'],
  737.                     "L_DATA_BACKUP" => $lang['Data_backup'],
  738.                     "L_ADDITIONAL_TABLES" => $lang['Additional_tables'],
  739.                     "L_START_BACKUP" => $lang['Start_backup'],
  740.                     "L_BACKUP_OPTIONS" => $lang['Backup_options'],
  741.                     "L_GZIP_COMPRESS" => $lang['Gzip_compress'],
  742.                     "L_NO" => $lang['No'],
  743.                     "L_YES" => $lang['Yes'],
  744.  
  745.                     "S_HIDDEN_FIELDS" => $s_hidden_fields,
  746.                     "S_DBUTILS_ACTION" => append_sid("admin_db_utilities.$phpEx"))
  747.                 );
  748.                 $template->pparse("body");
  749.  
  750.                 break;
  751.  
  752.             }
  753.             else if( !isset($HTTP_POST_VARS['startdownload']) && !isset($HTTP_GET_VARS['startdownload']) )
  754.             {
  755.                 if(is_array($additional_tables))
  756.                 {
  757.                     $additional_tables = implode(',', $additional_tables);
  758.                 }
  759.                 $template->set_filenames(array(
  760.                     "body" => "admin/admin_message_body.tpl")
  761.                 );
  762.  
  763.                 $template->assign_vars(array(
  764.                     "META" => '<meta http-equiv="refresh" content="2;url=' . append_sid("admin_db_utilities.$phpEx?perform=backup&additional_tables=" . quotemeta($additional_tables) . "&backup_type=$backup_type&drop=1&backupstart=1&gzipcompress=$gzipcompress&startdownload=1") . '">',
  765.  
  766.                     "MESSAGE_TITLE" => $lang['Database_Utilities'] . " : " . $lang['Backup'],
  767.                     "MESSAGE_TEXT" => $lang['Backup_download'])
  768.                 );
  769.  
  770.                 include('./page_header_admin.'.$phpEx);
  771.  
  772.                 $template->pparse("body");
  773.  
  774.                 include('./page_footer_admin.'.$phpEx);
  775.  
  776.             }
  777.             header("Pragma: no-cache");
  778.             $do_gzip_compress = FALSE;
  779.             if( $gzipcompress )
  780.             {
  781.                 $phpver = phpversion();
  782.  
  783.                 if($phpver >= "4.0")
  784.                 {
  785.                     if(extension_loaded("zlib"))
  786.                     {
  787.                         $do_gzip_compress = TRUE;
  788.                     }
  789.                 }
  790.             }
  791.             if($do_gzip_compress)
  792.             {
  793.                 @ob_start();
  794.                 @ob_implicit_flush(0);
  795.                 header("Content-Type: application/x-gzip; name=\"phpbb_db_backup.sql.gz\"");
  796.                 header("Content-disposition: attachment; filename=phpbb_db_backup.sql.gz");
  797.             }
  798.             else
  799.             {
  800.                 header("Content-Type: text/x-delimtext; name=\"phpbb_db_backup.sql\"");
  801.                 header("Content-disposition: attachment; filename=phpbb_db_backup.sql");
  802.             }
  803.  
  804.             //
  805.             // Build the sql script file...
  806.             //
  807.             echo "#\n";
  808.             echo "# phpBB Backup Script\n";
  809.             echo "# Dump of tables for $dbname\n";
  810.             echo "#\n# DATE : " .  gmdate("d-m-Y H:i:s", time()) . " GMT\n";
  811.             echo "#\n";
  812.  
  813.             if(SQL_LAYER == 'postgresql')
  814.             {
  815.                  echo "\n" . pg_get_sequences("\n", $backup_type);
  816.             }
  817.             for($i = 0; $i < count($tables); $i++)
  818.             {
  819.                 $table_name = $tables[$i];
  820.  
  821.                 switch (SQL_LAYER)
  822.                 {
  823.                     case 'postgresql':
  824.                         $table_def_function = "get_table_def_postgresql";
  825.                         $table_content_function = "get_table_content_postgresql";
  826.                         break;
  827.  
  828.                     case 'mysql':
  829.                     case 'mysql4':
  830.                         $table_def_function = "get_table_def_mysql";
  831.                         $table_content_function = "get_table_content_mysql";
  832.                         break;
  833.                 }
  834.  
  835.                 if($backup_type != 'data')
  836.                 {
  837.                     echo "#\n# TABLE: " . $table_prefix . $table_name . "\n#\n";
  838.                     echo $table_def_function($table_prefix . $table_name, "\n") . "\n";
  839.                 }
  840.  
  841.                 if($backup_type != 'structure')
  842.                 {
  843.                     $table_content_function($table_prefix . $table_name, "output_table_content");
  844.                 }
  845.             }
  846.             
  847.             if($do_gzip_compress)
  848.             {
  849.                 $Size = ob_get_length();
  850.                 $Crc = crc32(ob_get_contents());
  851.                 $contents = gzcompress(ob_get_contents());
  852.                 ob_end_clean();
  853.                 echo "\x1f\x8b\x08\x00\x00\x00\x00\x00".substr($contents, 0, strlen($contents) - 4).gzip_PrintFourChars($Crc).gzip_PrintFourChars($Size);
  854.             }
  855.             exit;
  856.  
  857.             break;
  858.  
  859.         case 'restore':
  860.             if(!isset($HTTP_POST_VARS['restore_start']))
  861.             {
  862.                 //
  863.                 // Define Template files...
  864.                 //
  865.                 include('./page_header_admin.'.$phpEx);
  866.  
  867.                 $template->set_filenames(array(
  868.                     "body" => "admin/db_utils_restore_body.tpl")
  869.                 );
  870.  
  871.                 $s_hidden_fields = "<input type=\"hidden\" name=\"perform\" value=\"restore\" /><input type=\"hidden\" name=\"perform\" value=\"$perform\" />";
  872.  
  873.                 $template->assign_vars(array(
  874.                     "L_DATABASE_RESTORE" => $lang['Database_Utilities'] . " : " . $lang['Restore'],
  875.                     "L_RESTORE_EXPLAIN" => $lang['Restore_explain'],
  876.                     "L_SELECT_FILE" => $lang['Select_file'],
  877.                     "L_START_RESTORE" => $lang['Start_Restore'],
  878.  
  879.                     "S_DBUTILS_ACTION" => append_sid("admin_db_utilities.$phpEx"),
  880.                     "S_HIDDEN_FIELDS" => $s_hidden_fields)
  881.                 );
  882.                 $template->pparse("body");
  883.  
  884.                 break;
  885.  
  886.             }
  887.             else
  888.             {
  889.                 //
  890.                 // Handle the file upload ....
  891.                 // If no file was uploaded report an error...
  892.                 //
  893.                 $backup_file_name = (!empty($HTTP_POST_FILES['backup_file']['name'])) ? $HTTP_POST_FILES['backup_file']['name'] : "";
  894.                 $backup_file_tmpname = ($HTTP_POST_FILES['backup_file']['tmp_name'] != "none") ? $HTTP_POST_FILES['backup_file']['tmp_name'] : "";
  895.                 $backup_file_type = (!empty($HTTP_POST_FILES['backup_file']['type'])) ? $HTTP_POST_FILES['backup_file']['type'] : "";
  896.  
  897.                 if($backup_file_tmpname == "" || $backup_file_name == "")
  898.                 {
  899.                     message_die(GENERAL_MESSAGE, $lang['Restore_Error_no_file']);
  900.                 }
  901.                 //
  902.                 // If I file was actually uploaded, check to make sure that we
  903.                 // are actually passed the name of an uploaded file, and not
  904.                 // a hackers attempt at getting us to process a local system
  905.                 // file.
  906.                 //
  907.                 if( file_exists(phpbb_realpath($backup_file_tmpname)) )
  908.                 {
  909.                     if( preg_match("/^(text\/[a-zA-Z]+)|(application\/(x\-)?gzip(\-compressed)?)|(application\/octet-stream)$/is", $backup_file_type) )
  910.                     {
  911.                         if( preg_match("/\.gz$/is",$backup_file_name) )
  912.                         {
  913.                             $do_gzip_compress = FALSE;
  914.                             $phpver = phpversion();
  915.                             if($phpver >= "4.0")
  916.                             {
  917.                                 if(extension_loaded("zlib"))
  918.                                 {
  919.                                     $do_gzip_compress = TRUE;
  920.                                 }
  921.                             }
  922.  
  923.                             if($do_gzip_compress)
  924.                             {
  925.                                 $gz_ptr = gzopen($backup_file_tmpname, 'rb');
  926.                                 $sql_query = "";
  927.                                 while( !gzeof($gz_ptr) )
  928.                                 {
  929.                                     $sql_query .= gzgets($gz_ptr, 100000);
  930.                                 }
  931.                             }
  932.                             else
  933.                             {
  934.                                 message_die(GENERAL_ERROR, $lang['Restore_Error_decompress']);
  935.                             }
  936.                         }
  937.                         else
  938.                         {
  939.                             $sql_query = fread(fopen($backup_file_tmpname, 'r'), filesize($backup_file_tmpname));
  940.                         }
  941.                         //
  942.                         // Comment this line out to see if this fixes the stuff...
  943.                         //
  944.                         //$sql_query = stripslashes($sql_query);
  945.                     }
  946.                     else
  947.                     {
  948.                         message_die(GENERAL_ERROR, $lang['Restore_Error_filename'] ." $backup_file_type $backup_file_name");
  949.                     }
  950.                 }
  951.                 else
  952.                 {
  953.                     message_die(GENERAL_ERROR, $lang['Restore_Error_uploading']);
  954.                 }
  955.  
  956.                 if($sql_query != "")
  957.                 {
  958.                     // Strip out sql comments...
  959.                     $sql_query = remove_remarks($sql_query);
  960.                     $pieces = split_sql_file($sql_query, ";");
  961.  
  962.                     $sql_count = count($pieces);
  963.                     for($i = 0; $i < $sql_count; $i++)
  964.                     {
  965.                         $sql = trim($pieces[$i]);
  966.  
  967.                         if(!empty($sql) and $sql[0] != "#")
  968.                         {
  969.                             if(VERBOSE == 1)
  970.                             {
  971.                                 echo "Executing: $sql\n<br>";
  972.                                 flush();
  973.                             }
  974.  
  975.                             $result = $db->sql_query($sql);
  976.  
  977.                             if(!$result && ( !(SQL_LAYER == 'postgresql' && eregi("drop table", $sql) ) ) )
  978.                             {
  979.                                 message_die(GENERAL_ERROR, "Error importing backup file", "", __LINE__, __FILE__, $sql);
  980.                             }
  981.                         }
  982.                     }
  983.                 }
  984.  
  985.                 include('./page_header_admin.'.$phpEx);
  986.  
  987.                 $template->set_filenames(array(
  988.                     "body" => "admin/admin_message_body.tpl")
  989.                 );
  990.  
  991.                 $message = $lang['Restore_success'];
  992.  
  993.                 $template->assign_vars(array(
  994.                     "MESSAGE_TITLE" => $lang['Database_Utilities'] . " : " . $lang['Restore'],
  995.                     "MESSAGE_TEXT" => $message)
  996.                 );
  997.  
  998.                 $template->pparse("body");
  999.                 break;
  1000.             }
  1001.             break;
  1002.     }
  1003. }
  1004.  
  1005. include('./page_footer_admin.'.$phpEx);
  1006.  
  1007. ?>
  1008.