Ejemplo n.º 1
0
 public function __toString()
 {
     try {
         return $this->compile(Database::instance());
     } catch (Exception $e) {
         return JsonApiApplication_Exception::text($e);
     }
 }
Ejemplo n.º 2
0
 /**
  * Sets the last_active timestamp and saves the session.
  *
  *     $session->write();
  *
  * [!!] Any errors that occur during session writing will be logged,
  * but not displayed, because sessions are written after output has
  * been sent.
  *
  * @return  boolean
  * @uses    Kohana::$log
  */
 public function write()
 {
     if (headers_sent() or $this->_destroyed) {
         // Session cannot be written when the headers are sent or when
         // the session has been destroyed
         return FALSE;
     }
     // Set the last active timestamp
     $this->_data['last_active'] = time();
     try {
         return $this->_write();
     } catch (Exception $e) {
         // Log & ignore all errors when a write fails
         JsonApiApplication::$log->add(Log::ERROR, JsonApiApplication_Exception::text($e))->write();
         return FALSE;
     }
 }
Ejemplo n.º 3
0
 /**
  * Send file download as the response. All execution will be halted when
  * this method is called! Use TRUE for the filename to send the current
  * response as the file content. The third parameter allows the following
  * options to be set:
  *
  * Type      | Option    | Description                        | Default Value
  * ----------|-----------|------------------------------------|--------------
  * `boolean` | inline    | Display inline instead of download | `FALSE`
  * `string`  | mime_type | Manual mime type                   | Automatic
  * `boolean` | delete    | Delete the file after sending      | `FALSE`
  *
  * Download a file that already exists:
  *
  *     $request->send_file('media/packages/JsonApiApplication.zip');
  *
  * Download generated content as a file:
  *
  *     $request->response($content);
  *     $request->send_file(TRUE, $filename);
  *
  * [!!] No further processing can be done after this method is called!
  *
  * @param   string  $filename   filename with path, or TRUE for the current response
  * @param   string  $download   downloaded file name
  * @param   array   $options    additional options
  * @return  void
  * @throws  JsonApiApplication_Exception
  * @uses    File::mime_by_ext
  * @uses    File::mime
  * @uses    Request::send_headers
  */
 public function send_file($filename, $download = NULL, array $options = NULL)
 {
     if (!empty($options['mime_type'])) {
         $mime = $options['mime_type'];
     }
     if ($filename === TRUE) {
         if (empty($download)) {
             throw new JsonApiApplication_Exception('Download name must be provided for streaming files');
         }
         $options['delete'] = FALSE;
         if (!isset($mime)) {
             $mime = File::mime_by_ext(strtolower(pathinfo($download, PATHINFO_EXTENSION)));
         }
         $file_data = (string) $this->_body;
         $size = strlen($file_data);
         $file = tmpfile();
         fwrite($file, $file_data);
         unset($file_data);
     } else {
         $filename = realpath($filename);
         if (empty($download)) {
             $download = pathinfo($filename, PATHINFO_BASENAME);
         }
         $size = filesize($filename);
         if (!isset($mime)) {
             $mime = File::mime_by_ext(pathinfo($download, PATHINFO_EXTENSION));
         }
         $file = fopen($filename, 'rb');
     }
     if (!is_resource($file)) {
         throw new JsonApiApplication_Exception('Could not read file to send: :file', array(':file' => $download));
     }
     $disposition = empty($options['inline']) ? 'attachment' : 'inline';
     list($start, $end) = $this->_calculate_byte_range($size);
     if (!empty($options['resumable'])) {
         if ($start > 0 or $end < $size - 1) {
             $this->_status = 206;
         }
         $this->_header['content-range'] = 'bytes ' . $start . '-' . $end . '/' . $size;
         $this->_header['accept-ranges'] = 'bytes';
     }
     $this->_header['content-disposition'] = $disposition . '; filename="' . $download . '"';
     $this->_header['content-type'] = $mime;
     $this->_header['content-length'] = (string) ($end - $start + 1);
     if (Request::user_agent('browser') === 'Internet Explorer') {
         if (Request::$initial->secure()) {
             // http://support.microsoft.com/kb/316431
             $this->_header['pragma'] = $this->_header['cache-control'] = 'public';
         }
         if (version_compare(Request::user_agent('version'), '8.0', '>=')) {
             // http://ajaxian.com/archives/ie-8-security
             $this->_header['x-content-type-options'] = 'nosniff';
         }
     }
     $this->send_headers();
     while (ob_get_level()) {
         ob_end_flush();
     }
     ignore_user_abort(TRUE);
     if (!JsonApiApplication::$safe_mode) {
         set_time_limit(0);
     }
     $block = 1024 * 16;
     fseek($file, $start);
     while (!feof($file) and ($pos = ftell($file)) <= $end) {
         if (connection_aborted()) {
             break;
         }
         if ($pos + $block > $end) {
             $block = $end - $pos + 1;
         }
         echo fread($file, $block);
         flush();
     }
     fclose($file);
     if (!empty($options['delete'])) {
         try {
             unlink($filename);
         } catch (Exception $e) {
             $error = JsonApiApplication_Exception::text($e);
             if (is_object(JsonApiApplication::$log)) {
                 JsonApiApplication::$log->add(Log::ERROR, $error);
                 JsonApiApplication::$log->write();
             }
         }
     }
     exit;
 }
