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