/** * Constructor */ public function __construct($message, array $migration, array $variables = array(), $code = 0) { $variables[':migration-id'] = $migration['id']; $variables[':migration-group'] = $migration['mgroup']; $this->_migration = $migration; parent::__construct($message, $variables, $code); }
/** * 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 [Optional] * @param array $values The array of values for the error message [Optional] * @param integer $code The error code for the exception [Optional] */ public function __construct($alias, Validation $object, $message = 'Failed to validate array', array $values = NULL, $code = 0) { $this->_alias = $alias; $this->_objects['_object'] = $object; $this->_objects['_has_many'] = FALSE; parent::__construct($message, $values, $code); }
/** * Return the SQL query string. * * @return string * * @throws \Gleez_Exception */ public function __toString() { try { // Return the SQL string return $this->compile(Database::instance()); } catch (\Exception $e) { return \Gleez_Exception::text($e); } }
/** * Generate a Response for the current Exception * * @return Response * * @uses Gleez_Exception::response * @uses Gleez_Exception::log */ public function get_response() { // Lets log the Exception parent::log($this); return parent::response($this); }
/** * 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` * * Example: * ~~~ * // Download a file that already exists * $request->send_file('media/packages/gleez.zip'); * * // Download generated content as a file: * $request->response($content); * $request->send_file(TRUE, $filename); * ~~~ * * [!!] Note: 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 [Optional] * @param array $options Additional options [Optional] * * @return void * * @throws Gleez_Exception * * @uses File::mime_by_ext * @uses File::mime * @uses Request::send_headers * @uses Gleez_Exception::text */ 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 Gleez_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->_body; // 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_by_ext(strtolower(pathinfo($download, PATHINFO_EXTENSION))); } // Open the file for reading $file = fopen($filename, 'rb'); } if (!is_resource($file)) { throw new Gleez_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->_header['content-range'] = 'bytes ' . $start . '-' . $end . '/' . $size; $this->_header['accept-ranges'] = 'bytes'; } // Set the headers for a download $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') { // Naturally, IE does not act like a real browser... 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'; } } // 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 = Gleez_Exception::text($e); if (is_object(Kohana::$log)) { // Add this exception to the log // And make sure the logs are written Log::error($error)->write(); } // Do NOT display the exception, it will corrupt the output! } } // Stop execution exit; }
/** * 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 = Gleez_Exception::text($e); // Add this exception to the log Log::error($error); } // Showing any kind of error will be "inside" image data return ''; } }
public function format_for_cli() { return Gleez_Exception::text($e); }
/** * Renders the pagination links * * @return string Pagination output (HTML) * * @uses Gleez_Exception::handler */ public function __toString() { try { return $this->render(); } catch (Exception $e) { Gleez_Exception::handler($e); return ''; } }
/** * Runs the Gleez environment * * @uses Gleez::_set_cookie * @uses Route::set * @uses Route::defaults * @uses Config::load * @uses I18n::initialize * @uses Module::load_modules */ public static function ready() { if (self::$_init) { // Do not allow execution twice return; } // Gleez is now initialized? self::$_init = TRUE; // Set default cookie salt and lifetime self::_set_cookie(); // Check database config file exist or not Gleez::$installed = file_exists(APPPATH . 'config/database.php'); if (Gleez::$installed) { // Database config reader and writer Kohana::$config->attach(new Config_Database()); } if (Kohana::$environment !== Kohana::DEVELOPMENT) { Gleez_Exception::$error_view = 'errors/stack'; } // Turn off notices and strict errors in production if (Kohana::$environment === Kohana::PRODUCTION) { // Turn off notices and strict errors error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT); } // Initialize the locale from settings I18n::initialize(); // Disable the kohana powered headers // @todo Remove it, use Gleez::$expose Kohana::$expose = FALSE; /** * If database.php doesn't exist, then we assume that the Gleez is not * properly installed and send to the installer. */ if (!Gleez::$installed) { Session::$default = 'cookie'; Gleez_Exception::$error_view = 'errors/error'; // Static file serving (CSS, JS, images) Route::set('install/media', 'media(/<file>)', array('file' => '.+'))->defaults(array('controller' => 'install', 'action' => 'media', 'file' => NULL, 'directory' => 'install')); Route::set('install', '(install(/<action>))', array('action' => 'index|systemcheck|database|install|finalize'))->defaults(array('controller' => 'install', 'directory' => 'install')); return; } // Set the default session type Session::$default = Config::get('site.session_type'); // Initialize Gleez modules Module::load_modules(FALSE); // Load the active theme(s) Theme::load_themes(); }
/** * Sets the last_active timestamp and saves the session. * * Example: * ~~~ * $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 Log::add */ 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 Log::error(Gleez_Exception::text($e))->write(); return FALSE; } }
/** * Do save * * @uses Arr::get * @uses Module::available * @uses Module::is_active * @uses Module::deactivate * @uses Module::is_installed * @uses Module::upgrade * @uses Module::install * @uses Module::activate * @uses Module::event * @uses Cache::delete_all * @uses Log::add * @uses Gleez_Exception::text */ private function _do_save() { $changes = new stdClass(); $changes->activate = array(); $changes->deactivate = array(); $activated_names = array(); $deactivated_names = array(); foreach (Module::available() as $module_name => $info) { if ($info->locked) { continue; } try { $desired = Arr::get($_POST, $module_name) == 1; if ($info->active and !$desired and Module::is_active($module_name)) { Module::deactivate($module_name); $changes->deactivate[] = $module_name; $deactivated_names[] = __($info->name); } elseif (!$info->active and $desired and !Module::is_active($module_name)) { if (Module::is_installed($module_name)) { Module::upgrade($module_name); } else { Module::install($module_name); } Module::activate($module_name); $changes->activate[] = $module_name; $activated_names[] = __($info->name); } } catch (Exception $e) { Log::error(Gleez_Exception::text($e)); } } Module::event('module_change', $changes); // @todo This type of collation is questionable from an i18n perspective if ($activated_names) { Message::success(__('Activated: %names', array('%names' => join(", ", $activated_names)))); } if ($deactivated_names) { Message::success(__('Deactivated: %names', array('%names' => join(", ", $deactivated_names)))); } // Clear any cache for sure Cache::instance()->delete_all(); }
/** * Validation constructor * * @param Validation $array Validation object * @param string $message Error message [Optional] * @param array $values Translation variables [Optional] * @param integer $code The exception code [Optional] * @param Exception $previous Previous exception [Optional] */ 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); }
/** * Magic method, returns the output of [View::render] * * @return string * * @uses View::render * @uses Gleez_Exception::_handler * @uses Response::body */ 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 = Gleez_Exception::_handler($e); return $error_response->body(); } }