]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - html.c
Add an option to create the per_user_limit files
[thirdparty/sarg.git] / html.c
diff --git a/html.c b/html.c
index c6ef89993942803b1946aec552abb922349679c5..ad5be738e80adcea141bdf66d228aa911fa98070 100644 (file)
--- a/html.c
+++ b/html.c
@@ -1,6 +1,6 @@
 /*
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
- *                                                            1998, 2012
+ *                                                            1998, 2013
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
 #include "include/conf.h"
 #include "include/defs.h"
 
+//! Number of limits.
+int PerUserLimitsNumber=0;
+//! Log user's who downloaded more than the limit.
+struct PerUserLimitStruct PerUserLimits[MAX_USER_LIMITS];
+//! How to create a per user file.
+enum PerUserFileCreationEnum PerUserFileCreation=PUFC_Always;
+
 extern struct globalstatstruct globstat;
 
 void htmlrel(void)
 {
-       FILE *fp_in, *fp_ou, *fp_ip, *fp_ip2, *fp_usr;
+       FILE *fp_in, *fp_ou, *fp_ip, *fp_ip2;
 
        long long int nnbytes=0, unbytes=0, tnbytes=0, totbytes=0, totbytes2=0;
        long long int totelap=0, totelap2=0, nnelap=0, unelap=0, tnelap=0;
@@ -51,7 +58,9 @@ void htmlrel(void)
        double perc=0, perc2=0, ouperc=0, inperc=0;
        int count;
        int cstatus;
-       int have_denied_report;
+       int i;
+       unsigned int user_limit[(MAX_USER_LIMITS+sizeof(unsigned int)-1)/sizeof(unsigned int)];
+       bool have_denied_report;
        const char *sort_field;
        const char *sort_order;
        char siteind[MAX_TRUNCATED_URL];
@@ -77,6 +86,31 @@ void htmlrel(void)
 
        sort_labels(&sort_field,&sort_order);
 
+       switch (PerUserFileCreation)
+       {
+               case PUFC_Always:
+                       for (i=0 ; i<PerUserLimitsNumber ; i++) {
+                               FILE *fp_usr=fopen(PerUserLimits[i].File,"wt");
+                               if (fp_usr==NULL) {
+                                       debuga(_("Cannot create empty per_user_limit file \"%s\": %s"),PerUserLimits[i].File,
+                                                  strerror(errno));
+                                       exit(EXIT_FAILURE);
+                               }
+                               fclose(fp_usr);
+                       }
+                       break;
+
+               case PUFC_AsRequired:
+                       for (i=0 ; i<PerUserLimitsNumber ; i++) {
+                               if (access(PerUserLimits[i].File,R_OK)==0 && unlink(PerUserLimits[i].File)==-1) {
+                                       debuga(_("Cannot delete per_user_limit file \"%s\": %s"),PerUserLimits[i].File,
+                                                  strerror(errno));
+                                       exit(EXIT_FAILURE);
+                               }
+                       }
+                       break;
+       }
+
        uscan=userinfo_startscan();
        if (uscan == NULL) {
                debuga(_("Cannot enumerate the user list\n"));
@@ -107,7 +141,7 @@ void htmlrel(void)
                }
                if ((fp_in = fopen(arqin, "r")) == 0){
                        if (uinfo->no_report) continue;
-                       debuga(_("(html3) Cannot open file %s\n"),arqin);
+                       debuga(_("(html3) Cannot open file %s: %s\n"),arqin,strerror(errno));
                        exit(EXIT_FAILURE);
                }
 
@@ -115,20 +149,23 @@ void htmlrel(void)
                        debuga(_("Output file name too long: %s/%s/%s.html\n"),outdirname,uinfo->filename,uinfo->filename);
                        exit(EXIT_FAILURE);
                }
-               if (snprintf(duser,sizeof(duser),"%s/%s/denied_%s.html",tmp,uinfo->filename,uinfo->filename)>=sizeof(duser)) {
-                       debuga(_("File name too long: %s/%s/denied_%s.html\n"),tmp,uinfo->filename,uinfo->filename);
+               if (snprintf(duser,sizeof(duser),"%s/denied_%s.html",outdirname,uinfo->filename)>=sizeof(duser)) {
+                       debuga(_("File name too long: %s/denied_%s.html\n"),outdirname,uinfo->filename);
                        exit(EXIT_FAILURE);
                }
                if(access(duser, R_OK) != 0)
-                       have_denied_report=0;
+                       have_denied_report=false;
                else
-                       have_denied_report=1;
+                       have_denied_report=true;
 
                if ((line=longline_create())==NULL) {
                        debuga(_("Not enough memory to read file %s\n"),arqin);
                        exit(EXIT_FAILURE);
                }
 
+               for (i=0 ; i<sizeof(user_limit)/sizeof(user_limit[0]) ; i++)
+                       user_limit[i]=0;
+
                tnacc=0;
                tnbytes=0;
                tnelap=0;
@@ -174,7 +211,7 @@ void htmlrel(void)
                rewind(fp_in);
 
                if ((fp_ou = fopen(arqou, "w")) == 0){
-                       debuga(_("(html5) Cannot open file %s\n"),arqou);
+                       debuga(_("(html5) Cannot open file %s: %s\n"),arqou,strerror(errno));
                        exit(EXIT_FAILURE);
                }
 
@@ -187,17 +224,16 @@ void htmlrel(void)
                fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("User report"));
                close_html_header(fp_ou);
 
+               if (have_denied_report) {
+                       fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou);
+                       fprintf(fp_ou,"<tr><td class=\"header_l\" colspan=\"11\"><a href=\"denied_%s.html\">%s</a></td></tr>\n",uinfo->filename,_("SmartFilter report"));
+                       fputs("<tr><td></td></tr>\n</table></div>\n",fp_ou);
+               }
+
                fputs("<div class=\"report\"><table cellpadding=\"2\" cellspacing=\"1\"",fp_ou);
                if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou);
                fputs(">\n",fp_ou);
 
-               /*! \bug This line lays before the header of the table and is therefore invalid
-               HTML. It will also break the sorttable script. Beside, I suspect it serves no purpose.
-               */
-               if(have_denied_report) {
-                       fprintf(fp_ou,"<tr><td class=\"header_l\" colspan=\"11\"><a href=\"denied_%s.html\">%s</a> %s</td></tr>\n",uinfo->filename,_("SmartFilter"),_("Report"));
-               }
-
                fputs("<thead><tr><th class=\"sorttable_nosort\"></th><th class=\"header_l",fp_ou);
                if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou);
                fprintf(fp_ou,"\">%s</th>",_("ACCESSED SITE"));
