<?php

if('sco_ajax'==get_class($this)):
	$client_id 		=  $this->get_option('yahoo_app_id','',true);
	$client_secret 	=  $this->get_option('yahoo_app_secret','',true);
	//-----
	if(!class_exists('http_class'))			require( $path.'oauth-api/http.php');
	if(!class_exists('oauth_client_class'))	require( $path.'oauth-api/oauth_client.php');

	$client = new oauth_client_class;
	$client->debug = false;
	$client->debug_http = true;
	$client->oauth_version = '1.0a';
	$client->request_token_url = 'https://api.login.yahoo.com/oauth/v2/get_request_token';
	$client->dialog_url = 'https://api.login.yahoo.com/oauth/v2/request_auth';
	$client->access_token_url = 'https://api.login.yahoo.com/oauth/v2/get_token';
	$client->authorization_header = false;

	$client->redirect_uri 	= $redirect_uri;
	$client->client_id 		= $client_id;
	$client->client_secret 	= $client_secret;

	if(strlen($client->client_id) == 0
	|| strlen($client->client_secret) == 0)
		$this->error_message( __('Settings error, network api credentials not set.','rhsco') );

	/* API permissions
	 */
	if(($success = $client->Initialize())){
		if(($success = $client->Process())){
			if(strlen($client->access_token)){
				$success = $client->CallAPI(
					'https://query.yahooapis.com/v1/yql', 
					'GET', array(
						'q'=>'select * from social.profile where guid=me',
						'format'=>'json'
					), array('FailOnAccessError'=>true), $user);
			}
		}
		$success = $client->Finalize($success);
	}
	if($client->exit)
		exit;
	if($success){
		$user_short = $user->query->results->profile;
		$user = (object)array(
			'id'		=> $user_short->guid,
			'username' 	=> substr($user_short->emails->handle, 0, strpos($user_short->emails->handle,'@')),
			'name'		=> $user_short->givenName.' '.$user_short->familyName,
			'first_name'=> $user_short->givenName,
			'last_name' => $user_short->familyName,
			'email' 	=> $user_short->emails->handle,
			'link'		=> $user_short->profileUrl
		);
		$SCO_Profile = new SCO_Profile( $network, (array)$user, $this->cb_get_option, true, true );
	}else{
		$this->error_message( sprintf( __('Error authenticating with social network: %s','rhsco'), HtmlSpecialChars($client->error) ) );
	}
else:
	$this->error_message( __('Settings error, cannot link directly to this file.', 'rhsco') );
endif; 
 

?>