/*
- * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
- * 1998, 2008
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
+ * 1998, 2010
*
* 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
{
FILE *fp_in, *fp_ou;
-
- char url[MAXLEN];
- char ourl[MAXLEN];
- char nacc[20];
- char nbytes[20];
- char ntime[20];
- char ntemp[40];
- char ttnacc[20];
- char ttnbytes[20];
- char ttntime[20];
+
+ char *buf;
+ char *url;
+ char *ourl=NULL;
char csort[255];
char general[MAXLEN];
char general2[MAXLEN];
char general3[MAXLEN];
- char per[MAXLEN];
char sites[MAXLEN];
char report[MAXLEN];
- char period[100];
- char sortf[10];
- char sortt[10];
+ const char *sortf;
+ const char *sortt;
+ long long int nacc;
+ long long int nbytes;
+ long long int ntime;
long long int tnacc=0;
long long int tnbytes=0;
long long int tntime=0;
long long int twork1=0, twork2=0, twork3=0;
int regs=0;
int cstatus;
+ int url_len;
+ int ourl_size=0;
+ struct getwordstruct gwarea;
+ longline line;
+ struct generalitemstruct item;
- if(strcmp(Privacy,"yes") == 0)
+ if(Privacy)
return;
- sprintf(general,"%s/sarg-general",dirname);
- sprintf(sites,"%s/sarg-sites",dirname);
- sprintf(general2,"%s/sarg-general2",dirname);
- sprintf(general3,"%s/sarg-general3",dirname);
- sprintf(per,"%s/sarg-period",dirname);
+ sprintf(general,"%s/sarg-general",outdirname);
+ sprintf(sites,"%s/sarg-sites",outdirname);
+ sprintf(general2,"%s/sarg-general2",outdirname);
+ sprintf(general3,"%s/sarg-general3",outdirname);
- if (strstr(ReportType,"topusers") == 0)
- sprintf(report,"%s/index.html",dirname);
+ if ((ReportType & REPORT_TYPE_TOPUSERS) == 0)
+ sprintf(report,"%s/index.html",outdirname);
else
- sprintf(report,"%s/topsites.html",dirname);
-
- if ((fp_in = fopen(per, "r")) == 0) {
- fprintf(stderr, "SARG: (topuser) %s: %s\n",text[45],per);
- exit(1);
- }
+ sprintf(report,"%s/topsites.html",outdirname);
- fgets(period,sizeof(period),fp_in);
- fclose(fp_in);
-
- sprintf(csort,"sort -k 4,4 -o '%s' '%s'",general2,general);
+ sprintf(csort,"sort -k 4,4 -o \"%s\" \"%s\"",general2,general);
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(general2,"r"))==NULL) {
- fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],general2);
- fprintf(stderr, "SARG: sort command: %s\n",csort);
- exit(1);
+ debuga(_("(topsites) Cannot open log file %s\n"),general2);
+ debuga(_("sort command: %s\n"),csort);
+ exit(EXIT_FAILURE);
}
if((fp_ou=fopen(general3,"w"))==NULL) {
- fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],general3);
- exit(1);
+ debuga(_("(topsites) Cannot open log file %s\n"),general3);
+ exit(EXIT_FAILURE);
}
- while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
- if (getword(url,sizeof(url),buf,' ')<0) {
- printf("SARG: Maybe you have an invalid user in your %s file of the topsites.\n",general2);
- exit(1);
- }
- if(strcmp(url,"TOTAL") == 0) {
- if (getword(ttnacc,sizeof(ttnacc),buf,' ')<0) {
- printf("SARG: Maybe you have an invalid total number of access in your %s file of the topsites.\n",general2);
- exit(1);
- }
- if (getword(ttnbytes,sizeof(ttnbytes),buf,' ')<0) {
- printf("SARG: Maybe you have an invalid total number of bytes in your %s file of the topsites.\n",general2);
- exit(1);
- }
- if (getword(ttntime,sizeof(ttntime),buf,' ')<0) {
- printf("SARG: Maybe you have an invalid total time in your %s file of the topsites.\n",general2);
- exit(1);
- }
- continue;
- }
- if (getword(nacc,sizeof(nacc),buf,' ')<0) {
- printf("SARG: Maybe you have an invalid number of access in your %s file of the topsites.\n",general2);
- exit(1);
- }
- if (getword(nbytes,sizeof(nbytes),buf,' ')<0) {
- printf("SARG: Maybe you have an invalid number of bytes in your %s file of the topsites.\n",general2);
- exit(1);
- }
- if (getword(url,sizeof(url),buf,' ')<0) {
- printf("SARG: Maybe you have an invalid url in your %s file of the topsites.\n",general2);
- exit(1);
- }
- if (getword(ntemp,sizeof(ntemp),buf,' ')<0) {
- printf("SARG: Maybe you have a broken record or garbage in column 5 in your %s file of the topsites.\n",general2);
- exit(1);
- }
- if (getword(ntemp,sizeof(ntemp),buf,' ')<0) {
- printf("SARG: Maybe you have a broken record or garbage in column 6 in your %s file of the topsites.\n",general2);
- exit(1);
- }
- if (getword(ntemp,sizeof(ntemp),buf,' ')<0) {
- printf("SARG: Maybe you have a broken record or garbage in column 7 in your %s file of the topsites.\n",general2);
- exit(1);
- }
- if (getword(ntime,sizeof(ntime),buf,' ')<0) {
- printf("SARG: Maybe you have a broken record or garbage in column 8 in your %s file of the topsites.\n",general2);
- exit(1);
- }
+ if ((line=longline_create())==NULL) {
+ debuga(_("Not enough memory to read file %s\n"),general2);
+ exit(EXIT_FAILURE);
+ }
+
+ while((buf=longline_read(fp_in,line))!=NULL) {
+ ger_read(buf,&item,general2);
+ if(item.total) continue;
if(!regs) {
- strcpy(ourl,url);
+ url_len=strlen(item.url);
+ if (!ourl || 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);
regs++;
}
- if(strcmp(url,ourl) != 0) {
- my_lltoa(tnacc,val1,15);
- my_lltoa(tnbytes,val2,15);
- my_lltoa(tntime,val3,15);
- fprintf(fp_ou,"%s %s %s %s\n",val1,val2,val3,ourl);
- strcpy(ourl,url);
+ if(strcmp(item.url,ourl) != 0) {
+ fprintf(fp_ou,"%lld\t%lld\t%lld\t%s\n",tnacc,tnbytes,tntime,ourl);
+ 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);
tnacc=0;
tnbytes=0;
tntime=0;
}
- tnacc+=my_atoll(nacc);
- tnbytes+=my_atoll(nbytes);
- tntime+=my_atoll(ntime);
+ tnacc+=item.nacc;
+ tnbytes+=item.nbytes;
+ tntime+=item.nelap;
}
+ fclose(fp_in);
+ unlink(general2);
+ longline_destroy(&line);
- my_lltoa(tnacc,val1,15);
- my_lltoa(tnbytes,val2,15);
- my_lltoa(tntime,val3,15);
- fprintf(fp_ou,"%s %s %s %s\n",val1,val2,val3,ourl);
+ if (ourl) {
+ fprintf(fp_ou,"%lld\t%lld\t%lld\t%s\n",tnacc,tnbytes,tntime,ourl);
+ free(ourl);
+ }
- fclose(fp_in);
fclose(fp_ou);
- unlink(general2);
strlow(TopsitesSortField);
strlow(TopsitesSortType);
if(strcmp(TopsitesSortField,"connect") == 0)
- strcpy(sortf,"1,1");
- if(strcmp(TopsitesSortField,"bytes") == 0)
- strcpy(sortf,"2,2");
+ sortf="-k 1,1 -k 2,2";
+ else if(strcmp(TopsitesSortField,"bytes") == 0)
+ sortf="-k 2,2 -k 1,1";
+ else
+ sortf="";
if(strcmp(TopsitesSortType,"a") == 0)
- strcpy(sortt," ");
- if(strcmp(TopsitesSortType,"d") == 0)
- strcpy(sortt,"-r");
+ sortt="";
+ else if(strcmp(TopsitesSortType,"d") == 0)
+ sortt="-r";
+ else
+ sortt="";
- sprintf(csort,"sort %s -k %s -o '%s' '%s'",sortt,sortf,sites,general3);
+ sprintf(csort,"sort %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3);
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(sites,"r"))==NULL) {
- fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],sites);
- fprintf(stderr, "SARG: sort command: %s\n",csort);
- exit(1);
+ debuga(_("(topsites) Cannot open log file %s\n"),sites);
+ debuga(_("sort command: %s\n"),csort);
+ exit(EXIT_FAILURE);
}
unlink(general2);
unlink(general3);
if((fp_ou=fopen(report,"w"))==NULL) {
- fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],report);
- exit(1);
+ debuga(_("(topsites) Cannot open log file %s\n"),report);
+ exit(EXIT_FAILURE);
}
- regs=0;
-
- 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);
- fprintf(fp_ou,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
- if(strlen(LogoImage) > 0) fprintf(fp_ou, "<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s> %s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n",LogoImage,Width,Height,LogoText);
-
- if(strcmp(IndexTree,"date") == 0)
- show_sarg(fp_ou,"../../..");
- else
- show_sarg(fp_ou, "..");
-
- fputs("<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=\"header3\">%s: %s</td></tr>\n",text[89],period);
- fprintf(fp_ou,"<tr><th class=\"header3\">%s %s %s</th></tr>\n",text[83],TopSitesNum,text[84]);
- fputs("</table></center>\n",fp_ou);
+ write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"));
+ fputs("<tr><td class=\"header_c\">",fp_ou);
+ fprintf(fp_ou,_("Period: %s"),period.html);
+ fputs("</td></tr>\n",fp_ou);
+ fputs("<tr><th class=\"header_c\">",fp_ou);
+ fprintf(fp_ou,_("Top %d sites"),TopSitesNum);
+ fputs("</th></tr>\n",fp_ou);
+ close_html_header(fp_ou);
- fputs("<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[100],text[91],text[92],text[93],text[99]);
+ 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",_("NUM"),_("ACCESSED SITE"),_("CONNECT"),_("BYTES"),_("TIME"));
- regs=1;
+ regs=0;
ntopsites = 0;
- while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
- if(regs>atoi(TopSitesNum))
- break;
- if (getword(nacc,sizeof(nacc),buf,' ')<0) {
- printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",sites);
- exit(1);
+ if ((line=longline_create())==NULL) {
+ debuga(_("Not enough memory to read file %s\n"),sites);
+ exit(EXIT_FAILURE);
+ }
+
+ while(regs<TopSitesNum && (buf=longline_read(fp_in,line))!=NULL) {
+ getword_start(&gwarea,buf);
+ if (getword_atoll(&nacc,&gwarea,'\t')<0) {
+ debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
+ exit(EXIT_FAILURE);
+ }
+ if (nacc == 0) continue;
+ if (getword_atoll(&nbytes,&gwarea,'\t')<0 || getword_atoll(&ntime,&gwarea,'\t')<0) {
+ debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
+ exit(EXIT_FAILURE);
}
- if (atoi(nacc) == 0) continue;
- if (getword(nbytes,sizeof(nbytes),buf,' ')<0 ||
- getword(ntime,sizeof(ntime),buf,' ')<0 || getword(url,sizeof(url),buf,' ')<0) {
- printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",sites);
- exit(1);
+ if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
+ debuga(_("The url is invalid in file %s\n"),sites);
+ exit(EXIT_FAILURE);
}
- twork1=my_atoll(nacc);
- twork2=my_atoll(nbytes);
- twork3=my_atoll(ntime);
+ twork1=nacc;
+ twork2=nbytes;
+ twork3=ntime;
strcpy(wwork1,fixnum(twork1,1));
strcpy(wwork2,fixnum(twork2,1));
strcpy(wwork3,fixtime(twork3));
- if(strlen(BlockIt) > 0)
- sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"../images/sarg-squidguard-block.png\" border=\"0\"></a> ",wwwDocumentRoot,BlockIt,url);
- else BlockImage[0]='\0';
+ fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2 link\">",++regs);
+ if(BlockIt[0] != '\0') {
+ fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt);
+ output_html_url(fp_ou,url);
+ fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",fp_ou);
+ }
- fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\"><font class=\"link\">%s</font></td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",regs,BlockImage,url,url,wwork1,wwork2,wwork3);
- regs++;
+ fputs("<a href=\"http://",fp_ou);
+ output_html_url(fp_ou,url);
+ fputs("\">",fp_ou);
+ output_html_string(fp_ou,url,100);
+ fprintf(fp_ou,"</a></td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",wwork1,wwork2,wwork3);
}
-
-
- fputs("</table></center>\n",fp_ou);
-
- show_info(fp_ou);
-
- fputs("</body>\n</html>\n",fp_ou);
-
fclose(fp_in);
- fclose(fp_ou);
+ longline_destroy(&line);
+
+ 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));
return;