]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - dansguardian_report.c
Normalize the messages to have less messages to translate.
[thirdparty/sarg.git] / dansguardian_report.c
index 613bb53addbb6e8f506f9d330119de9d07587c5d..74cf8d36cf21fbd5494d18d811eb03f3da66b271 100644 (file)
@@ -1,10 +1,11 @@
 /*
- * AUTHOR: Pedro Lineu Orso                         pedro.orso@gmail.com
- *                                                            1998, 2009
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
+ *                                                            1998, 2013
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ *     http://sourceforge.net/projects/sarg/forums/forum/363374
  * ---------------------------------------------------------------------
  *
  *  This program is free software; you can redistribute it and/or modify
 #include "include/conf.h"
 #include "include/defs.h"
 
-void dansguardian_report(void)
+static void show_ignored_dansguardian(FILE *fp_ou,int count)
 {
+       char ignored[80];
+
+       snprintf(ignored,sizeof(ignored),ngettext("%d more dansguardian entry not shown here…","%d more dansguardian entries not shown here…",count),count);
+       fprintf(fp_ou,"<tr><td class=\"data\"></td><td class=\"data\"></td><td class=\"data\"></td><td class=\"data2 more\">%s</td><td class=\"data\"></td></tr>\n",ignored);
+}
 
-   FILE *fp_in = NULL, *fp_ou = NULL;
-
-   char url[MAXLEN];
-   char dansguardian_in[MAXLEN];
-   char per[MAXLEN];
-   char report[MAXLEN];
-   char period[100];
-   char ip[MAXLEN];
-   char rule[255];
-   char oip[MAXLEN];
-   char user[MAXLEN];
-   char ouser[MAXLEN];
-   char date[15];
-   char date2[15];
-   char hour[15];
-   char *str;
-   int  z=0;
-   int  count=0;
-
-   ouser[0]='\0';
-
-   sprintf(dansguardian_in,"%s/dansguardian.log",tmp);
-   if(!dansguardian_count) {
-      unlink(dansguardian_in);
-      return;
-   }
-
-   sprintf(per,"%s/sarg-period",dirname);
-   sprintf(report,"%s/dansguardian.html",dirname);
-
-   if ((fp_in = fopen(per, "r")) == 0) {
-      fprintf(stderr, "SARG: (dansguardian_report) %s: %s\n",text[45],per);
-      exit(1);
-   }
-
-   if (!fgets(period,sizeof(period),fp_in)) {
-      fprintf(stderr,"SARG: (dansguardian_report) read error in %s\n",per);
-      exit(1);
-   }
-   fclose(fp_in);
-
-   if((fp_in=MY_FOPEN(dansguardian_in,"r"))==NULL) {
-     fprintf(stderr, "SARG: (dansguardian_report) %s: %s\n",text[8],dansguardian_in);
-     exit(1);
-   }
-
-   if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
-     fprintf(stderr, "SARG: (dansguardian_report) %s: %s\n",text[8],report);
-     exit(1);
-   }
-
-   fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
-   css(fp_ou);
-   fputs("</head>\n",fp_ou);
-   if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
-   fprintf(fp_ou,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
-   write_logo_image(fp_ou);
-
-   if(strcmp(IndexTree,"date") == 0)
-      show_sarg(fp_ou, "../../..");
-   else
-      show_sarg(fp_ou,"..");
-   fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
-   fprintf(fp_ou,"<tr><th class=\"title\">%s</th></tr>\n",Title);
-
-   fprintf(fp_ou,"<tr><td class=\"header\">%s: %s</td></tr>\n",text[89],period);
-   fprintf(fp_ou,"<tr><th class=\"header3\">%s</th></tr>\n",text[128]);
-   fputs("</table></div>\n",fp_ou);
-
-   fputs("<div align=\"center\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou);
-   fputs("<tr><td></td></tr>\n",fp_ou);
-   fprintf(fp_ou,"<tr><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th></tr>\n",text[98],text[111],text[110],text[91],text[129]);
-
-   while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
-      if (getword(user,sizeof(user),buf,'\t')<0 || getword(date2,sizeof(date2),buf,'\t')<0 ||
-          getword(hour,sizeof(hour),buf,'\t')<0 || getword(ip,sizeof(ip),buf,'\t')<0 ||
-          getword(url,sizeof(url),buf,'\t')<0 || getword(rule,sizeof(rule),buf,'\n')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",dansguardian_in);
-         exit(1);
-      }
-
-      if(strcmp(UserIp,"yes") == 0)
-           strcpy(user,ip);
-
-      bzero(date, 15);
-      if(strncmp(df,"u",1) != 0) {
-         strncpy(date,date2+6,2);
-         strcat(date,"/");
-         strncat(date,date2+4,2);
-         strcat(date,"/");
-         strncat(date,date2,4);
-      } else {
-         strncpy(date,date2+4,2);
-         strcat(date,"/");
-         strncat(date,date2+6,2);
-         strcat(date,"/");
-         strncat(date,date2,4);
-      }
-
-      if((str=(char *) strstr(user, "_")) != (char *) NULL ) {
-         if((str=(char *) strstr(str+1, "_")) != (char *) NULL )
-            fixip(user);
-      }
-
-      if(strcmp(Ip2Name,"yes") == 0)
-         ip2name(ip,sizeof(ip));
-
-      if(!z) {
-         strcpy(ouser,user);
-         strcpy(oip,ip);
-         z++;
-      } else {
-         if(strcmp(ouser,user) == 0)
-            user[0]='\0';
-         if(user[0] != '\0')
-            strcpy(ouser,user);
-         if(strcmp(oip,ip) == 0)
-            ip[0]='\0';
-         if(ip[0] != '\0')
-            strcpy(oip,ip);
-      }
-
-//      get_usertab_name(user,name,sizeof(name));
-      user_find(name, user);
-
-      if(dotinuser && strchr(name,'_')) {
-         subs(name,sizeof(name),"_",".");
-      }
-
-     if(DansGuardianReportLimit) {
-         if(strcmp(ouser2,name) == 0) {
-            count++;
-         } else {
-            count=1;
-            strcpy(ouser2,name);
-         }
-         if(count >= DansGuardianReportLimit)
-            continue;
-      }
-
-      fprintf(fp_ou,"<tr><td class=\"data2\" nospaw>%s</td><td class=\"data2\" nospaw>%s</td><td class=\"data2\" nospaw>%s-%s</td><td class=\"data2\" nospaw><a href=\"http://%s\">%s</a></td><td class=\"data2\" nospaw>%s</td></tr>\n",name,ip,date,hour,url,url,rule);
-   }
-
-   fputs("</table>\n",fp_ou);
-
-   show_info(fp_ou);
-
-   fputs("</body>\n</html>\n",fp_ou);
-
-   fclose(fp_in);
-   fclose(fp_ou);
-
-   unlink(dansguardian_in);
-
-   return;
+void dansguardian_report(void)
+{
+       FILE *fp_in = NULL, *fp_ou = NULL;
+
+       char buf[MAXLEN];
+       char *url;
+       char dansguardian_in[MAXLEN];
+       char report[MAXLEN];
+       char ip[MAXLEN];
+       char rule[255];
+       char oip[MAXLEN];
+       char user[MAXLEN];
+       char ouser[MAXLEN];
+       char date[15];
+       char date2[15];
+       char hour[15];
+       char ouser2[255];
+       int  z=0;
+       int  count=0;
+       struct getwordstruct gwarea;
+
+       ouser[0]='\0';
+
+       snprintf(dansguardian_in,sizeof(dansguardian_in),"%s/dansguardian.int_log",tmp);
+       if(!dansguardian_count) {
+               if (!KeepTempLog && unlink(dansguardian_in))
+                       debuga(_("Cannot delete \"%s\": %s\n"),dansguardian_in,strerror(errno));
+               if (debugz>=LogLevel_Process) debugaz(_("Dansguardian report not generated because it is empty\n"));
+               return;
+       }
+
+       sprintf(report,"%s/dansguardian.html",outdirname);
+
+       if((fp_in=MY_FOPEN(dansguardian_in,"r"))==NULL) {
+               debuga(_("Cannot open file \"%s\": %s\n"),dansguardian_in,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
+               debuga(_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("DansGuardian"),HTML_JS_NONE);
+       fputs("<tr><td class=\"header_c\">",fp_ou);
+       fprintf(fp_ou,_("Period: %s"),period.html);
+       fputs("</td></tr>\n",fp_ou);
+       fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("DansGuardian"));
+       close_html_header(fp_ou);
+
+       fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou);
+       fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"),_("CAUSE"));
+
+       while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
+               getword_start(&gwarea,buf);
+               if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(date2,sizeof(date2),&gwarea,'\t')<0 ||
+                   getword(hour,sizeof(hour),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
+                       debuga(_("Maybe you have a broken record or garbage in your %s file\n"),dansguardian_in);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
+                       debuga(_("Maybe you have a broken url in your %s file\n"),dansguardian_in);
+                       exit(EXIT_FAILURE);
+               }
+               if (getword(rule,sizeof(rule),&gwarea,'\n')<0) {
+                       debuga(_("Maybe you have a broken rule in your %s file\n"),dansguardian_in);
+                       exit(EXIT_FAILURE);
+               }
+
+               if(UserIp)
+                       strcpy(user,ip);
+
+               memset(date,0,sizeof(date));
+               if (df!='u') {
+                       strncpy(date,date2+6,2);
+                       strcat(date,"/");
+                       strncat(date,date2+4,2);
+                       strcat(date,"/");
+                       strncat(date,date2,4);
+               } else {
+                       strncpy(date,date2+4,2);
+                       strcat(date,"/");
+                       strncat(date,date2+6,2);
+                       strcat(date,"/");
+                       strncat(date,date2,4);
+               }
+
+               if(Ip2Name)
+                       ip2name(ip,sizeof(ip));
+
+               if(!z) {
+                       strcpy(ouser,user);
+                       strcpy(oip,ip);
+                       z++;
+               } else {
+                       if(strcmp(ouser,user) == 0)
+                               user[0]='\0';
+                       if(user[0] != '\0')
+                               strcpy(ouser,user);
+                       if(strcmp(oip,ip) == 0)
+                               ip[0]='\0';
+                       if(ip[0] != '\0')
+                               strcpy(oip,ip);
+               }
+
+               user_find(name, sizeof(name), user);
+
+               if(DansGuardianReportLimit) {
+                       if(strcmp(ouser2,name) == 0) {
+                               count++;
+                       } else {
+                               if(count>DansGuardianReportLimit && DansGuardianReportLimit>0)
+                                       show_ignored_dansguardian(fp_ou,count-DansGuardianReportLimit);
+                               count=1;
+                               strcpy(ouser2,name);
+                       }
+                       if(count > DansGuardianReportLimit)
+                               continue;
+               }
+
+               fprintf(fp_ou,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td><td class=\"data2\">%s-%s</td><td class=\"data2\">",name,ip,date,hour);
+               output_html_link(fp_ou,url,100);
+               fprintf(fp_ou,"</td><td class=\"data2\">%s</td></tr>\n",rule);
+       }
+       fclose(fp_in);
+
+       if(count>DansGuardianReportLimit && DansGuardianReportLimit>0)
+               show_ignored_dansguardian(fp_ou,count-DansGuardianReportLimit);
+
+       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(_("Write error in \"%s\": %s\n"),report,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if (!KeepTempLog && unlink(dansguardian_in)) {
+               debuga(_("Cannot delete \"%s\": %s\n"),dansguardian_in,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       return;
 }