<commit_before><?php

require_once 'oauth2Action.class.php';

/**
 * @package Core
 * @subpackage externalServices
 */
class facebookoauth2Action extends oauth2Action
{
	const SUB_ACTION_REDIRECT_SCREEN = 'redirect-screen';
	const SUB_ACTION_PROCESS_OAUTH2_RESPONSE = 'process-oauth2-response';
	const SUB_ACTION_LOGIN_SCREEN = 'login-screen';
	const FACEBOOK_DISTRIBUTION_ACCESS_URL = "/api_v3/index.php?service=contentdistribution_distributionprofile&distributionProfile%3AobjectType=KalturaFacebookDistributionProfile";

	private $authDataCache;

	private function getAuthDataCache()
	{
		if(!$this->authDataCache)
			$this->authDataCache = new kAuthDataCache();

		return $this->authDataCache;
	}


	public function execute()
	{
		set_include_path(get_include_path().PATH_SEPARATOR.KALTURA_ROOT_PATH.'/infra/general/');
		require_once 'FacebookGraphSdkUtils.php';
		require_once 'kDistributionPersistentDataHandler.php';

		$nextAction = base64_decode($this->getRequestParameter(FacebookConstants::FACEBOOK_NEXT_ACTION_REQUEST_PARAM));

		// understand the sub action based on our url parameters
		if ($nextAction == self::SUB_ACTION_REDIRECT_SCREEN)
		{
			$this->subAction = self::SUB_ACTION_REDIRECT_SCREEN;
			$this->executeRedirectScreen();
		}
		elseif ($nextAction == self::SUB_ACTION_PROCESS_OAUTH2_RESPONSE)
		{
			$this->subAction = self::SUB_ACTION_PROCESS_OAUTH2_RESPONSE;
			$this->executeProcessOAuth2Response();
		}
		else
		{
			$this->subAction = self::SUB_ACTION_LOGIN_SCREEN;
			$this->executeLoginScreen();
		}

		return sfView::SUCCESS;
	}

	/**
	 * display login form
	 */
	protected function executeLoginScreen()
	{
		$this->loginError = null;
		$this->partnerError = null;
		$this->serviceUrl = requestUtils::getHost();
		$params = $this->getForwardParameters();
		$params[FacebookConstants::FACEBOOK_NEXT_ACTION_REQUEST_PARAM] = base64_encode(self::SUB_ACTION_REDIRECT_SCREEN);
		$this->nextUrl = $this->getController()->genUrl('extservices/facebookoauth2?'.http_build_query($params, null, '&')).'?ks=';
	}

	/**
	 *  display a message to the user before redirecting him to facebook
	 */
	protected function executeRedirectScreen()
	{
		$appId = $this->getFromConfig(FacebookConstants::FACEBOOK_APP_ID_REQUEST_PARAM);
		$appSecret = $this->getFromConfig(FacebookConstants::FACEBOOK_APP_SECRET_REQUEST_PARAM);
		$permissions = explode(',',base64_decode($this->getRequestParameter(FacebookConstants::FACEBOOK_PERMISSIONS_REQUEST_PARAM)));
		$providerId = base64_decode($this->getRequestParameter(FacebookConstants::FACEBOOK_PROVIDER_ID_REQUEST_PARAM));
		$requestPartnerId = base64_decode($this->getRequestParameter(FacebookConstants::FACEBOOK_PARTNER_ID_REQUEST_PARAM));

		$ksStr = $this->getRequestParameter(FacebookConstants::FACEBOOK_KS_REQUEST_PARAM);
		$this->ksError = null;
		$this->partnerError = null;
		$ksValid = $this->processKs($ksStr);
		if (!$ksValid)
		{
			$this->ksError = true;
			return;
		}

		$ks = kCurrentContext::$ks_object;
		$contextPartnerId = $ks->partner_id;
		if ( empty($requestPartnerId) || $contextPartnerId != $requestPartnerId)
		{
			$this->partnerError = true;
			return;
		}

		$ks = $this->generateTimeLimitedKs($contextPartnerId);
		$params = $this->getForwardParameters();
		$params[FacebookConstants::FACEBOOK_KS_REQUEST_PARAM] = $ks;
		$accessURL = $this->getFacebookDistributionAccessURL($providerId, $ks);
		$dataHandler = new kDistributionPersistentDataHandler($accessURL);
		//$redirectUrl = $this->getController()->genUrl('extservices/facebookoauth2?'.http_build_query($params, null, '&'), true);
		$redirectUrl = "http://".kDataCenterMgr::getCurrentDcName()."/index.php/'extservices/facebookoauth2?".
			http_build_query(array(FacebookConstants::FACEBOOK_NEXT_ACTION_REQUEST_PARAM => self::SUB_ACTION_PROCESS_OAUTH2_RESPONSE), null, '&');
		$reRequestPermissions = base64_decode($this->getRequestParameter(FacebookConstants::FACEBOOK_RE_REQUEST_PERMISSIONS_REQUEST_PARAM));
		$fb = FacebookGraphSdkUtils::createFacebookInstance($appId, $appSecret, $dataHandler);
		$loginHelper = $fb->getRedirectLoginHelper();
		$this->oauth2Url = FacebookGraphSdkUtils::getLoginUrl($loginHelper, $redirectUrl, $permissions, $reRequestPermissions);
		$persistentDataHandler = $loginHelper->getPersistentDataHandler();
		$state = $persistentDataHandler->get(FacebookConstants::FACEBOOK_LOGIN_STATE);
		$authDataCache = $this->getAuthDataCache();
		$authDataCache->store($state, $params);
	}

