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