]> git.ipfire.org Git - thirdparty/sarg.git/blob - index.c
Ported r167 from branches/v2_2_6_1 (several changes).
[thirdparty/sarg.git] / index.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
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.
14 *
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.
19 *
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.
23 *
24 */
25
26 #include "include/conf.h"
27 #include "include/defs.h"
28
29 void make_index(void)
30 {
31
32 FILE *fp_ou, *fp_ou2, *fp_ou3, *fp_tmp, *fp_tmp2, *fp_tmp3;
33 DIR *dirp, *dirp2, *dirp3;
34 struct dirent *direntp;
35 struct dirent *direntp2;
36 struct dirent *direntp3;
37 char html[MAXLEN];
38 char wdir[MAXLEN];
39 char wdir_tmp[MAXLEN];
40 char wdir_tmp2[MAXLEN];
41 char wdir_tmp3[MAXLEN];
42 char newname[512];
43 char month[4];
44 char period[80];
45 char data[80];
46 char tuser[20];
47 char tbytes[20];
48 char media[20];
49 char ftime[128];
50 char day[16], mon[16], year[40], hour[10];
51 char *str;
52 int cstatus;
53 int iyear, imonth, iday, ihour, iminute, isecond;
54 char y1[5], y2[5];
55 char d1[3], d2[3];
56 char m1[4], m2[4];
57 struct getwordstruct gwarea;
58 int iyear, imonth, iday, ihour, iminute, isecond, idst;
59
60 if(LastLog[0] != '\0') mklastlog(outdir);
61
62 sprintf(wdir,"%sindex.html",outdir);
63 sprintf(wdir_tmp,"%sindex.unsort",outdir);
64 sprintf(wdir_tmp2,"%sindex.sort",outdir);
65 strcpy(hbc1,"class=\"header\"");
66
67 if(strcmp(Index,"no") == 0) {
68 if(access(wdir, R_OK) == 0) unlink(wdir);
69 return;
70 }
71
72 if(debug) debuga("%s",text[53]);
73
74 // Root dir
75 dirp = opendir(outdir);
76 while ((direntp = readdir( dirp )) != NULL) {
77 if(strcmp(IndexTree,"date") == 0) {
78 if(!isdigit(direntp->d_name[0]) && !isdigit(direntp->d_name[1])) continue;
79 if(strlen(direntp->d_name) > 4) {
80 bzero(y1,5);
81 bzero(y2,5);
82 bzero(m1,4);
83 bzero(m2,4);
84 bzero(d1,3);
85 bzero(d2,3);
86 if(strcmp(df,"u") == 0) {
87 strncpy(y1,direntp->d_name,4);
88 strncpy(m1,direntp->d_name+4,3);
89 strncpy(d1,direntp->d_name+7,2);
90 strncpy(y2,direntp->d_name+10,4);
91 strncpy(m2,direntp->d_name+14,3);
92 strncpy(d2,direntp->d_name+17,2);
93 } else if(strcmp(df,"e") == 0) {
94 strncpy(y1,direntp->d_name+5,4);
95 strncpy(m1,direntp->d_name+2,3);
96 strncpy(d1,direntp->d_name,2);
97 strncpy(y2,direntp->d_name+15,4);
98 strncpy(m2,direntp->d_name+12,3);
99 strncpy(d2,direntp->d_name+10,2);
100 }
101 conv_month(m1);
102 conv_month(m2);
103 }
104 sprintf(val1,"%s%s",outdir,y1);
105 if(access(val1, R_OK) != 0) mkdir(val1,0755);
106 if(strcmp(m1,m2) != 0) sprintf(val2,"%s/%s-%s",val1,m1,m2);
107 else sprintf(val2,"%s/%s",val1,m1);
108 if(access(val2, R_OK) != 0) mkdir(val2,0755);
109 if(strcmp(d1,d2) != 0) sprintf(val3,"%s/%s-%s",val2,d1,d2);
110 else sprintf(val3,"%s/%s",val2,d1);
111 sprintf(val4,"%s%s",outdir,direntp->d_name);
112 rename(val4,val3);
113 sprintf(val5,"%s/images",val2);
114 if(access(val5, R_OK) != 0) {
115 sprintf(val5,"ln -s \"%simages\" \"%s/images\"",outdir,val2);
116 cstatus=system(val5);
117 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
118 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
119 fprintf(stderr, "SARG: command: %s\n",val5);
120 exit(1);
121 }
122 }
123 } else {
124 if(!isdigit(direntp->d_name[0]) && !isdigit(direntp->d_name[1])) continue;
125 if(strlen(direntp->d_name) == 4) {
126 strcpy(y1,direntp->d_name);
127 sprintf(val1,"%s%s",outdir,direntp->d_name);
128 dirp2 = opendir(val1);
129 while ((direntp2 = readdir( dirp2 )) != NULL) {
130 if(!isdigit(direntp2->d_name[0]) && !isdigit(direntp2->d_name[1])) continue;
131 sprintf(val2,"%s/%s",val1,direntp2->d_name);
132 dirp3 = opendir(val2);
133 while ((direntp3 = readdir( dirp3 )) != NULL) {
134 if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue;
135 bzero(newname,512);
136 strcpy(warea,direntp2->d_name);
137 if((str = strchr(warea,'-')) != 0) {
138 *str++ = '\0';
139 strcpy(m1,warea);
140 strcpy(m2,str);
141 conv_month_name(m1);
142 conv_month_name(m2);
143 } else {
144 strcpy(m1,warea);
145 conv_month_name(m1);
146 strcpy(m2,m1);
147 }
148 strcpy(warea,direntp3->d_name);
149 if((str = strchr(warea,'-')) != 0) {
150 *str++ = '\0';
151 strcpy(d1,warea);
152 strcpy(d2,warea);
153 } else {
154 strcpy(d1,warea);
155 strcpy(d2,warea);
156 }
157 if(strcmp(df,"u") == 0) sprintf(val4,"%s%s%s%s-%s%s%s",outdir,y1,m1,d1,y1,m2,d2);
158 else if(strcmp(df,"e") == 0) 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));
162 exit(1);
163 }
164 }
165 (void)rewinddir( dirp3 );
166 (void)closedir( dirp3 );
167 }
168 (void)rewinddir( dirp2 );
169 (void)closedir( dirp2 );
170 }
171 // sprintf(cmd,"rm -rf %s%s\n",outdir,direntp->d_name);
172 // system(cmd);
173 }
174 }
175 (void)rewinddir( dirp );
176 (void)closedir( dirp );
177
178 if((fp_tmp=fopen(wdir_tmp,"w"))==NULL) {
179 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp);
180 exit(1);
181 }
182
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]))) continue;
187 fprintf(fp_tmp,"%s\t%s\n",direntp->d_name,get_size(outdir,direntp->d_name));
188 continue;
189 } else {
190 if(strstr(direntp->d_name,"-") == 0) continue;
191 bzero(newname, 512);
192 if(strcmp(df,"u") == 0) {
193 strncat(newname,direntp->d_name,4);
194 strncpy(month,direntp->d_name+4,3);
195 } else {
196 strncat(newname,direntp->d_name+5,4);
197 strncpy(month,direntp->d_name+2,3);
198 }
199 month[3]='\0';
200 conv_month(month);
201 strcat(newname,month);
202 if(strcmp(df,"u") == 0) strncat(newname,direntp->d_name+7,2);
203 else strncat(newname,direntp->d_name,2);
204 obtdate(outdir,direntp->d_name,data);
205 obtuser(outdir,direntp->d_name,tuser);
206 obttotal(outdir,direntp->d_name,tbytes,tuser,media);
207 if (sscanf(data,"%d-%d-%d %d:%d:%d %d",&iyear,&imonth,&iday,&ihour,&iminute,&isecond,&idst)==7) {
208 formatdate(data,sizeof(data),iyear,imonth,iday,ihour,iminute,isecond,idst);
209 fprintf(fp_tmp,"%04d%02d%02d%02d%02d%02d;%s;%s;%s;%s;%s;%s\n",iyear,imonth,iday,ihour,iminute,isecond, direntp->d_name, data, tuser, tbytes, media,newname);
210 } else {
211 /*
212 Old code to parse a date stored by sarg before 2.2.6.1 in the sarg-date file of each report directory.
213 */
214 getword_start(&gwarea,data);
215 if (getword_multisep(mon,sizeof(mon),&gwarea,' ')<0) {
216 printf("SARG: Maybe you have a broken week day in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
217 exit(1);
218 }
219 if (getword_multisep(mon,sizeof(mon),&gwarea,' ')<0) {
220 printf("SARG: Maybe you have a broken month in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
221 exit(1);
222 }
223 if (getword_multisep(day,sizeof(day),&gwarea,' ')<0) {
224 printf("SARG: Maybe you have a broken day in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
225 exit(1);
226 }
227 if (getword_multisep(hour,sizeof(hour),&gwarea,' ')<0) {
228 printf("SARG: Maybe you have a broken time in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
229 exit(1);
230 }
231 do {
232 if (getword_multisep(year,sizeof(year),&gwarea,' ')<0) {
233 printf("SARG: Maybe you have a broken year in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
234 exit(1);
235 }
236 } while (year[0] && !isdigit(year[0])); //skip time zone information with spaces until the year is found
237 strcpy(html,hour);
238 if (sscanf(hour,"%d:%d:%d",&ihour,&iminute,&isecond)!=3) {
239 printf("SARG: Maybe you have a broken time in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
240 exit(1);
241 }
242 buildymd(day,mon,year,ftime);
243 fprintf(fp_tmp,"%s%02d%02d%02d;%s;%s;%s;%s;%s;%s\n",ftime, ihour, iminute, isecond, direntp->d_name, data, tuser, tbytes, media,newname);
244 }
245 continue;
246 }
247 }
248
249 if(fp_tmp) fclose(fp_tmp);
250 if(strcmp(IndexTree,"file") == 0) {
251 (void)rewinddir( dirp );
252 (void)closedir( dirp );
253 }
254
255 if(strcmp(IndexTree,"date") == 0) {
256 if(strcmp(IndexSortOrder,"A") == 0) sprintf(warea,"sort -k 1,1 \"%s\" -o \"%s\"", wdir_tmp, wdir_tmp2);
257 else sprintf(warea,"sort -r -k 1,1 \"%s\" -o \"%s\"", wdir_tmp, wdir_tmp2);
258 cstatus=system(warea);
259 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
260 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
261 fprintf(stderr, "SARG: sort command: %s\n",warea);
262 exit(1);
263 }
264 if((fp_tmp=fopen(wdir_tmp2,"r"))==NULL) {
265 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp2);
266 fprintf(stderr, "SARG: sort command: %s\n",warea);
267 exit(1);
268 }
269 unlink(wdir_tmp);
270 if((fp_ou=fopen(wdir,"w"))==NULL) {
271 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
272 exit(1);
273 }
274 write_html_header(fp_ou, ".");
275 fprintf(fp_ou,"<tr><th %s>%s</th><th %s>%s</th></tr>\n",hbc1,text[130],hbc1,text[132]);
276 while(fgets(wwork1,sizeof(wwork1),fp_tmp)!=NULL) {
277 getword_start(&gwarea,wwork1);
278 if (getword(tmp4,sizeof(tmp4),&gwarea,'\t')<0) {
279 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
280 exit(1);
281 }
282 fprintf(fp_ou,"<tr><td class=\"data2\"><a href=\"%s\">%s</a></td><td class=\"data2\">%s</td></tr>\n",tmp4,tmp4,gwarea.current);
283 sprintf(tmp2,"%s%s",outdir,tmp4);
284 sprintf(tmp3,"%s%s/index.unsort",outdir,tmp4);
285 // Year dir
286 if((fp_ou2=fopen(tmp3,"w"))==NULL) {
287 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp3);
288 exit(1);
289 }
290 dirp2 = opendir(tmp2);
291 while ((direntp2 = readdir( dirp2 )) != NULL) {
292 if(!isdigit(direntp2->d_name[0]) && !isdigit(direntp2->d_name[1])) continue;
293 fprintf(fp_ou2,"%s\n",direntp2->d_name);
294 }
295 if(fp_ou2) fclose(fp_ou2);
296 (void)rewinddir(dirp2);
297 (void)closedir(dirp2);
298 sprintf(wdir_tmp3,"%s%s/index.sort",outdir,tmp4);
299 if(strcmp(IndexSortOrder,"A") == 0) sprintf(csort,"sort -n \"%s\" -o \"%s\"", tmp3, wdir_tmp3);
300 else sprintf(csort,"sort -n -r \"%s\" -o \"%s\"", tmp3, wdir_tmp3);
301 cstatus=system(csort);
302 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
303 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
304 fprintf(stderr, "SARG: sort command: %s\n",csort);
305 exit(1);
306 }
307 unlink(tmp3);
308 if((fp_tmp2=fopen(wdir_tmp3,"r"))==NULL) {
309 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp3);
310 exit(1);
311 }
312 sprintf(tmp3,"%s%s/index.html",outdir,tmp4);
313 if((fp_ou2=fopen(tmp3,"w"))==NULL) {
314 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
315 exit(1);
316 }
317 write_html_header(fp_ou2,"..");
318 fprintf(fp_ou2,"<tr><th %s>%s/%s</th></tr>\n",hbc1,text[130],text[131]);
319 while(fgets(wwork1,sizeof(wwork1),fp_tmp2)!=NULL) {
320 fixendofline(wwork1);
321 strcpy(tmp5,wwork1);
322 if((str = strchr(tmp5,'-')) != 0) {
323 *str++ = '\0';
324 strcpy(tmp6,str);
325 name_month(tmp5,sizeof(tmp5));
326 name_month(tmp6,sizeof(tmp6));
327 sprintf(nmonth,"%s-%s",tmp5,tmp6);
328 } else {
329 strcpy(nmonth,tmp5);
330 name_month(nmonth,sizeof(nmonth));
331 }
332 fprintf(fp_ou2,"<tr><td class=\"data2\"><a href=\"%s\">%s %s</a></td></tr>\n",wwork1,tmp4,nmonth);
333
334 sprintf(val1,"%s%s/%s",outdir,tmp4,wwork1);
335 sprintf(tmp5,"%s%s/%s/index.unsort",outdir,tmp4,wwork1);
336 if((fp_ou3=fopen(tmp5,"w"))==NULL) {
337 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp5);
338 exit(1);
339 }
340 // month dir
341 dirp3 = opendir(val1);
342 while ((direntp3 = readdir( dirp3 )) != NULL) {
343 if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue;
344 fprintf(fp_ou3,"%s\n",direntp3->d_name);
345 }
346 if(fp_ou3) fclose(fp_ou3);
347 (void)rewinddir(dirp3);
348 (void)closedir(dirp3);
349 unlink(wdir_tmp3);
350 sprintf(tmp6,"%s%s/%s/index.sort",outdir,tmp4,wwork1);
351 if(strcmp(IndexSortOrder,"A") == 0) sprintf(csort,"sort -n \"%s\" -o \"%s\"", tmp5, tmp6);
352 else sprintf(csort,"sort -n -r \"%s\" -o \"%s\"", tmp5, tmp6);
353 cstatus=system(csort);
354 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
355 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
356 fprintf(stderr, "SARG: sort command: %s\n",csort);
357 exit(1);
358 }
359 unlink(tmp5);
360 sprintf(val2,"%s%s/%s/index.html",outdir,tmp4,wwork1);
361 sprintf(val3,"%s/%s",tmp4,wwork1);
362 unlink(val2);
363 if((fp_ou3=fopen(val2,"w"))==NULL) {
364 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],val2);
365 exit(1);
366 }
367 if((fp_tmp3=fopen(tmp6,"r"))==NULL) {
368 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp6);
369 exit(1);
370 }
371 write_html_header(fp_ou3,"../..");
372 fprintf(fp_ou3,"<tr><th %s>%s/%s/%s</th></tr>\n",hbc1,text[130],text[131],text[127]);
373 while(fgets(warea,MAXLEN,fp_tmp3)!=NULL) {
374 warea[strlen(warea)-1]='\0';
375 fprintf(fp_ou3,"<tr><td class=\"data2\"><a href=\"%s\">%s %s %s</a></td></tr>\n",warea,tmp4,nmonth,warea);
376 }
377 if(fp_tmp3) fclose(fp_tmp3);
378 write_html_trailer(fp_ou3);
379 if(fp_ou3) fclose(fp_ou3);
380 unlink(tmp6);
381 }
382 write_html_trailer(fp_ou2);
383 if(fp_ou2) fclose(fp_ou2);
384 }
385 (void)rewinddir(dirp);
386 (void)closedir(dirp);
387 if(fp_tmp) fclose(fp_tmp);
388 unlink(tmp6);
389 unlink(wdir_tmp2);
390 } else {
391 if(strcmp(IndexSortOrder,"A") == 0) sprintf(warea,"sort -t\";\" -k 7,7 -k 1,1 \"%s\" -o \"%s\"", wdir_tmp, wdir_tmp2);
392 else sprintf(warea,"sort -r -t\";\" -k 7,7 -k 1,1 \"%s\" -o \"%s\"", wdir_tmp, wdir_tmp2);
393 cstatus=system(warea);
394 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
395 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
396 fprintf(stderr, "SARG: sort command: %s\n",warea);
397 exit(1);
398 }
399 if((fp_tmp2=fopen(wdir_tmp2,"r"))==NULL) {
400 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp2);
401 fprintf(stderr, "SARG: sort command: %s\n",warea);
402 exit(1);
403 }
404 unlink(wdir_tmp);
405 if((fp_ou=fopen(wdir,"w"))==NULL) {
406 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
407 exit(1);
408 }
409 write_html_header(fp_ou,".");
410 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]);
411 while(fgets(buf,sizeof(buf),fp_tmp2)!=NULL) {
412 getword_start(&gwarea,buf);
413 if (getword_multisep(period,sizeof(period),&gwarea,';')<0) {
414 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
415 exit(1);
416 }
417 if (getword_multisep(period,sizeof(period),&gwarea,';')<0) {
418 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
419 exit(1);
420 }
421 if (getword_multisep(data,sizeof(data),&gwarea,';')<0) {
422 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
423 exit(1);
424 }
425 if (getword_multisep(tuser,sizeof(tuser),&gwarea,';')<0) {
426 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
427 exit(1);
428 }
429 if (getword_multisep(tbytes,sizeof(tbytes),&gwarea,';')<0) {
430 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
431 exit(1);
432 }
433 if (getword_multisep(media,sizeof(media),&gwarea,';')<0) {
434 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
435 exit(1);
436 }
437 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);
438 }
439 if(fp_tmp2) fclose(fp_tmp2);
440 unlink(wdir_tmp2);
441 }
442
443 write_html_trailer(fp_ou);
444 if(fp_ou) fclose(fp_ou);
445 }