]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - topuser.c
Indent the configure script for more readability.
[thirdparty/sarg.git] / topuser.c
index 67c3df1c02c95861f6b32e463a53c42870ea138a..42a5f4976cb36ed87fee84f268831967b9cd00d6 100644 (file)
--- a/topuser.c
+++ b/topuser.c
@@ -1,6 +1,6 @@
 /*
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
- *                                                            1998, 2012
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
 
 #include "include/conf.h"
 #include "include/defs.h"
+#include "include/filelist.h"
 
 extern struct globalstatstruct globstat;
 extern bool smartfilter;
+extern FileListObject UserAgentLog;
 
 /*!
 Save the total number of users. The number is written in sarg-users and set
@@ -40,21 +42,25 @@ static void set_total_users(int totuser)
 {
        char tusr[1024];
        FILE *fp_ou;
-       
+
        snprintf(tusr,sizeof(tusr),"%s/sarg-users",outdirname);
        if((fp_ou=fopen(tusr,"w"))==NULL) {
-               debuga(_("(topuser) Cannot open file %s\n"),tusr);
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tusr,strerror(errno));
                exit(EXIT_FAILURE);
        }
        fprintf(fp_ou,"%d\n",totuser);
-       if (fclose(fp_ou)==EOF)
-               debuga(_("Failed to close file %s - %s\n"),tusr,strerror(errno));
+       if (fclose(fp_ou)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),tusr,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
        globstat.totuser=totuser;
 }
 
 void topuser(void)
 {
-       FILE *fp_in = NULL, *fp_top1 = NULL, *fp_top2 = NULL, *fp_top3 = NULL;
+       FileObject *fp_in = NULL;
+       FileObject *fp_top1 = NULL;
+       FILE *fp_top2 = NULL, *fp_top3 = NULL;
        long long int ttnbytes=0, ttnacc=0, tnacc=0;
        long long int tnbytes=0, ttnelap=0, tnelap=0;
        long long int tnincache=0, tnoucache=0, ttnincache=0, ttnoucache=0;
@@ -82,16 +88,18 @@ void topuser(void)
        struct generalitemstruct item;
        struct userinfostruct *uinfo;
 
+       if (debugz>=LogLevel_Process)
+               debuga(__FILE__,__LINE__,_("Creating top users report...\n"));
        ntopuser = 0;
        snprintf(wger,sizeof(wger),"%s/sarg-general",outdirname);
-       if((fp_in=fopen(wger,"r"))==NULL) {
-               debuga(_("(topuser) Cannot open file %s\n"),wger);
+       if((fp_in=FileObject_Open(wger))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wger,FileObject_GetLastOpenError());
                exit(EXIT_FAILURE);
        }
 
        snprintf(top2,sizeof(top2),"%s/top.tmp",outdirname);
        if((fp_top2=fopen(top2,"w"))==NULL) {
-               debuga(_("(topuser) Cannot open file %s\n"),top2);
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top2,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -99,7 +107,7 @@ void topuser(void)
        totuser=0;
 
        if ((line=longline_create())==NULL) {
-               debuga(_("Not enough memory to read the file %s\n"),wger);
+               debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),wger);
                exit(EXIT_FAILURE);
        }
 
@@ -136,7 +144,10 @@ void topuser(void)
                tnincache+=item.incache;
                tnoucache+=item.oucache;
        }
-       fclose(fp_in);
+       if (FileObject_Close(fp_in)) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wger,FileObject_GetLastCloseError());
+               exit(EXIT_FAILURE);
+       }
        longline_destroy(&line);
 
        if (olduser[0] != '\0') {
@@ -152,12 +163,15 @@ void topuser(void)
                ttnincache+=tnincache;
                ttnoucache+=tnoucache;
        }
-       fclose(fp_top2);
+       if (fclose(fp_top2)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top2,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
 #ifdef ENABLE_DOUBLE_CHECK_DATA
        if (ttnacc!=globstat.nacc || ttnbytes!=globstat.nbytes || ttnelap!=globstat.elap ||
            ttnincache!=globstat.incache || ttnoucache!=globstat.oucache) {
-               debuga(_("Total statistics mismatch when reading %s to produce the top users\n"),wger);
+               debuga(__FILE__,__LINE__,_("Total statistics mismatch when reading \"%s\" to produce the top users\n"),wger);
                exit(EXIT_FAILURE);
        }
 #endif
@@ -172,7 +186,7 @@ void topuser(void)
                sort_field=_("connect");
        } else if((TopuserSort & TOPUSER_SORT_TIME) != 0) {
                sfield="-n -k 4,4";
-               sort_field=_("time");
+               sort_field=pgettext("duration","time");
        } else {
                sort_field=_("bytes");
        }
@@ -187,29 +201,29 @@ void topuser(void)
 
        snprintf(top1,sizeof(top1),"%s/top",outdirname);
        if (snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" %s %s -o \"%s\" \"%s\"", tmp, order, sfield, top1, top2)>=sizeof(csort)) {
-               debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),top2,top1);
+               debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),top2,top1);
                exit(EXIT_FAILURE);
        }
        cstatus=system(csort);
        if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-               debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
-               debuga(_("sort command: %s\n"),csort);
+               debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
 
-       if((fp_top1=fopen(top1,"r"))==NULL) {
-               debuga(_("(topuser) Cannot open file %s\n"),top1);
+       if((fp_top1=FileObject_Open(top1))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top1,FileObject_GetLastOpenError());
                exit(EXIT_FAILURE);
        }
 
-       if (unlink(top2)) {
-               debuga(_("Cannot delete %s - %s\n"),top2,strerror(errno));
+       if (!KeepTempLog && unlink(top2)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),top2,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
-       snprintf(top3,sizeof(top3),"%s/index.html",outdirname);
+       snprintf(top3,sizeof(top3),"%s/"INDEX_HTML_FILE,outdirname);
        if((fp_top3=fopen(top3,"w"))==NULL) {
-               debuga(_("(topuser) Cannot open file %s\n"),top3);
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top3,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -237,18 +251,21 @@ void topuser(void)
                if((ReportType & REPORT_TYPE_SITES_USERS) != 0 && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"siteuser.html\">%s</a></td></tr>\n",_("Sites & Users"));
                if(dansguardian_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian"));
                if(redirector_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector"));
-               if ((ReportType & REPORT_TYPE_DOWNLOADS) != 0 && download_count && !Privacy && ndownload) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"download.html\">%s</a></td></tr>\n",_("Downloads"));
-               if ((ReportType & REPORT_TYPE_DENIED) != 0 && denied_count && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
-               if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0 && authfail_count && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"authfail.html\">%s</a></td></tr>\n",_("Authentication Failures"));
+               if (is_download()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"download.html\">%s</a></td></tr>\n",_("Downloads"));
+               if (is_denied()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
+               if (is_authfail()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"authfail.html\">%s</a></td></tr>\n",_("Authentication Failures"));
                if(smartfilter) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
-               if(UserAgentLog[0] != '\0' && useragent_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\n",_("Useragent"));
+               if (!FileList_IsEmpty(UserAgentLog) && useragent_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\n",_("Useragent"));
                fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3);
        }
 
        if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) {
                fputs("</body>\n</html>\n",fp_top3);
-               fclose (fp_top3);
-               if (debugz) debugaz(_("No top users report because it is not configured in report_type\n"));
+               if (fclose (fp_top3)==EOF) {
+                       debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top3,strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
+               if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("No top users report because it is not configured in report_type\n"));
                return;
        }
 
@@ -269,6 +286,11 @@ void topuser(void)
                if (SortTableJs[0]) fputs(" sorttable_alpha",fp_top3);
                fprintf(fp_top3,"\">%s</th>",_("USERID"));
        }
+       if((TopUserFields & TOPUSERFIELDS_USERIP) != 0) {
+               fputs("<th class=\"header_l",fp_top3);
+               if (SortTableJs[0]) fputs(" sorttable_alpha",fp_top3);
+               fprintf(fp_top3,"\">%s</th>",_("USERIP"));
+       }
        if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
                fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("CONNECT"));
        if((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
@@ -282,7 +304,7 @@ void topuser(void)
        if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
                fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
        if((TopUserFields & TOPUSERFIELDS_PTIME) != 0)
-               fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",_("TIME"));
+               fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",pgettext("duration","TIME"));
 
        fputs("</tr></thead>\n",fp_top3);
 
@@ -291,34 +313,34 @@ void topuser(void)
        ntopuser = 0;
 
        if ((line=longline_create())==NULL) {
-               debuga(_("Not enough memory to read the downloaded files\n"));
+               debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),top1);
                exit(EXIT_FAILURE);
        }
 
        while((warea=longline_read(fp_top1,line))!=NULL) {
                getword_start(&gwarea,warea);
                if (getword(user,sizeof(user),&gwarea,'\t')<0) {
-                       debuga(_("There is a broken user in file %s\n"),top1);
+                       debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),top1);
                        exit(EXIT_FAILURE);
                }
                if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
-                       debuga(_("There is a broken number of bytes in file %s\n"),top1);
+                       debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),top1);
                        exit(EXIT_FAILURE);
                }
                if (getword_atoll(&nacc,&gwarea,'\t')<0) {
-                       debuga(_("There is a broken number of access in file %s\n"),top1);
+                       debuga(__FILE__,__LINE__,_("Invalid number of accesses in file \"%s\"\n"),top1);
                        exit(EXIT_FAILURE);
                }
                if (getword_atoll(&elap,&gwarea,'\t')<0) {
-                       debuga(_("There is a broken elpased time in file %s\n"),top1);
+                       debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),top1);
                        exit(EXIT_FAILURE);
                }
                if (getword_atoll(&incac,&gwarea,'\t')<0) {
-                       debuga(_("There is a broken in-cache size in file %s\n"),top1);
+                       debuga(__FILE__,__LINE__,_("Invalid in-cache size in file \"%s\"\n"),top1);
                        exit(EXIT_FAILURE);
                }
                if (getword_atoll(&oucac,&gwarea,'\n')<0) {
-                       debuga(_("There is a broken out-of-cache size in file %s\n"),top1);
+                       debuga(__FILE__,__LINE__,_("Invalid out-of-cache size in file \"%s\"\n"),top1);
                        exit(EXIT_FAILURE);
                }
                if(nacc < 1)
@@ -330,14 +352,11 @@ void topuser(void)
 
                uinfo=userinfo_find_from_id(user);
                if (!uinfo) {
-                       debuga(_("Unknown user ID %s in file %s\n"),user,top1);
+                       debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,top1);
                        exit(EXIT_FAILURE);
                }
                uinfo->topuser=1;
 
-               report_day(uinfo);
-               greport_day(uinfo);
-
                fputs("<tr>",fp_top3);
 
                posicao++;
@@ -349,17 +368,14 @@ void topuser(void)
                                fputs("<td class=\"data2\">",fp_top3);
 #ifdef HAVE_GD
                                if(Graphs && GraphFont[0]!='\0') {
+                                       greport_day(uinfo);
                                        //fprintf(fp_top3,"<a href=\"%s/graph_day.png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filename,ImageFile,_("Graphic"));
                                        fprintf(fp_top3,"<a href=\"%s/graph.html\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filename,ImageFile,_("Graphic"));
                                }
 #endif
+                               report_day(uinfo);
                                fprintf(fp_top3,"<a href=\"%s/d%s.html\"><img src=\"%s/datetime.png\" title=\"%s\" alt=\"T\"></a></td>",uinfo->filename,uinfo->filename,ImageFile,_("date/time report"));
-                       } else {
-                               sprintf(val1,"%s/d%s.html",outdirname,uinfo->filename);
-                               if (unlink(val1)) {
-                                       debuga(_("Cannot delete %s - %s\n"),val1,strerror(errno));
-                                       exit(EXIT_FAILURE);
-                               }
+                               day_deletefile(uinfo);
                        }
                }
                if((TopUserFields & TOPUSERFIELDS_USERID) != 0) {
@@ -368,6 +384,9 @@ void topuser(void)
                        else
                                fprintf(fp_top3,"<td class=\"data2\"><a href=\"%s/%s.html\">%s</a></td>",uinfo->filename,uinfo->filename,uinfo->label);
                }
+               if((TopUserFields & TOPUSERFIELDS_USERIP) != 0) {
+                       fprintf(fp_top3,"<td class=\"data2\">%s</td>",uinfo->ip);
+               }
                if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) {
                        fputs("<td class=\"data\"",fp_top3);
                        if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey=\"%"PRId64"\"",(int64_t)nacc);
@@ -388,7 +407,7 @@ void topuser(void)
                        fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td><td class=\"data\">%3.2lf%%</td>",inperc,ouperc);
 #ifdef ENABLE_DOUBLE_CHECK_DATA
                        if ((inperc!=0. || ouperc!=0.) && fabs(inperc+ouperc-100.)>=0.01) {
-                               debuga(_("The total of the in-cache and cache-miss is not 100%% at position %d (user %s)\n"),posicao,uinfo->label);
+                               debuga(__FILE__,__LINE__,_("The total of the in-cache and cache-miss is not 100%% at position %d (user %s)\n"),posicao,uinfo->label);
                        }
 #endif
                }
@@ -411,9 +430,12 @@ void topuser(void)
 
                topcount++;
        }
-       fclose(fp_top1);
-       if (unlink(top1)) {
-               debuga(_("Cannot delete %s - %s\n"),top1,strerror(errno));
+       if (FileObject_Close(fp_top1)) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),top1,FileObject_GetLastCloseError());
+               exit(EXIT_FAILURE);
+       }
+       if (!KeepTempLog && unlink(top1)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),top1,strerror(errno));
                exit(EXIT_FAILURE);
        }
        longline_destroy(&line);
@@ -424,7 +446,10 @@ void topuser(void)
                        fputs("<td></td>",fp_top3);
                if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0 && !indexonly)
                        fputs("<td></td>",fp_top3);
-               fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("TOTAL"));
+               if((TopUserFields & TOPUSERFIELDS_USERIP) != 0)
+                       fprintf(fp_top3,"<th class=\"header_l\" colspan=\"2\">%s</th>",_("TOTAL"));
+               else
+                       fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("TOTAL"));
 
                if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
                        fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(ttnacc,1));
@@ -439,7 +464,7 @@ void topuser(void)
                        fprintf(fp_top3,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc,ouperc);
 #ifdef ENABLE_DOUBLE_CHECK_DATA
                        if (fabs(inperc+ouperc-100.)>=0.01) {
-                               debuga(_("The total of the in-cache and cache-miss is not 100%%\n"));
+                               debuga(__FILE__,__LINE__,_("The total of the in-cache and cache-miss is not 100%%\n"));
                        }
 #endif
                }
@@ -458,7 +483,10 @@ void topuser(void)
                        fputs("<td></td>",fp_top3);
                if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0 && !indexonly)
                        fputs("<td></td>",fp_top3);
-               fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("AVERAGE"));
+               if((TopUserFields & TOPUSERFIELDS_USERIP) != 0)
+                       fprintf(fp_top3,"<th class=\"header_l\" colspan=\"2\">%s</th>",_("AVERAGE"));
+               else
+                       fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("AVERAGE"));
 
                if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
                        fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(ttnacc/totuser,1));
@@ -478,10 +506,11 @@ void topuser(void)
        }
 
        fputs("</table></div>\n",fp_top3);
-       if (write_html_trailer(fp_top3)<0)
-               debuga(_("Write error in top user list %s\n"),top3);
-       if (fclose(fp_top3)==EOF)
-               debuga(_("Failed to close the top user list %s - %s\n"),top3,strerror(errno));
+       write_html_trailer(fp_top3);
+       if (fclose(fp_top3)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
        return;
 }