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