home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phpnuke / PHP-Nuke-7.5.exe / html / admin / modules / backup.php < prev    next >
PHP Script  |  2004-07-24  |  8KB  |  256 lines

  1. <?php
  2.  
  3. /************************************************************************/
  4. /* PHP-NUKE: Web Portal System                                          */
  5. /* ===========================                                          */
  6. /*                                                                      */
  7. /* Save the database of a PHPNuke web site                              */
  8. /*                                                                      */
  9. /* Copyright (c) 2001 by Thomas Rudant (thomas.rudant@grunk.net)        */
  10. /* http://www.grunk.net                                                 */
  11. /* http://www.securite-internet.org                                     */
  12. /*                                    */
  13. /* This program is free software. You can redistribute it and/or modify */
  14. /* it under the terms of the GNU General Public License as published by */
  15. /* the Free Software Foundation; either version 2 of the License.       */
  16. /************************************************************************/
  17. /*         Additional security & Abstraction layer conversion           */
  18. /*                           2003 chatserv                              */
  19. /*      http://www.nukefixes.com -- http://www.nukeresources.com        */
  20. /************************************************************************/
  21.  
  22. if (!eregi("admin.php", $_SERVER['PHP_SELF'])) { die ("Access Denied"); }
  23. global $prefix, $db;
  24. $aid = substr("$aid", 0,25);
  25. $row = $db->sql_fetchrow($db->sql_query("SELECT radminsuper FROM " . $prefix . "_authors WHERE aid='$aid'"));
  26. if ($row['radminsuper'] == 1) {
  27.  
  28.     switch($op) {
  29.     
  30.         case "backup":
  31.         @set_time_limit(600);
  32.         $crlf="\n";
  33.  
  34.         switch($lang)
  35.         {
  36.             case french : 
  37.                 // French Text
  38.                 $strNoTablesFound    = "Aucune table n'a ΘtΘ trouvΘe dans cette base.";
  39.                 $strHost        = "Serveur";
  40.                 $strDatabase        = "Base de donnΘes";
  41.                 $strTableStructure    = "Structure de la table";
  42.                 $strDumpingData        = "Contenu de la table";
  43.                 $strError        = "Erreur";
  44.                 $strSQLQuery        = "requΩte SQL";
  45.                 $strMySQLSaid        = "MySQL a rΘpondu:";
  46.                 $strBack        = "Retour";
  47.                 $strFileName        = "Sauvegarde BD";
  48.                 $strName        = "Sauvegarde de la base de donnΘes";
  49.                 $strDone        = "effectuΘe le";
  50.                 $strat            = "α";
  51.                 $strby            = "par";
  52.                 $date_jour = date ("d-m-Y");
  53.                 break;
  54.                 
  55.             default : 
  56.                 // English Text    
  57.                 $strNoTablesFound = "No tables found in database.";
  58.                 $strHost = "Host";
  59.                 $strDatabase = "Database ";
  60.                 $strTableStructure = "Table structure for table";
  61.                 $strDumpingData = "Dumping data for table";
  62.                 $strError = "Error";
  63.                 $strSQLQuery = "SQL-query";
  64.                 $strMySQLSaid = "MySQL said: ";
  65.                 $strBack = "Back";
  66.                 $strFileName = "Save Database";
  67.                 $strName = "Database saved";
  68.                 $strDone = "On";
  69.                 $strat = "at";
  70.                 $strby = "by";
  71.                 $date_jour = date ("m-d-Y");        
  72.                 break;
  73.         }
  74.         
  75.         header("Content-disposition: filename=$strFileName $dbname $date_jour.sql");
  76.         header("Content-type: application/octetstream");
  77.         header("Pragma: no-cache");
  78.         header("Expires: 0");
  79.         
  80.         // doing some DOS-CRLF magic...
  81.         $client = $_SERVER["HTTP_USER_AGENT"];
  82.         if(ereg('[^(]*\((.*)\)[^)]*',$client,$regs)) 
  83.         {
  84.         $os = $regs[1];
  85.         // this looks better under WinX
  86.         if (eregi("Win",$os)) 
  87.             $crlf="\r\n";
  88.         }
  89.         
  90.         
  91.         function my_handler($sql_insert)
  92.         {
  93.             global $crlf;
  94.             echo "$sql_insert;$crlf";
  95.         }
  96.         
  97.         // Get the content of $table as a series of INSERT statements.
  98.         // After every row, a custom callback function $handler gets called.
  99.         // $handler must accept one parameter ($sql_insert);
  100.         function get_table_content($db, $table, $handler)
  101.         {
  102.             $result = mysql_db_query($db, "SELECT * FROM $table") or mysql_die();
  103.             $i = 0;
  104.             while($row = mysql_fetch_row($result))
  105.             {
  106.         //        set_time_limit(60); // HaRa
  107.                 $table_list = "(";
  108.         
  109.                 for($j=0; $j<mysql_num_fields($result);$j++)
  110.                     $table_list .= mysql_field_name($result,$j).", ";
  111.         
  112.                 $table_list = substr($table_list,0,-2);
  113.                 $table_list .= ")";
  114.         
  115.                 if(isset($GLOBALS["showcolumns"]))
  116.                     $schema_insert = "INSERT INTO $table $table_list VALUES (";
  117.                 else
  118.                     $schema_insert = "INSERT INTO $table VALUES (";
  119.         
  120.                 for($j=0; $j<mysql_num_fields($result);$j++)
  121.                 {
  122.                     if(!isset($row[$j]))
  123.                         $schema_insert .= " NULL,";
  124.                     elseif($row[$j] != "")
  125.                         $schema_insert .= " '".addslashes($row[$j])."',";
  126.                     else
  127.                         $schema_insert .= " '',";
  128.                 }
  129.                 $schema_insert = ereg_replace(",$", "", $schema_insert);
  130.                 $schema_insert .= ")";
  131.                 $handler(trim($schema_insert));
  132.                 $i++;
  133.             }
  134.             return (true);
  135.         }
  136.         
  137.         // Return $table's CREATE definition
  138.         // Returns a string containing the CREATE statement on success
  139.         function get_table_def($db, $table, $crlf)
  140.         {
  141.             $schema_create = "";
  142.             //$schema_create .= "DROP TABLE IF EXISTS $table;$crlf";
  143.             $schema_create .= "CREATE TABLE $table ($crlf";
  144.         
  145.             $result = mysql_db_query($db, "SHOW FIELDS FROM $table") or mysql_die();
  146.             while($row = mysql_fetch_array($result))
  147.             {
  148.                 $schema_create .= "   $row[Field] $row[Type]";
  149.         
  150.                 if(isset($row["Default"]) && (!empty($row["Default"]) || $row["Default"] == "0"))
  151.                     $schema_create .= " DEFAULT '$row[Default]'";
  152.                 if($row["Null"] != "YES")
  153.                     $schema_create .= " NOT NULL";
  154.                 if($row["Extra"] != "")
  155.                     $schema_create .= " $row[Extra]";
  156.                 $schema_create .= ",$crlf";
  157.             }
  158.             $schema_create = ereg_replace(",".$crlf."$", "", $schema_create);
  159.             $result = mysql_db_query($db, "SHOW KEYS FROM $table") or mysql_die();
  160.             while($row = mysql_fetch_array($result))
  161.             {
  162.                 $kname=$row['Key_name'];
  163.                 if(($kname != "PRIMARY") && ($row['Non_unique'] == 0))
  164.                     $kname="UNIQUE|$kname";
  165.                  if(!isset($index[$kname]))
  166.                      $index[$kname] = array();
  167.                  $index[$kname][] = $row['Column_name'];
  168.             }
  169.         
  170.             while(list($x, $columns) = @each($index))
  171.             {
  172.                  $schema_create .= ",$crlf";
  173.                  if($x == "PRIMARY")
  174.                      $schema_create .= "   PRIMARY KEY (" . implode($columns, ", ") . ")";
  175.                  elseif (substr($x,0,6) == "UNIQUE")
  176.                     $schema_create .= "   UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")";
  177.                  else
  178.                     $schema_create .= "   KEY $x (" . implode($columns, ", ") . ")";
  179.             }
  180.         
  181.             $schema_create .= "$crlf)";
  182.             return (stripslashes($schema_create));
  183.         }
  184.         
  185.         function mysql_die($error = "")
  186.         {
  187.             echo "<b> $strError </b><p>";
  188.             if(isset($sql_query) && !empty($sql_query))
  189.             {
  190.                 echo "$strSQLQuery: <pre>$sql_query</pre><p>";
  191.             }
  192.             if(empty($error))
  193.                 echo $strMySQLSaid.mysql_error();
  194.             else
  195.                 echo $strMySQLSaid.$error;
  196.             echo "<br><a href=\"javascript:history.go(-1)\">$strBack</a>";
  197.             exit;
  198.         }
  199.         
  200.         global $dbhost, $dbuname, $dbpass, $dbname;
  201.         mysql_pconnect($dbhost, $dbuname, $dbpass);
  202.         @mysql_select_db("$dbname") or die ("Unable to select database");
  203.         
  204.         $tables = mysql_list_tables($dbname);
  205.         
  206.         $num_tables = @mysql_numrows($tables);
  207.         if($num_tables == 0)
  208.         {
  209.             echo $strNoTablesFound;
  210.         }
  211.         else
  212.         {
  213.             $i = 0;
  214.             $heure_jour = date ("H:i");
  215.             print "# ========================================================$crlf";
  216.             print "#$crlf";
  217.             print "# $strName : $dbname$crlf";
  218.             print "# $strDone $date_jour $strat $heure_jour $strby $name !$crlf";
  219.             print "#$crlf";
  220.             print "# ========================================================$crlf";
  221.             print "$crlf";
  222.             
  223.             while($i < $num_tables)
  224.             { 
  225.                 $table = mysql_tablename($tables, $i);
  226.         
  227.                 print $crlf;
  228.                 print "# --------------------------------------------------------$crlf";
  229.                 print "#$crlf";
  230.                 print "# $strTableStructure '$table'$crlf";
  231.                 print "#$crlf";
  232.                 print $crlf;
  233.         
  234.                 echo get_table_def($dbname, $table, $crlf).";$crlf$crlf";
  235.                 
  236.             print "#$crlf";
  237.             print "# $strDumpingData '$table'$crlf";
  238.             print "#$crlf";
  239.             print $crlf;
  240.             
  241.             get_table_content($dbname, $table, "my_handler");
  242.         
  243.                 $i++;
  244.             }
  245.         }
  246.         
  247.         //Header("Location: admin.php?op=adminMain");
  248.         break;
  249.  
  250.     }
  251.             
  252. } else {
  253.     echo "Access Denied";
  254. }
  255.  
  256. ?>