forked from poppyred/1.6.x
/
exec.mysql.emptydb.php
executable file
·185 lines (153 loc) · 6.24 KB
/
exec.mysql.emptydb.php
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
$GLOBALS["FORCE"]=false;$GLOBALS["REINSTALL"]=false;
$GLOBALS["NO_HTTPD_CONF"]=false;
$GLOBALS["NO_HTTPD_RELOAD"]=false;
if(is_array($argv)){
if(preg_match("#--verbose#",implode(" ",$argv))){$GLOBALS["VERBOSE"]=true;}
if(preg_match("#--force#",implode(" ",$argv))){$GLOBALS["FORCE"]=true;}
if(preg_match("#--reinstall#",implode(" ",$argv))){$GLOBALS["REINSTALL"]=true;}
if(preg_match("#--no-httpd-conf#",implode(" ",$argv))){$GLOBALS["NO_HTTPD_CONF"]=true;}
if(preg_match("#--noreload#",implode(" ",$argv))){$GLOBALS["NO_HTTPD_RELOAD"]=true;}
if($GLOBALS["VERBOSE"]){ini_set('html_errors',0);ini_set('display_errors', 1);ini_set('error_reporting', E_ALL);}
}
if(posix_getuid()<>0){die("Cannot be used in web server mode\n\n");}
$GLOBALS["posix_getuid"]=0;
include_once(dirname(__FILE__) . '/ressources/class.users.menus.inc');
include_once(dirname(__FILE__) . '/ressources/class.mysql.inc');
include_once(dirname(__FILE__) . '/ressources/class.user.inc');
include_once(dirname(__FILE__) . '/ressources/class.ini.inc');
include_once(dirname(__FILE__) . '/ressources/class.mysql.inc');
include_once(dirname(__FILE__) . '/ressources/class.ldap.inc');
include_once(dirname(__FILE__) . '/framework/class.unix.inc');
include_once(dirname(__FILE__) . '/framework/frame.class.inc');
include_once(dirname(__FILE__) . '/framework/class.settings.inc');
if($argv[1]=="--db"){empty_db($argv[2]);}
function empty_db($database){
$GLOBALS["LOGSDB"]=$database;
$logFile="/usr/share/artica-postfix/ressources/logs/web/empty-{$GLOBALS["LOGSDB"]}.txt";
@unlink($logFile);
$unix=new unix();
$pidfile="/etc/artica-postfix/pids/".basename(__FILE__).".".__FUNCTION__.".pid";
$pid=@file_get_contents($pidfile);
if($unix->process_exists($pid)){
ouputz("already running PID:$pid",__LINE__);
return;
}
$t=time();
@file_put_contents($pidfile,getmypid());
ouputz("Instanciate library...",__LINE__);
$q=new mysql();
if($database=="syslogstore"){
ouputz("Please wait, Cleaning table store...",__LINE__);
$q->QUERY_SQL("TRUNCATE TABLE `store`","syslogstore");
if(!$q->ok){
ouputz("Error, $q->mysql_error",__LINE__);
if(preg_match("#Can't create.+?write to file#", $q->mysql_error)){
ouputz("Please wait, removing store table from disk...",__LINE__);
remove_disk_table("store",$database);
}
return;
}
ouputz("Cleaning syslogstore done took ".$unix->distanceOfTimeInWords($t,time(),true),__LINE__);
@unlink("/usr/share/artica-postfix/ressources/logs/web/squidlogs.stats");
return;
}
if($database=="squidlogs"){
emptysquidlogs();
ouputz("Cleaning squidlogs done took ".$unix->distanceOfTimeInWords($t,time(),true),__LINE__);
@unlink("/usr/share/artica-postfix/ressources/logs/web/squidlogs.stats");
return;
}
}
function remove_disk_table_all($table,$database){
$sock=new sockets();
$unix=new unix();
$MYSQL_DATA_DIR=$sock->GET_INFO("ChangeMysqlDir");
if($MYSQL_DATA_DIR==null){$MYSQL_DATA_DIR="/var/lib/mysql";}
ouputz("Remove $MYSQL_DATA_DIR/$database/$table.(*)",__LINE__);
$t=time();
$rm=$unix->find_program("rm");
shell_exec("$rm -f $MYSQL_DATA_DIR/$database/$table.*");
}
function remove_disk_table($table,$database){
$unix=new unix();
$sock=new sockets();
$MYSQL_DATA_DIR=$sock->GET_INFO("ChangeMysqlDir");
if($MYSQL_DATA_DIR==null){$MYSQL_DATA_DIR="/var/lib/mysql";}
ouputz("Remove $MYSQL_DATA_DIR/$database/$table.MYD(*)",__LINE__);
$t=time();
$rm=$unix->find_program("rm");
$touch=$unix->find_program("touch");
shell_exec("$rm -f $MYSQL_DATA_DIR/$database/$table.MYD");
shell_exec("$rm -f $MYSQL_DATA_DIR/$database/$table.MYD*");
ouputz("Cleaning $database/$table done took ".$unix->distanceOfTimeInWords($t,time(),true),__LINE__);
shell_exec("$touch $MYSQL_DATA_DIR/$database/$table.MYD");
@chmod("$MYSQL_DATA_DIR/$database/$table.MYD", 0755);
@chown("$MYSQL_DATA_DIR/$database/$table.MYD", "mysql");
@chgrp("$MYSQL_DATA_DIR/$database/$table.MYD", "mysql");
@unlink("/usr/share/artica-postfix/ressources/logs/web/squidlogs.stats");
}
function emptysquidlogs(){
$unix=new unix();
$sock=new sockets();
$q=new mysql();
$MYSQL_DATA_DIR=$sock->GET_INFO("ChangeMysqlDir");
if($MYSQL_DATA_DIR==null){$MYSQL_DATA_DIR="/var/lib/mysql";}
$dir="$MYSQL_DATA_DIR/squidlogs";
$TABLESDELETE["searchwords_*"]=true;
$TABLESDELETE["UserSizeD_*"]=true;
$TABLESDELETE["youtubeday_*"]=true;
$TABLESDELETE["*_week"]=true;
$TABLESDELETE["*_blocked_week"]=true;
$TABLESDELETE["*_members"]=true;
$TABLESDELETE["*_hour"]=true;
$TABLESDELETE["*_visited"]=true;
$TABLESDELETE["dansguardian_events_*"]=true;
$TABLESDELETE["squidhour_*"]=true;
$TABLESDELETE["visited_sites"]=true;
$TABLESDELETE["UserAuth*"]=true;
$TABLESDELETE["tables_day"]=true;
$TABLESDELETE["adansguardian_events_*"]=true;
$TABLESDELETE["*_day"]=true;
$TABLESDELETE["youtubeweek_*"]=true;
ouputz("Remove ".count($TABLESDELETE)." family tables in $MYSQL_DATA_DIR/squidlogs",__LINE__);
while (list ($pattern, $line) = each ($TABLESDELETE)){
foreach (glob("$dir/$pattern.MYD") as $filename) {
$table=str_replace(".MYD", "", basename($filename));
$table=str_replace(".frm", "", $table);
if(preg_match("#(.+?)\/(.+?)\.[A-Za-Z]+$#", $table,$re)){$table=$re[1];}
ouputz("Removing $table....",__LINE__);
$q->QUERY_SQL("DROP TABLE `$table`","squidlogs");
if(!$q->ok){
ouputz("Error, $q->mysql_error",__LINE__);
if(preg_match("#Unknown table#i", $q->mysql_error)){
remove_disk_table_all($table,"squidlogs");
continue;
}
remove_disk_table($table,"squidlogs");
$q->QUERY_SQL("DROP TABLE `$table`","squidlogs");
if(!$q->ok){ouputz("Error, $q->mysql_error",__LINE__);}
}
}
}
foreach (glob("$dir/*.BAK") as $filename) {
ouputz("Removing backup file ". basename($filename)."....",__LINE__);
@unlink($filename);
}
}
function ouputz($text,$line){
if(!isset($GLOBALS["CLASS_UNIX"])){$GLOBALS["CLASS_UNIX"]=new unix();}
//if($GLOBALS["VERBOSE"]){echo "$text\n";}
$pid=@getmypid();
$date=@date("H:i:s");
$logFile="/usr/share/artica-postfix/ressources/logs/web/empty-{$GLOBALS["LOGSDB"]}.txt";
if($GLOBALS["VERBOSE"]){echo "$logFile\n";}
if(is_file($logFile)){
$size=@filesize($logFile);
if($size>1000000){@unlink($logFile);}
}
$f = fopen($logFile, 'a');
fwrite($f, "$date [$pid][$line]: $text\n");
fclose($f);
chmod($logFile, 0777);
}