Source for file admin.mostlydbadmin.php

Documentation is available at admin.mostlydbadmin.php

  1. <?php
  2. /**
  3. @package Mambo
  4. @author Mambo Foundation Inc see README.php
  5. @copyright Mambo Foundation Inc.
  6. *  See COPYRIGHT.php for copyright notices and details.
  7. @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see
  8. *  LICENSE.php
  9. *  Mambo is free software; you can redistribute it and/or
  10. *  modify it under the terms of the GNU General Public License
  11. *  as published by the Free Software Foundation; version 2 of the
  12. *  License.
  13. */ 
  14.  
  15. /** ensure this file is being included by a parent file */
  16. defined'_VALID_MOS' or die'Direct Access to this location is not allowed.' );
  17.  
  18. // ensure user is coming from the admin side and has access to this function
  19. if (!($my->usertype=='Super Administrator'&& $adminside>0{
  20.     mosRedirect'index2.php'T_('You are not authorized to view this resource.') );
  21. }
  22.  
  23. require_once$mainframe->getPath'admin_html' ) );
  24. require_once"$mosConfig_absolute_path/administrator/includes/pcl/pclzip.lib.php);
  25.  
  26. $task    mosGetParam$_REQUEST"task""" );
  27. $file    mosGetParam$_POST"file"null );
  28. $upfile    mosGetParam($_FILES,"upfile",null);
  29. $tables mosGetParam$_POST"tables"null );
  30. $OutType mosGetParam$_POST"OutType"null );
  31. $OutDest mosGetParam$_POST"OutDest"null );
  32. $toBackUp mosGetParam$_POST"toBackUp"null );
  33.  
  34. switch ($task{
  35.     case "dbBackup":
  36.         dbBackup$option);
  37.         break;
  38.  
  39.     case "doBackup":
  40.         doBackup$tables,$OutType,$OutDest,$toBackUp,$_SERVER['HTTP_USER_AGENT']$local_backup_path);
  41.         break;
  42.  
  43.     case "dbRestore":
  44.         dbRestore$local_backup_path);
  45.         break;
  46.  
  47.     case "doRestore":
  48.         doRestore$file,$upfile,$local_backup_path);
  49.         break;
  50.  
  51.     case "xquery":
  52.     default:
  53.         xquery$option );
  54.         break;
  55. }
  56.  
  57.  
  58. function dbBackup$p_option {
  59.     global $database;
  60.  
  61.     $database->setQuery"SHOW tables" );
  62.     $tables $database->loadResultArray();
  63.     $tables2 arraymosHTML::makeOption'all'T_('All Mambo Tables') ) );
  64.     foreach ($tables as $table{
  65.         $tables2[mosHTML::makeOption$table );
  66.     }
  67.  
  68.     $tablelist mosHTML::selectList$tables2'tables[]''class="inputbox" size="5" multiple="multiple"',
  69.     'value''text''all' );
  70.  
  71.     HTML_dbadmin::backupIntro$tablelist$p_option );
  72. }
  73.  
  74. function doBackup$tables$OutType$OutDest$toBackUp$UserAgent$local_backup_path{
  75.     global $database;
  76.     global $mosConfig_db$mosConfig_sitename$version,$option,$task;
  77.  
  78.     if (!$tables[0])
  79.     {
  80.         HTML_dbadmin::showDbAdminMessage(T_('Error! No database table(s) specified. Please select at least one table and re-try.</p>')T_('DB Admin'),$option,$task);
  81.         return;
  82.     }
  83.  
  84.     /* Need to know what browser the user has to accomodate nonstandard headers */
  85.  
  86.     if (ereg('Opera(/| )([0-9].[0-9]{1,2})'$UserAgent)) {
  87.         $UserBrowser "Opera";
  88.     }
  89.     elseif (ereg('MSIE ([0-9].[0-9]{1,2})'$UserAgent)) {
  90.         $UserBrowser "IE";
  91.     else {
  92.         $UserBrowser '';
  93.     }
  94.  
  95.     /* Determine the mime type and file extension for the output file */
  96.  
  97.     if ($OutType == "bzip"{
  98.         $filename $mosConfig_db "_" date("jmYHis"".bz2";
  99.         $mime_type 'application/x-bzip';
  100.     elseif ($OutType == "gzip"{
  101.         $filename $mosConfig_db "_" date("jmYHis"".sql.gz";
  102.         $mime_type 'application/x-gzip';
  103.     elseif ($OutType == "zip"{
  104.         $filename $mosConfig_db "_" date("jmYHis"".zip";
  105.         $mime_type 'application/x-zip';
  106.     elseif ($OutType == "html"{
  107.         $filename $mosConfig_db "_" date("jmYHis"".html";
  108.         $mime_type ($UserBrowser == 'IE' || $UserBrowser == 'Opera''application/octetstream' 'application/octet-stream';
  109.     else {
  110.         $filename $mosConfig_db "_" date("jmYHis"".sql";
  111.         $mime_type ($UserBrowser == 'IE' || $UserBrowser == 'Opera''application/octetstream' 'application/octet-stream';
  112.     };
  113.  
  114.     /* Store all the tables we want to back-up in variable $tables[] */
  115.  
  116.     if ($tables[0== "all"{
  117.         array_pop($tables);
  118.         $database->setQuery("SHOW tables");
  119.         $database->query();
  120.         $tables array_merge($tables$database->loadResultArray());
  121.     }
  122.  
  123.     /* Store the "Create Tables" SQL in variable $CreateTable[$tblval] */
  124.     if ($toBackUp!="data")
  125.     {
  126.         foreach ($tables as $tblval)
  127.         {
  128.             $database->setQuery("SHOW CREATE table $tblval");
  129.             $database->query();
  130.             $CreateTable[$tblval$database->loadResultArray(1);
  131.         }
  132.     }
  133.  
  134.     /* Store all the FIELD TYPES being backed-up (text fields need to be delimited) in variable $FieldType*/
  135.     if ($toBackUp!="structure")
  136.     {
  137.         foreach ($tables as $tblval)
  138.         {
  139.             $database->setQuery("SHOW FIELDS FROM $tblval");
  140.             $database->query();
  141.             $fields $database->loadObjectList();
  142.             foreach($fields as $field)
  143.             {
  144.                 $FieldType[$tblval][$field->Fieldpreg_replace("/[(0-9)]/",''$field->Type);
  145.             }
  146.         }
  147.     }
  148.  
  149.     /* Build the fancy header on the dump file */
  150.     $OutBuffer "";
  151.     if ($OutType == 'html'{
  152.     else {
  153.         $OutBuffer .= "#\n";
  154.         $OutBuffer .= "# Mambo MySQL-Dump\n";
  155.         $OutBuffer .= "# http://www.mambo-foundation.org\n";
  156.         $OutBuffer .= "#\n";
  157.         $OutBuffer .= "# Host: $mosConfig_sitename\n";
  158.         $OutBuffer .= "# Generation Time: " date("M j, Y \a\\t H:i""\n";
  159.         $OutBuffer .= "# Server version: " $database->getVersion("\n";
  160.         $OutBuffer .= "# PHP Version: " phpversion("\n";
  161.         $OutBuffer .= "# Database : `" $mosConfig_db "`\n# --------------------------------------------------------\n";
  162.     }
  163.  
  164.     /* Okay, here's the meat & potatoes */
  165.     foreach ($tables as $tblval{
  166.         if ($toBackUp != "data"{
  167.             if ($OutType == 'html'{
  168.             else {
  169.                 $OutBuffer .= "#\n# Table structure for table `$tblval`\n";
  170.                 $OutBuffer .= "#\nDROP table IF EXISTS $tblval;\n";
  171.                 $OutBuffer .= $CreateTable[$tblval][0].";\r\n";
  172.             }
  173.         }
  174.         if ($toBackUp != "structure"{
  175.             if ($OutType == 'html'{
  176.                 $OutBuffer .= "<div align=\"left\">";
  177.                 $OutBuffer .= "<table cellspacing=\"0\" cellpadding=\"2\" border=\"1\">";
  178.                 $database->setQuery("SELECT * FROM $tblval");
  179.                 $rows $database->loadObjectList();
  180.  
  181.                 $OutBuffer .= "<tr><th colspan=\"".count@array_keys@$rows[0) )."\">`$tblval`</th></tr>";
  182.                 if (count$rows )) {
  183.                     $OutBuffer .= "<tr>";
  184.                     foreach($rows[0as $key => $value{
  185.                         $OutBuffer .= "<th>$key</th>";
  186.                     }
  187.                     $OutBuffer .= "</tr>";
  188.                 }
  189.  
  190.                 if ($rowsforeach($rows as $row)
  191.                 {
  192.                     $OutBuffer .= "<tr>";
  193.                     foreach (get_object_vars($rowas $key=>$value)
  194.                     {
  195.                         $value addslashes$value );
  196.                         $value str_replace"\n"'\r\n'$value );
  197.                         $value str_replace"\r"''$value );
  198.  
  199.                         $value htmlspecialchars$value );
  200.  
  201.                         if (preg_match ("/\b" $FieldType[$tblval][$key"\b/i""DATE TIME DATETIME CHAR VARCHAR TEXT TINYTEXT MEDIUMTEXT LONGTEXT BLOB TINYBLOB MEDIUMBLOB LONGBLOB ENUM SET"))
  202.                         {
  203.                             $OutBuffer .= "<td>'$value'</td>";
  204.                         }
  205.                         else
  206.                         {
  207.                             $OutBuffer .= "<td>$value</td>";
  208.                         }
  209.                     }
  210.                     $OutBuffer .= "</tr>";
  211.                 }
  212.                 $OutBuffer .= "</table></div><br />";
  213.             else {
  214.                 $OutBuffer .= "#\n# Dumping data for table `$tblval`\n#\n";
  215.                 $database->setQuery("SELECT * FROM $tblval");
  216.                 $rows $database->loadObjectList()if (!$rows$rows array();
  217.                 foreach($rows as $row)
  218.                 {
  219.                     $InsertDump "INSERT INTO $tblval VALUES (";
  220.                     //$arr = mosObjectToArray($row);
  221.                     //foreach($arr as $key => $value)
  222.                     foreach (get_object_vars($rowas $key=>$value)
  223.                     {
  224.                         $value addslashes$value );
  225.                         $value str_replace"\n"'\r\n'$value );
  226.                         $value str_replace"\r"''$value );
  227.                         if (preg_match ("/\b" $FieldType[$tblval][$key"\b/i""DATE TIME DATETIME CHAR VARCHAR TEXT TINYTEXT MEDIUMTEXT LONGTEXT BLOB TINYBLOB MEDIUMBLOB LONGBLOB ENUM SET"))
  228.                         {
  229.                             $InsertDump .= "'$value',";
  230.                         }
  231.                         else
  232.                         {
  233.                             $InsertDump .= "$value,";
  234.                         }
  235.                     }
  236.                     $OutBuffer .= rtrim($InsertDump,','");\n";
  237.                 }
  238.             }
  239.         }
  240.     }
  241.  
  242.     /* Send the HTML headers */
  243.     if ($OutDest == "remote"{
  244.         // dump anything in the buffer
  245.         @ob_end_clean();
  246.         ob_start();
  247.         header('Content-Type: ' $mime_type);
  248.         header('Expires: ' gmdate('D, d M Y H:i:s'' GMT');
  249.  
  250.         if ($UserBrowser == 'IE'{
  251.             header('Content-Disposition: inline; filename="' $filename '"');
  252.             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  253.             header('Pragma: public');
  254.         else {
  255.             header('Content-Disposition: attachment; filename="' $filename '"');
  256.             header('Pragma: no-cache');
  257.         }
  258.     }
  259.  
  260.     if ($OutDest == "screen" || $OutType == "html" {
  261.         if ($OutType == "html"{
  262.                 echo $OutBuffer;
  263.         else {
  264.             $OutBuffer str_replace("<","&lt;",$OutBuffer);
  265.             $OutBuffer str_replace(">","&gt;",$OutBuffer);
  266.             ?>
  267.             <form>
  268.                 <textarea rows="20" cols="80" name="sqldump"  style="background-color:#e0e0e0"><?php echo $OutBuffer;?></textarea>
  269.                 <br />
  270.                 <input type="button" onclick="javascript:this.form.sqldump.focus();this.form.sqldump.select();" class="button" value="Select All" />
  271.             </form>
  272.             <?php
  273.         }
  274.         exit();
  275.     }
  276.             
  277.     switch ($OutType{
  278.         case "sql" :
  279.             if ($OutDest == "local"{
  280.                 $fp fopen("$local_backup_path/$filename""w");
  281.                 if (!$fp{
  282.                     HTML_dbadmin::showDbAdminMessage(sprintf(T_('Database backup FAILURE!!<br />File %s/%s not writable<br />Please contact your admin/webmaster!</p>'),$local_backup_path,$filename),T_('DB Admin'),$option,$task);
  283.                     return;
  284.                 else {
  285.                     fwrite($fp$OutBuffer);
  286.                     fclose($fp);
  287.                     HTML_dbadmin::showDbAdminMessage(sprintf(T_('Database backup successful! Your file was saved on the server in directory :<br />%s/%s</p>'),$local_backup_path,$filename),T_('DB Admin'),$option,$task);
  288.                     return;
  289.                 }
  290.             else {
  291.                 echo $OutBuffer;
  292.                 ob_end_flush();
  293.                 ob_start();
  294.                 // do no more
  295.                 exit();
  296.             }
  297.             break;
  298.         case "bzip" :
  299.             if (function_exists('bzcompress')) {
  300.                 if ($OutDest == "local"{
  301.                     $fp fopen("$local_backup_path/$filename""wb");
  302.                     if (!$fp{
  303.                         echo "<p align=\"center\"  class=\"error\">".sprintf(T_('Database backup FAILURE!!<br />File %s/%s not writable<br />Please contact your admin/webmaster!'),$local_backup_path,$filename)."</p>";
  304.                     else {
  305.                         fwrite($fpbzcompress($OutBuffer));
  306.                         fclose($fp);
  307.                         HTML_dbadmin::showDbAdminMessage(sprintf(T_('Database backup successful! Your file was saved on the server in directory :<br />%s/%s</p>'),$local_backup_path,$filename),T_('DB Admin')$option,$task);
  308.                         return;
  309.                     }
  310.                 else {
  311.                     echo bzcompress($OutBuffer);
  312.                     ob_end_flush();
  313.                     ob_start();
  314.                     // do no more
  315.                     exit();
  316.                 }
  317.             else {
  318.                 echo $OutBuffer;
  319.             }
  320.             break;
  321.         case "gzip" :
  322.             if (function_exists('gzencode')) {
  323.                 if ($OutDest == "local"{
  324.                     $fp gzopen("$local_backup_path/$filename""wb");
  325.                     if (!$fp{
  326.                         HTML_dbadmin::showDbAdminMessage(sprintf(T_('Database backup FAILURE!!<br />File %s/%s not writable<br />Please contact your admin/webmaster!</p>'),$local_backup_path,$filename),T_('DB Admin'),$option,$task);
  327.                         return;
  328.                     else {
  329.                         gzwrite($fp,$OutBuffer);
  330.                         gzclose($fp);
  331.                         HTML_dbadmin::showDbAdminMessage(sprintf(T_('Database backup successful! Your file was saved on the server in directory :<br />%s/%s</p>'),$local_backup_path,$filename),T_('DB Admin'),$option,$task);
  332.                         return;
  333.                     }
  334.                 else {
  335.                     echo gzencode($OutBuffer);
  336.                     ob_end_flush();
  337.                     ob_start();
  338.                     // do no more
  339.                     exit();
  340.                 }
  341.             else {
  342.                 echo $OutBuffer;
  343.             }
  344.             break;
  345.         case "zip" :
  346.             if (function_exists('gzcompress')) {
  347.                 include "classes/zip.lib.php";
  348.                 $zipfile new zipfile();
  349.                 $zipfile -> addFile($OutBuffer$filename ".sql");
  350.                 }
  351.             switch ($OutDest{
  352.                 case "local" :
  353.                     $fp fopen("$local_backup_path/$filename""wb");
  354.                     if (!$fp{
  355.                         HTML_dbadmin::showDbAdminMessage(sprintf(T_('Database backup FAILURE!!<br />File %s/%s not writable<br />Please contact your admin/webmaster!</p>'),$local_backup_path,$filename),T_('DB Admin'),$option,$task);
  356.                         return;
  357.                     else {
  358.                         fwrite($fp$zipfile->file());
  359.                         fclose($fp);
  360.                         HTML_dbadmin::showDbAdminMessage(sprintf(T_('Database backup successful! Your file was saved on the server in directory :<br />%s/%s</p>'),$local_backup_path,$filename),T_('DB Admin'),$option,$task);
  361.                         return;
  362.                     }
  363.                     break;
  364.                 case "remote" :
  365.                     echo $zipfile->file();
  366.                     ob_end_flush();
  367.                     ob_start();
  368.                     // do no more
  369.                     exit();
  370.                     break;
  371.                 default :
  372.                     echo $OutBuffer;
  373.                     break;
  374.             }
  375.             break;
  376.     }
  377. }
  378.  
  379. function dbRestore$local_backup_path{
  380.     global $database;
  381.  
  382.     $uploads_okay (function_exists('ini_get')) ((strtolower(ini_get('file_uploads')) == 'on' || ini_get('file_uploads'== 1&& intval(ini_get('upload_max_filesize'))) (intval(@get_cfg_var('upload_max_filesize')));
  383.     if ($uploads_okay)
  384.     {
  385.         $enctype " enctype=\"multipart/form-data\"";
  386.     }
  387.     else
  388.     {
  389.         $enctype '';
  390.     }
  391.  
  392.     HTML_dbadmin::restoreIntro($enctype,$uploads_okay,$local_backup_path);
  393. }
  394.  
  395. function doRestore$file$uploadedFile$local_backup_path {
  396.     global $database$option,$task,$mosConfig_absolute_path;
  397.  
  398.     if(!is_null($uploadedFile&& is_array($uploadedFile&& $uploadedFile["name"!= "")
  399.     {
  400.         $base_Dir $mosConfig_absolute_path "/uploadfiles/";
  401.         if (!move_uploaded_file($uploadedFile['tmp_name']$base_Dir $uploadedFile['name']))
  402.         {
  403.             HTML_dbadmin::showDbAdminMessage(T_('Error! could not move uploaded file.</p>'),T_('DB Admin - Restore'),$option,$task);
  404.             return false;
  405.         }
  406.  
  407.     }
  408.     if ((!$file&& (!$uploadedFile['name']))
  409.     {
  410.         HTML_dbadmin::showDbAdminMessage(T_('Error! No restore file specified.</p>'),T_('DB Admin - Restore'),$option,$task);
  411.         return;
  412.     }
  413.  
  414.     if ($file)
  415.     {
  416.         if (isset($local_backup_path))
  417.         {
  418.             $infile        $local_backup_path "/" $file;
  419.             $upfileFull    $file;
  420.             $destfile $mosConfig_absolute_path "/uploadfiles/$file";
  421.  
  422.             // If it's a zip file, we copy it so we can extract it
  423.             if(eregi(".\.zip$",$upfileFull))
  424.             {
  425.                 copy($infile,$destfile);
  426.             }
  427.         }
  428.         else
  429.         {
  430.             HTML_dbadmin::showDbAdminMessage(T_('Error! Backup path in your configuration file has not been configured.</p>'),T_('DB Admin - Restore'),$option,$task);
  431.             return;
  432.         }
  433.     }
  434.     else
  435.     {
  436.  
  437.         $upfileFull    $uploadedFile['name'];
  438.         $infile    $base_Dir $uploadedFile['name']
  439.         
  440.     }
  441.  
  442.     if (!eregi(".\.sql$",$upfileFull&& !eregi(".\.bz2$",$upfileFull&& !eregi(".\.gz$",$upfileFull&& !eregi(".\.zip$",$upfileFull))
  443.     {
  444.         HTML_dbadmin::showDbAdminMessage(sprintf(T_('Error! Invalid file extension in input file (%s).<br />Only *.sql, *.bz2, or *.gz files may be uploaded.</p>'),$upfileFull),T_('DB Admin - Restore'),$option,$task);
  445.         return;
  446.     }
  447.     
  448.     if (substr($upfileFull,-3)==".gz")
  449.     {
  450.         if (function_exists('gzinflate'))
  451.         {
  452.             $fp=fopen("$infile","rb");
  453.             if ((!$fp|| filesize("$infile")==0)
  454.             {
  455.                 HTML_dbadmin::showDbAdminMessage(sprintf(T_('Error! Unable to open input file (%s) for reading or file contains no records.</p>'),$infile),T_('DB Admin - Restore'),$option,$task);
  456.                 return;
  457.             }
  458.             else
  459.             {
  460.                 $content fread($fp,filesize("$infile"));
  461.                 fclose($fp);
  462.                 $content gzinflate(substr($content,10));
  463.             }
  464.         }
  465.         else
  466.         {
  467.             HTML_dbadmin::showDbAdminMessage(T_('Error! Unable to process gzip file as gzinflate function is unavailable.</p>'),T_('DB Admin - Restore'),$option,$task);
  468.             return;
  469.         }
  470.     }
  471.     elseif (substr($upfileFull,-4)==".bz2")
  472.     {
  473.         if (function_exists('bzdecompress'))
  474.         {
  475.             $fp=fopen("$infile","rb");
  476.             if ((!$fp|| filesize("$infile")==0)
  477.             {
  478.                 HTML_dbadmin::showDbAdminMessage(sprintf(T_('Error! Unable to open input file (%s) for reading or file contains no records.</p>'),$infile),T_('DB Admin - Restore'),$option,$task);
  479.                 return;
  480.             }
  481.             else
  482.             {
  483.                 $content=fread($fp,filesize("$infile"));
  484.                 fclose($fp);
  485.                 $content=bzdecompress($content);
  486.             }
  487.         }
  488.         else
  489.         {
  490.             HTML_dbadmin::showDbAdminMessage(T_('Error! Unable to process bzip file as bzdecompress function is unavailable.</p>'),T_('DB Admin - Restore'),$option,$task);
  491.             return;
  492.         }
  493.     }
  494.     elseif (substr($upfileFull,-4)==".sql")
  495.     {
  496. echo T_('trying to access').' '.$infile;
  497.         $fp=fopen("$infile","r");
  498.         if ((!$fp|| filesize("$infile")==0)
  499.         {
  500.             HTML_dbadmin::showDbAdminMessage(sprintf(T_('Error! Unable to open input file (%s) for reading or file contains no records.</p>'),$infile),T_('DB Admin - Restore'),$option,$task);
  501.             return;
  502.         }
  503.         else
  504.         {
  505.             $content=fread($fp,filesize("$infile"));
  506.             fclose($fp);
  507.         }
  508.     }
  509.     elseif (substr($upfileFull,-4)==".zip")
  510.     {
  511.         // unzip the file
  512.         $base_Dir        $mosConfig_absolute_path "/uploadfiles/";
  513.         $archivename    $base_Dir $upfileFull;
  514.         $tmpdir            uniqid("dbrestore_");
  515.  
  516.         $isWindows (substr(PHP_OS03== 'WIN' && stristr $_SERVER["SERVER_SOFTWARE"]"microsoft"));
  517.         if($isWindows)
  518.         {
  519.             $extractdir    str_replace('/','\\',$base_Dir "$tmpdir/");
  520.             $archivename str_replace('/','\\',$archivename);
  521.         }
  522.         else
  523.         {
  524.             $extractdir    str_replace('\\','/',$base_Dir "$tmpdir/");
  525.             $archivename str_replace('\\','/',$archivename);
  526.         }
  527.  
  528.         $zipfile    new PclZip($archivename);
  529.         if($isWindows)
  530.             define('OS_WINDOWS',1);
  531.  
  532.         $ret $zipfile->extract(PCLZIP_OPT_PATH,$extractdir);
  533.         if($ret == 0)
  534.         {
  535.             HTML_dbadmin::showDbAdminMessage(sprintf(T_('Unrecoverable error \'%s\''),$zipfile->errorName(true)),T-('DB Admin - Restore'),$option,$task);
  536.             return false;
  537.         }
  538.         $filesinzip $zipfile->listContent();
  539.         if(is_array($filesinzip&& count($filesinzip0)
  540.         {
  541.             $fp            fopen($extractdir $filesinzip[0]["filename"],"r");
  542.             $content    fread($fp,filesize($extractdir $filesinzip[0]["filename"]));
  543.             fclose($fp);
  544.  
  545.             // Cleanup temp extract dir
  546.             deldir($extractdir);
  547.             //unlink($mosConfig_absolute_path . "uploadfiles/$file");
  548.  
  549.         }
  550.         else
  551.         {
  552.             HTML_dbadmin::showDbAdminMessage(sprintf(T_('No SQL file found in %s'),$upfileFull),T_('DB Admin - Restore'),$option,$task);
  553.             return;
  554.         }
  555.     }
  556.     else
  557.     {
  558.         HTML_dbadmin::showDbAdminMessage(sprintf(T_('Error! Unrecognized input file type. (%s : %s)</p>'),$infile,$upfileFull),T_('DB Admin - Restore'),$option,$task);
  559.         return;
  560.     }
  561.  
  562.  
  563.     $decodedIn    explode(chr(10),$content);
  564.     $decodedOut    "";
  565.     $queries    0;
  566.  
  567.     foreach ($decodedIn as $rawdata)
  568.     {
  569.         $rawdata=trim($rawdata);
  570.         if (($rawdata!=""&& ($rawdata{0}!="#"))
  571.         {
  572.             $decodedOut .= $rawdata;
  573.             if (substr($rawdata,-1)==";")
  574.             {
  575.                 if  ((substr($rawdata,-2)==");"|| (strtoupper(substr($decodedOut,0,6))!="INSERT"))
  576.                 {
  577.                     if (eregi('^(DROP|CREATE)[[:space:]]+(IF EXISTS[[:space:]]+)?(DATABASE)[[:space:]]+(.+)'$decodedOut))
  578.                     {
  579.                         HTML_dbadmin::showDbAdminMessage(T_('Error! Your input file contains a DROP or CREATE DATABASE statement. Please delete these statements before trying to restore the file.</p>'),T_('DB Admin - Restore'),$option,$task);
  580.                         return;
  581.                     }
  582.                     $database->setQuery($decodedOut);
  583.                     $database->query();
  584.                     $decodedOut="";
  585.                     $queries++;
  586.                 }
  587.             }
  588.         }
  589.     }
  590.     HTML_dbadmin::showDbAdminMessage(sprintf(T_('Success! Database has been restored to the backup you requested (%d SQL queries processed).</p>'),$queries),T_('DB Admin - Restore'),$option,$task);
  591.     return;
  592. }
  593.  
  594. function deldir($dir)
  595. {
  596.     $current_dir opendir($dir);
  597.     while($entryname readdir($current_dir))
  598.     {
  599.         if(is_dir("$dir/$entryname"and ($entryname != "." and $entryname!=".."))
  600.         {
  601.             deldir("${dir}/${entryname}");
  602.         }
  603.         elseif($entryname != "." and $entryname!="..")
  604.         {
  605.             unlink("${dir}/${entryname}");
  606.         }
  607.     }
  608.     closedir($current_dir);
  609.     rmdir($dir);
  610. }
  611.  
  612. function xquery$option {
  613.     global $database;
  614.  
  615.     $rows null;
  616.     $msg '';
  617.     $sql trimmosGetParam$_POST'sql''' ) );
  618.     $batch intvalmosGetParam$_POST'batch') );
  619.  
  620.     $allowed array"CREATE""SELECT""INSERT""UPDATE""DROP""ALTER" );
  621.     $words preg_split"/\s+/"$sql );
  622.     $cmd strtoupper$words[0);
  623.  
  624.     if ($sql == ""{
  625.         $msg T_('The query was empty.');
  626.     else if (!in_array$cmd$allowed)) {
  627.         $msg sprintf(T_('You are not permitted to execute a <strong>%s</strong> query'),$cmd);
  628.     else {
  629.         $database->setQuery$sql );
  630.         if ($batch{
  631.             // run batch, don't abort on error
  632.             $r $database->query_batchfalse );
  633.         else {
  634.             $r $database->query();
  635.         }
  636.         if ($r{
  637.             $msg T_('The query executed successfully.');
  638.             $msg .= sprintf(T_('<br />%d rows where affected.'),intval$database->getNumRows() ));
  639.  
  640.             if ($cmd == "SELECT"{
  641.                 $rows $database->loadObjectList();
  642.             }
  643.         else {
  644.             $msg sprintf(T_('The query was unsuccessful.  It return the error code %d'),$database->getErrorNum());
  645.             $msg .= "<br />" $database->getErrorMsg("";
  646.         }
  647.     }
  648.  
  649.     HTML_dbadmin::xquery$sql$msg$rows$option );
  650. }
  651. ?>

Documentation generated on Mon, 05 May 2008 16:15:53 +0400 by phpDocumentor 1.4.0