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