]> git.ipfire.org Git - thirdparty/sarg.git/blob - download.c
f0c61d521930db18e66c723b4412ecee2267241d
[thirdparty/sarg.git] / download.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26 #include "include/conf.h"
27 #include "include/defs.h"
28
29 void download_report(void)
30 {
31
32 FILE *fp_in = NULL, *fp_ou = NULL;
33
34 char url[MAXLEN];
35 char report_in[MAXLEN];
36 char wdirname[MAXLEN];
37 char report[MAXLEN];
38 char period[100];
39 char ip[MAXLEN];
40 char oip[MAXLEN];
41 char user[MAXLEN];
42 char ouser[MAXLEN];
43 char ouser2[MAXLEN];
44 char data[15];
45 char hora[15];
46 char *str;
47 int z=0;
48 int count=0;
49 int i;
50 struct getwordstruct gwarea;
51
52 ouser[0]='\0';
53 ouser2[0]='\0';
54
55 sprintf(report_in,"%s/sarg/download.log",TempDir);
56 if(access(report_in, R_OK) != 0)
57 return;
58
59 strcpy(wdirname,dirname);
60 sprintf(report,"%s/download.html",wdirname);
61 strcat(wdirname,"/");
62 strcat(wdirname,"sarg-period");
63
64 if ((fp_in = fopen(wdirname, "r")) == 0) {
65 fprintf(stderr, "SARG: (download) %s: %s\n",text[45],wdirname);
66 exit(1);
67 }
68
69 if (!fgets(period,sizeof(period),fp_in)) {
70 fprintf(stderr,"SARG: (download) read error in %s\n",wdirname);
71 exit(1);
72 }
73 fclose(fp_in);
74
75 if((fp_in=MY_FOPEN(report_in,"r"))==NULL) {
76 fprintf(stderr, "SARG: (download) %s: %s\n",text[8],report_in);
77 exit(1);
78 }
79
80 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
81 fprintf(stderr, "SARG: (download) %s: %s\n",text[8],report);
82 exit(1);
83 }
84
85 fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
86 css(fp_ou);
87 fputs("</head>\n",fp_ou);
88
89 fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
90
91 write_logo_image(fp_ou);
92
93 if(strcmp(IndexTree,"date") == 0)
94 show_sarg(fp_ou, "../../..");
95 else
96 show_sarg(fp_ou, "..");
97
98 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
99 fprintf(fp_ou,"<tr><th class=\"title\">%s</th></tr>\n",Title);
100
101 fprintf(fp_ou,"<tr><td class=\"header\">%s: %s</td></tr>\n",text[89],period);
102 fprintf(fp_ou,"<tr><th class=\"header3\">%s</th></tr>\n",text[125]);
103 fputs("</table></div>\n",fp_ou);
104
105 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
106 fputs("<tr><td></td></tr>\n",fp_ou);
107 fprintf(fp_ou,"<tr><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th></tr>\n",text[98],text[111],text[110],text[91]);
108
109 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
110 getword_start(&gwarea,buf);
111 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
112 getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
113 getword(url,sizeof(url),&gwarea,'\t')<0) {
114 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",report_in);
115 exit(1);
116 }
117
118 if((str=(char *) strstr(user, "_")) != (char *) NULL ) {
119 if((str=(char *) strstr(str+1, "_")) != (char *) NULL )
120 fixip(user);
121 }
122
123 if(strcmp(Ip2Name,"yes") == 0)
124 ip2name(ip,sizeof(ip));
125
126 if(!z) {
127 strcpy(ouser,user);
128 strcpy(oip,ip);
129 z++;
130 } else {
131 if(strcmp(ouser,user) == 0)
132 user[0]='\0';
133 if(user[0] != '\0')
134 strcpy(ouser,user);
135 if(strcmp(oip,ip) == 0)
136 ip[0]='\0';
137 if(ip[0] != '\0')
138 strcpy(oip,ip);
139 }
140
141 get_usertab_name(user,name,sizeof(name));
142
143 if(dotinuser && strchr(name,'_')) {
144 subs(name,sizeof(name),"_",".");
145 }
146
147 if(DownloadReportLimit) {
148 if(strcmp(ouser2,name) == 0) {
149 count++;
150 } else {
151 count=1;
152 strcpy(ouser2,name);
153 }
154 if(count >= DownloadReportLimit)
155 continue;
156 }
157
158 for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) url[i]=0;
159
160 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);
161 if(BlockIt[0]!='\0')
162 fprintf(fp_ou,"<a href=\"%s%s?url=%s\"><img src=\"%s/sarg-squidguard-block.png\" border=\"0\"></a>&nbsp;",wwwDocumentRoot,BlockIt,url,ImageFile);
163 fprintf(fp_ou,"<a href=\"%s\">",url);
164 output_html_string(fp_ou,url);
165 fputs("</a></td></tr>\n",fp_ou);
166 }
167
168 fputs("</table>\n",fp_ou);
169
170 show_info(fp_ou);
171 fputs("</body>\n</html>\n",fp_ou);
172
173 fclose(fp_in);
174 fclose(fp_ou);
175
176 unlink(report_in);
177
178 return;
179 }