Location: PHPKode > projects > Online Last.FM Player > class/lastfm.class.php
<?php

global $lcs_config_lastFm;
$lcs_config_lastFm = array();

$lcs_config_lastFm['version'] = '1.2';
$lcs_config_lastFm['platform'] = 'online';
$lcs_config_lastFm['host'] = 'ws.audioscrobbler.com';
$lcs_config_lastFm['port'] = 80;
$lcs_config_lastFm['handshake_vars'] = array();
$lcs_config_lastFm['handshake_vars'][0] = 'session';
$lcs_config_lastFm['handshake_vars'][1] = 'stream_url';
$lcs_config_lastFm['handshake_vars'][2] = 'subscriber';
$lcs_config_lastFm['handshake_vars'][3] = 'framehack';
$lcs_config_lastFm['handshake_vars'][4] = 'base_url';
$lcs_config_lastFm['handshake_vars'][5] = 'base_path';
$lcs_config_lastFm['meta_vars'] = array();
$lcs_config_lastFm['meta_vars'][0] = 'streaming';
$lcs_config_lastFm['meta_vars'][1] = 'discovery';
$lcs_config_lastFm['meta_vars'][2] = 'station';
$lcs_config_lastFm['meta_vars'][3] = 'station_url';
$lcs_config_lastFm['meta_vars'][4] = 'stationfeed';
$lcs_config_lastFm['meta_vars'][5] = 'stationfeed_url';
$lcs_config_lastFm['meta_vars'][6] = 'artist';
$lcs_config_lastFm['meta_vars'][7] = 'artist_url';
$lcs_config_lastFm['meta_vars'][8] = 'track';
$lcs_config_lastFm['meta_vars'][9] = 'track_url';
$lcs_config_lastFm['meta_vars'][10] = 'album';
$lcs_config_lastFm['meta_vars'][11] = 'album_url';
$lcs_config_lastFm['meta_vars'][12] = 'albumcover_small';
$lcs_config_lastFm['meta_vars'][13] = 'albumcover_medium';
$lcs_config_lastFm['meta_vars'][14] = 'albumcover_large';
$lcs_config_lastFm['meta_vars'][15] = 'trackduration';
$lcs_config_lastFm['meta_vars'][16] = 'trackprogress';
$lcs_config_lastFm['meta_vars'][17] = 'radiomode';
$lcs_config_lastFm['meta_vars'][18] = 'recordtoprofile';

class lcs_lastFm {
	var $active;
	var $username;
	var $password;
	var $settings;
	var $connection;
	var $info;
	
	function lcs_lastFm () {
		$this->settings = $GLOBALS['lcs_config_lastFm'];
		$this->active = 0;
	}
	
	function connect ($username, $password) {
		$this->username = $username;
		$this->password = md5($password);
		$this->info = array();
		$classSocket = new lcs_fsocket($this->settings['host'], $this->settings['port']);
		$this->active = 1;
		$out = "GET /radio/handshake.php?version=".$this->settings['version']."&platform=".$this->settings['platform']."&username=".$this->username."&passwordmd5=".$this->password." HTTP/1.0\r\n";
   		$out .= "Host: ".$this->settings['host']."\r\n";
   		$out .= "\r\n";
		$resp = $classSocket->send($out);
		$respExp = explode("\n", $resp);
		foreach ( $respExp as $line ) {
			// echo $line.'<br />';
			$match = 0;
			foreach ( $this->settings['handshake_vars'] as $hsVar ) {
				// echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$hsVar.' - ';
				if ( substr($line, 0, strlen($hsVar)) == $hsVar ) {
					// echo 1;
					$match = 1;
					$matchArray = array();
					$matchArray['varName'] = $hsVar;
					$matchArray['varLen'] = strlen($hsVar);
				}
				// else {
					// echo 0;
				// }
				// echo '<br />';
			}
			
			if ( $match == 1 ) {
				$content = substr($line, ($matchArray['varLen']+1));
				$this->info[$matchArray['varName']] = $content;
			}
		}
		
		$classSocket->close();
		$this->active = 0;
		
		unset($username);
		unset($password);
		unset($classSocket);
		unset($resp);
		unset($respExp);
		unset($out);
		unset($match);
		unset($matchArray);
		unset($content);
		
		if ( !isset($this->info['session']) || $this->info['session'] == 'FAILED' ) {
			return FALSE;
		}
		else {
			$_SESSION['streaming'] = 1;
			$_SESSION['info'] = $this->info;
			return TRUE;
		}
	}
	
