]> git.ipfire.org Git - thirdparty/sarg.git/blob - download.c
All the sprintf/fputs have been replaced by fprintf to avoid an intermediary buffer...
[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
28 void download_report(void)
29 {
30
31 FILE *fp_in = NULL, *fp_ou = NULL;
32
33 char url[MAXLEN];
34 char report_in[MAXLEN];
35 char wdirname[MAXLEN];
36 char report[MAXLEN];
37 char period[100];
38 char ip[MAXLEN];
39 char oip[MAXLEN];
40 char user[MAXLEN];
41 char ouser[MAXLEN];
42 char ouser2[MAXLEN];
43 char data[15];
44 char hora[15];
45 char *str;
46 int z=0;
47 int count=0;
48
49 ouser[0]='\0';
50
51 sprintf(report_in,"%s/sarg/download.log",TempDir);
52 if(access(report_in, R_OK) != 0)
53 return;
54
55 strcpy(wdirname,dirname);
56 sprintf(report,"%s/download.html",wdirname);
57 strcat(wdirname,"/");
58 strcat(wdirname,"sarg-period");
59
60 if ((fp_in = fopen(wdirname, "r")) == 0) {
61 fprintf(stderr, "SARG: (download) %s: %s\n",text[45],wdirname);
62 exit(1);
63 }
64
65 fgets(period,sizeof(period),fp_in);
66 fclose(fp_in);
67
68 #if defined(HAVE_FOPEN64)
69 if((fp_in=fopen64(report_in,"r"))==NULL) {
70 #else
71 if((fp_in=fopen(report_in,"r"))==NULL) {
72 #endif
73 fprintf(stderr, "SARG: (download) %s: %s\n",text[8],report_in);
74 exit(1);
75 }
76
77 #if defined(HAVE_FOPEN64)
78 if((fp_ou=fopen64(report,"w"))==NULL) {
79 #else
80 if((fp_ou=fopen(report,"w"))==NULL) {
81 #endif
82 fprintf(stderr, "SARG: (download) %s: %s\n",text[8],report);
83 exit(1);
84 }
85
86 fputs("<html>\n",fp_ou);
87 fputs("<head>\n",fp_ou);
88 fprintf(fp_ou," <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
89 css(fp_ou);
90 fputs("</head>\n",fp_ou);
91
92 fprintf(fp_ou,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
93
94 if(strlen(LogoImage) > 0) fprintf(fp_ou, "<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s>&nbsp;%s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n",LogoImage,Width,Height,LogoText);
95
96 if(strcmp(IndexTree,"date") == 0)
97 show_sarg(fp_ou, "../../..");
98 else
99 show_sarg(fp_ou, "..");
100
101 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ou);
102 fprintf(fp_ou,"<tr><th class=\"title\">%s</b></th></tr>\n",Title);
103
104 fprintf(fp_ou,"<tr><td class=\"header\">%s: %s</td></tr>\n",text[89],period);
105 fprintf(fp_ou,"<tr><th class=\"header3\">%s</th></tr>\n",text[125]);
106 fputs("</table></center>\n",fp_ou);
107
108 fputs("<center><table cellpadding=0 cellspacing=2>\n",fp_ou);
109 fputs("<tr><td></td></tr>\n",fp_ou);
110 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]);
111
112 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
113 if (getword(data,sizeof(data),buf,' ')<0 || getword(hora,sizeof(hora),buf,' ')<0 ||
114 getword(user,sizeof(user),buf,' ')<0 || getword(ip,sizeof(ip),buf,' ')<0 ||
115 getword(url,sizeof(url),buf,' ')<0) {
116 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",report_in);
117 exit(1);
118 }
119
120 if((str=(char *) strstr(user, "_")) != (char *) NULL ) {
121 if((str=(char *) strstr(str+1, "_")) != (char *) NULL )
122 fixip(user);
123 }
124
125 if(strcmp(Ip2Name,"yes") == 0)
126 ip2name(ip,sizeof(ip));
127
128 if(!z) {
129 strcpy(ouser,user);
130 strcpy(oip,ip);
131 z++;
132 } else {
133 if(strcmp(ouser,user) == 0)
134 user[0]='\0';
135 if(user[0] != '\0')
136 strcpy(ouser,user);
137 if(strcmp(oip,ip) == 0)
138 ip[0]='\0';
139 if(ip[0] != '\0')
140 strcpy(oip,ip);
141 }
142
143 if(UserTabFile[0] != '\0') {
144 sprintf(warea,":%s:",user);
145 if((str=(char *) strstr(userfile,warea)) != (char *) NULL ) {
146 z1=0;
147 str2=(char *) strstr(str+1,":");
148 str2++;
149 bzero(name, MAXLEN);
150 while(str2[z1] != ':') {
151 name[z1]=str2[z1];
152 z1++;
153 }
154 } else strcpy(name,user);
155 } else strcpy(name,user);
156
157 if(dotinuser && strstr(name,"_")) {
158 str2=(char *)subs(name,"_",".");
159 strcpy(name,str2);
160 }
161
162 if(DownloadReportLimit) {
163 if(strcmp(ouser2,name) == 0) {
164 count++;
165 } else {
166 count=1;
167 strcpy(ouser2,name);
168 }
169 if(count >= DownloadReportLimit)
170 continue;
171 }
172
173 if(strlen(BlockIt) > 0)
174 sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"%s/sarg-squidguard-block.png\" border=\"0\"></a>&nbsp;",wwwDocumentRoot,BlockIt,url,ImageFile);
175 else BlockImage[0]='\0';
176
177 fprintf(fp_ou,"<tr><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s-%s</td><td class=\"data2\">%s<a href=\"%s\">%s</a></td></th>\n",name,ip,data,hora,BlockImage,url,url);
178 }
179
180 fputs("</table>\n",fp_ou);
181
182 show_info(fp_ou);
183 fputs("</html>\n",fp_ou);
184
185 fclose(fp_in);
186 fclose(fp_ou);
187
188 unlink(report_in);
189
190 return;
191 }