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