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