/** * Returns the session key if there is a logged in user * * @return string|null The session key if there is a logged in user, NULL otherwise */ public function getSessionKey() { $cookies = Cookies::getList(Cookies::TYPE_SERVER); $cookieKey = '_s'; $key = isset($cookies[$cookieKey]) ? (string) $cookies[$cookieKey] : ''; return strlen((string) $key) > 70 ? $key : null; }
/** * Returns the id of the current active theme or theme in preview * * @return string The id of the current active theme or theme in preview */ public function getID() { if (!isset(self::$cache['id'])) { $cookies = Cookies::getList(Cookies::TYPE_SERVER); self::$cache['id'] = isset($cookies['tmpr']) ? $cookies['tmpr'] : InternalData\Themes::getActiveThemeID(); } return self::$cache['id']; }
static function sendRequest($url, $data = null, $sendCookies = false) { $app = App::$instance; if (!is_string($url)) { throw new \InvalidArgumentException(''); } if ($data !== null && !is_array($data)) { throw new \InvalidArgumentException(''); } if (!is_bool($sendCookies)) { throw new \InvalidArgumentException(''); } if (!is_array($data)) { $data = []; } $data['responseType'] = 'jsongz'; if (isset($data['_ajaxreferer'])) { $data['_ajaxreferer'] = str_replace($app->request->base . '/', Options::$serverUrl, $data['_ajaxreferer']); } $cookies = $sendCookies ? Cookies::getList(Cookies::TYPE_SERVER) : []; $send = function ($requestData = [], $counter = 1) use(&$send, $app, $url, $data, $cookies) { if ($counter > 10) { throw new \Exception('Too much requests'); } $response = self::makeRequest($url, array_merge($data, $requestData, ['requestNumber' => $counter]), $cookies); if (self::isRetryResponse($response)) { return $response; } $responseData = json_decode($response['body'], true); if (!is_array($responseData) || !array_key_exists('response', $responseData)) { throw new \Exception('Invalid response. Body: ' . $response['body']); } $responseData = $responseData['response']; $response['body'] = $responseData['body']; $responseMeta = $responseData['meta']; if (Options::$logServerRequestsData) { if (strlen($app->config->logsDir) > 0) { $log = "Bear CMS response data:\n"; $log .= 'Data: ' . trim(print_r($responseData, true)); $app->logger->log('info', $log); } } $resend = isset($responseMeta['resend']) && (int) $responseMeta['resend'] > 0; $resendRequestData = []; if (isset($responseMeta['commands']) && is_array($responseMeta['commands'])) { $commandsResults = []; foreach ($responseMeta['commands'] as $commandData) { if (isset($commandData['name']) && isset($commandData['data'])) { $commandResult = ''; $callback = ['\\BearCMS\\Internal\\ServerCommands', $commandData['name']]; if (is_callable($callback)) { $commandResult = call_user_func($callback, $commandData['data'], $response); } if (isset($commandData['key'])) { $commandsResults[$commandData['key']] = $commandResult; } } } if ($resend) { $resendRequestData['commandsResults'] = json_encode($commandsResults, JSON_UNESCAPED_UNICODE); } } if (isset($responseMeta['clientEvents'])) { $resendRequestData['clientEvents'] = $responseMeta['clientEvents']; $resend = true; } if (isset($responseMeta['currentUser'])) { $currentUserData = $responseMeta['currentUser']; $app->data->set(['key' => '.temp/bearcms/userkeys/' . md5($currentUserData['key']), 'body' => $currentUserData['id']]); } if (isset($responseMeta['clientEvents'])) { $responseBody = $response['body']; // Can be changed in a command } if ($resend) { $response = $send($resendRequestData, $counter + 1); } if (isset($responseMeta['clientEvents'])) { $response['bodyPrefix'] = $responseBody; } return $response; }; $response = $send(); if ($sendCookies) { Cookies::setList(Cookies::TYPE_SERVER, Cookies::parseServerCookies($response['header'])); } return $response; }
} if (is_array($elementsEditorData) && isset($elementsEditorData['result']) && is_array($elementsEditorData['result']) && isset($elementsEditorData['result']['content'])) { $domDocument = new HTML5DOMDocument(); $domDocument->loadHTML($content); $domDocument->insertHTML($elementsEditorData['result']['content']); $content = $domDocument->saveHTML(); } else { $response = new App\Response\TemporaryUnavailable(); } } // It's needed even when there is no editable zone on the current page (editing a blog post for instance) $domDocument = new HTML5DOMDocument(); $domDocument->loadHTML($content); $domDocument->insertHTML('<html><body><script src="' . htmlentities($context->assets->getUrl('assets/HTML5DOMDocument.min.js')) . '"></script></body></html>'); $content = $domDocument->saveHTML(); $content = Server::updateAssetsUrls($content, false); if (strpos($content, '{body}') !== false) { $content = str_replace('{body}', '<component src="data:base64,' . base64_encode($response->content) . '"/>', $content); } elseif (strpos($content, '{jsonEncodedBody}') !== false) { $content = str_replace('{jsonEncodedBody}', json_encode($app->components->process($response->content)), $content); } $response->content = $app->components->process($content); } else { $response = new App\Response\TemporaryUnavailable(); } }, ['priority' => 1000]); if (Options::hasServer() && (Options::hasFeature('USERS') || Options::hasFeature('USERS_LOGIN_*'))) { $app->hooks->add('responseCreated', function () { Cookies::update(); }, ['priority' => 1001]); }
/** * * @param array $data * @param array $response * @throws \Exception */ static function temporaryRedirect($data, $response) { $app = App::$instance; if (!isset($data['url'])) { throw new \Exception(''); } Cookies::setList(Cookies::TYPE_SERVER, Cookies::parseServerCookies($response['header'])); Cookies::update(); $app->respond(new App\Response\TemporaryRedirect($data['url'])); exit; }