/*
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
- * 1998, 2010
+ * 1998, 2011
*
* SARG donations:
* please look at http://sarg.sourceforge.net/donations.php
#include "include/conf.h"
#include "include/defs.h"
+extern struct globalstatstruct globstat;
+
void htmlrel(void)
{
- DIR *dirp;
FILE *fp_in, *fp_ou, *fp_ip, *fp_ip2, *fp_usr;
- struct dirent *direntp;
long long int nnbytes=0, unbytes=0, tnbytes=0, totbytes=0, totbytes2=0;
long long int totelap=0, totelap2=0, nnelap=0, unelap=0, tnelap=0;
long long int incache=0, oucache=0, tnincache=0, tnoucache=0, twork=0;
long long int ntotuser;
long long int userbytes, userelap;
char *buf;
- char arqin[MAXLEN], arqou[MAXLEN], arqper[MAXLEN], arqip[MAXLEN];
+ char arqin[MAXLEN], arqou[MAXLEN], arqip[MAXLEN];
char *url, tmsg[50], csort[MAXLEN];
- char user[MAXLEN], duser[MAXLEN];
+ char duser[MAXLEN];
char user_ip[MAXLEN], olduserip[MAXLEN], tmp2[MAXLEN], tmp3[MAXLEN];
- char denied_report[255];
- char *str;
char warea[MAXLEN];
- char totuser[8];
char tmp6[MAXLEN];
char *user_url;
- long long int tnacc=0, ttnacc=0, unacc=0;
+ long long int tnacc=0, ttnacc=0;
double perc=0, perc2=0, ouperc=0, inperc=0;
int count;
int cstatus;
- const char txtext[]=".txt";
+ int have_denied_report;
const char *sort_field;
const char *sort_order;
- int dlen;
char siteind[MAX_TRUNCATED_URL];
struct getwordstruct gwarea;
longline line,line1;
- struct generalitemstruct item;
const struct userinfostruct *uinfo;
+ userscan uscan;
- if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
-
- strcpy(tmp2,tmp);
- strcat(tmp2,"/sargtmp.unsort");
-
- strcpy(tmp3,tmp);
- strcat(tmp3,"/sargtmp.log");
-
- snprintf(arqper,sizeof(arqper),"%s/sarg-general",outdirname);
- if ((fp_in = fopen(arqper, "r")) == 0){
- debuga(_("(html2) Cannot open file %s\n"),arqper);
- exit(EXIT_FAILURE);
- }
- if ((line=longline_create())==NULL) {
- debuga(_("Not enough memory to read file %s\n"),arqper);
+ if (snprintf(tmp2,sizeof(tmp2),"%s/sargtmp.unsort",tmp)>=sizeof(tmp2)) {
+ debuga(_("File name too long: %s/sargtmp.unsort\n"),tmp);
exit(EXIT_FAILURE);
}
- ttnacc=0;
- totbytes=0;
- totelap=0;
- while((buf=longline_read(fp_in,line))!=NULL) {
- ger_read(buf,&item,arqper);
- if(item.total) {
- ttnacc+=item.nacc;
- totbytes+=item.nbytes;
- totelap+=item.nelap;
- }
- }
- fclose(fp_in);
- longline_destroy(&line);
- snprintf(arqper,sizeof(arqper),"%s/sarg-users",outdirname);
- if ((fp_in = fopen(arqper, "r")) == 0){
- debuga(_("(html11) Cannot open file %s\n"),arqper);
+ if (snprintf(tmp3,sizeof(tmp3),"%s/sargtmp.log",tmp)>=sizeof(tmp3)) {
+ debuga(_("File name too long: %s/sargtmp.log\n"),tmp);
exit(EXIT_FAILURE);
}
- if (!fgets(totuser,sizeof(totuser),fp_in)) {
- debuga(_("(html11) read error in %s\n"),arqper);
- exit(EXIT_FAILURE);
- }
- fclose(fp_in);
- ntotuser=my_atoll(totuser);
- if (ntotuser<=0) ntotuser=1;
+
+ tnacc=globstat.nacc;
+ totbytes=globstat.nbytes;
+ totelap=globstat.elap;
+ ntotuser=globstat.totuser;
greport_prepare();
sort_labels(&sort_field,&sort_order);
- if ((dirp = opendir(tmp)) == NULL) {
- debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
+ uscan=userinfo_startscan();
+ if (uscan == NULL) {
+ debuga(_("Cannot enumerate the user list\n"));
exit(EXIT_FAILURE);
}
- while ( (direntp = readdir( dirp )) != NULL ) {
- dlen=strlen(direntp->d_name)-(sizeof(txtext)-1);
- if (dlen<0) continue;
- if(strcmp(direntp->d_name+dlen,txtext) != 0)
- continue;
-
- count=1;
- if (dlen>0) {
- if (dlen>=sizeof(user)) continue;
- strncpy(user,direntp->d_name,dlen);
- user[dlen]=0;
- } else {
- user[0]=0;
- }
-
- uinfo=userinfo_find_from_file(user);
- if (!uinfo) {
- debuga(_("Unknown user ID %s in directory %s\n"),user,tmp);
+ while ( (uinfo = userinfo_advancescan(uscan)) != NULL ) {
+ if (snprintf(warea,sizeof(warea),"%s/%s",outdirname,uinfo->filename)>=sizeof(warea)) {
+ debuga(_("Destination directory too long: %s/%s\n"),outdirname,uinfo->filename);
exit(EXIT_FAILURE);
}
- str=strrchr(user,'.');
- if (str)
- strcpy(denied_report,str+1);
- else
- strcpy(denied_report,user);
+ if (!uinfo->topuser) {
+ //! \todo Instead of deleting the supernumerary directories, don't create them in the first place.
+ unlinkdir(warea,0);
+ continue;
+ }
- if (snprintf(warea,sizeof(warea),"%s/%s",outdirname,user)>=sizeof(warea)) {
- debuga(_("Destination directory too long: %s/%s\n"),outdirname,user);
- exit(EXIT_FAILURE);
+ if (access(warea, R_OK) != 0) {
+ if (mkdir(warea,0755)) {
+ debuga(_("Cannot create directory %s - %s\n"),warea,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
}
- mkdir(warea,0755);
+ tmpsort(uinfo);
report_day(uinfo);
greport_day(uinfo);
- if (snprintf(arqin,sizeof(arqin),"%s/%s",tmp,direntp->d_name)>=sizeof(arqin)) {
- debuga(_("Input file name too long: %s/%s\n"),tmp,direntp->d_name);
+ if (snprintf(arqin,sizeof(arqin),"%s/htmlrel.txt",tmp)>=sizeof(arqin)) {
+ debuga(_("Input file name too long: %s/htmlrel.txt\n"),tmp);
+ exit(EXIT_FAILURE);
+ }
+ if ((fp_in = fopen(arqin, "r")) == 0){
+ if (uinfo->no_report) continue;
+ debuga(_("(html3) Cannot open file %s\n"),arqin);
exit(EXIT_FAILURE);
}
+
if (snprintf(arqou,sizeof(arqou),"%s/%s/%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqou)) {
debuga(_("Output file name too long: %s/%s/%s.html\n"),outdirname,uinfo->filename,uinfo->filename);
exit(EXIT_FAILURE);
}
- if (snprintf(duser,sizeof(duser),"%s/%s/denied_%s.html",tmp,direntp->d_name,denied_report)>=sizeof(duser)) {
- debuga(_("File name too long: %s/%s/denied_%s.html\n"),tmp,direntp->d_name,denied_report);
+ if (snprintf(duser,sizeof(duser),"%s/%s/denied_%s.html",tmp,uinfo->filename,uinfo->filename)>=sizeof(duser)) {
+ debuga(_("File name too long: %s/%s/denied_%s.html\n"),tmp,uinfo->filename,uinfo->filename);
exit(EXIT_FAILURE);
}
if(access(duser, R_OK) != 0)
- denied_report[0]='\0';
-
- if ((fp_in = fopen(arqin, "r")) == 0){
- debuga(_("(html3) Cannot open file %s\n"),arqin);
- exit(EXIT_FAILURE);
- }
+ have_denied_report=0;
+ else
+ have_denied_report=1;
if ((line=longline_create())==NULL) {
debuga(_("Not enough memory to read file %s\n"),arqin);
/*! \bug This line lays before the header of the table and is therefore invalid
HTML. It will also break the sorttable script. Beside, I suspect it serves no purpose.
*/
- if(denied_report[0]!='\0') {
- fprintf(fp_ou,"<tr><td class=\"header_l\" colspan=\"11\"><a href=\"denied_%s.html\">%s</a> %s</td></tr>\n",denied_report,_("SmartFilter"),_("Report"));
+ if(have_denied_report) {
+ fprintf(fp_ou,"<tr><td class=\"header_l\" colspan=\"11\"><a href=\"denied_%s.html\">%s</a> %s</td></tr>\n",uinfo->filename,_("SmartFilter"),_("Report"));
}
fputs("<thead><tr><th class=\"sorttable_nosort\"></th><th class=\"header_l",fp_ou);
if(debug) {
debuga(_("Making report: %s\n"),uinfo->id);
}
+ count=0;
+ arqip[0]='\0';
while((buf=longline_read(fp_in,line))!=NULL) {
getword_start(&gwarea,buf);
fprintf(fp_ou,"<td class=\"data2\"><span style=\"color:%s;\">%s</span></td>",PrivacyStringColor,PrivacyString);
else {
fputs("<td class=\"data2\">",fp_ou);
- if(BlockIt[0]!='\0') {
+ if(BlockIt[0]!='\0' && url[0]!=ALIAS_PREFIX) {
fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
output_html_url(fp_ou,url);
fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a> ",tmp6);
}
- fputs("<a href=\"http://",fp_ou);
- output_html_url(fp_ou,url);
- fputs("\">",fp_ou);
- output_html_string(fp_ou,url,100);
- fputs("</a></td>",fp_ou);
+ output_html_link(fp_ou,url,100);
+ fputs("</td>",fp_ou);
}
if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) {
fclose(fp_ip);
fclose(fp_ip2);
- sprintf(csort,"sort -n -T \"%s\" -k 1,1 -k 2,2 -o \"%s\" \"%s\"",tmp,tmp3,tmp2);
+ if (snprintf(csort,sizeof(csort),"sort -n -t \"\t\" -T \"%s\" -k 1,1 -k 2,2 -o \"%s\" \"%s\"",tmp,tmp3,tmp2)>=sizeof(csort)) {
+ debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
+ exit(EXIT_FAILURE);
+ }
cstatus=system(csort);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
}
strcpy(olduserip,user_ip);
- unacc=0;
unbytes=0;
unelap=0;
}
fclose(fp_ip);
longline_destroy(&line1);
- unlink(tmp2);
- unlink(tmp3);
+ if (unlink(tmp2)) {
+ debuga(_("Cannot delete %s - %s\n"),tmp2,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ if (unlink(tmp3)) {
+ debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
if (olduserip[0]!='\0') {
fprintf(fp_ou,"<tr><td></td><td class=\"data\">%s</td>",olduserip);
}
}
- unacc=0;
unbytes=0;
unelap=0;
}
fclose(fp_in);
longline_destroy(&line);
- if(iprel)
- unlink(arqip);
- unlink(arqin);
+ if (iprel && arqip[0]) {
+ if (unlink(arqip)) {
+ debuga(_("Cannot delete %s - %s\n"),arqip,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ }
+ if (unlink(arqin)) {
+ debuga(_("Cannot delete %s - %s\n"),arqin,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
if ((UserReportFields & (USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE)) != 0)
fputs("<tfoot>",fp_ou);
}
}
-// if(indexonly) {
-// unlink(arqou);
-// continue;
-// }
-
if ((ReportType & REPORT_TYPE_TOPUSERS) != 0 && (UserReportFields & USERREPORTFIELDS_AVERAGE) != 0) {
totbytes2=totbytes/ntotuser;
totelap2=totelap/ntotuser;
htaccess(uinfo);
}
- (void)closedir(dirp);
+ userinfo_stopscan(uscan);
greport_cleanup();
return;