]> git.ipfire.org Git - thirdparty/sarg.git/blame - report.c
Build the .htaccess of each report out of a template file.
[thirdparty/sarg.git] / report.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
9 * ---------------------------------------------------------------------
10 *
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.
15 *
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.
20 *
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.
24 *
25 */
26
27#include "include/conf.h"
28#include "include/defs.h"
29
e5b2c6f0
FM
30static FILE *fp_tt=NULL;
31
5f3cfd1d
FM
32static void maketmp(const char *user, const char *dirname, int debug, int indexonly);
33static void maketmp_hour(const char *user, const char *dirname, int indexonly);
f2ec8c75
FM
34static void gravatmp_hora(const char *dirname, const struct userinfostruct *uinfo, const char *data, const char *hora, long long int elap, long long int accbytes, int indexonly);
35static void gravatmpf(const struct userinfostruct *uinfo, const char *oldurl, long long int nacc, long long int nbytes, const char *oldmsg, long long int nelap, int indexonly, long long int incache, long long int oucache);
36static void gravaporuser(const struct userinfostruct *uinfo, const char *dirname, const char *url, const char *ip, const char *data, const char *hora, long long int tam, long long int elap, int indexonly);
37static void gravager(FILE *fp_gen, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache);
5160b452 38static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart);
5f3cfd1d 39
32e71fa4 40void gerarel(void)
25697a35
GS
41{
42
d6e703cc 43 FILE *fp_in;
0a4e18e1 44 FILE *fp_gen;
25697a35 45
ac422f9b 46 char *buf;
f2ec8c75 47 char accdia[11], acchora[9], accip[MAXLEN], *accurl;
e6414a9d 48 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
f2ec8c75 49 char wdirname[MAXLEN];
e5b2c6f0 50 char *oldurl=NULL;
f2ec8c75 51 char oldmsg[50], acccode[MAXLEN/2 - 1], oldacccode[MAXLEN/2 - 1], user[MAX_USER_LEN];
4157aa09 52 char ipantes[MAXLEN], nameantes[MAXLEN];
25697a35 53 char accsmart[MAXLEN];
623c04aa 54 char crc2[MAXLEN/2 -1];
e5b2c6f0 55 char siteind[MAX_TRUNCATED_URL];
ac422f9b 56 char arqtt[256];
e5b2c6f0
FM
57 char *oldurltt=NULL;
58 char oldaccdiatt[11],oldacchoratt[9];
2240dcea
FM
59 char tmp3[MAXLEN];
60 char tmp4[5];
f2ec8c75 61 char u2[MAX_USER_LEN];
4157aa09
FM
62 long long int nbytes=0;
63 long long int nelap=0;
25697a35
GS
64 long long int nacc=0;
65 long long int rtotal=0;
66 long long int incache=0;
67 long long int oucache=0;
6e792ade 68 long long int accbytes, accelap;
9c7c6346 69 char *str;
25697a35
GS
70 DIR *dirp;
71 struct dirent *direntp;
8db846b0
FM
72 const char logext[]=".log";
73 int dlen;
e5b2c6f0
FM
74 int url_len;
75 int ourl_size=0;
76 int ourltt_size=0;
1bafebef 77 int same_url;
f2ec8c75 78 int new_user;
9c7c6346 79 struct getwordstruct gwarea;
ac422f9b 80 struct longlinestruct line;
f2ec8c75 81 struct userinfostruct *uinfo,*puinfo;
25697a35
GS
82
83 ipantes[0]='\0';
25697a35
GS
84 smartfilter=0;
85
d5d021c5 86 sprintf(outdirname, "%s%s", outdir, period);
e6414a9d 87 vrfydir(period, addr, site, us, email);
25697a35
GS
88
89 if(debugz){
d5d021c5 90 debugaz("outdirname",outdirname);
25697a35 91 }
48864d28 92
d5d021c5 93 gperiod(outdirname,period);
25697a35 94
4157aa09 95 if(UserAgentLog[0] != '\0' && email[0] == '\0') useragent();
25697a35 96
d5d021c5 97 snprintf(wdirname,sizeof(wdirname),"%s/sarg-general",outdirname);
1bafebef 98 if((fp_gen=MY_FOPEN(wdirname,"w"))==NULL){
d5d021c5 99 debuga(_("(report) Cannot open file: %s\n"),wdirname);
0a4e18e1
FM
100 exit(1);
101 }
102
f2ec8c75 103 puinfo=NULL;
25697a35 104 strncat(tmp,"/sarg",5);
e5b2c6f0 105 fp_tt=NULL;
25697a35
GS
106
107 dirp = opendir(tmp);
108 while ((direntp = readdir( dirp )) != NULL ) {
8db846b0
FM
109 dlen=strlen(direntp->d_name)-(sizeof(logext)-1);
110 if (dlen<0) continue;
111 if((strcmp(direntp->d_name+dlen,logext) != 0) ||
d6e703cc
FM
112 (strncmp(direntp->d_name,"download.log",12) == 0) ||
113 (strncmp(direntp->d_name,"denied.log",10) == 0) ||
5f3cfd1d 114 (strncmp(direntp->d_name,"authfail.log.unsort",19) == 0))
25697a35 115 continue;
8db846b0
FM
116 if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) {
117 fprintf(stderr, "SARG: (report) directory entry too long: %s/%s\n",tmp,direntp->d_name);
25697a35
GS
118 exit(1);
119 }
b6d3adea 120 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
c36c7384 121 fprintf(stderr, "SARG: (report) %s: %s\n",_("Cannot open file"),tmp);
4bcb77cf
FM
122 exit(1);
123 }
48864d28 124
8db846b0
FM
125 if (dlen>0) {
126 if (dlen>=sizeof(user)) continue;
127 strncpy(user,direntp->d_name,dlen);
128 user[dlen]=0;
129 } else {
130 user[0]='\0';
25697a35 131 }
48864d28 132
f2ec8c75
FM
133 uinfo=userinfo_find_from_file(user);
134 if (!uinfo) {
10210234 135 debuga(_("Ignoring unknown user file %s\n"),user);
f2ec8c75
FM
136 continue;
137 }
138 strcpy(u2,uinfo->id);
72c27633
FM
139 if(Ip2Name && uinfo->id_is_ip) {
140 strcpy(ipantes,u2);
141 ip2name(u2,sizeof(u2));
142 strcpy(nameantes,u2);
143 }
f2ec8c75
FM
144 user_find(uinfo->label,MAX_USER_LEN, u2);
145
25697a35
GS
146 maketmp(user,tmp,debug,indexonly);
147 maketmp_hour(user,tmp,indexonly);
48864d28 148
25697a35 149 ttopen=0;
e5b2c6f0
FM
150 oldurltt=NULL;
151 ourltt_size=0;
ac422f9b
FM
152 memset(oldaccdiatt,0,sizeof(oldaccdiatt));
153 memset(oldacchoratt,0,sizeof(oldacchoratt));
f2ec8c75 154 new_user=1;
48864d28 155
ac422f9b 156 if (longline_prepare(&line)<0) {
10210234 157 debuga(_("Not enough memory to read the downloaded files\n"));
ac422f9b
FM
158 exit(1);
159 }
160
161 while((buf=longline_read(fp_in,&line))!=NULL) {
9c7c6346
FM
162 getword_start(&gwarea,buf);
163 if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
f2ec8c75 164 getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
e5b2c6f0 165 getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
9c7c6346 166 getword(acccode,sizeof(acccode),&gwarea,'\t')<0) {
10210234 167 debuga(_("There is a broken record or garbage in file %s\n"),tmp3);
4bcb77cf
FM
168 exit(1);
169 }
d6e703cc 170 if(strncmp(acccode,"TCP_DENIED/407",14) == 0) continue;
e34a7c98 171 if (getword_atoll(&accelap,&gwarea,'\t')<0) {
10210234 172 debuga(_("There is a broken elapsed time in file %s\n"),tmp3);
e6414a9d
FM
173 exit(1);
174 }
e34a7c98 175 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
10210234 176 debuga(_("There is a broken smart info in file %s\n"),tmp3);
4bcb77cf
FM
177 exit(1);
178 }
48864d28 179
4157aa09 180 if(accsmart[0] != '\0') {
25697a35 181 smartfilter++;
d5d021c5 182 grava_SmartFilter(outdirname,uinfo->id,accip,accdia,acchora,accurl,accsmart);
25697a35
GS
183 }
184
246c8489 185 if(Ip2Name) {
25697a35
GS
186 if(strcmp(accip,ipantes) != 0) {
187 strcpy(ipantes,accip);
a1c55d8c 188 ip2name(accip,sizeof(accip));
25697a35
GS
189 strcpy(nameantes,accip);
190 } else strcpy(accip,nameantes);
191 }
48864d28 192
d5d021c5 193 gravatmp_hora(outdirname,uinfo,accdia,acchora,accelap,accbytes,indexonly);
25697a35
GS
194
195 if(iprel){
d5d021c5 196 gravaporuser(uinfo,outdirname,accurl,accip,accdia,acchora,accbytes,accelap,indexonly);
25697a35
GS
197 }
198
199 if(!rtotal){
e5b2c6f0
FM
200 url_len=strlen(accurl);
201 if (!oldurl || url_len>=ourl_size) {
202 ourl_size=url_len+1;
203 oldurl=realloc(oldurl,ourl_size);
204 if (!oldurl) {
10210234 205 debuga(_("Not enough memory to store the url\n"));
e5b2c6f0
FM
206 exit(1);
207 }
208 }
25697a35
GS
209 strcpy(oldurl,accurl);
210 strcpy(oldacccode,acccode);
f2ec8c75 211 puinfo=uinfo;
25697a35
GS
212 strcpy(oldaccip,accip);
213 strcpy(oldaccdia,accdia);
214 strcpy(oldacchora,acchora);
f2ec8c75 215 new_user=0;
25697a35
GS
216 rtotal++;
217 }
1bafebef 218 same_url=(strcmp(oldurl,accurl) == 0);
25697a35
GS
219
220 if(site[0] != '\0') {
f2ec8c75 221 if(new_user){
25697a35
GS
222 strcpy(oldmsg,"OK");
223 if(strstr(oldacccode,"DENIED") != 0)
c36c7384 224 sprintf(oldmsg,"%s",_("DENIED"));
f2ec8c75
FM
225 gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
226 gravager(fp_gen,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
25697a35
GS
227 nacc=0;
228 nbytes=0;
229 nelap=0;
230 incache=0;
231 oucache=0;
232 }
5f3cfd1d 233 } else {
f2ec8c75 234 if(!same_url || new_user){
25697a35 235 if(strstr(oldacccode,"DENIED") != 0)
c36c7384 236 strcpy(oldmsg,_("DENIED"));
ac422f9b
FM
237 else
238 strcpy(oldmsg,"OK");
f2ec8c75
FM
239 gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
240 gravager(fp_gen,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
25697a35
GS
241 nacc=0;
242 nbytes=0;
243 nelap=0;
244 incache=0;
245 oucache=0;
32e71fa4 246 }
5f3cfd1d
FM
247 }
248 nacc++;
6e792ade
FM
249 nbytes+=accbytes;
250 nelap+=accelap;
5f3cfd1d 251
ac422f9b 252 if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0 &&
e5b2c6f0 253 (!oldurltt || strcmp(oldurltt,accurl) || strcmp(oldaccdiatt,accdia) || strcmp(oldacchoratt,acchora))) {
ac422f9b 254
5f3cfd1d 255 if(!ttopen) {
e5b2c6f0 256 url_to_file(accurl,siteind,sizeof(siteind));
d5d021c5 257 snprintf(arqtt,sizeof(arqtt),"%s/%s",outdirname,uinfo->filename);
5f3cfd1d
FM
258 if(access(arqtt, R_OK) != 0)
259 my_mkdir(arqtt);
d5d021c5 260 snprintf(arqtt,sizeof(arqtt),"%s/%s/tt%s-%s.html",outdirname,uinfo->filename,uinfo->filename,siteind);
5f3cfd1d 261 if ((fp_tt = fopen(arqtt, "w")) == 0) {
c36c7384 262 fprintf(stderr, "SARG: (report) %s: %s\n",_("Cannot open file"),arqtt);
5f3cfd1d
FM
263 exit(1);
264 }
265 ttopen=1;
266
24977ddb 267 /*
e6414a9d 268 if(Privacy)
5f3cfd1d
FM
269 sprintf(httplink,"<font size=%s color=%s><href=http://%s>%s", \
270 FontSize,PrivacyStringColor,PrivacyString,PrivacyString);
271 else
4157aa09 272 sprintf(httplink,"<font size=%s><a href=\"http://%s\">%s</a>",FontSize,accurl,accurl);
24977ddb 273 */
5f3cfd1d 274
c0ec9cc7 275 write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report"));
c36c7384
FM
276 fprintf(fp_tt,"<tr><td class=\"header_l\">%s:&nbsp;%s</td></tr>\n",_("Period"),period);
277 fprintf(fp_tt,"<tr><td class=\"header_l\">%s:&nbsp;%s</td></tr>\n",_("User"),uinfo->label);
278 fprintf(fp_tt,"<tr><td class=\"header_l\">%s:&nbsp;%s, %s</td></tr>\n",_("Sort"),UserSortField,UserSortOrder);
279 fprintf(fp_tt,"<tr><th class=\"header_c\">%s</th></tr>\n",_("User"));
c0ec9cc7
FM
280 close_html_header(fp_tt);
281
282 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_tt);
5f3cfd1d 283 fputs("<tr><td></td><td></td></tr>",fp_tt);
c36c7384 284 strncpy(tmp4,_("DATE/TIME"),4);
2240dcea 285 tmp4[4]='\0';
c36c7384 286 fprintf(fp_tt,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("ACCESSED SITE"),tmp4,_("DATE/TIME")+5);
94ff9470
GS
287 }
288
e5b2c6f0
FM
289 fputs("<tr><td class=\"data\">",fp_tt);
290 output_html_string(fp_tt,accurl,100);
291 fprintf(fp_tt,"</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",accdia,acchora);
25697a35 292
e5b2c6f0
FM
293 url_len=strlen(accurl);
294 if (!oldurltt || url_len>=ourltt_size) {
295 ourltt_size=url_len+1;
296 oldurltt=realloc(oldurltt,ourltt_size);
297 if (!oldurltt) {
10210234 298 debuga(_("Not enough memory to store the url\n"));
e5b2c6f0
FM
299 exit(1);
300 }
301 }
ac422f9b
FM
302 strcpy(oldurltt,accurl);
303 strcpy(oldaccdiatt,accdia);
304 strcpy(oldacchoratt,acchora);
305 }
25697a35 306
9c7c6346
FM
307 strcpy(crc2,acccode);
308 str=strchr(crc2,'/');
309 if (str) *str='\0';
5f3cfd1d 310 if(strstr(crc2,"MISS") != 0)
6e792ade
FM
311 oucache+=accbytes;
312 else incache+=accbytes;
25697a35 313
f2ec8c75
FM
314 if(new_user) {
315 new_user=0;
f2ec8c75 316 day_totalize(tmp,puinfo,indexonly);
5f3cfd1d
FM
317 }
318
f2ec8c75 319 puinfo=uinfo;
5f3cfd1d 320 strcpy(oldacccode,acccode);
5f3cfd1d 321 strcpy(oldaccip,accip);
1bafebef
FM
322 if (!same_url) {
323 url_len=strlen(accurl);
324 if (url_len>=ourl_size) {
325 ourl_size=url_len+1;
326 oldurl=realloc(oldurl,ourl_size);
327 if (!oldurl) {
10210234 328 debuga(_("Not enough memory to store the url\n"));
1bafebef
FM
329 exit(1);
330 }
e5b2c6f0 331 }
1bafebef 332 strcpy(oldurl,accurl);
e5b2c6f0 333 }
5f3cfd1d
FM
334 strcpy(oldaccdia,accdia);
335 strcpy(oldacchora,acchora);
25697a35 336
5f3cfd1d 337 }
5f3cfd1d 338 fclose(fp_in);
ac422f9b 339 longline_free(&line);
e5b2c6f0 340 if (oldurltt) free(oldurltt);
1bafebef 341 unlink(tmp3);
25697a35 342 }
05b90947 343 closedir(dirp);
25697a35 344
e5b2c6f0
FM
345 if (oldurl) {
346 if(strstr(oldacccode,"DENIED") != 0)
c36c7384 347 strcpy(oldmsg,_("DENIED"));
e5b2c6f0
FM
348 else
349 strcpy(oldmsg,"OK");
f2ec8c75 350 gravatmpf(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
f2ec8c75 351 gravager(fp_gen,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
e5b2c6f0
FM
352 free(oldurl);
353 }
0a4e18e1 354 fclose(fp_gen);
f2ec8c75 355 if (puinfo) day_totalize(tmp,puinfo,indexonly);
25697a35
GS
356
357 tmpsort();
358
d5d021c5 359 totalger(outdirname, debug, outdir);
25697a35 360
5542cb59 361 if(email[0] == '\0') {
085c1e1f 362 if((ReportType & REPORT_TYPE_DOWNLOADS) != 0) download_report();
25697a35 363
65740c50 364 if(DansGuardianConf[0] != '\0') {
25697a35
GS
365 dansguardian_log();
366 }
367
491b862f 368 squidguard_log();
25697a35 369
25697a35
GS
370 topuser();
371
085c1e1f 372 if((ReportType & REPORT_TYPE_TOPSITES) != 0) topsites();
25697a35 373
085c1e1f 374 if((ReportType & REPORT_TYPE_SITES_USERS) != 0) siteuser();
25697a35
GS
375 gen_denied_report();
376
25697a35
GS
377 authfail_report();
378
d6e703cc 379 if(smartfilter) smartfilter_report();
25697a35 380
65740c50 381 if(DansGuardianConf[0] != '\0') dansguardian_report();
25697a35 382
491b862f 383 squidguard_report();
25697a35 384
085c1e1f 385 if((ReportType & REPORT_TYPE_USERS_SITES) != 0) htmlrel();
25697a35 386
491b862f 387 make_index();
25697a35 388
d5d021c5 389 if(SuccessfulMsg) debuga(_("Successful report generated on %s\n"),outdirname);
25697a35 390 } else {
d5d021c5 391 geramail(outdirname, debug, outdir, userip, email, TempDir);
25697a35 392
e6414a9d 393 if((strcmp(email,"stdout") != 0) && SuccessfulMsg)
d5d021c5 394 debuga(_("Successful report generated and sent to %s\n"),email);
25697a35
GS
395 }
396
397 if(indexonly) {
d5d021c5 398 index_only(outdirname, debug);
25697a35
GS
399 }
400
c274f011
FM
401 /*
402 2009-10-13(Frederic) This piece of code is never called so it is commented out for good.
d6e703cc
FM
403 if(strlen(email) < 0)
404 removetmp(dirname);
c274f011 405 */
25697a35
GS
406
407 return;
408}
409
5f3cfd1d 410static void maketmp(const char *user, const char *dirname, int debug, int indexonly)
25697a35
GS
411{
412
413 FILE *fp_ou;
414
415 char wdirname[MAXLEN];
416
417 if(indexonly) return;
085c1e1f 418 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 419
10210234 420 if(debug) debuga(_("Making file: %s/%s\n"),tmp,user);
2240dcea 421 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,user)>=sizeof(wdirname)) {
10210234 422 debuga(_("Temporary file name too long: %s/%s.utmp\n"),tmp,user);
2240dcea 423 exit(1);
25697a35
GS
424 }
425
25697a35 426 if((fp_ou=fopen(wdirname,"w"))==NULL){
d5d021c5 427 debuga(_("(report) Cannot open file: %s\n"),wdirname);
25697a35
GS
428 exit(1);
429 }
430
431 fclose(fp_ou);
432 return;
433}
434
435
5f3cfd1d 436static void maketmp_hour(const char *user, const char *dirname, int indexonly)
25697a35
GS
437{
438
439 FILE *fp_ou;
440
441 char wdirname[MAXLEN];
442
443 if(indexonly) return;
085c1e1f 444 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 445
2240dcea 446 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,user)>=sizeof(wdirname)) {
10210234 447 debuga(_("Temporary file name too long: %s/%s.htmp\n"),tmp,user);
2240dcea
FM
448 exit(1);
449 }
25697a35 450
25697a35 451 if((fp_ou=fopen(wdirname,"w"))==NULL){
d5d021c5 452 debuga(_("(report-1) Cannot open file: %s - %s\n"),wdirname,strerror(errno));
25697a35
GS
453 exit(1);
454 }
455
456 fclose(fp_ou);
457 return;
458}
459
460
f2ec8c75 461void gravatmp(const struct userinfostruct *uinfo, const char *oldurl, long long int nacc, long long int nbytes, const char *oldmsg, long long int nelap, int indexonly, long long int incache, long long int oucache)
25697a35 462{
25697a35 463 FILE *fp_ou;
25697a35
GS
464 char wdirname[MAXLEN];
465
466 if(indexonly) return;
085c1e1f 467 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 468
f2ec8c75 469 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
10210234 470 debuga(_("Temporary file name too long: %s/%s.utmp\n"),tmp,uinfo->filename);
0a4e18e1
FM
471 exit(1);
472 }
25697a35 473
b6d3adea 474 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
d5d021c5 475 debuga(_("(report) Cannot open file: %s\n"),wdirname);
25697a35 476 exit(1);
4157aa09 477 }
25697a35 478
e5b2c6f0 479 fprintf(fp_ou,"%lld\t%lld\t%s\t%s\t%lld\t%lld\t%lld\n",nacc,nbytes,oldurl,oldmsg,nelap,incache,oucache);
25697a35
GS
480
481 fclose(fp_ou);
482 ttopen=0;
483
484 if(fp_tt) {
c0ec9cc7 485 fputs("</table>\n</div>\n",fp_tt);
25697a35
GS
486 fputs("</body>\n</html>\n",fp_tt);
487 fclose(fp_tt);
e5b2c6f0 488 fp_tt=NULL;
25697a35
GS
489 }
490
491 return;
25697a35
GS
492}
493
f2ec8c75 494static void gravatmpf(const struct userinfostruct *uinfo,const char *oldurl, long long int nacc, long long int nbytes, const char *oldmsg, long long int nelap, int indexonly, long long int incache, long long int oucache)
25697a35 495{
25697a35 496 FILE *fp_ou;
25697a35
GS
497 char wdirname[MAXLEN];
498
f2ec8c75 499 if(indexonly || (ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 500
f2ec8c75 501 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
10210234 502 debuga(_("Path too long %s/%s.utmp\n"),tmp,uinfo->filename);
0a4e18e1
FM
503 exit(1);
504 }
25697a35 505
b6d3adea 506 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
d5d021c5 507 debuga(_("(report) Cannot open file: %s\n"),wdirname);
25697a35
GS
508 exit(1);
509 }
510
f2ec8c75 511 fprintf(fp_ou,"%lld\t%lld\t%s\t%s\t%lld\t%lld\t%lld\n",nacc,nbytes,oldurl,oldmsg,nelap,incache,oucache);
25697a35
GS
512
513 fclose(fp_ou);
f2ec8c75
FM
514 ttopen=0;
515
516 if(fp_tt) {
517 fputs("</table>\n",fp_tt);
518 fputs("</body>\n</html>\n",fp_tt);
519 fclose(fp_tt);
520 fp_tt=NULL;
521 }
25697a35
GS
522
523 return;
524}
525
526
f2ec8c75 527static void gravatmp_hora(const char *dirname, const struct userinfostruct *uinfo, const char *data, const char *hora, long long int elap, long long int bytes, int indexonly)
25697a35
GS
528{
529
530 FILE *fp_ou;
25697a35
GS
531 char wdirname[MAXLEN];
532
085c1e1f 533 if(indexonly || ((ReportType & REPORT_TYPE_USERS_SITES) == 0)) return;
25697a35 534
f2ec8c75 535 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
10210234 536 debuga(_("Path too long %s/%s.htmp\n"),tmp,uinfo->filename);
0a4e18e1
FM
537 exit(1);
538 }
25697a35 539
b6d3adea 540 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
10210234 541 debuga(_("(report-2) Cannot open file: %s - %s\n"),wdirname,strerror(errno));
25697a35 542 exit(1);
4157aa09 543 }
25697a35 544
f2ec8c75
FM
545 if(strcmp(datetimeby,"bytes") == 0) fprintf(fp_ou,"%s\t%s\t%lld\n",data,hora,bytes);
546 else fprintf(fp_ou,"%s\t%s\t%lld\n",data,hora,elap);
25697a35
GS
547
548 fclose(fp_ou);
549
550 return;
25697a35
GS
551}
552
553
f2ec8c75 554static void gravaporuser(const struct userinfostruct *uinfo, const char *dirname, const char *url, const char *ip, const char *data, const char *hora, long long int tam, long long int elap, int indexonly)
25697a35
GS
555{
556
557 FILE *fp_ou;
25697a35
GS
558 char wdirname[MAXLEN];
559
085c1e1f 560 if(indexonly || ((ReportType & REPORT_TYPE_USERS_SITES) == 0)) return;
25697a35 561
f2ec8c75 562 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.ip",tmp,uinfo->filename)>=sizeof(wdirname)) {
10210234 563 debuga(_("Path too long %s/%s.ip\n"),tmp,uinfo->filename);
0a4e18e1
FM
564 exit(1);
565 }
25697a35 566
b6d3adea 567 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
d5d021c5 568 debuga(_("(report) Cannot open file: %s\n"),wdirname);
25697a35
GS
569 exit(1);
570 }
571
f2ec8c75 572 fprintf(fp_ou,"%s\t%s\t%s\t%s\t%lld\t%lld\n",ip,url,data,hora,tam,elap);
25697a35
GS
573
574 fclose(fp_ou);
25697a35
GS
575
576 return;
4157aa09 577
25697a35
GS
578}
579
580
f2ec8c75 581static void gravager(FILE *fp_gen, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache)
25697a35 582{
f2ec8c75 583 fprintf(fp_gen,"%s\t%lld\t%lld\t%s\t%s\t%s\t%s\t%lld\t%lld\t%lld\n",uinfo->id,nacc,nbytes,url,ip,hora,dia,nelap,incache,oucache);
25697a35 584 return;
25697a35
GS
585}
586
2240dcea
FM
587int ger_read(char *buffer,struct generalitemstruct *item,const char *filename)
588{
589 int i;
590 int sign;
591 long long int number;
592
593 if (strncmp(buffer,"TOTAL\t",6)==0) {
594 item->total=1;
595 buffer+=6;
596
597 sign=+1;
598 if (*buffer == '-') {
599 buffer++;
600 sign=-1;
601 } else if (*buffer == '+') {
602 buffer++;
603 }
604 number=0LL;
605 while (isdigit(*buffer))
606 number=(number * 10) + (*buffer++)-'0';
607 if (*buffer!='\t') {
10210234 608 debuga(_("Invalid total number of accesses in %s\n"),filename);
2240dcea
FM
609 exit(1);
610 }
611 buffer++;
612 item->nacc=number*sign;
613
614 sign=+1;
615 if (*buffer == '-') {
616 buffer++;
617 sign=-1;
618 } else if (*buffer == '+') {
619 buffer++;
620 }
621 number=0LL;
622 while (isdigit(*buffer))
623 number=(number * 10) + (*buffer++)-'0';
624 if (*buffer!='\t') {
10210234 625 debuga(_("Invalid total size in %s\n"),filename);
2240dcea
FM
626 exit(1);
627 }
628 buffer++;
629 item->nbytes=number*sign;
630
631 sign=+1;
632 if (*buffer == '-') {
633 buffer++;
634 sign=-1;
635 } else if (*buffer == '+') {
636 buffer++;
637 }
638 number=0LL;
639 while (isdigit(*buffer))
640 number=(number * 10) + (*buffer++)-'0';
641 if (*buffer!='\t') {
10210234 642 debuga(_("Invalid total elapsed time in %s\n"),filename);
2240dcea
FM
643 exit(1);
644 }
645 buffer++;
646 item->nelap=number*sign;
647
648 sign=+1;
649 if (*buffer == '-') {
650 buffer++;
651 sign=-1;
652 } else if (*buffer == '+') {
653 buffer++;
654 }
655 number=0LL;
656 while (isdigit(*buffer))
657 number=(number * 10) + (*buffer++)-'0';
658 if (*buffer!='\t') {
10210234 659 debuga(_("Invalid total cache hit in %s\n"),filename);
2240dcea
FM
660 exit(1);
661 }
662 buffer++;
663 item->incache=number*sign;
664
665 sign=+1;
666 if (*buffer == '-') {
667 buffer++;
668 sign=-1;
669 } else if (*buffer == '+') {
670 buffer++;
671 }
672 number=0LL;
673 while (isdigit(*buffer))
674 number=(number * 10) + (*buffer++)-'0';
675 if (*buffer!='\0') {
10210234 676 debuga(_("Invalid total cache miss in %s\n"),filename);
2240dcea
FM
677 exit(1);
678 }
679 item->oucache=number*sign;
680 } else {
681 item->total=0;
682
683 item->user=buffer;
684 for (i=0 ; i<MAX_USER_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
685 if (*buffer!='\t') {
10210234 686 debuga(_("User name too long or invalid in %s\n"),filename);
2240dcea
FM
687 exit(1);
688 }
689 *buffer++='\0';
690
691 sign=+1;
692 if (*buffer == '-') {
693 buffer++;
694 sign=-1;
695 } else if (*buffer == '+') {
696 buffer++;
697 }
698 number=0LL;
699 while (isdigit(*buffer))
700 number=(number * 10) + (*buffer++)-'0';
701 if (*buffer!='\t') {
10210234 702 debuga(_("Invalid number of accesses in %s\n"),filename);
2240dcea
FM
703 exit(1);
704 }
705 buffer++;
706 item->nacc=number*sign;
707
708 sign=+1;
709 if (*buffer == '-') {
710 buffer++;
711 sign=-1;
712 } else if (*buffer == '+') {
713 buffer++;
714 }
715 number=0LL;
716 while (isdigit(*buffer))
717 number=(number * 10) + (*buffer++)-'0';
718 if (*buffer!='\t') {
10210234 719 debuga(_("Invalid number of bytes in %s\n"),filename);
2240dcea
FM
720 exit(1);
721 }
722 buffer++;
723 item->nbytes=number*sign;
724
725 item->url=buffer;
726 while ((unsigned char)*buffer>=' ') buffer++;
727 if (*buffer!='\t') {
10210234 728 debuga(_("URL too long or invalid in %s\n"),filename);
2240dcea
FM
729 exit(1);
730 }
731 *buffer++='\0';
732
733 item->ip=buffer;
734 for (i=0 ; i<MAX_IP_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
735 if (*buffer!='\t') {
10210234 736 debuga(_("IP address too long or invalid in %s\n"),filename);
2240dcea
FM
737 exit(1);
738 }
739 *buffer++='\0';
740
741 item->time=buffer;
742 for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
743 if (*buffer!='\t') {
10210234 744 debuga(_("Time too long or invalid in %s\n"),filename);
2240dcea
FM
745 exit(1);
746 }
747 *buffer++='\0';
748
749 item->date=buffer;
750 for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
751 if (*buffer!='\t') {
10210234 752 debuga(_("Date too long or invalid in %s\n"),filename);
2240dcea
FM
753 exit(1);
754 }
755 *buffer++='\0';
756
757 sign=+1;
758 if (*buffer == '-') {
759 buffer++;
760 sign=-1;
761 } else if (*buffer == '+') {
762 buffer++;
763 }
764 number=0LL;
765 while (isdigit(*buffer))
766 number=(number * 10) + (*buffer++)-'0';
767 if (*buffer!='\t') {
10210234 768 debuga(_("Invalid elapsed time in %s\n"),filename);
2240dcea
FM
769 exit(1);
770 }
771 buffer++;
772 item->nelap=number*sign;
773
774 sign=+1;
775 if (*buffer == '-') {
776 buffer++;
777 sign=-1;
778 } else if (*buffer == '+') {
779 buffer++;
780 }
781 number=0LL;
782 while (isdigit(*buffer))
783 number=(number * 10) + (*buffer++)-'0';
784 if (*buffer!='\t') {
10210234 785 debuga(_("Invalid cache hit size in %s\n"),filename);
2240dcea
FM
786 exit(1);
787 }
788 buffer++;
789 item->incache=number*sign;
790
791 sign=+1;
792 if (*buffer == '-') {
793 buffer++;
794 sign=-1;
795 } else if (*buffer == '+') {
796 buffer++;
797 }
798 number=0LL;
799 while (isdigit(*buffer))
800 number=(number * 10) + (*buffer++)-'0';
801 if (*buffer!='\0') {
10210234 802 debuga(_("Invalid cache miss size in %s\n"),filename);
2240dcea
FM
803 exit(1);
804 }
805 item->oucache=number*sign;
806 }
807 return(0);
808}
809
5160b452 810static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart)
25697a35
GS
811{
812
813 FILE *fp_ou;
25697a35
GS
814 char wdirname[MAXLEN];
815
816 sprintf(wdirname,"%s/smartfilter.unsort",dirname);
817
b6d3adea 818 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
d5d021c5 819 debuga(_("(report) Cannot open file: %s\n"),wdirname);
25697a35
GS
820 exit(1);
821 }
822
120d768c 823 fprintf(fp_ou,"%s\t%s\t%s\t%s\t%s\t%s\n",user,data,hora,ip,url,smart);
25697a35
GS
824 fputs("</body>\n</html>\n",fp_tt);
825
826 fclose(fp_ou);
827
828 return;
829
830}