]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
1164c474 | 3 | * 1998, 2010 |
25697a35 GS |
4 | * |
5 | * SARG donations: | |
6 | * please look at http://sarg.sourceforge.net/donations.php | |
1164c474 FM |
7 | * Support: |
8 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |
25697a35 GS |
9 | * --------------------------------------------------------------------- |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
24 | * | |
25 | */ | |
26 | ||
27 | #include "include/conf.h" | |
5f3cfd1d | 28 | #include "include/defs.h" |
25697a35 | 29 | |
b25b96fe FM |
30 | static void make_date_index(void); |
31 | static void make_file_index(void); | |
06ced858 FM |
32 | static void file_index_to_date_index(const char *entry); |
33 | static void date_index_to_file_index(const char *entry); | |
34 | ||
32e71fa4 | 35 | void make_index(void) |
25697a35 | 36 | { |
b25b96fe | 37 | DIR *dirp; |
25697a35 | 38 | struct dirent *direntp; |
25697a35 | 39 | char wdir[MAXLEN]; |
25697a35 | 40 | |
0511cf2d | 41 | if(LastLog > 0) mklastlog(outdir); |
25697a35 | 42 | |
0349fa24 | 43 | if(Index == INDEX_NO) { |
b25b96fe | 44 | sprintf(wdir,"%sindex.html",outdir); |
d6e703cc | 45 | if(access(wdir, R_OK) == 0) unlink(wdir); |
25697a35 GS |
46 | return; |
47 | } | |
48 | ||
10210234 | 49 | if(debug) debuga(_("Making index.html\n")); |
25697a35 | 50 | |
06ced858 | 51 | // convert any old report hierarchy |
e21b6c02 FM |
52 | if ((dirp = opendir(outdir)) == NULL) { |
53 | debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno)); | |
54 | exit(EXIT_FAILURE); | |
55 | } | |
491b862f | 56 | while ((direntp = readdir( dirp )) != NULL) { |
06ced858 | 57 | if(isdigit(direntp->d_name[0]) && isdigit(direntp->d_name[1])) { |
0349fa24 | 58 | if(IndexTree == INDEX_TREE_DATE) |
06ced858 FM |
59 | file_index_to_date_index(direntp->d_name); |
60 | else | |
61 | date_index_to_file_index(direntp->d_name); | |
491b862f | 62 | } |
25697a35 | 63 | } |
43f18f45 | 64 | closedir(dirp); |
25697a35 | 65 | |
0349fa24 | 66 | if(IndexTree == INDEX_TREE_DATE) { |
b25b96fe FM |
67 | make_date_index(); |
68 | } else { | |
69 | make_file_index(); | |
25697a35 | 70 | } |
b25b96fe | 71 | } |
25697a35 | 72 | |
b25b96fe FM |
73 | static void make_date_index(void) |
74 | { | |
75 | FILE *fp_ou, *fp_ou2, *fp_ou3; | |
76 | DIR *dirp, *dirp2, *dirp3; | |
77 | struct dirent *direntp; | |
78 | struct dirent *direntp2; | |
79 | struct dirent *direntp3; | |
b25b96fe FM |
80 | char yearindex[MAXLEN]; |
81 | char yeardir[MAXLEN]; | |
82 | char yearnum[10]; | |
83 | char monthindex[MAXLEN]; | |
84 | char monthdir[MAXLEN]; | |
85 | char monthname1[9], monthname2[9]; | |
86 | char nmonth[30]; | |
87 | char monthnum[10]; | |
88 | char dayindex[MAXLEN]; | |
89 | char daynum[10]; | |
c0ec9cc7 | 90 | char title[80]; |
b25b96fe FM |
91 | int yearsort[150]; |
92 | int nyears; | |
93 | int year; | |
94 | int monthsort[144]; | |
95 | int nmonths; | |
96 | int m1, m2, month; | |
97 | int daysort[31*31]; | |
98 | int ndays; | |
99 | int d1, d2, day; | |
100 | int i, y, m, d; | |
101 | int order; | |
102 | ||
103 | sprintf(yearindex,"%sindex.html",outdir); | |
b25b96fe FM |
104 | |
105 | nyears=0; | |
e21b6c02 FM |
106 | if ((dirp = opendir(outdir)) == NULL) { |
107 | debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno)); | |
108 | exit(EXIT_FAILURE); | |
109 | } | |
491b862f | 110 | while ((direntp = readdir( dirp )) != NULL) { |
b25b96fe FM |
111 | if(strlen(direntp->d_name) > 4 || !isdigit(direntp->d_name[0]) || !isdigit(direntp->d_name[1]) || |
112 | !isdigit(direntp->d_name[2]) || !isdigit(direntp->d_name[3])) continue; | |
113 | year=atoi(direntp->d_name); | |
114 | if (nyears>=sizeof(yearsort)/sizeof(yearsort[0])) { | |
115 | /* | |
116 | If too many years are listed in the directory, we ignore the earliest years. The yearsort array | |
117 | is big enough to accomodate the most ambitious use of sarg but this safety is added to prevent | |
118 | a crash should the directory be polluted by other entries. | |
119 | */ | |
120 | if (year>yearsort[0]) { | |
121 | for (i=1 ; i<nyears && year>yearsort[i] ; i++) | |
122 | yearsort[i-1]=yearsort[i]; | |
123 | yearsort[i-1]=year; | |
491b862f | 124 | } |
b25b96fe FM |
125 | } else { |
126 | for (i=nyears ; i>0 && year<yearsort[i-1] ; i--) { | |
127 | yearsort[i]=yearsort[i-1]; | |
4bcb77cf | 128 | } |
b25b96fe FM |
129 | yearsort[i]=year; |
130 | nyears++; | |
491b862f | 131 | } |
25697a35 | 132 | } |
06b39c87 | 133 | closedir( dirp ); |
25697a35 | 134 | |
b25b96fe FM |
135 | order=(strcmp(IndexSortOrder,"A") == 0) ? 1 : -1; |
136 | ||
137 | if((fp_ou=fopen(yearindex,"w"))==NULL) { | |
fcdc0918 | 138 | debuga(_("(index) Cannot open file %s - %s\n"),yearindex,strerror(errno)); |
06b39c87 | 139 | exit(EXIT_FAILURE); |
b25b96fe | 140 | } |
fcdc0918 | 141 | write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nyears)); |
c0ec9cc7 FM |
142 | close_html_header(fp_ou); |
143 | fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",fp_ou); | |
c36c7384 | 144 | fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("YEAR"),_("SIZE")); |
b25b96fe FM |
145 | for (y=0 ; y<nyears ; y++) { |
146 | if (order>0) | |
147 | year=yearsort[y]; | |
148 | else | |
149 | year=yearsort[nyears-1-y]; | |
150 | sprintf(yearnum,"%04d",year); | |
151 | fprintf(fp_ou,"<tr><td class=\"data2\"><a href=\"%s/index.html\">%s</a></td><td class=\"data2\">%s</td></tr>\n",yearnum,yearnum,get_size(outdir,yearnum)); | |
152 | sprintf(yeardir,"%s%s",outdir,yearnum); | |
153 | // Year dir | |
154 | nmonths=0; | |
e21b6c02 FM |
155 | if ((dirp2 = opendir(yeardir)) == NULL) { |
156 | debuga(_("Failed to open directory %s - %s\n"),yeardir,strerror(errno)); | |
157 | exit(EXIT_FAILURE); | |
158 | } | |
b25b96fe FM |
159 | while ((direntp2 = readdir( dirp2 )) != NULL) { |
160 | if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue; | |
161 | i=-1; | |
162 | if (sscanf(direntp2->d_name,"%d%n",&m1,&i)!=1 || m1<=0 || m1>12 || i<0) continue; | |
163 | if (direntp2->d_name[i]=='-') { | |
164 | if (sscanf(direntp2->d_name+i+1,"%d",&m2)!=1 || m2<m1 || m2>12) continue; | |
165 | } else if (direntp2->d_name[i]!='\0') { | |
166 | continue; | |
167 | } else { | |
168 | m2=0; | |
491b862f | 169 | } |
b25b96fe | 170 | if (nmonths>=sizeof(monthsort)/sizeof(monthsort[0])) { |
fcdc0918 | 171 | debuga(_("Too many month directories in %s\nSupernumerary entries are ignored\n"),yeardir); |
b25b96fe | 172 | break; |
456d78a5 | 173 | } |
b25b96fe FM |
174 | month=m1*16+m2; |
175 | for (i=nmonths ; i>0 && month<monthsort[i-1] ; i--) { | |
176 | monthsort[i]=monthsort[i-1]; | |
491b862f | 177 | } |
b25b96fe FM |
178 | monthsort[i]=month; |
179 | nmonths++; | |
180 | } | |
06b39c87 | 181 | closedir(dirp2); |
b25b96fe FM |
182 | sprintf(monthindex,"%s/index.html",yeardir); |
183 | if((fp_ou2=fopen(monthindex,"w"))==NULL) { | |
fcdc0918 | 184 | debuga(_("(index) Cannot open file %s - %s\n"),monthindex,strerror(errno)); |
06b39c87 | 185 | exit(EXIT_FAILURE); |
b25b96fe | 186 | } |
fcdc0918 | 187 | snprintf(title,sizeof(title),ngettext("SARG: report for %04d","SARG: reports for %04d",nmonths),year); |
c0ec9cc7 FM |
188 | write_html_header(fp_ou2,1,title); |
189 | close_html_header(fp_ou2); | |
190 | fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",fp_ou2); | |
c36c7384 | 191 | fprintf(fp_ou2,"<tr><th class=\"header_l\">%s/%s</th></tr>\n",_("YEAR"),_("MONTH")); |
b25b96fe FM |
192 | for (m=0 ; m<nmonths ; m++) { |
193 | if (order>0) | |
194 | month=monthsort[m]; | |
195 | else | |
196 | month=monthsort[nmonths-1-m]; | |
197 | m1=month / 16; | |
198 | if(month % 16 != 0) { | |
199 | m2=month % 16; | |
200 | sprintf(monthnum,"%02d-%02d",m1,m2); | |
201 | sprintf(monthname1,"%02d",m1); | |
202 | sprintf(monthname2,"%02d",m2); | |
203 | name_month(monthname1,sizeof(monthname1)); | |
204 | name_month(monthname2,sizeof(monthname2)); | |
205 | sprintf(nmonth,"%s-%s",monthname1,monthname2); | |
206 | } else { | |
207 | sprintf(nmonth,"%02d",m1); | |
208 | sprintf(monthnum,"%02d",m1); | |
209 | name_month(nmonth,sizeof(nmonth)); | |
491b862f | 210 | } |
b25b96fe | 211 | fprintf(fp_ou2,"<tr><td class=\"data2\"><a href=\"%s/index.html\">%s %s</a></td></tr>\n",monthnum,yearnum,nmonth); |
4157aa09 | 212 | |
b25b96fe FM |
213 | sprintf(monthdir,"%s/%s",yeardir,monthnum); |
214 | // month dir | |
215 | ndays=0; | |
e21b6c02 FM |
216 | if ((dirp3 = opendir(monthdir)) == NULL) { |
217 | debuga(_("Failed to open directory %s - %s\n"),monthdir,strerror(errno)); | |
218 | exit(EXIT_FAILURE); | |
219 | } | |
b25b96fe FM |
220 | while ((direntp3 = readdir( dirp3 )) != NULL) { |
221 | if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue; | |
222 | i=-1; | |
223 | if (sscanf(direntp3->d_name,"%d%n",&d1,&i)!=1 || d1<=0 || d1>31 || i<0) continue; | |
224 | if (direntp3->d_name[i]=='-') { | |
225 | if (sscanf(direntp3->d_name+i+1,"%d",&d2)!=1 || d2<d1 || d2>31) continue; | |
226 | } else if (direntp3->d_name[i]!='\0') { | |
227 | continue; | |
228 | } else { | |
229 | d2=0; | |
491b862f | 230 | } |
b25b96fe | 231 | if (ndays>=sizeof(daysort)/sizeof(daysort[0])) { |
fcdc0918 | 232 | debuga(_("Too many day directories in %s\nSupernumerary entries are ignored\n"),monthdir); |
b25b96fe | 233 | break; |
456d78a5 | 234 | } |
b25b96fe FM |
235 | day=d1*32+d2; |
236 | for (i=ndays ; i>0 && day<daysort[i-1] ; i--) { | |
237 | daysort[i]=daysort[i-1]; | |
491b862f | 238 | } |
b25b96fe FM |
239 | daysort[i]=day; |
240 | ndays++; | |
241 | } | |
06b39c87 | 242 | closedir(dirp3); |
b25b96fe FM |
243 | sprintf(dayindex,"%s/index.html",monthdir); |
244 | if((fp_ou3=fopen(dayindex,"w"))==NULL) { | |
fcdc0918 | 245 | debuga(_("(index) Cannot open file %s - %s\n"),dayindex,strerror(errno)); |
06b39c87 | 246 | exit(EXIT_FAILURE); |
b25b96fe | 247 | } |
fcdc0918 | 248 | snprintf(title,sizeof(title),ngettext("SARG: report for %04d/%02d","SARG: reports for %04d/%02d",ndays),year,month); |
c0ec9cc7 FM |
249 | write_html_header(fp_ou3,2,title); |
250 | close_html_header(fp_ou3); | |
251 | fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",fp_ou3); | |
c36c7384 | 252 | fprintf(fp_ou3,"<tr><th class=\"header_l\">%s/%s/%s</th></tr>\n",_("YEAR"),_("MONTH"),_("DAYS")); |
b25b96fe FM |
253 | for (d=0 ; d<ndays ; d++) { |
254 | if (order>0) | |
255 | day=daysort[d]; | |
256 | else | |
257 | day=daysort[ndays-1-d]; | |
258 | d1=day / 32; | |
259 | if(day % 32 != 0) { | |
260 | d2=day % 32; | |
261 | sprintf(daynum,"%02d-%02d",d1,d2); | |
262 | } else { | |
263 | sprintf(daynum,"%02d",d1); | |
491b862f | 264 | } |
b25b96fe | 265 | fprintf(fp_ou3,"<tr><td class=\"data2\"><a href=\"%s/index.html\">%s %s %s</a></td></tr>\n",daynum,yearnum,nmonth,daynum); |
491b862f | 266 | } |
c0ec9cc7 | 267 | fputs("</table></div>\n",fp_ou3); |
fa6552b0 FM |
268 | if (write_html_trailer(fp_ou3)<0) |
269 | debuga(_("Write error in the index %s\n"),dayindex); | |
270 | if (fclose(fp_ou3)==EOF) | |
271 | debuga(_("Failed to close the index file %s - %s\n"),dayindex,strerror(errno)); | |
456d78a5 | 272 | } |
c0ec9cc7 | 273 | fputs("</table></div>\n",fp_ou2); |
fa6552b0 FM |
274 | if (write_html_trailer(fp_ou2)<0) |
275 | debuga(_("Write error in the index %s\n"),monthindex); | |
276 | if (fclose(fp_ou2)==EOF) | |
277 | debuga(_("Failed to close the index file %s - %s\n"),monthindex,strerror(errno)); | |
b25b96fe FM |
278 | } |
279 | ||
c0ec9cc7 | 280 | fputs("</table></div>\n",fp_ou); |
fa6552b0 FM |
281 | if (write_html_trailer(fp_ou)<0) |
282 | debuga(_("Write error in the index %s\n"),yearindex); | |
283 | if (fclose(fp_ou)==EOF) | |
284 | debuga(_("Failed to close the index file %s - %s\n"),yearindex,strerror(errno)); | |
b25b96fe FM |
285 | } |
286 | ||
287 | static void make_file_index(void) | |
288 | { | |
289 | #define MAX_CREATION_DATE 15 | |
b25b96fe FM |
290 | FILE *fp_ou; |
291 | DIR *dirp; | |
292 | struct dirent *direntp; | |
293 | char wdir[MAXLEN]; | |
b25b96fe | 294 | char data[80]; |
b25b96fe FM |
295 | char tbytes[20]; |
296 | char media[20]; | |
297 | char ftime[128]; | |
298 | char day[6], mon[8], year[40], hour[10]; | |
299 | int iyear, imonth, iday, ihour, iminute, isecond, idst; | |
300 | int nsort; | |
301 | int nallocated; | |
302 | int order; | |
303 | int i; | |
d25d4e6a | 304 | int tuser; |
b25b96fe FM |
305 | struct getwordstruct gwarea; |
306 | struct sortstruct | |
307 | { | |
fa6552b0 | 308 | int year, month, day, sortnum; |
b25b96fe | 309 | char creationdate[MAX_CREATION_DATE]; |
5e6e0640 | 310 | char *dirname; |
b25b96fe FM |
311 | char date[60]; |
312 | } **sortlist, *item, **tempsort; | |
313 | ||
314 | sprintf(wdir,"%sindex.html",outdir); | |
b25b96fe FM |
315 | |
316 | order=(strcmp(IndexSortOrder,"A") == 0) ? 1 : -1; | |
317 | ||
e21b6c02 FM |
318 | if ((dirp = opendir(outdir)) == NULL) { |
319 | debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno)); | |
320 | exit(EXIT_FAILURE); | |
321 | } | |
b25b96fe FM |
322 | |
323 | nsort=0; | |
324 | nallocated=0; | |
325 | sortlist=NULL; | |
326 | while ((direntp = readdir( dirp )) != NULL) { | |
327 | if (strchr(direntp->d_name,'-') == 0) continue; | |
b25b96fe FM |
328 | item=malloc(sizeof(*item)); |
329 | if (!item) { | |
fcdc0918 | 330 | debuga(_("not enough memory to sort the index\n")); |
06b39c87 | 331 | exit(EXIT_FAILURE); |
456d78a5 | 332 | } |
b25b96fe | 333 | if(strcmp(df,"u") == 0) { |
fa6552b0 FM |
334 | item->year=atoi(direntp->d_name); |
335 | item->month=conv_month(direntp->d_name+4); | |
336 | item->day=atoi(direntp->d_name+7); | |
b25b96fe | 337 | } else { |
fa6552b0 FM |
338 | item->year=atoi(direntp->d_name+5); |
339 | item->month=conv_month(direntp->d_name+2); | |
340 | item->day=atoi(direntp->d_name); | |
491b862f | 341 | } |
fa6552b0 | 342 | item->sortnum=(item->year*16+item->month)*32+item->day; |
b25b96fe FM |
343 | obtdate(outdir,direntp->d_name,data); |
344 | if (sscanf(data,"%d-%d-%d %d:%d:%d %d",&iyear,&imonth,&iday,&ihour,&iminute,&isecond,&idst)==7) { | |
345 | formatdate(data,sizeof(data),iyear,imonth,iday,ihour,iminute,isecond,idst); | |
346 | snprintf(item->creationdate,sizeof(item->creationdate),"%04d%02d%02d%02d%02d%02d",iyear,imonth,iday,ihour,iminute,isecond); | |
347 | } else { | |
348 | /* | |
349 | Old code to parse a date stored by sarg before 2.2.6.1 in the sarg-date file of each report directory. | |
350 | */ | |
351 | getword_start(&gwarea,data); | |
352 | if (getword_skip(16,&gwarea,' ')<0) { | |
10210234 | 353 | debuga(_("Maybe you have a broken week day in your %s%s/sarg-date file\n"),outdir,direntp->d_name); |
06b39c87 | 354 | exit(EXIT_FAILURE); |
4bcb77cf | 355 | } |
b25b96fe | 356 | if (getword_multisep(mon,sizeof(mon),&gwarea,' ')<0) { |
10210234 | 357 | debuga(_("Maybe you have a broken month in your %s%s/sarg-date file\n"),outdir,direntp->d_name); |
06b39c87 | 358 | exit(EXIT_FAILURE); |
4bcb77cf | 359 | } |
b25b96fe | 360 | if (getword_multisep(day,sizeof(day),&gwarea,' ')<0) { |
10210234 | 361 | debuga(_("Maybe you have a broken day in your %s%s/sarg-date file\n"),outdir,direntp->d_name); |
06b39c87 | 362 | exit(EXIT_FAILURE); |
4bcb77cf | 363 | } |
b25b96fe | 364 | if (getword_multisep(hour,sizeof(hour),&gwarea,' ')<0) { |
10210234 | 365 | debuga(_("Maybe you have a broken time in your %s%s/sarg-date file\n"),outdir,direntp->d_name); |
06b39c87 | 366 | exit(EXIT_FAILURE); |
4bcb77cf | 367 | } |
b25b96fe FM |
368 | do { |
369 | if (getword_multisep(year,sizeof(year),&gwarea,' ')<0) { | |
10210234 | 370 | debuga(_("Maybe you have a broken year in your %s%s/sarg-date file\n"),outdir,direntp->d_name); |
06b39c87 | 371 | exit(EXIT_FAILURE); |
b25b96fe FM |
372 | } |
373 | } while (year[0] && !isdigit(year[0])); //skip time zone information with spaces until the year is found | |
374 | if (sscanf(hour,"%d:%d:%d",&ihour,&iminute,&isecond)!=3) { | |
10210234 | 375 | debuga(_("Maybe you have a broken time in your %s%s/sarg-date file\n"),outdir,direntp->d_name); |
06b39c87 | 376 | exit(EXIT_FAILURE); |
4bcb77cf | 377 | } |
b25b96fe FM |
378 | buildymd(day,mon,year,ftime); |
379 | snprintf(item->creationdate,sizeof(item->creationdate),"%s%02d%02d%02d",ftime, ihour, iminute, isecond); | |
380 | } | |
5e6e0640 FM |
381 | item->dirname=strdup(direntp->d_name); |
382 | if (!item->dirname) { | |
383 | debuga(_("Not enough memory to store the directory name \"%s\" in the index\n"),direntp->d_name); | |
384 | exit(EXIT_FAILURE); | |
385 | } | |
b25b96fe FM |
386 | strncpy(item->date,data,sizeof(item->date)); |
387 | if (nsort+1>nallocated) { | |
388 | nallocated+=10; | |
389 | tempsort=realloc(sortlist,nallocated*sizeof(*item)); | |
390 | if (!tempsort) { | |
fcdc0918 | 391 | debuga(_("not enough memory to sort the index\n")); |
06b39c87 | 392 | exit(EXIT_FAILURE); |
4bcb77cf | 393 | } |
b25b96fe FM |
394 | sortlist=tempsort; |
395 | } | |
396 | for (i=nsort ; i>0 ; i--) { | |
fa6552b0 FM |
397 | if (item->sortnum>sortlist[i-1]->sortnum) break; |
398 | if (item->sortnum==sortlist[i-1]->sortnum) { | |
399 | if (strcmp(item->creationdate,sortlist[i-1]->creationdate)>=0) break; | |
b25b96fe FM |
400 | } |
401 | sortlist[i]=sortlist[i-1]; | |
491b862f | 402 | } |
b25b96fe FM |
403 | sortlist[i]=item; |
404 | nsort++; | |
25697a35 | 405 | } |
4157aa09 | 406 | |
06b39c87 | 407 | closedir( dirp ); |
b25b96fe FM |
408 | |
409 | if((fp_ou=fopen(wdir,"w"))==NULL) { | |
fcdc0918 | 410 | debuga(_("(index) Cannot open file %s\n"),wdir); |
06b39c87 | 411 | exit(EXIT_FAILURE); |
b25b96fe | 412 | } |
fcdc0918 | 413 | write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nsort)); |
c0ec9cc7 FM |
414 | close_html_header(fp_ou); |
415 | fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",fp_ou); | |
c36c7384 | 416 | fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("FILE/PERIOD"),_("CREATION DATE"),_("USERS"),_("BYTES"),_("AVERAGE")); |
b25b96fe FM |
417 | for (i=0 ; i<nsort ; i++) { |
418 | if (order>0) | |
419 | item=sortlist[i]; | |
420 | else | |
421 | item=sortlist[nsort-i-1]; | |
d25d4e6a | 422 | tuser=obtuser(outdir,item->dirname); |
b25b96fe | 423 | obttotal(outdir,item->dirname,tbytes,tuser,media); |
d25d4e6a | 424 | fprintf(fp_ou,"<tr><td class=\"data2\"><a href='%s/%s'>%s</a></td><td class=\"data2\">%s</td><td class=\"data\">%d</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",item->dirname,ReplaceIndex,item->dirname,item->date,tuser,tbytes,media); |
b25b96fe | 425 | } |
c0ec9cc7 | 426 | fputs("</table></div>\n",fp_ou); |
fa6552b0 FM |
427 | if (write_html_trailer(fp_ou)<0) |
428 | debuga(_("Write error in the index %s\n"),wdir); | |
429 | if (fclose(fp_ou)==EOF) | |
430 | debuga(_("Failed to close the index file %s - %s\n"),wdir,strerror(errno)); | |
b25b96fe FM |
431 | |
432 | if (sortlist) { | |
5e6e0640 FM |
433 | for (i=0 ; i<nsort ; i++) { |
434 | free(sortlist[i]->dirname); | |
b25b96fe | 435 | free(sortlist[i]); |
5e6e0640 | 436 | } |
b25b96fe FM |
437 | free(sortlist); |
438 | } | |
25697a35 | 439 | } |
06ced858 FM |
440 | |
441 | static void file_index_to_date_index(const char *entry) | |
442 | { | |
fa6552b0 | 443 | int y1, y2, m1, m2, d1, d2; |
06ced858 FM |
444 | int i, j; |
445 | int ndirlen; | |
b5f13803 | 446 | int monthlen; |
fa6552b0 | 447 | char sm1[8], sm2[8]; |
06ced858 FM |
448 | char olddir[MAXLEN], newdir[MAXLEN]; |
449 | ||
450 | if(strlen(entry) < 19) return; | |
451 | ||
452 | y1=0; | |
453 | y2=0; | |
fa6552b0 FM |
454 | memset(sm1,0,sizeof(sm1)); |
455 | memset(sm2,0,sizeof(sm2)); | |
06ced858 FM |
456 | d1=0; |
457 | d2=0; | |
458 | i=0; | |
459 | if(strcmp(df,"u") == 0) { | |
460 | for (j=0 ; entry[i] && isdigit(entry[i]) ; j++) | |
461 | y1=y1*10+(entry[i++]-'0'); | |
462 | if (j!=4) return; | |
fa6552b0 FM |
463 | for (j=0 ; j<sizeof(sm1)-1 && entry[i] && isalpha(entry[i]) ; j++) |
464 | sm1[j]=entry[i++]; | |
06ced858 | 465 | if (j!=3) return; |
fa6552b0 | 466 | sm1[j]='\0'; |
06ced858 FM |
467 | for (j=0 ; entry[i] && isdigit(entry[i]) ; j++) |
468 | d1=d1*10+(entry[i++]-'0'); | |
469 | if (j!=2) return; | |
470 | ||
471 | if (entry[i++]!='-') return; | |
472 | ||
473 | for (j=0 ; entry[i] && isdigit(entry[i]) ; j++) | |
474 | y2=y2*10+(entry[i++]-'0'); | |
475 | if (j!=4) return; | |
fa6552b0 FM |
476 | for (j=0 ; j<sizeof(sm2)-1 && entry[i] && isalpha(entry[i]) ; j++) |
477 | sm2[j]=entry[i++]; | |
06ced858 | 478 | if (j!=3) return; |
fa6552b0 | 479 | sm2[j]='\0'; |
06ced858 FM |
480 | for (j=0 ; entry[i] && isdigit(entry[i]) ; j++) |
481 | d2=d2*10+(entry[i++]-'0'); | |
482 | if (j!=2) return; | |
483 | } else if(strcmp(df,"e") == 0) { | |
484 | for (j=0 ; entry[i] && isdigit(entry[i]) ; j++) | |
485 | d1=d1*10+(entry[i++]-'0'); | |
486 | if (j!=2) return; | |
fa6552b0 FM |
487 | for (j=0 ; j<sizeof(sm1)-1 && entry[i] && isalpha(entry[i]) ; j++) |
488 | sm1[j]=entry[i++]; | |
06ced858 | 489 | if (j!=3) return; |
fa6552b0 | 490 | sm1[j]='\0'; |
06ced858 FM |
491 | for (j=0 ; entry[i] && isdigit(entry[i]) ; j++) |
492 | y1=y1*10+(entry[i++]-'0'); | |
493 | if (j!=4) return; | |
494 | ||
495 | if (entry[i++]!='-') return; | |
496 | ||
497 | for (j=0 ; entry[i] && isdigit(entry[i]) ; j++) | |
498 | d2=d2*10+(entry[i++]-'0'); | |
499 | if (j!=2) return; | |
fa6552b0 FM |
500 | for (j=0 ; j<sizeof(sm2)-1 && entry[i] && isalpha(entry[i]) ; j++) |
501 | sm2[j]=entry[i++]; | |
06ced858 | 502 | if (j!=3) return; |
fa6552b0 | 503 | sm2[j]='\0'; |
06ced858 FM |
504 | for (j=0 ; entry[i] && isdigit(entry[i]) ; j++) |
505 | y2=y2*10+(entry[i++]-'0'); | |
506 | if (j!=4) return; | |
507 | } else | |
508 | return; | |
509 | ||
fa6552b0 FM |
510 | m1=conv_month(sm1); |
511 | m2=conv_month(sm2); | |
06ced858 FM |
512 | ndirlen=sprintf(newdir,"%s%04d",outdir,y1); |
513 | if(access(newdir, R_OK) != 0) mkdir(newdir,0755); | |
fa6552b0 FM |
514 | if(m1 != m2) ndirlen+=sprintf(newdir+ndirlen,"/%02d-%02d",m1,m2); |
515 | else ndirlen+=sprintf(newdir+ndirlen,"/%02d",m1); | |
06ced858 | 516 | if(access(newdir, R_OK) != 0) mkdir(newdir,0755); |
b5f13803 | 517 | monthlen=ndirlen; |
06ced858 FM |
518 | if(d1!=d2) ndirlen+=sprintf(newdir+ndirlen,"/%02d-%02d",d1,d2); |
519 | else ndirlen+=sprintf(newdir+ndirlen,"/%02d",d1); | |
520 | ||
521 | sprintf(olddir,"%s%s",outdir,entry); | |
522 | if (rename(olddir,newdir)) { | |
fcdc0918 | 523 | debuga(_("(index) rename error from \"%s\" to \"%s\" - %s\n"),olddir,newdir,strerror(errno)); |
06b39c87 | 524 | exit(EXIT_FAILURE); |
06ced858 FM |
525 | } |
526 | ||
b5f13803 FM |
527 | strcpy(newdir+monthlen,"/images"); |
528 | if(access(newdir, R_OK) != 0) { | |
529 | #ifdef HAVE_SYMLINK | |
530 | char linkdir[MAXLEN]; | |
531 | ||
532 | sprintf(linkdir,"%simages",outdir); | |
533 | if (symlink(linkdir,newdir)) { | |
fcdc0918 | 534 | debuga(_("failed to create link \"%s\" to \"%s\" - %s\n"),linkdir,newdir,strerror(errno)); |
06b39c87 | 535 | exit(EXIT_FAILURE); |
b5f13803 FM |
536 | } |
537 | #else | |
538 | char cmd[MAXLEN]; | |
539 | int cstatus; | |
540 | ||
541 | sprintf(cmd,"ln -s \"%simages\" \"%s/images\"",outdir,newdir); | |
542 | cstatus=system(cmd); | |
543 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
fcdc0918 FM |
544 | debuga(_("command return status %d\n"),WEXITSTATUS(cstatus)); |
545 | debuga(_("command: %s\n"),cmd); | |
06b39c87 | 546 | exit(EXIT_FAILURE); |
b5f13803 FM |
547 | } |
548 | #endif | |
549 | } | |
06ced858 FM |
550 | } |
551 | ||
552 | static void date_index_to_file_index(const char *entry) | |
553 | { | |
554 | int y1, next; | |
fa6552b0 | 555 | int m1, m2; |
06ced858 | 556 | int d1, d2; |
fa6552b0 | 557 | int val1len; |
06ced858 FM |
558 | int i, j; |
559 | char val1[MAXLEN]; | |
fa6552b0 | 560 | const char *sm1, *sm2; |
06ced858 FM |
561 | char *str; |
562 | char newdir[MAXLEN], olddir[MAXLEN]; | |
563 | DIR *dirp2, *dirp3; | |
564 | struct dirent *direntp2; | |
565 | struct dirent *direntp3; | |
566 | ||
567 | if(strlen(entry) != 4) return; | |
568 | ||
569 | next=-1; | |
570 | if (sscanf(entry,"%d%n",&y1,&next)!=1 || next<0 || entry[next]) return; | |
571 | ||
e21b6c02 | 572 | val1len=snprintf(val1,sizeof(val1),"%s%s",outdir,entry); |
06ced858 FM |
573 | dirp2 = opendir(val1); |
574 | if (!dirp2) return; | |
575 | while ((direntp2 = readdir( dirp2 )) != NULL) { | |
576 | if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue; | |
577 | i=0; | |
578 | str=direntp2->d_name; | |
fa6552b0 FM |
579 | m1=0; |
580 | for (j=0 ; j<2 && str[i] && isdigit(str[i]) ; j++) | |
581 | m1=(m1*10)+(str[i++]-'0'); | |
582 | if (j>=2) continue; | |
583 | sm1=conv_month_name(m1); | |
06ced858 FM |
584 | if (str[i]=='-') { |
585 | i++; | |
fa6552b0 FM |
586 | m2=0; |
587 | for (j=0 ; j<2 && str[i] && isdigit(str[i]) ; j++) | |
588 | m2=(m2*10)+(str[i++]-'0'); | |
589 | if (j>=2) continue; | |
590 | sm2=conv_month_name(m2); | |
06ced858 | 591 | } else if (!str[i]) { |
fa6552b0 | 592 | sm2=sm1; |
06ced858 FM |
593 | } else { |
594 | continue; | |
595 | } | |
596 | ||
597 | sprintf(val1+val1len,"/%s",direntp2->d_name); | |
598 | dirp3 = opendir(val1); | |
599 | if (!dirp3) continue; | |
600 | while ((direntp3 = readdir( dirp3 )) != NULL) { | |
601 | if(!isdigit(direntp3->d_name[0]) || !isdigit(direntp3->d_name[1])) continue; | |
602 | i=0; | |
603 | str=direntp3->d_name; | |
604 | d1=0; | |
605 | for (j=0 ; str[i] && isdigit(str[i]) ; j++) | |
606 | d1=d1*10+(str[i++]-'0'); | |
607 | if (j!=2) continue; | |
608 | if (str[i]=='-') { | |
609 | i++; | |
610 | d2=0; | |
611 | for (j=0 ; str[i] && isdigit(str[i]) ; j++) | |
612 | d2=d2*10+(str[i++]-'0'); | |
613 | if (j!=2) continue; | |
614 | } else if (!str[i]) { | |
615 | d2=d1; | |
616 | } else { | |
617 | continue; | |
618 | } | |
619 | ||
fa6552b0 FM |
620 | if(strcmp(df,"u") == 0) sprintf(newdir,"%s%04d%s%02d-%04d%s%02d",outdir,y1,sm1,d1,y1,sm2,d2); |
621 | else if(strcmp(df,"e") == 0) sprintf(newdir,"%s%02d%s%04d-%02d%s%04d",outdir,d1,sm1,y1,d2,sm2,y1); | |
06ced858 FM |
622 | else continue; |
623 | sprintf(olddir,"%s%04d/%s/%s",outdir,y1,direntp2->d_name,direntp3->d_name); | |
624 | if(rename(olddir,newdir)) { | |
fcdc0918 | 625 | debuga(_("(index) rename error from \"%s\" to \"%s\" - %s\n"),olddir,newdir,strerror(errno)); |
06b39c87 | 626 | exit(EXIT_FAILURE); |
06ced858 FM |
627 | } |
628 | } | |
06b39c87 | 629 | closedir( dirp3 ); |
06ced858 | 630 | } |
06b39c87 | 631 | closedir( dirp2 ); |
b5f13803 FM |
632 | |
633 | /*! | |
634 | \bug The links to the images in the reports are broken after moving the directories | |
635 | as the the HTML files are not at the right level for the images any more. | |
636 | */ | |
06ced858 FM |
637 | } |
638 |