Verzeichnisstruktur phpBB-2.0.0


Veröffentlicht
03.04.2002

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

admin_db_utilities.php

Zuletzt modifiziert: 09.10.2024, 12:51 - Dateigröße: 25.66 KiB


 0001  <?php
 0002  /***************************************************************************
 0003  *                             admin_db_utilities.php
 0004  *                              -------------------
 0005  *     begin                : Thu May 31, 2001
 0006  *     copyright            : (C) 2001 The phpBB Group
 0007  *     email                : support@phpbb.com
 0008  *
 0009  *     $Id$
 0010  *
 0011  ****************************************************************************/
 0012   
 0013  /***************************************************************************
 0014   *
 0015   *   This program is free software; you can redistribute it and/or modify
 0016   *   it under the terms of the GNU General Public License as published by
 0017   *   the Free Software Foundation; either version 2 of the License, or
 0018   *   (at your option) any later version.
 0019   *
 0020   ***************************************************************************/
 0021   
 0022  /***************************************************************************
 0023  *    We will attempt to create a file based backup of all of the data in the
 0024  *    users phpBB database.  The resulting file should be able to be imported by
 0025  *    the db_restore.php function, or by using the mysql command_line
 0026  *
 0027  *    Some functions are adapted from the upgrade_20.php script and others
 0028  *    adapted from the unoficial phpMyAdmin 2.2.0.
 0029  ***************************************************************************/
 0030   
 0031  define('IN_PHPBB', 1);
 0032   
 0033  if( !empty($setmodules) )
 0034  {
 0035      $filename = basename(__FILE__);
 0036      $module['General']['Backup_DB'] = $filename . "?perform=backup";
 0037   
 0038      $file_uploads = (@phpversion() >= '4.0.0') ? @ini_get('file_uploads') : @get_cfg_var('file_uploads');
 0039   
 0040      if( (empty($file_uploads) || $file_uploads != 0) && (strtolower($file_uploads) != 'off') && (@phpversion() != '4.0.4pl1') )
 0041      {
 0042          $module['General']['Restore_DB'] = $filename . "?perform=restore";
 0043      }
 0044   
 0045      return;
 0046  }
 0047   
 0048  //
 0049  // Load default header
 0050  //
 0051  $no_page_header = TRUE;
 0052  $phpbb_root_path = "./../";
 0053  require($phpbb_root_path . 'extension.inc');
 0054  require('./pagestart.' . $phpEx);
 0055  include($phpbb_root_path . 'includes/sql_parse.'.$phpEx);
 0056   
 0057  //
 0058  // Set VERBOSE to 1  for debugging info..
 0059  //
 0060  define("VERBOSE", 0);
 0061   
 0062  //
 0063  // Increase maximum execution time, but don't complain about it if it isn't
 0064  // allowed.
 0065  //
 0066  @set_time_limit(1200);
 0067   
 0068  // -----------------------
 0069  // The following functions are adapted from phpMyAdmin and upgrade_20.php
 0070  //
 0071  function gzip_PrintFourChars($Val)
 0072  {
 0073      for ($i = 0; $i < 4; $i ++)
 0074      {
 0075          $return .= chr($Val % 256);
 0076          $Val = floor($Val / 256);
 0077      }
 0078      return $return;
 0079  } 
 0080   
 0081   
 0082   
 0083  //
 0084  // This function is used for grabbing the sequences for postgres...
 0085  //
 0086  function pg_get_sequences($crlf, $backup_type)
 0087  {
 0088      global $db;
 0089   
 0090      $get_seq_sql = "SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*'
 0091          AND relkind = 'S' ORDER BY relname";
 0092   
 0093      $seq = $db->sql_query($get_seq_sql);
 0094   
 0095      if( !$num_seq = $db->sql_numrows($seq) )
 0096      {
 0097   
 0098          $return_val = "# No Sequences Found $crlf";
 0099   
 0100      }
 0101      else
 0102      {
 0103          $return_val = "# Sequences $crlf";
 0104          $i_seq = 0;
 0105   
 0106          while($i_seq < $num_seq)
 0107          {
 0108              $row = $db->sql_fetchrow($seq);
 0109              $sequence = $row['relname'];
 0110   
 0111              $get_props_sql = "SELECT * FROM $sequence";
 0112              $seq_props = $db->sql_query($get_props_sql);
 0113   
 0114              if($db->sql_numrows($seq_props) > 0)
 0115              {
 0116                  $row1 = $db->sql_fetchrow($seq_props);
 0117   
 0118                  if($backup_type == 'structure')
 0119                  {
 0120                      $row['last_value'] = 1;
 0121                  }
 0122   
 0123                  $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";
 0124   
 0125              }  // End if numrows > 0
 0126   
 0127              if(($row['last_value'] > 1) && ($backup_type != 'structure'))
 0128              {
 0129                  $return_val .= "SELECT NEXTVALE('$sequence'); $crlf";
 0130                  unset($row['last_value']);
 0131              }
 0132   
 0133              $i_seq++;
 0134   
 0135          } // End while..
 0136   
 0137      } // End else...
 0138   
 0139      return $returnval;
 0140   
 0141  } // End function...
 0142   
 0143  //
 0144  // The following functions will return the "CREATE TABLE syntax for the
 0145  // varying DBMS's
 0146  //
 0147  // This function returns, will return the table def's for postgres...
 0148  //
 0149  function get_table_def_postgresql($table, $crlf)
 0150  {
 0151      global $drop, $db;
 0152   
 0153      $schema_create = "";
 0154      //
 0155      // Get a listing of the fields, with their associated types, etc.
 0156      //
 0157   
 0158      $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
 0159          FROM pg_class c, pg_attribute a, pg_type t
 0160          WHERE c.relname = '$table'
 0161              AND a.attnum > 0
 0162              AND a.attrelid = c.oid
 0163              AND a.atttypid = t.oid
 0164          ORDER BY a.attnum";
 0165      $result = $db->sql_query($field_query);
 0166   
 0167      if(!$result)
 0168      {
 0169          message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $field_query);
 0170      } // end if..
 0171   
 0172      if ($drop == 1)
 0173      {
 0174          $schema_create .= "DROP TABLE $table;$crlf";
 0175      } // end if
 0176   
 0177      //
 0178      // Ok now we actually start building the SQL statements to restore the tables
 0179      //
 0180   
 0181      $schema_create .= "CREATE TABLE $table($crlf";
 0182   
 0183      while ($row = $db->sql_fetchrow($result))
 0184      {
 0185          //
 0186          // Get the data from the table
 0187          //
 0188          $sql_get_default = "SELECT d.adsrc AS rowdefault
 0189              FROM pg_attrdef d, pg_class c
 0190              WHERE (c.relname = '$table')
 0191                  AND (c.oid = d.adrelid)
 0192                  AND d.adnum = " . $row['attnum'];
 0193          $def_res = $db->sql_query($sql_get_default);
 0194   
 0195          if (!$def_res)
 0196          {
 0197              unset($row['rowdefault']);
 0198          }
 0199          else
 0200          {
 0201              $row['rowdefault'] = @pg_result($def_res, 0, 'rowdefault');
 0202          }
 0203   
 0204          if ($row['type'] == 'bpchar')
 0205          {
 0206              // Internally stored as bpchar, but isn't accepted in a CREATE TABLE statement.
 0207              $row['type'] = 'char';
 0208          }
 0209   
 0210          $schema_create .= '    ' . $row['field'] . ' ' . $row['type'];
 0211   
 0212          if (eregi('char', $row['type']))
 0213          {
 0214              if ($row['lengthvar'] > 0)
 0215              {
 0216                  $schema_create .= '(' . ($row['lengthvar'] -4) . ')';
 0217              }
 0218          }
 0219   
 0220          if (eregi('numeric', $row['type']))
 0221          {
 0222              $schema_create .= '(';
 0223              $schema_create .= sprintf("%s,%s", (($row['lengthvar'] >> 16) & 0xffff), (($row['lengthvar'] - 4) & 0xffff));
 0224              $schema_create .= ')';
 0225          }
 0226   
 0227          if (!empty($row['rowdefault']))
 0228          {
 0229              $schema_create .= ' DEFAULT ' . $row['rowdefault'];
 0230          }
 0231   
 0232          if ($row['notnull'] == 't')
 0233          {
 0234              $schema_create .= ' NOT NULL';
 0235          }
 0236   
 0237          $schema_create .= ",$crlf";
 0238   
 0239      }
 0240      //
 0241      // Get the listing of primary keys.
 0242      //
 0243   
 0244      $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
 0245          FROM pg_class bc, pg_class ic, pg_index i, pg_attribute ta, pg_attribute ia
 0246          WHERE (bc.oid = i.indrelid)
 0247              AND (ic.oid = i.indexrelid)
 0248              AND (ia.attrelid = i.indexrelid)
 0249              AND    (ta.attrelid = bc.oid)
 0250              AND (bc.relname = '$table')
 0251              AND (ta.attrelid = i.indrelid)
 0252              AND (ta.attnum = i.indkey[ia.attnum-1])
 0253          ORDER BY index_name, tab_name, column_name ";
 0254      $result = $db->sql_query($sql_pri_keys);
 0255   
 0256      if(!$result)
 0257      {
 0258          message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $sql_pri_keys);
 0259      }
 0260   
 0261      while ( $row = $db->sql_fetchrow($result))
 0262      {
 0263          if ($row['primary_key'] == 't')
 0264          {
 0265              if (!empty($primary_key))
 0266              {
 0267                  $primary_key .= ', ';
 0268              }
 0269   
 0270              $primary_key .= $row['column_name'];
 0271              $primary_key_name = $row['index_name'];
 0272   
 0273          }
 0274          else
 0275          {
 0276              //
 0277              // We have to store this all this info because it is possible to have a multi-column key...
 0278              // we can loop through it again and build the statement
 0279              //
 0280              $index_rows[$row['index_name']]['table'] = $table;
 0281              $index_rows[$row['index_name']]['unique'] = ($row['unique_key'] == 't') ? ' UNIQUE ' : '';
 0282              $index_rows[$row['index_name']]['column_names'] .= $row['column_name'] . ', ';
 0283          }
 0284      }
 0285   
 0286      if (!empty($index_rows))
 0287      {
 0288          while(list($idx_name, $props) = each($index_rows))
 0289          {
 0290              $props['column_names'] = ereg_replace(", $", "" , $props['column_names']);
 0291              $index_create .= 'CREATE ' . $props['unique'] . " INDEX $idx_name ON $table (" . $props['column_names'] . ");$crlf";
 0292          }
 0293      }
 0294   
 0295      if (!empty($primary_key))
 0296      {
 0297          $schema_create .= "    CONSTRAINT $primary_key_name PRIMARY KEY ($primary_key),$crlf";
 0298      }
 0299   
 0300      //
 0301      // Generate constraint clauses for CHECK constraints
 0302      //
 0303      $sql_checks = "SELECT rcname as index_name, rcsrc
 0304          FROM pg_relcheck, pg_class bc
 0305          WHERE rcrelid = bc.oid
 0306              AND bc.relname = '$table'
 0307              AND NOT EXISTS (
 0308                  SELECT *
 0309                      FROM pg_relcheck as c, pg_inherits as i
 0310                      WHERE i.inhrelid = pg_relcheck.rcrelid
 0311                          AND c.rcname = pg_relcheck.rcname
 0312                          AND c.rcsrc = pg_relcheck.rcsrc
 0313                          AND c.rcrelid = i.inhparent
 0314              )";
 0315      $result = $db->sql_query($sql_checks);
 0316   
 0317      if (!$result)
 0318      {
 0319          message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $sql_checks);
 0320      }
 0321   
 0322      //
 0323      // Add the constraints to the sql file.
 0324      //
 0325      while ($row = $db->sql_fetchrow($result))
 0326      {
 0327          $schema_create .= '    CONSTRAINT ' . $row['index_name'] . ' CHECK ' . $row['rcsrc'] . ",$crlf";
 0328      }
 0329   
 0330      $schema_create = ereg_replace(',' . $crlf . '$', '', $schema_create);
 0331      $index_create = ereg_replace(',' . $crlf . '$', '', $index_create);
 0332   
 0333      $schema_create .= "$crlf);$crlf";
 0334   
 0335      if (!empty($index_create))
 0336      {
 0337          $schema_create .= $index_create;
 0338      }
 0339   
 0340      //
 0341      // Ok now we've built all the sql return it to the calling function.
 0342      //
 0343      return (stripslashes($schema_create));
 0344   
 0345  }
 0346   
 0347  //
 0348  // This function returns the "CREATE TABLE" syntax for mysql dbms...
 0349  //
 0350  function get_table_def_mysql($table, $crlf)
 0351  {
 0352      global $drop, $db;
 0353   
 0354      $schema_create = "";
 0355      $field_query = "SHOW FIELDS FROM $table";
 0356      $key_query = "SHOW KEYS FROM $table";
 0357   
 0358      //
 0359      // If the user has selected to drop existing tables when doing a restore.
 0360      // Then we add the statement to drop the tables....
 0361      //
 0362      if ($drop == 1)
 0363      {
 0364          $schema_create .= "DROP TABLE IF EXISTS $table;$crlf";
 0365      }
 0366   
 0367      $schema_create .= "CREATE TABLE $table($crlf";
 0368   
 0369      //
 0370      // Ok lets grab the fields...
 0371      //
 0372      $result = $db->sql_query($field_query);
 0373      if(!$result)
 0374      {
 0375          message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $field_query);
 0376      }
 0377   
 0378      while ($row = $db->sql_fetchrow($result))
 0379      {
 0380          $schema_create .= '    ' . $row['Field'] . ' ' . $row['Type'];
 0381   
 0382          if(!empty($row['Default']))
 0383          {
 0384              $schema_create .= ' DEFAULT \'' . $row['Default'] . '\'';
 0385          }
 0386   
 0387          if($row['Null'] != "YES")
 0388          {
 0389              $schema_create .= ' NOT NULL';
 0390          }
 0391   
 0392          if($row['Extra'] != "")
 0393          {
 0394              $schema_create .= ' ' . $row['Extra'];
 0395          }
 0396   
 0397          $schema_create .= ",$crlf";
 0398      }
 0399      //
 0400      // Drop the last ',$crlf' off ;)
 0401      //
 0402      $schema_create = ereg_replace(',' . $crlf . '$', "", $schema_create);
 0403   
 0404      //
 0405      // Get any Indexed fields from the database...
 0406      //
 0407      $result = $db->sql_query($key_query);
 0408      if(!$result)
 0409      {
 0410          message_die(GENERAL_ERROR, "FAILED IN get_table_def (show keys)", "", __LINE__, __FILE__, $key_query);
 0411      }
 0412   
 0413      while($row = $db->sql_fetchrow($result))
 0414      {
 0415          $kname = $row['Key_name'];
 0416   
 0417          if(($kname != 'PRIMARY') && ($row['Non_unique'] == 0))
 0418          {
 0419              $kname = "UNIQUE|$kname";
 0420          }
 0421   
 0422          if(!is_array($index[$kname]))
 0423          {
 0424              $index[$kname] = array();
 0425          }
 0426   
 0427          $index[$kname][] = $row['Column_name'];
 0428      }
 0429   
 0430      while(list($x, $columns) = @each($index))
 0431      {
 0432          $schema_create .= ", $crlf";
 0433   
 0434          if($x == 'PRIMARY')
 0435          {
 0436              $schema_create .= '    PRIMARY KEY (' . implode($columns, ', ') . ')';
 0437          }
 0438          elseif (substr($x,0,6) == 'UNIQUE')
 0439          {
 0440              $schema_create .= '    UNIQUE ' . substr($x,7) . ' (' . implode($columns, ', ') . ')';
 0441          }
 0442          else
 0443          {
 0444              $schema_create .= "    KEY $x (" . implode($columns, ', ') . ')';
 0445          }
 0446      }
 0447   
 0448      $schema_create .= "$crlf);";
 0449   
 0450      if(get_magic_quotes_runtime())
 0451      {
 0452          return(stripslashes($schema_create));
 0453      }
 0454      else
 0455      {
 0456          return($schema_create);
 0457      }
 0458   
 0459  } // End get_table_def_mysql
 0460   
 0461   
 0462  //
 0463  // This fuction will return a tables create definition to be used as an sql
 0464  // statement.
 0465  //
 0466  //
 0467  // The following functions Get the data from the tables and format it as a
 0468  // series of INSERT statements, for each different DBMS...
 0469  // After every row a custom callback function $handler gets called.
 0470  // $handler must accept one parameter ($sql_insert);
 0471  //
 0472  //
 0473  // Here is the function for postgres...
 0474  //
 0475  function get_table_content_postgresql($table, $handler)
 0476  {
 0477      global $db;
 0478   
 0479      //
 0480      // Grab all of the data from current table.
 0481      //
 0482   
 0483      $result = $db->sql_query("SELECT * FROM $table");
 0484   
 0485      if (!$result)
 0486      {
 0487          message_die(GENERAL_ERROR, "Failed in get_table_content (select *)", "", __LINE__, __FILE__, "SELECT * FROM $table");
 0488      }
 0489   
 0490      $i_num_fields = $db->sql_numfields($result);
 0491   
 0492      for ($i = 0; $i < $i_num_fields; $i++)
 0493      {
 0494          $aryType[] = $db->sql_fieldtype($i, $result);
 0495          $aryName[] = $db->sql_fieldname($i, $result);
 0496      }
 0497   
 0498      $iRec = 0;
 0499   
 0500      while($row = $db->sql_fetchrow($result))
 0501      {
 0502          $schema_vals = '';
 0503          $schema_fields = '';
 0504          $schema_insert = '';
 0505          //
 0506          // Build the SQL statement to recreate the data.
 0507          //
 0508          for($i = 0; $i < $i_num_fields; $i++)
 0509          {
 0510              $strVal = $row[$aryName[$i]];
 0511              if (eregi("char|text|bool", $aryType[$i]))
 0512              {
 0513                  $strQuote = "'";
 0514                  $strEmpty = "";
 0515                  $strVal = addslashes($strVal);
 0516              }
 0517              elseif (eregi("date|timestamp", $aryType[$i]))
 0518              {
 0519                  if (empty($strVal))
 0520                  {
 0521                      $strQuote = "";
 0522                  }
 0523                  else
 0524                  {
 0525                      $strQuote = "'";
 0526                  }
 0527              }
 0528              else
 0529              {
 0530                  $strQuote = "";
 0531                  $strEmpty = "NULL";
 0532              }
 0533   
 0534              if (empty($strVal) && $strVal != "0")
 0535              {
 0536                  $strVal = $strEmpty;
 0537              }
 0538   
 0539              $schema_vals .= " $strQuote$strVal$strQuote,";
 0540              $schema_fields .= " $aryName[$i],";
 0541   
 0542          }
 0543   
 0544          $schema_vals = ereg_replace(",$", "", $schema_vals);
 0545          $schema_vals = ereg_replace("^ ", "", $schema_vals);
 0546          $schema_fields = ereg_replace(",$", "", $schema_fields);
 0547          $schema_fields = ereg_replace("^ ", "", $schema_fields);
 0548   
 0549          //
 0550          // Take the ordered fields and their associated data and build it
 0551          // into a valid sql statement to recreate that field in the data.
 0552          //
 0553          $schema_insert = "INSERT INTO $table ($schema_fields) VALUES($schema_vals);";
 0554   
 0555          $handler(trim($schema_insert));
 0556      }
 0557   
 0558      return(true);
 0559   
 0560  }// end function get_table_content_postgres...
 0561   
 0562  //
 0563  // This function is for getting the data from a mysql table.
 0564  //
 0565   
 0566  function get_table_content_mysql($table, $handler)
 0567  {
 0568      global $db;
 0569   
 0570      // Grab the data from the table.
 0571      if (!($result = $db->sql_query("SELECT * FROM $table")))
 0572      {
 0573          message_die(GENERAL_ERROR, "Failed in get_table_content (select *)", "", __LINE__, __FILE__, "SELECT * FROM $table");
 0574      }
 0575   
 0576      // Loop through the resulting rows and build the sql statement.
 0577      if ($row = $db->sql_fetchrow($result))
 0578      {
 0579          $handler("\n#\n# Table Data for $table\n#\n");
 0580          $field_names = array();
 0581   
 0582          // Grab the list of field names.
 0583          $num_fields = $db->sql_numfields($result);
 0584          $table_list = '(';
 0585          for ($j = 0; $j < $num_fields; $j++)
 0586          {
 0587              $field_names[$j] = $db->sql_fieldname($j, $result);
 0588              $table_list .= (($j > 0) ? ', ' : '') . $field_names[$j];
 0589              
 0590          }
 0591          $table_list .= ')';
 0592   
 0593          do
 0594          {
 0595              // Start building the SQL statement.
 0596              $schema_insert = "INSERT INTO $table $table_list VALUES(";
 0597   
 0598              // Loop through the rows and fill in data for each column
 0599              for ($j = 0; $j < $num_fields; $j++)
 0600              {
 0601                  $schema_insert .= ($j > 0) ? ', ' : '';
 0602   
 0603                  if(!isset($row[$field_names[$j]]))
 0604                  {
 0605                      //
 0606                      // If there is no data for the column set it to null.
 0607                      // There was a problem here with an extra space causing the
 0608                      // sql file not to reimport if the last column was null in
 0609                      // any table.  Should be fixed now :) JLH
 0610                      //
 0611                      $schema_insert .= 'NULL';
 0612                  }
 0613                  elseif ($row[$field_names[$j]] != '')
 0614                  {
 0615                      $schema_insert .= '\'' . addslashes($row[$field_names[$j]]) . '\'';
 0616                  }
 0617                  else
 0618                  {
 0619                      $schema_insert .= '\'\'';
 0620                  }
 0621              }
 0622   
 0623              $schema_insert .= ');';
 0624   
 0625              // Go ahead and send the insert statement to the handler function.
 0626              $handler(trim($schema_insert));
 0627   
 0628          }
 0629          while ($row = $db->sql_fetchrow($result));
 0630      }
 0631   
 0632      return(true);
 0633  }
 0634   
 0635  function output_table_content($content)
 0636  {
 0637      global $tempfile;
 0638   
 0639      //fwrite($tempfile, $content . "\n");
 0640      //$backup_sql .= $content . "\n";
 0641      echo $content ."\n";
 0642      return;
 0643  }
 0644  //
 0645  // End Functions
 0646  // -------------
 0647   
 0648   
 0649  //
 0650  // Begin program proper
 0651  //
 0652  if( isset($HTTP_GET_VARS['perform']) || isset($HTTP_POST_VARS['perform']) )
 0653  {
 0654      $perform = (isset($HTTP_POST_VARS['perform'])) ? $HTTP_POST_VARS['perform'] : $HTTP_GET_VARS['perform'];
 0655   
 0656      switch($perform)
 0657      {
 0658          case 'backup':
 0659   
 0660              $error = false;
 0661              switch(SQL_LAYER)
 0662              {
 0663                  case 'oracle':
 0664                      $error = true;
 0665                      break;
 0666                  case 'db2':
 0667                      $error = true;
 0668                      break;
 0669                  case 'msaccess':
 0670                      $error = true;
 0671                      break;
 0672                  case 'mssql':
 0673                  case 'mssql-odbc':
 0674                      $error = true;
 0675                      break;
 0676              }
 0677   
 0678              if ($error)
 0679              {
 0680                  include('./page_header_admin.'.$phpEx);
 0681   
 0682                  $template->set_filenames(array(
 0683                      "body" => "admin/admin_message_body.tpl")
 0684                  );
 0685   
 0686                  $template->assign_vars(array(
 0687                      "MESSAGE_TITLE" => $lang['Information'],
 0688                      "MESSAGE_TEXT" => $lang['Backups_not_supported'])
 0689                  );
 0690   
 0691                  $template->pparse("body");
 0692   
 0693                  include('./page_footer_admin.'.$phpEx);
 0694              }
 0695   
 0696              $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');
 0697   
 0698              $additional_tables = (isset($HTTP_POST_VARS['additional_tables'])) ? $HTTP_POST_VARS['additional_tables'] : ( (isset($HTTP_GET_VARS['additional_tables'])) ? $HTTP_GET_VARS['additional_tables'] : "" );
 0699   
 0700              $backup_type = (isset($HTTP_POST_VARS['backup_type'])) ? $HTTP_POST_VARS['backup_type'] : ( (isset($HTTP_GET_VARS['backup_type'])) ? $HTTP_GET_VARS['backup_type'] : "" );
 0701   
 0702              $gzipcompress = (!empty($HTTP_POST_VARS['gzipcompress'])) ? $HTTP_POST_VARS['gzipcompress'] : ( (!empty($HTTP_GET_VARS['gzipcompress'])) ? $HTTP_GET_VARS['gzipcompress'] : 0 );
 0703   
 0704              $drop = (!empty($HTTP_POST_VARS['drop'])) ? intval($HTTP_POST_VARS['drop']) : ( (!empty($HTTP_GET_VARS['drop'])) ? intval($HTTP_GET_VARS['drop']) : 0 );
 0705   
 0706              if(!empty($additional_tables))
 0707              {
 0708                  if(ereg(",", $additional_tables))
 0709                  {
 0710                      $additional_tables = split(",", $additional_tables);
 0711   
 0712                      for($i = 0; $i < count($additional_tables); $i++)
 0713                      {
 0714                          $tables[] = trim($additional_tables[$i]);
 0715                      }
 0716   
 0717                  }
 0718                  else
 0719                  {
 0720                      $tables[] = trim($additional_tables);
 0721                  }
 0722              }
 0723   
 0724              if( !isset($HTTP_POST_VARS['backupstart']) && !isset($HTTP_GET_VARS['backupstart']))
 0725              {
 0726                  include('./page_header_admin.'.$phpEx);
 0727   
 0728                  $template->set_filenames(array(
 0729                      "body" => "admin/db_utils_backup_body.tpl")
 0730                  );    
 0731                  $s_hidden_fields = "<input type=\"hidden\" name=\"perform\" value=\"backup\" /><input type=\"hidden\" name=\"drop\" value=\"1\" /><input type=\"hidden\" name=\"perform\" value=\"$perform\" />";
 0732   
 0733                  $template->assign_vars(array(
 0734                      "L_DATABASE_BACKUP" => $lang['Database_Utilities'] . " : " . $lang['Backup'],
 0735                      "L_BACKUP_EXPLAIN" => $lang['Backup_explain'],
 0736                      "L_FULL_BACKUP" => $lang['Full_backup'],
 0737                      "L_STRUCTURE_BACKUP" => $lang['Structure_backup'],
 0738                      "L_DATA_BACKUP" => $lang['Data_backup'],
 0739                      "L_ADDITIONAL_TABLES" => $lang['Additional_tables'],
 0740                      "L_START_BACKUP" => $lang['Start_backup'],
 0741                      "L_BACKUP_OPTIONS" => $lang['Backup_options'],
 0742                      "L_GZIP_COMPRESS" => $lang['Gzip_compress'],
 0743                      "L_NO" => $lang['No'],
 0744                      "L_YES" => $lang['Yes'],
 0745   
 0746                      "S_HIDDEN_FIELDS" => $s_hidden_fields,
 0747                      "S_DBUTILS_ACTION" => append_sid("admin_db_utilities.$phpEx"))
 0748                  );
 0749                  $template->pparse("body");
 0750   
 0751                  break;
 0752   
 0753              }
 0754              else if( !isset($HTTP_POST_VARS['startdownload']) && !isset($HTTP_GET_VARS['startdownload']) )
 0755              {
 0756                  if(is_array($additional_tables))
 0757                  {
 0758                      $additional_tables = implode(',', $additional_tables);
 0759                  }
 0760                  $template->set_filenames(array(
 0761                      "body" => "admin/admin_message_body.tpl")
 0762                  );
 0763   
 0764                  $template->assign_vars(array(
 0765                      "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&amp;backupstart=1&gzipcompress=$gzipcompress&startdownload=1") . '">',
 0766   
 0767                      "MESSAGE_TITLE" => $lang['Database_Utilities'] . " : " . $lang['Backup'],
 0768                      "MESSAGE_TEXT" => $lang['Backup_download'])
 0769                  );
 0770   
 0771                  include('./page_header_admin.'.$phpEx);
 0772   
 0773                  $template->pparse("body");
 0774   
 0775                  include('./page_footer_admin.'.$phpEx);
 0776   
 0777              }
 0778              header("Pragma: no-cache");
 0779              $do_gzip_compress = FALSE;
 0780              if( $gzipcompress )
 0781              {
 0782                  $phpver = phpversion();
 0783   
 0784                  if($phpver >= "4.0")
 0785                  {
 0786                      if(extension_loaded("zlib"))
 0787                      {
 0788                          $do_gzip_compress = TRUE;
 0789                      }
 0790                  }
 0791              }
 0792              if($do_gzip_compress)
 0793              {
 0794                  @ob_start();
 0795                  @ob_implicit_flush(0);
 0796                  header("Content-Type: application/x-gzip; name=\"phpbb_db_backup.sql.gz\"");
 0797                  header("Content-disposition: attachment; filename=phpbb_db_backup.sql.gz");
 0798              }
 0799              else
 0800              {
 0801                  header("Content-Type: text/x-delimtext; name=\"phpbb_db_backup.sql\"");
 0802                  header("Content-disposition: attachment; filename=phpbb_db_backup.sql");
 0803              }
 0804   
 0805              //
 0806              // Build the sql script file...
 0807              //
 0808              echo "#\n";
 0809              echo "# phpBB Backup Script\n";
 0810              echo "# Dump of tables for $dbname\n";
 0811              echo "#\n# DATE : " .  gmdate("d-m-Y H:i:s", time()) . " GMT\n";
 0812              echo "#\n";
 0813   
 0814              if(SQL_LAYER == 'postgresql')
 0815              {
 0816                   echo "\n" . pg_get_sequences("\n", $backup_type);
 0817              }
 0818              for($i = 0; $i < count($tables); $i++)
 0819              {
 0820                  $table_name = $tables[$i];
 0821   
 0822                  switch (SQL_LAYER)
 0823                  {
 0824                      case 'postgresql':
 0825                          $table_def_function = "get_table_def_postgresql";
 0826                          $table_content_function = "get_table_content_postgresql";
 0827                          break;
 0828   
 0829                      case 'mysql':
 0830                      case 'mysql4':
 0831                          $table_def_function = "get_table_def_mysql";
 0832                          $table_content_function = "get_table_content_mysql";
 0833                          break;
 0834                  }
 0835   
 0836                  if($backup_type != 'data')
 0837                  {
 0838                      echo "#\n# TABLE: " . $table_prefix . $table_name . "\n#\n";
 0839                      echo $table_def_function($table_prefix . $table_name, "\n") . "\n";
 0840                  }
 0841   
 0842                  if($backup_type != 'structure')
 0843                  {
 0844                      $table_content_function($table_prefix . $table_name, "output_table_content");
 0845                  }
 0846              }
 0847              
 0848              if($do_gzip_compress)
 0849              {
 0850                  $Size = ob_get_length();
 0851                  $Crc = crc32(ob_get_contents());
 0852                  $contents = gzcompress(ob_get_contents());
 0853                  ob_end_clean();
 0854                  echo "\x1f\x8b\x08\x00\x00\x00\x00\x00".substr($contents, 0, strlen($contents) - 4).gzip_PrintFourChars($Crc).gzip_PrintFourChars($Size);
 0855              }
 0856              exit;
 0857   
 0858              break;
 0859   
 0860          case 'restore':
 0861              if(!isset($HTTP_POST_VARS['restore_start']))
 0862              {
 0863                  //
 0864                  // Define Template files...
 0865                  //
 0866                  include('./page_header_admin.'.$phpEx);
 0867   
 0868                  $template->set_filenames(array(
 0869                      "body" => "admin/db_utils_restore_body.tpl")
 0870                  );
 0871   
 0872                  $s_hidden_fields = "<input type=\"hidden\" name=\"perform\" value=\"restore\" /><input type=\"hidden\" name=\"perform\" value=\"$perform\" />";
 0873   
 0874                  $template->assign_vars(array(
 0875                      "L_DATABASE_RESTORE" => $lang['Database_Utilities'] . " : " . $lang['Restore'],
 0876                      "L_RESTORE_EXPLAIN" => $lang['Restore_explain'],
 0877                      "L_SELECT_FILE" => $lang['Select_file'],
 0878                      "L_START_RESTORE" => $lang['Start_Restore'],
 0879   
 0880                      "S_DBUTILS_ACTION" => append_sid("admin_db_utilities.$phpEx"),
 0881                      "S_HIDDEN_FIELDS" => $s_hidden_fields)
 0882                  );
 0883                  $template->pparse("body");
 0884   
 0885                  break;
 0886   
 0887              }
 0888              else
 0889              {
 0890                  //
 0891                  // Handle the file upload ....
 0892                  // If no file was uploaded report an error...
 0893                  //
 0894                  $backup_file_name = (!empty($HTTP_POST_FILES['backup_file']['name'])) ? $HTTP_POST_FILES['backup_file']['name'] : "";
 0895                  $backup_file_tmpname = ($HTTP_POST_FILES['backup_file']['tmp_name'] != "none") ? $HTTP_POST_FILES['backup_file']['tmp_name'] : "";
 0896                  $backup_file_type = (!empty($HTTP_POST_FILES['backup_file']['type'])) ? $HTTP_POST_FILES['backup_file']['type'] : "";
 0897   
 0898                  if($backup_file_tmpname == "" || $backup_file_name == "")
 0899                  {
 0900                      message_die(GENERAL_MESSAGE, $lang['Restore_Error_no_file']);
 0901                  }
 0902                  //
 0903                  // If I file was actually uploaded, check to make sure that we
 0904                  // are actually passed the name of an uploaded file, and not
 0905                  // a hackers attempt at getting us to process a local system
 0906                  // file.
 0907                  //
 0908                  if( file_exists(phpbb_realpath($backup_file_tmpname)) )
 0909                  {
 0910                      if( preg_match("/^(text\/[a-zA-Z]+)|(application\/(x\-)?gzip(\-compressed)?)|(application\/octet-stream)$/is", $backup_file_type) )
 0911                      {
 0912                          if( preg_match("/\.gz$/is",$backup_file_name) )
 0913                          {
 0914                              $do_gzip_compress = FALSE;
 0915                              $phpver = phpversion();
 0916                              if($phpver >= "4.0")
 0917                              {
 0918                                  if(extension_loaded("zlib"))
 0919                                  {
 0920                                      $do_gzip_compress = TRUE;
 0921                                  }
 0922                              }
 0923   
 0924                              if($do_gzip_compress)
 0925                              {
 0926                                  $gz_ptr = gzopen($backup_file_tmpname, 'rb');
 0927                                  $sql_query = "";
 0928                                  while( !gzeof($gz_ptr) )
 0929                                  {
 0930                                      $sql_query .= gzgets($gz_ptr, 100000);
 0931                                  }
 0932                              }
 0933                              else
 0934                              {
 0935                                  message_die(GENERAL_ERROR, $lang['Restore_Error_decompress']);
 0936                              }
 0937                          }
 0938                          else
 0939                          {
 0940                              $sql_query = fread(fopen($backup_file_tmpname, 'r'), filesize($backup_file_tmpname));
 0941                          }
 0942                          //
 0943                          // Comment this line out to see if this fixes the stuff...
 0944                          //
 0945                          //$sql_query = stripslashes($sql_query);
 0946                      }
 0947                      else
 0948                      {
 0949                          message_die(GENERAL_ERROR, $lang['Restore_Error_filename'] ." $backup_file_type $backup_file_name");
 0950                      }
 0951                  }
 0952                  else
 0953                  {
 0954                      message_die(GENERAL_ERROR, $lang['Restore_Error_uploading']);
 0955                  }
 0956   
 0957                  if($sql_query != "")
 0958                  {
 0959                      // Strip out sql comments...
 0960                      $sql_query = remove_remarks($sql_query);
 0961                      $pieces = split_sql_file($sql_query, ";");
 0962   
 0963                      $sql_count = count($pieces);
 0964                      for($i = 0; $i < $sql_count; $i++)
 0965                      {
 0966                          $sql = trim($pieces[$i]);
 0967   
 0968                          if(!empty($sql) and $sql[0] != "#")
 0969                          {
 0970                              if(VERBOSE == 1)
 0971                              {
 0972                                  echo "Executing: $sql\n<br>";
 0973                                  flush();
 0974                              }
 0975   
 0976                              $result = $db->sql_query($sql);
 0977   
 0978                              if(!$result && ( !(SQL_LAYER == 'postgresql' && eregi("drop table", $sql) ) ) )
 0979                              {
 0980                                  message_die(GENERAL_ERROR, "Error importing backup file", "", __LINE__, __FILE__, $sql);
 0981                              }
 0982                          }
 0983                      }
 0984                  }
 0985   
 0986                  include('./page_header_admin.'.$phpEx);
 0987   
 0988                  $template->set_filenames(array(
 0989                      "body" => "admin/admin_message_body.tpl")
 0990                  );
 0991   
 0992                  $message = $lang['Restore_success'];
 0993   
 0994                  $template->assign_vars(array(
 0995                      "MESSAGE_TITLE" => $lang['Database_Utilities'] . " : " . $lang['Restore'],
 0996                      "MESSAGE_TEXT" => $message)
 0997                  );
 0998   
 0999                  $template->pparse("body");
 1000                  break;
 1001              }
 1002              break;
 1003      }
 1004  }
 1005   
 1006  include('./page_footer_admin.'.$phpEx);
 1007   
 1008  ?>
 1009