示例#1
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;
 }
 /**
  * 
  * @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;
 }