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