public function execute() { // TODO: This ONLY will work if API and Social tiers are co-located! $response = array(); // Finish the API session, because we need to start a social session session_regenerate_id(true); $_SESSION = array(); $network_session = new RingsideSocialSession(); $rest = RingsideSocialUtils::getAdminClient(); $session_key = $rest->auth_createAppSession($this->uid, RingsideSocialConfig::$apiKey, false); $network_session->addApiSessionKey(RingsideSocialConfig::$apiKey, $session_key); $network_session->setNetwork($this->user_network_key); //$network_session->addApiSessionKey($apiKey, $session_key); $network_session->setUserId($this->uid); // TODO: Do user identity mapping right now //$network_session->setPrincipalId($pid); //$network_session->setTrust($trust_key); //$network_session->setCallbackUrl($social_callback); $network_session->setLoggedIn(true); $response[self::RESPONSE_SOCIAL_SESSION]['session_id'] = $network_session->getSessionKey(); $response[self::RESPONSE_SOCIAL_SESSION]['initial_expiry'] = $network_session->getExpiry(); session_write_close(); return $response; }
/** * Returns an array of all app fbml blocks * formatted for display. * $location enum(narrow|wide) indicates the style to be used when fbml is returned * $arryExcludeAppNames is an array of app names which should not appear in the list * */ function getAllFormattedHtmlForUserOpenSocialApps($location = "narrow", $arryExcludeAppNames = null) { $userAppList = Api_Bo_App::getApplicationListByUserId($this->uid); $this->debug("******************** DUMPING OS userAppList for {$this->uid}" . " which is " . count($userAppList) . " apps"); $aggregateFbml = ""; $fbmlArry = array(); if (!empty($userAppList)) { foreach ($userAppList as $index => $appInfo1) { $appInfo = $appInfo1['RingsideApp']; $this->debug("Processing " . $appInfo['name'] . " " . $appInfo['app_id']); $this->debug("{$appInfo1}=" . var_export($appInfo1, true)); //$returnedFbml=$this->getFbmlForApp($appInfo1['app_id'],$this->uid); $app_id = $appInfo1['app_id']; $app_name = $appInfo['name']; $enabled = true; // Don't show this app if it is not open social if ($appInfo['canvas_type'] != 2) { $enabled = false; $this->debug("{$app_name} rejected as not open social."); } if ($enabled) { //$app_appBlock=$this->getFormatedFbmlFromAppName($app_name,$location,true,$returnedFbml); //$fbmlArry=array_merge($fbmlArry,$app_appBlock); $block = array("<div id='app-{$app_name}-header' class='wide_app_header'> "); $block[] = $app_name; //$block[]="<div style='float: right;' ><form id='formmove' name='formmove' method='post' ><input name='action' type='hidden' value='saveColChange'/><input name='appname' type='hidden' value='$app_name'/><a onClick='form.submit();' href=''> [move] </a></form></div>"; $block[] = "</div>"; $frameParams = $_REQUEST; $callbackQuery = http_build_query($frameParams); $owner_id = $_REQUEST['id']; if ($owner_id == '') { $owner_id = $this->uid; } // Get api_key $keyService = Api_ServiceFactory::create('KeyService'); $domainService = Api_ServiceFactory::create('DomainService'); $domainId = $domainService->getNativeIdByName('Ringside'); $app_keysArray = $keyService->getKeyset($app_id, $domainId); $app_keys = $app_keysArray; /* $app_keysArray=Api_Bo_App::getUsersAppKeys($this->uid,$app_id); $app_keys=$app_keysArray[0]; $this->debug(var_export($app_keysArray,true)); foreach($app_keysArray as $testApp_keys){ if($testApp_keys['network_id']==$socialApiKey){ $app_keys=$testApp_keys; } } $app_keys=$app_keysArray[0]; */ $this->debug("{$app_keys}=" . var_export($app_keys, true)); $social_session = new RingsideSocialSession(); $social_session->addApiSessionKey($app_keys['api_key'], $app_keys['secret']); $this->debug("Building Social Session with " . $app_keys['api_key'] . " and " . $app_keys['secret']); $osGadgetUrl = RingsideSocialConfig::$socialRoot . '/gadgets/ifr?view=profile&synd=ringside&fb_sig_api_key=' . $app_keys['api_key'] . '&fb_sig_owner_id=' . $owner_id . '&url=' . urlencode($appInfo['callback_url']) . '&social_session_key=' . $social_session->getSessionKey(); $this->debug("osGadgetUrl= {$osGadgetUrl}"); $block[] = '<iframe src="' . $osGadgetUrl . '" height="400" width="450"></iframe>'; $fbmlArry = array_merge($fbmlArry, $block); } } } return $fbmlArry; }
/** * Get the session key between an application and the API server. * * @param unknown_type $api_key * @param unknown_type $secret_key * @param RingsideSocialSession $socialSession * @return string session key for the API container */ public static function getApiSessionKey($api_key, $secret_key, RingsideSocialSession $socialSession) { $uid = $socialSession->getUserId(); $sessionKey = $socialSession->getApiSessionKey($api_key); if ($sessionKey != null) { // Validate Session Key is still valid. $apiClient = new RingsideApiClientsRest($api_key, $secret_key, $sessionKey); $apiClient->setNetworkKey($socialSession->getNetwork()); try { $apiClient->users_getLoggedInUser(); } catch (Exception $e) { // error_log( "Session expired? " . $e->getMessage() ) ; // error_log($e->getTraceAsString()); $sessionKey = null; $socialSession->unsetApiSessionKey($api_key); } } if ($sessionKey == null && $uid != null) { // Need to simulate being app and auth, approve, get... which of course // TODO we need to re-think once we are working. // TODO catch some exceptions. try { // Configure where we get the URL for the REST SERVER from. $apiClient = new RingsideApiClientsRest($api_key, $secret_key, null, null, RingsideSocialConfig::$apiKey); // Once the client is authenticated with a session, the network key will be associated via the session $apiClient->setNetworkKey($socialSession->getNetwork()); $auth_token = $apiClient->auth_createToken($socialSession->getExpiry() == null ? true : false); $result = $apiClient->auth_approveToken($uid); $result = $apiClient->auth_getSession($auth_token); if (!empty($apiClient->session_key)) { $sessionKey = trim($apiClient->session_key); $socialSession->addApiSessionKey($api_key, $sessionKey); } } catch (Exception $exception) { error_log("Error creating session key " . $exception); } } return $sessionKey; }
/** * Authorize the user against the api_key, app_id, or canvas_url * * This produces a SocialSession Object. * * Possible Params: * network_key * trust_key * api_key * canvas_url * auth_token * social_callback */ public function authorize() { $network_session = null; $network_key = $this->getParam('network_key'); $auth_token = $this->getParam('auth_token'); $social_callback = $this->getParam('social_callback'); $api_key = $this->getParam('api_key'); $canvas_url = $this->getParam('canvas_url'); $user_name = $this->getParam('user_name'); $trust_key = $this->getParam('trust_key'); if (!isset($trust_key)) { $trust_key = $socialApiKey; } $result = $this->getAppProperties(); if ($result) { $callback = isset($result['callback_url']) ? $result['callback_url'] : ''; $apiKey = isset($result['api_key']) ? $result['api_key'] : ''; $apiSecret = isset($result['secret_key']) ? $result['secret_key'] : ''; if (!isset($social_callback)) { $social_callback = $callback; } try { if (isset($apiKey) && isset($apiSecret)) { $auth_url = $this->getAuthUrl($trust_key); $fb = new RingsideApiClients($apiKey, $apiSecret, null, $auth_url); //public function __construct($api_key, $secret, $session_key = null, $url = null) { $result = $fb->do_get_session($auth_token); $session_key = $fb->api_client->session_key; $uid = $fb->api_client->users_getLoggedInUser(); $pids = $fb->api_client->users_mapToSubject(array($uid), $network_key, $result['application_id']); // RingsideSocialDbPrincipal::getPrincipalForSubject($uid, $network_key, $user_name, $trust_key); //if ( isset($pids) ) { // getPrincipalForSubject accepts and returns multiple IDs $pid = 0; if (isset($pids)) { $pid = $pids[0]; } // bool setcookie ( string $name [, string $value [, int $expire [, string $path [, string $domain [, bool $secure [, bool $httponly ]]]]]] ) $network_session = new RingsideSocialSession(); $network_session->setNetwork($network_key); $network_session->addApiSessionKey($apiKey, $session_key); $network_session->setUserId($uid); $network_session->setPrincipalId($pid); $network_session->setTrust($trust_key); $network_session->setCallbackUrl($social_callback); $network_session->setLoggedIn(true); $context = $this->getContext($fb->api_client, $network_session); if (strrpos($social_callback, '?') == 0) { return $social_callback . '?' . $context; } else { return $social_callback . '&' . $context; } //} else { // $this->error = "Unable to set Principle!"; //} } } catch (Exception $exception) { error_log("Exception : " . $exception->getMessage() . "\n" . $exception->getTraceAsString()); $this->error = "Exception : " . $exception->getMessage() . "\n" . $exception->getTraceAsString(); } } if (!isset($network_session)) { error_log("Application with api_key: {$api_key} or canvas_url: {$canvas_url} not found! Creating session and redirecting to {$social_callback}!"); $network_session = new RingsideSocialSession(null); $network_session->setNetwork($network_key); $network_session->setTrust($trust_key); $network_session->setCallbackUrl($social_callback); if (strrpos($social_callback, '?') == 0) { return $social_callback . "?social_session_key=" . $network_session->getSessionKey(); } else { return $social_callback . "?social_session_key=" . $network_session->getSessionKey(); } } }