]>
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 | |
25697a35 GS |
35 | char user[MAXLEN]; |
36 | char url[MAXLEN]; | |
37 | char wuser[MAXLEN]; | |
38 | char ourl[MAXLEN]; | |
25697a35 | 39 | char csort[255]; |
d6e703cc FM |
40 | char general[MAXLEN]; |
41 | char general2[MAXLEN]; | |
25697a35 | 42 | char per[MAXLEN]; |
25697a35 GS |
43 | char sites[MAXLEN]; |
44 | char report[MAXLEN]; | |
d6e703cc | 45 | char period[100]; |
25697a35 GS |
46 | int regs=0; |
47 | int ucount=0; | |
48 | char *users; | |
6e792ade FM |
49 | long long int nbytes; |
50 | long long int obytes; | |
51 | long long int nacc; | |
456d78a5 | 52 | int cstatus; |
9c7c6346 | 53 | struct getwordstruct gwarea; |
25697a35 | 54 | |
e6414a9d | 55 | if(Privacy) |
d6e703cc FM |
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); | |
25697a35 GS |
63 | sprintf(report,"%s/siteuser.html",dirname); |
64 | ||
65 | if ((fp_in = fopen(per, "r")) == 0) { | |
05b90947 | 66 | fprintf(stderr, "SARG: (siteuser) %s: %s\n",text[45],per); |
25697a35 GS |
67 | exit(1); |
68 | } | |
69 | ||
05b90947 FM |
70 | if (!fgets(period,sizeof(period),fp_in)) { |
71 | fprintf(stderr,"SARG: (siteuser) read error in %s\n",per); | |
72 | exit(1); | |
73 | } | |
25697a35 GS |
74 | fclose(fp_in); |
75 | ||
9a2efbd0 | 76 | sprintf(csort,"sort -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general); |
456d78a5 FM |
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 | } | |
25697a35 | 83 | |
d6e703cc FM |
84 | if((fp_in=fopen(general2,"r"))==NULL) { |
85 | fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],general2); | |
456d78a5 | 86 | fprintf(stderr, "SARG: sort command: %s\n",csort); |
25697a35 GS |
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 | ||
c0ec9cc7 | 95 | write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Sites & Users")); |
b18ce4a0 FM |
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]); | |
c0ec9cc7 | 98 | close_html_header(fp_ou); |
25697a35 | 99 | |
c0ec9cc7 | 100 | fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou); |
25697a35 | 101 | fputs("<tr><td></td></tr>\n",fp_ou); |
e6414a9d | 102 | if(BytesInSitesUsersReport) |
b18ce4a0 | 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]); |
354c1a68 | 104 | else |
b18ce4a0 | 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]); |
dfb337be | 106 | |
25697a35 GS |
107 | user[0]='\0'; |
108 | ourl[0]='\0'; | |
6e792ade | 109 | obytes=0; |
25697a35 GS |
110 | |
111 | if((users=(char *) malloc(204800))==NULL){ | |
112 | fprintf(stderr, "SARG: ERROR: %s",text[87]); | |
113 | exit(1); | |
114 | } | |
936c9905 | 115 | strcpy(users," "); |
25697a35 GS |
116 | |
117 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
9c7c6346 FM |
118 | getword_start(&gwarea,buf); |
119 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { | |
4bcb77cf FM |
120 | printf("SARG: Maybe you have an invalid user in the %s file of the siteuser.\n",general2); |
121 | exit(1); | |
122 | } | |
25697a35 GS |
123 | if(strcmp(user,"TOTAL") == 0) |
124 | continue; | |
125 | if(userip) | |
126 | fixip(user); | |
127 | ||
965c4a6f | 128 | user_find(name, sizeof(name), user); |
936c9905 FM |
129 | |
130 | if(dotinuser && strchr(name,'_')) { | |
48864d28 | 131 | subs(name,sizeof(name),"_","."); |
94ff9470 | 132 | } |
25697a35 | 133 | |
936c9905 FM |
134 | /* |
135 | In fact, even the first call is unecessary as the resolved user variable is never used. | |
e6414a9d | 136 | if(Ip2Name) |
a1c55d8c | 137 | ip2name(user,sizeof(user)); |
936c9905 | 138 | */ |
25697a35 | 139 | |
936c9905 FM |
140 | /* |
141 | Is this redundant ip2name a mistake or is it really necessary ? It definitely slow down sarg | |
142 | if the first ip2name succeed because it will try to resolve a name which is not an IP but if | |
143 | the first ip2name failed, the second attempt may find the expected address which was resolved | |
144 | in the mean time by the DNS server. | |
e6414a9d | 145 | if(Ip2Name) |
a1c55d8c | 146 | ip2name(user,sizeof(user)); |
936c9905 | 147 | */ |
25697a35 | 148 | |
6e792ade | 149 | if (getword_atoll(&nacc,&gwarea,'\t')<0){ |
4bcb77cf FM |
150 | printf("SARG: Maybe you have an invalid number of access in your %s file of the siteuser.\n",general2); |
151 | exit(1); | |
152 | } | |
6e792ade FM |
153 | if (nacc > 0) nsitesusers = 1; |
154 | if (getword_atoll(&nbytes,&gwarea,'\t')<0){ | |
4bcb77cf FM |
155 | printf("SARG: Maybe you have an invalid number of bytes in your %s file of the siteuser.\n",general2); |
156 | exit(1); | |
157 | } | |
9c7c6346 | 158 | if (getword(url,sizeof(url),&gwarea,'\t')<0) { |
4bcb77cf FM |
159 | printf("SARG: Maybe you have an invalid url in your %s file of the siteuser.\n",general2); |
160 | exit(1); | |
161 | } | |
25697a35 GS |
162 | |
163 | if(!regs) { | |
164 | strcpy(ourl,url); | |
6e792ade | 165 | obytes=nbytes; |
25697a35 GS |
166 | regs++; |
167 | } | |
dfb337be | 168 | |
25697a35 GS |
169 | sprintf(wuser," %s ",name); |
170 | if(strstr(users,wuser) == 0 && strcmp(url,ourl) == 0) { | |
171 | strcat(users,name); | |
172 | strcat(users," "); | |
173 | ucount++; | |
174 | if(ucount>4) { | |
175 | strcat(users,"<br>"); | |
176 | ucount=0; | |
177 | } | |
178 | } | |
179 | ||
180 | if(SiteUsersReportLimit) { | |
181 | if(regs >= SiteUsersReportLimit) | |
182 | continue; | |
183 | } | |
184 | ||
dfb337be | 185 | if(BlockIt[0]!='\0') |
c0ec9cc7 | 186 | sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",wwwDocumentRoot,BlockIt,ourl); |
25697a35 GS |
187 | else BlockImage[0]='\0'; |
188 | ||
d6e703cc | 189 | if(strcmp(url,ourl) != 0 && nsitesusers) { |
e6414a9d | 190 | if(BytesInSitesUsersReport) { |
6e792ade | 191 | sprintf(wwork2,"%s",fixnum(obytes,1)); |
dfb337be | 192 | fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\">%s</a></td><td class=\"data\">%s</td><td class=\"data2\">%s</td></tr>\n",regs,BlockImage,ourl,ourl,wwork2,users); |
354c1a68 | 193 | } else |
dfb337be | 194 | fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\">%s</a></td><td class=\"data2\">%s</td></tr>\n",regs,BlockImage,ourl,ourl,users); |
25697a35 GS |
195 | regs++; |
196 | ucount=0; | |
354c1a68 | 197 | strcpy(users,name); |
25697a35 GS |
198 | strcat(users," "); |
199 | strcpy(ourl,url); | |
6e792ade | 200 | obytes=nbytes; |
25697a35 GS |
201 | } |
202 | } | |
c0ec9cc7 | 203 | fclose(fp_in); |
25697a35 | 204 | |
d6e703cc | 205 | if(nsitesusers) { |
dfb337be | 206 | fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\"><a href=\"http://%s\">%s</a></td><td class=\"data2\">%s</td></tr>\n",regs,ourl,ourl,users); |
d6e703cc | 207 | } |
25697a35 | 208 | |
d6e703cc | 209 | unlink(general2); |
25697a35 | 210 | |
c0ec9cc7 FM |
211 | fputs("</table></div>\n",fp_ou); |
212 | write_html_trailer(fp_ou); | |
25697a35 GS |
213 | fclose(fp_ou); |
214 | ||
491b862f GS |
215 | if(users) |
216 | free(users); | |
217 | ||
25697a35 GS |
218 | return; |
219 | ||
220 | } |