<?php

if('sco_ajax'==get_class($this)):
	$client_id 		=  $this->get_option('pinterest_app_id','',true);
	$client_secret 	=  $this->get_option('pinterest_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 = '2.0';
	$client->dialog_url = 'https://api.pinterest.com/oauth?client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&scope={SCOPE}&state={STATE}&response_type=code';
	$client->access_token_url = 'https://api.pinterest.com/v1/oauth/token';
	//--
	$client->redirect_uri	= str_replace('http', 'https', $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
	 */
	$client->scope = 'read_public';
	if(($success = $client->Initialize())){
		if(($success = $client->Process())){
			if(strlen($client->access_token)){
				$success = $client->CallAPI(
					'https://api.pinterest.com/v1/me/', 
					'GET', array(), array('FailOnAccessError'=>true), $user);
			}
		}
		$success = $client->Finalize($success);
	}
	if($client->exit)
		exit;
	if($success){
		extract((array)$user->data);
		$user = (object)array(
			'id'		=> $id,
			'username' 	=> (!empty($last_name)?strtolower(substr($first_name, 0, 1).'.'.$last_name):strtolower($first_name)),
			'name'		=> $first_name.' '.$last_name,
			'first_name'=> $first_name,
			'last_name' => $last_name,
			'link'		=> $url
		);

		$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; 
 

?>