]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
c37945ed | 2 | * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com |
e3af0ae9 | 3 | * 1998, 2009 |
94ff9470 | 4 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
25697a35 GS |
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" | |
5f3cfd1d | 27 | #include "include/defs.h" |
25697a35 | 28 | |
32e71fa4 | 29 | void topsites(void) |
25697a35 GS |
30 | { |
31 | ||
32 | FILE *fp_in, *fp_ou; | |
12afa4e6 | 33 | |
25697a35 GS |
34 | char url[MAXLEN]; |
35 | char ourl[MAXLEN]; | |
36 | char nacc[20]; | |
37 | char nbytes[20]; | |
38 | char ntime[20]; | |
12afa4e6 | 39 | char ntemp[255]; |
25697a35 GS |
40 | char ttnacc[20]; |
41 | char ttnbytes[20]; | |
42 | char ttntime[20]; | |
43 | char csort[255]; | |
d6e703cc FM |
44 | char general[MAXLEN]; |
45 | char general2[MAXLEN]; | |
46 | char general3[MAXLEN]; | |
25697a35 GS |
47 | char per[MAXLEN]; |
48 | char sites[MAXLEN]; | |
49 | char report[MAXLEN]; | |
d6e703cc | 50 | char period[100]; |
25697a35 GS |
51 | char sortf[10]; |
52 | char sortt[10]; | |
53 | long long int tnacc=0; | |
54 | long long int tnbytes=0; | |
55 | long long int tntime=0; | |
56 | long long int twork1=0, twork2=0, twork3=0; | |
57 | int regs=0; | |
456d78a5 | 58 | int cstatus; |
25697a35 | 59 | |
d6e703cc FM |
60 | if(strcmp(Privacy,"yes") == 0) |
61 | return; | |
25697a35 | 62 | |
d6e703cc FM |
63 | sprintf(general,"%s/sarg-general",dirname); |
64 | sprintf(sites,"%s/sarg-sites",dirname); | |
65 | sprintf(general2,"%s/sarg-general2",dirname); | |
66 | sprintf(general3,"%s/sarg-general3",dirname); | |
67 | sprintf(per,"%s/sarg-period",dirname); | |
491b862f GS |
68 | |
69 | if (strstr(ReportType,"topusers") == 0) | |
70 | sprintf(report,"%s/index.html",dirname); | |
71 | else | |
72 | sprintf(report,"%s/topsites.html",dirname); | |
25697a35 GS |
73 | |
74 | if ((fp_in = fopen(per, "r")) == 0) { | |
75 | fprintf(stderr, "SARG: (topuser) %s: %s\n",text[45],per); | |
76 | exit(1); | |
77 | } | |
78 | ||
05b90947 FM |
79 | if (!fgets(period,sizeof(period),fp_in)) { |
80 | fprintf(stderr,"SARG: (topuser) read error in %s\n",per); | |
81 | exit(1); | |
82 | } | |
25697a35 GS |
83 | fclose(fp_in); |
84 | ||
9a2efbd0 | 85 | sprintf(csort,"sort -k 4,4 -o \"%s\" \"%s\"",general2,general); |
456d78a5 FM |
86 | cstatus=system(csort); |
87 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
88 | fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus)); | |
89 | fprintf(stderr, "SARG: sort command: %s\n",csort); | |
90 | exit(1); | |
91 | } | |
25697a35 | 92 | |
d6e703cc FM |
93 | if((fp_in=fopen(general2,"r"))==NULL) { |
94 | fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],general2); | |
456d78a5 | 95 | fprintf(stderr, "SARG: sort command: %s\n",csort); |
25697a35 GS |
96 | exit(1); |
97 | } | |
98 | ||
d6e703cc FM |
99 | if((fp_ou=fopen(general3,"w"))==NULL) { |
100 | fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],general3); | |
25697a35 GS |
101 | exit(1); |
102 | } | |
103 | ||
104 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
120d768c | 105 | if (getword(url,sizeof(url),buf,'\t')<0) { |
4bcb77cf FM |
106 | printf("SARG: Maybe you have an invalid user in your %s file of the topsites.\n",general2); |
107 | exit(1); | |
108 | } | |
25697a35 | 109 | if(strcmp(url,"TOTAL") == 0) { |
120d768c | 110 | if (getword(ttnacc,sizeof(ttnacc),buf,'\t')<0) { |
4bcb77cf FM |
111 | printf("SARG: Maybe you have an invalid total number of access in your %s file of the topsites.\n",general2); |
112 | exit(1); | |
113 | } | |
120d768c | 114 | if (getword(ttnbytes,sizeof(ttnbytes),buf,'\t')<0) { |
4bcb77cf FM |
115 | printf("SARG: Maybe you have an invalid total number of bytes in your %s file of the topsites.\n",general2); |
116 | exit(1); | |
117 | } | |
120d768c | 118 | if (getword(ttntime,sizeof(ttntime),buf,'\t')<0) { |
4bcb77cf FM |
119 | printf("SARG: Maybe you have an invalid total time in your %s file of the topsites.\n",general2); |
120 | exit(1); | |
121 | } | |
25697a35 GS |
122 | continue; |
123 | } | |
120d768c | 124 | if (getword(nacc,sizeof(nacc),buf,'\t')<0) { |
4bcb77cf FM |
125 | printf("SARG: Maybe you have an invalid number of access in your %s file of the topsites.\n",general2); |
126 | exit(1); | |
127 | } | |
120d768c | 128 | if (getword(nbytes,sizeof(nbytes),buf,'\t')<0) { |
4bcb77cf FM |
129 | printf("SARG: Maybe you have an invalid number of bytes in your %s file of the topsites.\n",general2); |
130 | exit(1); | |
131 | } | |
120d768c | 132 | if (getword(url,sizeof(url),buf,'\t')<0) { |
4bcb77cf FM |
133 | printf("SARG: Maybe you have an invalid url in your %s file of the topsites.\n",general2); |
134 | exit(1); | |
135 | } | |
120d768c | 136 | if (getword(ntemp,sizeof(ntemp),buf,'\t')<0) { |
4bcb77cf FM |
137 | printf("SARG: Maybe you have a broken record or garbage in column 5 in your %s file of the topsites.\n",general2); |
138 | exit(1); | |
139 | } | |
120d768c | 140 | if (getword(ntemp,sizeof(ntemp),buf,'\t')<0) { |
4bcb77cf FM |
141 | printf("SARG: Maybe you have a broken record or garbage in column 6 in your %s file of the topsites.\n",general2); |
142 | exit(1); | |
143 | } | |
120d768c | 144 | if (getword(ntemp,sizeof(ntemp),buf,'\t')<0) { |
4bcb77cf FM |
145 | printf("SARG: Maybe you have a broken record or garbage in column 7 in your %s file of the topsites.\n",general2); |
146 | exit(1); | |
147 | } | |
120d768c | 148 | if (getword(ntime,sizeof(ntime),buf,'\t')<0) { |
4bcb77cf FM |
149 | printf("SARG: Maybe you have a broken record or garbage in column 8 in your %s file of the topsites.\n",general2); |
150 | exit(1); | |
151 | } | |
25697a35 GS |
152 | |
153 | if(!regs) { | |
154 | strcpy(ourl,url); | |
155 | regs++; | |
156 | } | |
157 | ||
158 | if(strcmp(url,ourl) != 0) { | |
159 | my_lltoa(tnacc,val1,15); | |
160 | my_lltoa(tnbytes,val2,15); | |
161 | my_lltoa(tntime,val3,15); | |
120d768c | 162 | fprintf(fp_ou,"%s\t%s\t%s\t%s\n",val1,val2,val3,ourl); |
25697a35 GS |
163 | strcpy(ourl,url); |
164 | tnacc=0; | |
165 | tnbytes=0; | |
166 | tntime=0; | |
167 | } | |
168 | ||
169 | tnacc+=my_atoll(nacc); | |
170 | tnbytes+=my_atoll(nbytes); | |
171 | tntime+=my_atoll(ntime); | |
172 | } | |
173 | ||
174 | my_lltoa(tnacc,val1,15); | |
175 | my_lltoa(tnbytes,val2,15); | |
176 | my_lltoa(tntime,val3,15); | |
120d768c | 177 | fprintf(fp_ou,"%s\t%s\t%s\t%s\n",val1,val2,val3,ourl); |
25697a35 GS |
178 | |
179 | fclose(fp_in); | |
180 | fclose(fp_ou); | |
d6e703cc | 181 | unlink(general2); |
25697a35 GS |
182 | |
183 | strlow(TopsitesSortField); | |
184 | strlow(TopsitesSortType); | |
185 | ||
186 | if(strcmp(TopsitesSortField,"connect") == 0) | |
187 | strcpy(sortf,"1,1"); | |
188 | if(strcmp(TopsitesSortField,"bytes") == 0) | |
189 | strcpy(sortf,"2,2"); | |
190 | if(strcmp(TopsitesSortType,"a") == 0) | |
191 | strcpy(sortt," "); | |
192 | if(strcmp(TopsitesSortType,"d") == 0) | |
193 | strcpy(sortt,"-r"); | |
194 | ||
9a2efbd0 | 195 | sprintf(csort,"sort %s -k %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3); |
456d78a5 FM |
196 | cstatus=system(csort); |
197 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
198 | fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus)); | |
199 | fprintf(stderr, "SARG: sort command: %s\n",csort); | |
200 | exit(1); | |
201 | } | |
202 | if((fp_in=fopen(sites,"r"))==NULL) { | |
203 | fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],sites); | |
204 | fprintf(stderr, "SARG: sort command: %s\n",csort); | |
205 | exit(1); | |
206 | } | |
25697a35 | 207 | |
d6e703cc FM |
208 | unlink(general2); |
209 | unlink(general3); | |
25697a35 | 210 | |
25697a35 GS |
211 | if((fp_ou=fopen(report,"w"))==NULL) { |
212 | fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],report); | |
213 | exit(1); | |
214 | } | |
215 | ||
216 | regs=0; | |
217 | ||
d6e703cc | 218 | 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); |
25697a35 GS |
219 | css(fp_ou); |
220 | fputs("</head>\n",fp_ou); | |
dfb337be FM |
221 | fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage); |
222 | write_logo_image(fp_ou); | |
25697a35 | 223 | |
491b862f GS |
224 | if(strcmp(IndexTree,"date") == 0) |
225 | show_sarg(fp_ou,"../../.."); | |
226 | else | |
227 | show_sarg(fp_ou, ".."); | |
228 | ||
dfb337be | 229 | fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou); |
354c1a68 | 230 | fprintf(fp_ou,"<tr><th class=\"title\">%s</th></tr>\n",Title); |
48864d28 | 231 | |
354c1a68 FM |
232 | fprintf(fp_ou,"<tr><td class=\"header3\">%s: %s</td></tr>\n",text[89],period); |
233 | fprintf(fp_ou,"<tr><th class=\"header3\">%s %s %s</th></tr>\n",text[83],TopSitesNum,text[84]); | |
dfb337be | 234 | fputs("</table></div>\n",fp_ou); |
25697a35 | 235 | |
dfb337be | 236 | fputs("<div align=\"center\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou); |
25697a35 | 237 | fputs("<tr><td></td></tr>\n",fp_ou); |
354c1a68 | 238 | fprintf(fp_ou,"<tr><th class=\"header\">%s</th><th class=\"header\">%s</th> \ |
25697a35 GS |
239 | <th class=\"header\">%s</th><th class=\"header\">%s</th> \ |
240 | <th class=\"header\">%s</th></tr>\n", \ | |
241 | text[100],text[91],text[92],text[93],text[99]); | |
25697a35 GS |
242 | |
243 | regs=1; | |
d6e703cc | 244 | ntopsites = 0; |
25697a35 GS |
245 | |
246 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
247 | if(regs>atoi(TopSitesNum)) | |
248 | break; | |
120d768c | 249 | if (getword(nacc,sizeof(nacc),buf,'\t')<0) { |
4bcb77cf FM |
250 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",sites); |
251 | exit(1); | |
252 | } | |
d6e703cc | 253 | if (atoi(nacc) == 0) continue; |
120d768c FM |
254 | if (getword(nbytes,sizeof(nbytes),buf,'\t')<0 || |
255 | getword(ntime,sizeof(ntime),buf,'\t')<0 || getword(url,sizeof(url),buf,'\t')<0) { | |
4bcb77cf FM |
256 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",sites); |
257 | exit(1); | |
258 | } | |
25697a35 GS |
259 | |
260 | twork1=my_atoll(nacc); | |
261 | twork2=my_atoll(nbytes); | |
262 | twork3=my_atoll(ntime); | |
263 | ||
48864d28 FM |
264 | strcpy(wwork1,fixnum(twork1,1)); |
265 | strcpy(wwork2,fixnum(twork2,1)); | |
266 | strcpy(wwork3,fixtime(twork3)); | |
25697a35 GS |
267 | |
268 | if(strlen(BlockIt) > 0) | |
269 | sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"../images/sarg-squidguard-block.png\" border=\"0\"></a> ",wwwDocumentRoot,BlockIt,url); | |
270 | else BlockImage[0]='\0'; | |
271 | ||
272 | ||
dfb337be | 273 | fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\"><font class=\"link\">%s</font></a></td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",regs,BlockImage,url,url,wwork1,wwork2,wwork3); |
25697a35 GS |
274 | regs++; |
275 | } | |
276 | ||
277 | ||
dfb337be | 278 | fputs("</table>\n</div>\n",fp_ou); |
25697a35 GS |
279 | |
280 | show_info(fp_ou); | |
281 | ||
282 | fputs("</body>\n</html>\n",fp_ou); | |
48864d28 | 283 | |
25697a35 GS |
284 | fclose(fp_in); |
285 | fclose(fp_ou); | |
286 | ||
287 | return; | |
288 | ||
289 | } |