Exemple #1
0
 public function setContentTypeForFormat($format)
 {
     if (!empty($format)) {
         $mime_type = Ak::mime_content_type('file.' . $format);
         if (!empty($mime_type)) {
             $this->addHeader('Content-Type', $mime_type);
         }
     }
 }
Exemple #2
0
 /**
  * Sends the file by streaming it 4096 bytes at a time. This way the
  * whole file doesn't need to be read into memory at once.  This makes
  * it feasible to send even large files.
  * 
  * Be careful to sanitize the path parameter if it coming from a web
  * page.  sendFile($params['path']) allows a malicious user to
  * download any file on your server.
  * 
  * Options:
  * * <tt>filename</tt> - suggests a filename for the browser to use.
  *   Defaults to realpath($path).
  * * <tt>type</tt> - specifies an HTTP content type.
  *   Defaults to 'application/octet-stream'.
  * * <tt>disposition</tt> - specifies whether the file will be shown inline or downloaded.  
  *   Valid values are 'inline' and 'attachment' (default).
  * * <tt>stream</tt> - whether to send the file to the user agent as it is read (true)
  *   or to read the entire file before sending (false). Defaults to true.
  * * <tt>buffer_size</tt> - specifies size (in bytes) of the buffer used to stream the file.
  *   Defaults to 4096.
  * 
  * The default Content-Type and Content-Disposition headers are
  * set to download arbitrary binary files in as many browsers as
  * possible.  IE versions 4, 5, 5.5, and 6 are all known to have
  * a variety of quirks (especially when downloading over SSL).
  * 
  * Simple download:
  *   sendFile('/path/to.zip');
  * 
  * Show a JPEG in browser:
  *   sendFile('/path/to.jpeg', array('type' => 'image/jpeg', 'disposition' => 'inline'));
  * 
  * Read about the other Content-* HTTP headers if you'd like to
  * provide the user with more information (such as Content-Description).
  * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11
  * 
  * Also be aware that the document may be cached by proxies and browsers.
  * The Pragma and Cache-Control headers declare how the file may be cached
  * by intermediaries.  They default to require clients to validate with
  * the server before releasing cached responses.  See
  * http://www.mnot.net/cache_docs/ for an overview of web caching and
  * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
  * for the Cache-Control header spec.
  */
 function sendFile($path, $options = array())
 {
     $path = realpath($path);
     if (!file_exists($path)) {
         trigger_error(Ak::t('Cannot read file %path', array('%path' => $path)), E_USER_NOTICE);
         return false;
     }
     $options['length'] = empty($options['length']) ? filesize($path) : $options['length'];
     $options['filename'] = empty($options['filename']) ? basename($path) : $options['filename'];
     $options['type'] = empty($options['type']) ? Ak::mime_content_type($path) : $options['type'];
     $this->performed_render = false;
     $this->_sendFileHeaders($options);
     if (!empty($options['stream'])) {
         require_once AK_LIB_DIR . DS . 'AkStream.php';
         $this->render(array('text' => new AkStream($path, $options['buffer_size'])));
     } else {
         $this->render(array('text' => Ak::file_get_contents($path)));
     }
 }
Exemple #3
0
 public function test_mime_type_detection()
 {
     // png is not in any RFC so we might want to check if it has a /x- preffix for non standard values
     $this->assertTrue(in_array(Ak::mime_content_type(AK_PUBLIC_DIR . DS . 'images' . DS . 'akelos_framework_logo.png'), array('image/png', 'image/x-png')));
     $this->assertEqual(Ak::mime_content_type('C:\\Folder\\image.png'), 'image/png');
 }
Exemple #4
0
 /**
 * Sends the file by streaming it 4096 bytes at a time. This way the
 * whole file doesn't need to be read into memory at once.  This makes
 * it feasible to send even large files.
 *
 * Be careful to sanitize the path parameter if it coming from a web
 * page.  sendFile($params['path']) allows a malicious user to
 * download any file on your server.
 *
 * Options:
 * * <tt>filename</tt> - suggests a filename for the browser to use.
 *   Defaults to realpath($path).
 * * <tt>type</tt> - specifies an HTTP content type.
 *   Defaults to 'application/octet-stream'.
 * * <tt>disposition</tt> - specifies whether the file will be shown inline or downloaded.
 *   Valid values are 'inline' and 'attachment' (default).
 * * <tt>stream</tt> - whether to send the file to the user agent as it is read (true)
 *   or to read the entire file before sending (false). Defaults to true.
 * * <tt>buffer_size</tt> - specifies size (in bytes) of the buffer used to stream the file.
 *   Defaults to 4096.
 * <tt>:x_sendfile</tt> - uses X-Sendfile to send the file when set to +true+. This is currently
     only available with Lighttpd/Apache2 and specific modules installed and activated. Since this
 *   uses the web server to send the file, this may lower memory consumption on your server and
 *   it will not block your application for further requests.
 *   See http://blog.lighttpd.net/articles/2006/07/02/x-sendfile and
 *   http://tn123.ath.cx/mod_xsendfile/ for details. Defaults to +false+.
 *
 * The default Content-Type and Content-Disposition headers are
 * set to download arbitrary binary files in as many browsers as
 * possible.  IE versions 4, 5, 5.5, and 6 are all known to have
 * a variety of quirks (especially when downloading over SSL).
 *
 * Simple download:
 *   sendFile('/path/to.zip');
 *
 * Show a JPEG in browser:
 *   sendFile('/path/to.jpeg', array('type' => 'image/jpeg', 'disposition' => 'inline'));
 *
 * Read about the other Content-* HTTP headers if you'd like to
 * provide the user with more information (such as Content-Description).
 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11
 *
 * Also be aware that the document may be cached by proxies and browsers.
 * The Pragma and Cache-Control headers declare how the file may be cached
 * by intermediaries.  They default to require clients to validate with
 * the server before releasing cached responses.  See
 * http://www.mnot.net/cache_docs/ for an overview of web caching and
 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
 * for the Cache-Control header spec.
 */
 public function sendFile($path, $options = array())
 {
     $path = realpath($path);
     if (!file_exists($path)) {
         $Exception = new ControllerException(Ak::t('Cannot read file %path', array('%path' => $path)));
         $Exception->status = 500;
         throw $Exception;
     }
     $this->performed_render = false;
     if (!empty($options['x_sendfile'])) {
         $this->_log("Sending X-Sendfile header {$path}");
         $this->Response->addHeader(array('X-Sendfile' => $path));
         $this->renderNothing(empty($options['status']) ? 200 : $options['status']);
         return;
     }
     $options['length'] = empty($options['length']) ? filesize($path) : $options['length'];
     $options['filename'] = empty($options['filename']) ? basename($path) : $options['filename'];
     $options['type'] = empty($options['type']) ? Ak::mime_content_type($path) : $options['type'];
     $this->_sendFileHeaders($options);
     if (!empty($options['stream'])) {
         $this->render(array('text' => new AkStream($path, $options['buffer_size'])));
     } else {
         $this->render(array('text' => AkFileSystem::file_get_contents($path)));
     }
 }