<?
if (!defined('IN_BANDTOPIA'))
{
	exit;
}

require '../include/utilities/tmhOAuth.php';
require '../include/utilities/tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
  'consumer_key'    => 'C2mJaoBhARf1AL6MejI8ZA',
  'consumer_secret' => 'rWwj73STuxN4ueZcaUqqCO35wVSGeWCmbtKXnEP2C3Y',
));

function outputError($tmhOAuth) {
  echo 'There was an error: ' . $tmhOAuth->response['response'] . PHP_EOL;
}

// Step 1: Request a temporary token
function request_token($tmhOAuth) {
  $code = $tmhOAuth->request(
	'POST',
	$tmhOAuth->url('oauth/request_token', ''),
	array(
	  'oauth_callback' => tmhUtilities::php_self()
	)
  );
  
   if ($code == 200) {
	$_SESSION['oauth'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
	authorize($tmhOAuth);
  } else {
	outputError($tmhOAuth);
  }
}

// Step 2: Direct the user to the authorize web page
function authorize($tmhOAuth) {
  $authurl = $tmhOAuth->url("oauth/authorize", '') .  "?oauth_token={$_SESSION['oauth']['oauth_token']}";
  header("Location: {$authurl}");

  // in case the redirect doesn't fire
  echo '<p>To complete the OAuth flow please visit URL: <a href="'. $authurl . '">' . $authurl . '</a></p>';
}

// Step 3: This is the code that runs when Twitter redirects the user to the callback. Exchange the temporary token for a permanent access token
function access_token($tmhOAuth) {
  $tmhOAuth->config['user_token']  = $_SESSION['oauth']['oauth_token'];
  $tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];

	$tempLinkObj = new LinksObj();
	$tempLinkObj->LinkId = $_SESSION['LinkIdAuthorizing'];
	$tempLinkObj->UserToken = $tmhOAuth->config['user_token'];
	$tempLinkObj->UserKey = $tmhOAuth->config['user_secret'];
	
	$tempLinkObj->UpdateTwitterTokens();

  $code = $tmhOAuth->request(
    'POST',
    $tmhOAuth->url('oauth/access_token', ''),
    array(
      'oauth_verifier' => $_REQUEST['oauth_verifier']
    )
  );

  if ($code == 200) {
    $_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
    unset($_SESSION['oauth']);
    header('Location: ' . tmhUtilities::php_self());
  } else {
    outputError($tmhOAuth);
  }
}

$linkObj = new LinksObj();

if ($action == 'linkAdmin')
{
	$template -> set_filenames(array(
		'page' => MODULE_PATH.'profiles/template/linkAdmin.htm'));

	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		$linkObj->ProfileId = $_POST['ProfileId'];
		$linkObj->LinkId = $_POST['LinkId'];
		$linkObj->LinkName = $_POST['LinkName'];
		$linkObj->LinkUrl = $_POST['LinkUrl'];
		$linkObj->LinkIcon = $_POST['LinkIcon'];
		$linkObj->IsSocialMedia = $_POST['IsSocialMedia'];
		$linkObj->LinkUsername = $_POST['LinkUsername'];
		$linkObj->LinkPassword = $_POST['LinkPassword'];
		$linkObj->SocialMediaType = $_POST['SocialMediaType'];
		$linkObj->SocialMediaAccountName = $_POST['SocialMediaAccountName'];
		
		$linkMsg = $linkObj->UpdateLink();
		if(strpos($linkMsg, 'error'))
		{
			$template -> assign_block_vars('page_error', array());
			$template -> assign_block_vars('page_error.error_msg', array(
				'ERROR_MSG' => $linkMsg
			));
		} else {
			$template -> assign_block_vars('page_msg', array());
			$template -> assign_block_vars('page_msg.msg', array(
				'MSG' => $linkMsg
			));
		}
		
		foreach ($linkObj as $key => $value)
		{
			$linkObj->$key = stripslashes($value);
		}
	}
	else
	{
		if (isset($_GET['LinkId']))
		{
			$linkObj->LinkId = $_GET['LinkId'];
			$linkObj->GetLink();
		}
		else
		{
			$linkObj->ProfileId = $_SESSION['UserProfileId'];
		}
	}

	$template -> assign_vars(array(
		'PROFILE_ID' => $linkObj->ProfileId,
		'LINK_ID' => $linkObj->LinkId,
		'LINK_NAME' => $linkObj->LinkName,
		'LINK_URL' => $linkObj->LinkUrl,
		'LINK_ICON' => $linkObj->LinkIcon,
		'IS_SOCIAL_MEDIA' => $linkObj->IsSocialMedia,
		'LINK_USERNAME' => $linkObj->LinkUsername,
		'LINK_PASSWORD' => $linkObj->LinkPassword,
		'SOCIAL_MEDIA_TYPE' => $linkObj->SocialMediaType,
		'SOCIAL_MEDIA_ACCOUNT_NAME' => $linkObj->SocialMediaAccountName
		));
}
elseif ($action == 'twitterStart')
{
	$_SESSION['LinkIdAuthorizing'] = $_GET['LinkId'];
  request_token($tmhOAuth);
}
elseif ($action == 'twitterAccess')
{
  access_token($tmhOAuth);
}




?>