<?php
/*
COPYRIGHT 2009 Damien Keitel
This file is part of Facebook 2011.
Facebook 2011 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Facebook 2011 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Facebook 2011. If not, see <http://www.gnu.org/licenses/>.*/
if (!defined('IN_PHPBB'))
{
exit;
}
define('FB2011_FACEBOOK_LOGIN', 0);
define('FB2011_FACEBOOK_PROFILE', 1);
define('FB2011_HIDE_POST_LOGON', 2); // Does the user want to be asked if they want to see the 'hide online' and autologin screen after verification?
define('FB2011_USER_OPTION_COUNT', 3);
function fb_language_select($default = '')
{
global $config, $phpbb_root_path ;
$get_locale = simplexml_load_file($phpbb_root_path . '/fb2011/FacebookLocales.xml');
$arr = $get_locale->locale;
$fb_lang_options = '';
foreach($arr as $locale)
{
$selected = ($locale->codes->code->standard->representation == $default) ? ' selected="selected"' : '';
$fb_lang_options .= '<option name="' . $locale->codes->code->standard->representation . '" id="' . $locale->codes->code->standard->representation . '" value="' . $locale->codes->code->standard->representation . '"' . $selected . '>' . $locale->englishName . '</option>';
}
return $fb_lang_options;
}
/**
* Extract an fb2011 settings value
* break it down and convert it to an array.
* @param integer $settings An Integer value that is retrieved from the congif table.
*
* @return array $fb_settings An array containing 0 or 1 values to determine the settings.
*/
function parse_signed_request($signed_request, $secret)
{
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = self::base64_url_decode($encoded_sig);
$data = json_decode(self::base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256')
{
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}
// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input)
{
return base64_decode(strtr($input, '-_', '+/'));
}
function get_fb2011_cookie($app_id, $app_secret)
{
$args = array();
@parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value)
{
if ($key != 'sig')
{
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $app_secret) != @$args['sig'])
{
return null;
}
return $args;
}
function get_fb2011_settings($settings)
{
if($settings == 0)
{
$fb2011_settings = array_fill(0, FB2011_USER_OPTION_COUNT, 0);
}
else
{
$fb2011_settings = decbin($settings);
$fb2011_settings = strrev($fb2011_settings);
$fb2011_settings = str_split($fb2011_settings);
}
return $fb2011_settings;
}
/**
* Converts fb2011 settings array to an Interger value that
* is stored in the $config array.
*
* @param array $fb2011_settings An array containing boolean values to determine the settings.
*/
function set_fb2011_settings($settings)
{
$fb2011_settings = implode($settings);
$fb2011_settings = strrev($fb2011_settings);
$fb2011_settings = bindec($fb2011_settings);
return $fb2011_settings;
}
/**
* Retrieve the Alternate Login settings value stored in the
* alternatelogin_settings table.
*
* @param integer $user_id The user id from the $user array.
*
* @return An integer value for conversion if neccesary.
*/
function get_fb2011_user_settings($user_id)
{
global $db;
$sql = 'SELECT fb2011_user_settings' .
' FROM ' . FB2011_USER_DATA .
" WHERE user_id = '$user_id'";
$result = $db->sql_query($sql);
if(!$result)
{
return false;
}
$ret_value = $db->sql_fetchfield('fb2011_user_settings');
return $ret_value;
}
/**
* Set the Alternate Login settings value stored in the
* alternatelogin_settings table.
*
* @param integer $user_id The user id can be found in the $user array.
*
* @param integer $settings The integer value of the user settings. Can be converted from the get_al_settings() function.
*
* @return A boolean value indicating whether or not the operation succeded.
*/
function set_fb2011_user_settings($user_id)
{
global $db;
$sql = 'UPDATE ' . FB2011_USER_DATA .
" SET fb2011_user_settings = '$settings'
WHERE user_id = '$user_id'";
$result = $db->sql_query($sql);
if(!$result)
{
return false;
}
else
{
return true;
}
}
?>