Ejemplo n.º 4
0
 public static function response(Exception $e)
 {
     try {
         $class = get_class($e);
         $code = $e->getCode();
         $message = $e->getMessage();
         $file = $e->getFile();
         $line = $e->getLine();
         $trace = $e->getTrace();
         if ($e instanceof HTTP_Exception and $trace[0]["function"] == "factory") {
             extract(array_shift($trace));
         }
         if ($e instanceof ErrorException) {
             if (function_exists("xdebug_get_function_stack") and $code == E_ERROR) {
                 $trace = array_slice(array_reverse(xdebug_get_function_stack()), 4);
                 foreach ($trace as &$frame) {
                     /**
                      * XDebug pre 2.1.1 doesn"t currently set the call type key
                      * http://bugs.xdebug.org/view.php?id=695
                      */
                     if (!isset($frame["type"])) {
                         $frame["type"] = "??";
                     }
                     if ("dynamic" === $frame["type"]) {
                         $frame["type"] = "->";
                     } elseif ("static" === $frame["type"]) {
                         $frame["type"] = "::";
                     }
                     if (isset($frame["params"]) and !isset($frame["args"])) {
                         $frame["args"] = $frame["params"];
                     }
                 }
             }
             if (isset(JsonApiApplication_Exception::$php_errors[$code])) {
                 $code = JsonApiApplication_Exception::$php_errors[$code];
             }
         }
         /**
          * The stack trace becomes unmanageable inside PHPUnit.
          *
          * The error view ends up several GB in size, taking
          * serveral minutes to render.
          */
         if (defined("PHPUnit_MAIN_METHOD") or defined("PHPUNIT_COMPOSER_INSTALL") or defined("__PHPUNIT_PHAR__")) {
             $trace = array_slice($trace, 0, 2);
         }
         $view = View::factory(JsonApiApplication_Exception::$error_view, get_defined_vars());
         $response = Response::factory();
         $response->status($e instanceof HTTP_Exception ? $e->getCode() : 500);
         $response->headers("Content-Type", JsonApiApplication_Exception::$error_view_content_type . "; charset=" . JsonApiApplication::$charset);
         $response->body($view->render());
     } catch (Exception $e) {
         $response = Response::factory();
         $response->status(500);
         $response->headers("Content-Type", "text/plain");
         $response->body(JsonApiApplication_Exception::text($e));
     }
     return $response;
 }