<?php
   error_reporting(0);
   
   // Function to write html header information only for display form and XML
   // In case of saving file we don't need this code
   function writeHeader(){
      echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
              <html>
                <head>
                  <title>Database To XML</title>
                  <link href="style/style.css" rel="stylesheet" type="text/css" />
                </head>
              <body>';
   }

   // Check whether one of the submit button was pressed
   if (isset($_POST['saveBtn']) || isset($_POST['dispBtn'])){
       
      // Set the variables from the user input 
      $host   = isset($_POST['hostname'])? $_POST['hostname'] : 'localhost';
      $user   = isset($_POST['username'])? $_POST['username'] : '';
      $pass   = isset($_POST['password'])? $_POST['password'] : '';
      $port   = isset($_POST['port'])    ? $_POST['port']     : '3306';
      $out    = '';
      $outfile= '';
    
      $fullhost = $host.":".$port;  
    
      // Make a MySQL connection
      $link = mysql_connect($fullhost,$user,$pass);
      
      // Check if connection was success
      if (!$link){
        echo '<div id="main"><div id="formheader">Can not connect to database: '.mysql_error().' </div></div>';
        die;
      }
      
      // Get all databases
      $dbs = mysql_query('SHOW DATABASES');
      $outfile.='<MySQLserver hostname="'.$host.'" username="'.$user.'" password="'.$pass.'" port="'.$port.'">'."\n";
      $out .= htmlspecialchars('<MySQLserver hostname="'.$host.'" username="'.$user.'" password="'.$pass.'" port="'.$port.'">'."\n");
      
      while ($dbrow = mysql_fetch_array($dbs, MYSQL_NUM)) {
         $db = $dbrow[0];
         mysql_selectdb($db,$link);
         $result = mysql_query('SHOW TABLES');
         $outfile.="   <Database name='$db'>\n";
         $out.="&nbsp;&nbsp;&nbsp;";
         $out.=htmlspecialchars("<Database name='$db'>\n");

         // Get all tables for the actual database         
         while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            $table = $row[0]  ;
            $struct = mysql_query('DESCRIBE '.$table);
            $outfile.='      <Table name="'.$table.'">'."\n";
            $out.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            $out.= htmlspecialchars('<Table name="'.$table.'">'."\n");
                
            // Get all fields for the actual table    
            while ($row2 = mysql_fetch_array($struct, MYSQL_NUM)) {
               $autoi = (strstr($row2[5],'auto_increment')) ? 'YES' : 'NO';
               $tmp ='         <Field name="'.$row2[0].'" ';
               $tmp.='type="'.$row2[1].'" ';
               $tmp.='null="'.$row2[2].'" ';
               $tmp.='key="'.$row2[3].'" ';
               $tmp.='default="'.$row2[4].'" ';
               $tmp.='autoinc="'.$autoi.'"/>'."\n";
               $outfile.=$tmp;
               $out.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
               $out.=htmlspecialchars($tmp);
            }
            
            // Close table element
            $outfile.='      </Table>'."\n";
            $out.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";     
            $out.=htmlspecialchars('</Table>'."\n");
                
         }
         
         // Close database element
         $outfile.="   </Database>\n";
         $out.="&nbsp;&nbsp;&nbsp;";
         $out.=htmlspecialchars("</Database>\n");
      
      }
      
      // Close the server element
      $outfile.='</MySQLserver>';
      $out.=htmlspecialchars('</MySQLserver>');
        
      // Check output format and display or save the result
      if (isset($_POST['dispBtn'])){
         writeHeader();
         echo '<div id="main2"><div id="formbody2">';
         echo nl2br($out) ;
         echo '</div></div>';    
      }
      else{
         // Save output as file
         $filecontent=$outfile;
         $downloadfile='MySQL_Server.xml';
           
         header("HTTP/1.1 200 OK");
         header("Content-Length: ".strlen($filecontent));
         header("Content-Type: application/force-download");
         header("Content-Disposition: attachment; filename=$downloadfile");
         header("Content-Transfer-Encoding: binary");            

         echo $filecontent;
            
      }
        
   }
   // Display the form for the user
   else {
       writeHeader();
?>    

      <div id="main">
        <div id="formheader">Database connection informations</div>
        <div id="formbody">
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="dbXml" id="dbXml">
            <table align="center">
              <tr><th>Hostname:</th><td><input name="hostname" type="text" size="30" maxlength="15" value="localhost"></td></tr>
              <tr><th>Username:</th><td><input name="username" type="text" size="30" maxlength="15"></td></tr>
              <tr><th>Password:</th><td><input name="password" type="password" size="30" maxlength="15"></td></tr>
              <tr><th>Port:</th><td><input name="port" type="text" size="30" value="3306"></td></tr>
              <tr>
                 <td align="center"><br/><input class="text" type="submit" name="saveBtn" value="Save as..."></td>
                 <td align="center"><br/><input class="text" type="submit" name="dispBtn" value="Display output"></td>
              </tr>
            </table>  
          </form>

        </div>
      </div>
   </body>            
<?php
   }
?>