Exemplo n.º 1
0
 /**
  * Handle an incoming request.
  *
  * @param  Request $request
  * @param  \Closure $next
  * @return Response
  * @throws InvalidCsrfTokenException
  */
 public function handle(Request $request, Closure $next) : Response
 {
     $cookieData = $request->cookie('csrfToken');
     if ($cookieData) {
         $this->_token = $cookieData;
     }
     $createCookie = false;
     if ($request->method() == 'GET' and $cookieData === null) {
         $this->_token = hash('sha1', Text::uuid());
         $createCookie = true;
     }
     if (in_array($request->method(), ['PATCH', 'PUT', 'POST', 'DELETE'])) {
         $post = $request->data['_csrfToken'];
         $header = $request->header('X-CSRF-Token');
         if (empty($cookieData)) {
             throw new InvalidCsrfTokenException('Missing CSRF token cookie');
         }
         if ($post !== $cookieData and $header !== $cookieData) {
             throw new InvalidCsrfTokenException('CSRF token mismatch');
         }
     }
     $response = $next($request);
     if ($createCookie) {
         $response->cookie('csrfToken', $this->_token);
     }
     return $response;
 }
Exemplo n.º 2
0
 /**
  * Checks the filesize
  *
  * @param string|array $check Value to check.
  * @param string|null $operator See `BasicProvider::comparison()`.
  * @param int|string|null $size Size in bytes or human readable string like '5MB'.
  * @return bool Success
  */
 public function fileSize($check, $operator = null, $size = null)
 {
     if (is_array($check) && isset($check['tmp_name'])) {
         $check = $check['tmp_name'];
     }
     if (is_string($size)) {
         $size = Text::parseFileSize($size);
     }
     $filesize = filesize($check);
     return $this->comparison($filesize, $operator, $size);
 }
Exemplo n.º 3
0
 /**
  * Check if a shell class exists for the given name.
  *
  * @param string $shell The shell name to look for.
  * @return string|bool Either the classname or false.
  */
 protected function _shellExists($shell)
 {
     $text = new Text($shell);
     $shell = $text->camelize();
     $class = __NAMESPACE__ . "\\Shell\\{$shell}Shell";
     if (class_exists($class)) {
         return $class;
     }
     return false;
 }
Exemplo n.º 4
0
 /**
  * Takes a processed array of data from an error and displays it in the chosen format.
  *
  * @param string $data Data to output.
  * @return void
  */
 public function outputError($data)
 {
     $defaults = ['level' => 0, 'error' => 0, 'code' => 0, 'description' => '', 'file' => '', 'line' => 0, 'context' => [], 'start' => 2];
     $data += $defaults;
     $files = $this->trace(['start' => $data['start'], 'format' => 'points']);
     $code = '';
     $file = null;
     if (isset($files[0]['file'])) {
         $file = $files[0];
     } elseif (isset($files[1]['file'])) {
         $file = $files[1];
     }
     if ($file) {
         $code = $this->excerpt($file['file'], $file['line'] - 1, 1);
     }
     $trace = $this->trace(['start' => $data['start'], 'depth' => '20']);
     $insertOpts = ['before' => '{:', 'after' => '}'];
     $context = [];
     $links = [];
     $info = '';
     foreach ((array) $data['context'] as $var => $value) {
         $context[] = "\${$var} = " . $this->exportVar($value, 3);
     }
     switch ($this->_outputFormat) {
         case false:
             $this->_data[] = compact('context', 'trace') + $data;
             return;
         case 'log':
             $this->log(compact('context', 'trace') + $data);
             return;
     }
     $data['trace'] = $trace;
     $data['id'] = 'cakeErr' . uniqid();
     $tpl = $this->_templates[$this->_outputFormat] + $this->_templates['base'];
     if (isset($tpl['links'])) {
         foreach ($tpl['links'] as $key => $val) {
             $links[$key] = Text::insert($val, $data, $insertOpts);
         }
     }
     if (!empty($tpl['escapeContext'])) {
         $context = h($context);
     }
     $infoData = compact('code', 'context', 'trace');
     foreach ($infoData as $key => $value) {
         if (empty($value) || !isset($tpl[$key])) {
             continue;
         }
         if (is_array($value)) {
             $value = implode("\n", $value);
         }
         $info .= Text::insert($tpl[$key], [$key => $value] + $data, $insertOpts);
     }
     $links = implode(' ', $links);
     if (isset($tpl['callback']) && is_callable($tpl['callback'])) {
         return call_user_func($tpl['callback'], $data, compact('links', 'info'));
     }
     echo Text::insert($tpl['error'], compact('links', 'info') + $data, $insertOpts);
 }
