]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/extlib/minify/min/lib/Minify/Controller/Version1.php
Added minify plugin
[quix0rs-gnu-social.git] / plugins / Minify / extlib / minify / min / lib / Minify / Controller / Version1.php
1 <?php
2 /**
3  * Class Minify_Controller_Version1  
4  * @package Minify
5  */
6
7 require_once 'Minify/Controller/Base.php';
8
9 /**
10  * Controller class for emulating version 1 of minify.php
11  * 
12  * <code>
13  * Minify::serve('Version1');
14  * </code>
15  * 
16  * @package Minify
17  * @author Stephen Clay <steve@mrclay.org>
18  */
19 class Minify_Controller_Version1 extends Minify_Controller_Base {
20     
21     /**
22      * Set up groups of files as sources
23      * 
24      * @param array $options controller and Minify options
25      * @return array Minify options
26      * 
27      */
28     public function setupSources($options) {
29         self::_setupDefines();
30         if (MINIFY_USE_CACHE) {
31             $cacheDir = defined('MINIFY_CACHE_DIR')
32                 ? MINIFY_CACHE_DIR
33                 : '';
34             Minify::setCache($cacheDir);
35         }
36         $options['badRequestHeader'] = 'HTTP/1.0 404 Not Found';
37         $options['contentTypeCharset'] = MINIFY_ENCODING;
38
39         // The following restrictions are to limit the URLs that minify will
40         // respond to. Ideally there should be only one way to reference a file.
41         if (! isset($_GET['files'])
42             // verify at least one file, files are single comma separated, 
43             // and are all same extension
44             || ! preg_match('/^[^,]+\\.(css|js)(,[^,]+\\.\\1)*$/', $_GET['files'], $m)
45             // no "//" (makes URL rewriting easier)
46             || strpos($_GET['files'], '//') !== false
47             // no "\"
48             || strpos($_GET['files'], '\\') !== false
49             // no "./"
50             || preg_match('/(?:^|[^\\.])\\.\\//', $_GET['files'])
51         ) {
52             return $options;
53         }
54         $extension = $m[1];
55         
56         $files = explode(',', $_GET['files']);
57         if (count($files) > MINIFY_MAX_FILES) {
58             return $options;
59         }
60         
61         // strings for prepending to relative/absolute paths
62         $prependRelPaths = dirname($_SERVER['SCRIPT_FILENAME'])
63             . DIRECTORY_SEPARATOR;
64         $prependAbsPaths = $_SERVER['DOCUMENT_ROOT'];
65         
66         $sources = array();
67         $goodFiles = array();
68         $hasBadSource = false;
69         
70         $allowDirs = isset($options['allowDirs'])
71             ? $options['allowDirs']
72             : MINIFY_BASE_DIR;
73         
74         foreach ($files as $file) {
75             // prepend appropriate string for abs/rel paths
76             $file = ($file[0] === '/' ? $prependAbsPaths : $prependRelPaths) . $file;
77             // make sure a real file!
78             $file = realpath($file);
79             // don't allow unsafe or duplicate files
80             if (parent::_fileIsSafe($file, $allowDirs) 
81                 && !in_array($file, $goodFiles)) 
82             {
83                 $goodFiles[] = $file;
84                 $srcOptions = array(
85                     'filepath' => $file
86                 );
87                 $this->sources[] = new Minify_Source($srcOptions);
88             } else {
89                 $hasBadSource = true;
90                 break;
91             }
92         }
93         if ($hasBadSource) {
94             $this->sources = array();
95         }
96         if (! MINIFY_REWRITE_CSS_URLS) {
97             $options['rewriteCssUris'] = false;
98         }
99         return $options;
100     }
101     
102     private static function _setupDefines()
103     {
104         $defaults = array(
105             'MINIFY_BASE_DIR' => realpath($_SERVER['DOCUMENT_ROOT'])
106             ,'MINIFY_ENCODING' => 'utf-8'
107             ,'MINIFY_MAX_FILES' => 16
108             ,'MINIFY_REWRITE_CSS_URLS' => true
109             ,'MINIFY_USE_CACHE' => true
110         );
111         foreach ($defaults as $const => $val) {
112             if (! defined($const)) {
113                 define($const, $val);
114             }
115         }
116     }
117 }
118