]> git.ipfire.org Git - thirdparty/sarg.git/blob - smartfilter.c
Moved all the functions declarations from conf.h to defs.h for consistency.
[thirdparty/sarg.git] / smartfilter.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26 #include "include/conf.h"
27 #include "include/defs.h"
28
29 void smartfilter_report(void)
30 {
31
32 FILE *fp_in = NULL, *fp_ou = NULL, *fp_user = NULL;
33
34 char url[MAXLEN];
35 char csort[255];
36 char smart_in[MAXLEN];
37 char smart_ou[MAXLEN];
38 char per[MAXLEN];
39 char sites[MAXLEN];
40 char report[MAXLEN];
41 char period[100];
42 char ip[MAXLEN];
43 char user[MAXLEN];
44 char ouser[MAXLEN];
45 char data[15];
46 char hora[15];
47 char smartcat[256];
48 char smartheader[15];
49 char ftime[128];
50 char smartuser[MAXLEN];
51 char *str;
52 int fuser=0;
53 int cstatus;
54
55 ouser[0]='\0';
56
57 sprintf(smartheader,"%s",text[116]);
58 strup(smartheader);
59
60 sprintf(smart_in,"%s/smartfilter.unsort",dirname);
61 sprintf(sites,"%s/sarg-sites",dirname);
62 sprintf(smart_ou,"%s/smartfilter.log",dirname);
63 sprintf(per,"%s/sarg-period",dirname);
64 sprintf(report,"%s/smartfilter.html",dirname);
65
66 if ((fp_in = fopen(per, "r")) == 0) {
67 fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[45],per);
68 exit(1);
69 }
70
71 fgets(period,sizeof(period),fp_in);
72 fclose(fp_in);
73
74 sprintf(csort,"sort -n -k 1,1 -k 2,2 -k 3,3 -o '%s' '%s'",smart_ou,smart_in);
75 cstatus=system(csort);
76 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
77 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
78 fprintf(stderr, "SARG: sort command: %s\n",csort);
79 exit(1);
80 }
81 if((fp_in=fopen(smart_ou,"r"))==NULL) {
82 fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[8],smart_ou);
83 fprintf(stderr, "SARG: sort command: %s\n",csort);
84 exit(1);
85 }
86 unlink(smart_in);
87
88 if((fp_ou=fopen(report,"w"))==NULL) {
89 fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[8],report);
90 exit(1);
91 }
92
93 fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
94 fputs("</head>\n",fp_ou);
95 if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
96 fprintf(fp_ou,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
97 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ou);
98 if(strlen(LogoImage) > 0) fprintf(fp_ou, "<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s>&nbsp;%s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n",LogoImage,Width,Height,LogoText);
99
100 fprintf(fp_ou,"<tr><th align=center><b><font color=%s size=+1>%s</font></b></th></tr>\n",TiColor,Title);
101 fprintf(fp_ou,"<tr><td align=center bgcolor=%s><font size=%s>%s: %s</font></td></tr>\n",HeaderBgColor,FontSize,text[89],period);
102 fprintf(fp_ou,"<tr><th bgcolor=%s align=center><font size=%s>%s</font></th></tr>\n",HeaderBgColor,FontSize,text[116]);
103 fputs("</table></center>\n",fp_ou);
104
105 fputs("<center><table cellpadding=0 cellspacing=2>\n",fp_ou);
106 fputs("<tr><td></td></tr>\n",fp_ou);
107 fputs("<tr><td></td></tr>\n",fp_ou);
108 fputs("<tr><td></td></tr>\n",fp_ou);
109 fprintf(fp_ou,"<tr><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th></tr>\n",HeaderBgColor,FontSize,text[98],HeaderBgColor,FontSize,text[111],HeaderBgColor,FontSize,text[110],HeaderBgColor,FontSize,text[91],HeaderBgColor,FontSize,smartheader);
110
111 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
112 if (getword(user,sizeof(user),buf,' ')<0 || getword(data,sizeof(data),buf,' ')<0 ||
113 getword(hora,sizeof(hora),buf,' ')<0 || getword(ip,sizeof(ip),buf,' ')<0 ||
114 getword(url,sizeof(url),buf,' ')<0 || getword(smartcat,sizeof(smartcat),buf,'\n')<0) {
115 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",smart_ou);
116 exit(1);
117 }
118
119 if((str=(char *) strstr(user, "_")) != (char *) NULL ) {
120 if((str=(char *) strstr(str+1, "_")) != (char *) NULL )
121 fixip(user);
122 }
123
124 if(strcmp(ouser,user) != 0) {
125 strcpy(ouser,user);
126 sprintf(smartuser,"%s/denied_%s.html",dirname,user);
127 if(fuser) {
128 fuser=0;
129 fputs("</table>\n",fp_user);
130 if(strcmp(ShowSargInfo,"yes") == 0) {
131 zdate(ftime, DateFormat);
132 fprintf(fp_user,"<br><br><center><font size=-2>%s <a href='%s'>%s-%s</a> %s %s</font></center>\n",text[108],URL,PGM,VERSION,text[109],ftime);
133 }
134 fputs("</body>\n</html>\n",fp_user);
135 fclose(fp_user);
136 }
137 if ((fp_user = fopen(smartuser, "a")) == 0) {
138 fprintf(stderr, "SARG: (smartfilter) %s: %s\n",text[45],smartuser);
139 exit(1);
140 }
141 fuser=1;
142
143 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"",fp_ou);
144 fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_ou);
145 fputs("<html>\n",fp_user);
146 fputs("<head>\n",fp_user);
147 fprintf(fp_user," <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
148 fputs("</head>\n",fp_user);
149
150 if(strlen(FontFace) > 0) {
151 /*
152 Before merging the sprintf and the fputs, the code looked like this:
153 sprintf(html2,"<font face=%s>\n",FontFace);
154 fputs(url,fp_user);
155 The two lines don't use the same buffer so the string formated by sprintf is not the string
156 written to fp_user. I (fmarchal) assumed it was a typo and replaced it by a fprintf but
157 that font tag is not valid outside of the body. So, the generated html was likely
158 containing garbage not rendered by the browser.
159 */
160 fprintf(fp_user,"<font face=%s>\n",FontFace);
161 }
162 fprintf(fp_user,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
163 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_user);
164 if(strlen(LogoImage) > 0) fprintf(fp_user,"<tr><th align=left><img src='%s' border=0 align=absmiddle width=%s height=%s><font color=%s>%s</font>\n",LogoImage,Width,Height,LogoTextColor,LogoText);
165 fprintf(fp_user,"<tr><th align=center><b><font color=%s size=+1>%s</font></b></th></tr>\n",TiColor,Title);
166 fprintf(fp_user,"<tr><td align=center bgcolor=%s><font size=%s>%s: %s</font></td></tr>\n",HeaderBgColor,FontSize,text[89],period);
167 fprintf(fp_user,"<tr><td align=center bgcolor=%s><font size=%s>%s:</font><font size=%s> %s</font></td></tr>\n",HeaderBgColor,FontSize,text[90],FontSize,user);
168 fputs("</table></center>\n",fp_user);
169 fputs("<center><table cellpadding=0 cellspacing=2>\n",fp_user);
170 fputs("<tr><td></td></tr>\n",fp_user);
171 fputs("<tr><td></td></tr>\n",fp_user);
172 fputs("<tr><td></td></tr>\n",fp_user);
173 fprintf(fp_user,"<tr><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th><th bgcolor=%s><font size=%s>%s</font></th></tr>\n",HeaderBgColor,FontSize,text[98],HeaderBgColor,FontSize,text[111],HeaderBgColor,FontSize,text[110],HeaderBgColor,FontSize,text[91],HeaderBgColor,FontSize,smartheader);
174 }
175 fprintf(fp_user,"<tr><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s-%s</font></td><td bgcolor=%s><font size=%s>%s</font></td><td bgcolor=%s><font size=%s>%s</font></td></th>\n",TxBgColor,FontSize,user,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
176
177 fprintf(fp_ou,"<tr><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s</font></td><td bgcolor=%s align=center><font size=%s>%s-%s</font></td><td bgcolor=%s><font size=%s>%s</font></td><td bgcolor=%s><font size=%s>%s</font></td></th>\n",TxBgColor,FontSize,user,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
178 }
179
180 fputs("</table>\n",fp_ou);
181
182 if(strcmp(ShowSargInfo,"yes") == 0) {
183 zdate(ftime, DateFormat);
184 fprintf(fp_ou,"<br><br><center><font size=-2>%s <a href='%s'>%s-%s</a> %s %s</font></center>\n",text[108],URL,PGM,VERSION,text[109],ftime);
185 }
186
187 fputs("</body>\n</html>\n",fp_user);
188
189 fclose(fp_ou);
190 if(fp_user) {
191 fputs("</table>\n",fp_user);
192 if(strcmp(ShowSargInfo,"yes") == 0) {
193 zdate(ftime, DateFormat);
194 fprintf(fp_user,"<br><br><center><font size=-2>%s <a href='%s'>%s-%s</a> %s %s</font></center>\n",text[108],URL,PGM,VERSION,text[109],ftime);
195 }
196 fputs("</body>\n</html>\n",fp_user);
197 fclose(fp_user);
198 }
199
200 return;
201 }