Code - steamcaststream

<?php
/*****
/* Steamcast Class to pull info about your stream and pass it off to other pages
/* Author: Jay Krivanek (http://www.radiotoolbox.com and http://www.steamcast.com)
/* License: Free to use and distribute for anyone any where.
/* No warranty implied or offered for the code below.  Use at your own risk!
/* */

/****
/* So I know you are in a super huge rush to use this class and dig in.  First a few things about the class.
/* I literally wrote this code in an hour.  So there might be bugs, but hey, it's free!
/* You use this class to access either all streams on a server or one.  If you need only one, then please specify so in the
/* $steamcast_url of the constructor.  If you want all your streams then just pass the base url to the constructor.
/*
/* Example:
/* // In this example we just want one stream's info, that stream would be whatever is at mount /live
/* $steamcast = new SteamcastStream('http://localhost:8000/live', 'admin', 'mysecretpassword');
/*
/* // In this example we want all of the streams that localhost:8000 has
/* $steamcast = new SteamcastStream('http://localhost:8000/', 'admin', 'mysecretpassword');
/*
/* You can also pass authentication details and the address in one single argument like so
/* $steamcast = new SteamcastStream('http://admin:mysecretpassword@localhost:8000/live');
/*
/* Any credentials passed this way will override the optional user and pass credentials.
/* Once this is done you will need to retrieve the stats by calling $steamcast->retrieve_stats()
/* If this call succeeds it will return true, false will be returned if something is wrong.
/* 
/* You also might want to check if the server has streams by checking $steamcast->has_streams()
/* This will return true if it does, false if not.  If this returns false then the only valid call you can make is
/* $steamcast->to_array() which will return an array with some general information about the server state.
/* This can happen if the server has no mounts created or an invalid mount url was passed to the constructor.
/*
/* You can get the details of a particular stream by calling $steamcast->stream_info().  If more than one mount is expected
/* You can pass the index into this function $steamcast->stream_info(2), use $steamcast->stream_count() to ensure that '2' would have 
/* valid data in it.
/* */

class SteamcastStream
{
    
// Properties
    
private $url = array();
    private 
$user;
    private 
$pass;
    private 
$data;

    
// Methods
    
    // Users of this class should construct the object with the base url of their steamcast server and
    // authentication details for statistics
    
public function __construct($steamcast_url$username ''$password '')
    {
        
$this->user $username;
        
$this->pass $password;
        
$this->url parse_url($steamcast_url);
        if (!isset(
$this->url['host']) && !isset($this->url['scheme']) && isset($this->url['path']))
        {
            
$this->url parse_url('http://'.$this->url['path']);
        }
        if (isset(
$this->url['user']))
            
$this->user $this->url['user'];
        if (isset(
$this->url['pass']))
            
$this->pass $this->url['pass'];
        if (!isset(
$this->url['port']))
            
$this->url['port'] = 80;
        if (!isset(
$this->url['path']))
            
$this->url['path'] = '/';
    }

    
    
// This function actually is required to retrieve the stats.
    
public function retrieve_stats()
    {
        
$opts = array(
            
'http'=>array(
                
'method'=>"GET",
                
'header'=>"User-Agent: Steamcast Stats (compatible; Mozilla/5.0)\r\n" .
                      
"Authorization: Basic ".base64_encode($this->user.':'.$this->pass)."\r\n"
                
)
            );

        
$context stream_context_create($opts);

        
$query '';
        if (
$this->url['path'] != '/')
            
$query '?src='.urlencode($this->url['path']);
        
        
$json file_get_contents('http://'.$this->url['host'].':'.$this->url['port'].'/admin/status.json'.$queryfalse$context);
        
$array json_decode($json,TRUE);
        if (
$array)
        {
            
$this->data $array;
            return 
true;
        }
        else
        {
            return 
false;
        }
    }

    
// returns the count of stream nodes in the array.
    
public function stream_count()
    {
        return 
count($this->data['sources']);
    }

    
// tells us if we have any source nodes in the sheet we retrieved.
    
public function has_streams()
    {
        return isset(
$this->data['sources']) && count($this->data['sources']) > 0;
    }

    
// Gives us access to all resources we successfully retrieved.
    
public function to_array()
    {
        return 
$this->data;
    }

    
// Returns just data for the stream we want by index.
    
public function stream_info($index 0)
    {
        if (
count($this->data['sources']) > $index)
            return 
$this->data['sources'][$index];
        else
            return 
NULL;
    }
}


//This is an example 
/*
$steamcast = new SteamcastStream('mysteamcastserver.com', 'admin', 'mysecretpassword');
if ($steamcast->retrieve_stats() && $steamcast->has_streams())
{
    $data = $steamcast->stream_info();
    if ($data != NULL)
    {
        echo 'Wow, '.$data['name'].' has some shtuff in it!  Currently there are '.$data['nodes'].' listeners!  They are currently hearing '.$data['meta_song'].'';

        foreach ($data['played'] as $key => $val)
        {
            echo '<br> at '.date('r', $val['entry_time']).' we played '.$val['song_title'];
        }
        foreach ($data['peers'] as $key => $val)
        {
            echo '<br> '.$val['ip'].' connected at '.date('r', $val['pconnect_time']).' with '.$val['user_agent'];
        }
    }
    else
    {
        echo 'No Mount by this name!';
    }
}
else
{
    echo "This failed :(";
}
*/

?>