All pastes #201245 Raw Edit

Stuff

public php v1 · immutable
#201245 ·published 2006-10-13 18:31 UTC
rendered paste body
<?php//version 1.01/*This class is written by coalado.*/class SimBrowse  {	function SimBrowse(){	$this->info=array();	$this->info["User-Agent"]="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7";	$this->info["Accept"]="*/*";	$this->info["Accept-Encoding"]="*";	$this->info["Accept-Language"]="*";		$this->info["Accept-Charset"]="ISO-8859-1,utf-8;q=0.7,*;q=0.7";	$this->info["Keep-Alive"]=30;	$this->info["Connection"]="close";		$this->requestType="GET";	$this->newLine="\r\n";	$this->timeout=15;	$this->socketTimeout=15;	$this->debug=0;	$this->connected=0;	}		//setter function for the header info	function changeHeaderInfo($name,$info){		$this->info[$name]=$info;		return true;		}		// requesttype (POST,GET,OPTIONS etc	function setRequest($type){		$this->requestType=$type;		}		//return the header 	function getOutgoingHeader($host,$path){		$this->debug("Create Header for ".$host." and path ".$path);		$header=$this->requestType." ".$path." HTTP/1.1".$this->newLine;		$header.="Host: ".$host.$this->newLine;		foreach ($this->info as $name => $value) {			$header.=$name.": ".$value.$this->newLine;		}		$header.=$this->newLine;			return $header;	}		// reads the icomming header	function getHeader($fp){		if(!$this->fp){			$this->debug("Cannot read header. No connection.");			return false;		}		$header = '';		do {			$line = chop(fgets($fp));			$header .= $line."\n";					} while (!empty($line) and !@feof($fp));		return $header;		}	//parsing	function parseHeader($str){		$result=array();		preg_match('~^HTTP/1\.\d (?P<status>\d+)~', $str, $matches);		$result["status"]=$matches['status'];			$str=explode("\n",$str);				$x=0;		for($i=0;$i<count($str);$i++){				$split=strpos($str[$i],":");			if(!$split){				$result[$x]=$str[$i];				$x++;			}else{				$result[substr($str[$i],0,$split)]=trim(chop(substr($str[$i],$split+1)));			}		}		return $result;	}	function debug($str){		if($this->debug){			echo "<pre>\n";			print_r($str);			echo "</pre>\n";		}	}	// Redirect if needed	function redirect(){		$this->debug("Redirect to ".$this->incommingHeader["Location"]);		return $this->connect($this->incommingHeader["Location"]);		}	//connect to an url and send/receive headers. follow redirects as far as possible	function connect($url){		if($this->timeout)set_time_limit($this->timeout);		$this->url=$url;		$this->debug(" Got url: ".$url);		$parsedurl = parse_url($url);+		$this->debug($parsedurl);		$host = $parsedurl['host'];		$path = empty($parsedurl['path']) ? '/' : $path = $parsedurl['path'];		if (!empty($parsedurl['query'])){			$path .= '?'.$parsedurl['query'];		}		$port = empty($parsedurl['port']) ? 80 : $port = $parsedurl['port'];		$outgoingHeader= $this->getOutgoingHeader($host,$path);			$this->debug("Connect to: ".$host.":".$port." Timeout: ".$this->socketTimeout);		$fp = fsockopen ($host, $port, $errno, $errstr, $this->socketTimeout);			$this->fp=$fp;		if (!$fp) {			$this->debug($errstr." (".$errno.")");			return false;			$this->connected=0;		}			$this->debug($outgoingHeader);		fputs ($fp, $outgoingHeader);			$incommingHeader=$this->getHeader($fp);		$this->debug($incommingHeader);		$incommingHeader=$this->parseHeader($incommingHeader);		$this->incommingHeader=$incommingHeader;		if($this->incommingHeader["Location"]){			return $this->redirect();		}		$this->connected=1;		return $incommingHeader;	}	// reads data.	function getData($bytes=-1){		if(!$this->connected || !$this->fp){			$this->debug("Cannot read data. no Connection");			return false;		}				$result = '';		if($bytes>0){			$this->debug("Read data: ".$bytes." Bytes");			while(strlen($result)<$bytes){				$result .= fread($this->fp,$bytes-strlen($result));				}				}else{			$this->debug("Read data: complete");			while (!@feof($this->fp)) {				$result .= fread($this->fp,1024);										}		}		$this->data=$result;		return $this->data;		}	function closeConnection(){		fclose($this->fp);		}}?><?require_once('./class.simBrowse.php');function debug($str){//	echo "<pre>\n";//	print_r($str);//echo "</pre>\n";}//after 15 seconds without an keepaive request the script stops$keepalive=15;if($_GET["action"]=="keepalive"|| !$_GET["id"]){	// read the metainfo file and pass contents to flash	if($_GET["id"]){			$id=$_GET["id"];	}else{		$id=uniqid('');	}	@mkdir("./idfiles");	@unlink("./idfiles/.".$id);	$fp=fopen("./idfiles/.".$id,"w+");	fputs($fp,time());	fclose($fp);	echo "&result=".$id."&";	readfile("metaData".$id.".txt");}elseif($_GET["action"]=="stream"){	// SimBrowse is a class to send headerinfo etc easiely	$sp=new SimBrowse();	// Tell the shoutcast server that its winamp that is connecting	$sp->changeHeaderInfo("User-Agent","Winamp");	//request metainfos	$sp->changeHeaderInfo("Icy-MetaData","1");	$sp->debug=0;	$sp->timeout=0;	//get the stream header (SimBrowse follows redirect Headers by itself. radio stations use redirect to share the traffic under multiple servers	$incommingHeader=$sp->connect($_GET["stream"]);		//make flash think its an mp3 file	$nl="\r\n";	$header="HTTP/1.1 200 OK".$nl;	$header.="Notice:This is a php-stream".$nl;	$header.="Content-Type: audio/mpeg".$nl;	//content length for some browsers	$header.="Content-Length: 99999999999".$nl;	while(1){		//Clearcache that fileatime works fine		clearstatcache();		//break the script if flash does not send the keep alive request		if((time()-fileatime("./idfiles/.".$_GET["id"]))>$keepalive){			debug("Flash Verbindung abgebrochen : ".(time()-filectime("./idfiles/.".$_GET["id"])));			@unlink("./metaData".$_GET["id"].".txt");			@unlink("./idfiles/.".$_GET["id"]);			break;		}		//set Timelimit. the script must nt abort		set_time_limit(10);		//read the mp3 block		$mp3Block=$sp->getData($incommingHeader["icy-metaint"]);		//read the metasize block. this bytes indicates how big the following metatag is.		$metaSizeByte=ord($sp->getData(1));				if($metaSizeByte){			//metasize found. read it			$metadata=explode(";",$sp->getData($metaSizeByte*16));			//parse the metatag and save it together with some incomming headerinfos in the metatag file.			$fp=fopen("metaData".$_GET["id"].".txt","w+");			for($i=0;$i<count($metadata);$i++){				$entry=explode("=",$metadata[$i]);								if($entry[0] && $entry[1])fputs($fp,"\n&".$entry[0]."=".$entry[1]."&");			}					fputs($fp,"\n&"."icy-name"."=".$incommingHeader ["icy-name"]."&");			fputs($fp,"\n&"."icy-genre"."=".$incommingHeader ["icy-genre"]."&");			fputs($fp,"\n&"."icy-br"."=".$incommingHeader ["icy-br"]."&");					fclose($fp);				}		echo $mp3Block;		}}?>