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