Beispiel #1
0
 /**
  * Sends the specified content as a file to the browser.
  *
  * Note that this method only prepares the response for file sending. The file is not sent
  * until {@see \rock\response\Response::send()} is called explicitly or implicitly. The latter is done after you return from a controller action.
  *
  * @param string $content the content to be sent.
  *                        The existing {@see \rock\response\Response::$content} will be discarded.
  * @param string $attachmentName the file name shown to the user.
  * @param array $options additional options for sending the file. The following options are supported:
  *
  *  - `mimeType`: the MIME type of the content. Defaults to 'application/octet-stream'.
  *  - `inline`: boolean, whether the browser should open the file within the browser window. Defaults to false,
  *     meaning a download dialog will pop up.
  *
  * @return static the response object itself
  * @throws ResponseException if the requested range is not satisfiable
  */
 public function sendContentAsFile($content, $attachmentName, $options = [])
 {
     $headers = $this->getHeaders();
     $contentLength = StringHelper::byteLength($content);
     $range = $this->getHttpRange($contentLength);
     if ($range === false) {
         $headers->set('Content-Range', "bytes */{$contentLength}");
         throw new ResponseException('Requested range not satisfiable');
     }
     $mimeType = isset($options['mimeType']) ? $options['mimeType'] : 'application/octet-stream';
     $this->setDownloadHeaders($attachmentName, $mimeType, !empty($options['inline']), $contentLength);
     list($begin, $end) = $range;
     if ($begin != 0 || $end != $contentLength - 1) {
         $this->setStatusCode(206);
         $headers->set('Content-Range', "bytes {$begin}-{$end}/{$contentLength}");
         $this->content = StringHelper::byteSubstr($content, $begin, $end - $begin + 1);
     } else {
         $this->setStatusCode(200);
         $this->content = $content;
     }
     $this->format = self::FORMAT_RAW;
     return $this;
 }
Beispiel #2
0
 /**
  * Performs string comparison using timing attack resistant approach.
  * @see http://codereview.stackexchange.com/questions/13512
  * @param string $expected string to compare.
  * @param string $actual user-supplied string.
  * @return boolean whether strings are equal.
  */
 public function compareString($expected, $actual)
 {
     $expected .= "";
     $actual .= "";
     $expectedLength = StringHelper::byteLength($expected);
     $actualLength = StringHelper::byteLength($actual);
     $diff = $expectedLength - $actualLength;
     for ($i = 0; $i < $actualLength; $i++) {
         $diff |= ord($actual[$i]) ^ ord($expected[$i % $expectedLength]);
     }
     return $diff === 0;
 }