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