]> git.ipfire.org Git - thirdparty/sarg.git/blame - index.c
Write HTML trailer before closing the file.
[thirdparty/sarg.git] / index.c
CommitLineData
25697a35 1/*
c37945ed
FM
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
94ff9470 4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
25697a35
GS
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
491b862f 28void make_index()
25697a35
GS
29{
30
491b862f
GS
31 FILE *fp_ou, *fp_ou2, *fp_ou3, *fp_tmp, *fp_tmp2, *fp_tmp3;
32 DIR *dirp, *dirp2, *dirp3;
25697a35 33 struct dirent *direntp;
491b862f
GS
34 struct dirent *direntp2;
35 struct dirent *direntp3;
25697a35
GS
36 char html[MAXLEN];
37 char wdir[MAXLEN];
38 char wdir_tmp[MAXLEN];
39 char wdir_tmp2[MAXLEN];
491b862f 40 char wdir_tmp3[MAXLEN];
25697a35
GS
41 char newname[512];
42 char month[4];
43 char period[80];
44 char data[80];
45 char tuser[20];
46 char tbytes[20];
47 char media[20];
48 char ftime[128];
49 char day[4], mon[4], year[6], hour[10];
50 char h[3], m[3], s[3];
51
d6e703cc 52 if(LastLog[0] != '\0') mklastlog(outdir,debug);
25697a35
GS
53
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\"");
58
59 if(strcmp(Index,"no") == 0) {
d6e703cc 60 if(access(wdir, R_OK) == 0) unlink(wdir);
25697a35
GS
61 return;
62 }
63
d6e703cc 64 if(debug) debuga(text[53]);
25697a35 65
491b862f
GS
66 // Root dir
67 dirp = opendir(outdir);
68 while ((direntp = readdir( dirp )) != NULL) {
69 if(strcmp(IndexTree,"date") == 0) {
d6e703cc 70 if(!isdigit(direntp->d_name[0]) && !isdigit(direntp->d_name[1])) continue;
491b862f
GS
71 if(strlen(direntp->d_name) > 4) {
72 bzero(y1,5);
73 bzero(y2,5);
74 bzero(m1,4);
75 bzero(m2,4);
76 bzero(d1,3);
77 bzero(d2,3);
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);
92 }
93 conv_month(m1);
94 conv_month(m2);
95 }
96 sprintf(val1,"%s%s",outdir,y1);
d6e703cc
FM
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);
491b862f
GS
103 sprintf(val4,"%s%s",outdir,direntp->d_name);
104 rename(val4,val3);
105 sprintf(val5,"%s/images",val2);
106 if(access(val5, R_OK) != 0) {
107 sprintf(val5,"ln -s %simages %s/images",outdir,val2);
108 system(val5);
109 }
110 } else {
d6e703cc 111 if(!isdigit(direntp->d_name[0]) && !isdigit(direntp->d_name[1])) continue;
491b862f
GS
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) {
d6e703cc 117 if(!isdigit(direntp2->d_name[0]) && !isdigit(direntp2->d_name[1])) continue;
491b862f
GS
118 sprintf(val2,"%s/%s",val1,direntp2->d_name);
119 dirp3 = opendir(val2);
120 while ((direntp3 = readdir( dirp3 )) != NULL) {
d6e703cc 121 if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue;
491b862f
GS
122 bzero(newname,512);
123 strcpy(warea,direntp2->d_name);
124 if(strstr(warea,"-") != 0) {
125 getword(m1,warea,'-');
126 strcpy(m2,warea);
127 conv_month_name(m1);
128 conv_month_name(m2);
129 } else {
130 strcpy(m1,warea);
131 conv_month_name(m1);
132 strcpy(m2,m1);
133 }
134 strcpy(warea,direntp3->d_name);
135 if(strstr(warea,"-") != 0) {
136 getword(d1,warea,'-');
137 strcpy(d2,warea);
138 } else {
139 strcpy(d1,warea);
140 strcpy(d2,warea);
141 }
d6e703cc
FM
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);
491b862f
GS
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));
147 exit(1);
148 }
149 }
150 (void)rewinddir( dirp3 );
151 (void)closedir( dirp3 );
152 }
153 (void)rewinddir( dirp2 );
154 (void)closedir( dirp2 );
155 }
156// sprintf(cmd,"rm -rf %s%s\n",outdir,direntp->d_name);
157// system(cmd);
158 }
25697a35 159 }
491b862f
GS
160 (void)rewinddir( dirp );
161 (void)closedir( dirp );
25697a35
GS
162
163 if((fp_tmp=fopen(wdir_tmp,"w"))==NULL) {
164 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp);
165 exit(1);
166 }
167
168 dirp = opendir(outdir);
491b862f
GS
169 while ((direntp = readdir( dirp )) != NULL) {
170 if(strcmp(IndexTree,"date") == 0) {
d6e703cc
FM
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));
25697a35 173 continue;
491b862f 174 } else {
d6e703cc 175 if(strstr(direntp->d_name,"-") == 0) continue;
491b862f
GS
176 bzero(newname, 512);
177 if(strcmp(df,"u") == 0) {
178 strncat(newname,direntp->d_name,4);
179 strncpy(month,direntp->d_name+4,3);
180 } else {
181 strncat(newname,direntp->d_name+5,4);
182 strncpy(month,direntp->d_name+2,3);
183 }
184 month[3]='\0';
185 conv_month(month);
186 strcat(newname,month);
d6e703cc
FM
187 if(strcmp(df,"u") == 0) strncat(newname,direntp->d_name+7,2);
188 else strncat(newname,direntp->d_name,2);
491b862f
GS
189 obtdate(outdir,direntp->d_name,data);
190 obtuser(outdir,direntp->d_name,tuser);
191 obttotal(outdir,direntp->d_name,tbytes,tuser,media);
192 strcpy(html,data);
193 getword(mon,html,' ');
194 getword(mon,html,' ');
195 getword(day,html,' ');
196 getword(hour,html,' ');
197 getword(year,html,' ');
198 getword(year,html,' ');
199 strcpy(html,hour);
200 getword(h,html,':');
201 getword(m,html,':');
202 strcpy(s,html);
203 buildymd(day,mon,year,ftime);
d6e703cc 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);
491b862f
GS
205 continue;
206 }
25697a35 207 }
94ff9470 208
491b862f
GS
209 if(fp_tmp) fclose(fp_tmp);
210 if(strcmp(IndexTree,"file") == 0) {
211 (void)rewinddir( dirp );
212 (void)closedir( dirp );
25697a35
GS
213 }
214
491b862f 215 if(strcmp(IndexTree,"date") == 0) {
d6e703cc
FM
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);
491b862f
GS
218 system(warea);
219 unlink(wdir_tmp);
220 if((fp_tmp=fopen(wdir_tmp2,"r"))==NULL) {
221 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp2);
222 exit(1);
223 }
224 if((fp_ou=fopen(wdir,"w"))==NULL) {
225 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
226 exit(1);
227 }
228 write_html_header(fp_ou, ".");
d6e703cc 229 fprintf(fp_ou,"<tr><th %s>%s</th><th %s>%s</th></tr>\n",hbc1,text[130],hbc1,text[132]);
491b862f
GS
230 while(fgets(wwork1,MAXLEN,fp_tmp)!=NULL) {
231 getword(tmp4,wwork1,' ');
d6e703cc 232 fprintf(fp_ou,"<tr><td class=\"data2\"><a href=\"%s\">%s</a></td><td class=\"data2\">%s</td></tr>\n",tmp4,tmp4,wwork1);
491b862f
GS
233 sprintf(tmp2,"%s%s",outdir,tmp4);
234 sprintf(tmp3,"%s%s/index.unsort",outdir,tmp4);
235 // Year dir
236 if((fp_ou2=fopen(tmp3,"w"))==NULL) {
237 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp3);
238 exit(1);
239 }
240 dirp2 = opendir(tmp2);
241 while ((direntp2 = readdir( dirp2 )) != NULL) {
d6e703cc
FM
242 if(!isdigit(direntp2->d_name[0]) && !isdigit(direntp2->d_name[1])) continue;
243 fprintf(fp_ou2,"%s\n",direntp2->d_name);
491b862f
GS
244 }
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);
d6e703cc
FM
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);
491b862f
GS
251 system(csort);
252 unlink(tmp3);
253 if((fp_tmp2=fopen(wdir_tmp3,"r"))==NULL) {
254 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp3);
255 exit(1);
256 }
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);
260 exit(1);
261 }
262 write_html_header(fp_ou2,"..");
d6e703cc 263 fprintf(fp_ou2,"<tr><th %s>%s/%s</th></tr>\n",hbc1,text[130],text[131]);
491b862f
GS
264 while(fgets(wwork1,MAXLEN,fp_tmp2)!=NULL) {
265 wwork1[strlen(wwork1)-1]='\0';
266 strcpy(tmp5,wwork1);
267 if(strstr(tmp5,"-") != 0) {
268 getword(warea,tmp5,'-');
269 name_month(warea);
270 sprintf(tmp6,"%s-",warea);
271 name_month(tmp5);
272 sprintf(nmonth,"%s%s",tmp6,tmp5);
273 } else {
274 strcpy(nmonth,tmp5);
275 name_month(nmonth);
276 }
d6e703cc 277 fprintf(fp_ou2,"<tr><td class=\"data2\"><a href=\"%s\">%s %s</a></td></tr>\n",wwork1,tmp4,nmonth);
491b862f
GS
278
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);
283 exit(1);
284 }
285 // month dir
286 dirp3 = opendir(val1);
287 while ((direntp3 = readdir( dirp3 )) != NULL) {
d6e703cc
FM
288 if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue;
289 fprintf(fp_ou3,"%s\n",direntp3->d_name);
491b862f
GS
290 }
291 if(fp_ou3) fclose(fp_ou3);
292 (void)rewinddir(dirp3);
293 (void)closedir(dirp3);
294 unlink(wdir_tmp3);
295 sprintf(tmp6,"%s%s/%s/index.sort",outdir,tmp4,wwork1);
d6e703cc
FM
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);
491b862f
GS
298 system(csort);
299 unlink(tmp5);
300 sprintf(val2,"%s%s/%s/index.html",outdir,tmp4,wwork1);
301 sprintf(val3,"%s/%s",tmp4,wwork1);
302 unlink(val2);
303 if((fp_ou3=fopen(val2,"w"))==NULL) {
304 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],val2);
305 exit(1);
306 }
307 if((fp_tmp3=fopen(tmp6,"r"))==NULL) {
308 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp6);
309 exit(1);
310 }
311 write_html_header(fp_ou3,"../..");
d6e703cc 312 fprintf(fp_ou3,"<tr><th %s>%s/%s/%s</th></tr>\n",hbc1,text[130],text[131],text[127]);
491b862f
GS
313 while(fgets(warea,MAXLEN,fp_tmp3)!=NULL) {
314 warea[strlen(warea)-1]='\0';
d6e703cc 315 fprintf(fp_ou3,"<tr><td class=\"data2\"><a href=\"%s\">%s %s %s</a></td></tr>\n",warea,tmp4,nmonth,warea);
491b862f
GS
316 }
317 if(fp_tmp3) fclose(fp_tmp3);
491b862f 318 write_html_trailer(fp_ou3);
121c2a89 319 if(fp_ou3) fclose(fp_ou3);
491b862f
GS
320 unlink(tmp6);
321 }
322 write_html_trailer(fp_ou2);
323 if(fp_ou2) fclose(fp_ou2);
324 }
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);
330 unlink(tmp6);
331 unlink(wdir_tmp2);
332 } else {
d6e703cc
FM
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);
491b862f
GS
335 system(warea);
336 unlink(wdir_tmp);
337 if((fp_ou=fopen(wdir,"w"))==NULL) {
338 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
339 exit(1);
340 }
341 write_html_header(fp_ou,".");
d6e703cc 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]);
491b862f
GS
343 if((fp_tmp2=fopen(wdir_tmp2,"r"))==NULL) {
344 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp2);
345 exit(1);
346 }
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,';');
d6e703cc 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);
491b862f
GS
355 }
356 if(fp_tmp2) fclose(fp_tmp2);
357 unlink(wdir_tmp2);
25697a35 358 }
491b862f 359
d6e703cc 360 fputs("</table></center>",fp_ou);
25697a35
GS
361
362 zdate(ftime, DateFormat);
363
364 show_info(fp_ou);
365
d6e703cc 366 fputs("</body>\n</html>\n",fp_ou);
25697a35 367}