/** * Serve the file to the browser AND cache it for direct access if in STAGING OR PRODUCTION. */ public function action_index() { $file = $this->request->param('file'); $ext = pathinfo($file, PATHINFO_EXTENSION); $path = Kohana::find_file('assets', $file, FALSE); if ($path === FALSE) { throw HTTP_Exception::factory('404', 'File not found!'); } $dir = DOCROOT . 'assets' . DIRECTORY_SEPARATOR; // Set the proper headers for browser caching $this->response->headers('content-type', File::mime_by_ext($ext)); $this->response->headers('last-modified', date('r', filemtime($path))); $content = file_get_contents($path); $this->response->body($content); // Don't cache the assets unless we are in STAGING OR PRODUCTION. if (Kohana::$environment >= Kohana::STAGING) { return; } // Only cache for specific extensions. if (!in_array($ext, $this->_cache_extensions)) { return; } // Check if assets sub dir exist. $parts = explode('/', $file); $file = array_pop($parts); foreach ($parts as $part) { $dir .= $part . DIRECTORY_SEPARATOR; if (!is_dir($dir)) { mkdir($dir); } } file_put_contents($dir . $file, $content); }
public function action_index() { $group = $this->request->param('group'); if (!$group) { throw new HTTP_Exception_404(); } $config = Kohana::$config->load('thumb')->get($group); if (!$config) { throw new HTTP_Exception_404(); } $file = $this->request->param('file'); if (!$file) { throw new HTTP_Exception_404(); } $path = Arr::get($config, 'path', ''); if (!empty($path)) { $path = rtrim($path, '/') . '/'; } $thumb = Thumb::create($group, $path . $file); if ($thumb) { // Get the extension from the filename $ext = strtolower(pathinfo($thumb, PATHINFO_EXTENSION)); $mime = File::mime_by_ext($ext); $this->response->headers('Content-Type', $mime ? $mime : 'image/jpeg'); $this->response->body(file_get_contents($thumb)); } else { throw new HTTP_Exception_500(); } }
/** * Attempt to get the mime type from a file. This method is horribly * unreliable, due to PHP being horribly unreliable when it comes to * determining the mime type of a file. * * $mime = File::mime($file); * * @param string file name or path * @return string mime type on success * @return FALSE on failure */ public static function mime($filename) { // Get the complete path to the file $filename = realpath($filename); // Get the extension from the filename $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension)) { // Use getimagesize() to find the mime type on images $file = getimagesize($filename); if (isset($file['mime'])) { return $file['mime']; } } if (class_exists('finfo', FALSE)) { if ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME)) { return $info->file($filename); } } if (ini_get('mime_magic.magicfile') and function_exists('mime_content_type')) { // The mime_content_type function is only useful with a magic file return mime_content_type($filename); } if (!empty($extension)) { return File::mime_by_ext($extension); } // Unable to find the mime-type return FALSE; }
/** * When Kohana::$environment is set to Kohana::DEVELOPMENT * the asset will be served from this Controller. * Otherwise the file will be served here once and then * copied to DOCROOT and subsequently served from there. * * @return void */ public function action_copy() { $asset = $this->request->param('asset'); $last_modified = date(DATE_RFC1123, filemtime($asset)); if ($last_modified === $this->request->headers('if-modified-since')) { // Speed up the request by sending not modified $this->response->status(304); return; } if (Publicize::should_copy_to_docroot()) { $uri = $this->request->param('uri'); Publicize::copy_to_docroot($asset, $uri); } $extension = pathinfo($asset, PATHINFO_EXTENSION); // Bug in 3.2 prevents 'Content-Type' to work as expected -> 'content-type' // see: https://github.com/kohana/core/commit/4605ccb6957a7b3a9854792328c937d1db003502 $this->response->headers(array( 'content-type' => File::mime_by_ext($extension), 'Content-Length' => (string) filesize($asset), 'Last-Modified' => $last_modified, )); $this->response->body(file_get_contents($asset)); }
public function action_media() { if ($media = $this->request->param('media')) { if (preg_match('/\\.swf$/iD', $media)) { $file = preg_replace('/^(.+?)\\.swf$/iD', '$1', $media); $this->request->headers['Content-Type'] = File::mime_by_ext('swf'); if ($file = Kohana::find_file('media', $file, 'swf')) { $this->request->response = file_get_contents($file); } return; } $this->request->headers['Content-Type'] = File::mime_by_ext('js'); $file = preg_replace('/^(.+?)\\.js$/iD', '$1', $media); if ($file = Kohana::find_file('media', $file, 'js')) { $this->request->response = file_get_contents($file); } /* foreach(explode('+', $media) as $file) { $file = preg_replace('/^(.+?)\.js$/iD', '$1', $file); if($file = Kohana::find_file('media', $file, 'js')) { $this->request->response .= file_get_contents($file).PHP_EOL; } } */ } }
public function before() { $directory = $this->request->directory(); $controller = $this->request->controller(); $action = $this->request->action(); $format = $this->_detect_response_format(); $mime = File::mime_by_ext($format) ?: 'text/html'; $this->response->view(['basedir' => $controller, 'directory' => $directory, 'action' => $action, 'format' => $format]); $this->response->view->set(['basedir' => $controller, 'directory' => $directory, 'controller' => $controller, 'action' => $action, 'format' => $format, 'request' => $this->request, 'method' => $this->request->method(), 'secure' => $this->request->secure(), 'route' => $this->request->route(), 'route_name' => Route::name($this->request->route()), 'params' => $this->request->param(), 'query' => $this->request->query()]); $this->format = $format; if ($this->_layout === NULL) { $layout = strtolower($controller); if ($this->response->view->viewfile_exists($layout, 'layouts')) { $this->_layout = $layout; } else { if ($this->response->view->viewfile_exists($this->_default_layout, 'layouts')) { $this->_layout = $this->_default_layout; } } } $this->response->headers('Content-Type', $mime . '; charset=' . Kohana::$charset); if ($this->_layout) { View::layout($this->_layout); } $this->_before_action && $this->_call_method_for_action($this->_before_action); }
public function get_mime_type() { $extension = strtolower(pathinfo($this->_filename, PATHINFO_EXTENSION)); $mime = File::mime_by_ext($extension); if (!$mime) { $mime = 'application/octet-stream'; } return $mime; }
public function action_media($file) { $ext = pathinfo($file, PATHINFO_EXTENSION); $file = substr($file, 0, -(strlen($ext) + 1)); if ($file = Kohana::find_file('media', $file, $ext)) { $this->request->response = file_get_contents($file); } else { $this->request->status = 404; } $this->request->headers['Content-Type'] = File::mime_by_ext($ext); }
/** * This action issues access and refresh tokens and is called only * by the 3rd party. All output should be JSON. * * You DO NOT need to extend/replace this action. */ public function action_token() { $this->response->headers('Content-Type', File::mime_by_ext('json')); try { // Attempt to issue a token $this->response->body($this->_oauth->token()); } catch (OAuth2_Exception $e) { // Something went wrong, lets give a formatted error $this->response->status(400); $this->response->body($e->getJsonError()); } }
public function action_img($filename, $ext) { if (self::check(300) === FALSE) { self::set(300); } $info = pathinfo($filename); $file = Kohana::find_file('views/images', basename($info['basename']), $ext); if (!$file) { throw new Kohana_Exception("No such file or directory (:filename)", array('filename' => "{$filename}.{$ext}")); } $this->request->send_file($file, FALSE, array('inline' => 1, 'mime_type' => File::mime_by_ext($ext))); }
/** * Generate a Response for the 404 Exception. * * @return Response */ public function get_response() { $ext = pathinfo(Request::current()->url(), PATHINFO_EXTENSION); $mimetype = FALSE; if ($ext and !($mimetype = File::mime_by_ext($ext))) { $mimetype = 'application/octet-stream'; } if ($mimetype) { return Response::factory()->headers('content-type', $mimetype)->status(404); } else { return parent::get_response(); } }
public function action_media() { $file = $this->request->param('file'); $ext = pathinfo($file, PATHINFO_EXTENSION); $file = substr($file, 0, -(strlen($ext) + 1)); if ($file = Kohana::find_file('media', $file, $ext)) { $this->response->check_cache(sha1($this->request->uri()) . filemtime($file), $this->request); $this->response->body(file_get_contents($file)); $this->response->headers('content-type', File::mime_by_ext($ext)); $this->response->headers('last-modified', date('r', filemtime($file))); } else { throw new HTTP_Exception_404('File not found.'); } }
public function action_file() { $request = Request::instance(); $file = $request->param('file'); $ext = pathinfo($file, PATHINFO_EXTENSION); $file = substr($file, 0, -(strlen($ext) + 1)); if ($file = Kohana::find_file('media', $file, $ext)) { $request->response = file_get_contents($file); } else { Kohana::$log->add(Kohana::ERROR, 'Admin media controller error while loading file, ' . $file); $request->status = 404; } $request->headers['Content-Type'] = File::mime_by_ext($ext); }
public function get_response() { $ext = pathinfo(Request::current()->url(), PATHINFO_EXTENSION); $mimetype = FALSE; if ($ext and !($mimetype = File::mime_by_ext($ext))) { $mimetype = 'application/octet-stream'; } if ($mimetype and $mimetype !== 'text/html') { return Response::factory()->headers('content-type', $mimetype)->status(404); } if (($page = Model_Page_Front::findByField('behavior_id', 'page_not_found')) !== FALSE) { return Request::factory($page->url)->query('message', $this->message)->execute()->status(404); } throw new HTTP_Exception_404('Something went wrong'); }
public function action_img($filename = null) { if (!$filename) { throw new Kohana_Exception("No such file or directory"); } if (Kohana_Core::$environment != Kohana::DEVELOPMENT && self::check(300) === FALSE) { self::set(300); } $info = pathinfo($filename); $ext = $info['extension']; $filename = $info['filename']; $file = Kohana::find_file('views/images', basename($info['basename'], ".{$ext}"), $ext); if (!$file) { throw new Kohana_Exception("No such file or directory (:filename)", array('filename' => "{$filename}.{$ext}")); } $this->response->send_file($file, FALSE, array('inline' => 1, 'mime_type' => File::mime_by_ext($ext))); }
/** * Process the verification. * * @return void */ public function action_index() { // Verify the credentials $service = $this->request->param('service'); $success = FALSE; if (!empty($service)) { $success = MMI_API_Verify_Custom::factory($service)->verify(); } // Send the response $this->request->headers['Content-Type'] = File::mime_by_ext('txt'); if ($success) { $this->request->response = 'Success'; } else { $this->request->response = 'Unauthorized'; $this->request->status = 401; } }
public function action_file() { // Get the file path from the request $file = $this->request->param('file'); // Find the file extension $ext = pathinfo($file, PATHINFO_EXTENSION); // Remove the extension from the filename $file = substr($file, 0, -(strlen($ext) + 1)); if ($file = Kohana::find_file('files', $file, $ext)) { // Send the file content as the response $this->request->response = file_get_contents($file); } else { // Return a 404 status $this->request->status = 404; } // Set the content type for this extension $this->request->headers['Content-Type'] = File::mime_by_ext($ext); }
public function action_media() { // Get the file path from the request $file = $this->request->param('file'); $ext = $this->request->param('ext'); // Remove the extension from the filename if ($file = Kohana::find_file('media', $file, $ext)) { // Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed $this->check_cache(sha1($this->request->uri()) . filemtime($file)); // Send the file content as the response $this->response->body(file_get_contents($file)); // Set the proper headers to allow caching $this->response->headers('content-type', File::mime_by_ext($ext)); $this->response->headers('last-modified', date('r', filemtime($file))); } else { throw new HTTP_Exception_404(); } }
function action_index() { $file = $this->request->param('file'); $ext = pathinfo($file, PATHINFO_EXTENSION); $file = substr($file, 0, -(strlen($ext) + 1)); if ($file = Kohana::find_file('vendor/ckeditor', $file, $ext)) { // Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed $this->check_cache(sha1($this->request->uri()) . filemtime($file)); // Send the file content as the response $this->response->body(file_get_contents($file)); // Set the proper headers to allow caching $this->response->headers('content-type', File::mime_by_ext($ext)); $this->response->headers('last-modified', date('r', filemtime($file))); } else { // Return a 404 status $this->response->status(404); } }
public function action_media() { $file = $this->request->param('file'); $ext = pathinfo($file, PATHINFO_EXTENSION); $file = substr($file, 0, -(strlen($ext) + 1)); if ($ext == 'php') { include 'modules/cms/media/' . $file . '.' . $ext; } else { if ($file = Kohana::find_file('media', $file, $ext)) { //$this->response->check_cache(sha1($this->request->uri()).filemtime($file), $this->request); $this->response->body(file_get_contents($file)); $this->response->headers('content-type', File::mime_by_ext($ext)); $this->response->headers('last-modified', date('r', filemtime($file))); } else { $this->response->status(404); } } }
/** * Static file serving (CSS, JS, images, etc.) * * @uses Request::param * @uses Request::uri * @uses Kohana::find_file * @uses Response::check_cache * @uses Response::body * @uses Response::headers * @uses Response::status * @uses File::mime_by_ext * @uses File::getExt * @uses Config::get * @uses Log::add * @uses System::mkdir */ public function action_serve() { // Get file theme from the request $theme = $this->request->param('theme', FALSE); // Get the file path from the request $file = $this->request->param('file'); // Find the file extension $ext = File::getExt($file); // Remove the extension from the filename $file = substr($file, 0, -(strlen($ext) + 1)); if ($file_name = Kohana::find_file('media', $file, $ext)) { // Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed $this->response->check_cache(sha1($this->request->uri()) . filemtime($file_name), $this->request); // Send the file content as the response $this->response->body(file_get_contents($file_name)); // Set the proper headers to allow caching $this->response->headers('content-type', File::mime_by_ext($ext)); $this->response->headers('last-modified', date('r', filemtime($file_name))); // This is ignored by check_cache $this->response->headers('cache-control', 'public, max-age=2592000'); if (Config::get('media.cache', FALSE)) { // Set base path $path = Config::get('media.public_dir', 'media'); // Override path if we're in admin if ($theme) { $path = $path . DS . $theme; } // Save the contents to the public directory for future requests $public_path = $path . DS . $file . '.' . $ext; $directory = dirname($public_path); if (!is_dir($directory)) { // Recursively create the directories needed for the file System::mkdir($directory, 0777, TRUE); } file_put_contents($public_path, $this->response->body()); } } else { Log::error('Media controller error while loading file: :file', array(':file' => $file)); // Return a 404 status $this->response->status(404); } }
public function action_get() { // Get the file path from the request $file = $this->request->param('file'); // Find the file extension $ext = pathinfo($file, PATHINFO_EXTENSION); // Remove the extension from the filename $path = substr($file, 0, -(strlen($ext) + 1)); // Find the full path to the file $path = Kohana::find_file('media', $path, $ext); if (!$path) { throw new Shindig_Exception('File does not exist: :file', array(':file' => $file)); } // Set the content type for this extension $this->request->headers['Content-Type'] = File::mime_by_ext($ext); $this->request->headers['Content-Length'] = filesize($path); // Open the file and send the entire contents $file = fopen($path, 'rb'); fpassthru($file); fclose($file); }
/** * This is a copy of the media action from the userguide module * * @return void */ public function action_media() { // Get the file path from the request $file = $this->request->param('file'); // Find the file extension $ext = pathinfo($file, PATHINFO_EXTENSION); // Remove the extension from the filename $file = substr($file, 0, -(strlen($ext) + 1)); if ($file = Kohana::find_file('media', $file, $ext)) { // Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed $this->request->check_cache(sha1($this->request->uri) . filemtime($file)); // Send the file content as the response $this->request->response = file_get_contents($file); } else { // Return a 404 status $this->request->status = 404; } // Set the proper headers to allow caching $this->request->headers['Content-Type'] = File::mime_by_ext($ext); $this->request->headers['Content-Length'] = filesize($file); $this->request->headers['Last-Modified'] = date('r', filemtime($file)); }
public static function video_tag($source, array $attributes = NULL) { if (strpos('//', $source) === FALSE && isset($source[0]) && $source[0] !== '/') { $version = ''; $format = 'mp4'; if (preg_match('#^(?<source>.+)\\.(?<format>\\w+)$#', $source, $matches)) { $source = $matches['source']; $format = $matches['format']; } if (Kohana::$config->load('assets.versionizable') === TRUE) { $file_name = Assets::get_file($source, $format); if ($file_name && is_file($file_name)) { $version = '-' . hash_hmac_file('md5', $file_name, Kohana::$config->load('assets.versionizable.hmac_password')); } } $source = '/assets/' . $source . $version . '.' . $format; $mime = File::mime_by_ext($format); } else { $mime = File::mime($source); } return '<video' . HTML::attributes($attributes) . '><source src="' . $source . '" type="' . $mime . '" /><a href="' . $source . '">' . $source . '</a></video>'; }
public function action_index() { // Generate and check the ETag for this file $this->request->check_cache(sha1($this->request->uri)); // Get the file path from the request $file = $this->request->param('file'); // Find the file extension $ext = pathinfo($file, PATHINFO_EXTENSION); // Remove the extension from the filename $file = substr($file, 0, -(strlen($ext) + 1)); if ($file = Kohana::find_file('media', $file, $ext)) { // Send the file content as the response $this->request->response = file_get_contents($file); } else { // Return a 404 status $this->request->status = 404; } // Set the proper headers to allow caching $this->request->headers['Content-Type'] = File::mime_by_ext($ext); $this->request->headers['Content-Length'] = filesize($file); $this->request->headers['Last-Modified'] = date('r', filemtime($file)); }
public function action_index() { // Get the file path from the request $file = $this->request->param('file'); // Find the file extension $ext = pathinfo($file, PATHINFO_EXTENSION); // Remove the extension from the filename $file = substr($file, 0, -(strlen($ext) + 1)); // Find the file in 'static/' dirs across the cascading file system $filename = Kohana::find_file('static/', $file, $ext); if ($filename) { // Set the max-age for the file $max_age = 60 * 60 * 24 * 30; $filemtime = gmdate('r', filemtime($filename)); // Set the etag to compare $etag = md5($filename . $filemtime); // Get the etag $match_etag = $this->request->headers('if-none-match'); // Get the filemtime $match_filemtime = $this->request->headers('if-modified-since'); if ($filemtime == $match_filemtime || $etag == $match_etag) { $this->response->status(304); return; } // Get the file content and deliver it $this->response->body(file_get_contents($filename)); // Set the proper headers to allow caching $this->response->headers('Content-Type', File::mime_by_ext($ext)); $this->response->headers('Content-Length', filesize($filename)); $this->response->headers('Last-Modified', $filemtime); $this->response->headers('Expires', gmdate('r', time() + $max_age)); $this->response->headers('Cache-Control', 'max-age=' . $max_age . ', public'); $this->response->headers('Etag', $etag); } else { $this->response->status(404); // Throw some suitable exception here } }
public function action_media($dir = 'media/logsqlite', $file = '') { if (!$file) { // Get the file path from the request $file = $this->request->param('file'); } // Find the file extension $ext = pathinfo($file, PATHINFO_EXTENSION); // Remove the extension from the filename $file = substr($file, 0, -(strlen($ext) + 1)); if ($file = Kohana::find_file($dir, $file, $ext)) { // Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed $this->check_cache(sha1($this->request->uri()) . filemtime($file)); // Send the file content as the response $this->response->body(file_get_contents($file)); // Set the proper headers to allow caching $this->response->headers('content-type', File::mime_by_ext($ext)); $this->response->headers('last-modified', date('r', filemtime($file))); } else { // Return a 404 status $this->response->status(404); } }
/** * Attempt to get the mime type from a file. This method is horribly * unreliable, due to PHP being horribly unreliable when it comes to * determining the mime type of a file. * * $mime = File::mime($file); * * @param string $filepath file name or path * @param string $realname The real filename of the file (without the path) * @return string mime type on success * @return FALSE on failure */ public static function mime($filename, $realname) { // Get the complete path to the file $filename = realpath($filename); // Get the extension from the filename $extension = strtolower(pathinfo($realname, PATHINFO_EXTENSION)); if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension)) { // Use getimagesize() to find the mime type on images $file = getimagesize($filename); if (isset($file['mime'])) { return $file['mime']; } } // First lets try finfo if (class_exists('finfo')) { if ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME)) { return $info->file($filename); } else { if ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME, 'magic')) { return $info->file($filename); } } } // No finfo, so lets try mime_content_type if (function_exists('mime_content_type')) { $mimetype = mime_content_type($filename); if ($mimetype) { return $mimetype; } } // Nothing else has worked,lets try the mimetypes global array if (!empty($extension)) { return File::mime_by_ext($extension); } // Unable to find the mime-type return FALSE; }
/** * Process the verification. * * @return void */ public function action_index() { // Verify the credentials $service = $this->request->param('service'); $success = FALSE; if (!empty($service)) { switch (strtolower($service)) { case MMI_API::SERVICE_FACEBOOK: $success = MMI_API_Verify_OAuth::factory($service)->verify(); break; default: $success = MMI_API_Verify_OAuth::factory()->verify($service); break; } } // Send the response $this->request->headers['Content-Type'] = File::mime_by_ext('txt'); if ($success) { $this->request->response = 'Success'; } else { $this->request->response = 'Unauthorized'; $this->request->status = 401; } }
/** * Media routing code. Allows lazy users to load images via Kohana. See also: init.php. * I recommend just serving the files via apache, e.g. copy the public directory to your webroot. */ public function action_media() { // prevent auto render $this->auto_render = FALSE; // Generate and check the ETag for this file // $this->request->check_cache(sha1($this->request->uri)); // Get the file path from the request $file = Request::current()->param('file'); $dir = Request::current()->param('dir'); // Find the file extension $ext = pathinfo($file, PATHINFO_EXTENSION); // Remove the extension from the filename $file = substr($file, 0, -(strlen($ext) + 1)); $file = Kohana::find_file('public', $dir . '/' . $file, $ext); if ($file) { // Send the file content as the response $this->response->body(file_get_contents($file)); } else { // Return a 404 status $this->response->status(404); } // Set the proper headers to allow caching $this->response->headers('Content-Type', File::mime_by_ext($ext)); $this->response->headers('Content-Length', (string) filesize($file)); $this->response->headers('Last-Modified', date('r', filemtime($file))); }