]> git.ipfire.org Git - thirdparty/sarg.git/blob - siteuser.c
Merge commit '24eb624ea44bf4c82c602ce'
[thirdparty/sarg.git] / siteuser.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2012
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27 #include "include/conf.h"
28 #include "include/defs.h"
29
30 void siteuser(void)
31 {
32 FILE *fp_in, *fp_ou;
33
34 char *buf;
35 char *ourl;
36 char csort[255];
37 char general[MAXLEN];
38 char general2[MAXLEN];
39 char sites[MAXLEN];
40 char report[MAXLEN];
41 int regs=0;
42 int ourl_size;
43 int url_len;
44 int topuser_link;
45 int nsitesusers;
46 int cstatus;
47 longline line;
48 struct generalitemstruct item;
49 struct userinfostruct *uinfo;
50
51 if(Privacy) {
52 if (debugz) debugaz(_("Sites and users report not generated because privacy option is on\n"));
53 return;
54 }
55
56 nsitesusers = 0;
57 sprintf(general,"%s/sarg-general",outdirname);
58 sprintf(sites,"%s/sarg-sites",outdirname);
59 sprintf(general2,"%s/sarg-general2",outdirname);
60 sprintf(report,"%s/siteuser.html",outdirname);
61
62 if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general)>=sizeof(csort)) {
63 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general,general2);
64 exit(EXIT_FAILURE);
65 }
66 cstatus=system(csort);
67 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
68 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
69 debuga(_("sort command: %s\n"),csort);
70 exit(EXIT_FAILURE);
71 }
72
73 if((fp_in=fopen(general2,"r"))==NULL) {
74 debuga(_("(siteuser) Cannot open log file %s\n"),general2);
75 debuga(_("sort command: %s\n"),csort);
76 exit(EXIT_FAILURE);
77 }
78
79 if((fp_ou=fopen(report,"w"))==NULL) {
80 debuga(_("(siteuser) Cannot open log file %s\n"),report);
81 exit(EXIT_FAILURE);
82 }
83
84 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Sites & Users"),HTML_JS_SORTTABLE);
85 fputs("<tr><td class=\"header_c\">",fp_ou);
86 fprintf(fp_ou,_("Period: %s"),period.html);
87 fputs("</td></tr>\n",fp_ou);
88 fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Sites & Users"));
89 close_html_header(fp_ou);
90
91 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\"",fp_ou);
92 if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou);
93 fprintf(fp_ou,">\n<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l",_("NUM"));
94 if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou);
95 fprintf(fp_ou,"\">%s</th>",_("ACCESSED SITE"));
96 if(BytesInSitesUsersReport)
97 fprintf(fp_ou,"<th class=\"header_l\">%s</th>",_("BYTES"));
98 fputs("<th class=\"header_l",fp_ou);
99 if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou);
100 fprintf(fp_ou,"\">%s</th></tr></thead>\n",_("USERS"));
101
102 ourl=NULL;
103 ourl_size=0;
104
105 userinfo_clearflag();
106 topuser_link=((ReportType & REPORT_TYPE_USERS_SITES) != 0 && !indexonly);
107
108 if ((line=longline_create())==NULL) {
109 debuga(_("Not enough memory to read file %s\n"),general2);
110 exit(EXIT_FAILURE);
111 }
112
113 while((buf=longline_read(fp_in,line))!=NULL) {
114 ger_read(buf,&item,general2);
115 if(item.total) continue;
116 uinfo=userinfo_find_from_id(item.user);
117 if (!uinfo) {
118 debuga(_("Unknown user ID %s in file %s\n"),item.user,general2);
119 exit(EXIT_FAILURE);
120 }
121
122 if (item.nacc > 0) nsitesusers = 1;
123 if (!nsitesusers) continue;
124
125 if (ourl==NULL || strcmp(item.url,ourl) != 0) {
126 if (regs>0) fputs("</td></tr>\n",fp_ou);
127
128 regs++;
129 if (SiteUsersReportLimit && regs >= SiteUsersReportLimit)
130 break;
131 fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",regs);
132
133 url_len=strlen(item.url);
134 if (url_len>=ourl_size) {
135 ourl_size=url_len+1;
136 ourl=realloc(ourl,ourl_size);
137 if (!ourl) {
138 debuga(_("Not enough memory to store the url\n"));
139 exit(EXIT_FAILURE);
140 }
141 }
142 strcpy(ourl,item.url);
143
144 if(BlockIt[0]!='\0' && ourl[0]!=ALIAS_PREFIX) {
145 fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
146 output_html_url(fp_ou,ourl);
147 fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
148 }
149 output_html_link(fp_ou,ourl,100);
150 fputs("</td>",fp_ou);
151
152 if (BytesInSitesUsersReport) {
153 fputs("<td class=\"data\"",fp_ou);
154 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(int64_t)item.nbytes);
155 fprintf(fp_ou,">%s</td>",fixnum(item.nbytes,1));
156 }
157 fputs("<td class=\"data2\">",fp_ou);
158
159 userinfo_clearflag();
160 if (topuser_link && uinfo->topuser)
161 fprintf(fp_ou,"<a href=\"%s/%s.html\">%s</a>",uinfo->filename,uinfo->filename,uinfo->label);
162 else
163 fprintf(fp_ou,"%s",uinfo->label);
164 uinfo->flag=1;
165 }
166 else if (uinfo->flag==0) {
167 if (topuser_link && uinfo->topuser)
168 fprintf(fp_ou," <a href=\"%s/%s.html\">%s</a>",uinfo->filename,uinfo->filename,uinfo->label);
169 else
170 fprintf(fp_ou," %s",uinfo->label);
171 uinfo->flag=1;
172 }
173
174 }
175 fclose(fp_in);
176 longline_destroy(&line);
177
178 if(regs>0) {
179 fputs("</td></tr>\n",fp_ou);
180 }
181 if (ourl) free(ourl);
182
183 if (unlink(general2)) {
184 debuga(_("Cannot delete %s - %s\n"),general2,strerror(errno));
185 exit(EXIT_FAILURE);
186 }
187
188 fputs("</table></div>\n",fp_ou);
189 if (write_html_trailer(fp_ou)<0)
190 debuga(_("Write error in file %s\n"),report);
191 if (fclose(fp_ou)==EOF)
192 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
193
194 return;
195 }