]>
git.ipfire.org Git - thirdparty/sarg.git/blob - index.c
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')
53 mklastlog(outdir
,debug
);
55 sprintf(wdir
,"%sindex.html",outdir
);
56 sprintf(wdir_tmp
,"%sindex.unsort",outdir
);
57 sprintf(wdir_tmp2
,"%sindex.sort",outdir
);
58 strcpy(hbc1
,"class=\"header\"");
60 if(strcmp(Index
,"no") == 0) {
61 if(access(wdir
, R_OK
) == 0)
70 dirp
= opendir(outdir
);
71 while ((direntp
= readdir( dirp
)) != NULL
) {
72 if(strcmp(IndexTree
,"date") == 0) {
73 if(!isdigit(direntp
->d_name
[0]) && !isdigit(direntp
->d_name
[1]))
75 if(strlen(direntp
->d_name
) > 4) {
82 if(strcmp(df
,"u") == 0) {
83 strncpy(y1
,direntp
->d_name
,4);
84 strncpy(m1
,direntp
->d_name
+4,3);
85 strncpy(d1
,direntp
->d_name
+7,2);
86 strncpy(y2
,direntp
->d_name
+10,4);
87 strncpy(m2
,direntp
->d_name
+14,3);
88 strncpy(d2
,direntp
->d_name
+17,2);
89 } else if(strcmp(df
,"e") == 0) {
90 strncpy(y1
,direntp
->d_name
+5,4);
91 strncpy(m1
,direntp
->d_name
+2,3);
92 strncpy(d1
,direntp
->d_name
,2);
93 strncpy(y2
,direntp
->d_name
+15,4);
94 strncpy(m2
,direntp
->d_name
+12,3);
95 strncpy(d2
,direntp
->d_name
+10,2);
100 sprintf(val1
,"%s%s",outdir
,y1
);
101 if(access(val1
, R_OK
) != 0)
103 if(strcmp(m1
,m2
) != 0)
104 sprintf(val2
,"%s/%s-%s",val1
,m1
,m2
);
106 sprintf(val2
,"%s/%s",val1
,m1
);
107 if(access(val2
, R_OK
) != 0)
109 if(strcmp(d1
,d2
) != 0)
110 sprintf(val3
,"%s/%s-%s",val2
,d1
,d2
);
112 sprintf(val3
,"%s/%s",val2
,d1
);
113 sprintf(val4
,"%s%s",outdir
,direntp
->d_name
);
115 sprintf(val5
,"%s/images",val2
);
116 if(access(val5
, R_OK
) != 0) {
117 sprintf(val5
,"ln -s %simages %s/images",outdir
,val2
);
121 if(!isdigit(direntp
->d_name
[0]) && !isdigit(direntp
->d_name
[1]))
123 if(strlen(direntp
->d_name
) == 4) {
124 strcpy(y1
,direntp
->d_name
);
125 sprintf(val1
,"%s%s",outdir
,direntp
->d_name
);
126 dirp2
= opendir(val1
);
127 while ((direntp2
= readdir( dirp2
)) != NULL
) {
128 if(!isdigit(direntp2
->d_name
[0]) && !isdigit(direntp2
->d_name
[1]))
130 sprintf(val2
,"%s/%s",val1
,direntp2
->d_name
);
131 dirp3
= opendir(val2
);
132 while ((direntp3
= readdir( dirp3
)) != NULL
) {
133 if(!isdigit(direntp3
->d_name
[0]) && !isdigit(direntp3
->d_name
[1]))
136 strcpy(warea
,direntp2
->d_name
);
137 if(strstr(warea
,"-") != 0) {
138 getword(m1
,warea
,'-');
147 strcpy(warea
,direntp3
->d_name
);
148 if(strstr(warea
,"-") != 0) {
149 getword(d1
,warea
,'-');
155 if(strcmp(df
,"u") == 0)
156 sprintf(val4
,"%s%s%s%s-%s%s%s",outdir
,y1
,m1
,d1
,y1
,m2
,d2
);
157 else if(strcmp(df
,"e") == 0)
158 sprintf(val4
,"%s%s%s%s-%s%s%s",outdir
,d1
,m1
,y1
,d2
,m2
,y1
);
159 sprintf(val5
,"%s%s/%s/%s",outdir
,y1
,direntp2
->d_name
,direntp3
->d_name
);
160 if(rename(val5
,val4
)) {
161 fprintf(stderr
, "SARG: (index) rename error - %s\n",strerror(errno
));
165 (void)rewinddir( dirp3
);
166 (void)closedir( dirp3
);
168 (void)rewinddir( dirp2
);
169 (void)closedir( dirp2
);
171 // sprintf(cmd,"rm -rf %s%s\n",outdir,direntp->d_name);
175 (void)rewinddir( dirp
);
176 (void)closedir( dirp
);
178 if((fp_tmp
=fopen(wdir_tmp
,"w"))==NULL
) {
179 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir_tmp
);
183 dirp
= opendir(outdir
);
184 while ((direntp
= readdir( dirp
)) != NULL
) {
185 if(strcmp(IndexTree
,"date") == 0) {
186 if(strlen(direntp
->d_name
) > 4 || !isdigit(direntp
->d_name
[0]) && !isdigit(direntp
->d_name
[1]))
188 sprintf(warea
,"%s %s\n",direntp
->d_name
,get_size(outdir
,direntp
->d_name
));
189 fputs(warea
, fp_tmp
);
192 if(strstr(direntp
->d_name
,"-") == 0)
195 if(strcmp(df
,"u") == 0) {
196 strncat(newname
,direntp
->d_name
,4);
197 strncpy(month
,direntp
->d_name
+4,3);
199 strncat(newname
,direntp
->d_name
+5,4);
200 strncpy(month
,direntp
->d_name
+2,3);
204 strcat(newname
,month
);
205 if(strcmp(df
,"u") == 0)
206 strncat(newname
,direntp
->d_name
+7,2);
208 strncat(newname
,direntp
->d_name
,2);
209 obtdate(outdir
,direntp
->d_name
,data
);
210 obtuser(outdir
,direntp
->d_name
,tuser
);
211 obttotal(outdir
,direntp
->d_name
,tbytes
,tuser
,media
);
213 getword(mon
,html
,' ');
214 getword(mon
,html
,' ');
215 getword(day
,html
,' ');
216 getword(hour
,html
,' ');
217 getword(year
,html
,' ');
218 getword(year
,html
,' ');
223 buildymd(day
,mon
,year
,ftime
);
224 sprintf(warea
,"%s%s%s%s;%s;%s;%s;%s;%s;%s\n",ftime
, h
, m
, s
, direntp
->d_name
, data
, tuser
, tbytes
, media
,newname
);
225 fputs(warea
, fp_tmp
);
230 if(fp_tmp
) fclose(fp_tmp
);
231 if(strcmp(IndexTree
,"file") == 0) {
232 (void)rewinddir( dirp
);
233 (void)closedir( dirp
);
236 if(strcmp(IndexTree
,"date") == 0) {
237 if(strcmp(IndexSortOrder
,"A") == 0)
238 sprintf(warea
,"sort -k 1,1 '%s' -o '%s'", wdir_tmp
, wdir_tmp2
);
240 sprintf(warea
,"sort -r -k 1,1 '%s' -o '%s'", wdir_tmp
, wdir_tmp2
);
243 if((fp_tmp
=fopen(wdir_tmp2
,"r"))==NULL
) {
244 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir_tmp2
);
247 if((fp_ou
=fopen(wdir
,"w"))==NULL
) {
248 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir
);
251 write_html_header(fp_ou
, ".");
252 sprintf(html
,"<tr><th %s>%s</th><th %s>%s</th></tr>\n",hbc1
,text
[130],hbc1
,text
[132]);
254 while(fgets(wwork1
,MAXLEN
,fp_tmp
)!=NULL
) {
255 getword(tmp4
,wwork1
,' ');
256 sprintf(html
,"<tr><td class=\"data2\"><a href=\"%s\">%s</a></td><td class=\"data2\">%s</td></tr>\n",tmp4
,tmp4
,wwork1
);
258 sprintf(tmp2
,"%s%s",outdir
,tmp4
);
259 sprintf(tmp3
,"%s%s/index.unsort",outdir
,tmp4
);
261 if((fp_ou2
=fopen(tmp3
,"w"))==NULL
) {
262 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],tmp3
);
265 dirp2
= opendir(tmp2
);
266 while ((direntp2
= readdir( dirp2
)) != NULL
) {
267 if(!isdigit(direntp2
->d_name
[0]) && !isdigit(direntp2
->d_name
[1]))
269 sprintf(warea
,"%s\n",direntp2
->d_name
);
272 if(fp_ou2
) fclose(fp_ou2
);
273 (void)rewinddir(dirp2
);
274 (void)closedir(dirp2
);
275 sprintf(wdir_tmp3
,"%s%s/index.sort",outdir
,tmp4
);
276 if(strcmp(IndexSortOrder
,"A") == 0)
277 sprintf(csort
,"sort -n '%s' -o '%s'", tmp3
, wdir_tmp3
);
279 sprintf(csort
,"sort -n -r '%s' -o '%s'", tmp3
, wdir_tmp3
);
282 if((fp_tmp2
=fopen(wdir_tmp3
,"r"))==NULL
) {
283 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir_tmp3
);
286 sprintf(tmp3
,"%s%s/index.html",outdir
,tmp4
);
287 if((fp_ou2
=fopen(tmp3
,"w"))==NULL
) {
288 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir
);
291 write_html_header(fp_ou2
,"..");
292 sprintf(html
,"<tr><th %s>%s/%s</th></tr>\n",hbc1
,text
[130],text
[131]);
294 while(fgets(wwork1
,MAXLEN
,fp_tmp2
)!=NULL
) {
295 wwork1
[strlen(wwork1
)-1]='\0';
297 if(strstr(tmp5
,"-") != 0) {
298 getword(warea
,tmp5
,'-');
300 sprintf(tmp6
,"%s-",warea
);
302 sprintf(nmonth
,"%s%s",tmp6
,tmp5
);
307 sprintf(html
,"<tr><td class=\"data2\"><a href=\"%s\">%s %s</a></td></tr>\n",wwork1
,tmp4
,nmonth
);
310 sprintf(val1
,"%s%s/%s",outdir
,tmp4
,wwork1
);
311 sprintf(tmp5
,"%s%s/%s/index.unsort",outdir
,tmp4
,wwork1
);
312 if((fp_ou3
=fopen(tmp5
,"w"))==NULL
) {
313 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],tmp5
);
317 dirp3
= opendir(val1
);
318 while ((direntp3
= readdir( dirp3
)) != NULL
) {
319 if(!isdigit(direntp3
->d_name
[0]) && !isdigit(direntp3
->d_name
[1]))
321 sprintf(warea
,"%s\n",direntp3
->d_name
);
325 if(fp_ou3
) fclose(fp_ou3
);
326 (void)rewinddir(dirp3
);
327 (void)closedir(dirp3
);
329 sprintf(tmp6
,"%s%s/%s/index.sort",outdir
,tmp4
,wwork1
);
330 if(strcmp(IndexSortOrder
,"A") == 0)
331 sprintf(csort
,"sort -n '%s' -o '%s'", tmp5
, tmp6
);
333 sprintf(csort
,"sort -n -r '%s' -o '%s'", tmp5
, tmp6
);
336 sprintf(val2
,"%s%s/%s/index.html",outdir
,tmp4
,wwork1
);
337 sprintf(val3
,"%s/%s",tmp4
,wwork1
);
339 if((fp_ou3
=fopen(val2
,"w"))==NULL
) {
340 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],val2
);
343 if((fp_tmp3
=fopen(tmp6
,"r"))==NULL
) {
344 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],tmp6
);
347 write_html_header(fp_ou3
,"../..");
348 sprintf(html
,"<tr><th %s>%s/%s/%s</th></tr>\n",hbc1
,text
[130],text
[131],text
[127]);
350 while(fgets(warea
,MAXLEN
,fp_tmp3
)!=NULL
) {
351 warea
[strlen(warea
)-1]='\0';
352 sprintf(html
,"<tr><td class=\"data2\"><a href=\"%s\">%s %s %s</a></td></tr>\n",warea
,tmp4
,nmonth
,warea
);
355 if(fp_tmp3
) fclose(fp_tmp3
);
356 if(fp_ou3
) fclose(fp_ou3
);
357 write_html_trailer(fp_ou3
);
360 write_html_trailer(fp_ou2
);
361 if(fp_ou2
) fclose(fp_ou2
);
363 (void)rewinddir(dirp
);
364 (void)closedir(dirp
);
365 write_html_trailer(fp_ou
);
366 if(fp_ou
) fclose(fp_ou
);
367 if(fp_tmp
) fclose(fp_tmp
);
371 if(strcmp(IndexSortOrder
,"A") == 0)
372 sprintf(warea
,"sort -t';' +6.5n +6.2M +6.0n '%s' -o '%s'", wdir_tmp
, wdir_tmp2
);
374 sprintf(warea
,"sort -t';' +6.5nr +6.2Mr +6.0nr '%s' -o '%s'", wdir_tmp
, wdir_tmp2
);
377 if((fp_ou
=fopen(wdir
,"w"))==NULL
) {
378 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir
);
381 write_html_header(fp_ou
,".");
382 sprintf(html
,"<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]);
384 if((fp_tmp2
=fopen(wdir_tmp2
,"r"))==NULL
) {
385 fprintf(stderr
, "SARG: (index) %s: %s\n",text
[45],wdir_tmp2
);
388 while(fgets(buf
,MAXLEN
,fp_tmp2
)!=NULL
) {
389 getword(period
,buf
,';');
390 getword(period
,buf
,';');
391 getword(data
,buf
,';');
392 getword(tuser
,buf
,';');
393 getword(tbytes
,buf
,';');
394 getword(media
,buf
,';');
395 sprintf(html
,"<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
);
398 if(fp_tmp2
) fclose(fp_tmp2
);
402 strcpy(html
,"</table></center>");
405 zdate(ftime
, DateFormat
);
409 strcpy(html
,"</body>\n</html>\n");