forked from poppyred/1.6.x
/
exec.squidguard-tail.php
executable file
·118 lines (93 loc) · 3.71 KB
/
exec.squidguard-tail.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
<?php
include_once(dirname(__FILE__).'/ressources/class.templates.inc');
include_once(dirname(__FILE__).'/framework/frame.class.inc');
if(posix_getuid()<>0){die("Cannot be used in web server mode\n\n");}
if(!Build_pid_func(__FILE__,"MAIN")){
events("Already executed.. aborting the process");
die();
}
if($argv[1]=='--date'){echo date("Y-m-d H:i:s")."\n";}
$pid=getmypid();
$pidfile="/etc/artica-postfix/".basename(__FILE__).".pid";
@mkdir("/var/log/artica-postfix/squid-stats",0666,true);
events("running $pid ");
file_put_contents($pidfile,$pid);
$pipe = fopen("php://stdin", "r");
while(!feof($pipe)){
$buffer .= fgets($pipe, 4096);
Parseline($buffer);
$buffer=null;
}
fclose($pipe);
events("Shutdown...");
die();
function Parseline($buffer){
$buffer=trim($buffer);
if($buffer==null){return null;}
if(strpos($buffer,"init urllist")>0){return ;}
if(strpos($buffer,"init expressionlist")>0){return ;}
if(strpos($buffer,"init domainlist")>0){return ;}
if(preg_match('#INFO: loading dbfile (.+)#',$buffer,$re)){
events("LOADING $re[1]");
$GLOBALS[__FILE__]["DBFILE"]=trim($re[1]);
return null;
}
if(preg_match("#FATAL: Error db_open: Unknown error#",$buffer,$re)){
events("ERROR ON {$GLOBALS[__FILE__]["DBFILE"]} : $buffer");
if(basename($GLOBALS[__FILE__]["DBFILE"])=="urls.db"){
events("urls.db -> create ".dirname($GLOBALS[__FILE__]["DBFILE"])."/urls it and recompile it");
@file_put_contents(dirname($GLOBALS[__FILE__]["DBFILE"])."/urls","www.". md5(time()).".bv");
}
$file="/etc/artica-postfix/croned.1/squidguard.". md5($GLOBALS[__FILE__]["DBFILE"]).".error";
if(IfFileTime($file)){
$cmd=LOCATE_PHP5_BIN2()." /usr/share/artica-postfix/exec.squidguard.php --compile-single \"{$GLOBALS[__FILE__]["DBFILE"]}\" &";
events("$cmd");
shell_exec($cmd);
WriteFileCache($file);
}
return null;
}
if(preg_match("#\]\s+(.+?):\s+Cannot allocate memory#",$buffer,$re)){
events("ERROR ON {$re[1]} : Cannot allocate memory -> create it");
@file_put_contents($re[1],"www.". md5(time()).".bv");
squid_admin_mysql(1, "Web filtering: Cannot allocate memory","$buffer");
return null;
}
if(preg_match("#\]\s+(.+?):\s+No such file or directory#",$buffer,$re)){
events("ERROR ON {$re[1]} : No such file or directory -> create it");
@file_put_contents($re[1],"www.nodomain.bv");
return null;
}
if(strpos($buffer,"ERROR: Going into emergency mode")>0){
events("ERROR: Going into emergency mode");
squid_admin_mysql(1, "Web filtering: turn to emergency mode","$buffer\nPlease contact your support to fix this problem\ncurrently, no filtering urls will be enabled");
send_email_events("squidguard: squidguard turn to emergency mode","SquidGuard claim\n$buffer\nPlease contact your support to fix this problem\ncurrently, no filtering urls will be enabled","proxy");
return ;
}
events("Not filtered: $buffer");
}
function squid_watchdog_events($text){
$unix=new unix();
if(function_exists("debug_backtrace")){$trace=debug_backtrace();if(isset($trace[1])){$sourcefile=basename($trace[1]["file"]);$sourcefunction=$trace[1]["function"];$sourceline=$trace[1]["line"];}}
$unix->events($text,"/var/log/squid.watchdog.log",false,$sourcefunction,$sourceline);
}
function IfFileTime($file,$min=10){
if(file_time_min($file)>$min){return true;}
return false;
}
function WriteFileCache($file){
@unlink("$file");
@unlink($file);
@file_put_contents($file,"#");
}
function events($text){
$pid=@getmypid();
$date=@date("H:i:s");
$logFile="/var/log/artica-postfix/squidguard-tail.debug";
$size=@filesize($logFile);
if($size>1000000){@unlink($logFile);}
$f = @fopen($logFile, 'a');
@fwrite($f, "$date [$pid]:: ".basename(__FILE__)." $text\n");
@fclose($f);
}
?>