- FILE *fp_in = NULL, *fp_ou = NULL;
-
- char *buf;
- char *url;
- char report_in[MAXLEN];
- char wdirname[MAXLEN];
- char report[MAXLEN];
- char period[100];
- char ip[MAXLEN];
- char oip[MAXLEN];
- char user[MAXLEN];
- char ouser[MAXLEN];
- char ouser2[MAXLEN];
- char data[15];
- char hora[15];
- int z=0;
- int count=0;
- int i;
- int new_user;
- struct getwordstruct gwarea;
- struct longlinestruct line;
- struct userinfostruct *uinfo;
-
- ouser[0]='\0';
- ouser2[0]='\0';
-
- sprintf(report_in,"%s/sarg/download.log",TempDir);
- if(access(report_in, R_OK) != 0)
- return;
-
- strcpy(wdirname,outdirname);
- sprintf(report,"%s/download.html",wdirname);
- strcat(wdirname,"/");
- strcat(wdirname,"sarg-period");
-
- if ((fp_in = fopen(wdirname, "r")) == 0) {
- debuga(_("(download) Cannot open file: %s\n"),wdirname);
- exit(1);
- }
-
- if (!fgets(period,sizeof(period),fp_in)) {
- debuga(_("(download) read error in %s\n"),wdirname);
- exit(1);
- }
- fclose(fp_in);
-
- if((fp_in=MY_FOPEN(report_in,"r"))==NULL) {
- fprintf(stderr, "SARG: (download) %s: %s\n",_("Cannot open log file"),report_in);
- exit(1);
- }
-
- if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
- fprintf(stderr, "SARG: (download) %s: %s\n",_("Cannot open log file"),report);
- exit(1);
- }
-
- write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Downloads"));
- fprintf(fp_ou,"<tr><td class=\"header_l\">%s: %s</td></tr>\n",_("Period"),period);
- fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Downloads"));
- close_html_header(fp_ou);
-
- fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
- fputs("<tr><td></td></tr>\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></tr>\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"));
-
- if (longline_prepare(&line)<0) {
- debuga(_("Not enough memory to read the downloaded files\n"));
- exit(1);
- }
-
- while((buf=longline_read(fp_in,&line))!=NULL) {
- getword_start(&gwarea,buf);
- if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
- getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
- debuga(_("There is a broken record or garbage in file %s\n"),report_in);
- exit(1);
- }
- if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
- debuga(_("There is a broken url in file %s\n"),report_in);
- exit(1);
- }
-
- uinfo=userinfo_find_from_id(user);
- if (!uinfo) {
- debuga(_("Unknown user ID %s in file %s\n"),user,report_in);
- exit(1);
- }
- new_user=0;
- if(!z) {
- strcpy(ouser,user);
- strcpy(oip,ip);
- z++;
- new_user=1;
- } else {
- if(strcmp(ouser,user) != 0) {
- strcpy(ouser,user);
- new_user=1;
- }
- if(strcmp(oip,ip) != 0) {
- strcpy(oip,ip);
- new_user=1;
- }
- }
-
- if(DownloadReportLimit) {
- if(strcmp(ouser2,uinfo->label) == 0) {
- count++;
- } else {
- count=1;
- strcpy(ouser2,uinfo->label);
- }
- if(count >= DownloadReportLimit)
- continue;
- }
-
- for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) url[i]=0;
-
- fputs("<tr>",fp_ou);
- if (new_user)
- fprintf(fp_ou,"<td class=\"data\">%s</td><td class=\"data\">%s</td>",uinfo->label,ip);
- else
- fputs("<td class=\"data\"></td><td class=\"data\"></td>",fp_ou);
- fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">",data,hora);
- if(BlockIt[0]!='\0') {
- 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> ",ImageFile);
- }
- fputs("<a href=\"http://",fp_ou);
- output_html_url(fp_ou,url);
- fputs("\">http://",fp_ou);
- output_html_string(fp_ou,url,100);
- fputs("</a></td></tr>\n",fp_ou);
- }
- fclose(fp_in);
- longline_free(&line);
-
- fputs("</table></div>\n",fp_ou);
- write_html_trailer(fp_ou);
- fclose(fp_ou);
-
- unlink(report_in);
-
- return;
+/*!
+Generate the report of the downloaded files. The list of the suffixes to take into account
+is set with set_download_suffix().
+*/
+void download_report(void)
+{
+ FileObject *fp_in = NULL;
+ FILE *fp_ou = NULL;
+
+ char *buf;
+ char *url;
+ char report_in[MAXLEN];
+ char report[MAXLEN];
+ char ip[MAXLEN];
+ char oip[MAXLEN];
+ char user[MAXLEN];
+ char ouser[MAXLEN];
+ char ouser2[MAXLEN];
+ char data[15];
+ char hora[15];
+ int z=0;
+ int count=0;
+ int i;
+ int day,month,year;
+ bool new_user;
+ struct getwordstruct gwarea;
+ longline line;
+ struct userinfostruct *uinfo;
+ struct tm t;
+
+ if (!download_exists) {
+ if (!KeepTempLog && download_unsort[0]!='\0' && unlink(download_unsort))
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),download_unsort,strerror(errno));
+ download_unsort[0]='\0';
+ if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("No downloaded files to report\n"));
+ return;
+ }
+
+ if (debugz>=LogLevel_Process)
+ debuga(__FILE__,__LINE__,_("Creating download report...\n"));
+ ouser[0]='\0';
+ ouser2[0]='\0';
+
+ // sort the raw file
+ snprintf(report_in,sizeof(report_in),"%s/download.int_log",tmp);
+ download_sort(report_in);
+
+ // produce the report.
+ snprintf(report,sizeof(report),"%s/download.html",outdirname);
+
+ if((fp_in=FileObject_Open(report_in))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report_in,FileObject_GetLastOpenError());
+ exit(EXIT_FAILURE);
+ }
+
+ if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Downloads"),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",_("Downloads"));
+ close_html_header(fp_ou);
+
+ fputs("<div class=\"report\"><table cellpadding=\"0\" 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></tr>\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"));
+
+ if ((line=longline_create())==NULL) {
+ debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),report_in);
+ exit(EXIT_FAILURE);
+ }
+
+ while((buf=longline_read(fp_in,line))!=NULL) {
+ getword_start(&gwarea,buf);
+ if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
+ getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
+ debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),report_in);
+ exit(EXIT_FAILURE);
+ }
+ if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
+ debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),report_in);
+ exit(EXIT_FAILURE);
+ }
+ if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
+ computedate(year,month,day,&t);
+ strftime(data,sizeof(data),"%x",&t);
+
+ uinfo=userinfo_find_from_id(user);
+ if (!uinfo) {
+ debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,report_in);
+ exit(EXIT_FAILURE);
+ }
+ new_user=false;
+ if(!z) {
+ strcpy(ouser,user);
+ strcpy(oip,ip);
+ z++;
+ new_user=true;
+ } else {
+ if(strcmp(ouser,user) != 0) {
+ strcpy(ouser,user);
+ new_user=true;
+ }
+ if(strcmp(oip,ip) != 0) {
+ strcpy(oip,ip);
+ new_user=true;
+ }
+ }
+
+ if(DownloadReportLimit) {
+ if(strcmp(ouser2,uinfo->label) == 0) {
+ count++;
+ } else {
+ count=1;
+ strcpy(ouser2,uinfo->label);
+ }
+ if(count >= DownloadReportLimit)
+ continue;
+ }
+
+ for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) url[i]=0;
+
+ fputs("<tr>",fp_ou);
+ if (new_user) {
+ if (uinfo->topuser)
+ fprintf(fp_ou,"<td class=\"data\"><a href=\"%s/%s.html\">%s</a></td><td class=\"data\">%s</td>",uinfo->filename,uinfo->filename,uinfo->label,ip);
+ else
+ fprintf(fp_ou,"<td class=\"data\">%s</td><td class=\"data\">%s</td>",uinfo->label,ip);
+ } else
+ fputs("<td class=\"data\"></td><td class=\"data\"></td>",fp_ou);
+ fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">",data,hora);
+ 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> ",ImageFile);
+ }
+ output_html_link(fp_ou,url,100);
+ fputs("</td></tr>\n",fp_ou);
+ }
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),report_in,FileObject_GetLastCloseError());
+ exit(EXIT_FAILURE);
+ }
+ longline_destroy(&line);
+
+ fputs("</table></div>\n",fp_ou);
+ 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);
+ }
+
+ if (!KeepTempLog && unlink(report_in)) {
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),report_in,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ return;