]> git.ipfire.org Git - thirdparty/sarg.git/blob - topsites.c
Fix some translations
[thirdparty/sarg.git] / topsites.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2011
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 #ifdef ENABLE_DOUBLE_CHECK_DATA
31 extern struct globalstatstruct globstat;
32 #endif
33
34 void topsites(void)
35 {
36 FILE *fp_in, *fp_ou;
37
38 char *buf;
39 char *url;
40 char *ourl=NULL;
41 char csort[255];
42 char general[MAXLEN];
43 char general2[MAXLEN];
44 char general3[MAXLEN];
45 char sites[MAXLEN];
46 char report[MAXLEN];
47 const char *sortf;
48 const char *sortt;
49 long long int nacc;
50 long long int nbytes;
51 long long int ntime;
52 long long int tnacc=0;
53 long long int tnbytes=0;
54 long long int tntime=0;
55 long long int twork1=0, twork2=0, twork3=0;
56 #ifdef ENABLE_DOUBLE_CHECK_DATA
57 long long int ttnacc=0;
58 long long int ttnbytes=0;
59 long long int ttntime=0;
60 #endif
61 int regs=0;
62 int cstatus;
63 int url_len;
64 int ourl_size=0;
65 struct getwordstruct gwarea;
66 longline line;
67 struct generalitemstruct item;
68
69 if(Privacy) {
70 if (debugz) debugaz(_("Top sites report not produced because privacy option is on\n"));
71 return;
72 }
73
74 sprintf(general,"%s/sarg-general",outdirname);
75 sprintf(sites,"%s/sarg-sites",outdirname);
76 sprintf(general2,"%s/sarg-general2",outdirname);
77 sprintf(general3,"%s/sarg-general3",outdirname);
78
79 if ((ReportType & REPORT_TYPE_TOPUSERS) == 0)
80 sprintf(report,"%s/index.html",outdirname);
81 else
82 sprintf(report,"%s/topsites.html",outdirname);
83
84 if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 4,4 -o \"%s\" \"%s\"",general2,general)>=sizeof(csort)) {
85 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general,general2);
86 exit(EXIT_FAILURE);
87 }
88 cstatus=system(csort);
89 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
90 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
91 debuga(_("sort command: %s\n"),csort);
92 exit(EXIT_FAILURE);
93 }
94
95 if((fp_in=fopen(general2,"r"))==NULL) {
96 debuga(_("(topsites) Cannot open log file %s\n"),general2);
97 debuga(_("sort command: %s\n"),csort);
98 exit(EXIT_FAILURE);
99 }
100
101 if((fp_ou=fopen(general3,"w"))==NULL) {
102 debuga(_("(topsites) Cannot open log file %s\n"),general3);
103 exit(EXIT_FAILURE);
104 }
105
106 if ((line=longline_create())==NULL) {
107 debuga(_("Not enough memory to read file %s\n"),general2);
108 exit(EXIT_FAILURE);
109 }
110
111 while((buf=longline_read(fp_in,line))!=NULL) {
112 ger_read(buf,&item,general2);
113 if(item.total) continue;
114
115 if(!regs) {
116 url_len=strlen(item.url);
117 if (!ourl || url_len>=ourl_size) {
118 ourl_size=url_len+1;
119 ourl=realloc(ourl,ourl_size);
120 if (!ourl) {
121 debuga(_("Not enough memory to store the url\n"));
122 exit(EXIT_FAILURE);
123 }
124 }
125 strcpy(ourl,item.url);
126 regs++;
127 }
128
129 if(strcmp(item.url,ourl) != 0) {
130 /*
131 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
132 to print a long long int unless it is exactly 64-bits long.
133 */
134 fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl);
135 url_len=strlen(item.url);
136 if (url_len>=ourl_size) {
137 ourl_size=url_len+1;
138 ourl=realloc(ourl,ourl_size);
139 if (!ourl) {
140 debuga(_("Not enough memory to store the url\n"));
141 exit(EXIT_FAILURE);
142 }
143 }
144 strcpy(ourl,item.url);
145 tnacc=0;
146 tnbytes=0;
147 tntime=0;
148 }
149
150 tnacc+=item.nacc;
151 tnbytes+=item.nbytes;
152 tntime+=item.nelap;
153 #ifdef ENABLE_DOUBLE_CHECK_DATA
154 ttnacc+=item.nacc;
155 ttnbytes+=item.nbytes;
156 ttntime+=item.nelap;
157 #endif
158 }
159 fclose(fp_in);
160 longline_destroy(&line);
161
162 if (ourl) {
163 /*
164 This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
165 to print a long long int unless it is exactly 64-bits long.
166 */
167 fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl);
168 free(ourl);
169 }
170
171 fclose(fp_ou);
172
173 #ifdef ENABLE_DOUBLE_CHECK_DATA
174 if (ttnacc!=globstat.nacc || ttnbytes!=globstat.nbytes || ttntime!=globstat.elap) {
175 debuga(_("Total statistics mismatch when reading %s to produce the top sites\n"),general2);
176 exit(EXIT_FAILURE);
177 }
178 #endif
179
180 if (unlink(general2)) {
181 debuga(_("Cannot delete %s - %s\n"),general2,strerror(errno));
182 exit(EXIT_FAILURE);
183 }
184
185 if((TopsitesSort & TOPSITE_SORT_CONNECT) != 0) {
186 sortf="-k 1,1 -k 2,2";
187 } else if((TopsitesSort & TOPSITE_SORT_BYTES) != 0) {
188 sortf="-k 2,2 -k 1,1";
189 } else if((TopsitesSort & TOPSITE_SORT_TIME) != 0) {
190 sortf="-k 3,3";
191 } else {
192 sortf="-k 2,2 -k 1,1"; //default is BYTES
193 }
194 if((TopsitesSort & TOPSITE_SORT_REVERSE) != 0) {
195 sortt="-r";
196 } else {
197 sortt="";
198 }
199
200 if (snprintf(csort,sizeof(csort),"sort -t \"\t\" %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3)>=sizeof(csort)) {
201 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general3,sites);
202 exit(EXIT_FAILURE);
203 }
204 cstatus=system(csort);
205 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
206 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
207 debuga(_("sort command: %s\n"),csort);
208 exit(EXIT_FAILURE);
209 }
210 if((fp_in=fopen(sites,"r"))==NULL) {
211 debuga(_("(topsites) Cannot open log file %s\n"),sites);
212 debuga(_("sort command: %s\n"),csort);
213 exit(EXIT_FAILURE);
214 }
215
216 if (unlink(general3)) {
217 debuga(_("Cannot delete %s - %s\n"),general3,strerror(errno));
218 exit(EXIT_FAILURE);
219 }
220
221 if((fp_ou=fopen(report,"w"))==NULL) {
222 debuga(_("(topsites) Cannot open log file %s\n"),report);
223 exit(EXIT_FAILURE);
224 }
225
226 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"),HTML_JS_SORTTABLE);
227 fputs("<tr><td class=\"header_c\">",fp_ou);
228 fprintf(fp_ou,_("Period: %s"),period.html);
229 fputs("</td></tr>\n",fp_ou);
230 fputs("<tr><th class=\"header_c\">",fp_ou);
231 fprintf(fp_ou,_("Top %d sites"),TopSitesNum);
232 fputs("</th></tr>\n",fp_ou);
233 close_html_header(fp_ou);
234
235 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_ou);
236 if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou);
237 fputs(">\n",fp_ou);
238 fprintf(fp_ou,"<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l",_("NUM"));
239 if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou);
240 fprintf(fp_ou,"\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr></thead>\n",_("ACCESSED SITE"),_("CONNECT"),_("BYTES"),_("TIME"));
241
242 regs=0;
243 ntopsites = 0;
244
245 if ((line=longline_create())==NULL) {
246 debuga(_("Not enough memory to read file %s\n"),sites);
247 exit(EXIT_FAILURE);
248 }
249
250 while(regs<TopSitesNum && (buf=longline_read(fp_in,line))!=NULL) {
251 getword_start(&gwarea,buf);
252 if (getword_atoll(&nacc,&gwarea,'\t')<0) {
253 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
254 exit(EXIT_FAILURE);
255 }
256 if (nacc == 0) continue;
257 if (getword_atoll(&nbytes,&gwarea,'\t')<0 || getword_atoll(&ntime,&gwarea,'\t')<0) {
258 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
259 exit(EXIT_FAILURE);
260 }
261 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
262 debuga(_("The url is invalid in file %s\n"),sites);
263 exit(EXIT_FAILURE);
264 }
265
266 twork1=nacc;
267 twork2=nbytes;
268 twork3=ntime;
269
270 fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",++regs);
271
272 if(BlockIt[0] != '\0' && url[0]!=ALIAS_PREFIX) {
273 fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt);
274 output_html_url(fp_ou,url);
275 fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
276 }
277
278 output_html_link(fp_ou,url,100);
279 fputs("</td><td class=\"data\"",fp_ou);
280 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork1);
281 fprintf(fp_ou,">%s</td>",fixnum(twork1,1));
282 fputs("<td class=\"data\"",fp_ou);
283 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork2);
284 fprintf(fp_ou,">%s</td>",fixnum(twork2,1));
285 fputs("<td class=\"data\"",fp_ou);
286 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork3);
287 fprintf(fp_ou,">%s</td></tr>\n",fixtime(twork3));
288 }
289 fclose(fp_in);
290 longline_destroy(&line);
291
292 fputs("</table></div>\n",fp_ou);
293 if (write_html_trailer(fp_ou)<0)
294 debuga(_("Write error in file %s\n"),report);
295 if (fclose(fp_ou)==EOF)
296 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
297
298 return;
299 }