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