Example #1
0
 /**
  * @param string     $message
  * @param null|array $args
  * @param bool       $end_line
  * @param bool       $output
  */
 static function writeReplace($message, $args = NULL, $end_line = FALSE, $output = TRUE)
 {
     if (Kohana_Helpers_Core::isCLI() && self::classExists()) {
         $args = Kohana_Helpers_Arr::asArray($args);
         if ($output) {
             Kohana_Minion_CLI::write_replace(self::date() . strtr($message, $args), $end_line);
         } elseif (FALSE !== ($logLevel = self::config('cli.log.replace', static::$logReplace))) {
             Kohana::$log->add($logLevel, $message, $args);
         }
     }
 }
Example #2
0
 /**
  * @param Exception $e
  *
  * @return array|null
  */
 public static function exception(Exception $e = NULL)
 {
     if ($e instanceof Exception) {
         $data = ['message' => Helpers_Text::trimAsNULL($e->getMessage()), 'code' => $e->getCode()];
         if (!Kohana_Helpers_Core::isProduction()) {
             $data = Kohana_Helpers_Arr::merge($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => $e->getTrace(), 'previous' => Helpers_Arr::exception($e->getPrevious())]);
         }
     } else {
         $data = NULL;
     }
     return $data;
 }
Example #3
0
 /**
  * @param null|string $input
  * @param null|bool   $parseFiles
  *
  * @return array
  */
 public static function parseMultiPartContent($input = NULL, $parseFiles = NULL)
 {
     NULL !== $input or $input = static::current()->body();
     NULL !== $parseFiles or $parseFiles = static::config('request.data.parseBodyFiles.enabled', FALSE);
     $data = [];
     $files = $parseFiles ? [] : NULL;
     // grab multipart boundary from content type header
     preg_match('/boundary=["]*([^"\\s;]+)/', $_SERVER['CONTENT_TYPE'], $matches);
     if (count($matches)) {
         $boundary = preg_quote($matches[1]);
         // split content by boundary and get rid of last -- element
         $blocks = preg_split("/-+{$boundary}/", $input);
         // loop data blocks
         foreach ($blocks as $block) {
             if (empty($block)) {
                 continue;
             }
             $parts = preg_split('/[\\r\\n][\\r\\n]/', trim($block, "\r\n"), 2, PREG_SPLIT_NO_EMPTY);
             if (count($parts) != 2) {
                 continue;
             }
             list($raw_headers, $input) = $parts;
             if (preg_match('/name="([^"]+)"(; *filename="([^"]+)")?/', $raw_headers, $matches)) {
                 $name = rawurldecode($matches[1]);
                 $filename = isset($matches[3]) ? $matches[3] : NULL;
                 if (!isset($filename)) {
                     $input = Helpers_Text::trimAsNULL($input);
                     $_tmp = "{$name}={$input}";
                     $_data = NULL;
                     parse_str($_tmp, $_data);
                     $data = Helpers_Arr::merge($data, $_data);
                 } elseif (isset($files)) {
                     $_tmpname = tempnam(NULL, 'tmp');
                     if (FALSE !== $_tmpname) {
                         if (preg_match('@^Content-Type:@im', $input)) {
                             $input = trim(preg_replace('@^Content-Type:[^\\n]*@i', "", $input), "\r\n");
                         }
                         file_put_contents($_tmpname, $input);
                         chmod($_tmpname, static::config('request.data.parseBodyFiles.chmod', 0666));
                         $files[$name] = ['name' => $filename, 'type' => mime_content_type($_tmpname), 'tmp_name' => $_tmpname, 'error' => UPLOAD_ERR_OK, 'size' => filesize($_tmpname)];
                     }
                 }
             }
         }
         if (Kohana_Helpers_Arr::count($files)) {
             foreach ($files as $fileKey => $fileInfo) {
                 $_FILES[$fileKey] = $fileInfo;
             }
             if (static::config('request.data.parseBodyFiles.deleteOnShutdown', TRUE)) {
                 register_shutdown_function(function () use($files) {
                     foreach ($files as $row) {
                         @unlink($row['tmp_name']);
                     }
                 });
             }
         }
     }
     return $data;
 }