Exemplo n.º 5
0
 /**
  * testHumanization method
  *
  * @return void
  */
 public function testHumanization()
 {
     $testArray = ["posts" => "Posts", "posts_tags" => "Posts Tags", "file_systems" => "File Systems", null => '', false => '', 'hello_wörld' => 'Hello Wörld', '福岡_city' => '福岡 City'];
     foreach ($testArray as $key => $expected) {
         $text = new Text($key);
         $this->assertSame($expected, $text->humanize());
     }
 }
Exemplo n.º 6
0
 /**
  * Wraps a complete block of text to a specific width, can optionally wrap
  * at word breaks.
  *
  * ### Options
  *
  * - `width` The width to wrap to. Defaults to 72.
  * - `wordWrap` Only wrap on words breaks (spaces) Defaults to true.
  * - `indent` String to indent with. Defaults to null.
  * - `indentAt` 0 based index to start indenting at. Defaults to 0.
  *
  * @param array|int $options Array of options to use, or an integer to wrap the text to.
  * @return string Formatted text.
  */
 public function wrapBlock($options = [])
 {
     if (is_numeric($options)) {
         $options = ['width' => $options];
     }
     $options += ['width' => 72, 'wordWrap' => true, 'indent' => null, 'indentAt' => 0];
     if (!empty($options['indentAt']) && $options['indentAt'] === 0) {
         $indentLength = !empty($options['indent']) ? strlen($options['indent']) : 0;
         $options['width'] = $options['width'] - $indentLength;
         return $this->wrap($options);
     }
     $wrapped = $this->wrap($options);
     if (!empty($options['indent'])) {
         $indentationLength = mb_strlen($options['indent']);
         $chunks = explode("\n", $wrapped);
         $count = count($chunks);
         if ($count < 2) {
             return $wrapped;
         }
         $toRewrap = '';
         for ($i = $options['indentAt']; $i < $count; $i++) {
             $toRewrap .= mb_substr($chunks[$i], $indentationLength) . ' ';
             unset($chunks[$i]);
         }
         $options['width'] -= $indentationLength;
         $options['indentAt'] = 0;
         $toRewrapText = new Text($toRewrap);
         $rewrapped = $toRewrapText->wrap($options);
         $newChunks = explode("\n", $rewrapped);
         $chunks = array_merge($chunks, $newChunks);
         $wrapped = implode("\n", $chunks);
     }
     return $wrapped;
 }
Exemplo n.º 7
0
 /**
  * Get random bytes from a secure source.
  *
  * This method will fall back to an insecure source an trigger a warning
  * if it cannot find a secure source of random data.
  *
  * @param int $length The number of bytes you want.
  * @return string Random bytes in binary.
  */
 public static function randomBytes($length)
 {
     if (function_exists('random_bytes')) {
         return random_bytes($length);
     }
     if (function_exists('openssl_random_pseudo_bytes')) {
         $bytes = openssl_random_pseudo_bytes($length, $strongSource);
         if (!$strongSource) {
             trigger_error('openssl was unable to use a strong source of entropy. ' . 'Consider updating your system libraries, or ensuring ' . 'you have more available entropy.', E_USER_WARNING);
         }
         return $bytes;
     }
     trigger_error('You do not have a safe source of random data available. ' . 'Install either the openssl extension, or paragonie/random_compat. ' . 'Falling back to an insecure random source.', E_USER_WARNING);
     $bytes = '';
     $byteLength = 0;
     while ($byteLength < $length) {
         $bytes .= hash('sha512', Text::uuid() . uniqid(mt_rand(), true));
         $byteLength = strlen($bytes);
     }
     return substr($bytes, 0, $length);
 }