<?php

/*
Examples:

------------
To make API calls:
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}

*/

require_once "Facebook/base_facebook.php";

/**
* Extends the BaseFacebook class with the intent of using
* PHP sessions to store user ids and access tokens.
*/
class Atmail_Provider_SocialNetwork_FacebookInterface extends BaseFacebook
{

	protected static $kSupportedKeys = array('state', 'code', 'access_token', 'user_id');



	/**
	* Provides the implementations of the inherited abstract
	* methods.  The implementation uses PHP sessions to maintain
	* a store for authorization codes, user ids, CSRF states, and
	* access tokens.
	*/
	protected function setPersistentData($key, $value) 
	{
		if (!in_array($key, self::$kSupportedKeys)) 
		{
			self::errorLog('Unsupported key passed to setPersistentData.');
			return;
		}

		$session_var_name = $this->constructSessionVariableName($key);
		$this->_session[$session_var_name] = $value;
	}

	protected function getPersistentData($key, $default = false) {

		if (!in_array($key, self::$kSupportedKeys)) 
		{
			self::errorLog('Unsupported key passed to getPersistentData.');
			return $default;
		}

		$session_var_name = $this->constructSessionVariableName($key);
		return isset($this->_session[$session_var_name]) ?
			$this->_session[$session_var_name] : $default;
	}

	protected function clearPersistentData($key) {
		if (!in_array($key, self::$kSupportedKeys)) {
			self::errorLog('Unsupported key passed to clearPersistentData.');
			return;
		}

		$session_var_name = $this->constructSessionVariableName($key);
		unset($this->_session[$session_var_name]);
	}

	protected function clearAllPersistentData() {
		foreach (self::$kSupportedKeys as $key) {
			$this->clearPersistentData($key);
		}
	}

	protected function constructSessionVariableName($key) 
	{
		return implode('_', array('fb', $this->getAppId(), $key));
	}
	
	protected function errorLogold1($message)
	{
		
		Zend_Registry::get('log')->debug( __METHOD__ . ' #' . __LINE__ . ": Error: " . $message);
		
		
	}
 
}
