]> git.ipfire.org Git - thirdparty/sarg.git/blame - topsites.c
Messages ported to gettext. Only French translation exists
[thirdparty/sarg.git] / topsites.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
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"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
32e71fa4 30void topsites(void)
25697a35
GS
31{
32
33 FILE *fp_in, *fp_ou;
12afa4e6 34
ac422f9b 35 char *buf;
e5b2c6f0
FM
36 char *url;
37 char *ourl=NULL;
25697a35 38 char csort[255];
d6e703cc
FM
39 char general[MAXLEN];
40 char general2[MAXLEN];
41 char general3[MAXLEN];
25697a35
GS
42 char per[MAXLEN];
43 char sites[MAXLEN];
44 char report[MAXLEN];
d6e703cc 45 char period[100];
2240dcea
FM
46 const char *sortf;
47 const char *sortt;
6e792ade
FM
48 long long int nacc;
49 long long int nbytes;
50 long long int ntime;
25697a35
GS
51 long long int tnacc=0;
52 long long int tnbytes=0;
53 long long int tntime=0;
54 long long int twork1=0, twork2=0, twork3=0;
55 int regs=0;
456d78a5 56 int cstatus;
e5b2c6f0
FM
57 int url_len;
58 int ourl_size=0;
9c7c6346 59 struct getwordstruct gwarea;
ac422f9b 60 struct longlinestruct line;
2240dcea 61 struct generalitemstruct item;
25697a35 62
e6414a9d 63 if(Privacy)
d6e703cc 64 return;
25697a35 65
d5d021c5
FM
66 sprintf(general,"%s/sarg-general",outdirname);
67 sprintf(sites,"%s/sarg-sites",outdirname);
68 sprintf(general2,"%s/sarg-general2",outdirname);
69 sprintf(general3,"%s/sarg-general3",outdirname);
70 sprintf(per,"%s/sarg-period",outdirname);
491b862f 71
085c1e1f 72 if ((ReportType & REPORT_TYPE_TOPUSERS) == 0)
d5d021c5 73 sprintf(report,"%s/index.html",outdirname);
491b862f 74 else
d5d021c5 75 sprintf(report,"%s/topsites.html",outdirname);
25697a35
GS
76
77 if ((fp_in = fopen(per, "r")) == 0) {
fcdc0918 78 debuga(_("(topsites) Cannot open file %s\n"),per);
25697a35
GS
79 exit(1);
80 }
81
05b90947 82 if (!fgets(period,sizeof(period),fp_in)) {
d574e592 83 debuga(_("(topsites) read error in %s\n"),per);
05b90947
FM
84 exit(1);
85 }
25697a35
GS
86 fclose(fp_in);
87
9a2efbd0 88 sprintf(csort,"sort -k 4,4 -o \"%s\" \"%s\"",general2,general);
456d78a5
FM
89 cstatus=system(csort);
90 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
d5d021c5
FM
91 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
92 debuga(_("sort command: %s\n"),csort);
456d78a5
FM
93 exit(1);
94 }
25697a35 95
d6e703cc 96 if((fp_in=fopen(general2,"r"))==NULL) {
d574e592 97 debuga(_("(topsites) Cannot open log file %s\n"),general2);
d5d021c5 98 debuga(_("sort command: %s\n"),csort);
25697a35
GS
99 exit(1);
100 }
101
d6e703cc 102 if((fp_ou=fopen(general3,"w"))==NULL) {
d574e592 103 debuga(_("(topsites) Cannot open log file %s\n"),general3);
25697a35
GS
104 exit(1);
105 }
106
ac422f9b 107 if (longline_prepare(&line)<0) {
10210234 108 debuga(_("Not enough memory to read file %s\n"),general2);
ac422f9b
FM
109 exit(1);
110 }
111
112 while((buf=longline_read(fp_in,&line))!=NULL) {
2240dcea
FM
113 ger_read(buf,&item,general2);
114 if(item.total) continue;
25697a35
GS
115
116 if(!regs) {
2240dcea 117 url_len=strlen(item.url);
e5b2c6f0
FM
118 if (!ourl || url_len>=ourl_size) {
119 ourl_size=url_len+1;
120 ourl=realloc(ourl,ourl_size);
121 if (!ourl) {
10210234 122 debuga(_("Not enough memory to store the url\n"));
e5b2c6f0
FM
123 exit(1);
124 }
125 }
2240dcea 126 strcpy(ourl,item.url);
25697a35
GS
127 regs++;
128 }
129
2240dcea
FM
130 if(strcmp(item.url,ourl) != 0) {
131 fprintf(fp_ou,"%lld\t%lld\t%lld\t%s\n",tnacc,tnbytes,tntime,ourl);
132 url_len=strlen(item.url);
e5b2c6f0
FM
133 if (url_len>=ourl_size) {
134 ourl_size=url_len+1;
135 ourl=realloc(ourl,ourl_size);
136 if (!ourl) {
10210234 137 debuga(_("Not enough memory to store the url\n"));
e5b2c6f0
FM
138 exit(1);
139 }
140 }
2240dcea 141 strcpy(ourl,item.url);
25697a35
GS
142 tnacc=0;
143 tnbytes=0;
144 tntime=0;
145 }
146
2240dcea
FM
147 tnacc+=item.nacc;
148 tnbytes+=item.nbytes;
149 tntime+=item.nelap;
25697a35 150 }
ac422f9b
FM
151 fclose(fp_in);
152 unlink(general2);
153 longline_free(&line);
25697a35 154
e5b2c6f0 155 if (ourl) {
2240dcea 156 fprintf(fp_ou,"%lld\t%lld\t%lld\t%s\n",tnacc,tnbytes,tntime,ourl);
e5b2c6f0
FM
157 free(ourl);
158 }
25697a35 159
25697a35 160 fclose(fp_ou);
25697a35
GS
161
162 strlow(TopsitesSortField);
163 strlow(TopsitesSortType);
164
165 if(strcmp(TopsitesSortField,"connect") == 0)
2240dcea
FM
166 sortf="-k 1,1 -k 2,2";
167 else if(strcmp(TopsitesSortField,"bytes") == 0)
168 sortf="-k 2,2 -k 1,1";
169 else
170 sortf="";
25697a35 171 if(strcmp(TopsitesSortType,"a") == 0)
2240dcea
FM
172 sortt="";
173 else if(strcmp(TopsitesSortType,"d") == 0)
174 sortt="-r";
175 else
176 sortt="";
25697a35 177
2240dcea 178 sprintf(csort,"sort %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3);
456d78a5
FM
179 cstatus=system(csort);
180 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
d574e592
FM
181 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
182 debuga(_("sort command: %s\n"),csort);
456d78a5
FM
183 exit(1);
184 }
185 if((fp_in=fopen(sites,"r"))==NULL) {
d574e592
FM
186 debuga(_("(topsites) Cannot open log file %s\n"),sites);
187 debuga(_("sort command: %s\n"),csort);
456d78a5
FM
188 exit(1);
189 }
25697a35 190
d6e703cc
FM
191 unlink(general2);
192 unlink(general3);
25697a35 193
25697a35 194 if((fp_ou=fopen(report,"w"))==NULL) {
d574e592 195 debuga(_("(topsites) Cannot open log file %s\n"),report);
25697a35
GS
196 exit(1);
197 }
198
c0ec9cc7 199 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"));
d574e592
FM
200 fputs("<tr><td class=\"header_c\">",fp_ou);
201 fprintf(fp_ou,_("Period: %s"),period);
202 fputs("</td></tr>\n",fp_ou);
203 fputs("<tr><th class=\"header_c\">",fp_ou);
204 fprintf(fp_ou,_("Top %d sites"),TopSitesNum);
205 fputs("</th></tr>\n",fp_ou);
c0ec9cc7 206 close_html_header(fp_ou);
25697a35 207
c0ec9cc7 208 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou);
25697a35 209 fputs("<tr><td></td></tr>\n",fp_ou);
c36c7384 210 fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("NUM"),_("ACCESSED SITE"),_("CONNECT"),_("BYTES"),_("TIME"));
25697a35 211
c0ec9cc7 212 regs=0;
d6e703cc 213 ntopsites = 0;
25697a35 214
ac422f9b 215 if (longline_prepare(&line)<0) {
10210234 216 debuga(_("Not enough memory to read file %s\n"),sites);
ac422f9b
FM
217 exit(1);
218 }
219
220 while(regs<TopSitesNum && (buf=longline_read(fp_in,&line))!=NULL) {
9c7c6346 221 getword_start(&gwarea,buf);
6e792ade 222 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
d574e592 223 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
4bcb77cf
FM
224 exit(1);
225 }
6e792ade 226 if (nacc == 0) continue;
e5b2c6f0 227 if (getword_atoll(&nbytes,&gwarea,'\t')<0 || getword_atoll(&ntime,&gwarea,'\t')<0) {
d574e592 228 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
4bcb77cf
FM
229 exit(1);
230 }
e5b2c6f0 231 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
10210234 232 debuga(_("The url is invalid in file %s\n"),sites);
e5b2c6f0
FM
233 exit(1);
234 }
25697a35 235
6e792ade
FM
236 twork1=nacc;
237 twork2=nbytes;
238 twork3=ntime;
25697a35 239
48864d28
FM
240 strcpy(wwork1,fixnum(twork1,1));
241 strcpy(wwork2,fixnum(twork2,1));
242 strcpy(wwork3,fixtime(twork3));
25697a35 243
e5b2c6f0 244 fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2 link\">",++regs);
25697a35 245
ac422f9b
FM
246 if(BlockIt[0] != '\0') {
247 fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt);
248 output_html_url(fp_ou,url);
249 fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
250 }
25697a35 251
ac422f9b
FM
252 fputs("<a href=\"http://",fp_ou);
253 output_html_url(fp_ou,url);
254 fputs("\">",fp_ou);
255 output_html_string(fp_ou,url,100);
256 fprintf(fp_ou,"</a></td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",wwork1,wwork2,wwork3);
25697a35 257 }
25697a35 258 fclose(fp_in);
ac422f9b 259 longline_free(&line);
c0ec9cc7
FM
260
261 fputs("</table></div>\n",fp_ou);
262 write_html_trailer(fp_ou);
25697a35
GS
263 fclose(fp_ou);
264
265 return;
266
267}