]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
1164c474 | 3 | * 1998, 2010 |
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 GS |
31 | { |
32 | ||
33 | FILE *fp_in, *fp_ou; | |
48864d28 | 34 | |
ac422f9b | 35 | char *buf; |
25697a35 | 36 | char wuser[MAXLEN]; |
e5b2c6f0 | 37 | char *ourl; |
25697a35 | 38 | char csort[255]; |
d6e703cc FM |
39 | char general[MAXLEN]; |
40 | char general2[MAXLEN]; | |
25697a35 GS |
41 | char sites[MAXLEN]; |
42 | char report[MAXLEN]; | |
25697a35 GS |
43 | int regs=0; |
44 | int ucount=0; | |
e5b2c6f0 FM |
45 | int ourl_size; |
46 | int url_len; | |
25697a35 | 47 | char *users; |
6e792ade | 48 | long long int obytes; |
456d78a5 | 49 | int cstatus; |
afaa3b67 | 50 | longline line; |
2240dcea | 51 | struct generalitemstruct item; |
f2ec8c75 | 52 | const struct userinfostruct *uinfo; |
25697a35 | 53 | |
e6414a9d | 54 | if(Privacy) |
d6e703cc FM |
55 | return; |
56 | ||
57 | nsitesusers = 0; | |
d5d021c5 FM |
58 | sprintf(general,"%s/sarg-general",outdirname); |
59 | sprintf(sites,"%s/sarg-sites",outdirname); | |
60 | sprintf(general2,"%s/sarg-general2",outdirname); | |
d5d021c5 | 61 | sprintf(report,"%s/siteuser.html",outdirname); |
25697a35 | 62 | |
9a2efbd0 | 63 | sprintf(csort,"sort -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general); |
456d78a5 FM |
64 | cstatus=system(csort); |
65 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
10210234 FM |
66 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
67 | debuga(_("sort command: %s\n"),csort); | |
06b39c87 | 68 | exit(EXIT_FAILURE); |
456d78a5 | 69 | } |
25697a35 | 70 | |
d6e703cc | 71 | if((fp_in=fopen(general2,"r"))==NULL) { |
d574e592 | 72 | debuga(_("(siteuser) Cannot open log file %s\n"),general2); |
10210234 | 73 | debuga(_("sort command: %s\n"),csort); |
06b39c87 | 74 | exit(EXIT_FAILURE); |
25697a35 GS |
75 | } |
76 | ||
77 | if((fp_ou=fopen(report,"w"))==NULL) { | |
d574e592 | 78 | debuga(_("(siteuser) Cannot open log file %s\n"),report); |
06b39c87 | 79 | exit(EXIT_FAILURE); |
25697a35 GS |
80 | } |
81 | ||
c0ec9cc7 | 82 | write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Sites & Users")); |
fa6552b0 | 83 | fputs("<tr><td class=\"header_c\">",fp_ou); |
fd46f082 | 84 | fprintf(fp_ou,_("Period: %s"),period.html); |
fa6552b0 | 85 | fputs("</td></tr>\n",fp_ou); |
c36c7384 | 86 | fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Sites & Users")); |
c0ec9cc7 | 87 | close_html_header(fp_ou); |
25697a35 | 88 | |
c0ec9cc7 | 89 | fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou); |
e6414a9d | 90 | if(BytesInSitesUsersReport) |
c36c7384 | 91 | 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",_("NUM"),_("ACCESSED SITE"),_("BYTES"),_("USERS")); |
354c1a68 | 92 | else |
c36c7384 | 93 | fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("NUM"),_("ACCESSED SITE"),_("USERS")); |
dfb337be | 94 | |
e5b2c6f0 FM |
95 | ourl=NULL; |
96 | ourl_size=0; | |
6e792ade | 97 | obytes=0; |
25697a35 GS |
98 | |
99 | if((users=(char *) malloc(204800))==NULL){ | |
10210234 | 100 | debuga(_("ERROR: Cannot load. Memory fault\n")); |
06b39c87 | 101 | exit(EXIT_FAILURE); |
25697a35 | 102 | } |
936c9905 | 103 | strcpy(users," "); |
25697a35 | 104 | |
afaa3b67 | 105 | if ((line=longline_create())==NULL) { |
10210234 | 106 | debuga(_("Not enough memory to read file %s\n"),general2); |
06b39c87 | 107 | exit(EXIT_FAILURE); |
ac422f9b FM |
108 | } |
109 | ||
afaa3b67 | 110 | while((buf=longline_read(fp_in,line))!=NULL) { |
2240dcea FM |
111 | ger_read(buf,&item,general2); |
112 | if(item.total) continue; | |
f2ec8c75 FM |
113 | uinfo=userinfo_find_from_id(item.user); |
114 | if (!uinfo) { | |
10210234 | 115 | debuga(_("Unknown user ID %s in file %s\n"),item.user,general2); |
06b39c87 | 116 | exit(EXIT_FAILURE); |
94ff9470 | 117 | } |
25697a35 | 118 | |
2240dcea | 119 | if (item.nacc > 0) nsitesusers = 1; |
25697a35 | 120 | if(!regs) { |
2240dcea | 121 | url_len=strlen(item.url); |
e5b2c6f0 FM |
122 | if (!ourl || url_len>=ourl_size) { |
123 | ourl_size=url_len+1; | |
124 | ourl=realloc(ourl,ourl_size); | |
125 | if (!ourl) { | |
10210234 | 126 | debuga(_("Not enough memory to store the url\n")); |
06b39c87 | 127 | exit(EXIT_FAILURE); |
e5b2c6f0 FM |
128 | } |
129 | } | |
2240dcea FM |
130 | strcpy(ourl,item.url); |
131 | obytes=item.nbytes; | |
25697a35 GS |
132 | regs++; |
133 | } | |
dfb337be | 134 | |
f2ec8c75 | 135 | sprintf(wuser," %s ",uinfo->label); |
2240dcea | 136 | if(strstr(users,wuser) == 0 && strcmp(item.url,ourl) == 0) { |
f2ec8c75 | 137 | strcat(users,uinfo->label); |
25697a35 GS |
138 | strcat(users," "); |
139 | ucount++; | |
140 | if(ucount>4) { | |
141 | strcat(users,"<br>"); | |
142 | ucount=0; | |
143 | } | |
144 | } | |
145 | ||
146 | if(SiteUsersReportLimit) { | |
147 | if(regs >= SiteUsersReportLimit) | |
148 | continue; | |
149 | } | |
150 | ||
2240dcea | 151 | if(strcmp(item.url,ourl) != 0 && nsitesusers) { |
ac422f9b FM |
152 | fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",regs); |
153 | if(BlockIt[0]!='\0') { | |
154 | fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt); | |
155 | output_html_url(fp_ou,ourl); | |
156 | fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",fp_ou); | |
157 | } | |
158 | fputs("<a href=\"http://",fp_ou); | |
159 | output_html_url(fp_ou,ourl); | |
160 | fputs("\">",fp_ou); | |
161 | output_html_string(fp_ou,ourl,100); | |
162 | fputs("</a></td>",fp_ou); | |
163 | ||
164 | if(BytesInSitesUsersReport) | |
165 | fprintf(fp_ou,"<td class=\"data\">%s</td>",fixnum(obytes,1)); | |
166 | fprintf(fp_ou,"<td class=\"data2\">%s</td></tr>\n",users); | |
167 | ||
25697a35 GS |
168 | regs++; |
169 | ucount=0; | |
f2ec8c75 | 170 | strcpy(users,uinfo->label); |
25697a35 | 171 | strcat(users," "); |
2240dcea | 172 | url_len=strlen(item.url); |
e5b2c6f0 FM |
173 | if (url_len>=ourl_size) { |
174 | ourl_size=url_len+1; | |
175 | ourl=realloc(ourl,ourl_size); | |
176 | if (!ourl) { | |
10210234 | 177 | debuga(_("Not enough memory to store the url\n")); |
06b39c87 | 178 | exit(EXIT_FAILURE); |
e5b2c6f0 FM |
179 | } |
180 | } | |
2240dcea FM |
181 | strcpy(ourl,item.url); |
182 | obytes=item.nbytes; | |
25697a35 GS |
183 | } |
184 | } | |
c0ec9cc7 | 185 | fclose(fp_in); |
afaa3b67 | 186 | longline_destroy(&line); |
25697a35 | 187 | |
d6e703cc | 188 | if(nsitesusers) { |
ac422f9b FM |
189 | fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\"><a href=\"http://",regs); |
190 | output_html_url(fp_ou,ourl); | |
191 | fputs("\">",fp_ou); | |
192 | output_html_string(fp_ou,ourl,100); | |
193 | fprintf(fp_ou,"</a></td><td class=\"data2\">%s</td></tr>\n",users); | |
d6e703cc | 194 | } |
e5b2c6f0 | 195 | if (ourl) free(ourl); |
25697a35 | 196 | |
d6e703cc | 197 | unlink(general2); |
25697a35 | 198 | |
c0ec9cc7 | 199 | fputs("</table></div>\n",fp_ou); |
fa6552b0 FM |
200 | if (write_html_trailer(fp_ou)<0) |
201 | debuga(_("Write error in file %s\n"),report); | |
202 | if (fclose(fp_ou)==EOF) | |
203 | debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); | |
25697a35 | 204 | |
491b862f GS |
205 | if(users) |
206 | free(users); | |
207 | ||
25697a35 | 208 | return; |
25697a35 | 209 | } |