<?

class SecureMedia {

    function 
SecureMedia$options ) {

        
$this->dir $options'dir' ];
        
$this->password $options'password' ];

    }

    function 
get$key$file$type 'swf' ) {

        
// Code modified to support conditional GET by Duncan Gough (duncan@3rdsense.com)

    
$filename str_replace( array( '../''..\\' ), array( '''' ), $filename );
        
$filename $this->dir $file;
        
        if ( 
$type == 'swf' )
        {
            
$content_type 'application/x-shockwave-flash';
        }
        else if ( 
$type == 'dcr' )
        {
            
$content_type 'application/x-director';
        }

        
$keystring md5$this->password date"Ymd" ) );
        
        if(
$keystring == $key )
        {

            
// Send the main headers
            
$last_modified gmdate'D, d M Y H:i:s \G\M\T'filemtime$filename ) );
            
$etag '"' md5$last_modified ) . '"';

            
header'Last-Modified: ' $last_modified );
            
header'ETag: ' $etag );

            
// Conditional GET:
            // If the relevant headers are there, the browser has the file in its cache so we needn't send the data again :)
            // Code by Simon Willison - http://simon.incutio.com/archive/2003/04/23/conditionalGet
            
$if_modified_since = isset( $_SERVER'HTTP_IF_MODIFIED_SINCE' ] ) ? stripslashes$_SERVER'HTTP_IF_MODIFIED_SINCE' ] ) : false;

            
$if_none_match = isset( $_SERVER'HTTP_IF_NONE_MATCH' ] ) ? stripslashes$_SERVER'HTTP_IF_NONE_MATCH' ] ) : false;

            
// No headers, send the file
            
if( !$if_modified_since && !$if_none_match )
                
$this->_return_file$content_type$filename );

            
// ETag is there but doesn't match
            
if( $if_none_match && $if_none_match != $etag )
                
$this->_return_file($content_type$filename );

            
// if-modified-since is there but doesn't match
            
if( $if_modified_since && $if_modified_since != $last_modified )
                
$this->_return_file$content_type$filename );

            
// No need to send the file again, the browser can use a cached version
            
header('HTTP/1.0 304 Not Modified');

            exit;

        }
        else
        {

            
header'Location: http://www.playaholics.com/denied/' );
            exit;

        }

    }

    function 
_return_file$content_type$filename ) {

        
// Content-Length was proving to be a hassle when it came
        // to browser inter-operability, so I'm just providing the
        // Content-Type and the raw data. For example, Shockwave
        // games weren't pullin in the content from Macromedia (fonts
        // and 3D plugins) on Firefox, returning Partial-Content errors.
        //  Might use readfile() once I'm happy this is stable..
        
header'Content-Type: ' $content_type );
        
$fd = @fopen ($filename"rb");
        
$data = @fread ($fdfilesize ($filename));
        
fclose ($fd);
        print 
$data;
        exit;

        
/*
        Return the file data and the correct header.
        If the browser accepts gzip encoded data, then
        set the Content-Length header accordingly, as we
        gzip the output of our pages in php.ini
        
        UPDATE - if anyone can make SecureMedia work reliably with
        all browsers, and send the correct Content-Length headers
        depending on whether the server supports gzip and the browser
        handles gzip, please do so :)

        $fd = @fopen ($filename, "rb");
        $data = @fread ($fd, filesize ($filename));
        fclose ($fd);

        header( 'Accept-Ranges: bytes' );

        if( strstr( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ], 'gzip' ) )
        {
            header( 'Content-Length: ' . strlen( @gzencode( $data ) ) );
        }
        else
        {
            header( 'Content-Length: ' . strlen( $data ) );
        }
        header( 'Content-Type: ' . $content_type );
        print $data;
        exit;
        */

    
}

}

?>