<?php
define('API_SCRIPT_ROOT', 'https://theparacast.com/forum/api');
define('API_SCRIPT_CLIENT_ID', '----------');
define('API_SCRIPT_CLIENT_SECRET', '----------E');
/* API SCRIPT FUNCTIONS START */
function apiScriptGetAccessToken($username, $password, $cookieName = null)
{
foreach ([
'API_SCRIPT_ROOT',
'API_SCRIPT_CLIENT_ID',
'API_SCRIPT_CLIENT_SECRET'
] as $apiScriptConstant) {
if (!defined($apiScriptConstant)) {
throw new Exception(sprintf('%s must be defined!', $apiScriptConstant));
}
}
if ($cookieName === null) {
$cookieName = API_SCRIPT_CLIENT_ID . 'AccessToken';
}
if (is_string($cookieName) && isset($_COOKIE[$cookieName])) {
return $_COOKIE[$cookieName];
}
$token = apiScriptPostOauthToken($username, $password);
if (is_string($cookieName) && strlen($cookieName) > 0) {
setcookie($cookieName, $token['access_token'], time() + $token['expires_in']);
}
return $token['access_token'];
}
function apiScriptGetUserMe($accessToken)
{
$result = @file_get_contents(API_SCRIPT_ROOT . '/index.php?users/me&oauth_token=' . $accessToken);
if (!is_string($result)) {
return null;
}
$json = @json_decode($result, true);
if (!is_array($json) || !isset($json['user'])) {
return null;
}
return $json['user'];
}
function apiScriptPostOauthToken($username, $password)
{
$fields = [
'grant_type' => 'password',
'username' => $username,
'password' => $password,
'client_id' => API_SCRIPT_CLIENT_ID,
'client_secret' => API_SCRIPT_CLIENT_SECRET
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, API_SCRIPT_ROOT . '/index.php?oauth/token');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = @curl_exec($ch);
curl_close($ch);
$json = @json_decode($result, true);
if (!is_array($json) || !isset($json['access_token'])) {
return null;
}
return $json;
}
function apiScriptTestUserGroups(array $user, $groupIdsList)
{
if (!is_string($groupIdsList)) {
return false;
}
$groupIds = preg_split('/[^0-9]/', $groupIdsList, -1, PREG_SPLIT_NO_EMPTY);
$groupIds = array_map('intval', $groupIds);
if (count($groupIds) === 0) {
return true;
}
if (!isset($user['user_groups'])) {
return false;
}
foreach ($user['user_groups'] as $userGroup) {
if (in_array($userGroup['user_group_id'], $groupIds, true)) {
return true;
}
}
return false;
}
/* API SCRIPT FUNCTIONS END */
if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) && strpos($_SERVER['REDIRECT_HTTP_AUTHORIZATION'], 'Basic ') === 0) {
list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($_SERVER['REDIRECT_HTTP_AUTHORIZATION'], 6)));
}
if (empty($_SERVER['PHP_AUTH_USER']) ||
empty($_SERVER['PHP_AUTH_PW']) ||
!($accessToken = apiScriptGetAccessToken($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']))
) {
header('WWW-Authenticate: Basic realm="The Paracast+"');
header('HTTP/1.1 401 Unauthorized');
die('Please authenticate with your Paracast Forum username and password.');
}
if (!($user = apiScriptGetUserMe($accessToken)) ||
!apiScriptTestUserGroups($user, '3,4,5,8,9,10,11')
) {
header('HTTP/1.1 403 Forbidden');
die('Your account has not been upgraded to access The Paracast+.');
}
header('Content-Type: application/xml; charset=utf-8');
$doc = new DOMDocument();
$doc->load('288h7su1ksh9.xml');
echo $doc->saveXML();