set_filenames( array('db_backup_admin_content' => dirname(__FILE__).'/db_backup_admin.tpl') ); function get_structure($db,$prefixe,$wholedb,$onlyHistory,$excludeHistory) { $a=explode(";",list_tables($db,$prefixe,$wholedb,$onlyHistory,$excludeHistory)); foreach ($a as $table) { $SQL[]=segment_backup($table);} return implode("\r", $SQL); } function list_tables($db,$prefixe,$wholedb,$onlyHistory,$excludeHistory) { $tables = mysql_list_tables($db); $result=""; while ($td = mysql_fetch_array($tables)) { $table = $td[0]; if (!( (!$wholedb and strpos($table, $prefixe) !== 0) or ($onlyHistory and strpos($table, $prefixe . 'history') !== 0) or ($excludeHistory and strpos($table, $prefixe . 'history') === 0) )) { $result.=$table.";"; } } return substr($result,0,-1); } function segment_backup($table) { $r = mysql_query("SHOW CREATE TABLE `$table`"); if ($r) { $insert_sql = ""; $d = mysql_fetch_array($r); $d[1] .= ";"; $SQL[] = str_replace("\n", "", $d[1]); $table_query = mysql_query("SELECT * FROM `$table`"); $num_fields = mysql_num_fields($table_query); while ($fetch_row = mysql_fetch_array($table_query)) { $insert_sql .= "INSERT INTO $table VALUES("; for ($n=1;$n<=$num_fields;$n++) { $m = $n - 1; $insert_sql .= "'".mysql_real_escape_string($fetch_row[$m])."', "; } $insert_sql = substr($insert_sql,0,-2); $insert_sql .= ");\n"; } if ($insert_sql!= "\r") { $SQL[] = $insert_sql; } } return implode("\r", $SQL); } if (isset($_POST['submit'])) { // ******************************************************************************************* if (($_POST['submit']==l10n('db_backup_erase'))&&(isset($_POST['db_backupfiles']))) { unlink ($chemin.'/'.$_POST['db_backupfiles']); array_push($page['infos'],l10n('db_backup_file').' "'.date('Y-m-d-H-i-s').'.sql" '.l10n('db_backup_erase_message')); } // ******************************************************************************************* if (($_POST['submit']==l10n('db_backup_download'))&&(isset($_POST['db_backupfiles']))) { $File_url=$_POST['db_backupfiles']; header("Content-disposition: attachment; filename=$File_url"); header("Content-Type: application/force-download"); header("Content-Transfer-Encoding: application/octet-stream\n"); header("Content-Length: ".filesize($chemin .'/'. $File_url)); header("Pragma: no-cache"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public"); header("Expires: 0"); readfile($chemin .'/'. $File_url); } // ******************************************************************************************* if ($_POST['submit']==l10n('db_backup_dump')) { $wholeDb = isset($_POST['wholeDb']) ? true : false; $onlyHistory = isset($_POST['onlyHistory']) ? true : false; $excludeHistory = isset($_POST['excludeHistory']) ? true : false; $filename =$wholeDb ? $cfgBase."-" : "piwigo-"; $filename.=$onlyHistory ? "history-" : ""; $filename.=$excludeHistory ? "no-history-" : ""; $filename.=date('Y-m-d-H-i-s').".sql"; // step by step process initialisation if (($_POST['submit'])&&(isset($_POST['step_by_step']))&&!(isset($_POST['tables']))) { if (isset($_POST['saveToserver'])) {$template->assign('DB_BACKUP_SAVING_STEP','checked');} $template->assign('DB_BACKUP_PROCESS',list_tables($cfgBase,$prefixeTable,$wholeDb,$onlyHistory,$excludeHistory)); $template->assign('DB_BACKUP_TABLE_QUANTITY',sizeof(explode(";",list_tables($cfgBase,$prefixeTable,$wholeDb,$onlyHistory,$excludeHistory)))); $template->assign('DB_BACKUP_TABLE_CURRENT',"0"); $template->assign('DB_BACKUP_TABLE_PERCENT',"0"); $template->assign('DB_BACKUP_FILENAME',$filename); } else { $result = get_structure($cfgBase,$prefixeTable,$wholeDb,$onlyHistory,$excludeHistory); $template->assign('DB_BACKUP_SQL_OUTPUT', htmlspecialchars($result)); if (isset($_POST['saveToserver'])) { $file=fopen($chemin."/".$filename,'x+'); fwrite($file, $result); fclose($file); array_push($page['infos'],l10n('db_backup_file').' "'.$filename.'" '.l10n('db_backup_dump_message')); } } } } // step by step process start if ((isset($_POST['step_by_step']))&&(isset($_POST['tables']))) { $tables=explode(";",$_POST['tables']); $filename=$_POST['filename']; $template->assign('DB_BACKUP_FILENAME',$filename); $table_quantity = isset($_POST['table_quantity']) ? $_POST['table_quantity'] : sizeof($tables); $template->assign('DB_BACKUP_TABLE_QUANTITY',$table_quantity); $template->assign('DB_BACKUP_TABLE_CURRENT',$_POST['table_current']+1); $template->assign('DB_BACKUP_TABLE_PERCENT',floor((($_POST['table_current']+1)*100)/$table_quantity)); // step by step process end if ($_POST['table_current']+1==$table_quantity){ if (isset($_POST['saveToserver'])) { array_push($page['infos'],l10n('db_backup_file').' "'.$filename.'" '.l10n('db_backup_dump_message')); } $segment=segment_backup($tables[0]); $template->assign('DB_BACKUP_SQL_OUTPUT',stripslashes($_POST['table_segment']).$segment); } // continue step by step process else { $toprocess=array_shift($tables); $template->assign('DB_BACKUP_PROCESS',implode(";",$tables)); $segment=segment_backup($toprocess); $template->assign('DB_BACKUP_TABLE_SEGMENT',stripslashes($_POST['table_segment']).$segment); } if (isset($_POST['saveToserver'])) { $template->assign('DB_BACKUP_SAVING_STEP','checked'); $file=fopen($chemin."/".$filename,'a'); fwrite($file,$segment); fclose($file); } } // listing the backup files $backupfilesid=array(); $dossier = opendir($chemin); while ($Fichier = readdir($dossier)) { if ($Fichier != "." && $Fichier != ".." && $Fichier != "index.php" && $Fichier != ".htaccess") { $nomFichier = $chemin."/".$Fichier; array_push($backupfilesid,$Fichier); } } closedir($dossier); $template->assign('db_backupfilesid', $backupfilesid); $template->assign_var_from_handle( 'ADMIN_CONTENT', 'db_backup_admin_content'); ?>