示例#1
0
 /**
  * Adds the rate limit headers to the response
  * @param Response $response
  * @param integer $limit the maximum number of allowed requests during a period
  * @param integer $remaining the remaining number of allowed requests within the current period
  * @param integer $reset the number of seconds to wait before having maximum number of allowed requests again
  */
 public function addHeaders(Response $response, $limit, $remaining, $reset)
 {
     if ($this->sendHeaders) {
         $response->getHeaders()->set('X-Rate-Limit-Limit', $limit)->set('X-Rate-Limit-Remaining', $remaining)->set('X-Rate-Limit-Reset', $reset);
         $response->setStatusCode(429);
     }
 }
 /**
  * Formats the specified response.
  * @param Response $response the response to be formatted.
  */
 public function format(Response $response)
 {
     $charset = $this->encoding === null ? $response->charset : $this->encoding;
     if (stripos($this->contentType, 'charset') === false) {
         $this->contentType .= '; charset=' . $charset;
     }
     $response->getHeaders()->set('Content-Type', $this->contentType);
     $data = $response->data;
     if (isset($data['language'])) {
         $this->_feedWriter->setChannelElement('language', $data['language']);
     }
     if (!isset($data['pubDate'])) {
         $this->_feedWriter->setChannelElement('pubDate', date(DATE_RSS, time()));
     }
     if (isset($data['title'])) {
         $this->_feedWriter->setTitle($data['title']);
     }
     if (isset($data['link'])) {
         $this->_feedWriter->setLink($data['link']);
     }
     if (isset($data['description'])) {
         $this->_feedWriter->setDescription($data['description']);
     }
     if (isset($data['items']) && is_array($data['items'])) {
         if ($data['items']) {
             foreach ($data['items'] as $item) {
                 $newItem = $this->_feedWriter->createNewItem();
                 $newItem->addElementArray($item);
                 $this->_feedWriter->addItem($newItem);
             }
         }
     }
     $response->content = $this->_feedWriter->generateFeed();
 }
示例#3
0
 /**
  * Formats the specified response.
  * @param Response $response the response to be formatted.
  */
 public function format($response)
 {
     if (stripos($this->contentType, 'charset') === false) {
         $this->contentType .= '; charset=' . $response->charset;
     }
     $response->getHeaders()->set('Content-Type', $this->contentType);
     $response->content = $response->data;
 }
示例#4
0
 /**
  * Formats response data in JSONP format.
  *
  * @param Response $response
  * @throws ResponseException
  */
 protected function formatJsonp($response)
 {
     $response->getHeaders()->set('Content-Type', 'application/javascript; charset=UTF-8');
     if (is_array($response->data) && isset($response->data['data'], $response->data['callback'])) {
         $response->content = sprintf('%s(%s);', $response->data['callback'], Json::encode($response->data['data']));
     } else {
         $response->content = '';
         throw new ResponseException("The 'jsonp' response requires that the data be an array consisting of both 'data' and 'callback' elements.");
     }
 }
 /**
  * Formats the specified response.
  * @param Response $response the response to be formatted.
  */
 public function format(Response $response)
 {
     $charset = $this->encoding === null ? $response->charset : $this->encoding;
     if (stripos($this->contentType, 'charset') === false) {
         $this->contentType .= '; charset=' . $charset;
     }
     $response->getHeaders()->set('Content-Type', $this->contentType);
     foreach ($response->data as $value) {
         $this->_sitemap->add($value['loc'], Helper::getValue($value['lastmod']), Helper::getValue($value['changefreq']), isset($value['priority']) ? $value['priority'] : null);
     }
     $response->content = $this->_sitemap->toString();
 }
示例#6
0
 /**
  * Formats the specified response.
  * @param Response $response the response to be formatted.
  */
 public function format($response)
 {
     $charset = $this->encoding === null ? $response->charset : $this->encoding;
     if (stripos($this->contentType, 'charset') === false) {
         $this->contentType .= '; charset=' . $charset;
     }
     $response->getHeaders()->set('Content-Type', $this->contentType);
     $dom = new DOMDocument($this->version, $charset);
     $root = new DOMElement($this->rootTag);
     $dom->appendChild($root);
     $this->buildXml($root, $response->data);
     $response->content = trim($dom->saveXML());
 }
