<?php
/*
 * Helper class for the Rapleaf Person Book API
 * 		(http://www.rapleaf.com/apidoc/v2/abook)
 * Gets the XML response from the Rapleaf server and parses it into a PHP object.
 * Created by Abhishek Nagaraj. Inspired by the Address Book API by Mimi Sun (http://mimisun.net/rapleaf/index.html)
 *
 * Usage: 
 * 1. $profile = new RapleafProfile($_POST['api_key']) to initialize
 *  2. $result = $profile->getData($_POST['email']) to query the API for a details
 *  See the readme.txt for details.
 *  
 * 04/04/2008
 *
 */

class RapleafProfile{
	var $api_key;
	var $url;
	var $status; 
	
	
	function RapleafProfile($api_key, $url= 'http://api.rapleaf.com/v2/person/') {
		$this->api_key = $api_key;
		$this->url = $url;	
		$this->email_id=$email_id;
	}	
	
	# The main function which is called to get all information
	function getData($email) {
		# assemble post_data string
		$base_url = $this->url . $email. '?api_key=' . $this->api_key . '&i=bmr';
		$response = $this->getRequest($base_url);

		# the return structure
		$result = array(
			'status'   => '',  # HTTP status code returned by the server
			'error'    => '',  # error message if there are any
			'basics' => array(),
			'memberships-primary' => array(),	
			'memberships-supplemental' => array(),		
			'reputation' => array(),				
		);
		
		$result['status'] = $this->status;

		if ($this->status == '200') { #OK
			$result['basics'] = $this->getBasics($response);		
			$result['memberships-primary'] = $this->getMemberships($response,'primary');	
			$result['memberships-supplemental'] = $this->getMemberships($response,'supplemental');	
			$result['reputation'] = $this->getReputation($response);				
		}elseif ($this->status == '201') {
			$result['error'] = 'This person has now been created. Check back shortly and we should have data.'.$response;
		}  
		elseif ($this->status == '400') {
			$result['error'] = 'Invalid email address.'.$response;
		} elseif ($this->status == '401') {
			$result['error'] = 'API key was not provided or is invalid.';
		} elseif ($this->status == '403') {
			$result['error'] = 'Your daily query limit has been exceeded. The default limit is 4,000.';
		} elseif ($this->status == '500') {
			$result['error'] = 'There was an unexpected error on our server. This should be very rare and if you see it please contact developer@rapleaf.com.';
		} 
		return $result;
	}	
	
	# Parse the xml response text to get basic details
	function getBasics($str) {
		$xml = simplexml_load_string($str);
		$result = array();
		$universities = array();
		$occupations = array();
		
		if(!is_null($xml->basics->universities->university)){
			foreach($xml->basics->universities->university as $university){
				$universities[] = strval($university);
			}
		}
		
		if(!is_null($xml->basics->occupations->occupation)){
			$count = 0;
			foreach($xml->basics->occupations->occupation as $occupation){
				
				if($occupation['job_title']) { 
					$occupations[$count]['job_title'] = strval($occupation['job_title']);
				} else {
					$occupations[$count]['job_title'] = NULL;
				}
				if($occupation['company']) {
					$occupations[$count]['company'] = strval($occupation['company']);
				} else {
					$occupations[$count]['company'] = NULL;
				}
				$count++;
			}
		}

		$result= array(
				'name' => $xml->basics->name, 
				'age' => $xml->basics->age,
				'gender' => $xml->basics->gender,
				'location' => $xml->basics->location,
				'earliest_known_activity' => $xml->basics->earliest_known_activity,
				'latest_known_activity' => $xml->basics->latest_known_activity,
				'num_friends' => $xml->basics->num_friends,		
				'universities' => $universities,
				'occupations' => $occupations,
				);

		return $result;
	}

	
	
	#Parse the xml response text to get Primary/Supplemental Membership details
	function getMemberships($str,$val) {
		$xml = simplexml_load_string($str);
		$result = array();

		if(!is_null($xml->memberships->$val->membership)){
			$count = 0;	
			foreach($xml->memberships->$val->membership as $membership){
						
				$result[$count]['site'] = strval($membership['site']);
				$result[$count]['exists'] = strval($membership['exists']);
				$result[$count]['profile_url'] = strval($membership['profile_url']);
				$count++;
			}

		}
		return $result;
	}		
	
	# Parse the xml response to get the Reputation details
	function getReputation($str) {
		$xml = simplexml_load_string($str);
		$result = array();
		$badges = array();
		
		if(!is_null($xml->reputation->badges->badge)){			
			foreach($xml->reputation->badges->badge as $badge){
					$badges[] = strval($badge);				
			}
		}
		
		$result= array(
				'score' => strval($xml->reputation->score), 
				'commerce_score' => strval($xml->reputation->commerce_score),
				'percent_positive' => strval($xml->reputation->percent_positive),
				'rapleaf_profile_url' => strval($xml->reputation->rapleaf_profile_url),
				'badges' => $badges,
				);
	
		return $result;
	}
		
	# cURL function to fetch the XML schema
	function getRequest( $url ){
	$ch      = curl_init( $url );
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
	$content = curl_exec( $ch );
	$this->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close( $ch );
	return $content;
	}	
}

?> 





