Пример #1
0
 /**
  * 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;
 }
Пример #2
0
 /**
  * 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'];
 }
Пример #3
0
 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;
 }
Пример #4
0
     if ($itemData[0] === 'container') {
         $requestArguments['hasEditableContainers'] = '1';
     }
 }
 $cacheKey = json_encode(['adminUI', $app->request->base, $requestArguments, $app->bearCMS->currentUser->getSessionKey(), $app->bearCMS->currentUser->getPermissions(), get_class_vars('\\BearCMS\\Internal\\Options'), $serverCookies]);
 $adminUIData = $app->cache->get($cacheKey);
 if (!is_array($adminUIData)) {
     $adminUIData = Server::call('adminui', $requestArguments, true);
     $app->cache->set($cacheKey, $adminUIData, is_array($adminUIData) && isset($adminUIData['result']) ? 99999 : 10);
 }
 if (is_array($adminUIData) && isset($adminUIData['result']) && is_array($adminUIData['result']) && isset($adminUIData['result']['content']) && strlen($adminUIData['result']['content']) > 0) {
     $content = $adminUIData['result']['content'];
     if ((Options::hasFeature('ELEMENTS') || Options::hasFeature('ELEMENTS_*')) && !empty(ElementsHelper::$editorData)) {
         $requestArguments = [];
         $requestArguments['data'] = json_encode(ElementsHelper::$editorData);
         $cacheKey = json_encode(['elementsEditor', $app->request->base, $requestArguments, $app->bearCMS->currentUser->getSessionKey(), $app->bearCMS->currentUser->getPermissions(), get_class_vars('\\BearCMS\\Internal\\Options'), Cookies::getList(Cookies::TYPE_SERVER)]);
         $elementsEditorData = $app->cache->get($cacheKey);
         if (!is_array($elementsEditorData)) {
             $elementsEditorData = Server::call('elementseditor', $requestArguments, true);
             $app->cache->set($cacheKey, $elementsEditorData, is_array($elementsEditorData) && isset($elementsEditorData['result']) ? 99999 : 10);
         }
         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)