]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - useragent.c
Fix wrong tags label.
[thirdparty/sarg.git] / useragent.c
index db63c40a78cfe407f0e16750d40666da89cd932f..b47bfced4973bb79718839403f720e9738f1a789 100644 (file)
@@ -31,6 +31,7 @@ void useragent(void)
 {
 
    FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL;
+   char buf[MAXLEN];
    char ip[MAXLEN], data[MAXLEN], agent[MAXLEN], user[MAXLEN];
    char ipbefore[MAXLEN], namebefore[MAXLEN];
    char tagent[MAXLEN];
@@ -39,11 +40,15 @@ void useragent(void)
    char hfile[MAXLEN];
    char idate[MAXLEN], fdate[MAXLEN];
    char tmp2[MAXLEN];
+   char tmp3[MAXLEN];
+   char day[4],month[5],year[5], wdate[20];
+   char csort[MAXLEN];
    int  agentot=0, agentot2=0, agentdif=0, cont=0, nagent;
    unsigned long totregsl=0;
    int cstatus;
-   char ftime[128];
-   struct getwordstruct gwarea;
+   int ndate;
+   double perc;
+   struct getwordstruct gwarea, gwarea1;
 
    ip[0]='\0';
    data[0]='\0';
@@ -58,17 +63,17 @@ void useragent(void)
    sprintf(tmp2,"%s/squagent.log",TempDir);
 
    if((fp_in=fopen(UserAgentLog,"r"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],UserAgentLog);
-      exit(1);
+      debuga(_("(useragent) Cannot open file %s\n"),UserAgentLog);
+      exit(EXIT_FAILURE);
    }
 
    if((fp_ou=fopen(tmp3,"w"))==NULL) {
-      fprintf(stderr, "SARG: (email) %s: %s\n",text[45],tmp3);
-      exit(1);
+      debuga(_("(useragent) Cannot open file %s\n"),tmp3);
+      exit(EXIT_FAILURE);
    }
 
    if(debug) {
-      debuga("%s: %s",text[66],UserAgentLog);
+      debuga(_("Reading useragent log: %s\n"),UserAgentLog);
    }
 
    while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
@@ -76,14 +81,25 @@ void useragent(void)
       getword_start(&gwarea,buf);
       if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,'[')<0 ||
           getword(data,sizeof(data),&gwarea,' ')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
-         exit(1);
+         debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog);
+         exit(EXIT_FAILURE);
       }
+      getword_start(&gwarea1,data);
+      if (getword(day,sizeof(day),&gwarea1,'/')<0 || getword(month,sizeof(month),&gwarea1,'/')<0 ||
+          getword(year,sizeof(year),&gwarea1,':')<0) {
+         debuga(_("Maybe you have a broken date in your %s file\n"),UserAgentLog);
+         exit(EXIT_FAILURE);
+      }
+      buildymd(day,month,year,wdate);
+      ndate=atoi(wdate);
+      if (ndate<dfrom) continue;
+      if (ndate>duntil) break;
       if(totregsl == 1)
          strcpy(idate,data);
+      strcpy(fdate,data);
       if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
-         exit(1);
+         debuga(_("Maybe you have a broken useragent entry in your %s file\n"),UserAgentLog);
+         exit(EXIT_FAILURE);
       }
       strcpy(warea,agent);
       strup(warea);
@@ -92,8 +108,8 @@ void useragent(void)
 
       if(gwarea.current[0]!='\0') {
          if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) {
-            printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
-            exit(1);
+            debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog);
+            exit(EXIT_FAILURE);
          }
       }
 
@@ -104,77 +120,61 @@ void useragent(void)
 
       fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user);
       user[0]='\0';
+      useragent_count++;
    }
 
