Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php defined('SYSPATH') or die('No direct script access.');
- class Controller_Media extends Controller {
- public function before()
- {
- parent::before();
- $this->config = Kohana::$config->load('media');
- $this->folder = 'assets';
- $this->member = new Helper_Member;
- if(isset($_GET['url-sign']) AND preg_match('/^[\.\d\w\/]+$/', $_GET['url-sign']))
- {
- $this->member->account = Model_Account::get_by('url-sign', $_GET['url-sign']);
- }
- }
- public function action_css()
- {
- $this->member->access_permission("public");
- $this->handle_request(
- 'style',
- $this->request->param('path'),
- $this->config['styles']['extension']
- );
- }
- public function action_js()
- {
- $this->member->access_permission("public");
- $this->handle_request(
- 'script',
- $this->request->param('path'),
- $this->config['scripts']['extension']
- );
- }
- public function action_views()
- {
- $this->member->access_permission("public");
- $this->handle_request(
- 'view',
- $this->request->param('path'),
- $this->config['views']['extension']
- );
- }
- public function action_fonts()
- {
- $this->member->access_permission("public");
- $extension = $this->find_extension(
- $this->config['fonts']['directory'].$this->request->param('path'),
- $this->config['fonts']['extension']
- );
- $this->handle_request(
- 'font',
- $this->request->param('path'),
- $extension
- );
- }
- public function action_img()
- {
- $this->member->access_permission("public");
- $extension = $this->find_extension(
- $this->config['images']['directory'].$this->request->param('path'),
- $this->config['images']['extension']
- );
- $this->handle_request(
- 'image',
- $this->request->param('path'),
- $extension
- );
- }
- public function action_video()
- {
- $this->member->access_permission("member");
- $this->folder = 'files';
- $extension = $this->find_extension(
- $this->config['videos']['directory'].$this->request->param('path'),
- $this->config['videos']['extension']
- );
- $this->handle_request(
- 'video',
- $this->request->param('path'),
- $extension
- );
- }
- public function action_audio()
- {
- if(isset($_GET['url-sign']))
- {
- $this->member->access_membership("audio");
- }
- else
- {
- $this->member->access_permission("member");
- }
- $this->folder = 'files';
- $extension = $this->find_extension(
- $this->config['audios']['directory'].$this->request->param('path'),
- $this->config['audios']['extension']
- );
- $this->handle_request(
- 'audio',
- $this->request->param('path'),
- $extension
- );
- }
- public function action_avatar()
- {
- $this->member->access_permission("public");
- $this->folder = 'files';
- $extension = $this->find_extension(
- $this->config['avatars']['directory'].$this->request->param('path'),
- $this->config['images']['extension']
- );
- $this->handle_request(
- 'avatar',
- $this->request->param('path'),
- $extension
- );
- }
- public function action_cv_document()
- {
- if(isset($_GET['url-sign']))
- {
- $this->member->access_membership("editing");
- }
- else
- {
- $this->member->access_permission("member");
- }
- $this->folder = 'files';
- $extension = $this->find_extension(
- $this->config['cv_documents']['directory'].$this->request->param('path'),
- $this->config['cv_documents']['extension']
- );
- $this->handle_request(
- 'cv_document',
- $this->request->param('path'),
- $extension
- );
- }
- public function action_qr()
- {
- $this->member->access_permission("public");
- $this->folder = 'files';
- $extension = $this->find_extension(
- $this->config['qrs']['directory'].$this->request->param('path'),
- $this->config['images']['extension']
- );
- $this->handle_request(
- 'qr',
- $this->request->param('path'),
- $extension
- );
- }
- protected function handle_request($action, $path, $extension)
- {
- $config_key = Inflector::plural($action);
- $file = $this->config[$config_key]['directory'].$path;
- if ($this->find_file($file, $extension))
- {
- $this->serve_file($file, $extension);
- }
- else
- {
- $this->error();
- }
- }
- protected function find_file($file, $extension)
- {
- $path_parts = pathinfo($file);
- // echo Debug::vars($path_parts, $this->folder, $path_parts['dirname']."/".$path_parts['filename'], $extension);
- return Kohana::find_file($this->folder, $path_parts['dirname']."/".$path_parts['filename'], $extension);
- }
- protected function find_extension($file, $exts)
- {
- $pi = pathinfo($file);
- foreach ($exts as $extension)
- {
- if ($pi['extension'] == $extension)
- {
- return $extension;
- }
- }
- return FALSE;
- }
- protected function serve_file($file, $extension)
- {
- $path = $this->find_file($file, $extension);
- $this->response->headers('Content-Type', File::mime_by_ext($extension));
- $this->response->headers('Content-Length', (string) filesize($path));
- $this->response->headers('Cache-Control','max-age=86400, public');
- $this->response->headers('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
- $this->response->body(file_get_contents($path));
- }
- protected function error()
- {
- throw new HTTP_Exception_404('File :file not found.', array(
- ':file' => $this->request->param('path', NULL),
- ));
- }
- } // End Welcome
Advertisement
Add Comment
Please, Sign In to add comment