]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - siteuser.c
Add support to decompress xz files
[thirdparty/sarg.git] / siteuser.c
index 1becea9cdbef5763d1d1b80a11ba207ce7795bba..a582b09277c4d8c8167343c7a7c725de304d51b3 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
- *                                                            1998, 2010
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
 
 void siteuser(void)
 {
-       FILE *fp_in, *fp_ou;
+       FileObject *fp_in;
+       FILE *fp_ou;
 
        char *buf;
-       char wuser[MAXLEN];
        char *ourl;
-       char csort[255];
+       char csort[4096];
        char general[MAXLEN];
        char general2[MAXLEN];
        char sites[MAXLEN];
        char report[MAXLEN];
        int regs=0;
-       int ucount=0;
        int ourl_size;
        int url_len;
-       char *users;
-       long long int obytes;
+       int topuser_link;
+       int nsitesusers;
        int cstatus;
        longline line;
        struct generalitemstruct item;
-       const struct userinfostruct *uinfo;
+       struct userinfostruct *uinfo;
 
-       if(Privacy)
+       if(Privacy) {
+               if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Sites & users report not generated because privacy option is on\n"));
                return;
+       }
 
+       if (debugz>=LogLevel_Process)
+               debuga(__FILE__,__LINE__,_("Creating report to list who visisted what site...\n"));
        nsitesusers = 0;
        sprintf(general,"%s/sarg-general",outdirname);
        sprintf(sites,"%s/sarg-sites",outdirname);
        sprintf(general2,"%s/sarg-general2",outdirname);
        sprintf(report,"%s/siteuser.html",outdirname);
 
-       sprintf(csort,"sort -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general);
+       if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general)>=sizeof(csort)) {
+               debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general,general2);
+               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_in=fopen(general2,"r"))==NULL) {
-               debuga(_("(siteuser) Cannot open log file %s\n"),general2);
-               debuga(_("sort command: %s\n"),csort);
+       if((fp_in=FileObject_Open(general2))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),general2,FileObject_GetLastOpenError());
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
 
        if((fp_ou=fopen(report,"w"))==NULL) {
-               debuga(_("(siteuser) Cannot open log file %s\n"),report);
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -94,20 +100,17 @@ void siteuser(void)
                fprintf(fp_ou,"<th class=\"header_l\">%s</th>",_("BYTES"));
        fputs("<th class=\"header_l",fp_ou);
        if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou);
+       /* TRANSLATORS: This is a column header showing the users who visited each site. */
        fprintf(fp_ou,"\">%s</th></tr></thead>\n",_("USERS"));
 
        ourl=NULL;
        ourl_size=0;
-       obytes=0;
 
-       if((users=(char *) malloc(204800))==NULL){
-               debuga(_("ERROR: Cannot load. Memory fault\n"));
-               exit(EXIT_FAILURE);
-       }
-       strcpy(users," ");
+       userinfo_clearflag();
+       topuser_link=((ReportType & REPORT_TYPE_USERS_SITES) != 0 && !indexonly);
 
        if ((line=longline_create())==NULL) {
-               debuga(_("Not enough memory to read file %s\n"),general2);
+               debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),general2);
                exit(EXIT_FAILURE);
        }
 
@@ -116,116 +119,85 @@ void siteuser(void)
                if(item.total) continue;
                uinfo=userinfo_find_from_id(item.user);
                if (!uinfo) {
-                       debuga(_("Unknown user ID %s in file %s\n"),item.user,general2);
+                       debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),item.user,general2);
                        exit(EXIT_FAILURE);
                }
 
                if (item.nacc > 0) nsitesusers = 1;
