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