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