-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcron.dansguardian.php
131 lines (92 loc) · 3.1 KB
/
cron.dansguardian.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
<?php
include_once(dirname(__FILE__).'/ressources/class.templates.inc');
include_once(dirname(__FILE__).'/ressources/class.mysql.inc');
$file=$argv[1];
$_GET["DEBUG"]=false;
if(trim(strtolower($argv[2]))=="--debug"){$_GET["DEBUG"]=true;}
if($argv[1]=='--rebuild-sites'){
rebuildsites();
die('finish');
}
die("depreciated");
if(!file_exists($file)){write_syslog(" Unable to stat $file",__FILE__);}
$q=new mysql();
$q->BuildTables();
if(!$q->TestingConnection()){write_syslog("Unable to logon to mysql",__FILE__);}
$datas_file=file_get_contents($file);
$datas=explode("\n",$datas_file);
write_syslog("$file: ".Count($datas) . " line(s) with " . strlen($datas_file). " bytes length",__FILE__);
while (list ($num, $val) = each ($datas) ){
if(trim($val==null)){continue;}
if($_GET["DEBUG"]){echo "Parsing line number $num/".Count($datas)."\n";}
parseDansLine($val);
}
@unlink($file);
System("/etc/init.d/artica-postfix restart squid &");
function parseDansLine($line){
if(!preg_match('#([0-9\.]+)\s+([0-9\:]+)\s+-\s+(.+?)\s+ht#',$line,$re)){
echo "Failed parsed \"$line\"\n";
}
$line="ht".str_replace($re[0],'',$line);
$date=str_replace('.',"-",$re[1]) . " " . $re[2];
$client=$re[3];
if(!preg_match('#^(.+?)\s+#',$line,$re)){
echo "Failed $line ".__LINE__."\n";
return false;
}
$line=str_replace($re[0],'',$line);
$uri=$re[1]."/";
if(preg_match('#^.+?:\/\/(.+?)[\/:\s]#',$uri,$re)){
$domain=$re[1];
}else{
echo "ERROR: unable to found the domain in \"$uri\"\n";
return null;
}
if(preg_match('#\*DENIED\*#',$line)){
if(preg_match("#DENIED\*\s+(.+?)[:\.](.+?)\s+(GET|POST)#",$line,$re)){
$TYPE=$re[1];
$raison=$re[2];
}else{
echo "ERROR: find reason in $line\n";
}
}else{
$TYPE="PASS";
}
$domainMD5=md5($domain);
AddSite($domain);
if($_GET["DEBUG"]){echo "Adding new $uri ($TYPE) [$raison] $client\n";}
$md5a=md5("$domainMD5$uri$TYPE$raison$client$date");
$q=new mysql();
$sql="INSERT INTO dansguardian_events(sitename,URI,`TYPE`,REASON,CLIENT,zDate,zMD5)
VALUES('$domainMD5','$uri','$TYPE','$raison','$client','$date','$md5a');";
$q->QUERY_SQL($sql,"artica_events");
if(!$q->ok){write_syslog("Failed : \"$sql\" ($q->mysql_error)",__FILE__);}
}
function AddSite($domain){
$domainMD5=md5($domain);
if(!$_GET["DD"][$domainMD5]){
if($_GET["DEBUG"]){echo "Adding site $domain\n";}
$q=new mysql();
$sql="INSERT INTO dansguardian_sites(website_md5,website) VALUES('$domainMD5','$domain');";
echo "Adding domain index $domain\n";
$q->QUERY_SQL($sql,"artica_events");
if(!$q->ok){write_syslog("AddSite():: Failed : \"$sql\" ($q->mysql_error)",__FILE__);}
$_GET["DD"][$domainMD5]=TRUE;
}else{
if($_GET["DEBUG"]){echo "$domain already exists in database\n";}
}
}
function rebuildsites(){
$q=new mysql();
$sql="SELECT uri FROM dansguardian_events";
$results=$q->QUERY_SQL($sql,'artica_events');
while ($ligne = mysql_fetch_array($results)) {
if(preg_match('#^.+?:\/\/(.+?)[\/:\s]#',$ligne["uri"],$re)){
$domain=$re[1];
AddSite($domain);
}else{
echo "ERROR: unable to found the domain in \"{$ligne["uri"]}\"\n";
}
}
}
?>