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