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