]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
110ce984 | 3 | * 1998, 2015 |
25697a35 GS |
4 | * |
5 | * SARG donations: | |
6 | * please look at http://sarg.sourceforge.net/donations.php | |
1164c474 FM |
7 | * Support: |
8 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |
25697a35 GS |
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" | |
5f3cfd1d | 28 | #include "include/defs.h" |
25697a35 | 29 | |
32e71fa4 | 30 | void siteuser(void) |
25697a35 | 31 | { |
9bd92830 FM |
32 | FILE *fp_in, *fp_ou; |
33 | ||
34 | char *buf; | |
9bd92830 | 35 | char *ourl; |
997a4328 | 36 | char csort[4096]; |
9bd92830 FM |
37 | char general[MAXLEN]; |
38 | char general2[MAXLEN]; | |
39 | char sites[MAXLEN]; | |
40 | char report[MAXLEN]; | |
41 | int regs=0; | |
9bd92830 FM |
42 | int ourl_size; |
43 | int url_len; | |
a58e6d54 FM |
44 | int topuser_link; |
45 | int nsitesusers; | |
9bd92830 FM |
46 | int cstatus; |
47 | longline line; | |
48 | struct generalitemstruct item; | |
a58e6d54 | 49 | struct userinfostruct *uinfo; |
9bd92830 | 50 | |
9f93fec3 | 51 | if(Privacy) { |
cb59dc47 | 52 | if (debugz>=LogLevel_Process) debugaz(_("Sites and users report not generated because privacy option is on\n")); |
9bd92830 | 53 | return; |
9f93fec3 | 54 | } |
9bd92830 | 55 | |
b7413c4c FM |
56 | if (debugz>=LogLevel_Process) |
57 | debuga(_("Creating report to list who visisted what site...\n")); | |
9bd92830 FM |
58 | nsitesusers = 0; |
59 | sprintf(general,"%s/sarg-general",outdirname); | |
60 | sprintf(sites,"%s/sarg-sites",outdirname); | |
61 | sprintf(general2,"%s/sarg-general2",outdirname); | |
62 | sprintf(report,"%s/siteuser.html",outdirname); | |
63 | ||
78eeb33f FM |
64 | if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general)>=sizeof(csort)) { |
65 | debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general,general2); | |
66 | exit(EXIT_FAILURE); | |
67 | } | |
9bd92830 FM |
68 | cstatus=system(csort); |
69 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
70 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
71 | debuga(_("sort command: %s\n"),csort); | |
72 | exit(EXIT_FAILURE); | |
73 | } | |
74 | ||
75 | if((fp_in=fopen(general2,"r"))==NULL) { | |
96dadc9f | 76 | debuga(_("Cannot open file \"%s\": %s\n"),general2,strerror(errno)); |
007905af FM |
77 | debuga(_("sort command: %s\n"),csort); |
78 | exit(EXIT_FAILURE); | |
9bd92830 FM |
79 | } |
80 | ||
81 | if((fp_ou=fopen(report,"w"))==NULL) { | |
96dadc9f | 82 | debuga(_("Cannot open file \"%s\": %s\n"),report,strerror(errno)); |
007905af | 83 | exit(EXIT_FAILURE); |
9bd92830 FM |
84 | } |
85 | ||
86 | write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Sites & Users"),HTML_JS_SORTTABLE); | |
87 | fputs("<tr><td class=\"header_c\">",fp_ou); | |
88 | fprintf(fp_ou,_("Period: %s"),period.html); | |
89 | fputs("</td></tr>\n",fp_ou); | |
90 | fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Sites & Users")); | |
91 | close_html_header(fp_ou); | |
92 | ||
93 | fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\"",fp_ou); | |
94 | if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou); | |
95 | fprintf(fp_ou,">\n<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l",_("NUM")); | |
96 | if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou); | |
97 | fprintf(fp_ou,"\">%s</th>",_("ACCESSED SITE")); | |
98 | if(BytesInSitesUsersReport) | |
99 | fprintf(fp_ou,"<th class=\"header_l\">%s</th>",_("BYTES")); | |
100 | fputs("<th class=\"header_l",fp_ou); | |
101 | if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou); | |
87e8625b | 102 | /* TRANSLATORS: This is a column header showing the users who visited each site. */ |
9bd92830 FM |
103 | fprintf(fp_ou,"\">%s</th></tr></thead>\n",_("USERS")); |
104 | ||
105 | ourl=NULL; | |
106 | ourl_size=0; | |
9bd92830 | 107 | |
a58e6d54 FM |
108 | userinfo_clearflag(); |
109 | topuser_link=((ReportType & REPORT_TYPE_USERS_SITES) != 0 && !indexonly); | |
9bd92830 FM |
110 | |
111 | if ((line=longline_create())==NULL) { | |
112 | debuga(_("Not enough memory to read file %s\n"),general2); | |
113 | exit(EXIT_FAILURE); | |
114 | } | |
115 | ||
116 | while((buf=longline_read(fp_in,line))!=NULL) { | |
117 | ger_read(buf,&item,general2); | |
118 | if(item.total) continue; | |
119 | uinfo=userinfo_find_from_id(item.user); | |
120 | if (!uinfo) { | |
00d1f9ed | 121 | debuga(_("Unknown user ID %s in file \"%s\"\n"),item.user,general2); |
9bd92830 FM |
122 | exit(EXIT_FAILURE); |
123 | } | |
124 | ||
125 | if (item.nacc > 0) nsitesusers = 1; | |
a58e6d54 FM |
126 | if (!nsitesusers) continue; |
127 | ||
128 | if (ourl==NULL || strcmp(item.url,ourl) != 0) { | |
129 | if (regs>0) fputs("</td></tr>\n",fp_ou); | |
130 | ||
131 | regs++; | |
132 | if (SiteUsersReportLimit && regs >= SiteUsersReportLimit) | |
133 | break; | |
134 | fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",regs); | |
135 | ||
9bd92830 | 136 | url_len=strlen(item.url); |
a58e6d54 | 137 | if (url_len>=ourl_size) { |
9bd92830 FM |
138 | ourl_size=url_len+1; |
139 | ourl=realloc(ourl,ourl_size); | |
140 | if (!ourl) { | |
141 | debuga(_("Not enough memory to store the url\n")); | |
142 | exit(EXIT_FAILURE); | |
143 | } | |
144 | } | |
145 | strcpy(ourl,item.url); | |
bd43d81f | 146 | |
67a93701 | 147 | if(BlockIt[0]!='\0' && ourl[0]!=ALIAS_PREFIX) { |
9bd92830 FM |
148 | fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt); |
149 | output_html_url(fp_ou,ourl); | |
150 | fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",fp_ou); | |
151 | } | |
6fa33a32 | 152 | output_html_link(fp_ou,ourl,100); |
67a93701 | 153 | fputs("</td>",fp_ou); |
9bd92830 FM |
154 | |
155 | if (BytesInSitesUsersReport) { | |
156 | fputs("<td class=\"data\"",fp_ou); | |
a58e6d54 FM |
157 | if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(int64_t)item.nbytes); |
158 | fprintf(fp_ou,">%s</td>",fixnum(item.nbytes,1)); | |
9bd92830 | 159 | } |
a58e6d54 FM |
160 | fputs("<td class=\"data2\">",fp_ou); |
161 | ||
162 | userinfo_clearflag(); | |
163 | if (topuser_link && uinfo->topuser) | |
164 | fprintf(fp_ou,"<a href=\"%s/%s.html\">%s</a>",uinfo->filename,uinfo->filename,uinfo->label); | |
165 | else | |
166 | fprintf(fp_ou,"%s",uinfo->label); | |
167 | uinfo->flag=1; | |
168 | } | |
169 | else if (uinfo->flag==0) { | |
170 | if (topuser_link && uinfo->topuser) | |
171 | fprintf(fp_ou," <a href=\"%s/%s.html\">%s</a>",uinfo->filename,uinfo->filename,uinfo->label); | |
172 | else | |
173 | fprintf(fp_ou," %s",uinfo->label); | |
174 | uinfo->flag=1; | |
9bd92830 | 175 | } |
a58e6d54 | 176 | |
9bd92830 | 177 | } |
204781f4 FM |
178 | if (fclose(fp_in)==EOF) { |
179 | debuga(_("Read error in \"%s\": %s\n"),general2,strerror(errno)); | |
180 | exit(EXIT_FAILURE); | |
181 | } | |
9bd92830 FM |
182 | longline_destroy(&line); |
183 | ||
a58e6d54 FM |
184 | if(regs>0) { |
185 | fputs("</td></tr>\n",fp_ou); | |
9bd92830 FM |
186 | } |
187 | if (ourl) free(ourl); | |
188 | ||
11767c6a FM |
189 | if (!KeepTempLog && unlink(general2)) { |
190 | debuga(_("Cannot delete \"%s\": %s\n"),general2,strerror(errno)); | |
08f9b029 FM |
191 | exit(EXIT_FAILURE); |
192 | } | |
9bd92830 FM |
193 | |
194 | fputs("</table></div>\n",fp_ou); | |
342bd723 | 195 | write_html_trailer(fp_ou); |
507460ae | 196 | if (fclose(fp_ou)==EOF) { |
96dadc9f | 197 | debuga(_("Write error in \"%s\": %s\n"),report,strerror(errno)); |
507460ae FM |
198 | exit(EXIT_FAILURE); |
199 | } | |
9bd92830 | 200 | |
9bd92830 | 201 | return; |
25697a35 | 202 | } |