1 | <?php |
---|
2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
3 | |
---|
4 | load_language('plugin.lang', DB_BACKUP_PATH); |
---|
5 | $chemin=DB_BACKUP_PATH . "backups"; |
---|
6 | global $template; |
---|
7 | |
---|
8 | $template->set_filenames( array('db_backup_admin_content' => dirname(__FILE__).'/db_backup_admin.tpl') ); |
---|
9 | |
---|
10 | function get_structure($db,$prefixe,$wholedb,$onlyHistory,$excludeHistory) { |
---|
11 | $a=explode(";",list_tables($db,$prefixe,$wholedb,$onlyHistory,$excludeHistory)); |
---|
12 | foreach ($a as $table) { $SQL[]=segment_backup($table);} |
---|
13 | return implode("\r", $SQL); |
---|
14 | } |
---|
15 | |
---|
16 | function list_tables($db,$prefixe,$wholedb,$onlyHistory,$excludeHistory) { |
---|
17 | $tables = mysql_list_tables($db); |
---|
18 | $result=""; |
---|
19 | while ($td = mysql_fetch_array($tables)) { |
---|
20 | $table = $td[0]; |
---|
21 | if (!( (!$wholedb and strpos($table, $prefixe) !== 0) or ($onlyHistory and strpos($table, $prefixe . 'history') !== 0) or ($excludeHistory and strpos($table, $prefixe . 'history') === 0) )) { |
---|
22 | $result.=$table.";"; |
---|
23 | } |
---|
24 | } |
---|
25 | return substr($result,0,-1); |
---|
26 | } |
---|
27 | |
---|
28 | function segment_backup($table) { |
---|
29 | $r = mysql_query("SHOW CREATE TABLE `$table`"); |
---|
30 | if ($r) { |
---|
31 | $insert_sql = ""; |
---|
32 | $d = mysql_fetch_array($r); |
---|
33 | $d[1] .= ";"; |
---|
34 | $SQL[] = str_replace("\n", "", $d[1]); |
---|
35 | $table_query = mysql_query("SELECT * FROM `$table`"); |
---|
36 | $num_fields = mysql_num_fields($table_query); |
---|
37 | while ($fetch_row = mysql_fetch_array($table_query)) { |
---|
38 | $insert_sql .= "INSERT INTO $table VALUES("; |
---|
39 | for ($n=1;$n<=$num_fields;$n++) { |
---|
40 | $m = $n - 1; |
---|
41 | $insert_sql .= "'".mysql_real_escape_string($fetch_row[$m])."', "; |
---|
42 | } |
---|
43 | $insert_sql = substr($insert_sql,0,-2); |
---|
44 | $insert_sql .= ");\n"; |
---|
45 | } |
---|
46 | if ($insert_sql!= "\r") { |
---|
47 | $SQL[] = $insert_sql; |
---|
48 | } |
---|
49 | } |
---|
50 | return implode("\r", $SQL); |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | |
---|
55 | if (isset($_POST['submit'])) { |
---|
56 | |
---|
57 | // ******************************************************************************************* |
---|
58 | if (($_POST['submit']==l10n('db_backup_erase'))&&(isset($_POST['db_backupfiles']))) { |
---|
59 | unlink ($chemin.'/'.$_POST['db_backupfiles']); |
---|
60 | array_push($page['infos'],l10n('db_backup_file').' "'.date('Y-m-d-H-i-s').'.sql" '.l10n('db_backup_erase_message')); |
---|
61 | } |
---|
62 | // ******************************************************************************************* |
---|
63 | if (($_POST['submit']==l10n('db_backup_download'))&&(isset($_POST['db_backupfiles']))) { |
---|
64 | $File_url=$_POST['db_backupfiles']; |
---|
65 | header("Content-disposition: attachment; filename=$File_url"); |
---|
66 | header("Content-Type: application/force-download"); |
---|
67 | header("Content-Transfer-Encoding: application/octet-stream\n"); |
---|
68 | header("Content-Length: ".filesize($chemin .'/'. $File_url)); |
---|
69 | header("Pragma: no-cache"); |
---|
70 | header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public"); |
---|
71 | header("Expires: 0"); |
---|
72 | readfile($chemin .'/'. $File_url); |
---|
73 | } |
---|
74 | // ******************************************************************************************* |
---|
75 | if ($_POST['submit']==l10n('db_backup_dump')) { |
---|
76 | $wholeDb = isset($_POST['wholeDb']) ? true : false; |
---|
77 | $onlyHistory = isset($_POST['onlyHistory']) ? true : false; |
---|
78 | $excludeHistory = isset($_POST['excludeHistory']) ? true : false; |
---|
79 | $filename =$wholeDb ? $cfgBase."-" : "piwigo-"; |
---|
80 | $filename.=$onlyHistory ? "history-" : ""; |
---|
81 | $filename.=$excludeHistory ? "no-history-" : ""; |
---|
82 | $filename.=date('Y-m-d-H-i-s').".sql"; |
---|
83 | |
---|
84 | // step by step process initialisation |
---|
85 | if (($_POST['submit'])&&(isset($_POST['step_by_step']))&&!(isset($_POST['tables']))) { |
---|
86 | if (isset($_POST['saveToserver'])) {$template->assign('DB_BACKUP_SAVING_STEP','checked');} |
---|
87 | $template->assign('DB_BACKUP_PROCESS',list_tables($cfgBase,$prefixeTable,$wholeDb,$onlyHistory,$excludeHistory)); |
---|
88 | $template->assign('DB_BACKUP_TABLE_QUANTITY',sizeof(explode(";",list_tables($cfgBase,$prefixeTable,$wholeDb,$onlyHistory,$excludeHistory)))); |
---|
89 | $template->assign('DB_BACKUP_TABLE_CURRENT',"0"); |
---|
90 | $template->assign('DB_BACKUP_TABLE_PERCENT',"0"); |
---|
91 | $template->assign('DB_BACKUP_FILENAME',$filename); |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | $result = get_structure($cfgBase,$prefixeTable,$wholeDb,$onlyHistory,$excludeHistory); |
---|
96 | $template->assign('DB_BACKUP_SQL_OUTPUT', htmlspecialchars($result)); |
---|
97 | if (isset($_POST['saveToserver'])) { |
---|
98 | $file=fopen($chemin."/".$filename,'x+'); |
---|
99 | fwrite($file, $result); |
---|
100 | fclose($file); |
---|
101 | array_push($page['infos'],l10n('db_backup_file').' "'.$filename.'" '.l10n('db_backup_dump_message')); |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | // step by step process start |
---|
108 | if ((isset($_POST['step_by_step']))&&(isset($_POST['tables']))) { |
---|
109 | |
---|
110 | $tables=explode(";",$_POST['tables']); |
---|
111 | $filename=$_POST['filename']; |
---|
112 | $template->assign('DB_BACKUP_FILENAME',$filename); |
---|
113 | $table_quantity = isset($_POST['table_quantity']) ? $_POST['table_quantity'] : sizeof($tables); |
---|
114 | $template->assign('DB_BACKUP_TABLE_QUANTITY',$table_quantity); |
---|
115 | $template->assign('DB_BACKUP_TABLE_CURRENT',$_POST['table_current']+1); |
---|
116 | $template->assign('DB_BACKUP_TABLE_PERCENT',floor((($_POST['table_current']+1)*100)/$table_quantity)); |
---|
117 | |
---|
118 | // step by step process end |
---|
119 | if ($_POST['table_current']+1==$table_quantity){ |
---|
120 | if (isset($_POST['saveToserver'])) { |
---|
121 | array_push($page['infos'],l10n('db_backup_file').' "'.$filename.'" '.l10n('db_backup_dump_message')); |
---|
122 | } |
---|
123 | $segment=segment_backup($tables[0]); |
---|
124 | $template->assign('DB_BACKUP_SQL_OUTPUT',stripslashes($_POST['table_segment']).$segment); |
---|
125 | } |
---|
126 | // continue step by step process |
---|
127 | else { |
---|
128 | $toprocess=array_shift($tables); |
---|
129 | $template->assign('DB_BACKUP_PROCESS',implode(";",$tables)); |
---|
130 | $segment=segment_backup($toprocess); |
---|
131 | $template->assign('DB_BACKUP_TABLE_SEGMENT',stripslashes($_POST['table_segment']).$segment); |
---|
132 | } |
---|
133 | |
---|
134 | if (isset($_POST['saveToserver'])) { |
---|
135 | $template->assign('DB_BACKUP_SAVING_STEP','checked'); |
---|
136 | $file=fopen($chemin."/".$filename,'a'); |
---|
137 | fwrite($file,$segment); |
---|
138 | fclose($file); |
---|
139 | } |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | // listing the backup files |
---|
146 | $backupfilesid=array(); |
---|
147 | $dossier = opendir($chemin); |
---|
148 | while ($Fichier = readdir($dossier)) { |
---|
149 | if ($Fichier != "." && $Fichier != ".." && $Fichier != "index.php" && $Fichier != ".htaccess") { |
---|
150 | $nomFichier = $chemin."/".$Fichier; |
---|
151 | array_push($backupfilesid,$Fichier); |
---|
152 | } |
---|
153 | } |
---|
154 | closedir($dossier); |
---|
155 | $template->assign('db_backupfilesid', $backupfilesid); |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | $template->assign_var_from_handle( 'ADMIN_CONTENT', 'db_backup_admin_content'); |
---|
160 | ?> |
---|