示例#7
0
文件: Route.php 项目: romeoz/rock
 public function init()
 {
     $this->request = Instance::ensure($this->request, Request::className());
     $this->response = Instance::ensure($this->response, Response::className());
     $this->calculateData();
     $handlers = $this->defaultRESTHandlers();
     $this->RESTHandlers = empty($this->RESTHandlers) ? $handlers : array_merge($handlers, $this->RESTHandlers);
 }
示例#8
0
 /**
  * Match by Custom
  *
  * @param array $rule array data of access
  * @return bool
  */
 protected function matchCustom(array $rule)
 {
     $rule['custom'][1] = Helper::getValue($rule['custom'][1], [], true);
     list($function, $args) = $rule['custom'];
     $result = (bool) call_user_func($function, array_merge(['owner' => $this->owner], $args));
     if (!$result && $this->sendHeaders && $this->response instanceof \rock\response\Response) {
         $this->response->status403();
     }
     return $result;
 }
示例#9
0
 protected function convertResponse(ResponseInterface $psrResponse, Request $request)
 {
     $request->setContentType($psrResponse->getHeaderLine('Content-Type'));
     $this->response->request = $request;
     $this->response->version = $psrResponse->getProtocolVersion();
     $this->response->setStatusCode($psrResponse->getStatusCode(), $psrResponse->getReasonPhrase());
     foreach ($psrResponse->getHeaders() as $name => $value) {
         $this->response->getHeaders()->setDefault($name, $value);
     }
     $this->response->content = $psrResponse->getBody()->getContents();
     return $this->response;
 }
示例#10
0
文件: classes.php 项目: romeoz/rock
        return !Rock::$app->user->isGuest();
    }
    return \rock\helpers\ArrayHelper::getValue(Rock::$app->user->getAll(), $keys);
}, 'call' => function (array $call, array $params = [], Template $template) {
    if (!isset($call[1])) {
        $call[1] = null;
    }
    list($class, $method) = $call;
    if ($class === 'context') {
        $object = $template->context;
        $function = [$object, $method];
    } elseif (function_exists($class) && !$class instanceof \Closure) {
        return call_user_func_array($class, $params);
    } else {
        $object = \rock\di\Container::load($class);
        if (!method_exists($object, $method)) {
            throw new \rock\base\BaseException(\rock\base\BaseException::UNKNOWN_METHOD, ['method' => "{$class}::{$method}"]);
        }
        $function = [$object, $method];
    }
    return call_user_func_array($function, $params);
}], 'title' => 'Demo', 'metaTags' => ['charset' => '<meta charset="' . Rock::$app->charset . '" />'], 'linkTags' => ['favicon' => '<link rel="Shortcut Icon" type="image/x-icon" href="/favicon.ico?10">'], 'snippets' => ['request.get' => ['class' => \rock\snippets\request\Get::className()], 'request.post' => ['class' => \rock\snippets\request\Post::className()], 'csrf' => ['class' => \rock\snippets\CSRF::className()], 'captchaView' => ['class' => \rock\snippets\CaptchaView::className()], 'activeForm' => ['class' => \rock\snippets\ActiveForm::className()]]], 'execute' => ['class' => \rock\execute\CacheExecute::className()], 'i18n' => ['class' => \rock\i18n\i18n::className(), 'pathsDicts' => ['ru' => ['@rock/messages/ru/lang.php', '@rock/messages/ru/validate.php'], 'en' => ['@rock/messages/en/lang.php', '@rock/messages/en/validate.php']], 'locale' => [\rock\LocaleProperties::className(), 'locale']], 'date' => ['class' => \rock\date\DateTime::className(), 'locale' => [\rock\LocaleProperties::className(), 'locale'], 'formats' => ['dmy' => function (\rock\date\DateTime $dateTime) {
    $nowYear = date('Y');
    $lastYear = $dateTime->format('Y');
    return $nowYear > $lastYear ? $dateTime->format('j F Y') : $dateTime->format('d F');
}, 'dmyhm' => function (\rock\date\DateTime $dateTime) {
    $nowYear = date('Y');
    $lastYear = $dateTime->format('Y');
    return $nowYear > $lastYear ? $dateTime->format('j F Y H:i') : $dateTime->format('j F H:i');
}]], 'mail' => ['class' => \rock\mail\Mail::className(), 'From' => 'support@' . (new \rock\request\Request())->getHost(), 'FromName' => 'Rock Framework'], 'url' => ['class' => \rock\url\Url::className()], 'request' => ['class' => \rock\request\Request::className(), 'locale' => [\rock\LocaleProperties::className(), 'locale']], 'response' => ['class' => \rock\response\Response::className(), 'locale' => [\rock\LocaleProperties::className(), 'locale']], 'htmlResponseFormatter' => ['class' => \rock\response\HtmlResponseFormatter::className()], 'jsonResponseFormatter' => ['class' => \rock\response\JsonResponseFormatter::className()], 'xmlResponseFormatter' => ['class' => \rock\response\XmlResponseFormatter::className()], 'rssResponseFormatter' => ['class' => \rock\response\RssResponseFormatter::className()], 'session' => ['class' => \rock\session\Session::className(), 'cookieParams' => ['httponly' => true, 'lifetime' => 60 * 60 * 24 * 60, 'setUseCookies' => \rock\session\Session::USE_ONLY_COOKIES]], 'cookie' => ['class' => \rock\cookie\Cookie::className()], 'security' => ['class' => Security::className()], 'sanitize' => ['class' => \rock\sanitize\Sanitize::className()], 'validate' => ['class' => \rock\validate\Validate::className(), 'locale' => [\rock\LocaleProperties::className(), 'locale']], 'csrf' => ['class' => \rock\csrf\CSRF::className()], 'captcha' => ['class' => \rock\captcha\Captcha::className(), 'length' => 0, 'whiteNoiseDensity' => 1 / 6, 'blackNoiseDensity' => 1 / 30], 'user' => ['class' => \rock\user\User::className(), 'container' => 'user'], 'rbac' => ['class' => \rock\rbac\DBManager::className()], 'log' => ['class' => \rock\log\Log::className()], Role::className() => ['class' => Role::className()], Permission::className() => ['class' => Permission::className()]], require __DIR__ . '/widgets.php');
示例#11
0
文件: Captcha.php 项目: romeoz/rock
 /**
  * Returns image captcha.
  *
  * @param bool $session
  * @param Response $response
  * @return null
  */
 public function getImage($session = true, Response $response)
 {
     if (!($data = $this->generate($session))) {
         return null;
     }
     $response->getHeaders()->set('Pragma', 'public')->set('Expires', '0')->set('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')->set('Content-Transfer-Encoding', 'binary')->set('Content-type', $data['mimeType']);
     return $data['image'];
 }
