MediaSteram Class for PHP

My PHP Stuff

A lot of video service like YouTube uses a secret link

(like this:

)

to send video data to the flash player. We don’t know the video file actually from what part of the server.

Actually, it sends a header (It is: “Content-Type: video/x-flv”) to the flash player and let the flash player downloads that file which contains the video data. After finding out this, I made this class for this purpose.


<?php

class MediaStream

{

    private 
$resource;

    private 
$type;

    private 
$handle;

    private 
$base64;

    public 
$error;

    

    public function 
__construct($a,$type,$base64 false,$context NULL)

    {

        
//make sure $a is set

        
if(!isset($a))

        {

            
$this->error 'You missed argument 1 : $a';

        }

        

        
//the stream is a base64 encoded string, process it

        
if($base64)

        {

            
$this->getResourceFromBase64EncodedString($a);

            
$this->base64 $base64;

        }

        else if(
preg_match("/(.*)(\.(.*))$/i",$a) || preg_match("/^(http:|ftp:|https:)(.*)$/",$a))

        {

            
//$a is not a base64 encoded string but it's a file or a url, open it and save it into a variable

            
if(isset($context))

            {

                
//user had defined the context

                
$this->handle = @fopen($a,"rb",false,$context);

            }

            else

            {

                
$this->handle = @fopen($a,"rb");

            }

        }

        else

        {

            
//$a ia neither a base64 encoded string nor a filename

            
$this->error "Error occured! The source is neither a base64 encoded string nor a file.";

        }

        
$this->type = isset($type) ? $type : ($this->error "Error occured! You haven't set the MIME type of the resource.");

    }

    

    private function 
getResourceFromBase64EncodedString($a)

    {

        if(isset(
$a))

        {

            
set_time_limit(0); //prevent large data being cut off

            
$this->resource base64_decode($a);

        }

        else

        {

            return 
false;

        }

    }

    

    public function 
OutputStream()

    {

        
//if error occured, stop output the stream

        
if(isset($this->error))

        {

            die(
$this->error);

        }

        
//determine if the resource is decoded from base64 encoded string

        
if($this->base64)

        {

            
header("Content-type: ".$this->type);

            echo 
$this->resource;

        }

        else if(isset(
$this->handle))//the resource is a file handle, output all contents in the file.

        
{

            if(
$this->handle === false)

            {

                die(
"Error occured while open the stream.");

            }

            

            
header("Content-type: ".$this->type);

            
fpassthru($this->handle);

        }

        else

        {

            die(
"No contents to output.");

        }

    }

    

    public function 
__destruct()

    {

        unset(
$this->resource);

        unset(
$this->type);

        if(isset(
$this->handle) && $this->handle !== false)

        {

            
fclose($this->handle);    

        }

        unset(
$this->handle);

        unset(
$this->error);

    }

}

//examples

$s = new MediaStream("es.php.net.flv","video/x-flv");

$s->OutputStream();

$s->__destruct();

?>

Hope it’ll help you. This time the PHP source code has coloured so it’s easier to read. If you found bugs please email or leave a comment to me.

The universal serial maker

My PHP Stuff

One day, I’ve made an idea: How about make something useful? Finally, I made this universal serial maker. This serial maker actually generates a 25-characters serials (xxxxx-xxxxx-xxxxx-xxxxx-xxxxx where x is uppercase A-Z and numbers.). The program guess the combinations of serials and outputs a random serial like:

MVDZM-1GWM7-LWQNQ-NPFM3-8X1S4

I4VUJ-AX939-FJLRY-JBBS9-ZB2CS

T4AJE-28XXR-NSL4G-GOIKX-TJA9F

QANQ6-03EMI-GPS13-V60G9-8E6C3

1J873-RXG0X-FE1NZ-B60C7-DCMMY

QJM3R-HMHQK-D6PMP-FJZKC-NILSM

78KGY-3S5A8-KJUCT-N2SHM-U8WVX

NN29Z-4ACY3-92VZE-IBWRN-3SRDZ

0UDAM-7LTG7-B16SP-9JER4-C6QK2

LDE9Y-MLUJ2-UR2FS-Y9DRK-QYAYL

This format of serials is suitable for many products. However, there’re still some products that don’t use this format of serials. (e.g. Adobe products.)

The source code:

<?php

for($i = 0;$i < 10; $i++)

{

for($j = 1 ;$j < 26;$j++)

{

@$result.= substr(str_shuffle(“0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ”),$j,1);

if(!is_float($j/5))

{

$result.=”-“;

}

}

echo substr($result,0,strlen($result)-1) . PHP_EOL;

$result = null;

}

?>

This generates 10 serials at a time. Hope it’ll help you.