Location: PHPKode > scripts > NewHaze API > newhaze-api.php
<?php
//////////////////////////////
// New Haze API Version 0.3 //
// PHP Client Version 0.5.1 //
//        11/04/2010        //
//       cURL Support       //
//      OAuth Protocol      //
//      © NewHaze.com       //
//  Info: devs.newhaze.com  //
//////////////////////////////
class NewHaze {
	private $consumer_key;
	private $consumer_secret;
	private $access_token;
	private $access_token_secret;
	private $current_user=0;
	private $last_nonce_id=0;
	private $version='0.5.1';
	private $api_version='0.3';
	public function __construct ($consumer_key, $consumer_secret) {
		$this->consumer_key = $consumer_key;
		$this->consumer_secret = $consumer_secret;
	}
	///////////////////URLs/////////////////////////////////
	function authorizeURL($token){
		return'http://www.newhaze.com/oauth/authorize.php?auth_token='.$token;
	}
	function permissionsURL($token){
		return'http://www.newhaze.com/oauth/permissions.php?token='.$token;
	}
	///////////////////OAuth Functions/////////////////////////////////
	function auth_setCurrentUser($access_token,$access_token_secret) {
		if(strlen($access_token)==32 and strlen($access_token_secret)==32) {
			$this->access_token = $access_token;
			$this->access_token_secret = $access_token_secret;
			return true;
		}else{
			return false;
		}
	}
	function getLoggedInUser() {
		if(strlen($this->access_token)==32 and strlen($this->access_token_secret)==32) {
			if($this->current_user>0) {
				return $this->current_user;
			}else{
				$response = $this->call_method('users.getCurrentUid',array());
				$this->current_user=$response->uid;
				return $response->uid;
			}
		}else{
			return 0;
		}
	}
	function auth_permissionToken($callback_url,$permissions=array()) {
		$send=array('callback'=>$callback_url);
		foreach($permissions as $permission){
			$send[$permission]=1;
		}
		return $this->call_method('auth.permissionToken',$send);
	}
	function auth_requestToken($callback_url) {
		return $this->call_method('auth.requestToken',array('callback'=>$callback_url));
	}
	function auth_requestAccessToken($auth_token,$auth_verifier,$auth_secret) {
		$response=$this->call_method('auth.requestAccessToken',array('auth_token'=>$auth_token,'auth_verifier'=>$auth_verifier,'auth_signature'=>md5($auth_token.$auth_secret)));
		if($this->auth_setCurrentUser($response->access_token,$response->access_token_secret)) {
			return $response;
		}else{
			return false;
		}
		
	}
	///////////////////Advert Functions/////////////////////////////////
	public function adverts_getSiteStats($start,$finish) {
		return $this->call_method('adverts.getSiteStats',array('start'=>$start,'finish'=>$finish));
	}
	///////////////////Avatar Functions/////////////////////////////////
	public function avatars_add($url,$name,$private=0) {
		return $this->call_method('avatars.add',array('url'=>$url,'name'=>$name,'private'=>$private));
	}
	public function avatars_edit($id,$name,$private) {
		return $this->call_method('avatars.edit',array('id'=>$id,'name'=>$name,'private'=>$private));
	}
	public function avatars_get($term='',$count=10,$start=0,$orderby='name',$order=1) {
		return $this->call_method('avatars.get',array('term'=>$term,'count'=>$count,'start'=>$start,'orderby'=>$orderby,'order'=>$order));
	}
	///////////////////Category Functions/////////////////////////////////
	public function categories_add($name) {
		return $this->call_method('categories.add',array('name'=>$name));
	}
	public function categories_edit($cid, $name) {
		return $this->call_method('categories.edit',array('cid'=>$cid,'name'=>$name));
	}
	public function categories_get($count=50, $start=0, $orderby='name', $order=1) {
		return $this->call_method('categories.get',array('count'=>$count,'start'=>$start,'orderby'=>$orderby,'order'=>$order));
	}
	public function categories_getInfo($cid) {
		return $this->call_method('categories.getInfo',array('cid'=>$cid));
	}
	public function categories_remove($cid) {
		return $this->call_method('categories.remove',array('cid'=>$cid));
	}
	///////////////////Comment Functions/////////////////////////////////
	public function comments_add($gid=0, $comment='', $rating=0) {
		return $this->call_method('comments.add',array('gid'=>$gid,'comment'=>$comment,'rating'=>$rating));
	}
	public function comments_get($uid=0, $gid=0, $count=50, $start=0, $order=1) {
		return $this->call_method('comments.get',array('uid'=>$uid,'gid'=>$gid,'count'=>$count,'start'=>$start,'orderby'=>$orderby,'order'=>$order));
	}
	public function comments_remove($cid=0) {
		return $this->call_method('comments.remove',array('cid'=>$cid));
	}
	///////////////////Favorites Functions/////////////////////////////////
	public function favorites_add($gid) {
		return $this->call_method('favorites.add',array('gid'=>$gid));
	}
	public function favorites_isFavorite($gid) {
		return $this->call_method('favorites.isFavorite',array('gid'=>$gid));
	}
	public function favorites_get($uid=false,$search='', $cid=0, $count=50, $start=0, $orderby='name', $order=1) {
		return $this->call_method('favorites.get',array('uid'=>$uid,'term'=>$search,'c'=>$cid,'count'=>$count,'start'=>$start,'orderby'=>$orderby,'order'=>$order));
	}
	public function favorites_remove($gid) {
		return $this->call_method('favorites.remove',array('gid'=>$gid));
	}
	///////////////////Feed Functions/////////////////////////////////
	public function feed_addProfileComment($uid,$comment) {
		return $this->call_method('feed.addProfileComment',array('uid'=>$uid,'comment'=>$comment));
	}
	public function feed_get($self=true,$friends=true,$num=20) {
		$send=array();
		$send['friends']='false';
		$send['self']='false';
		if($self===true) {
			$send['self']='true';
		}
		if($friends===true) {
			$send['friends']='true';
		}
		$send['num']=$num;
		return $this->call_method('feed.get',$send);
	}
	public function feed_removePost($pid) {
		return $this->call_method('feed.removePost',array('pid'=>$pid));
	}
	///////////////////Game Functions/////////////////////////////////
	public function friends_add($uid) {
		return $this->call_method('friends.add',array('uid'=>$uid));
	}
	public function friends_get($uid) {
		return $this->call_method('friends.get',array('uid'=>$uid));
	}
	public function friends_isFriend($uid) {
		return $this->call_method('friends.isFriend',array('uid'=>$uid));
	}
	public function friends_remove($uid) {
		return $this->call_method('friends.remove',array('uid'=>$uid));
	}
	///////////////////Game Functions/////////////////////////////////
	public function games_add($gid, $cid) {
		return $this->call_method('games.add',array('gid'=>$gid,'cid'=>$cid));
	}
	public function games_addImages($gid, $url) {
		return $this->call_method('games.addImages',array('gid'=>$gid,'url'=>$url));
	}
	public function games_get($search='', $cid=0, $count=50, $start=0, $orderby='name', $order=1) {
		return $this->call_method('games.get',array('term'=>$search,'c'=>$cid,'count'=>$count,'start'=>$start,'orderby'=>$orderby,'order'=>$order));
	}
	public function games_getInfo($gid) {
		return $this->call_method('games.getInfo',array('gid'=>$gid));
	}
	public function games_getLibrary($search='', $cid=0, $count=50, $start=0, $orderby='name', $order=1) {
		return $this->call_method('games.getLibrary',array('term'=>$search,'c'=>$cid,'count'=>$count,'start'=>$start,'orderby'=>$orderby,'order'=>$order));
	}
	public function games_remove($gid) {
		return $this->call_method('games.remove',array('gid'=>$gid));
	}
	///////////////////Newsletter Functions/////////////////////////////////
	public function newsletters_get() {
		return $this->call_method('newsletters.get',array());
	}
	public function newsletters_send($subject,$body) {
		return $this->call_method('newsletters.send',array('subject'=>$subject,'html'=>$body));
	}
	///////////////////Score Functions/////////////////////////////////
	public function scores_get($uid=0, $gid=0, $bid='', $count=50, $start=0, $order=1) {
		return $this->call_method('scores.get',array('uid'=>$uid,'gid'=>$gid,'bid'=>$bid,'count'=>$count,'start'=>$start,'order'=>$order));
	}
	///////////////////Site Functions/////////////////////////////////
	public function sites_getInfo() {
		return $this->call_method('sites.getInfo',array());
	}
	public function sites_setInfo($name='',$url='',$logo='',$newsletter='',$urls=array()) {
		$send=array('name'=>$name,'url'=>$url,'logo'=>$logo,'newsletter'=>$newsletter);
		foreach($urls as $t=>$s) {
			$send['url_'.$t]=$s;
		}
		return $this->call_method('sites.setInfo',$send);
	}
	///////////////////User Functions/////////////////////////////////
	public function users_get($count=50, $start=0, $orderby='first_name', $order=1) {
		return $this->call_method('users.get',array('count'=>$count,'start'=>$start,'orderby'=>$orderby,'order'=>$order));
	}
	public function users_getInfo($uid) {
		return $this->call_method('users.getInfo',array('uid'=>$uid));
	}
	public function users_getPermissions($uid) {
		return $this->call_method('users.getPermissions',array('uid'=>$uid));
	}
	public function users_setInfo($location,$avatar) {
		return $this->call_method('users.setInfo',array('location'=>$location,'avatar'=>$avatar));
	}
	///////////////////Private Functions/////////////////////////////////
	private function finalize_params($method, &$params) {
		$params['method'] = $method;
		$params['consumer_key'] = $this->consumer_key;
		$params['v'] = $this->api_version;
		$params['format'] = 'json';
		$params['nonce'] = microtime(true);
		if($params['nonce']<=$this->last_nonce_id) {
			$params['nonce']=$this->last_nonce_id + 0.001;
		}
		$params['timestamp'] = time();
		if($this->access_token!=null and $this->access_token_secret!=null) {
			$params['access_token'] = $this->access_token;
			$params['access_signature'] = md5($params['nonce'].$this->access_token_secret);
		}
	}
	private function create_post_string($params) {
		$post_params = array();
		foreach ($params as $key => &$val) {
			$post_params[] = $key.'='.$val;
		}
		return implode('&', $post_params);
	}
	private function call_method($method,$params=array()) {
		$this->finalize_params($method, $params);
		$post_string = $this->create_post_string($params);
		$post_string.="&sig=".md5($post_string . $this->consumer_secret);
		if(function_exists('curl_init')) {
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, 'http://connect.newhaze.com/api/');
				curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($ch, CURLOPT_USERAGENT, 'NewHaze API PHP5 Client '.$this->version.' (curl) ' . phpversion());
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
				curl_setopt($ch, CURLOPT_TIMEOUT, 30);
				$result = curl_exec($ch);
				curl_close($ch);
				return json_decode($result);
		}else{
			$result = $this->non_curl($post_string);
			return json_decode($result);
		}
		$this->last_nonce_id=$params['nonce'];
	}
	private function non_curl($string) {
		$context_id = stream_context_create(array('http' =>array('method' => 'POST','user_agent' => 'NewHaze API PHP5 Client '.$this->version.' (non-curl) ' . phpversion(),'header' => 'Content-Type: application/x-www-form-urlencoded' . "\r\n" .'Content-Length: ' . strlen($string),'content' => $string)));
		$sock = fopen('http://connect.newhaze.com/api/', 'r', false, $context_id);
		$result = '';
		if($sock) {
			while(!feof($sock)) {
				$result .= fgets($sock, 4096);
			}
			fclose($sock);
		}
		return $result;
	}
}
?>
Return current item: NewHaze API