Ejemplo n.º 1
0
 /**
  * Constructs a new exception for the specified model
  *
  * @param  string     $alias       The alias to use when looking for error messages
  * @param  Validation $object      The Validation object of the model
  * @param  string     $message     The error message
  * @param  array      $values      The array of values for the error message
  * @param  integer    $code        The error code for the exception
  * @return void
  */
 public function __construct($alias, Validation $object, $message = 'Failed to validate array', array $values = NULL, $code = 0, Exception $previous = NULL)
 {
     $this->_alias = $alias;
     $this->_objects['_object'] = $object;
     $this->_objects['_has_many'] = FALSE;
     parent::__construct($message, $values, $code, $previous);
 }
Ejemplo n.º 2
0
 public function __toString()
 {
     try {
         return $this->compile(Database::instance());
     } catch (Exception $e) {
         return JsonApiApplication_Exception::text($e);
     }
 }
Ejemplo n.º 3
0
 public function __toString()
 {
     try {
         return $this->render();
     } catch (Exception $e) {
         /**
          * Display the exception message.
          *
          * We use this method here because it"s impossible to throw an
          * exception from __toString().
          */
         $error_response = JsonApiApplication_Exception::_handler($e);
         return $error_response->body();
     }
 }
Ejemplo n.º 4
0
 public function execute_request(Request $request, Response $response)
 {
     $prefix = "Controller_";
     $directory = $request->directory();
     $controller = $request->controller();
     if ($directory) {
         $prefix .= str_replace(array("\\", "/"), "_", trim($directory, "/")) . "_";
     }
     if (JsonApiApplication::$profiling) {
         $benchmark = "'" . $request->uri() . "'";
         if ($request !== Request::$initial and Request::$current) {
             $benchmark .= " « '" . Request::$current->uri() . "'";
         }
         $benchmark = Profiler::start("Requests", $benchmark);
     }
     $previous = Request::$current;
     Request::$current = $request;
     $initial_request = $request === Request::$initial;
     try {
         if (!class_exists($prefix . $controller)) {
             throw HTTP_Exception::factory(404, "The requested URL :uri was not found on this server.", array(":uri" => $request->uri()))->request($request);
         }
         $class = new ReflectionClass($prefix . $controller);
         if ($class->isAbstract()) {
             throw new JsonApiApplication_Exception("Cannot create instances of abstract :controller", array(":controller" => $prefix . $controller));
         }
         $controller = $class->newInstance($request, $response);
         $response = $class->getMethod("execute")->invoke($controller);
         if (!$response instanceof Response) {
             throw new JsonApiApplication_Exception("Controller failed to return a Response");
         }
     } catch (HTTP_Exception $e) {
         if ($e->request() === NULL) {
             $e->request($request);
         }
         $response = $e->get_response();
     } catch (Exception $e) {
         $response = JsonApiApplication_Exception::_handler($e);
     }
     Request::$current = $previous;
     if (isset($benchmark)) {
         Profiler::stop($benchmark);
     }
     return $response;
 }
Ejemplo n.º 5
0
 public static function shutdown_handler()
 {
     if (!JsonApiApplication::$_init) {
         return;
     }
     try {
         if (JsonApiApplication::$caching === TRUE and JsonApiApplication::$_files_changed === TRUE) {
             // Write the file path cache
             JsonApiApplication::cache("JsonApiApplication::find_file()", JsonApiApplication::$_files);
         }
     } catch (Exception $e) {
         // Pass the exception to the handler
         JsonApiApplication_Exception::handler($e);
     }
     if (JsonApiApplication::$errors and $error = error_get_last() and in_array($error["type"], JsonApiApplication::$shutdown_errors)) {
         // Clean the output buffer
         ob_get_level() and ob_clean();
         // Fake an exception for nice debugging
         JsonApiApplication_Exception::handler(new ErrorException($error["message"], $error["type"], 0, $error["file"], $error["line"]));
         // Shutdown now to avoid a "death loop"
         exit(1);
     }
 }
Ejemplo n.º 6
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.º 7
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.º 8
0
 public function __construct(Validation $array, $message = 'Failed to validate array', array $values = NULL, $code = 0, Exception $previous = NULL)
 {
     $this->array = $array;
     parent::__construct($message, $values, $code, $previous);
 }
Ejemplo n.º 9
0
 public function get_response()
 {
     return JsonApiApplication_Exception::response($this);
 }
Ejemplo n.º 10
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;
 }