Old 16th September 2011, 04:09   #1
p0rt
Senior Member
 
Join Date: Nov 2010
Posts: 152
php sc_serv 2 status class

fully works and returns array of standard stats from sc_serv 2

use this function anywhere in while() { etc

/**
* SHOUTCAST - NULL NUKE
* get shoutcast server info
* $in_host = 127.0.0.1
* $in_port = 8000
* $in_sid = 1 [ DNAS stream id 1-7]
* $in_timeout = 1sec timeout to get stats
*/
function shoutcast($in_host, $in_port = 8000, $in_sid = 1, $in_timeout = 1) {

global $phpEx;

// /*
// RETURN/s
// $array['status']
// $array['listener']
// $array['maxlisteners']
// $array['peak']
// $array['title']
// $array['content_type']
// $array['genre']
// $array['url']
// $array['track']
// */

include_once "core_ShoutcastInfo.php";

$_SERVER['shoutcast'] = new ShoutcastInfo( '', '', '', '');

return $_SERVER['shoutcast'] -> ShoutcastInfo($in_host, $in_port, $in_sid, $in_timeout);


}

save the following code as core_ShoutcastInfo.php

Quote:
<?php

/**
/* p0rt = NULL NUKE = MADE RETURN ARRAY - SC_SERV 2
*/

/***************************************************************************
* ShoutcastInfo Class
* -------------------
* begin : Wednesday, Aug 18, 2004 - 4:12
* copyright : (C) 2004 MC Breit
* email : support@mcb.cc - MCB.CC - Free and Open Sources
* last modified : 18/08/04 - 06:26 - MC Breit
* version : 0.0.2
*
* last modified : 16/09/11 - 06:26 - p0rt
* version : 0.0.x
*
***************************************************************************/

/***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
***************************************************************************/

//
// Begin ShoutcastInfo class.
//

class ShoutcastInfo
{

//
// Begin of class variables
//

/*****
* @var $sock
* contains the socket handler.
*/
var $sock = FALSE;

/*****
* @var $error
* saves errorstr and numbers as array(no,str)
*/
var $error = array(NULL, NULL);

/*****
* @var $host
* contains the hostname for shoutcast server
*/
var $hostname = NULL;

/*****
* @var $port
* contains the port used for connection
*/
var $port = NULL;

/*****
* @var $sid
* contains the stream id used for sc_serv 2
*/
var $sid = NULL;

/*****
* @var $timeout
* the timeout for connection
*/
var $timeout = NULL;

/*****
* @var $parsed
* is true when datas where parsed
*/
var $parsed = FALSE;

/****
* @var $datas
* contains the unparsed datas
*/
var $datas = NULL;

/*****
* @var $pdatas
* assitioative array of parsed datas
*/
var $pdatas = NULL;

//
// Begin of class functions
//

/*****
* object ShoutcastInfo(string hostname [, int port [, int timeout])
* Crates an new Shoutcast Object. (Is the Class Creator)
*/
function ShoutcastInfo($hostname, $port=8888, $sid=1, $timeout=1)
{

$this->hostname = $hostname;
$this->port = $port;
$this->sid = $sid;
$this->timeout = $timeout;

if ( $this->connect() ) {

$this->send();

$array['status'] = $this->get_stat();
$array['listener'] = $this->get_listener();
$array['maxlisteners'] = $this->get_maxlisteners();
$array['peak'] = $this->get_peak();
$array['title'] = $this->get_title();
$array['content_type'] = $this->get_content_type();
$array['genre'] = $this->get_genre();
$array['url'] = $this->get_url();
$array['track'] = $this->get_track();

echo $array['track'];

$this->close();

return $array;

} else {

return (bool)FALSE;

}

} // ShoutcastInfo()

//
// Begin of socket and connection functions
//

/*****
* bool connect( void )
* creates server connection, returns true on success, else retruns false.
*/
function connect()
{

if( !$this->sock )
{
//Connect
$this->sock = fsockopen($this->hostname, $this->port, $this->error[0] , $this->error[1], $this->timeout);
}

//Check connection
if( $this->sock )
{
return TRUE;
}
else
{

$this->close();
return FALSE;

}
} // connect()

/*****
* bool close( void )
* closes current connection
*/
function close()
{
if( $this->sock )
{
fclose($this->sock);
}
} // close()

/*****
* bool refresh( void )
* closes connection and opens it again to get new datas.
* parsed datas will not replaced, but parsing will be
* able again.
*/
function refresh()
{
$this->close();
$this->sock = NULL;
if( !$this->connect() )
{
return FALSE;
}
$this->parsed = FALSE;
$this->send();
return TRUE;
} // refresh()

/*****
* void send( void )
* Sends http header and recives datas from server
*/
function send()
{

if( $this->sock )
{
//Send HTTP Header



fputs($this->sock, "GET /index.html?sid=". $this->sid ." HTTP/1.1\r\n"
."Host: 127.0.0.1/\r\n"
."User-Agent: Mozilla/5.0 Firefox/3.6.12)\r\n"
."Connection: Close\r\n"
."\r\n"
);

//Get datas

$this->datas = NULL;
while( !feof($this->sock) )
{

$this->datas .= fgets($this->sock, 128);
}


}
} // send()

/*****
* mixed error( [bool return])
* if return is true it will return a error message,
* else it will print it out (HTML Formatted!).
*/
function error($return=FALSE)
{
if( $return == FALSE )
{
print "<br><b>Error:</b> {$this->error[1]} (<i>{$this->error[0]}</i>)<br>";
return;
}
return "{$this->error[1]} ({$this->error[0]})";
}

//
// Begin of public functions
//

/*****
* bool get_stat( void )
* Checks that stream will be up and private/public.
*/
function get_stat()
{
if( strstr($this->datas, 'Server is currently up and') )
{
$this->pdatas['status'] = 1;
return TRUE;
}
else
{
$this->pdatas['status'] = 0;
return FALSE;
}
} // get_stat()

/*****
* integer get_listener( void )
* returns and resets the number of accutal listener.
*/
function get_listener()
{
//Is stream up?
if( $this->pdatas['status'] == 0 )
{
$this->pdatas['listener'] = 0;
return 0;
}

$this->pdatas['listener_max'] = explode('kbps with', $this->datas);
$this->pdatas['listener'] = explode(' of ', $this->pdatas['listener_max'][1]);
$this->pdatas['listener_max'] = $this->pdatas['listener'][1];
$this->pdatas['listener'] = $this->pdatas['listener'][0];
$this->pdatas['listener_max'] = explode(' l', $this->pdatas['listener_max']);
$this->pdatas['listener_max'] = $this->pdatas['listener_max'][0];

return $this->pdatas['listener'];
} // get_listener()

/*****
* integer get_maxlisteners( void )
* return max listeners - limit.
*/
function get_maxlisteners()
{
//Is stream up?
if( $this->pdatas['status'] == 0 )
{
$this->pdatas['listener'] = 0;
return 0;
}

$this->pdatas['listener_max'] = explode('kbps with ', $this->datas);
$this->pdatas['listener'] = explode(' of ', $this->pdatas['listener_max'][1]);
$this->pdatas['listener_max'] = $this->pdatas['listener'][1];
$this->pdatas['listener'] = $this->pdatas['listener'][0];
$this->pdatas['listener_max'] = explode(' l', $this->pdatas['listener_max']);
$this->pdatas['listener_max'] = $this->pdatas['listener_max'][0];

return $this->pdatas['listener_max'];
} // get_listener()

/*****
* integer get_peak( void )
* returns the listener peak from stream and resets it.
*/
function get_peak()
{
$this->pdatas['peak'] = $this->_extract_datas('Listener Peak: </font></td><td><font class="default"><b>');
return $this->pdatas['peak'];
} // get_peak()

/*****
* string get_title( void )
* returns and resetts the actual moderator/dj/stream_title at stream.
*/
function get_title()
{
//Is stream up?
if( $this->pdatas['status'] == 0 )
{
$this->pdatas['title'] = FALSE;
return FALSE;
}

$this->pdatas['title'] = $this->_extract_datas('Stream Name: </font></td><td><font class="default"><b>');
return $this->pdatas['title'];
} // get_title()

/*****
* string get_content_type( void )
* returns and resetts the actual ContentType at stream.
*/
function get_content_type()
{
//Is stream up?
if( $this->pdatas['status'] == 0 )
{
$this->pdatas['content_type'] = FALSE;
return FALSE;
}

$this->pdatas['content_type'] = $this->_extract_datas('Content Type: </font></td><td><font class="default"><b>');
return $this->pdatas['content_type'];
} // get_content_type()

/*****
* string get_genre( void )
* returns and resetts the actual Stream Genre.
*/
function get_genre()
{
//Is stream up?
if( $this->pdatas['status'] == 0 )
{
$this->pdatas['genre'] = FALSE;
return FALSE;
}

$this->pdatas['genre'] = $this->_extract_datas('Stream Genre: </font></td><td><font class="default"><b>');
return $this->pdatas['genre'];
} // get_genre()

/*****
* string get_url( void )
* returns and resetts the actual Stream URL.
*/
function get_url()
{
//Is stream up?
if( $this->pdatas['status'] == 0 )
{
$this->pdatas['url'] = 'none';
return 'none';
}

$this->pdatas['url'] = $this->_extract_datas('Stream URL: </font></td><td><font class="default"><b><a href="', '"');
return $this->pdatas['url'];
} // get_url()

/*****
* string get_track( void )
* returns and resetts the current track informations.
*/
function get_track()
{
//Is stream up?
if( $this->pdatas['status'] == 0 )
{
$this->pdatas['get_track'] = 'none';
return 'none';
}

// $sid =
$this->pdatas['get_track'] = $this->_extract_datas('Current Song: </font></td><td><font class="default"><b><a href="currentsong?sid='. $this->sid .'">', '"');
return $this->pdatas['get_track'];
} // get_track()

/*****
* array parse( void )
* get all the items aviable and return an assoc array.
* Note: Use this only if you need ALL the informations!
*/
function parse()
{
if( $this->parsed != TRUE )
{
//get all single infos
$this->get_stat();
$this->get_listener();
$this->get_maxlisteners();
$this->get_peak();
$this->get_title();
$this->get_content_type();
$this->get_genre();
$this->get_url();
$this->get_track();
//set parsed stat
$this->parsed = TRUE;
}

return $this->pdatas;

} // parse()

/*****
* mixed get_parsed_value( string key )
* Sucht aus dem geparsten array einen wert herraus und gibt ihn zurück
* wenn er noch nicht gesetzt ist wird NULL zurückgegeben.
*/
function get_parsed_value($key)
{
return ( isset($this->pdatas[$key]) ) ? $this->pdatas[$key] : FALSE;

} // get_parsed_value()

//
// Begin private functions
//

/*****
* private mixed _extract_datas(string match_str [, string ending])
* extracts and returns datas after an match_string and before net html tag or ending.
*/
function _extract_datas($match_str, $ending='<')
{
$datas = strstr($this->datas, $match_str);
//remove match_str because strstr starts before..
$datas = str_replace($match_str, '', $datas);
//split text after ending mark and throw all away isnt needed.
$datas = explode($ending, $datas);
return $datas[0];

} // _extract_datas()

} // ShoutcastInfo class

//
// Thats it folks!
//

?>
p0rt is offline   Reply With Quote
Old 16th September 2011, 07:39   #2
DrO
 
Join Date: Sep 2003
Posts: 27,873
right, so you've posted a script which appears to scrape the stream summary pages instead of going the far simpler (and supported options) /stats or other admin.cgi?sid=#&mode=viewxml&page=# to get that information.

/me shakes head in dismay.

-daz
DrO is offline   Reply With Quote
Old 16th September 2011, 12:57   #3
p0rt
Senior Member
 
Join Date: Nov 2010
Posts: 152
nope... you add the code to your website.. sc_serv is on a remote box...

on your web page for your site..

$scinfo = shoutcast( "server_ip", "8000", "streamid");

which will return an array, so then anywhere on your website you :

if ($scinfo ['status']) {
echo "status : <font color=\"lime\">Online</font><br />";
echo "Current Track : ". $scinfo ['track'] ."<br />";
echo "Listers : ". $scinfo ['listener'] ."/".$shoutcast['maxlisteners']."<br />";
echo "Genre : ". $scinfo ['genre'] ."<br />";
} else {
echo "status : <font color=\"red\">Offline</font><br />";
}

->cannot find the edit button, original class code has a table cell glitch and will destroy the layout of any page iit is used on.. so in core_ShoutcastInfo.php you need to replace :

Quote:
/*****
* string get_track( void )
* returns and resetts the current track informations.
*/
function get_track()
{
//Is stream up?
if( $this->pdatas['status'] == 0 )
{
$this->pdatas['get_track'] = 'none';
return 'none';
}

$this->pdatas['get_track'] = $this->_extract_datas('Current Song: </font></td><td><font class="default"><b><a href="currentsong?sid='. $this->sid .'">', '"');
return $this->pdatas['get_track'];
} // get_track()
with

Quote:
/*****
* string get_track( void )
* returns and resetts the current track informations.
*/
function get_track()
{
//Is stream up?
if( $this->pdatas['status'] == 0 )
{
$this->pdatas['get_track'] = 'none';
return 'none';
}

// $sid =
$this->pdatas['get_track'] = $this->_extract_datas('Current Song: </font></td><td><font class="default"><b><a href="currentsong?sid='. $this->sid .'">', '</a></b></td>');
return $this->pdatas['get_track'];
} // get_track()
p0rt is offline   Reply With Quote
Old 16th September 2011, 15:20   #4
DrO
 
Join Date: Sep 2003
Posts: 27,873
you're not reading what i said - the script is scraping the pages (which can and has changed between versions of the v2 DNAS i.e. it could break easily) rather than using the api pages (which are public facing) for getting the same information in a clean and proper manner.

if you prefer to do it in a hackish way then so be it, but recommending it to other people is not a good thing to do.

-daz
DrO is offline   Reply With Quote
Old 16th September 2011, 16:21   #5
p0rt
Senior Member
 
Join Date: Nov 2010
Posts: 152
its the way all sites have always used, you cannot access admin panel from a remote site and would be worthless. the class is to display the status of the server to the public embedded anywhere on your webpage, instead of having to access the http://server/index.html backend of sc_serv
p0rt is offline   Reply With Quote
Old 16th September 2011, 16:41   #6
DrO
 
Join Date: Sep 2003
Posts: 27,873
/me bangs head on desk

the whole point of the v2 tools is to do things better from the start which the v1 DNAS didn't do (or eventually allowed in later versions).

you've contradicted yourself as you're saying not having to access the http://server/index.html backend even though that is what the script you've posted is doing i.e. scraping the information from the public pages.

as i'm trying to tell you, there is no need to do that and (as you've not read the documentation), use http://server/stats?sid=# directly to get that information i.e. via another public page just like http://server/index.html also is.

