Ejemplo n.º 1
0
 /**
  * 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);
     }
 }
Ejemplo n.º 2
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`
  *
  * 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;
 }
Ejemplo n.º 3
0
 /**
  * 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 '';
     }
 }
Ejemplo n.º 4
0
 public function format_for_cli()
 {
     return Gleez_Exception::text($e);
 }
Ejemplo n.º 5
0
 /**
  * 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;
     }
 }
Ejemplo n.º 6
0
 /**
  * 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();
 }