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