]> git.ipfire.org Git - thirdparty/sarg.git/blob - topsites.c
Imported Sarg 2.1.0
[thirdparty/sarg.git] / topsites.c
1 /*
2 * AUTHOR: Pedro Lineu Orso orso@penguintech.com.br
3 * 1998, 2005
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
28 void topsites()
29 {
30
31 FILE *fp_in, *fp_ou;
32
33 char url[MAXLEN];
34 char ourl[MAXLEN];
35 char nacc[20];
36 char nbytes[20];
37 char ntime[20];
38 char ntemp[20];
39 char ttnacc[20];
40 char ttnbytes[20];
41 char ttntime[20];
42 char csort[255];
43 char geral[MAXLEN];
44 char geral2[MAXLEN];
45 char geral3[MAXLEN];
46 char per[MAXLEN];
47 char sites[MAXLEN];
48 char report[MAXLEN];
49 char periodo[100];
50 char ftime[128];
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
59
60 sprintf(geral,"%s/geral",dirname);
61 sprintf(sites,"%s/sites",dirname);
62 sprintf(geral2,"%s/geral2",dirname);
63 sprintf(geral3,"%s/geral3",dirname);
64 sprintf(per,"%s/periodo",dirname);
65
66 if (strstr(ReportType,"topusers") == 0)
67 sprintf(report,"%s/index.html",dirname);
68 else
69 sprintf(report,"%s/topsites.html",dirname);
70
71 if ((fp_in = fopen(per, "r")) == 0) {
72 fprintf(stderr, "SARG: (topuser) %s: %s\n",text[45],per);
73 exit(1);
74 }
75
76 fgets(periodo,sizeof(periodo),fp_in);
77 fclose(fp_in);
78
79 sprintf(csort,"sort -k 4,4 -o '%s' '%s'",geral2,geral);
80 system(csort);
81
82 if((fp_in=fopen(geral2,"r"))==NULL) {
83 fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],geral2);
84 exit(1);
85 }
86
87 if((fp_ou=fopen(geral3,"w"))==NULL) {
88 fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],geral3);
89 exit(1);
90 }
91
92 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
93 getword(url,buf,' ');
94 if(strcmp(url,"TOTAL") == 0) {
95 getword(ttnacc,buf,' ');
96 getword(ttnbytes,buf,' ');
97 getword(ttntime,buf,' ');
98 continue;
99 }
100 getword(nacc,buf,' ');
101 getword(nbytes,buf,' ');
102 getword(url,buf,' ');
103 getword(ntemp,buf,' ');
104 getword(ntemp,buf,' ');
105 getword(ntemp,buf,' ');
106 getword(ntime,buf,' ');
107
108 if(!regs) {
109 strcpy(ourl,url);
110 regs++;
111 }
112
113 if(strcmp(url,ourl) != 0) {
114 my_lltoa(tnacc,val1,15);
115 my_lltoa(tnbytes,val2,15);
116 my_lltoa(tntime,val3,15);
117 sprintf(buf,"%s %s %s %s\n",val1,val2,val3,ourl);
118 fputs(buf, fp_ou);
119 strcpy(ourl,url);
120 tnacc=0;
121 tnbytes=0;
122 tntime=0;
123 }
124
125 tnacc+=my_atoll(nacc);
126 tnbytes+=my_atoll(nbytes);
127 tntime+=my_atoll(ntime);
128 }
129
130 my_lltoa(tnacc,val1,15);
131 my_lltoa(tnbytes,val2,15);
132 my_lltoa(tntime,val3,15);
133 sprintf(buf,"%s %s %s %s\n",val1,val2,val3,ourl);
134 fputs(buf, fp_ou);
135
136 fclose(fp_in);
137 fclose(fp_ou);
138 unlink(geral2);
139
140 strlow(TopsitesSortField);
141 strlow(TopsitesSortType);
142
143 if(strcmp(TopsitesSortField,"connect") == 0)
144 strcpy(sortf,"1,1");
145 if(strcmp(TopsitesSortField,"bytes") == 0)
146 strcpy(sortf,"2,2");
147 if(strcmp(TopsitesSortType,"a") == 0)
148 strcpy(sortt," ");
149 if(strcmp(TopsitesSortType,"d") == 0)
150 strcpy(sortt,"-r");
151
152 sprintf(csort,"sort %s -k %s -o '%s' '%s'",sortt,sortf,sites,geral3);
153 system(csort);
154
155 unlink(geral2);
156 unlink(geral3);
157
158 if((fp_in=fopen(sites,"r"))==NULL) {
159 fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],sites);
160 exit(1);
161 }
162
163 if((fp_ou=fopen(report,"w"))==NULL) {
164 fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],report);
165 exit(1);
166 }
167
168 regs=0;
169
170 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"",fp_ou);
171 fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_ou);
172 fputs("<html>\n",fp_ou);
173 fputs("<head>\n",fp_ou);
174 sprintf(html," <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
175 fputs(html,fp_ou);
176 css(fp_ou);
177 fputs("</head>\n",fp_ou);
178
179 sprintf(url,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
180 fputs(url,fp_ou);
181
182 if(strlen(LogoImage) > 0) {
183 fputs("<center><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
184 sprintf(url,"<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s>&nbsp;%s</th></tr>\n",LogoImage,Width,Height,LogoText);
185 fputs(url,fp_ou);
186 fputs("<tr><td height=\"5\"></td></tr>\n",fp_ou);
187 fputs("</table>\n",fp_ou);
188 }
189
190 if(strcmp(IndexTree,"date") == 0)
191 show_sarg(fp_ou,"../../..");
192 else
193 show_sarg(fp_ou, "..");
194
195 fputs("<center><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
196 sprintf(url,"<tr><th class=\"title\">%s</th></tr>\n",Title);
197 fputs(url,fp_ou);
198
199 sprintf(url,"<tr><td class=\"header3\">%s: %s</td></tr>\n",text[89],periodo);
200 fputs(url,fp_ou);
201 sprintf(url,"<tr><td class=\"header3\">%s %s %s</td></tr>\n",text[83],TopSitesNum,text[84]);
202 fputs(url,fp_ou);
203 fputs("</table></center>\n",fp_ou);
204
205 fputs("<center><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou);
206 fputs("<tr><td></td></tr>\n",fp_ou);
207 sprintf(url,"<tr><td class=\"header\">%s</td><th class=\"header\">%s</th> \
208 <th class=\"header\">%s</th><th class=\"header\">%s</th> \
209 <th class=\"header\">%s</th></tr>\n", \
210 text[100],text[91],text[92],text[93],text[99]);
211 fputs(url,fp_ou);
212
213 regs=1;
214
215 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
216 if(regs>atoi(TopSitesNum))
217 break;
218 getword(nacc,buf,' ');
219 getword(nbytes,buf,' ');
220 getword(ntime,buf,' ');
221 getword(url,buf,' ');
222
223 twork1=my_atoll(nacc);
224 twork2=my_atoll(nbytes);
225 twork3=my_atoll(ntime);
226
227 sprintf(wwork1,"%s",fixnum(twork1,1));
228 sprintf(wwork2,"%s",fixnum(twork2,1));
229 sprintf(wwork3,"%s",fixnum(twork3,1));
230
231 if(strlen(BlockIt) > 0)
232 sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"../images/sarg-squidguard-block.png\" border=\"0\"></a>&nbsp;",wwwDocumentRoot,BlockIt,url);
233 else BlockImage[0]='\0';
234
235
236 sprintf(ourl,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\"><font class=\"link\">%s</font></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);
237 fputs(ourl,fp_ou);
238 regs++;
239 }
240
241
242 fputs("</table></center>\n",fp_ou);
243
244 show_info(fp_ou);
245
246 fputs("</body>\n</html>\n",fp_ou);
247
248 fclose(fp_in);
249 fclose(fp_ou);
250
251 return;
252
253 }