]>
git.ipfire.org Git - thirdparty/sarg.git/blob - index.c
ebeb239638a8afa3c6667ce2e8a2c10c9351044a
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
26 #include "include/conf.h"
31 FILE *fp_ou
, *fp_ou2
, *fp_ou3
, *fp_tmp
, *fp_tmp2
, *fp_tmp3
;
32 DIR *dirp
, *dirp2
, *dirp3
;
33 struct dirent
*direntp
;
34 struct dirent
*direntp2
;
35 struct dirent
*direntp3
;
38 char wdir_tmp
[MAXLEN
];
39 char wdir_tmp2
[MAXLEN
];
40 char wdir_tmp3
[MAXLEN
];
49 char day
[4], mon
[4], year
[6], hour
[10];
50 char h
[3], m
[3], s
[3];
52 if(LastLog
[0] != '\0') mklastlog(outdir
,debug
);
54 sprintf(wdir
,"%sindex.html",outdir
);
55 sprintf(wdir_tmp
,"%sindex.unsort",outdir
);
56 sprintf(wdir_tmp2
,"%sindex.sort",outdir
);
57 strcpy(hbc1
,"class=\"header\"");
59 if(strcmp(Index
,"no") == 0) {
60 if(access(wdir
, R_OK
) == 0) unlink(wdir
);
64 if(debug
) debuga(text
[53]);
67 dirp
= opendir(outdir
);
68 while ((direntp
= readdir( dirp
)) != NULL
) {
69 if(strcmp(IndexTree
,"date") == 0) {
70 if(!isdigit(direntp
->d_name
[0]) && !isdigit(direntp
->d_name
[1])) continue;
71 if(strlen(direntp
->d_name
) > 4) {
78 if(strcmp(df
,"u") == 0) {
79 strncpy(y1
,direntp
->d_name
,4);
80 strncpy(m1
,direntp
->d_name
+4,3);
81 strncpy(d1
,direntp
->d_name
+7,2);
82 strncpy(y2
,direntp
->d_name
+10,4);
83 strncpy(m2
,direntp
->d_name
+14,3);
84 strncpy(d2
,direntp
->d_name
+17,2);
85 } else if(strcmp(df
,"e") == 0) {
86 strncpy(y1
,direntp
->d_name
+5,4);
87 strncpy(m1
,direntp
->d_name
+2,3);
88 strncpy(d1
,direntp
->d_name
,2);
89 strncpy(y2
,direntp
->d_name
+15,4);
90 strncpy(m2
,direntp
->d_name
+12,3);
91 strncpy(d2
,direntp
->d_name
+10,2);
96 sprintf(val1
,"%s%s",outdir
,y1
);
97 if(access(val1
, R_OK
) != 0) mkdir(val1
,0755);
98 if(strcmp(m1
,m2
) != 0) sprintf(val2
,"%s/%s-%s",val1
,m1
,m2
);
99 else sprintf(val2
,"%s/%s",val1
,m1
);
100 if(access(val2
, R_OK
) != 0) mkdir(val2
,0755);
101 if(strcmp(d1
,d2
) != 0) sprintf(val3
,"%s/%s-%s",val2
,d1
,d2
);
102 else sprintf(val3
,"%s/%s",val2
,d1
);
103 sprintf(val4
,"%s%s",outdir
,direntp
->d_name
);
105 sprintf(val5
,"%s/images",val2
);
106 if(access(val5
, R_OK
) != 0) {
107 sprintf(val5
,"ln -s %simages %s/images",outdir
,val2
);
111 if(!isdigit(direntp
->d_name
[0]) && !isdigit(direntp
->d_name
[1])) continue;
112 if(strlen(direntp
->d_name
) == 4) {
113 strcpy(y1
,direntp
->d_name
);
114 sprintf(val1
,"%s%s",outdir
,direntp
->d_name
);
115 dirp2
= opendir(val1
);
116 while ((direntp2
= readdir( dirp2
)) != NULL
) {
117 if(!isdigit(direntp2
->d_name
[0]) && !isdigit(direntp2
->d_name
[1])) continue;
118 sprintf(val2
,"%s/%s",val1
,direntp2
->d_name
);
119 dirp3
= opendir(val2
);
120 while ((direntp3
= readdir( dirp3
)) != NULL
) {
121 if(!isdigit(direntp3
->d_name
[0]) && !isdigit(direntp3
->d_name
[1])) continue;
123 strcpy(warea
,direntp2
->d_name
);
124 if(strstr(warea
,"-") != 0) {
125 getword(m1
,warea
,'-');
134 strcpy(warea
,direntp3
->d_name
);
135 if(strstr(warea
,"-") != 0) {
136 getword(d1
,warea
,'-');
142 if(strcmp(df
,"u") == 0) sprintf(val4
,"%s%s%s%s-%s%s%s",outdir
,y1
,m1
,d1
,y1
,m2
,d2
);
143 else if(strcmp(df
,"e") == 0) sprintf(val4
,"%s%s%s%s-%s%s%s",outdir
,d1
,m1
,y1
,d2
,m2
,y1
);
144 sprintf(val5
,"%s%s/%s/%s",outdir
,y1
,direntp2
->d_name
,direntp3
->d_name
);
145 if(rename(val5
,val4
)) {
146 fprintf(stderr
, "SARG: (index) rename error - %s\n",strerror(errno
));
150 (void)rewinddir( dirp3
);
151 (void)closedir( dirp3
);
153 (void)rewinddir( dirp2
);
154 (void)closedir( dirp2
);
156 // sprintf(cmd,"rm -rf %s%s\n",outdir,direntp->d_name);
160 (void)rewinddir( dirp
);
161 (void)closedir( dirp
);
163 if((fp_tmp
=fopen(wdir_tmp
,"w"))==NULL
) {
164 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir_tmp
);
168 dirp
= opendir(outdir
);
169 while ((direntp
= readdir( dirp
)) != NULL
) {
170 if(strcmp(IndexTree
,"date") == 0) {
171 if(strlen(direntp
->d_name
) > 4 || !isdigit(direntp
->d_name
[0]) && !isdigit(direntp
->d_name
[1])) continue;
172 fprintf(fp_tmp
,"%s %s\n",direntp
->d_name
,get_size(outdir
,direntp
->d_name
));
175 if(strstr(direntp
->d_name
,"-") == 0) continue;
177 if(strcmp(df
,"u") == 0) {
178 strncat(newname
,direntp
->d_name
,4);
179 strncpy(month
,direntp
->d_name
+4,3);
181 strncat(newname
,direntp
->d_name
+5,4);
182 strncpy(month
,direntp
->d_name
+2,3);
186 strcat(newname
,month
);
187 if(strcmp(df
,"u") == 0) strncat(newname
,direntp
->d_name
+7,2);
188 else strncat(newname
,direntp
->d_name
,2);
189 obtdate(outdir
,direntp
->d_name
,data
);
190 obtuser(outdir
,direntp
->d_name
,tuser
);
191 obttotal(outdir
,direntp
->d_name
,tbytes
,tuser
,media
);
193 getword(mon
,html
,' ');
194 getword(mon
,html
,' ');
195 getword(day
,html
,' ');
196 getword(hour
,html
,' ');
197 getword(year
,html
,' ');
198 getword(year
,html
,' ');
203 buildymd(day
,mon
,year
,ftime
);
204 fprintf(fp_tmp
,"%s%s%s%s;%s;%s;%s;%s;%s;%s\n",ftime
, h
, m
, s
, direntp
->d_name
, data
, tuser
, tbytes
, media
,newname
);
209 if(fp_tmp
) fclose(fp_tmp
);
210 if(strcmp(IndexTree
,"file") == 0) {
211 (void)rewinddir( dirp
);
212 (void)closedir( dirp
);
215 if(strcmp(IndexTree
,"date") == 0) {
216 if(strcmp(IndexSortOrder
,"A") == 0) sprintf(warea
,"sort -k 1,1 '%s' -o '%s'", wdir_tmp
, wdir_tmp2
);
217 else sprintf(warea
,"sort -r -k 1,1 '%s' -o '%s'", wdir_tmp
, wdir_tmp2
);
220 if((fp_tmp
=fopen(wdir_tmp2
,"r"))==NULL
) {
221 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir_tmp2
);
224 if((fp_ou
=fopen(wdir
,"w"))==NULL
) {
225 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir
);
228 write_html_header(fp_ou
, ".");
229 fprintf(fp_ou
,"<tr><th %s>%s</th><th %s>%s</th></tr>\n",hbc1
,text
[130],hbc1
,text
[132]);
230 while(fgets(wwork1
,MAXLEN
,fp_tmp
)!=NULL
) {
231 getword(tmp4
,wwork1
,' ');
232 fprintf(fp_ou
,"<tr><td class=\"data2\"><a href=\"%s\">%s</a></td><td class=\"data2\">%s</td></tr>\n",tmp4
,tmp4
,wwork1
);
233 sprintf(tmp2
,"%s%s",outdir
,tmp4
);
234 sprintf(tmp3
,"%s%s/index.unsort",outdir
,tmp4
);
236 if((fp_ou2
=fopen(tmp3
,"w"))==NULL
) {
237 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],tmp3
);
240 dirp2
= opendir(tmp2
);
241 while ((direntp2
= readdir( dirp2
)) != NULL
) {
242 if(!isdigit(direntp2
->d_name
[0]) && !isdigit(direntp2
->d_name
[1])) continue;
243 fprintf(fp_ou2
,"%s\n",direntp2
->d_name
);
245 if(fp_ou2
) fclose(fp_ou2
);
246 (void)rewinddir(dirp2
);
247 (void)closedir(dirp2
);
248 sprintf(wdir_tmp3
,"%s%s/index.sort",outdir
,tmp4
);
249 if(strcmp(IndexSortOrder
,"A") == 0) sprintf(csort
,"sort -n '%s' -o '%s'", tmp3
, wdir_tmp3
);
250 else sprintf(csort
,"sort -n -r '%s' -o '%s'", tmp3
, wdir_tmp3
);
253 if((fp_tmp2
=fopen(wdir_tmp3
,"r"))==NULL
) {
254 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir_tmp3
);
257 sprintf(tmp3
,"%s%s/index.html",outdir
,tmp4
);
258 if((fp_ou2
=fopen(tmp3
,"w"))==NULL
) {
259 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir
);
262 write_html_header(fp_ou2
,"..");
263 fprintf(fp_ou2
,"<tr><th %s>%s/%s</th></tr>\n",hbc1
,text
[130],text
[131]);
264 while(fgets(wwork1
,MAXLEN
,fp_tmp2
)!=NULL
) {
265 wwork1
[strlen(wwork1
)-1]='\0';
267 if(strstr(tmp5
,"-") != 0) {
268 getword(warea
,tmp5
,'-');
270 sprintf(tmp6
,"%s-",warea
);
272 sprintf(nmonth
,"%s%s",tmp6
,tmp5
);
277 fprintf(fp_ou2
,"<tr><td class=\"data2\"><a href=\"%s\">%s %s</a></td></tr>\n",wwork1
,tmp4
,nmonth
);
279 sprintf(val1
,"%s%s/%s",outdir
,tmp4
,wwork1
);
280 sprintf(tmp5
,"%s%s/%s/index.unsort",outdir
,tmp4
,wwork1
);
281 if((fp_ou3
=fopen(tmp5
,"w"))==NULL
) {
282 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],tmp5
);
286 dirp3
= opendir(val1
);
287 while ((direntp3
= readdir( dirp3
)) != NULL
) {
288 if(!isdigit(direntp3
->d_name
[0]) && !isdigit(direntp3
->d_name
[1])) continue;
289 fprintf(fp_ou3
,"%s\n",direntp3
->d_name
);
291 if(fp_ou3
) fclose(fp_ou3
);
292 (void)rewinddir(dirp3
);
293 (void)closedir(dirp3
);
295 sprintf(tmp6
,"%s%s/%s/index.sort",outdir
,tmp4
,wwork1
);
296 if(strcmp(IndexSortOrder
,"A") == 0) sprintf(csort
,"sort -n '%s' -o '%s'", tmp5
, tmp6
);
297 else sprintf(csort
,"sort -n -r '%s' -o '%s'", tmp5
, tmp6
);
300 sprintf(val2
,"%s%s/%s/index.html",outdir
,tmp4
,wwork1
);
301 sprintf(val3
,"%s/%s",tmp4
,wwork1
);
303 if((fp_ou3
=fopen(val2
,"w"))==NULL
) {
304 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],val2
);
307 if((fp_tmp3
=fopen(tmp6
,"r"))==NULL
) {
308 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],tmp6
);
311 write_html_header(fp_ou3
,"../..");
312 fprintf(fp_ou3
,"<tr><th %s>%s/%s/%s</th></tr>\n",hbc1
,text
[130],text
[131],text
[127]);
313 while(fgets(warea
,MAXLEN
,fp_tmp3
)!=NULL
) {
314 warea
[strlen(warea
)-1]='\0';
315 fprintf(fp_ou3
,"<tr><td class=\"data2\"><a href=\"%s\">%s %s %s</a></td></tr>\n",warea
,tmp4
,nmonth
,warea
);
317 if(fp_tmp3
) fclose(fp_tmp3
);
318 write_html_trailer(fp_ou3
);
319 if(fp_ou3
) fclose(fp_ou3
);
322 write_html_trailer(fp_ou2
);
323 if(fp_ou2
) fclose(fp_ou2
);
325 (void)rewinddir(dirp
);
326 (void)closedir(dirp
);
327 write_html_trailer(fp_ou
);
328 if(fp_ou
) fclose(fp_ou
);
329 if(fp_tmp
) fclose(fp_tmp
);
333 if(strcmp(IndexSortOrder
,"A") == 0) sprintf(warea
,"sort -t';' -k 7,7 -k 1,1 '%s' -o '%s'", wdir_tmp
, wdir_tmp2
);
334 else sprintf(warea
,"sort -r -t';' -k 7,7 -k 1,1 '%s' -o '%s'", wdir_tmp
, wdir_tmp2
);
337 if((fp_ou
=fopen(wdir
,"w"))==NULL
) {
338 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir
);
341 write_html_header(fp_ou
,".");
342 fprintf(fp_ou
,"<tr><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th></tr>\n",hbc1
,text
[101],hbc1
,text
[102],hbc1
,text
[103],hbc1
,text
[93],hbc1
,text
[96]);
343 if((fp_tmp2
=fopen(wdir_tmp2
,"r"))==NULL
) {
344 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir_tmp2
);
347 while(fgets(buf
,MAXLEN
,fp_tmp2
)!=NULL
) {
348 getword(period
,buf
,';');
349 getword(period
,buf
,';');
350 getword(data
,buf
,';');
351 getword(tuser
,buf
,';');
352 getword(tbytes
,buf
,';');
353 getword(media
,buf
,';');
354 fprintf(fp_ou
,"<tr><td class=\"data2\"><a href='%s/%s'>%s</a></td><td class=\"data2\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",period
,ReplaceIndex
,period
,data
,tuser
,tbytes
,media
);
356 if(fp_tmp2
) fclose(fp_tmp2
);
360 fputs("</table></center>",fp_ou
);
362 zdate(ftime
, DateFormat
);
366 fputs("</body>\n</html>\n",fp_ou
);