]>
git.ipfire.org Git - thirdparty/sarg.git/blob - download.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
30 static char *DownloadSuffix
=NULL
;
31 static char **DownloadSuffixIndex
=NULL
;
32 static int NDownloadSuffix
=0;
34 void download_report(void)
37 FILE *fp_in
= NULL
, *fp_ou
= NULL
;
40 char report_in
[MAXLEN
];
41 char wdirname
[MAXLEN
];
55 struct getwordstruct gwarea
;
60 sprintf(report_in
,"%s/sarg/download.log",TempDir
);
61 if(access(report_in
, R_OK
) != 0)
64 strcpy(wdirname
,dirname
);
65 sprintf(report
,"%s/download.html",wdirname
);
67 strcat(wdirname
,"sarg-period");
69 if ((fp_in
= fopen(wdirname
, "r")) == 0) {
70 fprintf(stderr
, "SARG: (download) %s: %s\n",text
[45],wdirname
);
74 if (!fgets(period
,sizeof(period
),fp_in
)) {
75 fprintf(stderr
,"SARG: (download) read error in %s\n",wdirname
);
80 if((fp_in
=MY_FOPEN(report_in
,"r"))==NULL
) {
81 fprintf(stderr
, "SARG: (download) %s: %s\n",text
[8],report_in
);
85 if((fp_ou
=MY_FOPEN(report
,"w"))==NULL
) {
86 fprintf(stderr
, "SARG: (download) %s: %s\n",text
[8],report
);
90 write_html_header(fp_ou
,(IndexTree
== INDEX_TREE_DATE
) ? 3 : 1,_("Downloads"));
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]);
93 close_html_header(fp_ou
);
95 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou
);
96 fputs("<tr><td></td></tr>\n",fp_ou
);
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]);
99 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
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) {
104 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",report_in
);
108 if((str
=(char *) strstr(user
, "_")) != (char *) NULL
) {
109 if((str
=(char *) strstr(str
+1, "_")) != (char *) NULL
)
114 ip2name(ip
,sizeof(ip
));
121 if(strcmp(ouser
,user
) == 0)
125 if(strcmp(oip
,ip
) == 0)
131 user_find(name
, sizeof(name
), user
);
133 if(dotinuser
&& strchr(name
,'_')) {
134 subs(name
,sizeof(name
),"_",".");
137 if(DownloadReportLimit
) {
138 if(strcmp(ouser2
,name
) == 0) {
144 if(count
>= DownloadReportLimit
)
148 for (i
=strlen(url
)-1 ; i
>=0 && (unsigned char)url
[i
]<' ' ; i
--) url
[i
]=0;
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
);
152 fprintf(fp_ou
,"<a href=\"%s%s?url=%s\"><img src=\"%s/sarg-squidguard-block.png\"></a> ",wwwDocumentRoot
,BlockIt
,url
,ImageFile
);
153 fprintf(fp_ou
,"<a href=\"%s\">",url
);
154 output_html_string(fp_ou
,url
);
155 fputs("</a></td></tr>\n",fp_ou
);
159 fputs("</table></div>\n",fp_ou
);
160 write_html_trailer(fp_ou
);
168 void free_download(void)
170 if (DownloadSuffix
) {
171 free(DownloadSuffix
);
174 if (DownloadSuffixIndex
) {
175 free(DownloadSuffixIndex
);
176 DownloadSuffixIndex
=NULL
;
181 void set_download_suffix(const char *list
)
189 DownloadSuffix
=strdup(list
);
190 if (!DownloadSuffix
) {
191 fprintf(stderr
,"SARG: Download suffix list too long\n");
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");
203 str
= DownloadSuffix
;
204 for (i
=0 ; DownloadSuffix
[i
] ; i
++) {
205 if (DownloadSuffix
[i
] == ',') {
206 DownloadSuffix
[i
] = '\0';
209 for (j
=0 ; j
<NDownloadSuffix
&& (cmp
=strcasecmp(str
,DownloadSuffixIndex
[j
]))>0 ; j
++);
211 for (k
=NDownloadSuffix
; k
>j
; k
--)
212 DownloadSuffixIndex
[k
]=DownloadSuffixIndex
[k
-1];
214 DownloadSuffixIndex
[j
]=str
;
217 str
=DownloadSuffix
+i
+1;
223 for (j
=0 ; j
<NDownloadSuffix
&& (cmp
=strcasecmp(str
,DownloadSuffixIndex
[j
]))>0 ; j
++);
225 for (k
=NDownloadSuffix
; k
>j
; k
--)
226 DownloadSuffixIndex
[k
]=DownloadSuffixIndex
[k
-1];
228 DownloadSuffixIndex
[j
]=str
;
233 int is_download_suffix(const char *url
)
237 int down
, up
, center
;
240 const int max_suffix
=10;
242 if (DownloadSuffix
== NULL
|| NDownloadSuffix
== 0) return(0);
244 urllen
=strlen(url
)-1;
245 if (urllen
<=0) return(0);
246 if (url
[urllen
] == '.') return(0); //reject a single trailing dot
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
250 for (i
=0 ; i
<=max_suffix
&& i
<urllen
&& url
[urllen
-i
]!='.' ; i
++)
251 if (url
[urllen
-i
] == '/' || url
[urllen
-i
] == '?') return(0);
252 if (i
>max_suffix
|| i
>=urllen
) return(0);
254 suffix
=url
+urllen
-i
+1;
256 up
=NDownloadSuffix
-1;
259 cmp
=strcasecmp(suffix
,DownloadSuffixIndex
[center
]);
260 if (cmp
== 0) return(1);