[469] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
| 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
| 6 | * |
---|
| 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
| 13 | * |
---|
| 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * JS.inc.php |
---|
| 25 | * |
---|
| 26 | * Dynamically outputs minified JS data. |
---|
| 27 | * |
---|
| 28 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 29 | * @version 1.2 |
---|
| 30 | */ |
---|
[502] | 31 | class JS |
---|
| 32 | { |
---|
[469] | 33 | |
---|
| 34 | // Include these style sheets. |
---|
[484] | 35 | protected $_js_files = array('default' => array()); |
---|
[469] | 36 | |
---|
| 37 | // JS object parameters. |
---|
[484] | 38 | protected $_params = array( |
---|
[469] | 39 | 'character_set' => 'utf-8', |
---|
[554] | 40 | 'cache_enable' => false, |
---|
| 41 | 'cache_js' => null, // Same as above; kept for backwards compatibility. |
---|
[553] | 42 | 'cache_seconds' => 86400, |
---|
[469] | 43 | 'strip_whitespace' => false, |
---|
| 44 | 'output_compression' => false, |
---|
| 45 | ); |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 49 | * |
---|
| 50 | * @access public |
---|
| 51 | * @param array $params Array of parameters (key => val pairs). |
---|
| 52 | */ |
---|
| 53 | public function setParam($params) |
---|
| 54 | { |
---|
[479] | 55 | $app =& App::getInstance(); |
---|
[469] | 56 | |
---|
| 57 | if (isset($params) && is_array($params)) { |
---|
| 58 | // Merge new parameters with old overriding only those passed. |
---|
| 59 | $this->_params = array_merge($this->_params, $params); |
---|
[554] | 60 | // Maintaining backwards compatibility. |
---|
| 61 | if (isset($params['cache_js']) && !isset($params['cache_enable'])) { |
---|
| 62 | $this->_params['cache_enable'] = $params['cache_js']; |
---|
| 63 | } |
---|
[469] | 64 | } else { |
---|
| 65 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * Return the value of a parameter, if it exists. |
---|
| 71 | * |
---|
| 72 | * @access public |
---|
| 73 | * @param string $param Which parameter to return. |
---|
| 74 | * @return mixed Configured parameter value. |
---|
| 75 | */ |
---|
| 76 | public function getParam($param) |
---|
| 77 | { |
---|
[479] | 78 | $app =& App::getInstance(); |
---|
[469] | 79 | |
---|
[478] | 80 | if (array_key_exists($param, $this->_params)) { |
---|
[469] | 81 | return $this->_params[$param]; |
---|
| 82 | } else { |
---|
| 83 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 84 | return null; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * Add a file-path to the array of files to include as JS. |
---|
| 90 | * |
---|
| 91 | * @access public |
---|
| 92 | * @param string $file Include path to JS files. |
---|
| 93 | * @param mixed $realms Realm name string or array of realm names. |
---|
| 94 | * @return bool True on success, false on failure. |
---|
| 95 | */ |
---|
| 96 | public function setFile($file, $realms='') |
---|
| 97 | { |
---|
[479] | 98 | $app =& App::getInstance(); |
---|
[469] | 99 | |
---|
| 100 | if (!is_array($realms)) { |
---|
| 101 | $realms = array($realms); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | if ($fp = fopen($file, 'r', true)) { |
---|
| 105 | foreach ($realms as $realm) { |
---|
| 106 | $realm = '' == $realm ? 'default' : $realm; |
---|
| 107 | $this->_js_files[$realm][] = $file; |
---|
| 108 | } |
---|
| 109 | fclose($fp); |
---|
| 110 | return true; |
---|
| 111 | } else { |
---|
| 112 | $app->logMsg(sprintf('JS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__); |
---|
| 113 | return false; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * Output headers for JS. |
---|
| 119 | * |
---|
| 120 | * @access public |
---|
| 121 | * |
---|
| 122 | * @return bool False if no files have been set. |
---|
| 123 | */ |
---|
| 124 | public function headers($realm='') |
---|
| 125 | { |
---|
[479] | 126 | $app =& App::getInstance(); |
---|
[469] | 127 | |
---|
| 128 | $realm = '' == $realm ? 'default' : $realm; |
---|
| 129 | |
---|
| 130 | if (empty($this->_js_files[$realm])) { |
---|
| 131 | $app->logMsg(sprintf('JS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
| 132 | return false; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | // Get time of latest modified file, including this class file. |
---|
| 136 | $files_mtime = array(); |
---|
| 137 | foreach (array_merge($this->_js_files[$realm], array(__FILE__)) as $file) { |
---|
| 138 | $files_mtime[] = statIncludePath($file, 'mtime'); |
---|
| 139 | } |
---|
| 140 | sort($files_mtime, SORT_NUMERIC); |
---|
| 141 | $latest_mtime = array_pop($files_mtime); |
---|
| 142 | |
---|
| 143 | if ($this->getParam('output_compression') && extension_loaded('zlib')) { |
---|
| 144 | ob_start('ob_gzhandler'); |
---|
| 145 | } |
---|
| 146 | |
---|
[574] | 147 | header(sprintf('Content-Type: application/javascript; charset=%s', $this->getParam('character_set'))); |
---|
[555] | 148 | header(sprintf('Last-Modified: %s GMT', gmdate('D, d M Y H:i:s', $latest_mtime), null)); |
---|
[554] | 149 | if ($this->getParam('cache_enable')) { |
---|
[553] | 150 | header(sprintf('Cache-Control: private, max-age=%s', $this->getParam('cache_seconds'))); |
---|
[555] | 151 | header(sprintf('Expires: %s GMT', gmdate('D, d M Y H:i:s', $latest_mtime + $this->getParam('cache_seconds')))); |
---|
[553] | 152 | header('Pragma: private'); |
---|
[555] | 153 | header('Vary: Accept-Encoding'); |
---|
[469] | 154 | } else { |
---|
[603] | 155 | // Disallow HTTP caching entirely. http://stackoverflow.com/a/2068407 |
---|
| 156 | header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1. |
---|
| 157 | header('Pragma: no-cache'); // HTTP 1.0. |
---|
| 158 | header('Expires: 0'); // Proxies. |
---|
[469] | 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * Include JS files specified by setFile(). |
---|
| 164 | * |
---|
| 165 | * @access public |
---|
| 166 | * |
---|
| 167 | * @return bool False if no files have been set. |
---|
| 168 | */ |
---|
| 169 | public function output($realm='') |
---|
| 170 | { |
---|
[473] | 171 | $app =& App::getInstance(); |
---|
| 172 | |
---|
[469] | 173 | $realm = '' == $realm ? 'default' : $realm; |
---|
| 174 | |
---|
| 175 | if (empty($this->_js_files[$realm])) { |
---|
| 176 | $app->logMsg(sprintf('JS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
| 177 | return false; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | foreach ($this->_js_files[$realm] as $file) { |
---|
| 181 | if ($this->getParam('strip_whitespace')) { |
---|
| 182 | // Strip whitespace and print file. |
---|
[471] | 183 | echo preg_replace( |
---|
[589] | 184 | array('/(?<=^|;|{)\s*\/\/.*$/m', '/(?<=^|;|{)\s*\/\*.*?\*\//ms', '/[\n\r]+/', '/[ \t]+}[ \t]+/', '/[ \t]+{[ \t]+/', '/\s+=\s+/', '/^[ \t]+/m', '/[ \t]+$/m'), |
---|
| 185 | array('', '', "\n", '}', '{', '=', '', ''), file_get_contents($file, true) |
---|
[471] | 186 | ); |
---|
[469] | 187 | } else { |
---|
| 188 | // Include file as is. |
---|
| 189 | include $file; |
---|
| 190 | } |
---|
[589] | 191 | // Make sure there's at least one new-line between files. |
---|
| 192 | echo "\n"; |
---|
[469] | 193 | } |
---|
| 194 | |
---|
| 195 | if ($this->getParam('output_compression') && extension_loaded('zlib')) { |
---|
| 196 | ob_end_flush(); |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | } |
---|