	/**
	 * validate the response from facebook
	 */
	protected function executeProcessOAuth2Response()
	{
		$this->tokenError = null;
		$appId = $this->getFromConfig(FacebookConstants::FACEBOOK_APP_ID_REQUEST_PARAM);
		$appSecret = $this->getFromConfig(FacebookConstants::FACEBOOK_APP_SECRET_REQUEST_PARAM);
		$state = base64_decode($this->getRequestParameter(FacebookConstants::FACEBOOK_LOGIN_STATE));
		$authDataCache = $this->getAuthDataCache();
		$data = $authDataCache->retrieve($state);
		if(!$data)
		{
			$msg = "Error while retrieving auth data for facebook with id ".$state;
			KalturaLog::err($msg);
			throw new Exception($msg);
		}

		$pageId = $data[FacebookConstants::FACEBOOK_PAGE_ID_REQUEST_PARAM];
		$providerId = $data[FacebookConstants::FACEBOOK_PROVIDER_ID_REQUEST_PARAM];
		$ks = $data[FacebookConstants::FACEBOOK_KS_REQUEST_PARAM];
		$permissions = explode(',',$data[FacebookConstants::FACEBOOK_PERMISSIONS_REQUEST_PARAM]);

		try
		{
			$accessURL = $this->getFacebookDistributionAccessURL($providerId, $ks);
			$dataHandler = new kDistributionPersistentDataHandler($accessURL);

			$userAccessToken = FacebookGraphSdkUtils::getLongLivedUserAccessToken($appId, $appSecret, $dataHandler, $permissions);

			if($userAccessToken)
			{
				$pageAccessToken = FacebookGraphSdkUtils::getPageAccessToken($appId, $appSecret, $userAccessToken, $pageId, $dataHandler);
				if($pageAccessToken)
				{
					$dataHandler->set('userAccessToken', $userAccessToken);
					$dataHandler->set('pageAccessToken', $pageAccessToken);
				}
			}
		}
		catch(Exception $e)
		{
			$this->tokenError = true;
			$this->errorMessage = $e->getMessage();
		}
	}

	protected function getFromConfig($paramName, $default = null)
	{
		return kConf::get($paramName, 'facebook', $default);
	}


	protected function getForwardParameters()
	{
		$params = array(
			FacebookConstants::FACEBOOK_PERMISSIONS_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_PERMISSIONS_REQUEST_PARAM),
			FacebookConstants::FACEBOOK_PAGE_ID_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_PAGE_ID_REQUEST_PARAM),
			FacebookConstants::FACEBOOK_RE_REQUEST_PERMISSIONS_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_RE_REQUEST_PERMISSIONS_REQUEST_PARAM),
			FacebookConstants::FACEBOOK_PROVIDER_ID_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_PROVIDER_ID_REQUEST_PARAM),
			FacebookConstants::FACEBOOK_PARTNER_ID_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_PARTNER_ID_REQUEST_PARAM),
			FacebookConstants::FACEBOOK_KS_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_KS_REQUEST_PARAM)
		);

		return $params;
	}

	private function getFacebookDistributionAccessURL($providerId, $ks)
	{
		$host = requestUtils::getHost();
		return $host.self::FACEBOOK_DISTRIBUTION_ACCESS_URL."&id=".$providerId."&ks=".$ks;
	}
}<commit_msg>Naos-14.0.1-PLAT-8940 Facebook Auth fixing encode and decode issues<commit_after><?php

require_once 'oauth2Action.class.php';

/**
 * @package Core
 * @subpackage externalServices
 */
