]>
Commit | Line | Data |
---|---|---|
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 |
30 | static char *DownloadSuffix=NULL; |
31 | static char **DownloadSuffixIndex=NULL; | |
32 | static int NDownloadSuffix=0; | |
33 | ||
32e71fa4 | 34 | void 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 GS |
42 | char wdirname[MAXLEN]; |
43 | char report[MAXLEN]; | |
d6e703cc | 44 | char period[100]; |
25697a35 GS |
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]; | |
25697a35 GS |
52 | int z=0; |
53 | int count=0; | |
dfb337be | 54 | int i; |
f2ec8c75 | 55 | int new_user; |
9c7c6346 | 56 | struct getwordstruct gwarea; |
ac422f9b | 57 | struct longlinestruct line; |
f2ec8c75 | 58 | struct userinfostruct *uinfo; |
25697a35 GS |
59 | |
60 | ouser[0]='\0'; | |
4157aa09 | 61 | ouser2[0]='\0'; |
25697a35 GS |
62 | |
63 | sprintf(report_in,"%s/sarg/download.log",TempDir); | |
64 | if(access(report_in, R_OK) != 0) | |
65 | return; | |
66 | ||
d5d021c5 | 67 | strcpy(wdirname,outdirname); |
25697a35 GS |
68 | sprintf(report,"%s/download.html",wdirname); |
69 | strcat(wdirname,"/"); | |
d6e703cc | 70 | strcat(wdirname,"sarg-period"); |
25697a35 GS |
71 | |
72 | if ((fp_in = fopen(wdirname, "r")) == 0) { | |
9f70c14e | 73 | debuga(_("(download) Cannot open file %s\n"),wdirname); |
25697a35 GS |
74 | exit(1); |
75 | } | |
76 | ||
05b90947 | 77 | if (!fgets(period,sizeof(period),fp_in)) { |
10210234 | 78 | debuga(_("(download) read error in %s\n"),wdirname); |
05b90947 FM |
79 | exit(1); |
80 | } | |
25697a35 GS |
81 | fclose(fp_in); |
82 | ||
936c9905 | 83 | if((fp_in=MY_FOPEN(report_in,"r"))==NULL) { |
9f70c14e FM |
84 | debuga(_("(download) Cannot open log file %s\n"),report_in); |
85 | exit(1); | |
25697a35 GS |
86 | } |
87 | ||
936c9905 | 88 | if((fp_ou=MY_FOPEN(report,"w"))==NULL) { |
9f70c14e FM |
89 | debuga(_("(download) Cannot open log file %s\n"),report); |
90 | exit(1); | |
25697a35 GS |
91 | } |
92 | ||
c0ec9cc7 | 93 | write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Downloads")); |
c36c7384 FM |
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")); | |
c0ec9cc7 | 96 | close_html_header(fp_ou); |
25697a35 | 97 | |
c0ec9cc7 | 98 | fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou); |
25697a35 | 99 | fputs("<tr><td></td></tr>\n",fp_ou); |
c36c7384 | 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")); |
25697a35 | 101 | |
ac422f9b | 102 | if (longline_prepare(&line)<0) { |
10210234 | 103 | debuga(_("Not enough memory to read the downloaded files\n")); |
ac422f9b FM |
104 | exit(1); |
105 | } | |
106 | ||
107 | while((buf=longline_read(fp_in,&line))!=NULL) { | |
9c7c6346 FM |
108 | getword_start(&gwarea,buf); |
109 | if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 || | |
e5b2c6f0 | 110 | getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) { |
10210234 | 111 | debuga(_("There is a broken record or garbage in file %s\n"),report_in); |
e5b2c6f0 FM |
112 | exit(1); |
113 | } | |
114 | if (getword_ptr(buf,&url,&gwarea,'\t')<0) { | |
10210234 | 115 | debuga(_("There is a broken url in file %s\n"),report_in); |
4bcb77cf FM |
116 | exit(1); |
117 | } | |
dfb337be | 118 | |
f2ec8c75 FM |
119 | uinfo=userinfo_find_from_id(user); |
120 | if (!uinfo) { | |
10210234 | 121 | debuga(_("Unknown user ID %s in file %s\n"),user,report_in); |
f2ec8c75 | 122 | exit(1); |
25697a35 | 123 | } |
f2ec8c75 | 124 | new_user=0; |
25697a35 GS |
125 | if(!z) { |
126 | strcpy(ouser,user); | |
127 | strcpy(oip,ip); | |
128 | z++; | |
f2ec8c75 | 129 | new_user=1; |
25697a35 | 130 | } else { |
f2ec8c75 | 131 | if(strcmp(ouser,user) != 0) { |
25697a35 | 132 | strcpy(ouser,user); |
f2ec8c75 FM |
133 | new_user=1; |
134 | } | |
135 | if(strcmp(oip,ip) != 0) { | |
25697a35 | 136 | strcpy(oip,ip); |
f2ec8c75 FM |
137 | new_user=1; |
138 | } | |
94ff9470 GS |
139 | } |
140 | ||
141 | if(DownloadReportLimit) { | |
f2ec8c75 | 142 | if(strcmp(ouser2,uinfo->label) == 0) { |
4157aa09 | 143 | count++; |
25697a35 GS |
144 | } else { |
145 | count=1; | |
f2ec8c75 | 146 | strcpy(ouser2,uinfo->label); |
25697a35 | 147 | } |
94ff9470 | 148 | if(count >= DownloadReportLimit) |
25697a35 GS |
149 | continue; |
150 | } | |
151 | ||
dfb337be | 152 | for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) url[i]=0; |
25697a35 | 153 | |
f2ec8c75 FM |
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); | |
ac422f9b FM |
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> ",ImageFile); | |
164 | } | |
f2ec8c75 | 165 | fputs("<a href=\"http://",fp_ou); |
ac422f9b | 166 | output_html_url(fp_ou,url); |
f2ec8c75 | 167 | fputs("\">http://",fp_ou); |
ac422f9b | 168 | output_html_string(fp_ou,url,100); |
dfb337be | 169 | fputs("</a></td></tr>\n",fp_ou); |
25697a35 | 170 | } |
25697a35 | 171 | fclose(fp_in); |
ac422f9b | 172 | longline_free(&line); |
c0ec9cc7 FM |
173 | |
174 | fputs("</table></div>\n",fp_ou); | |
175 | write_html_trailer(fp_ou); | |
25697a35 GS |
176 | fclose(fp_ou); |
177 | ||
178 | unlink(report_in); | |
179 | ||
180 | return; | |
181 | } | |
6e792ade FM |
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) { | |
9f70c14e | 206 | debuga(_("Download suffix list too long\n")); |
6e792ade FM |
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) { | |
9f70c14e | 214 | debuga(_("Too many download suffixes\n")); |
6e792ade FM |
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) { | |
fabbc7cc | 223 | cmp = -1; |
6e792ade FM |
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) { | |
fabbc7cc | 237 | cmp = -1; |
6e792ade FM |
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 | ||
fabbc7cc | 257 | if (DownloadSuffix == NULL || NDownloadSuffix == 0) return(0); |
6e792ade FM |
258 | |
259 | urllen=strlen(url)-1; | |
260 | if (urllen<=0) return(0); | |
261 | if (url[urllen] == '.') return(0); //reject a single trailing dot | |
8947bb9b FM |
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 | |
6e792ade FM |
264 | |
265 | for (i=0 ; i<=max_suffix && i<urllen && url[urllen-i]!='.' ; i++) | |
8947bb9b | 266 | if (url[urllen-i] == '/' || url[urllen-i] == '?') return(0); |
6e792ade FM |
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 |