@@ -207,7 +243,7 @@ void htmlrel(void)
                if((UserReportFields & USERREPORTFIELDS_BYTES) != 0)
                        fprintf(fp_ou,"<th class=\"header_l\">%s</th>",_("BYTES"));
                if((UserReportFields & USERREPORTFIELDS_SETYB) != 0)
-                       fprintf(fp_ou,"<th class=\"header_l\">%%%s</th>",_("BYTES"));
+                       fprintf(fp_ou,"<th class=\"header_l\">%s</th>",_("%BYTES"));
                if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0)
                        fprintf(fp_ou,"<th class=\"header_c\" colspan=\"2\">%s</th><th style=\"display:none;\"></th>",_("IN-CACHE-OUT"));
                if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0)
@@ -215,7 +251,7 @@ void htmlrel(void)
                if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0)
                        fprintf(fp_ou,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
                if((UserReportFields & USERREPORTFIELDS_PTIME) != 0)
-                       fprintf(fp_ou,"<th class=\"header_l\">%%%s</th>",_("TIME"));
+                       fprintf(fp_ou,"<th class=\"header_l\">%s</th>",pgettext("duration","%TIME"));
 
                fputs("</tr></thead>\n",fp_ou);
 
@@ -265,8 +301,8 @@ void htmlrel(void)
                                        strcpy(tmp6,"../../images");
 
                                if((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0) {
-                                       url_to_file(url,siteind,sizeof(siteind));
-                                       fprintf(fp_ou,"<td class=\"data\"><a href=\"tt%s-%s.html\"><img src=\"%s/datetime.png\" title=\"%s\" alt=\"T\"></a></td>",uinfo->filename,siteind,tmp6,_("date/time report"));
+                                       url_to_anchor(url,siteind,sizeof(siteind));
+                                       fprintf(fp_ou,"<td class=\"data\"><a href=\"tt.html#%s\"><img src=\"%s/datetime.png\" title=\"%s\" alt=\"T\"></a></td>",siteind,tmp6,_("date/time report"));
                                } else {
                                        fprintf(fp_ou,"<td class=\"data\"></td>");
                                }
@@ -324,10 +360,9 @@ void htmlrel(void)
                                fputs("</tr>\n",fp_ou);
                                count++;
                        } else if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0) {
-                               url_to_file(url,siteind,sizeof(siteind));
-                               snprintf(tmp2,sizeof(tmp2),"%s/%s/tt%s-%s.html",outdirname,uinfo->filename,uinfo->filename,siteind);
-                               if (unlink(tmp2)!=0) {
-                                       debuga(_("Cannot delete unused file \"%s\" - %s\n"),tmp2,strerror(errno));
+                               snprintf(warea,sizeof(warea),"%s/%s/tt.html",outdirname,uinfo->filename);
+                               if (unlink(warea)!=0) {
+                                       debuga(_("Cannot delete \"%s\": %s\n"),warea,strerror(errno));
                                }
                        }
 
@@ -338,12 +373,12 @@ void htmlrel(void)
                                }
 
                                if ((fp_ip = fopen(arqip, "r")) == 0){
-                                       debuga(_("(html6) Cannot open file %s\n"),arqip);
+                                       debuga(_("(html6) Cannot open file %s: %s\n"),arqip,strerror(errno));
                                        exit(EXIT_FAILURE);
                                }
 
                                if ((fp_ip2 = MY_FOPEN(tmp2, "a")) == 0){
-                                       debuga(_("(html7) Cannot open file %s\n"),tmp2);
+                                       debuga(_("(html7) Cannot open file %s: %s\n"),tmp2,strerror(errno));
                                        exit(EXIT_FAILURE);
                                }
 
