1 <?php
 2 
 3 /**
 4  * @package redaxo\media-manager
 5  */
 6 class rex_effect_header extends rex_effect_abstract
 7 {
 8     public function execute()
 9     {
10         if ($this->params['cache'] == 'no_cache') {
11             $this->media->setHeader('Cache-Control', 'must-revalidate, proxy-revalidate, private, no-cache, max-age=0');
12             $this->media->setHeader('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT'); // in the past
13         } elseif ($this->params['cache'] !== 'cache' /* bc */ && $this->params['cache'] !== 'unspecified') {
14             switch ($this->params['cache']) {
15                 case 'max-age: 1 min':
16                     $seconds = 60;
17                     break;
18                 case 'max-age: 1 hour':
19                     $seconds = 60 * 60;
20                     break;
21                 case 'max-age: 1 day':
22                     $seconds = 60 * 60 * 24;
23                     break;
24                 case 'max-age: 1 week':
25                     $seconds = 60 * 60 * 24 * 7;
26                     break;
27                 case 'max-age: 1 month':
28                     $seconds = 60 * 60 * 24 * 30;
29                     break;
30                 case 'max-age: 1 year':
31                 case 'immutable':
32                     $seconds = 60 * 60 * 24 * 365;
33                     break;
34                 default:
35                     throw new LogicException(sprintf('Unsupported cache duration "%s".', $this->params['cache']));
36             }
37 
38             $cacheControl = 'proxy-revalidate, private, max-age='.$seconds;
39 
40             if ('immutable' === $this->params['cache']) {
41                 $cacheControl .= ', immutable';
42             }
43 
44             $this->media->setHeader('Cache-Control', $cacheControl);
45         }
46 
47         if ($this->params['download'] == 'download') {
48             $this->media->setHeader('Content-Disposition', 'attachment; filename="' . basename($this->media->getMediaFilename()) . '";');
49         }
50 
51         /*
52          header("Pragma: public"); // required
53          header("Expires: 0");
54          header("Content-Transfer-Encoding: binary");
55          header("Content-Length: ".$fsize);
56          */
57     }
58 
59     public function getName()
60     {
61         return rex_i18n::msg('media_manager_effect_header');
62     }
63 
64     public function getParams()
65     {
66         return [
67             [
68                 'label' => rex_i18n::msg('media_manager_effect_header_download'),
69                 'name' => 'download',
70                 'type' => 'select',
71                 'options' => ['open_media', 'download'],
72                 'default' => 'open_media',
73             ],
74             [
75                 'label' => rex_i18n::msg('media_manager_effect_header_cache'),
76                 'name' => 'cache',
77                 'type' => 'select',
78                 'options' => ['no_cache', 'unspecified', 'max-age: 1 min', 'max-age: 1 hour', 'max-age: 1 day', 'max-age: 1 week', 'max-age: 1 month', 'max-age: 1 year', 'immutable'],
79                 'default' => 'no_cache',
80             ],
81         ];
82     }
83 }
84