]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/extlib/minify/min/lib/Minify/Controller/Base.php
Added minify plugin
[quix0rs-gnu-social.git] / plugins / Minify / extlib / minify / min / lib / Minify / Controller / Base.php
1 <?php
2 /**
3  * Class Minify_Controller_Base  
4  * @package Minify
5  */
6
7 /**
8  * Base class for Minify controller
9  * 
10  * The controller class validates a request and uses it to create sources
11  * for minification and set options like contentType. It's also responsible
12  * for loading minifier code upon request.
13  * 
14  * @package Minify
15  * @author Stephen Clay <steve@mrclay.org>
16  */
17 abstract class Minify_Controller_Base {
18     
19     /**
20      * Setup controller sources and set an needed options for Minify::source
21      * 
22      * You must override this method in your subclass controller to set 
23      * $this->sources. If the request is NOT valid, make sure $this->sources 
24      * is left an empty array. Then strip any controller-specific options from 
25      * $options and return it. To serve files, $this->sources must be an array of
26      * Minify_Source objects.
27      * 
28      * @param array $options controller and Minify options
29      * 
30      * return array $options Minify::serve options
31      */
32     abstract public function setupSources($options);
33     
34     /**
35      * Get default Minify options for this controller.
36      * 
37      * Override in subclass to change defaults
38      *
39      * @return array options for Minify
40      */
41     public function getDefaultMinifyOptions() {
42         return array(
43             'isPublic' => true
44             ,'encodeOutput' => function_exists('gzdeflate')
45             ,'encodeMethod' => null // determine later
46             ,'encodeLevel' => 9
47             ,'minifierOptions' => array() // no minifier options
48             ,'contentTypeCharset' => 'utf-8'
49             ,'maxAge' => 1800 // 30 minutes
50             ,'rewriteCssUris' => true
51             ,'bubbleCssImports' => false
52             ,'quiet' => false // serve() will send headers and output
53             ,'debug' => false
54             
55             // if you override this, the response code MUST be directly after 
56             // the first space.
57             ,'badRequestHeader' => 'HTTP/1.0 400 Bad Request'
58             
59             // callback function to see/modify content of all sources
60             ,'postprocessor' => null
61             // file to require to load preprocessor
62             ,'postprocessorRequire' => null
63         );
64     }  
65
66     /**
67      * Get default minifiers for this controller.
68      * 
69      * Override in subclass to change defaults
70      *
71      * @return array minifier callbacks for common types
72      */
73     public function getDefaultMinifers() {
74         $ret[Minify::TYPE_JS] = array('JSMin', 'minify');
75         $ret[Minify::TYPE_CSS] = array('Minify_CSS', 'minify');
76         $ret[Minify::TYPE_HTML] = array('Minify_HTML', 'minify');
77         return $ret;
78     }
79     
80     /**
81      * Load any code necessary to execute the given minifier callback.
82      * 
83      * The controller is responsible for loading minification code on demand
84      * via this method. This built-in function will only load classes for
85      * static method callbacks where the class isn't already defined. It uses
86      * the PEAR convention, so, given array('Jimmy_Minifier', 'minCss'), this 
87      * function will include 'Jimmy/Minifier.php'.
88      * 
89      * If you need code loaded on demand and this doesn't suit you, you'll need
90      * to override this function in your subclass. 
91      * @see Minify_Controller_Page::loadMinifier()
92      * 
93      * @param callback $minifierCallback callback of minifier function
94      * 
95      * @return null
96      */
97     public function loadMinifier($minifierCallback)
98     {
99         if (is_array($minifierCallback)
100             && is_string($minifierCallback[0])
101             && !class_exists($minifierCallback[0], false)) {
102             
103             require str_replace('_', '/', $minifierCallback[0]) . '.php';
104         }
105     }
106     
107     /**
108      * Is a user-given file within an allowable directory, existing,
109      * and having an extension js/css/html/txt ?
110      * 
111      * This is a convenience function for controllers that have to accept
112      * user-given paths
113      *
114      * @param string $file full file path (already processed by realpath())
115      * 
116      * @param array $safeDirs directories where files are safe to serve. Files can also
117      * be in subdirectories of these directories.
118      * 
119      * @return bool file is safe
120      */
121     public static function _fileIsSafe($file, $safeDirs)
122     {
123         $pathOk = false;
124         foreach ((array)$safeDirs as $safeDir) {
125             if (strpos($file, $safeDir) === 0) {
126                 $pathOk = true;
127                 break;
128             }
129         }
130         $base = basename($file);
131         if (! $pathOk || ! is_file($file) || $base[0] === '.') {
132             return false;
133         }
134         list($revExt) = explode('.', strrev($base));
135         return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
136     }
137     
138     /**
139      * @var array instances of Minify_Source, which provide content and
140      * any individual minification needs.
141      * 
142      * @see Minify_Source
143      */
144     public $sources = array();
145     
146     /**
147      * Mix in default controller options with user-given options
148      * 
149      * @param array $options user options
150      * 
151      * @return array mixed options
152      */
153     public final function mixInDefaultOptions($options)
154     {
155         $ret = array_merge(
156             $this->getDefaultMinifyOptions(), $options
157         );
158         if (! isset($options['minifiers'])) {
159             $options['minifiers'] = array();
160         }
161         $ret['minifiers'] = array_merge(
162             $this->getDefaultMinifers(), $options['minifiers']
163         );
164         return $ret;
165     }
166     
167     /**
168      * Analyze sources (if there are any) and set $options 'contentType' 
169      * and 'lastModifiedTime' if they already aren't.
170      * 
171      * @param array $options options for Minify
172      * 
173      * @return array options for Minify
174      */
175     public final function analyzeSources($options = array()) 
176     {
177         if ($this->sources) {
178             if (! isset($options['contentType'])) {
179                 $options['contentType'] = Minify_Source::getContentType($this->sources);
180             }
181             // last modified is needed for caching, even if setExpires is set
182             if (! isset($options['lastModifiedTime'])) {
183                 $max = 0;
184                 foreach ($this->sources as $source) {
185                     $max = max($source->lastModified, $max);
186                 }
187                 $options['lastModifiedTime'] = $max;
188             }    
189         }
190         return $options;
191     }
192
193     /**
194      * Send message to the Minify logger
195      * @param string $msg
196      * @return null
197      */
198     protected function log($msg) {
199         require_once 'Minify/Logger.php';
200         Minify_Logger::log($msg);
201     }
202 }