-   strcpy(fdate,data);
-
    if(debug) {
-      debuga("   %s: %ld",text[10],totregsl);
+      debuga(_("   Records read: %ld\n"),totregsl);
    }
 
    fclose(fp_in);
    fclose(fp_ou);
 
    if(debug) {
-      debuga("%s: %s",text[54],tmp2);
+      debuga(_("Sorting file: %s\n"),tmp2);
    }
 
    sprintf(csort,"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3);
    cstatus=system(csort);
    if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-      fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
-      fprintf(stderr, "SARG: sort command: %s\n",csort);
-      exit(1);
+      debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+      debuga(_("sort command: %s\n"),csort);
+      exit(EXIT_FAILURE);
    }
    if((fp_in=fopen(tmp2,"r"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2);
-      fprintf(stderr, "SARG: sort command: %s\n",csort);
-      exit(1);
+      debuga(_("(useragent) Cannot open file %s\n"),tmp2);
+      debuga(_("sort command: %s\n"),csort);
+      exit(EXIT_FAILURE);
    }
 
    unlink(tmp3);
 
-   sprintf(hfile,"%s/%s/useragent.html", outdir,period);
+   snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname);
    if((fp_ht=fopen(hfile,"w"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile);
-      exit(1);
+      debuga(_("(useragent) Cannot open file %s\n"),hfile);
+      exit(EXIT_FAILURE);
    }
 
    if(debug)
-      debuga("%s",text[72]);
-
-   fprintf(fp_ht, "<!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_ht);
-   fputs("</head>\n",fp_ht);
-   //if(FontFace[0] != '\0') fprintf(fp_ht,"<font face=\"%s\">\n",FontFace);
-   fprintf(fp_ht,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
-   write_logo_image(fp_ht);
-
-   if(strcmp(IndexTree,"date") == 0)
-      show_sarg(fp_ht, "../../..");
-   else
-      show_sarg(fp_ht,"..");
-   fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
-   fprintf(fp_ht,"<tr><th align=\"center\" colspan=\"2\"><b><font color=\"%s\" size=\"+1\">%s</font></b></th></tr>\n",TiColor,text[105]);
-
-   fputs("<tr><td></td></tr><tr><td></td></tr>",fp_ht);
-   fputs("</table></div>\n",fp_ht);
+      debuga(_("Making Useragent report\n"));
 
-   fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
+   write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"));
+   fprintf(fp_ht,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Squid Useragent's Report"));
+   fprintf(fp_ht,"<tr><td class=\"header_c\">%s: %s - %s</td></tr>\n",_("Period"),idate,fdate);
+   close_html_header(fp_ht);
 
-   fprintf(fp_ht,"<tr><td align=\"right\" bgcolor=\"%s\">%s:<td align=\"left\" bgcolor=\"%s\">%s - %s</td></td></tr>\n",HeaderBgColor,text[89],TxBgColor,idate,fdate);
+   fputs("<br><br>\n",fp_ht);
 
-   fputs("</table></div>\n",fp_ht);
-   fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
-   fputs("<tr><td></td><td></td></tr>",fp_ht);
+   fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
+   fputs("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>",fp_ht);
 
-   fprintf(fp_ou,"<tr><th align=\"left\" bgcolor=\"%s\"><font color=\"%s\">%s</font></th><th bgcolor=\"%s\" align=\"left\"><font color=\"%s\">%s</font></th></tr>\n",HeaderBgColor,HeaderColor,text[98],HeaderBgColor,HeaderColor,text[106]);
+   fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("AGENT"));
 
    while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
       getword_start(&gwarea,buf);
       if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp2);
-         exit(1);
+         debuga(_("There is an invalid IP address in file %s\n"),tmp2);
+         exit(EXIT_FAILURE);
       }
 
       if(Ip2Name) {
@@ -185,19 +185,22 @@ void useragent(void)
          } else strcpy(ip,namebefore);
       }
 
-      if (getword(agent,sizeof(agent),&gwarea,'\t')<0 ||
-          getword(user,sizeof(user),&gwarea,'\t')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp2);
-         exit(1);
+      if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
+         debuga(_("There is an invalid useragent in file %s\n"),tmp2);
+         exit(EXIT_FAILURE);
+      }
+      if (getword(user,sizeof(user),&gwarea,'\t')<0) {
+         debuga(_("There is an invalid user ID in file %s\n"),tmp2);
+         exit(EXIT_FAILURE);
       }
 
       if(strcmp(user,user_old) != 0) {
-         fprintf(fp_ht,"<tr><td align=\"left\" bgcolor=\"%s\">%s</td><td align=\"left\" bgcolor=\"%s\">%s</td></tr>\n",TxBgColor,user,TxBgColor,agent);
+         fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td></tr>\n",user,agent);
          strcpy(user_old,user);
          strcpy(agent_old,agent);
       } else {
          if(strcmp(agent,agent_old) != 0) {
-            fprintf(fp_ht,"<tr><td></td><td align=\"left\" bgcolor=\"%s\">%s</td></tr>\n",TxBgColor,agent);
+            fprintf(fp_ht,"<tr><td></td><td class=\"data2\">%s</td></tr>\n",agent);
             strcpy(agent_old,agent);
          }
       }
@@ -205,26 +208,25 @@ void useragent(void)
 
    fputs("</table>\n",fp_ht);
    fclose(fp_in);
