]>
Commit | Line | Data |
---|---|---|
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 | 30 | void 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 sites[MAXLEN]; |
43 | char report[MAXLEN]; | |
2240dcea FM |
44 | const char *sortf; |
45 | const char *sortt; | |
6e792ade FM |
46 | long long int nacc; |
47 | long long int nbytes; | |
48 | long long int ntime; | |
25697a35 GS |
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; | |
456d78a5 | 54 | int cstatus; |
e5b2c6f0 FM |
55 | int url_len; |
56 | int ourl_size=0; | |
9c7c6346 | 57 | struct getwordstruct gwarea; |
afaa3b67 | 58 | longline line; |
2240dcea | 59 | struct generalitemstruct item; |
25697a35 | 60 | |
e6414a9d | 61 | if(Privacy) |
d6e703cc | 62 | return; |
25697a35 | 63 | |
d5d021c5 FM |
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); | |
491b862f | 68 | |
085c1e1f | 69 | if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) |
d5d021c5 | 70 | sprintf(report,"%s/index.html",outdirname); |
491b862f | 71 | else |
d5d021c5 | 72 | sprintf(report,"%s/topsites.html",outdirname); |
25697a35 | 73 | |
9a2efbd0 | 74 | sprintf(csort,"sort -k 4,4 -o \"%s\" \"%s\"",general2,general); |
456d78a5 FM |
75 | cstatus=system(csort); |
76 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
d5d021c5 FM |
77 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
78 | debuga(_("sort command: %s\n"),csort); | |
06b39c87 | 79 | exit(EXIT_FAILURE); |
456d78a5 | 80 | } |
25697a35 | 81 | |
d6e703cc | 82 | if((fp_in=fopen(general2,"r"))==NULL) { |
d574e592 | 83 | debuga(_("(topsites) Cannot open log file %s\n"),general2); |
d5d021c5 | 84 | debuga(_("sort command: %s\n"),csort); |
06b39c87 | 85 | exit(EXIT_FAILURE); |
25697a35 GS |
86 | } |
87 | ||
d6e703cc | 88 | if((fp_ou=fopen(general3,"w"))==NULL) { |
d574e592 | 89 | debuga(_("(topsites) Cannot open log file %s\n"),general3); |
06b39c87 | 90 | exit(EXIT_FAILURE); |
25697a35 GS |
91 | } |
92 | ||
afaa3b67 | 93 | if ((line=longline_create())==NULL) { |
10210234 | 94 | debuga(_("Not enough memory to read file %s\n"),general2); |
06b39c87 | 95 | exit(EXIT_FAILURE); |
ac422f9b FM |
96 | } |
97 | ||
afaa3b67 | 98 | while((buf=longline_read(fp_in,line))!=NULL) { |
2240dcea FM |
99 | ger_read(buf,&item,general2); |
100 | if(item.total) continue; | |
25697a35 GS |
101 | |
102 | if(!regs) { | |
2240dcea | 103 | url_len=strlen(item.url); |
e5b2c6f0 FM |
104 | if (!ourl || url_len>=ourl_size) { |
105 | ourl_size=url_len+1; | |
106 | ourl=realloc(ourl,ourl_size); | |
107 | if (!ourl) { | |
10210234 | 108 | debuga(_("Not enough memory to store the url\n")); |
06b39c87 | 109 | exit(EXIT_FAILURE); |
e5b2c6f0 FM |
110 | } |
111 | } | |
2240dcea | 112 | strcpy(ourl,item.url); |
25697a35 GS |
113 | regs++; |
114 | } | |
115 | ||
2240dcea FM |
116 | if(strcmp(item.url,ourl) != 0) { |
117 | fprintf(fp_ou,"%lld\t%lld\t%lld\t%s\n",tnacc,tnbytes,tntime,ourl); | |
118 | url_len=strlen(item.url); | |
e5b2c6f0 FM |
119 | if (url_len>=ourl_size) { |
120 | ourl_size=url_len+1; | |
121 | ourl=realloc(ourl,ourl_size); | |
122 | if (!ourl) { | |
10210234 | 123 | debuga(_("Not enough memory to store the url\n")); |
06b39c87 | 124 | exit(EXIT_FAILURE); |
e5b2c6f0 FM |
125 | } |
126 | } | |
2240dcea | 127 | strcpy(ourl,item.url); |
25697a35 GS |
128 | tnacc=0; |
129 | tnbytes=0; | |
130 | tntime=0; | |
131 | } | |
132 | ||
2240dcea FM |
133 | tnacc+=item.nacc; |
134 | tnbytes+=item.nbytes; | |
135 | tntime+=item.nelap; | |
25697a35 | 136 | } |
ac422f9b FM |
137 | fclose(fp_in); |
138 | unlink(general2); | |
afaa3b67 | 139 | longline_destroy(&line); |
25697a35 | 140 | |
e5b2c6f0 | 141 | if (ourl) { |
2240dcea | 142 | fprintf(fp_ou,"%lld\t%lld\t%lld\t%s\n",tnacc,tnbytes,tntime,ourl); |
e5b2c6f0 FM |
143 | free(ourl); |
144 | } | |
25697a35 | 145 | |
25697a35 | 146 | fclose(fp_ou); |
25697a35 GS |
147 | |
148 | strlow(TopsitesSortField); | |
149 | strlow(TopsitesSortType); | |
150 | ||
151 | if(strcmp(TopsitesSortField,"connect") == 0) | |
2240dcea FM |
152 | sortf="-k 1,1 -k 2,2"; |
153 | else if(strcmp(TopsitesSortField,"bytes") == 0) | |
154 | sortf="-k 2,2 -k 1,1"; | |
155 | else | |
156 | sortf=""; | |
25697a35 | 157 | if(strcmp(TopsitesSortType,"a") == 0) |
2240dcea FM |
158 | sortt=""; |
159 | else if(strcmp(TopsitesSortType,"d") == 0) | |
160 | sortt="-r"; | |
161 | else | |
162 | sortt=""; | |
25697a35 | 163 | |
2240dcea | 164 | sprintf(csort,"sort %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3); |
456d78a5 FM |
165 | cstatus=system(csort); |
166 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
d574e592 FM |
167 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
168 | debuga(_("sort command: %s\n"),csort); | |
06b39c87 | 169 | exit(EXIT_FAILURE); |
456d78a5 FM |
170 | } |
171 | if((fp_in=fopen(sites,"r"))==NULL) { | |
d574e592 FM |
172 | debuga(_("(topsites) Cannot open log file %s\n"),sites); |
173 | debuga(_("sort command: %s\n"),csort); | |
06b39c87 | 174 | exit(EXIT_FAILURE); |
456d78a5 | 175 | } |
25697a35 | 176 | |
d6e703cc FM |
177 | unlink(general2); |
178 | unlink(general3); | |
25697a35 | 179 | |
25697a35 | 180 | if((fp_ou=fopen(report,"w"))==NULL) { |
d574e592 | 181 | debuga(_("(topsites) Cannot open log file %s\n"),report); |
06b39c87 | 182 | exit(EXIT_FAILURE); |
25697a35 GS |
183 | } |
184 | ||
c0ec9cc7 | 185 | write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites")); |
d574e592 | 186 | fputs("<tr><td class=\"header_c\">",fp_ou); |
fa6552b0 | 187 | fprintf(fp_ou,_("Period: %s"),period.text); |
d574e592 FM |
188 | fputs("</td></tr>\n",fp_ou); |
189 | fputs("<tr><th class=\"header_c\">",fp_ou); | |
190 | fprintf(fp_ou,_("Top %d sites"),TopSitesNum); | |
191 | fputs("</th></tr>\n",fp_ou); | |
c0ec9cc7 | 192 | close_html_header(fp_ou); |
25697a35 | 193 | |
c0ec9cc7 | 194 | fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou); |
25697a35 | 195 | fputs("<tr><td></td></tr>\n",fp_ou); |
c36c7384 | 196 | 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 | 197 | |
c0ec9cc7 | 198 | regs=0; |
d6e703cc | 199 | ntopsites = 0; |
25697a35 | 200 | |
afaa3b67 | 201 | if ((line=longline_create())==NULL) { |
10210234 | 202 | debuga(_("Not enough memory to read file %s\n"),sites); |
06b39c87 | 203 | exit(EXIT_FAILURE); |
ac422f9b FM |
204 | } |
205 | ||
afaa3b67 | 206 | while(regs<TopSitesNum && (buf=longline_read(fp_in,line))!=NULL) { |
9c7c6346 | 207 | getword_start(&gwarea,buf); |
6e792ade | 208 | if (getword_atoll(&nacc,&gwarea,'\t')<0) { |
d574e592 | 209 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites); |
06b39c87 | 210 | exit(EXIT_FAILURE); |
4bcb77cf | 211 | } |
6e792ade | 212 | if (nacc == 0) continue; |
e5b2c6f0 | 213 | if (getword_atoll(&nbytes,&gwarea,'\t')<0 || getword_atoll(&ntime,&gwarea,'\t')<0) { |
d574e592 | 214 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites); |
06b39c87 | 215 | exit(EXIT_FAILURE); |
4bcb77cf | 216 | } |
e5b2c6f0 | 217 | if (getword_ptr(buf,&url,&gwarea,'\t')<0) { |
10210234 | 218 | debuga(_("The url is invalid in file %s\n"),sites); |
06b39c87 | 219 | exit(EXIT_FAILURE); |
e5b2c6f0 | 220 | } |
25697a35 | 221 | |
6e792ade FM |
222 | twork1=nacc; |
223 | twork2=nbytes; | |
224 | twork3=ntime; | |
25697a35 | 225 | |
48864d28 FM |
226 | strcpy(wwork1,fixnum(twork1,1)); |
227 | strcpy(wwork2,fixnum(twork2,1)); | |
228 | strcpy(wwork3,fixtime(twork3)); | |
25697a35 | 229 | |
e5b2c6f0 | 230 | fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2 link\">",++regs); |
25697a35 | 231 | |
ac422f9b FM |
232 | if(BlockIt[0] != '\0') { |
233 | fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt); | |
234 | output_html_url(fp_ou,url); | |
235 | fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",fp_ou); | |
236 | } | |
25697a35 | 237 | |
ac422f9b FM |
238 | fputs("<a href=\"http://",fp_ou); |
239 | output_html_url(fp_ou,url); | |
240 | fputs("\">",fp_ou); | |
241 | output_html_string(fp_ou,url,100); | |
242 | 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 | 243 | } |
25697a35 | 244 | fclose(fp_in); |
afaa3b67 | 245 | longline_destroy(&line); |
c0ec9cc7 FM |
246 | |
247 | fputs("</table></div>\n",fp_ou); | |
fa6552b0 FM |
248 | if (write_html_trailer(fp_ou)<0) |
249 | debuga(_("Write error in file %s\n"),report); | |
250 | if (fclose(fp_ou)==EOF) | |
251 | debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); | |
25697a35 GS |
252 | |
253 | return; | |
254 | ||
255 | } |