optimizer_dbbackup_clean.php
2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: optimizer_dbbackup_clean.php 31344 2012-08-15 04:01:32Z zhangjie $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class optimizer_dbbackup_clean {
public function __construct() {
}
public function check() {
global $_G;
$exportlog = $exportsize = $exportziplog = array();
$filecount = 0;
$this->check_exportfile($exportlog, $exportziplog, $exportsize, $filecount);
if(!$filecount) {
$return = array('status' => 0, 'type' => 'none', 'lang' => lang('optimizer', 'optimizer_dbbackup_clean_safe'));
} else {
$return = array('status' => 1, 'type' => 'notice', 'lang' => lang('optimizer', 'optimizer_dbbackup_clean_delete', array('filecount' => $filecount)));
}
return $return;
}
function get_backup_dir() {
$backupdirs = array();
$dir = dir(DISCUZ_ROOT.'./data');
while(($file = $dir->read()) !== FALSE) {
if(filetype(DISCUZ_ROOT.'./data/'.$file) == 'dir' && preg_match('/^backup_\w+/', $file)) {
$backupdirs[] = $file;
}
}
$dir->close();
return $backupdirs;
}
function check_exportfile(&$exportlog, &$exportziplog, &$exportsize, &$filecount) {
$backupdirs = $this->get_backup_dir();
if(empty($backupdirs)) {
return;
}
$filecount = 0;
foreach($backupdirs as $backupdir) {
$dir = dir(DISCUZ_ROOT.'./data/'.$backupdir);
while($entry = $dir->read()) {
$entry = './data/'.$backupdir.'/'.$entry;
if(is_file($entry)) {
if(preg_match("/\.sql$/i", $entry)) {
$filesize = filesize($entry);
$fp = fopen($entry, 'rb');
$identify = explode(',', base64_decode(preg_replace("/^# Identify:\s*(\w+).*/s", "\\1", fgets($fp, 256))));
fclose($fp);
$key = preg_replace('/^(.+?)(\-\d+)\.sql$/i', '\\1', basename($entry));
$exportlog[$key][$identify[4]] = array(
'version' => $identify[1],
'type' => $identify[2],
'method' => $identify[3],
'volume' => $identify[4],
'tablepre' => $identify[5],
'dbcharset' => $identify[6],
'filename' => $entry,
'dateline' => filemtime($entry),
'size' => $filesize
);
$filecount++;
$exportsize[$key] += $filesize;
} elseif(preg_match("/\.zip$/i", $entry)) {
$key = preg_replace('/^(.+?)(\-\d+)\.zip$/i', '\\1', basename($entry));
$filesize = filesize($entry);
$exportziplog[$key][] = array(
'type' => 'zip',
'filename' => $entry,
'size' => filesize($entry),
'dateline' => filemtime($entry)
);
$filecount++;
}
}
}
$dir->close();
}
}
}
?>