@@ -383,7 +418,10 @@ void htmlrel(void)
                                longline_destroy(&line1);
 
                                fclose(fp_ip);
-                               fclose(fp_ip2);
+                               if (fclose(fp_ip2)==EOF) {
+                                       debuga(_("Write error in %s: %s\n"),tmp2,strerror(errno));
+                                       exit(EXIT_FAILURE);
+                               }
 
                                if (snprintf(csort,sizeof(csort),"sort -n -t \"\t\" -T \"%s\" -k 1,1 -k 2,2 -o \"%s\" \"%s\"",tmp,tmp3,tmp2)>=sizeof(csort)) {
                                        debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
@@ -397,7 +435,12 @@ void htmlrel(void)
                                }
 
                                if ((fp_ip = MY_FOPEN(tmp3, "r")) == 0) {
-                                       debuga(_("(html8) Cannot open file %s\n"),tmp3);
+                                       debuga(_("(html8) Cannot open file %s: %s\n"),tmp3,strerror(errno));
+                                       exit(EXIT_FAILURE);
+                               }
+
+                               if (unlink(tmp2)) {
+                                       debuga(_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno));
                                        exit(EXIT_FAILURE);
                                }
 
@@ -451,12 +494,8 @@ void htmlrel(void)
                                fclose(fp_ip);
                                longline_destroy(&line1);
 
-                               if (unlink(tmp2)) {
-                                       debuga(_("Cannot delete %s - %s\n"),tmp2,strerror(errno));
-                                       exit(EXIT_FAILURE);
-                               }
                                if (unlink(tmp3)) {
-                                       debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno));
+                                       debuga(_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
                                        exit(EXIT_FAILURE);
                                }
 
