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