]> git.ipfire.org Git - thirdparty/sarg.git/blame - report.c
Use a substitution to maintain only one list of files to process
[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
d6e703cc 86 sprintf(dirname, "%s%s", outdir, period);
e6414a9d 87 vrfydir(period, addr, site, us, email);
25697a35
GS
88
89 if(debugz){
90 debugaz("dirname",dirname);
25697a35 91 }
48864d28
FM
92
93 gperiod(dirname,period);
25697a35 94
4157aa09 95 if(UserAgentLog[0] != '\0' && email[0] == '\0') useragent();
25697a35 96
0a4e18e1 97 sprintf(wdirname,"%s/sarg-general",dirname);
1bafebef 98 if((fp_gen=MY_FOPEN(wdirname,"w"))==NULL){
0a4e18e1
FM
99 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
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){
8db846b0 121 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],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) {
135 debuga(_("Ignoring unknown user file %s"),user);
136 continue;
137 }
138 strcpy(u2,uinfo->id);
139 if(Ip2Name && uinfo->id_is_ip) ip2name(u2,sizeof(u2));
140 user_find(uinfo->label,MAX_USER_LEN, u2);
141
25697a35
GS
142 strcpy(wdirname,dirname);
143 maketmp(user,tmp,debug,indexonly);
144 maketmp_hour(user,tmp,indexonly);
48864d28 145
25697a35 146 ttopen=0;
e5b2c6f0
FM
147 oldurltt=NULL;
148 ourltt_size=0;
ac422f9b
FM
149 memset(oldaccdiatt,0,sizeof(oldaccdiatt));
150 memset(oldacchoratt,0,sizeof(oldacchoratt));
f2ec8c75 151 new_user=1;
48864d28 152
ac422f9b 153 if (longline_prepare(&line)<0) {
f2ec8c75 154 debuga(_("Not enough memory to read the downloaded files."));
ac422f9b
FM
155 exit(1);
156 }
157
158 while((buf=longline_read(fp_in,&line))!=NULL) {
9c7c6346
FM
159 getword_start(&gwarea,buf);
160 if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
f2ec8c75 161 getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
e5b2c6f0 162 getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
9c7c6346 163 getword(acccode,sizeof(acccode),&gwarea,'\t')<0) {
f2ec8c75 164 debuga(_("There is a broken record or garbage in file %s"),tmp3);
4bcb77cf
FM
165 exit(1);
166 }
d6e703cc 167 if(strncmp(acccode,"TCP_DENIED/407",14) == 0) continue;
e34a7c98 168 if (getword_atoll(&accelap,&gwarea,'\t')<0) {
f2ec8c75 169 debuga(_("There is a broken elapsed time in file %s"),tmp3);
e6414a9d
FM
170 exit(1);
171 }
e34a7c98 172 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
f2ec8c75 173 debuga(_("There is a broken smart info in file %s"),tmp3);
4bcb77cf
FM
174 exit(1);
175 }
48864d28 176
4157aa09 177 if(accsmart[0] != '\0') {
25697a35
GS
178 smartfilter++;
179 strcpy(wdirname,dirname);
f2ec8c75 180 grava_SmartFilter(wdirname,uinfo->id,accip,accdia,acchora,accurl,accsmart);
25697a35
GS
181 }
182
246c8489 183 if(Ip2Name) {
25697a35
GS
184 if(strcmp(accip,ipantes) != 0) {
185 strcpy(ipantes,accip);
a1c55d8c 186 ip2name(accip,sizeof(accip));
25697a35
GS
187 strcpy(nameantes,accip);
188 } else strcpy(accip,nameantes);
189 }
48864d28 190
25697a35 191 strcpy(wdirname,dirname);
f2ec8c75 192 gravatmp_hora(wdirname,uinfo,accdia,acchora,accelap,accbytes,indexonly);
25697a35
GS
193
194 if(iprel){
195 strcpy(wdirname,dirname);
f2ec8c75 196 gravaporuser(uinfo,wdirname,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) {
205 debuga(_("Not enough memory to store the url"));
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)
224 sprintf(oldmsg,"%s",text[46]);
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)
ac422f9b
FM
236 strcpy(oldmsg,text[46]);
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));
f2ec8c75 257 snprintf(arqtt,sizeof(arqtt),"%s/%s",dirname,uinfo->filename);
5f3cfd1d
FM
258 if(access(arqtt, R_OK) != 0)
259 my_mkdir(arqtt);
f2ec8c75 260 snprintf(arqtt,sizeof(arqtt),"%s/%s/tt%s-%s.html",dirname,uinfo->filename,uinfo->filename,siteind);
5f3cfd1d
FM
261 if ((fp_tt = fopen(arqtt, "w")) == 0) {
262 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],arqtt);
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
FM
275 write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report"));
276 fprintf(fp_tt,"<tr><td class=\"header_l\">%s:&nbsp;%s</td></tr>\n",text[89],period);
f2ec8c75 277 fprintf(fp_tt,"<tr><td class=\"header_l\">%s:&nbsp;%s</td></tr>\n",text[90],uinfo->label);
c0ec9cc7
FM
278 fprintf(fp_tt,"<tr><td class=\"header_l\">%s:&nbsp;%s, %s</td></tr>\n",text[104],UserSortField,UserSortOrder);
279 fprintf(fp_tt,"<tr><th class=\"header_c\">%s</th></tr>\n",text[32]);
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);
5f3cfd1d 284 strncpy(tmp4,text[110],4);
2240dcea 285 tmp4[4]='\0';
b18ce4a0 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",text[91],tmp4,text[110]+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) {
298 debuga(_("Not enough memory to store the url"));
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;
5f3cfd1d 316 strcpy(wdirname,dirname);
f2ec8c75 317 day_totalize(tmp,puinfo,indexonly);
5f3cfd1d
FM
318 }
319
f2ec8c75 320 puinfo=uinfo;
5f3cfd1d 321 strcpy(oldacccode,acccode);
5f3cfd1d 322 strcpy(oldaccip,accip);
1bafebef
FM
323 if (!same_url) {
324 url_len=strlen(accurl);
325 if (url_len>=ourl_size) {
326 ourl_size=url_len+1;
327 oldurl=realloc(oldurl,ourl_size);
328 if (!oldurl) {
329 debuga(_("Not enough memory to store the url"));
330 exit(1);
331 }
e5b2c6f0 332 }
1bafebef 333 strcpy(oldurl,accurl);
e5b2c6f0 334 }
5f3cfd1d
FM
335 strcpy(oldaccdia,accdia);
336 strcpy(oldacchora,acchora);
25697a35 337
5f3cfd1d 338 }
5f3cfd1d 339 fclose(fp_in);
ac422f9b 340 longline_free(&line);
e5b2c6f0 341 if (oldurltt) free(oldurltt);
1bafebef 342 unlink(tmp3);
25697a35 343 }
05b90947 344 closedir(dirp);
25697a35 345
e5b2c6f0
FM
346 if (oldurl) {
347 if(strstr(oldacccode,"DENIED") != 0)
348 strcpy(oldmsg,text[46]);
349 else
350 strcpy(oldmsg,"OK");
351 strcpy(wdirname,dirname);
f2ec8c75 352 gravatmpf(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache);
e5b2c6f0 353 strcpy(wdirname,dirname);
f2ec8c75 354 gravager(fp_gen,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
e5b2c6f0
FM
355 free(oldurl);
356 }
0a4e18e1 357 fclose(fp_gen);
f2ec8c75 358 if (puinfo) day_totalize(tmp,puinfo,indexonly);
25697a35
GS
359
360 tmpsort();
361
362 strcpy(wdirname,dirname);
363 totalger(wdirname, debug, outdir);
364
5542cb59 365 if(email[0] == '\0') {
085c1e1f 366 if((ReportType & REPORT_TYPE_DOWNLOADS) != 0) download_report();
25697a35 367
65740c50 368 if(DansGuardianConf[0] != '\0') {
25697a35
GS
369 strcpy(wdirname,dirname);
370 dansguardian_log();
371 }
372
491b862f
GS
373 strcpy(wdirname,dirname);
374 squidguard_log();
25697a35
GS
375
376 strcpy(wdirname,dirname);
377 topuser();
378
085c1e1f 379 if((ReportType & REPORT_TYPE_TOPSITES) != 0) topsites();
25697a35 380
085c1e1f 381 if((ReportType & REPORT_TYPE_SITES_USERS) != 0) siteuser();
25697a35
GS
382 gen_denied_report();
383
384 strcpy(wdirname,dirname);
385 authfail_report();
386
d6e703cc 387 if(smartfilter) smartfilter_report();
25697a35 388
65740c50 389 if(DansGuardianConf[0] != '\0') dansguardian_report();
25697a35 390
491b862f 391 squidguard_report();
25697a35 392
085c1e1f 393 if((ReportType & REPORT_TYPE_USERS_SITES) != 0) htmlrel();
25697a35 394
491b862f 395 make_index();
25697a35 396
e6414a9d 397 if(SuccessfulMsg) fprintf(stderr, "SARG: %s %s\n",text[47],dirname);
25697a35
GS
398 } else {
399 strcpy(wdirname,dirname);
400 geramail(wdirname, debug, outdir, userip, email, TempDir);
401
e6414a9d 402 if((strcmp(email,"stdout") != 0) && SuccessfulMsg)
25697a35 403 fprintf(stderr, "SARG: %s %s\n",text[48],email);
25697a35
GS
404 }
405
406 if(indexonly) {
407 strcpy(wdirname,dirname);
408 index_only(wdirname, debug);
409 }
410
c274f011
FM
411 /*
412 2009-10-13(Frederic) This piece of code is never called so it is commented out for good.
d6e703cc
FM
413 if(strlen(email) < 0)
414 removetmp(dirname);
c274f011 415 */
25697a35
GS
416
417 return;
418}
419
5f3cfd1d 420static void maketmp(const char *user, const char *dirname, int debug, int indexonly)
25697a35
GS
421{
422
423 FILE *fp_ou;
424
425 char wdirname[MAXLEN];
426
427 if(indexonly) return;
085c1e1f 428 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 429
2240dcea
FM
430 if(debug) debuga("%s: %s/%s",text[49],tmp,user);
431 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,user)>=sizeof(wdirname)) {
432 debuga(_("Temporary file name too long: %s/%s.utmp"),tmp,user);
433 exit(1);
25697a35
GS
434 }
435
25697a35
GS
436 if((fp_ou=fopen(wdirname,"w"))==NULL){
437 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
438 exit(1);
439 }
440
441 fclose(fp_ou);
442 return;
443}
444
445
5f3cfd1d 446static void maketmp_hour(const char *user, const char *dirname, int indexonly)
25697a35
GS
447{
448
449 FILE *fp_ou;
450
451 char wdirname[MAXLEN];
452
453 if(indexonly) return;
085c1e1f 454 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 455
2240dcea
FM
456 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,user)>=sizeof(wdirname)) {
457 debuga(_("Temporary file name too long: %s/%s.htmp"),tmp,user);
458 exit(1);
459 }
25697a35 460
25697a35
GS
461 if((fp_ou=fopen(wdirname,"w"))==NULL){
462 fprintf(stderr, "SARG: (report-1) %s: %s - %s\n",text[45],wdirname,strerror(errno));
463 exit(1);
464 }
465
466 fclose(fp_ou);
467 return;
468}
469
470
f2ec8c75 471void 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 472{
25697a35 473 FILE *fp_ou;
25697a35
GS
474 char wdirname[MAXLEN];
475
476 if(indexonly) return;
085c1e1f 477 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 478
f2ec8c75
FM
479 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
480 debuga(_("Temporary file name too long: %s/%s.utmp"),tmp,uinfo->filename);
0a4e18e1
FM
481 exit(1);
482 }
25697a35 483
b6d3adea 484 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
25697a35
GS
485 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
486 exit(1);
4157aa09 487 }
25697a35 488
e5b2c6f0 489 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
490
491 fclose(fp_ou);
492 ttopen=0;
493
494 if(fp_tt) {
c0ec9cc7 495 fputs("</table>\n</div>\n",fp_tt);
25697a35
GS
496 fputs("</body>\n</html>\n",fp_tt);
497 fclose(fp_tt);
e5b2c6f0 498 fp_tt=NULL;
25697a35
GS
499 }
500
501 return;
25697a35
GS
502}
503
f2ec8c75 504static 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 505{
25697a35 506 FILE *fp_ou;
25697a35
GS
507 char wdirname[MAXLEN];
508
f2ec8c75 509 if(indexonly || (ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 510
f2ec8c75
FM
511 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
512 debuga(_("Path too long %s/%s.utmp"),tmp,uinfo->filename);
0a4e18e1
FM
513 exit(1);
514 }
25697a35 515
b6d3adea 516 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
f2ec8c75 517 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
25697a35
GS
518 exit(1);
519 }
520
f2ec8c75 521 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
522
523 fclose(fp_ou);
f2ec8c75
FM
524 ttopen=0;
525
526 if(fp_tt) {
527 fputs("</table>\n",fp_tt);
528 fputs("</body>\n</html>\n",fp_tt);
529 fclose(fp_tt);
530 fp_tt=NULL;
531 }
25697a35
GS
532
533 return;
534}
535
536
f2ec8c75 537static 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
538{
539
540 FILE *fp_ou;
25697a35
GS
541 char wdirname[MAXLEN];
542
085c1e1f 543 if(indexonly || ((ReportType & REPORT_TYPE_USERS_SITES) == 0)) return;
25697a35 544
f2ec8c75
FM
545 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
546 debuga(_("Path too long %s/%s.htmp"),tmp,uinfo->filename);
0a4e18e1
FM
547 exit(1);
548 }
25697a35 549
b6d3adea 550 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
f2ec8c75 551 fprintf(stderr, "SARG: (report-2) %s: %s - %s\n",text[45],wdirname,strerror(errno));
25697a35 552 exit(1);
4157aa09 553 }
25697a35 554
f2ec8c75
FM
555 if(strcmp(datetimeby,"bytes") == 0) fprintf(fp_ou,"%s\t%s\t%lld\n",data,hora,bytes);
556 else fprintf(fp_ou,"%s\t%s\t%lld\n",data,hora,elap);
25697a35
GS
557
558 fclose(fp_ou);
559
560 return;
25697a35
GS
561}
562
563
f2ec8c75 564static 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
565{
566
567 FILE *fp_ou;
25697a35
GS
568 char wdirname[MAXLEN];
569
085c1e1f 570 if(indexonly || ((ReportType & REPORT_TYPE_USERS_SITES) == 0)) return;
25697a35 571
f2ec8c75
FM
572 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.ip",tmp,uinfo->filename)>=sizeof(wdirname)) {
573 fprintf(stderr,"SARG: Path too long %s/%s.ip\n",tmp,uinfo->filename);
0a4e18e1
FM
574 exit(1);
575 }
25697a35 576
b6d3adea 577 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
25697a35
GS
578 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
579 exit(1);
580 }
581
f2ec8c75 582 fprintf(fp_ou,"%s\t%s\t%s\t%s\t%lld\t%lld\n",ip,url,data,hora,tam,elap);
25697a35
GS
583
584 fclose(fp_ou);
25697a35
GS
585
586 return;
4157aa09 587
25697a35
GS
588}
589
590
f2ec8c75 591static 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 592{
f2ec8c75 593 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 594 return;
25697a35
GS
595}
596
2240dcea
FM
597int ger_read(char *buffer,struct generalitemstruct *item,const char *filename)
598{
599 int i;
600 int sign;
601 long long int number;
602
603 if (strncmp(buffer,"TOTAL\t",6)==0) {
604 item->total=1;
605 buffer+=6;
606
607 sign=+1;
608 if (*buffer == '-') {
609 buffer++;
610 sign=-1;
611 } else if (*buffer == '+') {
612 buffer++;
613 }
614 number=0LL;
615 while (isdigit(*buffer))
616 number=(number * 10) + (*buffer++)-'0';
617 if (*buffer!='\t') {
618 debuga(_("Invalid total number of accesses in %s"),filename);
619 exit(1);
620 }
621 buffer++;
622 item->nacc=number*sign;
623
624 sign=+1;
625 if (*buffer == '-') {
626 buffer++;
627 sign=-1;
628 } else if (*buffer == '+') {
629 buffer++;
630 }
631 number=0LL;
632 while (isdigit(*buffer))
633 number=(number * 10) + (*buffer++)-'0';
634 if (*buffer!='\t') {
635 debuga(_("Invalid total size in %s"),filename);
636 exit(1);
637 }
638 buffer++;
639 item->nbytes=number*sign;
640
641 sign=+1;
642 if (*buffer == '-') {
643 buffer++;
644 sign=-1;
645 } else if (*buffer == '+') {
646 buffer++;
647 }
648 number=0LL;
649 while (isdigit(*buffer))
650 number=(number * 10) + (*buffer++)-'0';
651 if (*buffer!='\t') {
652 debuga(_("Invalid total elapsed time in %s"),filename);
653 exit(1);
654 }
655 buffer++;
656 item->nelap=number*sign;
657
658 sign=+1;
659 if (*buffer == '-') {
660 buffer++;
661 sign=-1;
662 } else if (*buffer == '+') {
663 buffer++;
664 }
665 number=0LL;
666 while (isdigit(*buffer))
667 number=(number * 10) + (*buffer++)-'0';
668 if (*buffer!='\t') {
669 debuga(_("Invalid total cache hit in %s"),filename);
670 exit(1);
671 }
672 buffer++;
673 item->incache=number*sign;
674
675 sign=+1;
676 if (*buffer == '-') {
677 buffer++;
678 sign=-1;
679 } else if (*buffer == '+') {
680 buffer++;
681 }
682 number=0LL;
683 while (isdigit(*buffer))
684 number=(number * 10) + (*buffer++)-'0';
685 if (*buffer!='\0') {
686 debuga(_("Invalid total cache miss in %s"),filename);
687 exit(1);
688 }
689 item->oucache=number*sign;
690 } else {
691 item->total=0;
692
693 item->user=buffer;
694 for (i=0 ; i<MAX_USER_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
695 if (*buffer!='\t') {
696 debuga(_("User name too long or invalid in %s"),filename);
697 exit(1);
698 }
699 *buffer++='\0';
700
701 sign=+1;
702 if (*buffer == '-') {
703 buffer++;
704 sign=-1;
705 } else if (*buffer == '+') {
706 buffer++;
707 }
708 number=0LL;
709 while (isdigit(*buffer))
710 number=(number * 10) + (*buffer++)-'0';
711 if (*buffer!='\t') {
712 debuga(_("Invalid number of accesses in %s"),filename);
713 exit(1);
714 }
715 buffer++;
716 item->nacc=number*sign;
717
718 sign=+1;
719 if (*buffer == '-') {
720 buffer++;
721 sign=-1;
722 } else if (*buffer == '+') {
723 buffer++;
724 }
725 number=0LL;
726 while (isdigit(*buffer))
727 number=(number * 10) + (*buffer++)-'0';
728 if (*buffer!='\t') {
729 debuga(_("Invalid number of bytes in %s"),filename);
730 exit(1);
731 }
732 buffer++;
733 item->nbytes=number*sign;
734
735 item->url=buffer;
736 while ((unsigned char)*buffer>=' ') buffer++;
737 if (*buffer!='\t') {
738 debuga(_("URL too long or invalid in %s"),filename);
739 exit(1);
740 }
741 *buffer++='\0';
742
743 item->ip=buffer;
744 for (i=0 ; i<MAX_IP_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
745 if (*buffer!='\t') {
746 debuga(_("IP address too long or invalid in %s"),filename);
747 exit(1);
748 }
749 *buffer++='\0';
750
751 item->time=buffer;
752 for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
753 if (*buffer!='\t') {
754 debuga(_("Time too long or invalid in %s"),filename);
755 exit(1);
756 }
757 *buffer++='\0';
758
759 item->date=buffer;
760 for (i=0 ; i<MAX_DATETIME_LEN-1 && (unsigned char)*buffer>=' ' ; i++) buffer++;
761 if (*buffer!='\t') {
762 debuga(_("Date too long or invalid in %s"),filename);
763 exit(1);
764 }
765 *buffer++='\0';
766
767 sign=+1;
768 if (*buffer == '-') {
769 buffer++;
770 sign=-1;
771 } else if (*buffer == '+') {
772 buffer++;
773 }
774 number=0LL;
775 while (isdigit(*buffer))
776 number=(number * 10) + (*buffer++)-'0';
777 if (*buffer!='\t') {
778 debuga(_("Invalid elapsed time in %s"),filename);
779 exit(1);
780 }
781 buffer++;
782 item->nelap=number*sign;
783
784 sign=+1;
785 if (*buffer == '-') {
786 buffer++;
787 sign=-1;
788 } else if (*buffer == '+') {
789 buffer++;
790 }
791 number=0LL;
792 while (isdigit(*buffer))
793 number=(number * 10) + (*buffer++)-'0';
794 if (*buffer!='\t') {
795 debuga(_("Invalid cache hit size in %s"),filename);
796 exit(1);
797 }
798 buffer++;
799 item->incache=number*sign;
800
801 sign=+1;
802 if (*buffer == '-') {
803 buffer++;
804 sign=-1;
805 } else if (*buffer == '+') {
806 buffer++;
807 }
808 number=0LL;
809 while (isdigit(*buffer))
810 number=(number * 10) + (*buffer++)-'0';
811 if (*buffer!='\0') {
812 debuga(_("Invalid cache miss size in %s"),filename);
813 exit(1);
814 }
815 item->oucache=number*sign;
816 }
817 return(0);
818}
819
5160b452 820static 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
821{
822
823 FILE *fp_ou;
25697a35
GS
824 char wdirname[MAXLEN];
825
826 sprintf(wdirname,"%s/smartfilter.unsort",dirname);
827
b6d3adea 828 if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){
25697a35
GS
829 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
830 exit(1);
831 }
832
120d768c 833 fprintf(fp_ou,"%s\t%s\t%s\t%s\t%s\t%s\n",user,data,hora,ip,url,smart);
25697a35
GS
834 fputs("</body>\n</html>\n",fp_tt);
835
836 fclose(fp_ou);
837
838 return;
839
840}