cosname

Kohana Controller various media handler example

Jun 7th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.29 KB | None | 0 0
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. class Controller_Media extends Controller {
  4.  
  5.     public function before()
  6.     {
  7.         parent::before();
  8.  
  9.         $this->config = Kohana::$config->load('media');
  10.         $this->folder = 'assets';
  11.         $this->member = new Helper_Member;
  12.  
  13.         if(isset($_GET['url-sign']) AND preg_match('/^[\.\d\w\/]+$/', $_GET['url-sign']))
  14.         {
  15.             $this->member->account = Model_Account::get_by('url-sign', $_GET['url-sign']);
  16.         }
  17.     }
  18.  
  19.     public function action_css()
  20.     {
  21.         $this->member->access_permission("public");
  22.         $this->handle_request(
  23.             'style',
  24.             $this->request->param('path'),
  25.             $this->config['styles']['extension']
  26.         );
  27.     }
  28.  
  29.     public function action_js()
  30.     {
  31.         $this->member->access_permission("public");
  32.  
  33.         $this->handle_request(
  34.             'script',
  35.             $this->request->param('path'),
  36.             $this->config['scripts']['extension']
  37.         );
  38.     }
  39.  
  40.     public function action_views()
  41.     {
  42.         $this->member->access_permission("public");
  43.  
  44.         $this->handle_request(
  45.             'view',
  46.             $this->request->param('path'),
  47.             $this->config['views']['extension']
  48.         );
  49.     }
  50.  
  51.     public function action_fonts()
  52.     {
  53.         $this->member->access_permission("public");
  54.  
  55.         $extension = $this->find_extension(
  56.             $this->config['fonts']['directory'].$this->request->param('path'),
  57.             $this->config['fonts']['extension']
  58.         );
  59.  
  60.         $this->handle_request(
  61.             'font',
  62.             $this->request->param('path'),
  63.             $extension
  64.         );
  65.     }
  66.  
  67.     public function action_img()
  68.     {
  69.         $this->member->access_permission("public");
  70.  
  71.         $extension = $this->find_extension(
  72.             $this->config['images']['directory'].$this->request->param('path'),
  73.             $this->config['images']['extension']
  74.         );
  75.  
  76.         $this->handle_request(
  77.             'image',
  78.             $this->request->param('path'),
  79.             $extension
  80.         );
  81.     }
  82.  
  83.     public function action_video()
  84.     {
  85.         $this->member->access_permission("member");
  86.         $this->folder = 'files';
  87.  
  88.         $extension = $this->find_extension(
  89.             $this->config['videos']['directory'].$this->request->param('path'),
  90.             $this->config['videos']['extension']
  91.         );
  92.  
  93.         $this->handle_request(
  94.             'video',
  95.             $this->request->param('path'),
  96.             $extension
  97.         );
  98.     }
  99.  
  100.     public function action_audio()
  101.     {
  102.         if(isset($_GET['url-sign']))
  103.         {
  104.             $this->member->access_membership("audio");
  105.         }
  106.         else
  107.         {
  108.             $this->member->access_permission("member");
  109.         }
  110.         $this->folder = 'files';
  111.        
  112.         $extension = $this->find_extension(
  113.             $this->config['audios']['directory'].$this->request->param('path'),
  114.             $this->config['audios']['extension']
  115.         );
  116.  
  117.         $this->handle_request(
  118.             'audio',
  119.             $this->request->param('path'),
  120.             $extension
  121.         );
  122.     }
  123.  
  124.     public function action_avatar()
  125.     {
  126.         $this->member->access_permission("public");
  127.         $this->folder = 'files';
  128.  
  129.         $extension = $this->find_extension(
  130.             $this->config['avatars']['directory'].$this->request->param('path'),
  131.             $this->config['images']['extension']
  132.         );
  133.  
  134.         $this->handle_request(
  135.             'avatar',
  136.             $this->request->param('path'),
  137.             $extension
  138.         );
  139.     }
  140.  
  141.     public function action_cv_document()
  142.     {
  143.         if(isset($_GET['url-sign']))
  144.         {
  145.             $this->member->access_membership("editing");
  146.         }
  147.         else
  148.         {
  149.             $this->member->access_permission("member");
  150.         }
  151.         $this->folder = 'files';
  152.  
  153.         $extension = $this->find_extension(
  154.             $this->config['cv_documents']['directory'].$this->request->param('path'),
  155.             $this->config['cv_documents']['extension']
  156.         );
  157.  
  158.         $this->handle_request(
  159.             'cv_document',
  160.             $this->request->param('path'),
  161.             $extension
  162.         );
  163.     }
  164.  
  165.     public function action_qr()
  166.     {
  167.         $this->member->access_permission("public");
  168.         $this->folder = 'files';
  169.  
  170.         $extension = $this->find_extension(
  171.             $this->config['qrs']['directory'].$this->request->param('path'),
  172.             $this->config['images']['extension']
  173.         );
  174.  
  175.         $this->handle_request(
  176.             'qr',
  177.             $this->request->param('path'),
  178.             $extension
  179.         );
  180.     }
  181.  
  182.     protected function handle_request($action, $path, $extension)
  183.     {
  184.         $config_key = Inflector::plural($action);
  185.         $file = $this->config[$config_key]['directory'].$path;
  186.  
  187.         if ($this->find_file($file, $extension))
  188.         {
  189.             $this->serve_file($file, $extension);
  190.         }
  191.         else
  192.         {
  193.             $this->error();
  194.         }
  195.     }
  196.  
  197.     protected function find_file($file, $extension)
  198.     {
  199.         $path_parts = pathinfo($file);
  200.  
  201.         // echo Debug::vars($path_parts, $this->folder, $path_parts['dirname']."/".$path_parts['filename'], $extension);
  202.  
  203.         return Kohana::find_file($this->folder, $path_parts['dirname']."/".$path_parts['filename'], $extension);
  204.     }
  205.  
  206.     protected function find_extension($file, $exts)
  207.     {
  208.         $pi = pathinfo($file);
  209.  
  210.         foreach ($exts as $extension)
  211.         {
  212.             if ($pi['extension'] == $extension)
  213.             {
  214.                 return $extension;
  215.             }
  216.         }
  217.  
  218.         return FALSE;
  219.     }
  220.  
  221.     protected function serve_file($file, $extension)
  222.     {
  223.         $path = $this->find_file($file, $extension);
  224.  
  225.         $this->response->headers('Content-Type', File::mime_by_ext($extension));
  226.         $this->response->headers('Content-Length', (string) filesize($path));
  227.         $this->response->headers('Cache-Control','max-age=86400, public');
  228.         $this->response->headers('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
  229.         $this->response->body(file_get_contents($path));
  230.     }
  231.  
  232.     protected function error()
  233.     {
  234.         throw new HTTP_Exception_404('File :file not found.', array(
  235.             ':file' => $this->request->param('path', NULL),
  236.         ));
  237.     }
  238.  
  239. } // End Welcome
Advertisement
Add Comment
Please, Sign In to add comment