class facebookoauth2Action extends oauth2Action
{
	const SUB_ACTION_REDIRECT_SCREEN = 'redirect-screen';
	const SUB_ACTION_PROCESS_OAUTH2_RESPONSE = 'process-oauth2-response';
	const SUB_ACTION_LOGIN_SCREEN = 'login-screen';
	const FACEBOOK_DISTRIBUTION_ACCESS_URL = "/api_v3/index.php?service=contentdistribution_distributionprofile&distributionProfile%3AobjectType=KalturaFacebookDistributionProfile";

	private $authDataCache;

	private function getAuthDataCache()
	{
		if(!$this->authDataCache)
			$this->authDataCache = new kAuthDataCache();

		return $this->authDataCache;
	}


	public function execute()
	{
		set_include_path(get_include_path().PATH_SEPARATOR.KALTURA_ROOT_PATH.'/infra/general/');
		require_once 'FacebookGraphSdkUtils.php';
		require_once 'kDistributionPersistentDataHandler.php';

		$nextAction = base64_decode($this->getRequestParameter(FacebookConstants::FACEBOOK_NEXT_ACTION_REQUEST_PARAM));

		// understand the sub action based on our url parameters
		if ($nextAction == self::SUB_ACTION_REDIRECT_SCREEN)
		{
			$this->subAction = self::SUB_ACTION_REDIRECT_SCREEN;
			$this->executeRedirectScreen();
		}
		elseif ($nextAction == self::SUB_ACTION_PROCESS_OAUTH2_RESPONSE)
		{
			$this->subAction = self::SUB_ACTION_PROCESS_OAUTH2_RESPONSE;
			$this->executeProcessOAuth2Response();
		}
		else
		{
			$this->subAction = self::SUB_ACTION_LOGIN_SCREEN;
			$this->executeLoginScreen();
		}

		return sfView::SUCCESS;
	}

	/**
	 * display login form
	 */
	protected function executeLoginScreen()
	{
		$this->loginError = null;
		$this->partnerError = null;
		$this->serviceUrl = requestUtils::getHost();
		$params = $this->getForwardParameters();
		$params[FacebookConstants::FACEBOOK_NEXT_ACTION_REQUEST_PARAM] = base64_encode(self::SUB_ACTION_REDIRECT_SCREEN);
		$this->nextUrl = $this->getController()->genUrl('extservices/facebookoauth2?'.http_build_query($params, null, '&')).'?ks=';
	}

	/**
	 *  display a message to the user before redirecting him to facebook
	 */
	protected function executeRedirectScreen()
	{
		$appId = $this->getFromConfig(FacebookConstants::FACEBOOK_APP_ID_REQUEST_PARAM);
		$appSecret = $this->getFromConfig(FacebookConstants::FACEBOOK_APP_SECRET_REQUEST_PARAM);
		$permissions = explode(',',base64_decode($this->getRequestParameter(FacebookConstants::FACEBOOK_PERMISSIONS_REQUEST_PARAM)));
		$providerId = base64_decode($this->getRequestParameter(FacebookConstants::FACEBOOK_PROVIDER_ID_REQUEST_PARAM));
		$requestPartnerId = base64_decode($this->getRequestParameter(FacebookConstants::FACEBOOK_PARTNER_ID_REQUEST_PARAM));

		$ksStr = $this->getRequestParameter(FacebookConstants::FACEBOOK_KS_REQUEST_PARAM);
		$this->ksError = null;
		$this->partnerError = null;
		$ksValid = $this->processKs($ksStr);
		if (!$ksValid)
		{
			$this->ksError = true;
			return;
		}

		$ks = kCurrentContext::$ks_object;
		$contextPartnerId = $ks->partner_id;
		if ( empty($requestPartnerId) || $contextPartnerId != $requestPartnerId)
		{
			$this->partnerError = true;
			return;
		}

		$ks = $this->generateTimeLimitedKs($contextPartnerId);
		$params = $this->getForwardParameters();
		$params[FacebookConstants::FACEBOOK_KS_REQUEST_PARAM] = $ks;
		$accessURL = $this->getFacebookDistributionAccessURL($providerId, $ks);
		$dataHandler = new kDistributionPersistentDataHandler($accessURL);
		//$redirectUrl = $this->getController()->genUrl('extservices/facebookoauth2?'.http_build_query($params, null, '&'), true);
		$redirectUrl = "http://".kDataCenterMgr::getCurrentDcName()."/index.php/extservices/facebookoauth2?".
			http_build_query(array(FacebookConstants::FACEBOOK_NEXT_ACTION_REQUEST_PARAM =>  base64_encode(self::SUB_ACTION_PROCESS_OAUTH2_RESPONSE)), null, '&');
		$reRequestPermissions = base64_decode($this->getRequestParameter(FacebookConstants::FACEBOOK_RE_REQUEST_PERMISSIONS_REQUEST_PARAM));
		$fb = FacebookGraphSdkUtils::createFacebookInstance($appId, $appSecret, $dataHandler);
		$loginHelper = $fb->getRedirectLoginHelper();
		$this->oauth2Url = FacebookGraphSdkUtils::getLoginUrl($loginHelper, $redirectUrl, $permissions, $reRequestPermissions);
		$persistentDataHandler = $loginHelper->getPersistentDataHandler();
		$state = $persistentDataHandler->get(FacebookConstants::FACEBOOK_LOGIN_STATE);
		$authDataCache = $this->getAuthDataCache();
		$authDataCache->store($state, $params);
	}

