<commit_before><?php defined('SYSPATH') OR die('No direct script access.');

/**
 * Facebook service adapter
 * requires 'auth' configuration array with apiId and secret
 * 
 * @package    OpenBuildings/services-manager
 * @author     <NAME>
 * @copyright  (c) 2012 OpenBuildings Inc.
 * @license    http://creativecommons.org/licenses/by-sa/3.0/legalcode
 */
abstract class Kohana_Service_Facebook extends Service implements Service_Type_Php, Service_Type_Javascript
{
	public $_facebook;
	public $_user_id;
	public $_user_data;
	public $_permissions;
	public $_meta = array();

	public function init()
	{
		$this->_meta['fb:app_id'] = Arr::path($this->_config, 'auth.appId');
		if ($admins = Arr::get($this->_config, 'admins'))
		{
			$this->_meta['fb:admins'] = $admins;
		}
	}

	public function og_post($action, $name, $url)
	{
		if ( ! $this->initialized() OR ! $this->og_enabled())
			return NULL;

		if ( ! Valid::url($url))
			throw new Kohana_Exception("URL :url passed to facebook is not valid", array(':url' => $url));

		if ($this->og_namespace())
		{
			$action = $this->og_namespace().':'.$action;
		}

		return $this->api("/me/{$action}", 'POST', array($name => $url));
	}

	public function delete($opengraph_id)
	{
		return $this->api('/'.$opengraph_id, 'DELETE');
	}

	/**
	 * Perform a post request with the facebook API
	 * @param  string $message [description]
	 * @param  string $name    [description]
	 * @param  string $url     [description]
	 * @param  string $picture [description]
	 * @return mixed
	 */
	public function feed_post($message, $name, $url, $picture = NULL)
	{
		if ( ! $this->initialized())
			return NULL;
		$defaults = Arr::get($this->_config, 'post', array());

		$attachment = Arr::merge($defaults, array(
			'message' => $message,
			'name' => $name,
			'link' => $url,
			'picture' => $picture ? $picture : Arr::get($defaults, 'picture')
		));
		
		$this->api('/me/feed', 'POST', $attachment);
	}

	public function og_enabled()
	{
		if ( ! $this->initialized())
			return NULL;

		return Arr::get($this->_config, 'og_enabled', FALSE);
	}

	/**
	 * Get the facebook permissions, authorized by the user OR check for a single permission or an array of permissions
	 * @return array
	 */
	
	/**
	 * Get the facebook permissions, authorized by the user OR check for a single permission or an array of permissions
	 * @param  string|array $permission check for those permissions
	 * @return bool|array
	 */
	protected function permissions($permission = NULL)
	{
		if ( ! $this->initialized())
			return NULL;

		if ( ! $this->_permissions)
		{
			$this->_permissions = array_keys(Arr::path($this->api('/me/permissions'), 'data.0', array()));
		}

		if ($permission !== NULL)
		{
			return count(array_diff((array) $permission, $this->_permissions)) == 0;
		}

		return $this->_permissions;
	}


	/**
	 * Get the data from a graph /me request
	 * @return array<commit_msg>fixed errors in fb service<commit_after><?php defined('SYSPATH') OR die('No direct script access.');

/**
 * Facebook service adapter
 * requires 'auth' configuration array with apiId and secret
 * 
 * @package    OpenBuildings/services-manager
 * @author     <NAME>
 * @copyright  (c) 2012 OpenBuildings Inc.
 * @license    http://creativecommons.org/licenses/by-sa/3.0/legalcode
 */
abstract class Kohana_Service_Facebook extends Service implements Service_Type_Php, Service_Type_Javascript
{
	public $_facebook;
	public $_user_id;
	public $_user_data;
	public $_permissions;
	public $_meta = array();

	public function init()
	{
		$this->_meta['fb:app_id'] = Arr::path($this->_config, 'auth.appId');
		if ($admins = Arr::get($this->_config, 'admins'))
		{
			$this->_meta['fb:admins'] = $admins;
		}
	}

	public function og_post($action, $name, $url)
	{
		if ( ! $this->initialized() OR ! $this->og_enabled())
			return NULL;

		if ( ! Valid::url($url))
			throw new Kohana_Exception("URL :url passed to facebook is not valid", array(':url' => $url));

		if ($this->og_namespace())
		{
			$action = $this->og_namespace().':'.$action;
		}

		return $this->api("/me/{$action}", 'POST', array($name => $url));
	}

	public function delete($opengraph_id)
	{
		return $this->api('/'.$opengraph_id, 'DELETE');
	}

	/**
	 * Perform a post request with the facebook API
	 * @param  string $message [description]
	 * @param  string $name    [description]
	 * @param  string $url     [description]
	 * @param  string $picture [description]
	 * @return mixed
	 */
	public function feed_post($message, $name, $url, $picture = NULL)
	{
		if ( ! $this->initialized())
			return NULL;
		$defaults = Arr::get($this->_config, 'post', array());

		$attachment = Arr::merge($defaults, array(
			'message' => $message,
			'name' => $name,
			'link' => $url,
			'picture' => $picture ? $picture : Arr::get($defaults, 'picture')
		));
		
		$this->api('/me/feed', 'POST', $attachment);
	}

	public function og_enabled()
	{
		if ( ! $this->initialized())
			return NULL;

		return Arr::get($this->_config, 'og_enabled', FALSE);
	}

	/**
	 * Get the facebook permissions, authorized by the user OR check for a single permission or an array of permissions
	 * @return array
	 */
	
	/**
	 * Get the facebook permissions, authorized by the user OR check for a single permission or an array of permissions
	 * @param  string|array $permission check for those permissions
	 * @return bool|array
	 */
	public function permissions($permission = NULL)
	{
		if ( ! $this->initialized())
			return NULL;

		if ( ! $this->_permissions)
		{
			$this->_permissions = array_keys(Arr::path($this->api('/me/permissions'), 'data.0', array()));
		}

		if ($permission !== NULL)
		{
			return count(array_diff((array) $permission, $this->_permissions)) == 0;
		}

		return $this->_permissions;
	}


	/**
	 * Get the data from a graph /me request
	 * @return array
{'fraction_non_alphanumeric': 0.16632373113854595, 'fraction_numerical': 0.0027434842249657062, 'mean_word_length': 3.881171548117155, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1668725', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1850, 'n_words': 507}