[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', |
---|
| 40 | 'cache_js' => false, |
---|
[553] | 41 | 'cache_seconds' => 86400, |
---|
[469] | 42 | 'strip_whitespace' => false, |
---|
| 43 | 'output_compression' => false, |
---|
| 44 | ); |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 48 | * |
---|
| 49 | * @access public |
---|
| 50 | * @param array $params Array of parameters (key => val pairs). |
---|
| 51 | */ |
---|
| 52 | public function setParam($params) |
---|
| 53 | { |
---|
[479] | 54 | $app =& App::getInstance(); |
---|
[469] | 55 | |
---|
| 56 | if (isset($params) && is_array($params)) { |
---|
| 57 | // Merge new parameters with old overriding only those passed. |
---|
| 58 | $this->_params = array_merge($this->_params, $params); |
---|
| 59 | } else { |
---|
| 60 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * Return the value of a parameter, if it exists. |
---|
| 66 | * |
---|
| 67 | * @access public |
---|
| 68 | * @param string $param Which parameter to return. |
---|
| 69 | * @return mixed Configured parameter value. |
---|
| 70 | */ |
---|
| 71 | public function getParam($param) |
---|
| 72 | { |
---|
[479] | 73 | $app =& App::getInstance(); |
---|
[469] | 74 | |
---|
[478] | 75 | if (array_key_exists($param, $this->_params)) { |
---|
[469] | 76 | return $this->_params[$param]; |
---|
| 77 | } else { |
---|
| 78 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 79 | return null; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Add a file-path to the array of files to include as JS. |
---|
| 85 | * |
---|
| 86 | * @access public |
---|
| 87 | * @param string $file Include path to JS files. |
---|
| 88 | * @param mixed $realms Realm name string or array of realm names. |
---|
| 89 | * @return bool True on success, false on failure. |
---|
| 90 | */ |
---|
| 91 | public function setFile($file, $realms='') |
---|
| 92 | { |
---|
[479] | 93 | $app =& App::getInstance(); |
---|
[469] | 94 | |
---|
| 95 | if (!is_array($realms)) { |
---|
| 96 | $realms = array($realms); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | if ($fp = fopen($file, 'r', true)) { |
---|
| 100 | foreach ($realms as $realm) { |
---|
| 101 | $realm = '' == $realm ? 'default' : $realm; |
---|
| 102 | $this->_js_files[$realm][] = $file; |
---|
| 103 | } |
---|
| 104 | fclose($fp); |
---|
| 105 | return true; |
---|
| 106 | } else { |
---|
| 107 | $app->logMsg(sprintf('JS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__); |
---|
| 108 | return false; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Output headers for JS. |
---|
| 114 | * |
---|
| 115 | * @access public |
---|
| 116 | * |
---|
| 117 | * @return bool False if no files have been set. |
---|
| 118 | */ |
---|
| 119 | public function headers($realm='') |
---|
| 120 | { |
---|
[479] | 121 | $app =& App::getInstance(); |
---|
[469] | 122 | |
---|
| 123 | $realm = '' == $realm ? 'default' : $realm; |
---|
| 124 | |
---|
| 125 | if (empty($this->_js_files[$realm])) { |
---|
| 126 | $app->logMsg(sprintf('JS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
| 127 | return false; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | // Get time of latest modified file, including this class file. |
---|
| 131 | $files_mtime = array(); |
---|
| 132 | foreach (array_merge($this->_js_files[$realm], array(__FILE__)) as $file) { |
---|
| 133 | $files_mtime[] = statIncludePath($file, 'mtime'); |
---|
| 134 | } |
---|
| 135 | sort($files_mtime, SORT_NUMERIC); |
---|
| 136 | $latest_mtime = array_pop($files_mtime); |
---|
| 137 | |
---|
| 138 | if ($this->getParam('output_compression') && extension_loaded('zlib')) { |
---|
| 139 | ob_start('ob_gzhandler'); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | if ($this->getParam('cache_js')) { |
---|
[553] | 143 | header(sprintf('Last-Modified: %s GMT', gmdate('D, d M Y H:i:s', $latest_mtime), null)); |
---|
| 144 | header(sprintf('Cache-Control: private, max-age=%s', $this->getParam('cache_seconds'))); |
---|
| 145 | header('Pragma: private'); |
---|
[469] | 146 | } else { |
---|
| 147 | header('Expires: -1'); |
---|
| 148 | header('Pragma: no-cache'); |
---|
| 149 | header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); |
---|
| 150 | } |
---|
| 151 | header('Content-Type: application/javascript; charset=' . $this->getParam('character_set')); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * Include JS files specified by setFile(). |
---|
| 156 | * |
---|
| 157 | * @access public |
---|
| 158 | * |
---|
| 159 | * @return bool False if no files have been set. |
---|
| 160 | */ |
---|
| 161 | public function output($realm='') |
---|
| 162 | { |
---|
[473] | 163 | $app =& App::getInstance(); |
---|
| 164 | |
---|
[469] | 165 | $realm = '' == $realm ? 'default' : $realm; |
---|
| 166 | |
---|
| 167 | if (empty($this->_js_files[$realm])) { |
---|
| 168 | $app->logMsg(sprintf('JS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
| 169 | return false; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | foreach ($this->_js_files[$realm] as $file) { |
---|
| 173 | if ($this->getParam('strip_whitespace')) { |
---|
| 174 | // Strip whitespace and print file. |
---|
[471] | 175 | echo preg_replace( |
---|
| 176 | array('!^\s*//.*$!m', '!/\*.*?\*/!s', '/[\n\r]+/', '/[ \t]*}[ \t]*/', '/\s*{\s*/', '/\s*=\s*/', '/^\s+/m'), |
---|
| 177 | array('', '', "\n", '}', '{', '=', ''), file_get_contents($file, true) |
---|
| 178 | ); |
---|
[469] | 179 | } else { |
---|
| 180 | // Include file as is. |
---|
| 181 | include $file; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | if ($this->getParam('output_compression') && extension_loaded('zlib')) { |
---|
| 186 | ob_end_flush(); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | } |
---|