	/**
	 * validate the response from facebook
	 */
	protected function executeProcessOAuth2Response()
	{
		$this->tokenError = null;
		$appId = $this->getFromConfig(FacebookConstants::FACEBOOK_APP_ID_REQUEST_PARAM);
		$appSecret = $this->getFromConfig(FacebookConstants::FACEBOOK_APP_SECRET_REQUEST_PARAM);
		$state = $this->getRequestParameter(FacebookConstants::FACEBOOK_LOGIN_STATE);
		$authDataCache = $this->getAuthDataCache();
		$data = $authDataCache->retrieve($state);
		if(!$data)
		{
			$msg = "Error while retrieving auth data for facebook with id ".$state;
			KalturaLog::err($msg);
			throw new Exception($msg);
		}

		$pageId = $data[FacebookConstants::FACEBOOK_PAGE_ID_REQUEST_PARAM];
		$providerId = $data[FacebookConstants::FACEBOOK_PROVIDER_ID_REQUEST_PARAM];
		$ks = $data[FacebookConstants::FACEBOOK_KS_REQUEST_PARAM];
		$permissions = explode(',',$data[FacebookConstants::FACEBOOK_PERMISSIONS_REQUEST_PARAM]);

		try
		{
			$accessURL = $this->getFacebookDistributionAccessURL($providerId, $ks);
			$dataHandler = new kDistributionPersistentDataHandler($accessURL);

			$userAccessToken = FacebookGraphSdkUtils::getLongLivedUserAccessToken($appId, $appSecret, $dataHandler, $permissions);

			if($userAccessToken)
			{
				$pageAccessToken = FacebookGraphSdkUtils::getPageAccessToken($appId, $appSecret, $userAccessToken, $pageId, $dataHandler);
				if($pageAccessToken)
				{
					$dataHandler->set('userAccessToken', $userAccessToken);
					$dataHandler->set('pageAccessToken', $pageAccessToken);
				}
			}
		}
		catch(Exception $e)
		{
			$this->tokenError = true;
			$this->errorMessage = $e->getMessage();
		}
	}

	protected function getFromConfig($paramName, $default = null)
	{
		return kConf::get($paramName, 'facebook', $default);
	}


	protected function getForwardParameters()
	{
		$params = array(
			FacebookConstants::FACEBOOK_PERMISSIONS_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_PERMISSIONS_REQUEST_PARAM),
			FacebookConstants::FACEBOOK_PAGE_ID_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_PAGE_ID_REQUEST_PARAM),
			FacebookConstants::FACEBOOK_RE_REQUEST_PERMISSIONS_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_RE_REQUEST_PERMISSIONS_REQUEST_PARAM),
			FacebookConstants::FACEBOOK_PROVIDER_ID_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_PROVIDER_ID_REQUEST_PARAM),
			FacebookConstants::FACEBOOK_PARTNER_ID_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_PARTNER_ID_REQUEST_PARAM),
			FacebookConstants::FACEBOOK_KS_REQUEST_PARAM =>
				$this->getRequestParameter(FacebookConstants::FACEBOOK_KS_REQUEST_PARAM)
		);

		return $params;
	}

	private function getFacebookDistributionAccessURL($providerId, $ks)
	{
		$host = requestUtils::getHost();
		return $host.self::FACEBOOK_DISTRIBUTION_ACCESS_URL."&id=".$providerId."&ks=".$ks;
	}
}
{'fraction_non_alphanumeric': 0.12745361656359236, 'fraction_numerical': 0.004571121269158376, 'mean_word_length': 7.228429203539823, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 83, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4232947', 'n_tokens_mistral': 5272, 'n_tokens_neox': 4755, 'n_words': 589}