Example #1
0
 function __construct(Request $request, $filename, $force_download = false)
 {
     if (!is_file($filename) || !is_readable($filename)) {
         throw new \Hydra\Exception\NotFoundHttpException("File not found: {$filename}");
     }
     $this->isPhp = preg_match('/\\.php$/', $filename);
     $this->filename = $filename;
     if ($force_download) {
         $this->headers['Content-Type'] = 'application/octet-stream';
     } else {
         // Try to guess Content-Type
         if (!$this->isPhp) {
             $this->headers['Content-Type'] = MimeTypeGuesser::getInstance()->guess($filename);
         } elseif ($request->app->config->response['guessPhpContentType']) {
             $ext = Utils::fileExt(substr($filename, 0, -4));
             if (isset($request->app->mimetypes[$ext])) {
                 $this->headers['Content-Type'] = $request->app->mimetypes[$ext];
             }
         }
     }
     if (!$this->isPhp) {
         $this->headers['Content-Length'] = filesize($filename);
     }
     $response = $this;
     parent::__construct($request, function () use($response) {
         // We don't want any left-over output when sending a file.
         ob_end_clean();
         if ($response->isPhp) {
             $request = $response->request;
             $app = $response->app;
             require $response->filename;
         } else {
             if ($response->app->config->response['XSendfile']) {
                 $filename = realpath($response->filename);
                 header("X-Sendfile: {$filename}");
             } else {
                 set_time_limit(0);
                 //Set the execution time to infinite.
                 readfile($response->filename);
             }
         }
     });
 }
Example #2
0
            $p += 2;
            return $p < 100 ? "{$p}%" : true;
        } else {
            // Error handling test.
            throw new \RuntimeException('Some error thrown.');
        }
    };
}
// Better exception output for JSON, JS and other non-HTML requests
$hooks['app.exception'][1000][] = function (ExceptionHandler $handler, \Exception $ex, App $app) {
    $request = $app->mainRequest;
    $format = null;
    $debug = $app->core->debug;
    // Get format from path info
    if ($request) {
        $format = Utils::fileExt($request->path);
    }
    // Get format from active route
    if ($request && isset($request->params)) {
        $format = $request->params['format'];
    }
    // Get format from response
    if ($request && $request->response) {
        $response = $request->response;
        if ($response->format) {
            $format = $request->response->format;
        }
    }
    // Return a valid JSON response in case of error
    if ($format == 'json') {
        if (!headers_sent()) {
Example #3
0
File: Html.php Project: z7/hydra
 static function dump($data, $indent = null)
 {
     $em = false;
     $type = 'unknown';
     $numeric = true;
     $prefix = $indent > 0 ? str_repeat("\t", $indent) : '';
     if (is_object($data)) {
         $type = 'object';
         if ($data instanceof \Closure) {
             $data = 'Closure';
             $em = true;
         } elseif (in_array($data, self::$_objects)) {
             $data = 'Object recursion';
             $em = true;
         } elseif (!method_exists($data, '__toString')) {
             self::$_objects[] = $data;
             $data = get_object_vars($data);
             $numeric = false;
             if (!$data) {
                 $data = 'Object';
                 $em = true;
             }
         }
     }
     if (is_array($data)) {
         if ($type == 'unknown') {
             $type = 'array';
         }
         $lines = array();
         if ($numeric) {
             $numeric = Utils::arrayIsNumeric($data);
         }
         foreach ($data as $key => $value) {
             $lines[] = ($numeric ? '' : "<strong>{$key}</strong>: ") . self::dump($value, $indent + 1);
         }
         $data = implode("</li>\n{$prefix}<li>", $lines);
         if ($data) {
             $data = "<li>{$data}</li>";
         }
         if (!$data) {
             $data = "Empty array";
             $em = true;
         } else {
             $data = $numeric ? "\n{$prefix}<ol>{$data}</ol>\n" : "\n{$prefix}<ul>{$data}</ul>\n";
         }
     } else {
         if (is_bool($data)) {
             $type = 'bool';
             $data = $data ? 'TRUE' : 'FALSE';
             $em = true;
         } elseif ($data === null) {
             $type = 'null';
             $data = 'NULL';
             $em = true;
         } elseif (is_int($data)) {
             $type = 'int';
             $em = true;
         } elseif (is_float($data)) {
             $type = 'float';
             $em = true;
         } elseif (is_resource($data)) {
             $type = 'resource';
             $data = 'RESOURCE';
             $em = true;
         } elseif (is_string($data)) {
             if ($type == 'unknown') {
                 $type = 'string';
             }
             if (preg_match('`^https?://[^\\s]+$`', $data)) {
                 $data = Utils::htmlentities($data);
                 $data = "<a href='{$data}'>{$data}</a>";
             } else {
                 $data = Utils::htmlentities($data);
             }
         }
     }
     if ($em) {
         $data = "<em class='dump-{$type}'>{$data}</em>";
     } else {
         $data = "<span class='dump-{$type}'>{$data}</span>";
     }
     if ($indent === null) {
         self::$_objects = array();
         $data = "<div class='dump'>{$data}</div>";
     }
     return (string) $data;
 }