<commit_before><?php

/**
 * This file contains an OAuth Connection Class
 * for Facebook.
 *
 * PHP Version 5.3
 *
 * @category   Libraries
 * @package    OAuth
 * @subpackage Libraries
 * @author     M2Mobi <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 */

/**
 * Facebook OAuth Connection Class
 *
 * @category   Libraries
 * @package    OAuth
 * @subpackage Libraries
 * @author     M2Mobi <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 */
class FacebookConnection implements OAuthConnectionInterface
{

    const NETWORK = 'facebook';

    /**
     * API access token
     * @var String
     */
    private $token;

    /**
     * Constructor.
     *
     * @param String $token User access token
     */
    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * Destructor.
     */
    public function __destruct()
    {
        unset($this->token);
    }

    /**
     * Get the request token from the Facebook.
     *
     * @param String $callback URL to receive the callback from the OAuth provider
     *
     * @return Array Array containing the 'oauth_token_secret' and the
     *              'request_token_secret' on success and FALSE otherwise
     */
    public function get_request_token($callback)
    {
        global $config;

        $state = md5(uniqid(rand(), TRUE));

        $url = $config['social'][NETWORK]['request_token_url']
            . '?client_id=' . $config['social']['facebook']['client_id']
            . '&redirect_uri=' . urlencode($callback)
            . '&state=' . $state
            . '&display=wap';

        return array(
                'state' => $state,
                'url' => $url
            );
    }

    /**
     * Get access token from Facebook.
     *
     * @param String $oauth_token          Oauth token
     * @param String $request_token_secret Request token secret
     *
     * @return Array Array containing the 'oauth token' and the 'oauth token secret',
     *               FALSE on failure.
     */
    public function get_access_token($oauth_token, $url_back)
    {
        global $config;

        $token_url = $config['social'][NETWORK]['access_token_url']
            . "?client_id=" . $config['social'][NETWORK]['client_id']
            . "&redirect_uri=" . urlencode($url_back)
            . "&client_secret=" . $config['social'][NETWORK]['app_secret']
            . "&code=" . $oauth_token;

        $response = file_get_contents($token_url);
        $params = null;
        parse_str($response, $params);

        return $params;
    }

    /**
     * Get user profile info from Facebook.
     *
     * @param String $access_oauth_token  Oauth token
     * @param String $access_token_secret Unused for Facebook
     *
     * @return Array Array containing the user profile information, FALSE otherwise
     */
    public function get_user_info($access_token, $access_token_secret = '')
    {
        global $config;

        $url = $config['social'][NETWORK]['own_profile_url'] . $access_token;
        return file_get_contents($url);
    }

    /**
     * Post a message to Facebook
     *
     * @param String $oauth_token         Oauth token
     * @param String $msg                 SocialMessage object already filled
     * @param String $access_token_secret Unused for Facebook
     *
     * @return Array Array containing the 'oauth token' and the 'oauth token secret',
     *               FALSE otherwise.
     */
    public function post_message($access_token, $message, $access_token_secret = '')
    {
        global $config;

        $curl = new Curl();
        $params = array('access_token' => $access_token, 'message' => $message->message);

        return $curl->simple_post($config['social'][NETWORK]['share_url'], $params);
    }

    /**
     * Parse a Facebook profile array and returns a Social Profile
     *
     * @param Array $user_info Array with the user information coming from Facebook
     *
     * @return SocialProfile Object containing the facebook profile info.<commit_msg>oauth: change get_user_info to use the new parser on FB class.<commit_after><?php

/**
 * This file contains an OAuth Connection Class
 * for Facebook.
 *
 * PHP Version 5.3
 *
 * @category   Libraries
 * @package    OAuth
 * @subpackage Libraries
 * @author     M2Mobi <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 */

/**
 * Facebook OAuth Connection Class
 *
 * @category   Libraries
 * @package    OAuth
 * @subpackage Libraries
 * @author     M2Mobi <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 */
class FacebookConnection implements OAuthConnectionInterface
{

    const NETWORK = 'facebook';

    /**
     * API access token
     * @var String
     */
    private $token;

    /**
     * Constructor.
     *
     * @param String $token User access token
     */
    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * Destructor.
     */
    public function __destruct()
    {
        unset($this->token);
    }

    /**
     * Get the request token from the Facebook.
     *
     * @param String $callback URL to receive the callback from the OAuth provider
     *
     * @return Array Array containing the 'oauth_token_secret' and the
     *              'request_token_secret' on success and FALSE otherwise
     */
    public function get_request_token($callback)
    {
        global $config;

        $state = md5(uniqid(rand(), TRUE));

        $url = $config['social'][NETWORK]['request_token_url']
            . '?client_id=' . $config['social']['facebook']['client_id']
            . '&redirect_uri=' . urlencode($callback)
            . '&state=' . $state
            . '&display=wap';

        return array(
                'state' => $state,
                'url' => $url
            );
    }

    /**
     * Get access token from Facebook.
     *
     * @param String $oauth_token          Oauth token
     * @param String $request_token_secret Request token secret
     *
     * @return Array Array containing the 'oauth token' and the 'oauth token secret',
     *               FALSE on failure.
     */
    public function get_access_token($oauth_token, $url_back)
    {
        global $config;

        $token_url = $config['social'][NETWORK]['access_token_url']
            . "?client_id=" . $config['social'][NETWORK]['client_id']
            . "&redirect_uri=" . urlencode($url_back)
            . "&client_secret=" . $config['social'][NETWORK]['app_secret']
            . "&code=" . $oauth_token;

        $response = file_get_contents($token_url);
        $params = null;
        parse_str($response, $params);

        return $params;
    }

    /**
     * Get user profile info from Facebook.
     *
     * @param String $access_oauth_token  Oauth token
     * @param String $access_token_secret Unused for Facebook
     *
     * @return Array Array containing the user profile information, FALSE otherwise
     */
    public function get_user_info($access_token, $access_token_secret = '')
    {
        global $config;

        $url = $config['social'][NETWORK]['own_profile_url'] . $access_token;
        $response = file_get_contents($url);
        if($response === FALSE)
        {
            return FALSE;
        }
        else
        {
            $user_info = Json::decode($response);
            if(!$user_info)
            {
                return FALSE;
            }
        }
        return $this->parse_facebook_profile($user_info);
    }

    /**
     * Post a message to Facebook
     *
     * @param String $oauth_token         Oauth token
     * @param String $msg                 SocialMessage object already filled
     * @param String $access_token_secret Unused for Facebook
     *
     * @return Array Array containing the 'oauth token' and the 'oauth token secret',
     *               FALSE otherwise.
     */
    public function post_message($access_token, $message, $access_token_secret = '')
    {
        global $config;

        $curl = new Curl();
        $params = array('access_token' => $access_token, 'message' => $message->message);

        return $curl->simple_post($config['social'][NETWORK]['share_url'], $params);
    }

    /**
     * Parse a Facebook profile array and returns a Social Profile
     *
     * @param Array $user_info Array with the user information coming from Facebook
     *
     * @return SocialProfile Object containing the facebook profile info.
{'fraction_non_alphanumeric': 0.12059064807219032, 'fraction_numerical': 0.0011719207781553968, 'mean_word_length': 2.1724907063197025, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2399900', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2402, 'n_words': 643}