/**
  * Renders the fbml into text and returns it.
  *
  * @param RingsideSocialSession $network_session
  * @param unknown_type $fbmlText
  * @return unknown
  */
 public function render(RingsideSocialSession $network_session, $fbmlText)
 {
     $response = array();
     $error = null;
     // Exceptions are valid FBML and should be returned
     // to the end user.
     //try {
     $api_key = $this->getParam('api_key');
     // build a Social Session to get the properties for the api key passed in
     $apiSessionKey = RingsideSocialUtils::getApiSessionKey(RingsideSocialConfig::$apiKey, RingsideSocialConfig::$secretKey, $network_session);
     $apiClientSocial = new RingsideApiClientsRest(RingsideSocialConfig::$apiKey, RingsideSocialConfig::$secretKey, $apiSessionKey);
     // Get the app properties
     $result = $apiClientSocial->admin_getAppProperties("application_id,application_name,api_key,secret_key,callback_url", null, null, $api_key);
     $secret = $result['secret_key'];
     $app_id = $result['application_id'];
     // Now create the real session for this api
     $session_key = RingsideSocialUtils::getApiSessionKey($api_key, $secret, $network_session);
     $restClient = new RingsideApiClientsRest($api_key, $secret, $session_key);
     $text = $this->renderFbml($fbmlText, $network_session, $restClient, $app_id);
     if (!empty($text)) {
         $response['content'] = $text;
     }
     //		} catch ( Exception $exception ) {
     //			error_log( "Exception : " . $exception->getMessage()." \n".$exception->getTraceAsString() );
     //			$error = RingsideSocialUtils::SOCIAL_ERROR_RENDER_EXCEPTION;
     //		}
     if ($error != null) {
         $response['error'] = $error;
     }
     return $response;
 }
 /**
  * Handle the trust situation for a web application, this
  * will force redirects to canvas or callback url as specified. 
  *
  * @param Application $application
  * @param int $uid
  * @param boolean $infinite
  * @param boolean $canvas
  * @param string next
  * @return boolean success failure
  */
 function handleWebAppTrust($apikey, $application, $uid, $infinite, $canvas, $trust, $next)
 {
     $this->trustUser($application, $uid, $infinite);
     try {
         // Get a client which is represents this SOCIAL engine to API relationship
         $apiSessionKey = RingsideSocialUtils::getApiSessionKey(RingsideSocialConfig::$apiKey, RingsideSocialConfig::$secretKey, $application->getSocialSession());
         $apiClientSocial = new RingsideApiClientsRest(RingsideSocialConfig::$apiKey, RingsideSocialConfig::$secretKey, $apiSessionKey, null, RingsideSocialConfig::$apiKey);
         // Get information about a given application.
         $result = $apiClientSocial->admin_getAppProperties("application_id,secret_key,canvas_url,callback_url", null, null, $apikey);
         $canvas_url = isset($result['canvas_url']) ? $result['canvas_url'] : "";
         $callback_url = isset($result['callback_url']) ? $result['callback_url'] : "";
         $secret = isset($result['secret_key']) ? $result['secret_key'] : "";
         if ($canvas === true) {
             $trust_info = $apiClientSocial->admin_getTrustInfo(array(RingsideSocialConfig::$apiKey));
             $canvas_root = $trust_info[0]['trust_canvas_url'];
             $this->redirect = $canvas_root . '/' . $canvas_url . $next;
         } else {
             // get the clients applications and create
             $appClient = new RingsideApiClientsRest($apikey, $secret, null, null, RingsideSocialConfig::$apiKey);
             $token = $appClient->auth_createToken($infinite);
             $appClient->auth_approveToken($uid);
             if (strpos($next, "?") === false) {
                 $next = $next . "?";
             } else {
                 if (strpos($next, "&") !== false) {
                     $next = $next . "&";
                 }
             }
             if ($trust === true) {
                 $redir = $next . "auth_token=" . $token;
             } else {
                 if (strpos($next, $callback_url) === 0) {
                     $redir = $next . "auth_token=" . $token;
                 } else {
                     $redir = $callback_url . $next . "auth_token=" . $token;
                 }
             }
             $this->redirect = $redir;
         }
         return true;
     } catch (Exception $e) {
         error_log($e->getMessage());
         $this->removeTrust($application, $uid);
         return false;
     }
 }