View Single Post
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