public function write(array $messages) { foreach ($messages as $key => $message) { if (is_object($message['body']) && $message['body'] instanceof Exception) { $messages[$key]['body'] = Kohana::exception_text($message['body']); } } parent::write($messages); }
/** * Return the SQL query string. * * @return string */ public final function __toString() { try { // Return the SQL string return $this->compile(Database::instance()); } catch (Exception $e) { return Kohana::exception_text($e); } }
/** * Get from GeoNames by geonameId * * @static * @param integer $id * @return array */ private static function _get($id, $lang = 'en') { $url = Kohana::config('geo.base_url') . '/get?geonameId=' . (int) $id . '&lang=' . $lang . '&style=full'; try { $xml = new SimpleXMLElement($url, null, true); Kohana::$log->add(Kohana::DEBUG, 'GeoNames OK: ' . $url); return $xml; } catch (Exception $e) { Kohana::$log->add(Kohana::ERROR, 'GeoNames failed: ' . $url . ' - ' . Kohana::exception_text($e)); return false; } }
/** * Magic object-to-string method. * * echo $exception; * * @uses Kohana::exception_text * @return string */ public function __toString() { return Kohana::exception_text($this); }
/** * Inline exception handler, displays the error message, source of the * exception, and the stack trace of the error. * * @uses Kohana::$php_errors * @uses Kohana::exception_text() * @param object exception object * @return boolean */ public static function exception_handler(Exception $e) { try { // Get the exception information $type = get_class($e); $code = $e->getCode(); $message = $e->getMessage(); $file = $e->getFile(); $line = $e->getLine(); // Create a text version of the exception $error = self::exception_text($e); if (is_object(self::$log)) { // Add this exception to the log self::$log->add(Kohana::ERROR, $error); } if (Kohana::$is_cli) { // Just display the text of the exception echo "\n{$error}\n"; return TRUE; } // Get the exception backtrace $trace = $e->getTrace(); if ($e instanceof ErrorException) { if (isset(self::$php_errors[$code])) { // Use the human-readable error name $code = self::$php_errors[$code]; } if (version_compare(PHP_VERSION, '5.3', '<')) { // Workaround for a bug in ErrorException::getTrace() that exists in // all PHP 5.2 versions. @see http://bugs.php.net/bug.php?id=45895 for ($i = count($trace) - 1; $i > 0; --$i) { if (isset($trace[$i - 1]['args'])) { // Re-position the args $trace[$i]['args'] = $trace[$i - 1]['args']; // Remove the args unset($trace[$i - 1]['args']); } } } } if (!headers_sent()) { // Make sure the proper content type is sent with a 500 status header('Content-Type: text/html; charset=' . Kohana::$charset, TRUE, 500); } // Start an output buffer ob_start(); // Include the exception HTML include self::find_file('views', 'kohana/error'); // Display the contents of the output buffer echo ob_get_clean(); return TRUE; } catch (Exception $e) { // Clean the output buffer if one exists ob_get_level() and ob_clean(); // Display the exception text echo Kohana::exception_text($e), "\n"; // Exit with an error status exit(1); } }
/** * Render the current image. * * echo $image; * * [!!] The output of this function is binary and must be rendered with the * appropriate Content-Type header or it will not be displayed correctly! * * @return string */ public function __toString() { try { // Render the current image return $this->render(); } catch (Exception $e) { if (is_object(Kohana::$log)) { // Get the text of the exception $error = Kohana::exception_text($e); // Add this exception to the log Kohana::$log->add(Kohana::ERROR, $error); } // Showing any kind of error will be "inside" image data return ''; } }
* Enable modules. Modules are referenced by a relative or absolute path. */ Kohana::modules(array('forum' => MODPATH . 'forum', 'message' => MODPATH . 'message', 'facebook' => MODPATH . 'facebook', 'modulargaming' => MODPATH . 'modulargaming', 'event' => MODPATH . 'event', 'jelly' => MODPATH . 'jelly', 'sprig' => MODPATH . 'sprig', 'database' => MODPATH . 'database', 'a1' => MODPATH . 'A1', 'a2' => MODPATH . 'A2', 'acl' => MODPATH . 'ACL', 'pagination' => MODPATH . 'pagination', 'captcha' => MODPATH . 'captcha', 'image' => MODPATH . 'image')); /** * Set the routes. Each route must have a minimum of a name, a URI and a set of * defaults for the URI. */ Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')->defaults(array('directory' => 'admin', 'controller' => 'welcome', 'action' => 'index')); Route::set('shop', 'shop(/<shop>(/<action>(/<item>)))')->defaults(array('controller' => 'shop', 'action' => 'index')); Route::set('npc', 'npc(/<npc>(/<action>(/<method>)))')->defaults(array('controller' => 'npc', 'action' => 'index')); Route::set('default', '(<controller>(/<action>(/<id>)))')->defaults(array('controller' => 'welcome', 'action' => 'index')); /** * Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO']. * If no source is specified, the URI will be automatically detected. */ $request = Request::instance(); try { // Attempt to execute the response $request->execute(); } catch (Exception $e) { if (!IN_PRODUCTION) { throw $e; } // Log the error Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e)); $request = Request::factory('errors/404')->execute(); } /** * Display the request response. */ echo $request->send_headers()->response;
/** * Tests Kohana::exception_text() * * @test * @dataProvider provider_exception_text * @covers Kohana::exception_text * @param object $exception exception to test * @param string $expected expected output */ public function test_exception_text($exception, $expected) { $this->assertEquals($expected, Kohana::exception_text($exception)); }
/** * 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 Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e))->write(); return FALSE; } }
<h1>Message Dump</h1> <?php foreach ($messages as $path => $name) { echo "<h3>{$path}</h3>"; try { echo Debug::dump(Kohana::message($name)); } catch (exception $e) { echo "Something went terribly wrong. Error message: " . Kohana::exception_text($e); } }
/** * 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/kohana.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 with path, or TRUE for the current response * @param string downloaded file name * @param array additional options * @return void * @throws Kohana_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'])) { // The mime-type has been manually set $mime = $options['mime_type']; } if ($filename === TRUE) { if (empty($download)) { throw new Kohana_Exception('Download name must be provided for streaming files'); } // Temporary files will automatically be deleted $options['delete'] = FALSE; if (!isset($mime)) { // Guess the mime using the file extension $mime = File::mime_by_ext(strtolower(pathinfo($download, PATHINFO_EXTENSION))); } // Get the content size $size = strlen($this->response); // Create a temporary file to hold the current response $file = tmpfile(); // Write the current response into the file fwrite($file, $this->response); // Prepare the file for reading fseek($file, 0); } else { // Get the complete file path $filename = realpath($filename); if (empty($download)) { // Use the file name as the download file name $download = pathinfo($filename, PATHINFO_BASENAME); } // Get the file size $size = filesize($filename); if (!isset($mime)) { // Get the mime type $mime = File::mime($filename); } // Open the file for reading $file = fopen($filename, 'rb'); } // Inline or download? $disposition = empty($options['inline']) ? 'attachment' : 'inline'; // Set the headers for a download $this->headers['Content-Disposition'] = $disposition . '; filename="' . $download . '"'; $this->headers['Content-Type'] = $mime; $this->headers['Content-Length'] = $size; if (!empty($options['resumable'])) { // @todo: ranged download processing } // Send all headers now $this->send_headers(); while (ob_get_level()) { // Flush all output buffers ob_end_flush(); } // Manually stop execution ignore_user_abort(TRUE); // Keep the script running forever set_time_limit(0); // Send data in 16kb blocks $block = 1024 * 16; while (!feof($file)) { if (connection_aborted()) { break; } // Output a block of the file echo fread($file, $block); // Send the data now flush(); } // Close the file fclose($file); if (!empty($options['delete'])) { try { // Attempt to remove the file unlink($filename); } catch (Exception $e) { // Create a text version of the exception $error = Kohana::exception_text($e); if (is_object(Kohana::$log)) { // Add this exception to the log Kohana::$log->add(Kohana::ERROR, $error); // Make sure the logs are written Kohana::$log->write(); } // Do NOT display the exception, it will corrupt the output! } } // Stop execution exit; }
/** * 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/kohana.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 with path, or TRUE for the current response * @param string downloaded file name * @param array additional options * @return void * @throws Kohana_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'])) { // The mime-type has been manually set $mime = $options['mime_type']; } if ($filename === TRUE) { if (empty($download)) { throw new Kohana_Exception('Download name must be provided for streaming files'); } // Temporary files will automatically be deleted $options['delete'] = FALSE; if ( ! isset($mime)) { // Guess the mime using the file extension $mime = File::mime_by_ext(strtolower(pathinfo($download, PATHINFO_EXTENSION))); } // Force the data to be rendered if $file_data = (string) $this->response; // Get the content size $size = strlen($file_data); // Create a temporary file to hold the current response $file = tmpfile(); // Write the current response into the file fwrite($file, $file_data); // File data is no longer needed unset($file_data); } else { // Get the complete file path $filename = realpath($filename); if (empty($download)) { // Use the file name as the download file name $download = pathinfo($filename, PATHINFO_BASENAME); } // Get the file size $size = filesize($filename); if ( ! isset($mime)) { // Get the mime type $mime = File::mime($filename); } // Open the file for reading $file = fopen($filename, 'rb'); } if ( ! is_resource($file)) { throw new Kohana_Exception('Could not read file to send: :file', array( ':file' => $download, )); } // Inline or download? $disposition = empty($options['inline']) ? 'attachment' : 'inline'; // Calculate byte range to download. list($start, $end) = $this->_calculate_byte_range($size); if ( ! empty($options['resumable'])) { if ($start > 0 OR $end < ($size - 1)) { // Partial Content $this->status = 206; } // Range of bytes being sent $this->headers['Content-Range'] = 'bytes '.$start.'-'.$end.'/'.$size; $this->headers['Accept-Ranges'] = 'bytes'; } // Set the headers for a download $this->headers['Content-Disposition'] = $disposition.'; filename="'.$download.'"'; $this->headers['Content-Type'] = $mime; $this->headers['Content-Length'] = ($end - $start) + 1; if (Request::user_agent('browser') === 'Internet Explorer') { // Naturally, IE does not act like a real browser... if (Request::$protocol === 'https') { // http://support.microsoft.com/kb/316431 $this->headers['Pragma'] = $this->headers['Cache-Control'] = 'public'; } if (version_compare(Request::user_agent('version'), '8.0', '>=')) { // http://ajaxian.com/archives/ie-8-security $this->headers['X-Content-Type-Options'] = 'nosniff'; } } // Send all headers now $this->send_headers(); while (ob_get_level()) { // Flush all output buffers ob_end_flush(); } // Manually stop execution ignore_user_abort(TRUE); if ( ! Kohana::$safe_mode) { // Keep the script running forever set_time_limit(0); } // Send data in 16kb blocks $block = 1024 * 16; fseek($file, $start); while ( ! feof($file) AND ($pos = ftell($file)) <= $end) { if (connection_aborted()) break; if ($pos + $block > $end) { // Don't read past the buffer. $block = $end - $pos + 1; } // Output a block of the file echo fread($file, $block); // Send the data now flush(); } // Close the file fclose($file); if ( ! empty($options['delete'])) { try { // Attempt to remove the file unlink($filename); } catch (Exception $e) { // Create a text version of the exception $error = Kohana::exception_text($e); if (is_object(Kohana::$log)) { // Add this exception to the log Kohana::$log->add(Kohana::ERROR, $error); // Make sure the logs are written Kohana::$log->write(); } // Do NOT display the exception, it will corrupt the output! } } // Stop execution exit; }
/** * @return string Either an XML document or a gzipped file */ public function render() { // Default uncompressed $response = $this->_xml->saveXML(); if ($this->gzip) { // Try and gzip the file before we send it off. try { $response = gzencode($response, $this->compression); } catch (ErrorException $e) { Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e)); } } return $response; }
public function __toString() { try { return $this->render(); } catch (Exception $e) { return Kohana::exception_text($e); } }
<h1>Config Dump</h1> <?php foreach ($configs as $path => $name) { echo "<h3>{$path}</h3>"; try { echo Debug::vars(Kohana::$config->load($name)); } catch (exception $e) { echo "Something went terribly wrong. This is usually caused by\n\t\t undefined constants because of missing dependancies. Error\n\t\t\t message: " . Kohana::exception_text($e); } }