]> git.ipfire.org Git - thirdparty/sarg.git/blob - index.c
Changed Pedro's address in headers.
[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
28 void make_index()
29 {
30
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;
36 char html[MAXLEN];
37 char wdir[MAXLEN];
38 char wdir_tmp[MAXLEN];
39 char wdir_tmp2[MAXLEN];
40 char wdir_tmp3[MAXLEN];
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
52 if(LastLog[0] != '\0')
53 mklastlog(outdir,debug);
54
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\"");
59
60 if(strcmp(Index,"no") == 0) {
61 if(access(wdir, R_OK) == 0)
62 unlink(wdir);
63 return;
64 }
65
66 if(debug)
67 debuga(text[53]);
68
69 // Root dir
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]))
74 continue;
75 if(strlen(direntp->d_name) > 4) {
76 bzero(y1,5);
77 bzero(y2,5);
78 bzero(m1,4);
79 bzero(m2,4);
80 bzero(d1,3);
81 bzero(d2,3);
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);
96 }
97 conv_month(m1);
98 conv_month(m2);
99 }
100 sprintf(val1,"%s%s",outdir,y1);
101 if(access(val1, R_OK) != 0)
102 mkdir(val1,0755);
103 if(strcmp(m1,m2) != 0)
104 sprintf(val2,"%s/%s-%s",val1,m1,m2);
105 else
106 sprintf(val2,"%s/%s",val1,m1);
107 if(access(val2, R_OK) != 0)
108 mkdir(val2,0755);
109 if(strcmp(d1,d2) != 0)
110 sprintf(val3,"%s/%s-%s",val2,d1,d2);
111 else
112 sprintf(val3,"%s/%s",val2,d1);
113 sprintf(val4,"%s%s",outdir,direntp->d_name);
114 rename(val4,val3);
115 sprintf(val5,"%s/images",val2);
116 if(access(val5, R_OK) != 0) {
117 sprintf(val5,"ln -s %simages %s/images",outdir,val2);
118 system(val5);
119 }
120 } else {
121 if(!isdigit(direntp->d_name[0]) && !isdigit(direntp->d_name[1]))
122 continue;
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]))
129 continue;
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]))
134 continue;
135 bzero(newname,512);
136 strcpy(warea,direntp2->d_name);
137 if(strstr(warea,"-") != 0) {
138 getword(m1,warea,'-');
139 strcpy(m2,warea);
140 conv_month_name(m1);
141 conv_month_name(m2);
142 } else {
143 strcpy(m1,warea);
144 conv_month_name(m1);
145 strcpy(m2,m1);
146 }
147 strcpy(warea,direntp3->d_name);
148 if(strstr(warea,"-") != 0) {
149 getword(d1,warea,'-');
150 strcpy(d2,warea);
151 } else {
152 strcpy(d1,warea);
153 strcpy(d2,warea);
154 }
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));
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]))
187 continue;
188 sprintf(warea,"%s %s\n",direntp->d_name,get_size(outdir,direntp->d_name));
189 fputs(warea, fp_tmp);
190 continue;
191 } else {
192 if(strstr(direntp->d_name,"-") == 0)
193 continue;
194 bzero(newname, 512);
195 if(strcmp(df,"u") == 0) {
196 strncat(newname,direntp->d_name,4);
197 strncpy(month,direntp->d_name+4,3);
198 } else {
199 strncat(newname,direntp->d_name+5,4);
200 strncpy(month,direntp->d_name+2,3);
201 }
202 month[3]='\0';
203 conv_month(month);
204 strcat(newname,month);
205 if(strcmp(df,"u") == 0)
206 strncat(newname,direntp->d_name+7,2);
207 else
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);
212 strcpy(html,data);
213 getword(mon,html,' ');
214 getword(mon,html,' ');
215 getword(day,html,' ');
216 getword(hour,html,' ');
217 getword(year,html,' ');
218 getword(year,html,' ');
219 strcpy(html,hour);
220 getword(h,html,':');
221 getword(m,html,':');
222 strcpy(s,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);
226 continue;
227 }
228 }
229
230 if(fp_tmp) fclose(fp_tmp);
231 if(strcmp(IndexTree,"file") == 0) {
232 (void)rewinddir( dirp );
233 (void)closedir( dirp );
234 }
235
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);
239 else
240 sprintf(warea,"sort -r -k 1,1 '%s' -o '%s'", wdir_tmp, wdir_tmp2);
241 system(warea);
242 unlink(wdir_tmp);
243 if((fp_tmp=fopen(wdir_tmp2,"r"))==NULL) {
244 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp2);
245 exit(1);
246 }
247 if((fp_ou=fopen(wdir,"w"))==NULL) {
248 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
249 exit(1);
250 }
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]);
253 fputs(html,fp_ou);
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);
257 fputs(html,fp_ou);
258 sprintf(tmp2,"%s%s",outdir,tmp4);
259 sprintf(tmp3,"%s%s/index.unsort",outdir,tmp4);
260 // Year dir
261 if((fp_ou2=fopen(tmp3,"w"))==NULL) {
262 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp3);
263 exit(1);
264 }
265 dirp2 = opendir(tmp2);
266 while ((direntp2 = readdir( dirp2 )) != NULL) {
267 if(!isdigit(direntp2->d_name[0]) && !isdigit(direntp2->d_name[1]))
268 continue;
269 sprintf(warea,"%s\n",direntp2->d_name);
270 fputs(warea,fp_ou2);
271 }
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);
278 else
279 sprintf(csort,"sort -n -r '%s' -o '%s'", tmp3, wdir_tmp3);
280 system(csort);
281 unlink(tmp3);
282 if((fp_tmp2=fopen(wdir_tmp3,"r"))==NULL) {
283 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp3);
284 exit(1);
285 }
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);
289 exit(1);
290 }
291 write_html_header(fp_ou2,"..");
292 sprintf(html,"<tr><th %s>%s/%s</th></tr>\n",hbc1,text[130],text[131]);
293 fputs(html,fp_ou2);
294 while(fgets(wwork1,MAXLEN,fp_tmp2)!=NULL) {
295 wwork1[strlen(wwork1)-1]='\0';
296 strcpy(tmp5,wwork1);
297 if(strstr(tmp5,"-") != 0) {
298 getword(warea,tmp5,'-');
299 name_month(warea);
300 sprintf(tmp6,"%s-",warea);
301 name_month(tmp5);
302 sprintf(nmonth,"%s%s",tmp6,tmp5);
303 } else {
304 strcpy(nmonth,tmp5);
305 name_month(nmonth);
306 }
307 sprintf(html,"<tr><td class=\"data2\"><a href=\"%s\">%s %s</a></td></tr>\n",wwork1,tmp4,nmonth);
308 fputs(html,fp_ou2);
309
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);
314 exit(1);
315 }
316 // month dir
317 dirp3 = opendir(val1);
318 while ((direntp3 = readdir( dirp3 )) != NULL) {
319 if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1]))
320 continue;
321 sprintf(warea,"%s\n",direntp3->d_name);
322 fputs(warea,fp_ou3);
323
324 }
325 if(fp_ou3) fclose(fp_ou3);
326 (void)rewinddir(dirp3);
327 (void)closedir(dirp3);
328 unlink(wdir_tmp3);
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);
332 else
333 sprintf(csort,"sort -n -r '%s' -o '%s'", tmp5, tmp6);
334 system(csort);
335 unlink(tmp5);
336 sprintf(val2,"%s%s/%s/index.html",outdir,tmp4,wwork1);
337 sprintf(val3,"%s/%s",tmp4,wwork1);
338 unlink(val2);
339 if((fp_ou3=fopen(val2,"w"))==NULL) {
340 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],val2);
341 exit(1);
342 }
343 if((fp_tmp3=fopen(tmp6,"r"))==NULL) {
344 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp6);
345 exit(1);
346 }
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]);
349 fputs(html,fp_ou3);
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);
353 fputs(html,fp_ou3);
354 }
355 if(fp_tmp3) fclose(fp_tmp3);
356 if(fp_ou3) fclose(fp_ou3);
357 write_html_trailer(fp_ou3);
358 unlink(tmp6);
359 }
360 write_html_trailer(fp_ou2);
361 if(fp_ou2) fclose(fp_ou2);
362 }
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);
368 unlink(tmp6);
369 unlink(wdir_tmp2);
370 } else {
371 if(strcmp(IndexSortOrder,"A") == 0)
372 sprintf(warea,"sort -t';' +6.5n +6.2M +6.0n '%s' -o '%s'", wdir_tmp, wdir_tmp2);
373 else
374 sprintf(warea,"sort -t';' +6.5nr +6.2Mr +6.0nr '%s' -o '%s'", wdir_tmp, wdir_tmp2);
375 system(warea);
376 unlink(wdir_tmp);
377 if((fp_ou=fopen(wdir,"w"))==NULL) {
378 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
379 exit(1);
380 }
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]);
383 fputs(html,fp_ou);
384 if((fp_tmp2=fopen(wdir_tmp2,"r"))==NULL) {
385 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp2);
386 exit(1);
387 }
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);
396 fputs(html,fp_ou);
397 }
398 if(fp_tmp2) fclose(fp_tmp2);
399 unlink(wdir_tmp2);
400 }
401
402 strcpy(html,"</table></center>");
403 fputs(html,fp_ou);
404
405 zdate(ftime, DateFormat);
406
407 show_info(fp_ou);
408
409 strcpy(html,"</body>\n</html>\n");
410 fputs(html,fp_ou);
411
412 return;
413 }