-   fclose(fp_ou);
 
    sprintf(csort,"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2);
    cstatus=system(csort);
    if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-      fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
-      fprintf(stderr, "SARG: sort command: %s\n",csort);
-      exit(1);
+      debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+      debuga(_("sort command: %s\n"),csort);
+      exit(EXIT_FAILURE);
    }
    if((fp_in=fopen(tmp3,"r"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp3);
-      fprintf(stderr, "SARG: sort command: %s\n",csort);
-      exit(1);
+      debuga(_("(useragent) Cannot open file %s\n"),tmp3);
+      debuga(_("sort command: %s\n"),csort);
+      exit(EXIT_FAILURE);
    }
 
    unlink(tmp2);
 
    if((fp_ou=fopen(tmp2,"w"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2);
-      exit(1);
+      debuga(_("(useragent) Cannot open file %s\n"),tmp2);
+      exit(EXIT_FAILURE);
    }
 
    agent_old[0]='\0';
@@ -232,10 +234,13 @@ void useragent(void)
 
    while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
       getword_start(&gwarea,buf);
-      if (getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
-          getword(agent,sizeof(agent),&gwarea,'\t')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
-         exit(1);
+      if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
+         debuga(_("There is an invalid IP address in file %s\n"),tmp);
+         exit(EXIT_FAILURE);
+      }
+      if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
+         debuga(_("There is an invalid useragent in file %s\n"),tmp);
+         exit(EXIT_FAILURE);
       }
 
       if(!cont) {
@@ -264,54 +269,43 @@ void useragent(void)
    sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2);
    cstatus=system(csort);
    if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-      fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
-      fprintf(stderr, "SARG: sort command: %s\n",csort);
-      exit(1);
+      debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+      debuga(_("sort command: %s\n"),csort);
+      exit(EXIT_FAILURE);
    }
    if((fp_in=fopen(tmp3,"r"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp3);
-      fprintf(stderr, "SARG: sort command: %s\n",csort);
-      exit(1);
+      debuga(_("(useragent) Cannot open file %s\n"),tmp3);
+      debuga(_("sort command: %s\n"),csort);
+      exit(EXIT_FAILURE);
    }
 
    unlink(tmp2);
 
-   if((fp_ht=fopen(hfile,"a"))==NULL) {
-      fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile);
-      exit(1);
-   }
-
    fputs("<br><br>\n",fp_ht);
 
-   fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
-   fprintf(fp_ht,"<tr><th align=\"left\" bgcolor=\"%s\"><font color=\"%s\">%s</font></th><th bgcolor=\"%s\" align=\"left\"><font color=\"%s\">%s</font></th><th align=\"center\" bgcolor=\"%s\"><font color=\"%s\">%%</font></th></tr>\n",HeaderBgColor,HeaderColor,text[106],HeaderBgColor,HeaderColor,text[107],HeaderBgColor,HeaderColor);
+   fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
+   fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_c\">%%</th></tr>\n",_("AGENT"),_("TOTAL"));
 
    perc=0.;
    while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
       fixendofline(buf);
       getword_start(&gwarea,buf);
       if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
-         exit(1);
+         debuga(_("There is an invalid useragent in file %s\n"),tmp);
+         exit(EXIT_FAILURE);
       }
       nagent=atoi(tagent);
-      if(agentot2>0) {
-         perc=nagent * 100;
-         perc=perc / agentot2;
-      }
-
-      fprintf(fp_ht,"<tr><td align=\"left\" bgcolor=\"%s\">%s</td><td align=\"right\" bgcolor=\"%s\">%d</td><td align=\"right\" bgcolor=\"%s\">%3.2f</td></tr>\n",TxBgColor,gwarea.current,TxBgColor,nagent,TxBgColor,perc);
-   }
+      perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.;
 
-   fputs("</table></html>\n",fp_ht);
-   if(ShowSargInfo) {
-      zdate(ftime, sizeof(ftime), DateFormat);
-      fprintf(fp_ht,"<br><br><div align=\"center\"><font size=\"-2\">%s <a href=\"%s\">%s-%s</a> %s %s</font></div>\n",text[108],URL,PGM,VERSION,text[109],ftime);
+      fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data\">%d</td><td class=\"data\">%3.2lf</td></tr>\n",gwarea.current,nagent,perc);
    }
-
-   fputs("</table>\n</body>\n</html>\n",fp_ht);
    fclose(fp_in);
-   fclose(fp_ht);
+
+   fputs("</table></div>\n",fp_ht);
+   if (write_html_trailer(fp_ht)<0)
+      debuga(_("Write error in file %s\n"),hfile);
+   if (fclose(fp_ht)==EOF)
+      debuga(_("Failed to close file %s - %s\n"),hfile,strerror(errno));
 
    unlink(tmp3);