示例#12
0
 /**
  * Redirects the browser to the last visited page.
  *
  * You can use this method in an action by returning the {@see \rock\response\Response} directly:
  *
  * ```php
  * // stop executing this action and redirect to last visited page
  * return $this->goBack();
  * ```
  *
  * For this function to work you have to  {@see \rock\user\User::setReturnUrl()} in appropriate places before.
  *
  * @param string|array $defaultUrl the default return URL in case it was not set previously.
  * If this is null and the return URL was not set previously, {@see \rock\request\Request::$homeUrl} will be redirected to.
  * Please refer to {@see \rock\user\User::setReturnUrl()} on accepted format of the URL.
  * @return Response the current response object
  * @see User::getReturnUrl()
  */
 public function goBack($defaultUrl = null)
 {
     return $this->response->redirect(Rock::$app->user->getReturnUrl($defaultUrl));
 }
示例#13
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     $this->request = Instance::ensure($this->request, Request::className());
     $this->response = Instance::ensure($this->response, Response::className());
 }
示例#14
0
 /**
  * Run mode debug.
  *
  * @param Response $response
  * @return Run
  */
 protected static function debuger(Response $response = null)
 {
     $run = new Run();
     if (isset($response)) {
         switch ($response->format) {
             case Response::FORMAT_JSON:
                 $handler = new JsonResponseHandler();
                 break;
             case Response::FORMAT_XML:
                 $handler = new XmlResponseHandler();
                 break;
             default:
                 $request = new Request();
                 if ($request->isAjax() || $request->isCORS()) {
                     $handler = new JsonResponseHandler();
                 } else {
                     $handler = new PrettyPageHandler();
                 }
         }
         $run->setSendHttpCode(500);
         $response->setStatusCode(500);
         $response->send();
     } else {
         $handler = new PrettyPageHandler();
     }
     $run->pushHandler($handler);
     //$run->register();
     return $run;
 }
示例#15
0
文件: CORS.php 项目: romeoz/rock
 /**
  * Adds the CORS headers to the response
  * @param Response $response
  * @param array CORS headers which have been computed
  */
 public function addCorsHeaders($response, $headers)
 {
     if (!empty($headers)) {
         $responseHeaders = $response->getHeaders();
         foreach ($headers as $field => $value) {
             $responseHeaders->set($field, $value);
         }
     }
 }