]>
git.ipfire.org Git - thirdparty/sarg.git/blob - download.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
30 static char * DownloadSuffix
= NULL
;
31 static char ** DownloadSuffixIndex
= NULL
;
32 static int NDownloadSuffix
= 0 ;
34 void download_report ( void )
37 FILE * fp_in
= NULL
, * fp_ou
= NULL
;
40 char report_in
[ MAXLEN
];
41 char wdirname
[ MAXLEN
];
55 struct getwordstruct gwarea
;
60 sprintf ( report_in
, "%s/sarg/download.log" , TempDir
);
61 if ( access ( report_in
, R_OK
) != 0 )
64 strcpy ( wdirname
, dirname
);
65 sprintf ( report
, "%s/download.html" , wdirname
);
67 strcat ( wdirname
, "sarg-period" );
69 if (( fp_in
= fopen ( wdirname
, "r" )) == 0 ) {
70 fprintf ( stderr
, "SARG: (download) %s: %s \n " , text
[ 45 ], wdirname
);
74 if (! fgets ( period
, sizeof ( period
), fp_in
)) {
75 fprintf ( stderr
, "SARG: (download) read error in %s \n " , wdirname
);
80 if (( fp_in
= MY_FOPEN ( report_in
, "r" ))== NULL
) {
81 fprintf ( stderr
, "SARG: (download) %s: %s \n " , text
[ 8 ], report_in
);
85 if (( fp_ou
= MY_FOPEN ( report
, "w" ))== NULL
) {
86 fprintf ( stderr
, "SARG: (download) %s: %s \n " , text
[ 8 ], report
);
90 fprintf ( fp_ou
, "<!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.01 Transitional//EN \" \" http://www.w3.org/TR/html4/loose.dtd \" > \n <html> \n <head> \n <meta http-equiv= \" Content-Type \" content= \" text/html; charset=%s \" > \n " , CharSet
);
92 fputs ( "</head> \n " , fp_ou
);
94 fprintf ( fp_ou
, "<body bgcolor= \" %s \" text= \" %s \" background= \" %s \" > \n " , BgColor
, TxColor
, BgImage
);
96 write_logo_image ( fp_ou
);
98 if ( IndexTree
== INDEX_TREE_DATE
)
99 show_sarg ( fp_ou
, "../../.." );
101 show_sarg ( fp_ou
, ".." );
103 fputs ( "<div align= \" center \" ><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n " , fp_ou
);
104 fprintf ( fp_ou
, "<tr><th class= \" title \" >%s</th></tr> \n " , Title
);
106 fprintf ( fp_ou
, "<tr><td class= \" header \" >%s: %s</td></tr> \n " , text
[ 89 ], period
);
107 fprintf ( fp_ou
, "<tr><th class= \" header3 \" >%s</th></tr> \n " , text
[ 125 ]);
108 fputs ( "</table></div> \n " , fp_ou
);
110 fputs ( "<div align= \" center \" ><table cellpadding= \" 0 \" cellspacing= \" 2 \" > \n " , fp_ou
);
111 fputs ( "<tr><td></td></tr> \n " , fp_ou
);
112 fprintf ( fp_ou
, "<tr><th class= \" header \" >%s</th><th class= \" header \" >%s</th><th class= \" header \" >%s</th><th class= \" header \" >%s</th></tr> \n " , text
[ 98 ], text
[ 111 ], text
[ 110 ], text
[ 91 ]);
114 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
115 getword_start (& gwarea
, buf
);
116 if ( getword ( data
, sizeof ( data
),& gwarea
, ' \t ' )< 0 || getword ( hora
, sizeof ( hora
),& gwarea
, ' \t ' )< 0 ||
117 getword ( user
, sizeof ( user
),& gwarea
, ' \t ' )< 0 || getword ( ip
, sizeof ( ip
),& gwarea
, ' \t ' )< 0 ||
118 getword ( url
, sizeof ( url
),& gwarea
, ' \t ' )< 0 ) {
119 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , report_in
);
123 if (( str
=( char *) strstr ( user
, "_" )) != ( char *) NULL
) {
124 if (( str
=( char *) strstr ( str
+ 1 , "_" )) != ( char *) NULL
)
129 ip2name ( ip
, sizeof ( ip
));
136 if ( strcmp ( ouser
, user
) == 0 )
140 if ( strcmp ( oip
, ip
) == 0 )
146 user_find ( name
, sizeof ( name
), user
);
148 if ( dotinuser
&& strchr ( name
, '_' )) {
149 subs ( name
, sizeof ( name
), "_" , "." );
152 if ( DownloadReportLimit
) {
153 if ( strcmp ( ouser2
, name
) == 0 ) {
159 if ( count
>= DownloadReportLimit
)
163 for ( i
= strlen ( url
)- 1 ; i
>= 0 && ( unsigned char ) url
[ i
]< ' ' ; i
--) url
[ i
]= 0 ;
165 fprintf ( fp_ou
, "<tr><td class= \" data \" >%s</td><td class= \" data \" >%s</td><td class= \" data \" >%s-%s</td><td class= \" data2 \" >" , name
, ip
, data
, hora
);
167 fprintf ( fp_ou
, "<a href= \" %s%s?url=%s \" ><img src= \" %s/sarg-squidguard-block.png \" border= \" 0 \" ></a> " , wwwDocumentRoot
, BlockIt
, url
, ImageFile
);
168 fprintf ( fp_ou
, "<a href= \" %s \" >" , url
);
169 output_html_string ( fp_ou
, url
);
170 fputs ( "</a></td></tr> \n " , fp_ou
);
173 fputs ( "</table> \n " , fp_ou
);
176 fputs ( "</body> \n </html> \n " , fp_ou
);
186 void free_download ( void )
188 if ( DownloadSuffix
) {
189 free ( DownloadSuffix
);
192 if ( DownloadSuffixIndex
) {
193 free ( DownloadSuffixIndex
);
194 DownloadSuffixIndex
= NULL
;
199 void set_download_suffix ( const char * list
)
207 DownloadSuffix
= strdup ( list
);
208 if (! DownloadSuffix
) {
209 fprintf ( stderr
, "SARG: Download suffix list too long \n " );
213 for ( i
= 0 ; list
[ i
] ; i
++)
214 if ( list
[ i
] == ',' ) j
++;
215 DownloadSuffixIndex
= malloc ( j
* sizeof ( char *));
216 if (! DownloadSuffixIndex
) {
217 fprintf ( stderr
, "SARG: Too many download suffixes \n " );
221 str
= DownloadSuffix
;
222 for ( i
= 0 ; DownloadSuffix
[ i
] ; i
++) {
223 if ( DownloadSuffix
[ i
] == ',' ) {
224 DownloadSuffix
[ i
] = '\0' ;
227 for ( j
= 0 ; j
< NDownloadSuffix
&& ( cmp
= strcasecmp ( str
, DownloadSuffixIndex
[ j
]))> 0 ; j
++);
229 for ( k
= NDownloadSuffix
; k
> j
; k
--)
230 DownloadSuffixIndex
[ k
]= DownloadSuffixIndex
[ k
- 1 ];
232 DownloadSuffixIndex
[ j
]= str
;
235 str
= DownloadSuffix
+ i
+ 1 ;
241 for ( j
= 0 ; j
< NDownloadSuffix
&& ( cmp
= strcasecmp ( str
, DownloadSuffixIndex
[ j
]))> 0 ; j
++);
243 for ( k
= NDownloadSuffix
; k
> j
; k
--)
244 DownloadSuffixIndex
[ k
]= DownloadSuffixIndex
[ k
- 1 ];
246 DownloadSuffixIndex
[ j
]= str
;
251 int is_download_suffix ( const char * url
)
255 int down
, up
, center
;
258 const int max_suffix
= 10 ;
260 if ( DownloadSuffix
== NULL
|| NDownloadSuffix
== 0 ) return ( 0 );
262 urllen
= strlen ( url
)- 1 ;
263 if ( urllen
<= 0 ) return ( 0 );
264 if ( url
[ urllen
] == '.' ) return ( 0 ); //reject a single trailing dot
265 for ( i
= 0 ; i
< urllen
&& ( url
[ i
]!= '/' || url
[ i
+ 1 ]== '/' ) && url
[ i
]!= '?' ; i
++);
266 if ( i
>= urllen
) return ( 0 ); // url is a hostname without any path or file to download
268 for ( i
= 0 ; i
<= max_suffix
&& i
< urllen
&& url
[ urllen
- i
]!= '.' ; i
++)
269 if ( url
[ urllen
- i
] == '/' || url
[ urllen
- i
] == '?' ) return ( 0 );
270 if ( i
> max_suffix
|| i
>= urllen
) return ( 0 );
272 suffix
= url
+ urllen
- i
+ 1 ;
274 up
= NDownloadSuffix
- 1 ;
277 cmp
= strcasecmp ( suffix
, DownloadSuffixIndex
[ center
]);
278 if ( cmp
== 0 ) return ( 1 );