	function command ($command) {
		if ( $command == 'skip' || $command == 'love' || $command == 'ban' ) {
			$classSocket = new lcs_fsocket($this->settings['host'], $this->settings['port']);
			$out = "GET ".$this->info['base_path']."/handshake.php?version=".$this->settings['version']."&platform=".$this->settings['platform']."&username=lotrgamemast&passwordmd5=e0f130db8df52d2bd71fd3a4808686da HTTP/1.0\r\n";
	   		$out .= "Host: ".$this->settings['host']."\r\n";
	   		$out .= "\r\n";
			$resp = $classSocket->send($out);
			
			$classSocket->close();
			$this->active = 0;
		}
	}
	
	function getMeta () {
		$return = array();
		$classSocket = new lcs_fsocket($this->settings['host'], $this->settings['port']);
		$this->active = 1;
		$out = "GET ".$this->info['base_path']."/np.php?session=".$this->info['session']." HTTP/1.0\r\n";
	   	$out .= "Host: ".$this->settings['host']."\r\n";
	   	$out .= "\r\n";
		$resp = $classSocket->send($out);
		$respExp = explode("\n", $resp);
		foreach ( $respExp as $line ) {
			// echo $line.'<br />';
			$match = 0;
			foreach ( $this->settings['meta_vars'] as $metaVar ) {
				// echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$metaVar.' - ';
				if ( substr($line, 0, strlen($metaVar)) == $metaVar ) {
					// echo 1;
					$match = 1;
					$matchArray = array();
					$matchArray['varName'] = $metaVar;
					$matchArray['varLen'] = strlen($metaVar);
				}
				// else {
					// echo 0;
				// }
				// echo '<br />';
			}
			
			if ( $match == 1 ) {
				$content = substr($line, ($matchArray['varLen']+1));
				$return[$matchArray['varName']] = $content;
			}
		}
		
		$classSocket->close();
		$this->active = 0;
		
		unset($classSocket);
		unset($out);
		unset($resp);
		unset($respExp);
		unset($match);
		unset($matchArray);
		unset($content);
		
		if ( !isset($return['streaming']) || $return['streaming'] == 'false' ) {
			return FALSE;
		}
		else {
			return $return;
		}
	}
	
	function changeStation ($url) {
		$classSocket = new lcs_fsocket($this->settings['host'], $this->settings['port']);
		$this->active = 1;
		$out = "GET ".$this->info['base_path']."/adjust.php?session=".$this->info['session']."&url=".$url." HTTP/1.0\r\n";
	   	$out .= "Host: ".$this->settings['host']."\r\n";
	   	$out .= "\r\n";
		$resp = $classSocket->send($out);
		$respExp = explode("\n", $resp);
		$match = 0;
		foreach ( $respExp as $line ) {
			//echo $line.' - ';
			if ( substr($line, 0, '8') == 'response' ) {
				//echo 1;
				$match = 1;
			}
			//else {
				//echo 0;
			//}
			//echo '<br />';
			
			if ( $match == 1 ) {
				$content = substr($line, 9);
			}
		}
			
		$classSocket->close();
		$this->active = 0;
		
		if ( !isset($content) || $content == 'FAILED' ) {
			return FALSE;
		}
		else {
			return TRUE;
		}		
	}
}

?>
Return current item: Online Last.FM Player