]> git.ipfire.org Git - thirdparty/sarg.git/blob - smartfilter.c
Make a translator friendly message out of pieced together words
[thirdparty/sarg.git] / smartfilter.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2012
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27 #include "include/conf.h"
28 #include "include/defs.h"
29
30 void smartfilter_report(void)
31 {
32 FILE *fp_in = NULL, *fp_ou = NULL, *fp_user = NULL;
33
34 char buf[MAXLEN];
35 char url[MAXLEN];
36 char csort[255];
37 char smart_in[MAXLEN];
38 char smart_ou[MAXLEN];
39 char sites[MAXLEN];
40 char report[MAXLEN];
41 char ip[MAXLEN];
42 char user[MAXLEN];
43 char ouser[MAXLEN];
44 char data[15];
45 char hora[15];
46 char smartcat[256];
47 char ftime[128];
48 char smartuser[MAXLEN];
49 int cstatus;
50 struct getwordstruct gwarea;
51 const struct userinfostruct *uinfo;
52
53 ouser[0]='\0';
54
55 if (snprintf(smart_in,sizeof(smart_in),"%s/smartfilter.int_unsort",tmp)>=sizeof(smart_in)) {
56 debuga(_("File name too long: %s/smartfilter.int_unsort\n"),tmp);
57 exit(EXIT_FAILURE);
58 }
59 if (snprintf(sites,sizeof(sites),"%s/sarg-sites",outdirname)>=sizeof(sites)) {
60 debuga(_("File name too long: %s/sarg-sites\n"),outdirname);
61 exit(EXIT_FAILURE);
62 }
63 if (snprintf(smart_ou,sizeof(smart_ou),"%s/smartfilter.int_log",tmp)>=sizeof(smart_ou)) {
64 debuga(_("File name too long: %s/smartfilter.int_log\n"),tmp);
65 exit(EXIT_FAILURE);
66 }
67 if (snprintf(report,sizeof(report),"%s/smartfilter.html",outdirname)>=sizeof(report)) {
68 debuga(_("File name too long: %s/smartfilter.html\n"),outdirname);
69 exit(EXIT_FAILURE);
70 }
71
72 if (snprintf(csort,sizeof(csort),"sort -n -t \"\t\" -k 1,1 -k 2,2 -k 3,3 -o \"%s\" \"%s\"",smart_ou,smart_in)>=sizeof(csort)) {
73 debuga(_("cannot build the sort command to sort file %s\n"),smart_in);
74 exit(EXIT_FAILURE);
75 }
76 cstatus=system(csort);
77 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
78 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
79 debuga(_("sort command: %s\n"),csort);
80 exit(EXIT_FAILURE);
81 }
82 if((fp_in=fopen(smart_ou,"r"))==NULL) {
83 debuga(_("(smartfilter) Cannot open log file %s: %s\n"),smart_ou,strerror(errno));
84 debuga(_("sort command: %s\n"),csort);
85 exit(EXIT_FAILURE);
86 }
87 if (!KeepTempLog && unlink(smart_in)) {
88 debuga(_("Cannot delete \"%s\": %s\n"),smart_in,strerror(errno));
89 exit(EXIT_FAILURE);
90 }
91
92 if((fp_ou=fopen(report,"w"))==NULL) {
93 debuga(_("(smartfilter) Cannot open log file %s: %s\n"),report,strerror(errno));
94 exit(EXIT_FAILURE);
95 }
96
97 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);
98 fputs("</head>\n",fp_ou);
99 if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
100 fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
101 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
102 write_logo_image(fp_ou);
103
104 fprintf(fp_ou,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title);
105 fprintf(fp_ou,"<tr><td align=\"center\" bgcolor=\"%s\"><font size=\"%s\">",HeaderBgColor,FontSize);
106 fprintf(fp_ou,_("Period: %s"),period.html);
107 fputs("</font></td></tr>\n",fp_ou);
108 fprintf(fp_ou,"<tr><th bgcolor=\"%s\" align=\"center\"><font size=\"%s\">%s</font></th></tr>\n",HeaderBgColor,FontSize,_("SmartFilter"));
109 fputs("</table></div>\n",fp_ou);
110
111 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
112 fputs("<tr><td></td></tr>\n",fp_ou);
113 fputs("<tr><td></td></tr>\n",fp_ou);
114 fputs("<tr><td></td></tr>\n",fp_ou);
115 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,_("USERID"),HeaderBgColor,FontSize,_("IP/NAME"),HeaderBgColor,FontSize,_("DATE/TIME"),HeaderBgColor,FontSize,_("ACCESSED SITE"),HeaderBgColor,FontSize,_("SMARTFILTER"));
116
117 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
118 getword_start(&gwarea,buf);
119 if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(data,sizeof(data),&gwarea,'\t')<0 ||
120 getword(hora,sizeof(hora),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
121 getword(url,sizeof(url),&gwarea,'\t')<0 || getword(smartcat,sizeof(smartcat),&gwarea,'\n')<0) {
122 debuga(_("There is a broken record or garbage in file %s\n"),smart_ou);
123 exit(EXIT_FAILURE);
124 }
125
126 uinfo=userinfo_find_from_id(user);
127 if (!uinfo) {
128 debuga(_("Unknown user ID %s in file %s\n"),user,smart_ou);
129 exit(EXIT_FAILURE);
130 }
131 if(strcmp(ouser,user) != 0) {
132 strcpy(ouser,user);
133 sprintf(smartuser,"%s/denied_%s.html",outdirname,uinfo->filename);
134 if (fp_user) {
135 fputs("</table>\n",fp_user);
136 if(ShowSargInfo) {
137 zdate(ftime, sizeof(ftime), df);
138 fputs("<br><br><div align=\"center\"><font size=\"-2\">",fp_user);
139 fprintf(fp_user,_("Generated by <a href=\"%s\">%s-%s</a> on %s"),URL,PGM,VERSION,ftime);
140 fputs("</font></div>\n",fp_user);
141 }
142 fputs("</body>\n</html>\n",fp_user);
143 if (fclose(fp_user)==EOF) {
144 debuga(_("Write error in %s: %s\n"),smartuser,strerror(errno));
145 exit(EXIT_FAILURE);
146 }
147 fp_user=NULL;
148 }
149 if ((fp_user = fopen(smartuser, "a")) == 0) {
150 debuga(_("(smartfilter) Cannot open file %s: %s\n"),smartuser,strerror(errno));
151 exit(EXIT_FAILURE);
152 }
153
154 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"",fp_ou);
155 fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_ou);
156 fputs("<html>\n",fp_user);
157 fputs("<head>\n",fp_user);
158 fprintf(fp_user," <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
159 fputs("</head>\n",fp_user);
160
161 if(FontFace[0] != 0) {
162 /*
163 Before merging the sprintf and the fputs, the code looked like this:
164 sprintf(html2,"<font face=%s>\n",FontFace);
165 fputs(url,fp_user);
166 The two lines don't use the same buffer so the string formated by sprintf is not the string
167 written to fp_user. I (fmarchal) assumed it was a typo and replaced it by a fprintf but
168 that font tag is not valid outside of the body. So, the generated html was likely
169 containing garbage not rendered by the browser.
170 */
171 fprintf(fp_user,"<font face=%s>\n",FontFace);
172 }
173 fprintf(fp_user,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
174 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_user);
175 if(LogoImage[0]!='\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);
176 fprintf(fp_user,"<tr><th align=\"center\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,Title);
177 fputs("<tr><td align=center bgcolor=\"%s\"><font size=\"%s\">",fp_user);
178 fprintf(fp_user,_("Period: %s"),period.html);
179 fputs("</font></td></tr>\n",fp_user);
180 fprintf(fp_user,"<tr><td align=\"center\" bgcolor=\"%s\"><font size=\"%s\">%s:</font><font size=\"%s\"> %s</font></td></tr>\n",HeaderBgColor,FontSize,_("User"),FontSize,uinfo->label);
181 fputs("</table></div>\n",fp_user);
182 fputs("<div align=\"center\"><table cellpadding=0 cellspacing=2>\n",fp_user);
183 fputs("<tr><td></td></tr>\n",fp_user);
184 fputs("<tr><td></td></tr>\n",fp_user);
185 fputs("<tr><td></td></tr>\n",fp_user);
186 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,_("USERID"),HeaderBgColor,FontSize,_("IP/NAME"),HeaderBgColor,FontSize,_("DATE/TIME"),HeaderBgColor,FontSize,_("ACCESSED SITE"),HeaderBgColor,FontSize,_("SMARTFILTER"));
187 }
188 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,uinfo->label,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
189
190 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,uinfo->label,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat);
191 }
192
193 fputs("</table>\n",fp_ou);
194
195 if(ShowSargInfo) {
196 zdate(ftime, sizeof(ftime), df);
197 fprintf(fp_ou,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
198 }
199
200 fputs("</body>\n</html>\n",fp_user);
201
202 if (fclose(fp_ou)==EOF) {
203 debuga(_("Write error in %s: %s\n"),report,strerror(errno));
204 exit(EXIT_FAILURE);
205 }
206 if(fp_user) {
207 fputs("</table>\n",fp_user);
208 if(ShowSargInfo) {
209 zdate(ftime, sizeof(ftime), df);
210 fprintf(fp_user,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime);
211 }
212 fputs("</body>\n</html>\n",fp_user);
213 if (fclose(fp_user)==EOF) {
214 debuga(_("Write error in %s: %s\n"),smartuser,strerror(errno));
215 exit(EXIT_FAILURE);
216 }
217 }
218
219 if (!KeepTempLog && unlink(smart_ou)) {
220 debuga(_("Cannot delete \"%s\": %s\n"),smart_ou,strerror(errno));
221 exit(EXIT_FAILURE);
222 }
223
224 return;
225 }