]>
git.ipfire.org Git - thirdparty/sarg.git/blob - smartfilter.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
30 void smartfilter_report ( void )
33 FILE * fp_in
= NULL
, * fp_ou
= NULL
, * fp_user
= NULL
;
38 char smart_in
[ MAXLEN
];
39 char smart_ou
[ MAXLEN
];
50 char smartuser
[ MAXLEN
];
53 struct getwordstruct gwarea
;
54 const struct userinfostruct
* uinfo
;
58 strcpy ( smartheader
, _ ( "SmartFilter" ));
61 sprintf ( smart_in
, "%s/smartfilter.unsort" , outdirname
);
62 sprintf ( sites
, "%s/sarg-sites" , outdirname
);
63 sprintf ( smart_ou
, "%s/smartfilter.log" , outdirname
);
64 sprintf ( report
, "%s/smartfilter.html" , outdirname
);
66 if ( snprintf ( csort
, sizeof ( csort
), "sort -n -k 1,1 -k 2,2 -k 3,3 -o \" %s \" \" %s \" " , smart_ou
, smart_in
)>= sizeof ( csort
)) {
67 debuga ( _ ( "cannot build the sort command to sort file %s \n " ), smart_in
);
70 cstatus
= system ( csort
);
71 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
72 debuga ( _ ( "sort command return status %d \n " ), WEXITSTATUS ( cstatus
));
73 debuga ( _ ( "sort command: %s \n " ), csort
);
76 if (( fp_in
= fopen ( smart_ou
, "r" ))== NULL
) {
77 debuga ( _ ( "(smartfilter) Cannot open log file %s \n " ), smart_ou
);
78 debuga ( _ ( "sort command: %s \n " ), csort
);
83 if (( fp_ou
= fopen ( report
, "w" ))== NULL
) {
84 debuga ( _ ( "(smartfilter) Cannot open log file %s \n " ), report
);
88 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
);
89 fputs ( "</head> \n " , fp_ou
);
90 if ( strlen ( FontFace
) > 0 ) fprintf ( fp_ou
, "<font face=%s> \n " , FontFace
);
91 fprintf ( fp_ou
, "<body bgcolor= \" %s \" text= \" %s \" background= \" %s \" > \n " , BgColor
, TxColor
, BgImage
);
92 fputs ( "<div align= \" center \" ><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n " , fp_ou
);
93 write_logo_image ( fp_ou
);
95 fprintf ( fp_ou
, "<tr><th align= \" center \" ><b><font color= \" %s \" size= \" +1 \" >%s</font></b></th></tr> \n " , TiColor
, Title
);
96 fprintf ( fp_ou
, "<tr><td align= \" center \" bgcolor= \" %s \" ><font size= \" %s \" >" , HeaderBgColor
, FontSize
);
97 fprintf ( fp_ou
, _ ( "Period: %s" ), period
. html
);
98 fputs ( "</font></td></tr> \n " , fp_ou
);
99 fprintf ( fp_ou
, "<tr><th bgcolor= \" %s \" align= \" center \" ><font size= \" %s \" >%s</font></th></tr> \n " , HeaderBgColor
, FontSize
, _ ( "SmartFilter" ));
100 fputs ( "</table></div> \n " , fp_ou
);
102 fputs ( "<div align= \" center \" ><table cellpadding= \" 0 \" cellspacing= \" 2 \" > \n " , fp_ou
);
103 fputs ( "<tr><td></td></tr> \n " , fp_ou
);
104 fputs ( "<tr><td></td></tr> \n " , fp_ou
);
105 fputs ( "<tr><td></td></tr> \n " , fp_ou
);
106 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
, smartheader
);
108 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
109 getword_start (& gwarea
, buf
);
110 if ( getword ( user
, sizeof ( user
),& gwarea
, ' \t ' )< 0 || getword ( data
, sizeof ( data
),& gwarea
, ' \t ' )< 0 ||
111 getword ( hora
, sizeof ( hora
),& gwarea
, ' \t ' )< 0 || getword ( ip
, sizeof ( ip
),& gwarea
, ' \t ' )< 0 ||
112 getword ( url
, sizeof ( url
),& gwarea
, ' \t ' )< 0 || getword ( smartcat
, sizeof ( smartcat
),& gwarea
, ' \n ' )< 0 ) {
113 debuga ( _ ( "There is a broken record or garbage in file %s \n " ), smart_ou
);
117 uinfo
= userinfo_find_from_id ( user
);
119 debuga ( _ ( "Unknown user ID %s in file %s \n " ), user
, smart_ou
);
122 if ( strcmp ( ouser
, user
) != 0 ) {
124 sprintf ( smartuser
, "%s/denied_%s.html" , outdirname
, uinfo
-> filename
);
127 fputs ( "</table> \n " , fp_user
);
129 zdate ( ftime
, sizeof ( ftime
), DateFormat
);
130 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
);
132 fputs ( "</body> \n </html> \n " , fp_user
);
135 if (( fp_user
= fopen ( smartuser
, "a" )) == 0 ) {
136 debuga ( _ ( "(smartfilter) Cannot open file %s \n " ), smartuser
);
141 fputs ( "<!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.01 Transitional//EN \" " , fp_ou
);
142 fputs ( " \" http://www.w3.org/TR/html4/loose.dtd \" > \n " , fp_ou
);
143 fputs ( "<html> \n " , fp_user
);
144 fputs ( "<head> \n " , fp_user
);
145 fprintf ( fp_user
, " <meta http-equiv= \" Content-Type \" content= \" text/html; charset=%s \" > \n " , CharSet
);
146 fputs ( "</head> \n " , fp_user
);
148 if ( FontFace
[ 0 ] != 0 ) {
150 Before merging the sprintf and the fputs, the code looked like this:
151 sprintf(html2,"<font face=%s>\n",FontFace);
153 The two lines don't use the same buffer so the string formated by sprintf is not the string
154 written to fp_user. I (fmarchal) assumed it was a typo and replaced it by a fprintf but
155 that font tag is not valid outside of the body. So, the generated html was likely
156 containing garbage not rendered by the browser.
158 fprintf ( fp_user
, "<font face=%s> \n " , FontFace
);
160 fprintf ( fp_user
, "<body bgcolor= \" %s \" text= \" %s \" background= \" %s \" > \n " , BgColor
, TxColor
, BgImage
);
161 fputs ( "<div align= \" center \" ><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n " , fp_user
);
162 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
);
163 fprintf ( fp_user
, "<tr><th align= \" center \" ><b><font color= \" %s \" size= \" +1 \" >%s</font></b></th></tr> \n " , TiColor
, Title
);
164 fputs ( "<tr><td align=center bgcolor= \" %s \" ><font size= \" %s \" >" , fp_user
);
165 fprintf ( fp_user
, _ ( "Period: %s" ), period
. html
);
166 fputs ( "</font></td></tr> \n " , fp_user
);
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
, _ ( "User" ), FontSize
, uinfo
-> label
);
168 fputs ( "</table></div> \n " , fp_user
);
169 fputs ( "<div align= \" 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
, _ ( "USERID" ), HeaderBgColor
, FontSize
, _ ( "IP/NAME" ), HeaderBgColor
, FontSize
, _ ( "DATE/TIME" ), HeaderBgColor
, FontSize
, _ ( "ACCESSED SITE" ), HeaderBgColor
, FontSize
, smartheader
);
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
, uinfo
-> label
, TxBgColor
, FontSize
, ip
, TxBgColor
, FontSize
, data
, hora
, TxBgColor
, FontSize
, url
, TxBgColor
, FontSize
, smartcat
);
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
, uinfo
-> label
, TxBgColor
, FontSize
, ip
, TxBgColor
, FontSize
, data
, hora
, TxBgColor
, FontSize
, url
, TxBgColor
, FontSize
, smartcat
);
180 fputs ( "</table> \n " , fp_ou
);
183 zdate ( ftime
, sizeof ( ftime
), DateFormat
);
184 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
);
187 fputs ( "</body> \n </html> \n " , fp_user
);
191 fputs ( "</table> \n " , fp_user
);
193 zdate ( ftime
, sizeof ( ftime
), DateFormat
);
194 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
);
196 fputs ( "</body> \n </html> \n " , fp_user
);