]> git.ipfire.org Git - thirdparty/sarg.git/blame - download.c
Output the period as an HTML text in the HTML reports
[thirdparty/sarg.git] / download.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
2824ec9b
FM
30/*@null@*/static char *DownloadSuffix=NULL;
31/*@null@*/static char **DownloadSuffixIndex=NULL;
6e792ade
FM
32static int NDownloadSuffix=0;
33
32e71fa4 34void download_report(void)
25697a35
GS
35{
36
37 FILE *fp_in = NULL, *fp_ou = NULL;
4157aa09 38
ac422f9b 39 char *buf;
e5b2c6f0 40 char *url;
25697a35 41 char report_in[MAXLEN];
25697a35 42 char report[MAXLEN];
25697a35
GS
43 char ip[MAXLEN];
44 char oip[MAXLEN];
45 char user[MAXLEN];
46 char ouser[MAXLEN];
47 char ouser2[MAXLEN];
48 char data[15];
49 char hora[15];
25697a35
GS
50 int z=0;
51 int count=0;
dfb337be 52 int i;
2824ec9b 53 bool new_user;
9c7c6346 54 struct getwordstruct gwarea;
afaa3b67 55 longline line;
f2ec8c75 56 struct userinfostruct *uinfo;
25697a35
GS
57
58 ouser[0]='\0';
4157aa09 59 ouser2[0]='\0';
25697a35 60
2824ec9b 61 snprintf(report_in,sizeof(report_in),"%s/sarg/download.log",TempDir);
25697a35
GS
62 if(access(report_in, R_OK) != 0)
63 return;
64
2824ec9b 65 snprintf(report,sizeof(report),"%s/download.html",outdirname);
25697a35 66
936c9905 67 if((fp_in=MY_FOPEN(report_in,"r"))==NULL) {
9f70c14e 68 debuga(_("(download) Cannot open log file %s\n"),report_in);
06b39c87 69 exit(EXIT_FAILURE);
25697a35
GS
70 }
71
936c9905 72 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
9f70c14e 73 debuga(_("(download) Cannot open log file %s\n"),report);
06b39c87 74 exit(EXIT_FAILURE);
25697a35
GS
75 }
76
c0ec9cc7 77 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Downloads"));
fa6552b0 78 fputs("<tr><td class=\"header_l\">",fp_ou);
fd46f082 79 fprintf(fp_ou,_("Period: %s"),period.html);
fa6552b0 80 fputs("</td></tr>\n",fp_ou);
c36c7384 81 fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Downloads"));
c0ec9cc7 82 close_html_header(fp_ou);
25697a35 83
c0ec9cc7 84 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
25697a35 85 fputs("<tr><td></td></tr>\n",fp_ou);
c36c7384 86 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></tr>\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"));
25697a35 87
afaa3b67 88 if ((line=longline_create())==NULL) {
10210234 89 debuga(_("Not enough memory to read the downloaded files\n"));
06b39c87 90 exit(EXIT_FAILURE);
ac422f9b
FM
91 }
92
afaa3b67 93 while((buf=longline_read(fp_in,line))!=NULL) {
9c7c6346
FM
94 getword_start(&gwarea,buf);
95 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
e5b2c6f0 96 getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
10210234 97 debuga(_("There is a broken record or garbage in file %s\n"),report_in);
06b39c87 98 exit(EXIT_FAILURE);
e5b2c6f0
FM
99 }
100 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
10210234 101 debuga(_("There is a broken url in file %s\n"),report_in);
06b39c87 102 exit(EXIT_FAILURE);
4bcb77cf 103 }
dfb337be 104
f2ec8c75
FM
105 uinfo=userinfo_find_from_id(user);
106 if (!uinfo) {
10210234 107 debuga(_("Unknown user ID %s in file %s\n"),user,report_in);
06b39c87 108 exit(EXIT_FAILURE);
25697a35 109 }
2824ec9b 110 new_user=false;
25697a35
GS
111 if(!z) {
112 strcpy(ouser,user);
113 strcpy(oip,ip);
114 z++;
2824ec9b 115 new_user=true;
25697a35 116 } else {
f2ec8c75 117 if(strcmp(ouser,user) != 0) {
25697a35 118 strcpy(ouser,user);
2824ec9b 119 new_user=true;
f2ec8c75
FM
120 }
121 if(strcmp(oip,ip) != 0) {
25697a35 122 strcpy(oip,ip);
2824ec9b 123 new_user=true;
f2ec8c75 124 }
94ff9470
GS
125 }
126
127 if(DownloadReportLimit) {
f2ec8c75 128 if(strcmp(ouser2,uinfo->label) == 0) {
4157aa09 129 count++;
25697a35
GS
130 } else {
131 count=1;
f2ec8c75 132 strcpy(ouser2,uinfo->label);
25697a35 133 }
94ff9470 134 if(count >= DownloadReportLimit)
25697a35
GS
135 continue;
136 }
137
dfb337be 138 for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) url[i]=0;
25697a35 139
f2ec8c75
FM
140 fputs("<tr>",fp_ou);
141 if (new_user)
142 fprintf(fp_ou,"<td class=\"data\">%s</td><td class=\"data\">%s</td>",uinfo->label,ip);
143 else
144 fputs("<td class=\"data\"></td><td class=\"data\"></td>",fp_ou);
145 fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">",data,hora);
ac422f9b
FM
146 if(BlockIt[0]!='\0') {
147 fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt);
148 output_html_url(fp_ou,url);
149 fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a>&nbsp;",ImageFile);
150 }
f2ec8c75 151 fputs("<a href=\"http://",fp_ou);
ac422f9b 152 output_html_url(fp_ou,url);
f2ec8c75 153 fputs("\">http://",fp_ou);
ac422f9b 154 output_html_string(fp_ou,url,100);
dfb337be 155 fputs("</a></td></tr>\n",fp_ou);
25697a35 156 }
25697a35 157 fclose(fp_in);
afaa3b67 158 longline_destroy(&line);
c0ec9cc7
FM
159
160 fputs("</table></div>\n",fp_ou);
fa6552b0
FM
161 if (write_html_trailer(fp_ou)<0)
162 debuga(_("Write error in file %s\n"),report);
163 if (fclose(fp_ou)==EOF)
164 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
25697a35
GS
165
166 unlink(report_in);
167
168 return;
169}
6e792ade
FM
170
171void free_download(void)
172{
173 if (DownloadSuffix) {
174 free(DownloadSuffix);
175 DownloadSuffix=NULL;
176 }
177 if (DownloadSuffixIndex) {
178 free(DownloadSuffixIndex);
179 DownloadSuffixIndex=NULL;
180 }
181 NDownloadSuffix=0;
182}
183
184void set_download_suffix(const char *list)
185{
186 char *str;
187 int i, j, k;
188 int cmp;
189
190 free_download();
191
192 DownloadSuffix=strdup(list);
193 if (!DownloadSuffix) {
9f70c14e 194 debuga(_("Download suffix list too long\n"));
06b39c87 195 exit(EXIT_FAILURE);
6e792ade
FM
196 }
197 j = 1;
198 for (i=0 ; list[i] ; i++)
199 if (list[i] == ',') j++;
200 DownloadSuffixIndex=malloc(j*sizeof(char *));
201 if (!DownloadSuffixIndex) {
9f70c14e 202 debuga(_("Too many download suffixes\n"));
06b39c87 203 exit(EXIT_FAILURE);
6e792ade
FM
204 }
205
206 str = DownloadSuffix;
207 for (i=0 ; DownloadSuffix[i] ; i++) {
208 if (DownloadSuffix[i] == ',') {
209 DownloadSuffix[i] = '\0';
210 if (*str) {
fabbc7cc 211 cmp = -1;
6e792ade
FM
212 for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++);
213 if (cmp != 0) {
214 for (k=NDownloadSuffix ; k>j ; k--)
215 DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1];
216 NDownloadSuffix++;
217 DownloadSuffixIndex[j]=str;
218 }
219 }
220 str=DownloadSuffix+i+1;
221 }
222 }
223
224 if (*str) {
fabbc7cc 225 cmp = -1;
6e792ade
FM
226 for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++);
227 if (cmp != 0) {
228 for (k=NDownloadSuffix ; k>j ; k--)
229 DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1];
230 NDownloadSuffix++;
231 DownloadSuffixIndex[j]=str;
232 }
233 }
234}
235
2824ec9b 236bool is_download_suffix(const char *url)
6e792ade
FM
237{
238 int urllen;
239 int i;
240 int down, up, center;
241 const char *suffix;
242 int cmp;
243 const int max_suffix=10;
244
2824ec9b 245 if (DownloadSuffix == NULL || NDownloadSuffix == 0) return(false);
6e792ade
FM
246
247 urllen=strlen(url)-1;
2824ec9b
FM
248 if (urllen<=0) return(false);
249 if (url[urllen] == '.') return(false); //reject a single trailing dot
8947bb9b 250 for (i=0 ; i<urllen && (url[i]!='/' || url[i+1]=='/') && url[i]!='?' ; i++);
2824ec9b 251 if (i>=urllen) return(false); // url is a hostname without any path or file to download
6e792ade
FM
252
253 for (i=0 ; i<=max_suffix && i<urllen && url[urllen-i]!='.' ; i++)
2824ec9b
FM
254 if (url[urllen-i] == '/' || url[urllen-i] == '?') return(false);
255 if (i>max_suffix || i>=urllen) return(false);
6e792ade
FM
256
257 suffix=url+urllen-i+1;
258 down=0;
259 up=NDownloadSuffix-1;
260 while (down<=up) {
261 center=(down+up)/2;
262 cmp=strcasecmp(suffix,DownloadSuffixIndex[center]);
2824ec9b 263 if (cmp == 0) return(true);
6e792ade
FM
264 if (cmp < 0)
265 up = center-1;
266 else
267 down = center+1;
268 }
2824ec9b 269 return(false);
6e792ade
FM
270}
271