]>
git.ipfire.org Git - thirdparty/sarg.git/blob - html.c
18b8dffaa188c968ec82591d8160ed1d09e3f3dc
2 * AUTHOR: Pedro Lineu Orso orso@brturbo.com.br
4 * SARG Squid Analysis Report Generator http://sarg-squid.org
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
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.
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.
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.
26 #include "include/conf.h"
31 FILE *fp_in
, *fp_ou
, *fp_ip
, *fp_ip2
, *fp_usr
, *fp_tt
;
33 struct dirent
*direntp
;
34 long long int nnbytes
=0, unbytes
=0, tnbytes
=0, totbytes
=0, totbytes2
=0;
35 long long int totelap
=0, totelap2
=0, nnelap
=0, unelap
=0, tnelap
=0;
36 long long int incache
=0, oucache
=0, tnincache
=0, tnoucache
=0, twork
=0, twork2
=0;
37 char arqin
[MAXLEN
], arqou
[MAXLEN
], arqper
[MAXLEN
], arqip
[MAXLEN
];
38 char nacc
[20], nbytes
[20], url
[1024], purl
[1024], tmsg
[30], tmsg2
[30], nelap
[20], csort
[MAXLEN
];
39 char periodo
[MAXLEN
], usuario
[MAXLEN
], wusuario
[MAXLEN
], u2
[MAXLEN
], duser
[MAXLEN
];
40 char userbytes
[20], userelap
[20], userurl
[1024], userhora
[9], userdia
[9];
41 char user_ip
[MAXLEN
], olduserip
[MAXLEN
], tmp2
[MAXLEN
], tmp3
[MAXLEN
], incac
[20], oucac
[20];
42 char denied_report
[255];
43 char ttd1
[3], ttd2
[3], ttd3
[5], ttt1
[3], ttt2
[3], ttt3
[3];
45 char ftime
[128], warea
[MAXLEN
];
46 char wtemp
[MAXLEN
], totuser
[8];
47 long long int tnacc
=0, ttnacc
=0, unacc
=0;
48 float perc
=0, perc2
=0, ouperc
=0, inperc
=0;
53 if(strstr(ReportType
,"users_sites") == 0) return;
56 strcat(tmp2
,"/sargtmp.unsort");
59 strcat(tmp3
,"/sargtmp.log");
61 strcpy(arqper
,dirname
);
62 strcat(arqper
,"/periodo");
64 if ((fp_in
= fopen(arqper
, "r")) == 0){
65 fprintf(stderr
, "SARG: (html1) %s: %s\n",text
[45],arqper
);
69 fgets(periodo
,sizeof(periodo
),fp_in
);
72 strcpy(arqper
,dirname
);
73 strcat(arqper
,"/geral");
75 if ((fp_in
= fopen(arqper
, "r")) == 0){
76 fprintf(stderr
, "SARG: (html2) %s: %s\n",text
[45],arqper
);
80 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
81 if(strstr(buf
,"TOTAL") == 0) {
82 getword(wtemp
,buf
,' ');
83 ttnacc
+=my_atoll(buf
);
84 getword(wtemp
,buf
,' ');
85 getword(wtemp
,buf
,' ');
86 totbytes
+=my_atoll(wtemp
);
87 getword(wtemp
,buf
,' ');
88 getword(wtemp
,buf
,' ');
89 getword(wtemp
,buf
,' ');
90 getword(wtemp
,buf
,' ');
91 getword(wtemp
,buf
,' ');
92 totelap
+=my_atoll(wtemp
);
99 while ( (direntp
= readdir( dirp
)) != NULL
) {
100 if(strstr(direntp
->d_name
,".txt") == 0)
104 strcpy(usuario
,direntp
->d_name
);
108 getword(warea
,usuario
,'.');
109 strcpy(denied_report
,warea
);
110 strcat(wusuario
,warea
);
112 if(strcmp(usuario
,"txt") !=0) {
113 strcat(wusuario
,".");
117 sprintf(warea
,"%s/%s",dirname
,wusuario
);
120 report_day(wusuario
, iprel
, userip
);
121 greport_day(wusuario
, iprel
, userip
);
123 strcpy(usuario
,wusuario
);
126 strcpy(arqou
,dirname
);
128 strcat(arqou
,usuario
);
130 strcat(arqou
,usuario
);
131 strcat(arqou
,".html");
133 strcat(duser
,"denied_");
134 strcat(arqin
,direntp
->d_name
);
136 if((str
=(char *) strstr(denied_report
, "_")) != (char *) NULL
) {
137 if((str
=(char *) strstr(str
+1, "_")) != (char *) NULL
)
138 fixip(denied_report
);
140 strcat(duser
,denied_report
);
141 strcat(duser
,".html");
142 if(access(duser
, R_OK
) != 0)
143 denied_report
[0]='\0';
145 if ((fp_in
= fopen(arqin
, "r")) == 0){
146 fprintf(stderr
, "SARG: (html3) %s: %s\n",text
[45],arqin
);
150 while(fgets(buf
,sizeof(buf
),fp_in
)!=NULL
) {
151 getword(wtemp
,buf
,' ');
152 tnacc
+=my_atoll(wtemp
);
153 getword(wtemp
,buf
,' ');
154 tnbytes
+=my_atoll(wtemp
);
155 getword(wtemp
,buf
,' ');
156 getword(wtemp
,buf
,' ');
157 getword(wtemp
,buf
,' ');
158 tnelap
+=my_atoll(wtemp
);
159 getword(wtemp
,buf
,' ');
160 tnincache
+=my_atoll(wtemp
);
161 getword(wtemp
,buf
,' ');
162 tnoucache
+=my_atoll(wtemp
);
167 if ((fp_in
= fopen(arqin
, "r")) == 0){
168 fprintf(stderr
, "SARG: (html4) %s: %s\n",text
[45],arqin
);
172 if ((fp_ou
= fopen(arqou
, "w")) == 0){
173 fprintf(stderr
, "SARG: (html5) %s: %s\n",text
[45],arqou
);
177 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"",fp_ou
);
178 fputs(" \"http://www.w3.org/TR/html4/loose.dtd\">\n",fp_ou
);
179 fputs("<html>\n",fp_ou
);
180 fputs("<head>\n",fp_ou
);
181 sprintf(html
," <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet
);
184 fputs("</head>\n",fp_ou
);
186 sprintf(buf
,"<body style=\"font-family:%s;font-size:%s;background-color:%s; \
187 background-image:url(%s)\">\n",FontFace
,TitleFontSize
,BgColor
,BgImage
);
190 if(strlen(LogoImage
) > 0) {
191 fputs("<center><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou
);
192 sprintf(html
,"<tr><th align=left><img src='%s' border=0 align=absmiddle width=%s height=%s><font color=%s>%s</font>\n",LogoImage
,Width
,Height
,LogoTextColor
,LogoText
);
194 fputs("</table>\n",fp_ou
);
197 show_sarg(fp_ou
,"../..");
198 fputs("<center><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou
);
199 sprintf(html
,"<tr><th class=\"title\" colspan=\"2\">%s</th></tr>\n",Title
);
207 if(strcmp(Ip2Name
,"yes") == 0)
210 if(UserTabFile
[0] != '\0') {
211 sprintf(warea
,":%s:",u2
);
212 if((str
=(char *) strstr(userfile
,warea
)) != (char *) NULL
) {
214 str2
=(char *) strstr(str
+1,":");
217 while(str2
[z1
] != ':') {
221 } else strcpy(name
,u2
);
222 } else strcpy(name
,u2
);
224 sprintf(html
,"<tr><td class=\"header\" colspan=\"2\">%s: %s</td></tr>\n",text
[89],periodo
);
226 sprintf(html
,"<tr><td class=\"header\" colspan=\"2\">%s: %s</td></tr>\n",text
[90],name
);
228 sprintf(html
,"<tr><td class=\"header\" colspan=\"2\">%s: %s, %s</td></tr>\n",text
[104],UserSortField
,UserSortOrder
);
230 sprintf(html
,"<tr><td class=\"header3\" colspan=2>%s %s</td></tr>\n",text
[32],text
[55]);
232 fputs("<tr><td></td></tr>\n",fp_ou
);
234 fputs("</table></center>\n",fp_ou
);
235 fputs("<center><table cellpadding=\"1\" cellspaging=\"2\">\n",fp_ou
);
237 if(strlen(denied_report
) > 0) {
238 sprintf(html
,"<tr><td class=\"header\" colspan=11><a href=\"denied_%s.html\">%s</a> %s</td></tr>\n",denied_report
,text
[116],text
[55]);
242 strcpy(val3
,text
[92]);
243 strcpy(val4
,text
[93]);
244 sprintf(val11
,"%%%s",text
[93]);
245 sprintf(val5
,"%s-%s-%s",text
[113],text
[114],text
[112]);
246 strcpy(val6
,text
[94]);
247 strcpy(val7
,text
[95]);
248 sprintf(val8
,"%%%s",text
[99]);
249 strcpy(val9
,"colspan=2");
252 if(strstr(UserReportFields
,"CONNECT") == 0)
254 if(strstr(UserReportFields
,"BYTES") == 0)
256 if(strstr(UserReportFields
,"SETYB") == 0)
258 if(strstr(UserReportFields
,"IN-CACHE-OUT") == 0) {
261 strcpy(val10
,"<td></td>");
263 if(strstr(UserReportFields
,"USED_TIME") == 0)
265 if(strstr(UserReportFields
,"MILISEC") == 0)
267 if(strstr(UserReportFields
,"%TIME") == 0)
270 sprintf(html
,"<tr><th></th><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header3\" %s>%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th></tr>\n",text
[91],val3
,val4
,val11
,val9
,val5
,val6
,val7
,val8
);
277 sprintf(msg
, "%s: %s",text
[61],u2
);
279 sprintf(msg
, "%s: %s",text
[61],usuario
);
283 fscanf(fp_in
,"%s",nacc
);
284 fscanf(fp_in
,"%s",nbytes
);
285 fscanf(fp_in
,"%s",url
);
286 fscanf(fp_in
,"%s",tmsg
);
287 fscanf(fp_in
,"%s",nelap
);
288 fscanf(fp_in
,"%s",incac
);
289 fscanf(fp_in
,"%s",oucac
);
291 while(!feof(fp_in
)) {
292 if(strncmp(tmsg
,"OK",2) != 0)
293 sprintf(tmsg
,"<td class=\"data\">%s</td>",text
[46]);
294 else bzero(tmsg
, 50);
296 nnbytes
=my_atoll(nbytes
);
297 nnelap
=my_atoll(nelap
);
298 incache
=my_atoll(incac
);
299 oucache
=my_atoll(oucac
);
311 perc2
=perc2
/ tnelap
;
315 inperc
=incache
* 100;
316 inperc
=inperc
/ nnbytes
;
320 ouperc
=oucache
* 100;
321 ouperc
=ouperc
/ nnbytes
;
324 twork
=my_atoll(nacc
);
325 sprintf(wwork1
,"%s",fixnum(twork
,1));
326 sprintf(wwork2
,"%s",fixnum(nnbytes
,1));
327 sprintf(wwork3
,"%s",fixnum(nnelap
,1));
329 if(strcmp(LongUrl
,"yes") != 0) {
330 getword(warea
,url
,'/');
331 sprintf(url
,"%s",warea
);
335 url_module(url
, module
);
336 getword(warea
,url
,'/');
337 sprintf(url
,"%s...%s",warea
,module
);
340 if(strcmp(Privacy
,"yes") == 0)
341 sprintf(httplink
,"<font color=%s><href=http://%s>%s", \
342 PrivacyStringColor
,PrivacyString
,PrivacyString
);
344 if(strlen(BlockIt
) > 0)
345 sprintf(httplink
,"<a href=\"%s%s?url=%s\"><img src=\"../../images/sarg-squidguard-block.png\" border=\"0\"></a><a href=http://%s> %s</a>",wwwDocumentRoot
,BlockIt
,urly
,urly
,urly
);
347 sprintf(httplink
,"<a href=http://%s title=\"%s\">%s</a>",urly
,urly
,url
);
349 if(strstr(ReportType
,"site_user_time_date") != 0) {
350 sprintf(ltext110
,"%s",text
[110]);
352 for(s
=ltext110
; *s
; ++s
)
355 strcpy(siteind
,urly
);
357 for(z1
=0; z1
<strlen(str
); z1
++) {
358 if(str
[z1
]=='?' || str
[z1
]=='-' || str
[z1
]=='.' || str
[z1
]==':' || str
[z1
]=='/' || str
[z1
]=='\\')
361 sprintf(href2
,"<a href=\"tt%s-%s.html\"><img src=\"../../images/datetime.png\" border=\"0\" title=\"%s %s\"></a>",usuario
,siteind
,ltext110
,text
[55]);
363 bzero(href2
, MAXLEN
);
367 sprintf(val2
,"%s",href2
);
368 sprintf(val3
,"%3.2f%%",perc
);
369 sprintf(val4
,"%3.2f%%",inperc
);
370 sprintf(val5
,"%3.2f%%",ouperc
);
371 sprintf(val6
,"%s",buildtime(nnelap
));
372 sprintf(val7
,"%3.2f%%",perc2
);
374 if(strstr(UserReportFields
,"CONNECT") == 0) {
378 if(strstr(UserReportFields
,"BYTES") == 0) {
382 if(strstr(UserReportFields
,"MILISEC") == 0) {
386 if(strstr(UserReportFields
,"SETYB") == 0) {
390 if(strstr(UserReportFields
,"IN-CACHE-OUT") == 0) {
396 if(strstr(UserReportFields
,"USED_TIME") == 0) {
400 if(strstr(UserReportFields
,"%TIME") == 0) {
404 if(strncmp(tmsg
," ",1) == 0)
407 sprintf(html
,"<tr><td class=\"data\">%s</td><td class=\"data2\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td>%s</tr>\n",val2
,httplink
,wwork1
,wwork2
,val3
,val4
,val5
,val6
,wwork3
,val7
,tmsg
);
409 if(UserReportLimit
) {
410 if(count
<= UserReportLimit
) {
414 } else fputs(html
,fp_ou
);
419 strcat(arqip
,usuario
);
422 if ((fp_ip
= fopen(arqip
, "r")) == 0){
423 fprintf(stderr
, "SARG: (html6) %s: %s\n",text
[45],arqip
);
427 if ((fp_ip2
= fopen(tmp2
, "a")) == 0){
428 fprintf(stderr
, "SARG: (html7) %s: %s\n",text
[45],tmp2
);
432 while(fgets(buf
,sizeof(buf
),fp_ip
)!=NULL
) {
433 if(strstr(buf
,url
) != 0)
440 sprintf(csort
,"sort -n -T %s -k 1,1 -k 5,5 -o %s %s",TempDir
,tmp3
,tmp2
);
443 if ((fp_ip
= fopen(tmp3
, "r")) == 0) {
444 fprintf(stderr
, "SARG: (html8) %s: %s\n",text
[45],tmp3
);
448 fscanf(fp_ip
,"%s",user_ip
);
449 fscanf(fp_ip
,"%s",userurl
);
450 fscanf(fp_ip
,"%s",userdia
);
451 fscanf(fp_ip
,"%s",userhora
);
452 fscanf(fp_ip
,"%s",userbytes
);
453 fscanf(fp_ip
,"%s",userelap
);
455 strcpy(olduserip
,user_ip
);
457 while(!feof(fp_ip
)) {
458 if(strcmp(user_ip
,olduserip
) != 0) {
459 my_lltoa(unelap
,val2
,0);
460 sprintf(wwork1
,"%s",fixnum(unbytes
,1));
461 sprintf(html
,"<tr><td></td><td class=\"data\">%s</td><td></td><td class=\"data\">%s</td><td></td><td></td><td></td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",olduserip
,wwork1
,buildtime(unelap
),val2
);
464 strcpy(olduserip
,user_ip
);
470 unbytes
=unbytes
+my_atoll(userbytes
);
471 unelap
=unelap
+my_atoll(userelap
);
473 fscanf(fp_ip
,"%s",user_ip
);
474 fscanf(fp_ip
,"%s",userurl
);
475 fscanf(fp_ip
,"%s",userdia
);
476 fscanf(fp_ip
,"%s",userhora
);
477 fscanf(fp_ip
,"%s",userbytes
);
478 fscanf(fp_ip
,"%s",userelap
);
487 my_lltoa(unelap
,val3
,0);
488 sprintf(wwork1
,"%s",fixnum(unbytes
,1));
489 sprintf(html
,"<tr><td></td><td class=\"data\">%s</td><td></td><td class=\"data\">%s</td><td></td><td></td><td></td><td class=\"data\">%s</td><td class=\"data\">%s</font></td></tr>\n",olduserip
,wwork1
,buildtime(unelap
),val3
);
497 fscanf(fp_in
,"%s",nacc
);
498 fscanf(fp_in
,"%s",nbytes
);
499 fscanf(fp_in
,"%s",url
);
500 fscanf(fp_in
,"%s",tmsg
);
501 fscanf(fp_in
,"%s",nelap
);
502 fscanf(fp_in
,"%s",incac
);
503 fscanf(fp_in
,"%s",oucac
);
504 // fscanf(fp_in,"%s",datestimes);
519 perc2
=tnelap
/ perc2
;
523 ouperc
=tnoucache
* 100;
524 ouperc
=ouperc
/ tnbytes
;
528 inperc
=tnincache
* 100;
529 inperc
=inperc
/ tnbytes
;
532 sprintf(wwork1
,"%s",fixnum(tnacc
,1));
533 sprintf(wwork2
,"%s",fixnum(tnbytes
,1));
534 sprintf(wwork3
,"%s",fixnum(tnelap
,1));
536 sprintf(val2
,"%s%s",href2
,ltext110
);
537 sprintf(val3
,"%3.2f%%",perc
);
538 sprintf(val4
,"%3.2f%%",inperc
);
539 sprintf(val5
,"%3.2f%%",ouperc
);
540 sprintf(val6
,"%s",buildtime(tnelap
));
541 sprintf(val7
,"%3.2f%%",perc2
);
543 strcpy(hbc1
,"class=\"header2\"");
544 strcpy(hbc2
,"class=\"header2\"");
545 strcpy(hbc3
,"class=\"header2\"");
546 strcpy(hbc4
,"class=\"header2\"");
547 strcpy(hbc5
,"class=\"header2\"");
548 strcpy(hbc6
,"class=\"header2\"");
549 strcpy(hbc7
,"class=\"header2\"");
550 strcpy(hbc8
,"class=\"header2\"");
551 strcpy(hbc9
,"class=\"header\"");
553 if(strstr(UserReportFields
,"CONNECT") == 0) {
557 if(strstr(UserReportFields
,"BYTES") == 0) {
561 if(strstr(UserReportFields
,"MILISEC") == 0) {
565 if(strstr(UserReportFields
,"SETYB") == 0) {
569 if(strstr(UserReportFields
,"IN-CACHE-OUT") == 0) {
573 if(strstr(UserReportFields
,"IN-CACHE-OUT") == 0) {
577 if(strstr(UserReportFields
,"USED_TIME") == 0) {
581 if(strstr(UserReportFields
,"%TIME") == 0) {
586 if(strstr(UserReportFields
,"TOTAL") != 0) {
587 sprintf(html
,"<tr><th></th><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</font></th><th %s>%s</font></th></tr>\n",hbc9
,text
[107],hbc1
,wwork1
,hbc2
,wwork2
,hbc4
,val3
,hbc5
,val4
,hbc6
,val5
,hbc7
,val6
,hbc3
,wwork3
,hbc8
,val7
);
593 if(atoi(PerUserLimit
) > 0) {
594 if(tnbytes
> (atoi(PerUserLimit
)*1000000)) {
596 if(access(PerUserLimitFile
, R_OK
) == 0) {
597 if((fp_usr
= fopen(PerUserLimitFile
, "r")) == 0) {
598 fprintf(stderr
, "SARG: (html9) %s: %s\n",text
[45],PerUserLimitFile
);
601 while(fgets(tmp5
,sizeof(tmp5
),fp_usr
)!=NULL
) {
602 if(strstr(tmp5
,"\n") != 0)
603 tmp5
[strlen(tmp5
)-1]='\0';
604 if(strcmp(tmp5
,u2
) == 0) {
613 if((fp_usr
= fopen(PerUserLimitFile
, "a")) == 0) {
614 fprintf(stderr
, "SARG: (html10) %s: %s\n",text
[45],PerUserLimitFile
);
617 sprintf(html
,"%s\n",u2
);
622 sprintf(msg
, "%s %s %s (%s MB). %s %s",text
[32],u2
,text
[74],PerUserLimit
,text
[75],PerUserLimitFile
);
629 strcpy(arqper
,dirname
);
630 strcat(arqper
,"/usuarios");
632 if ((fp_in
= fopen(arqper
, "r")) == 0){
633 fprintf(stderr
, "SARG: (html11) %s: %s\n",text
[45],arqper
);
637 fgets(totuser
,8,fp_in
);
640 totbytes2
=totbytes
/my_atoll(totuser
);
641 totelap2
=totelap
/my_atoll(totuser
);
644 perc
= totbytes
/ 100;
645 perc
= totbytes2
/ perc
;
649 perc2
= totelap
/ 100;
650 perc2
= totelap2
/ perc2
;
653 twork2
=my_atoll(totuser
);
656 sprintf(wwork1
,"%s",fixnum(twork
,1));
657 sprintf(wwork2
,"%s",fixnum(totbytes2
,1));
658 sprintf(wwork3
,"%s",fixnum(totelap2
,1));
660 sprintf(val6
,"%s",buildtime(totelap2
));
661 sprintf(val7
,"%3.2f%%",perc2
);
663 strcpy(hbc1
,"class=\"header2\"");
664 strcpy(hbc2
,"class=\"header2\"");
665 strcpy(hbc3
,"class=\"header2\"");
666 strcpy(hbc4
,"class=\"header2\"");
667 strcpy(hbc5
,"class=\"header2\"");
668 strcpy(hbc6
,"class=\"header\"");
670 if(strstr(UserReportFields
,"CONNECT") == 0) {
674 if(strstr(UserReportFields
,"BYTES") == 0) {
678 if(strstr(UserReportFields
,"MILISEC") == 0) {
682 if(strstr(UserReportFields
,"USED_TIME") == 0) {
686 if(strstr(UserReportFields
,"%TIME") == 0) {
691 if(strstr(UserReportFields
,"AVERAGE") != 0) {
692 sprintf(html
,"<tr><th></th><th %s>%s</th><th %s>%s</th><th %s>%s</th><th></th><th></th><th></th><th %s>%s</th><th %s>%s</font></th><th %s>%s</th></tr>\n",hbc6
,text
[96],hbc1
,wwork1
,hbc2
,wwork2
,hbc4
,val6
,hbc3
,wwork3
,hbc5
,val7
);
702 fputs("</center></table>\n",fp_ou
);
706 fputs("</body>\n</html>\n",fp_ou
);
714 (void)rewinddir(dirp
);
715 (void)closedir(dirp
);