]> git.ipfire.org Git - thirdparty/sarg.git/blame - download.c
Remove the line number from a message
[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"));
94558017 78 fputs("<tr><td class=\"header_c\">",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);
c36c7384 85 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 86
afaa3b67 87 if ((line=longline_create())==NULL) {
10210234 88 debuga(_("Not enough memory to read the downloaded files\n"));
06b39c87 89 exit(EXIT_FAILURE);
ac422f9b
FM
90 }
91
afaa3b67 92 while((buf=longline_read(fp_in,line))!=NULL) {
9c7c6346
FM
93 getword_start(&gwarea,buf);
94 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
e5b2c6f0 95 getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
10210234 96 debuga(_("There is a broken record or garbage in file %s\n"),report_in);
06b39c87 97 exit(EXIT_FAILURE);
e5b2c6f0
FM
98 }
99 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
10210234 100 debuga(_("There is a broken url in file %s\n"),report_in);
06b39c87 101 exit(EXIT_FAILURE);
4bcb77cf 102 }
dfb337be 103
f2ec8c75
FM
104 uinfo=userinfo_find_from_id(user);
105 if (!uinfo) {
10210234 106 debuga(_("Unknown user ID %s in file %s\n"),user,report_in);
06b39c87 107 exit(EXIT_FAILURE);
25697a35 108 }
2824ec9b 109 new_user=false;
25697a35
GS
110 if(!z) {
111 strcpy(ouser,user);
112 strcpy(oip,ip);
113 z++;
2824ec9b 114 new_user=true;
25697a35 115 } else {
f2ec8c75 116 if(strcmp(ouser,user) != 0) {
25697a35 117 strcpy(ouser,user);
2824ec9b 118 new_user=true;
f2ec8c75
FM
119 }
120 if(strcmp(oip,ip) != 0) {
25697a35 121 strcpy(oip,ip);
2824ec9b 122 new_user=true;
f2ec8c75 123 }
94ff9470
GS
124 }
125
126 if(DownloadReportLimit) {
f2ec8c75 127 if(strcmp(ouser2,uinfo->label) == 0) {
4157aa09 128 count++;
25697a35
GS
129 } else {
130 count=1;
f2ec8c75 131 strcpy(ouser2,uinfo->label);
25697a35 132 }
94ff9470 133 if(count >= DownloadReportLimit)
25697a35
GS
134 continue;
135 }
136
dfb337be 137 for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) url[i]=0;
25697a35 138
f2ec8c75
FM
139 fputs("<tr>",fp_ou);
140 if (new_user)
141 fprintf(fp_ou,"<td class=\"data\">%s</td><td class=\"data\">%s</td>",uinfo->label,ip);
142 else
143 fputs("<td class=\"data\"></td><td class=\"data\"></td>",fp_ou);
144 fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">",data,hora);
ac422f9b
FM
145 if(BlockIt[0]!='\0') {
146 fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt);
147 output_html_url(fp_ou,url);
148 fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a>&nbsp;",ImageFile);
149 }
f2ec8c75 150 fputs("<a href=\"http://",fp_ou);
ac422f9b 151 output_html_url(fp_ou,url);
f2ec8c75 152 fputs("\">http://",fp_ou);
ac422f9b 153 output_html_string(fp_ou,url,100);
dfb337be 154 fputs("</a></td></tr>\n",fp_ou);
25697a35 155 }
25697a35 156 fclose(fp_in);
afaa3b67 157 longline_destroy(&line);
c0ec9cc7
FM
158
159 fputs("</table></div>\n",fp_ou);
fa6552b0
FM
160 if (write_html_trailer(fp_ou)<0)
161 debuga(_("Write error in file %s\n"),report);
162 if (fclose(fp_ou)==EOF)
163 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
25697a35
GS
164
165 unlink(report_in);
166
167 return;
168}
6e792ade
FM
169
170void free_download(void)
171{
172 if (DownloadSuffix) {
173 free(DownloadSuffix);
174 DownloadSuffix=NULL;
175 }
176 if (DownloadSuffixIndex) {
177 free(DownloadSuffixIndex);
178 DownloadSuffixIndex=NULL;
179 }
180 NDownloadSuffix=0;
181}
182
183void set_download_suffix(const char *list)
184{
185 char *str;
186 int i, j, k;
187 int cmp;
188
189 free_download();
190
191 DownloadSuffix=strdup(list);
192 if (!DownloadSuffix) {
9f70c14e 193 debuga(_("Download suffix list too long\n"));
06b39c87 194 exit(EXIT_FAILURE);
6e792ade
FM
195 }
196 j = 1;
197 for (i=0 ; list[i] ; i++)
198 if (list[i] == ',') j++;
199 DownloadSuffixIndex=malloc(j*sizeof(char *));
200 if (!DownloadSuffixIndex) {
9f70c14e 201 debuga(_("Too many download suffixes\n"));
06b39c87 202 exit(EXIT_FAILURE);
6e792ade
FM
203 }
204
205 str = DownloadSuffix;
206 for (i=0 ; DownloadSuffix[i] ; i++) {
207 if (DownloadSuffix[i] == ',') {
208 DownloadSuffix[i] = '\0';
209 if (*str) {
fabbc7cc 210 cmp = -1;
6e792ade
FM
211 for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++);
212 if (cmp != 0) {
213 for (k=NDownloadSuffix ; k>j ; k--)
214 DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1];
215 NDownloadSuffix++;
216 DownloadSuffixIndex[j]=str;
217 }
218 }
219 str=DownloadSuffix+i+1;
220 }
221 }
222
223 if (*str) {
fabbc7cc 224 cmp = -1;
6e792ade
FM
225 for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++);
226 if (cmp != 0) {
227 for (k=NDownloadSuffix ; k>j ; k--)
228 DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1];
229 NDownloadSuffix++;
230 DownloadSuffixIndex[j]=str;
231 }
232 }
233}
234
2824ec9b 235bool is_download_suffix(const char *url)
6e792ade
FM
236{
237 int urllen;
238 int i;
239 int down, up, center;
240 const char *suffix;
241 int cmp;
242 const int max_suffix=10;
243
2824ec9b 244 if (DownloadSuffix == NULL || NDownloadSuffix == 0) return(false);
6e792ade
FM
245
246 urllen=strlen(url)-1;
2824ec9b
FM
247 if (urllen<=0) return(false);
248 if (url[urllen] == '.') return(false); //reject a single trailing dot
8947bb9b 249 for (i=0 ; i<urllen && (url[i]!='/' || url[i+1]=='/') && url[i]!='?' ; i++);
2824ec9b 250 if (i>=urllen) return(false); // url is a hostname without any path or file to download
6e792ade
FM
251
252 for (i=0 ; i<=max_suffix && i<urllen && url[urllen-i]!='.' ; i++)
2824ec9b
FM
253 if (url[urllen-i] == '/' || url[urllen-i] == '?') return(false);
254 if (i>max_suffix || i>=urllen) return(false);
6e792ade
FM
255
256 suffix=url+urllen-i+1;
257 down=0;
258 up=NDownloadSuffix-1;
259 while (down<=up) {
260 center=(down+up)/2;
261 cmp=strcasecmp(suffix,DownloadSuffixIndex[center]);
2824ec9b 262 if (cmp == 0) return(true);
6e792ade
FM
263 if (cmp < 0)
264 up = center-1;
265 else
266 down = center+1;
267 }
2824ec9b 268 return(false);
6e792ade
FM
269}
270