just because that's how it's been done before doesn't mean doing it the same for the new tools is the correct way (which it isn't) but i get the feeling you're going to do whatever you want, so i'm going to leave you to it as clearly you know more than me.

-daz
DrO is offline   Reply With Quote
Old 16th September 2011, 17:22   #7
p0rt
Senior Member
 
Join Date: Nov 2010
Posts: 152
p0rt is offline   Reply With Quote
Old 21st September 2011, 00:31   #8
p0rt
Senior Member
 
Join Date: Nov 2010
Posts: 152
xml version of the above, its all kind of the same. but returns the xml from stats?sid# as an array

/**
* SHOUTCAST - NULL NUKE
* get shoutcast server info
* $in_host = 127.0.0.1
* $in_port = 8000
* $in_sid = 1 [ DNAS stream id 1-7]
* $in_timeout = 1sec timeout to get stats
*/
function shoutcast($in_host, $in_port = 8000, $in_sid = 1) {

global $phpEx;

// /*
// RETURN/s
// $array['CURRENTLISTENERS']
// $array['STREAMSTATUS']
// $array['PEAKLISTENERS']
// $array['MAXLISTENERS']
// $array['UNIQUELISTENERS']
// $array['AVERAGETIME']
// $array['SERVERGENRE']
// $array['SERVERURL']
// $array['SERVERTITLE']
// $array['SONGTITLE']
// $array['NEXTTITLE']
// $array['BITRATE']
// $array['VERSION']
// $array['CONTENT']
// */

include_once "core_Get_Functions_shoutcast2info.php";

$feed = new sc2info;

$feed->sc2info();

return $feed->getsc2info( $in_host, $in_port, $in_sid );

}

