Пример #1
0
    public function execute(RingsideSocialClientInterface $socialClient)
    {
        $coreApp = $this->canvasUrl != null ? $this->plugin($this->canvasUrl) : false;
        $text = 'empty';
        $status = 200;
        $callback = '';
        // if this is not a core (aka system) app, then make a remote call to the remote app
        // otherwise, render the results of the system app via a local call
        if ($coreApp === false) {
            $text = null;
            try {
                $adminClient = RingsideSocialUtils::getAdminClient();
                $result = $adminClient->admin_getAppProperties("application_name,use_iframe,api_key,secret_key,callback_url,application_id", $this->appId, $this->canvasUrl, null, $socialClient->getCurrentNetwork());
                $callback = isset($result['callback_url']) ? $result['callback_url'] : '';
                $apiKey = isset($result['api_key']) ? $result['api_key'] : '';
                $apiSecret = isset($result['secret_key']) ? $result['secret_key'] : '';
                $canvasType = isset($result['use_iframe']) ? $result['use_iframe'] : '';
                $applicationid = isset($result['application_id']) ? $result['application_id'] : '';
                $networkSession = $socialClient->getNetworkSession();
                $principalId = $networkSession->getPrincipalId();
                $apiSessionKeyApp = RingsideSocialUtils::getApiSessionKey($apiKey, $apiSecret, $socialClient->getNetworkSession());
                $apiClientApplication = new RingsideApiClientsRest($apiKey, $apiSecret, $apiSessionKeyApp, null, $socialClient->getCurrentNetwork());
                $isAppAdded = false;
                if ($socialClient->inSession()) {
                    $isAppAdded = $apiClientApplication->users_isAppAdded();
                    $idmaps = $apiClientApplication->users_mapToPrincipal(array($socialClient->getCurrentUser()));
                    $nuser = null;
                    if (!empty($idmaps) && null != $socialClient->getCurrentUser()) {
                        foreach ($idmaps as $idmap) {
                            if ($idmap['uid'] == $socialClient->getCurrentUser()) {
                                $nuser = $idmap['pid'];
                            }
                        }
                    }
                    // TODO: Move setting network user in network session into login.php and map.php?
                    $networkSession->setPrincipalId($nuser);
                }
                $headers = array();
                $fbmlText = $this->renderRemote($callback, $apiKey, $apiSecret, $canvasType, $isAppAdded, $apiSessionKeyApp, $socialClient, $headers, $status);
                //            error_log("Status for $callback is $status");
                if ($fbmlText !== null && !empty($fbmlText)) {
                    if (strncmp($headers['content-type'], 'text/html', 9) === 0) {
                        $this->raw = false;
                        $text = $this->renderFbml($fbmlText, $socialClient->getNetworkSession(), $apiClientApplication, $applicationid);
                        // Need $socialUrl
                        if (include 'LocalSettings.php') {
                            $extra_end_scripts = <<<EOF

<script type='text/javascript'><!--
if ( typeof Ajax != 'undefined' ) {
  Ajax.API_KEY='{$apiKey}';
  Ajax.RENDER_URL='{$socialUrl}/render.php';
  Ajax.PROXY_URL='{$socialUrl}/proxyjs.php';
}
//--></script>
EOF;
                            // These are ONLY emitted for FBML remote applications to support FBJS!
                            $text .= $extra_end_scripts;
                        }
                    } else {
                        if (strncmp($headers['content-type'], 'text/', 5) === 0) {
                            // Send all other text (text/xml, text/css, etc.) back raw
                            $this->raw = true;
                            $text = $fbmlText;
                        } else {
                            error_log("No way to handle content type " . $headers['content-type']);
                            $this->error = RingsideSocialUtils::SOCIAL_ERROR_RENDER_EXCEPTION;
                        }
                    }
                } else {
                    if ($status < 200) {
                        $text = "The application did not finish processing prior to the timeout.";
                    } else {
                        if ($status < 300) {
                            $text = "The application returned an HTTP status code of 200 but no content.";
                        } else {
                            if ($status < 400) {
                                $text = "The application returned too many redirects.";
                            } else {
                                if ($status < 500) {
                                    $text = "The application is configured to point to an incorrect page.";
                                } else {
                                    if ($status < 600) {
                                        $text = "The application encountered an error during processing.";
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception $exception) {
                error_log("Remote Render Exception : " . $exception->getMessage());
                error_log($exception->getTraceAsString());
                $this->error = RingsideSocialUtils::SOCIAL_ERROR_NO_SUCH_PAGE;
            }
        } else {
            // making a request to a local system app
            try {
                $apiSessionKey = RingsideSocialUtils::getApiSessionKey(RingsideSocialConfig::$apiKey, RingsideSocialConfig::$secretKey, $socialClient->getNetworkSession());
                $apiClientSocial = new RingsideApiClientsRest(RingsideSocialConfig::$apiKey, RingsideSocialConfig::$secretKey, $apiSessionKey);
                $callback = "System Application " . $this->canvasUrl;
                error_log("Rendering system application {$callback}");
                $fbmlText = $this->renderLocal(RingsideSocialConfig::$apiKey, RingsideSocialConfig::$secretKey, $apiSessionKey, $socialClient);
                if (isset($coreApp->canvas_type) && $coreApp->canvas_type == RingsideAppsCommon::CANVASTYPE_IFRAME) {
                    $text = $fbmlText;
                } else {
                    if ($socialClient->inSession()) {
                        $apiSessionKey = RingsideSocialUtils::getApiSessionKey(RingsideSocialConfig::$apiKey, RingsideSocialConfig::$secretKey, $socialClient->getNetworkSession());
                        $apiClientSocial = new RingsideApiClientsRest(RingsideSocialConfig::$apiKey, RingsideSocialConfig::$secretKey, $apiSessionKey);
                    }
                    $text = $this->renderFbml($fbmlText, $socialClient->getNetworkSession(), $apiClientSocial, $socialClient->getCurrentUser());
                }
            } catch (Exception $exception) {
                error_log("Remote Local Exception : " . $exception->getMessage());
                error_log($exception->getTraceAsString());
                $this->error = RingsideSocialUtils::SOCIAL_ERROR_NO_SUCH_PAGE;
            }
        }
        $response = array();
        if (!empty($text)) {
            $response['content'] = $text;
        }
        if ($this->iframe != null) {
            $response['iframe'] = $this->iframe;
        }
        if ($this->redirect != null) {
            $response['redirect'] = $this->redirect;
        }
        if ($this->error != null) {
            $response['error'] = $this->error;
        }
        $response['status'] = $status;
        if (empty($response)) {
            $response['error'] = "The URL {$callback} returned no data";
        }
        $response['raw'] = $this->raw;
        return $response;
    }