-               if(!regs) {
+               if (!nsitesusers) continue;
+
+               if (ourl==NULL || strcmp(item.url,ourl) != 0) {
+                       if (regs>0) fputs("</td></tr>\n",fp_ou);
+
+                       regs++;
+                       if (SiteUsersReportLimit && regs >= SiteUsersReportLimit)
+                               break;
+                       fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",regs);
+
                        url_len=strlen(item.url);
-                       if (!ourl || url_len>=ourl_size) {
+                       if (url_len>=ourl_size) {
                                ourl_size=url_len+1;
                                ourl=realloc(ourl,ourl_size);
                                if (!ourl) {
-                                       debuga(_("Not enough memory to store the url\n"));
+                                       debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
                                        exit(EXIT_FAILURE);
                                }
                        }
                        strcpy(ourl,item.url);
-                       obytes=item.nbytes;
-                       regs++;
-               }
-
-               sprintf(wuser," %s ",uinfo->label);
-               if(strstr(users,wuser) == 0 && strcmp(item.url,ourl) == 0) {
-                       strcat(users,uinfo->label);
-                       strcat(users," ");
-                       ucount++;
-                       if(ucount>4) {
-                               strcat(users,"<br>");
-                               ucount=0;
-                       }
-               }
-
-               if(SiteUsersReportLimit) {
-                       if(regs >= SiteUsersReportLimit)
-                               continue;
-               }
 
-               if(strcmp(item.url,ourl) != 0 && nsitesusers) {
-                       fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",regs);
-                       if(BlockIt[0]!='\0') {
+                       if(BlockIt[0]!='\0' && ourl[0]!=ALIAS_PREFIX) {
                                fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
                                output_html_url(fp_ou,ourl);
                                fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
                        }
-                       fputs("<a href=\"http://",fp_ou);
-                       output_html_url(fp_ou,ourl);
-                       fputs("\">",fp_ou);
-                       output_html_string(fp_ou,ourl,100);
-                       fputs("</a></td>",fp_ou);
+                       output_html_link(fp_ou,ourl,100);
+                       fputs("</td>",fp_ou);
 
                        if (BytesInSitesUsersReport) {
                                fputs("<td class=\"data\"",fp_ou);
-                               if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(int64_t)obytes);
-                               fprintf(fp_ou,">%s</td>",fixnum(obytes,1));
+                               if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(int64_t)item.nbytes);
+                               fprintf(fp_ou,">%s</td>",fixnum(item.nbytes,1));
                        }
-                       fprintf(fp_ou,"<td class=\"data2\">%s</td></tr>\n",users);
-
-                       regs++;
-                       ucount=0;
-                       strcpy(users,uinfo->label);
-                       strcat(users," ");
-                       url_len=strlen(item.url);
-                       if (url_len>=ourl_size) {
-                               ourl_size=url_len+1;
-                               ourl=realloc(ourl,ourl_size);
-                               if (!ourl) {
-                                       debuga(_("Not enough memory to store the url\n"));
-                                       exit(EXIT_FAILURE);
-                               }
-                       }
-                       strcpy(ourl,item.url);
-                       obytes=item.nbytes;
+                       fputs("<td class=\"data2\">",fp_ou);
+
+                       userinfo_clearflag();
+                       if (topuser_link && uinfo->topuser)
+                               fprintf(fp_ou,"<a href=\"%s/%s.html\">%s</a>",uinfo->filename,uinfo->filename,uinfo->label);
+                       else
+                               fprintf(fp_ou,"%s",uinfo->label);
+                       uinfo->flag=1;
                }
+               else if (uinfo->flag==0) {
+                       if (topuser_link && uinfo->topuser)
+                               fprintf(fp_ou," <a href=\"%s/%s.html\">%s</a>",uinfo->filename,uinfo->filename,uinfo->label);
+                       else
+                               fprintf(fp_ou," %s",uinfo->label);
+                       uinfo->flag=1;
+               }
+
+       }
+       if (FileObject_Close(fp_in)) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),general2,FileObject_GetLastCloseError());
+               exit(EXIT_FAILURE);
        }
-       fclose(fp_in);
        longline_destroy(&line);
 
-       if(nsitesusers) {
-               fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",regs);
-               if(BlockIt[0]!='\0') {
-                       fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
-                       output_html_url(fp_ou,ourl);
-                       fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
-               }
-               fputs("<a href=\"http://",fp_ou);
-               output_html_url(fp_ou,ourl);
-               fputs("\">",fp_ou);
-               output_html_string(fp_ou,ourl,100);
-               fputs("</a></td>",fp_ou);
-               if (BytesInSitesUsersReport) {
-                       fputs("<td class=\"data\"",fp_ou);
-                       if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(int64_t)obytes);
-                       fprintf(fp_ou,">%s</td>",fixnum(obytes,1));
-               }
-               fprintf(fp_ou,"<td class=\"data2\">%s</td></tr>\n",users);
+       if(regs>0) {
+               fputs("</td></tr>\n",fp_ou);
        }
        if (ourl) free(ourl);
 
-       if (unlink(general2)) {
-               debuga(_("Cannot delete %s - %s\n"),general2,strerror(errno));
+       if (!KeepTempLog && unlink(general2)) {
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),general2,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        fputs("</table></div>\n",fp_ou);
-       if (write_html_trailer(fp_ou)<0)
-               debuga(_("Write error in file %s\n"),report);
-       if (fclose(fp_ou)==EOF)
-               debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
-
-       if(users)
-               free(users);
+       write_html_trailer(fp_ou);
+       if (fclose(fp_ou)==EOF) {
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
        return;
 }