@@ -486,13 +525,13 @@ void htmlrel(void)
                longline_destroy(&line);
 
                if (iprel && arqip[0]) {
-                       if (unlink(arqip)) {
-                               debuga(_("Cannot delete %s - %s\n"),arqip,strerror(errno));
+                       if (!KeepTempLog && unlink(arqip)) {
+                               debuga(_("Cannot delete \"%s\": %s\n"),arqip,strerror(errno));
                                exit(EXIT_FAILURE);
                        }
                }
-               if (unlink(arqin)) {
-                       debuga(_("Cannot delete %s - %s\n"),arqin,strerror(errno));
+               if (!KeepTempLog && unlink(arqin)) {
+                       debuga(_("Cannot delete \"%s\": %s\n"),arqin,strerror(errno));
                        exit(EXIT_FAILURE);
                }
 
@@ -525,34 +564,33 @@ void htmlrel(void)
                        fputs("</tr>\n",fp_ou);
                }
 
-               if(PerUserLimit > 0) {
-                       if(tnbytes > (PerUserLimit*1000000)) {
-                               limit_flag=0;
-                               if(access(PerUserLimitFile, R_OK) == 0) {
-                                       if((fp_usr = fopen(PerUserLimitFile, "r")) == 0) {
-                                               debuga(_("(html9) Cannot open file %s\n"),PerUserLimitFile);
+               if (PerUserLimitsNumber>0) {
+                       int limit=(int)(tnbytes/1000000LLU);
+                       int maskid;
+                       int mask;
+                       for (i=0 ; i<PerUserLimitsNumber ; i++) {
+                               maskid=i/sizeof(unsigned int);
+                               mask=0x1U << (i % sizeof(unsigned int));
+                               if (limit>PerUserLimits[i].Limit && (user_limit[maskid] & mask)==0) {
+                                       FILE *fp_usr;
+
+                                       if((fp_usr = fopen(PerUserLimits[i].File, "at")) == 0) {
+                                               debuga(_("(html10) Cannot open per user limit file %s: %s\n"),PerUserLimits[i].File,strerror(errno));
                                                exit(EXIT_FAILURE);
                                        }
-                                       while(fgets(tmp6,sizeof(tmp6),fp_usr)!=NULL) {
-                                               fixendofline(tmp6);
-                                               if(strcmp(tmp6,uinfo->label) == 0) {
-                                                       limit_flag=1;
-                                                       break;
-                                               }
-                                       }
-                                       fclose(fp_usr);
-                               }
-
-                               if(!limit_flag) {
-                                       if((fp_usr = fopen(PerUserLimitFile, "a")) == 0) {
-                                               debuga(_("(html10) Cannot open file %s\n"),PerUserLimitFile);
+                                       if (PerUserLimits[i].UserId)
+                                               fprintf(fp_usr,"%s\n",uinfo->label);
+                                       else
+                                               fprintf(fp_usr,"%s\n",uinfo->ip);
+                                       if (fclose(fp_usr)==EOF) {
+                                               debuga(_("Write error in per user limit file %s: %s\n"),PerUserLimits[i].File,strerror(errno));
                                                exit(EXIT_FAILURE);
                                        }
-                                       fprintf(fp_usr,"%s\n",uinfo->label);
-                                       fclose(fp_usr);
+                                       user_limit[maskid]|=mask;
 
                                        if(debug)
-                                               debuga(_("User %s limit exceeded (%d MB). Added to file %s\n"),uinfo->label,PerUserLimit,PerUserLimitFile);
+                                               debuga(_("User %s limit exceeded (%d MB). Added to file %s\n"),uinfo->label,
+                                                          PerUserLimits[i].Limit,PerUserLimits[i].File);
                                }
                        }
                }
@@ -584,8 +622,10 @@ void htmlrel(void)
                fputs("</table></div>\n",fp_ou);
                if (write_html_trailer(fp_ou)<0)
                        debuga(_("Write error in file %s\n"),arqou);
-               if (fclose(fp_ou)==EOF)
-                       debuga(_("Failed to close file %s - %s\n"),arqou,strerror(errno));
+               if (fclose(fp_ou)==EOF) {
+                       debuga(_("Write error in %s: %s\n"),arqou,strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
 
                htaccess(uinfo);
        }