Prevent malicious users from supplying directories containing ".." etc. for path...
[recordtv.git] / playonwii / index.php
blob97bc7e4fb43c4f09f4b98f38220d07d5ffc90354
1 <?php
3 include_once( "functions.php" );
5 $extra_path = sanitise_filename( $_GET["path"] );
6 $excluded_preprepare = explode( ":", $_GET["exclude"] );
8 $excluded = array();
10 foreach( $excluded_preprepare as $ex )
12 $excluded[] = sanitise_filename( $ex );
15 $full_videos_dir = $videos_dir;
17 if( $extra_path )
19 $full_videos_dir .= "/$extra_path";
22 function is_video_dir( $full_path )
24 if( substr_compare( $full_path, "/.", -2 ) == 0 or
25 substr_compare( $full_path, "/..", -3 ) == 0 or
26 substr_compare( $full_path, "/deleted", -8 ) == 0 or
27 substr_compare( $full_path, "/tvguide", -8 ) == 0 )
29 return false;
32 return is_dir( $full_path );
35 function is_not_excluded( $filename )
37 global $excluded;
39 return !in_array( $filename, $excluded );
42 function get_filenames_recursive( &$filenames, $top_dir, $sub_dir = "" )
44 $handle = opendir( "$top_dir/$sub_dir" );
45 if( $handle )
47 while( ( $filename = readdir( $handle ) ) )
49 if( strlen( $sub_dir ) > 0 )
51 $filepath = "$sub_dir/$filename";
53 else
55 $filepath = $filename;
57 if( is_not_excluded( $filename ) &&
58 is_video_dir( "$top_dir/$sub_dir/$filename" ) )
60 get_filenames_recursive( $filenames, $top_dir, $filepath );
62 else
64 $filenames[$filepath] = '';
67 closedir($handle);
71 function pretty_date( $date )
73 global $num_to_month;
75 $tm = mktime(
76 (int)( substr( $date, 11, 2 ) ),
77 (int)( substr( $date, 14, 2 ) ),
79 (int)( substr( $date, 5, 2 ) ),
80 (int)( substr( $date, 8, 2 ) ),
81 (int)( substr( $date, 0, 4 ) ) );
83 return date( "D d M, H:i", $tm );
86 function get_info_from_file( $fn )
88 $title = "";
89 $sub_title = "";
90 $description = "";
91 $date = "";
92 $channel = "";
94 $handle = fopen( $fn, "r" );
96 if( $handle )
98 while ( !feof( $handle ) )
100 $line = fgets( $handle );
101 $line = substr( $line, 0, -1 );
103 list( $k, $v ) = split( "=", $line, 2 );
105 switch( $k )
107 case "title":
109 $title = $v;
110 break;
112 case "sub_title":
114 $sub_title = $v;
115 break;
117 case "description":
119 $description = $v;
120 break;
122 case "startTime":
124 $date = $v;
125 break;
127 case "channel_pretty":
129 $channel = $v;
130 break;
135 fclose( $handle );
138 return array( $title, $date, $channel, $sub_title, $description );
141 function get_info_from_filename( $fn )
143 if( preg_match( '/^(.*?)-(.*)\\..*$/', $fn, $matches ) )
145 $title = $matches[1];
146 $date = $matches[2];
148 else
150 $title = substr( $fn, 0, strlen( $fn ) - 4 );
151 $date = "";
154 return array( $title, $date );
157 function get_info( $filename, $filenames )
159 global $full_videos_dir;
161 $title = Null;
163 if( preg_match( '/^(.*)\\.flv$/', $filename, $matches ) )
166 $infofn = $matches[1] . ".rtvinfo";
168 if( array_key_exists( $infofn, $filenames ) )
170 list( $title, $date, $channel, $sub_title, $description ) =
171 get_info_from_file( $full_videos_dir . "/" . $infofn );
173 else
175 list( $title, $date ) = get_info_from_filename( $filename );
176 $channel = "";
177 $sub_title = "";
178 $description = "";
180 $titles[$title][] = array( $num, $date, $channel, $sub_title,
181 $description );
182 $num++;
185 return array( $title, $date, $channel, $sub_title, $description );
188 // This is a hash filename -> nothing of all files in the videos directory
189 // and subdirectories
190 $filenames = array();
191 get_filenames_recursive( $filenames, $full_videos_dir );
193 // This is a hash filename -> nothing of all files in the deleted directory
194 $deleted_filenames = array();
195 $handle = opendir( $deleted_dir );
196 if( $handle )
198 while( ( $filename = readdir( $handle ) ) )
200 $deleted_filenames[$filename] = '';
203 closedir($handle);
207 // This is a hash title->array( array( filenumber, date, filename, channel ) )
208 $titles = array();
209 $num = 0;
211 $nondeleted_filenames = array();
213 foreach ( $filenames as $fn => $blank )
215 $modified_fn = str_replace( "/", "_", $fn );
216 if( !array_key_exists( $modified_fn, $deleted_filenames ) )
218 $nondeleted_filenames[$fn] = '';
221 #array_diff_key( $filenames, $deleted_filenames );
223 $sorted_fns = array_keys( $nondeleted_filenames );
224 sort( $sorted_fns );
226 foreach( $sorted_fns as $filename )
228 list( $title, $date, $channel, $sub_title, $description ) = get_info(
229 $filename, $nondeleted_filenames );
231 if( $title != Null )
233 $titles[$title][] = array( $num, $date, $channel, $sub_title,
234 $description, $filename );
235 $num++;
241 <html>
243 <head>
244 <title>Recorded programmes</title>
245 <style type="text/css">
246 body {
247 font-family: verdana, sans-serif;
248 text-align: center;
251 text-decoration: none;
252 color: black;
254 a:hover {
255 color: red;
257 a.deletelink {
258 color:red;
259 font-size: xx-small;
261 a.title {
262 color: blue;
264 span.smalltime {
265 font-size: smaller;
266 color: gray;
268 span.smalltime:hover {
269 color: red;
271 h2 {
272 font-size: x-large;
273 font-weight: normal;
274 color: blue;
275 margin: 2px;
277 td.dates {
278 font-size: small;
280 </style>
281 <script language="JavaScript">
283 function makeRequest( url, arg )
285 var httpRequest;
287 if( window.XMLHttpRequest ) // Mozilla, Safari etc.
289 httpRequest = new XMLHttpRequest();
291 else if( window.ActiveXObject ) // IE
295 httpRequest = new ActiveXObject( "Msxml2.XMLHTTP" );
297 catch( e )
301 httpRequest = new ActiveXObject( "Microsoft.XMLHTTP" );
303 catch( e )
309 if( !httpRequest )
311 return false;
314 httpRequest.onreadystatechange = function()
316 receiveAnswer( httpRequest, arg );
319 httpRequest.open('GET', url, true);
320 httpRequest.send('');
323 function receiveAnswer( httpRequest, prog_filename )
325 if( httpRequest.readyState == 4 )
327 if( httpRequest.status != 200 )
329 document.location = "delete_error.php?filename=" + prog_filename
335 function mouse_over( tr_id )
337 tr_el = document.getElementById( tr_id );
338 tr_el.style.backgroundColor = '#ffaaaa';
341 function mouse_out( tr_id )
343 tr_el = document.getElementById( tr_id );
344 tr_el.style.backgroundColor = 'transparent';
347 function title_click( table_id )
349 table_el = document.getElementById( table_id );
350 if( table_el.style.display == 'inline' )
352 table_el.style.display = 'none';
354 else
356 table_el.style.display = 'inline';
361 function delete_prog( prog_filename )
363 makeRequest( 'delete.php?filename=' + prog_filename, prog_filename );
364 tr_el = document.getElementById( 'tr_' + prog_filename );
365 tr_el.style.display = 'none';
368 </script>
369 </head>
371 <body>
372 <h1>Recorded programmes</h1>
374 <center>
375 <?php
376 ksort( $titles );
377 $table_counter = 0;
378 foreach( $titles as $title => $arr )
380 list( $num, $date, $channel, $sub_title ) = $arr[0];
381 print "<h2><a class='title' href='javascript: title_click( \"table_$table_counter\" )'>$title</a></h2>\n";
383 print "<table id='table_$table_counter' style='display: none' width='90%' cellpadding='0' cellspacing='0' border='0'>";
384 foreach( $arr as $lst )
386 list( $num, $date, $channel, $sub_title, $description,
387 $filename ) = $lst;
388 print "<tr id='tr_$filename'><td><a href='play.php?filename=$filename' style='padding-right: 10px'";
390 print " title='$description'";
392 print ">";
394 if( $sub_title )
396 print $sub_title . " <span class='smalltime'>(";
399 print pretty_date( $date );
401 if( $channel )
403 print " on $channel";
406 if( $sub_title )
408 print ")</span>";
411 print "</a></td>";
412 print "<td><a class='deletelink' onmouseover='mouse_over(\"tr_$filename\")' onmouseout='mouse_out(\"tr_$filename\")' href='javascript: delete_prog( \"$filename\" )'>[DELETE]</a></td></tr>\n";
414 print "</table>\n";
415 $table_counter += 1;
418 </center>
420 </body>
422 </html>