save the following as core_Get_Functions_shoutcast2info.php and replace _DP_CACHE_PAR with the tmp or cache dir you use to store junk files

Last edited by p0rt; 21st September 2011 at 02:55.
p0rt is offline   Reply With Quote
Old 21st September 2011, 02:56   #9
p0rt
Senior Member
 
Join Date: Nov 2010
Posts: 152
fixes cache not working, and long wait time while page loads if sc_serv is not running

Quote:
<?php

// +----------------------------------------------------------------------+
// | NULL-8X3-NUKE Version 1.2.4 |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html |
// +----------------------------------------------------------------------+
// | base: Allan Irvine <ai@ayrsoft.com> newsReader.php |
// +----------------------------------------------------------------------+
// | Author: p0rt |
// +----------------------------------------------------------------------+
// | Copyright: © 2011 p0r+ |
// +----------------------------------------------------------------------+

class sc2info {

var $content = array();
var $currentTag;
var $rss;
var $level;
var $startCollecting;
var $wantedElements;
var $cacheData;
var $ampReplace;

var $error;
var $sock;

var $sid = '1';
var $clients = '0';

function sc2info() {

$this->rss = xml_parser_create();

xml_set_object($this->rss, $this);
xml_parser_set_option($this->rss,XML_OPTION_CASE_FOLDING,1);
xml_set_element_handler($this->rss,_startElement,_endElement);
xml_set_character_data_handler($this->rss,_parseData);

$this->wantedElements['SHOUTCASTSERVER'] = array("CURRENTLISTENERS","PEAKLISTENERS","MAXLISTENERS","UNIQUELISTENERS","AVERAGETIME","SERVERGENRE","SERVERURL","SERVERTITLE","SONGTITLE","NEXTTITLE","STREAMHITS","STREAMSTATUS","STREAMPATH","BITRATE","CONTENT","VERSION");

}

function _startElement($t,$name,$attribs) {
$this->currentTag = $name;
}

function _endElement($t,$name) {
$this->startCollecting;
}

function _parseData($t,$cData) {

$cData = str_replace($this->ampReplace,"&",$cData);

if (eregi("CURRENTLISTENERS",$this->currentTag)) {

$this->content['CURRENTLISTENERS']=$cData;

} else if (eregi("STREAMSTATUS",$this->currentTag)) {

$this->content['STREAMSTATUS']=$cData;

} else if (eregi("PEAKLISTENERS",$this->currentTag)) {

$this->content['PEAKLISTENERS']=$cData;

} else if (eregi("MAXLISTENERS",$this->currentTag)) {

$this->content['MAXLISTENERS']=$cData;

} else if (eregi("UNIQUELISTENERS",$this->currentTag)) {

$this->content['UNIQUELISTENERS']=$cData;

} else if (eregi("AVERAGETIME",$this->currentTag)) {

$this->content['AVERAGETIME']=$cData;

} else if (eregi("SERVERGENRE",$this->currentTag)) {

$this->content['SERVERGENRE']=$cData;

} else if (eregi("SERVERURL",$this->currentTag)) {

$this->content['SERVERURL']=$cData;

} else if (eregi("SERVERTITLE",$this->currentTag)) {

$this->content['SERVERTITLE']=$cData;

} else if (eregi("SONGTITLE",$this->currentTag)) {

$this->content['SONGTITLE']=$cData;

} else if (eregi("NEXTTITLE",$this->currentTag)) {

$this->content['NEXTTITLE']=$cData;

} else if (eregi("BITRATE",$this->currentTag)) {

$this->content['BITRATE']=$cData;

} else if (eregi("VERSION",$this->currentTag)) {

$this->content['VERSION']=$cData;

} else if (eregi("CONTENT",$this->currentTag)) {

$this->content['CONTENT']=$cData;

}

}

function getsc2info($ip, $port, $sid, $css) {

$this->sid = $sid;

$source = "http://$ip:$port/stats?sid=$sid";

if ( !$this->sock = fsockopen($ip, $port, $this->error[0] , $this->error[1], 1) ) {

return array( 'STREAMSTATUS' => '0' );

}

fclose($this->sock);

$this->_readCacheData( $source );

if($this->_useCache($source)){

$cache = file( _DP_CACHE_PAR . md5($source) .".sidCache" );

} else {

$cache = file($source);

}

$file = implode("\n",$cache);
$this->ampReplace = md5(time());// quick fix, will need to address later on
$file = str_replace("&",$this->ampReplace,$file);// quick fix, will need to address later on

if( !xml_parse($this->rss,$file,1) ){

$ln = xml_get_current_line_number($this->rss);
$msg = xml_error_string(xml_get_error_code($this->rss));

return ("An XML error occurred on line $ln: $msg, cache file : ". _DP_CACHE_PAR . md5($source)."_sid". $this->sid .".sidCache" );

} else {

if(!$this->_useCache($source)){

$this->_writeCacheEntry($source);

}

return $this->content;

}
}

function _readCacheData( $source ) {

$file = file_get_contents( _DP_CACHE_PAR . md5($source)."_sid". $this->sid .".sidCache" );

if(strlen(trim($file))){
$this->cacheData = unserialize($file);
}

}

function _useCache($source) {

if($key = array_search($source,$this->cacheData)) {

$sec = explode(" ",$key);
if((time() - $sec[1])>(120)) {

return 0;

} else {

return 1;

}

} else {

return 0;

}
}

function _writeCacheEntry($source) {

$this->_updateCacheArray($source);

$fp = @fopen( _DP_CACHE_PAR . md5($source)."_sid". $this->sid .".sidCache", "w+" );
@fwrite( $fp, serialize($this->cacheData) );
@fclose( $fp );

$data = file($source);
$data = implode("\n",$data);

$fp = @fopen( _DP_CACHE_PAR . md5($source).".sidCache", "w+" );
@fwrite( $fp, $data );
@fclose( $fp );

}

function _updateCacheArray($source) {

if(is_array($this->cacheData) && in_array($source,$this->cacheData)) {

foreach($this->cacheData as $key=>$val) {

if(trim($val) == trim($source)) {

$key =microtime();

}

$new[$key]= $val;
}

$this->cacheData = $new;

} else {

$this->cacheData[microtime()] = $source;

}
}

}

?>
p0rt is offline   Reply With Quote
Reply
Go Back   Winamp & SHOUTcast Forums > SHOUTcast > SHOUTcast Site Design

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump