* Copyright 2001-2010 Strangecode, LLC * * This file is part of The Strangecode Codebase. * * The Strangecode Codebase is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your option) * any later version. * * The Strangecode Codebase is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * The Strangecode Codebase. If not, see . */ /** * CSS.inc.css * * Dynamically outputs cached CSS data. * * @author Quinn Comendant * @version 1.2 */ class CSS { // Include these style sheets. var $_css_files = array('default' => array()); // CSS object parameters. var $_params = array( 'cache_css' => true, 'character_set' => 'utf-8', ); /** * Set (or overwrite existing) parameters by passing an array of new parameters. * * @access public * @param array $params Array of parameters (key => val pairs). */ function setParam($params) { $app =& App::getInstance(); if (isset($params) && is_array($params)) { // Merge new parameters with old overriding only those passed. $this->_params = array_merge($this->_params, $params); } else { $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); } } /** * Return the value of a parameter, if it exists. * * @access public * @param string $param Which parameter to return. * @return mixed Configured parameter value. */ function getParam($param) { $app =& App::getInstance(); if (isset($this->_params[$param])) { return $this->_params[$param]; } else { $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); return null; } } /** * Add a file-path to the array of files to include as CSS. * * @access public * @param string $file Include path to css files. * @param mixed $realms Realm name string or array of realm names. * @return bool True on success, false on failure. */ function setFile($file, $realms='') { $app =& App::getInstance(); if (!is_array($realms)) { $realms = array($realms); } if ($fp = fopen($file, 'r', true)) { foreach ($realms as $realm) { $realm = '' == $realm ? 'default' : $realm; $this->_css_files[$realm][] = $file; } fclose($fp); return true; } else { $app->logMsg(sprintf('CSS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__); return false; } } /** * Output headers for CSS. * * @access public * * @return bool False if no files have been set. */ function headers($realm='') { $app =& App::getInstance(); $realm = '' == $realm ? 'default' : $realm; if (empty($this->_css_files[$realm])) { $app->logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); return false; } // Get time of latest modified file, including this class file. $files_mtime = array(); foreach (array_merge($this->_css_files[$realm], array(__FILE__)) as $file) { $files_mtime[] = statIncludePath($file, 'mtime'); } sort($files_mtime, SORT_NUMERIC); $latest_mtime = array_pop($files_mtime); if ($this->_params['cache_css']) { header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $latest_mtime) . ' GMT'); header('Cache-Control: public, max-age=86400'); } else { header('Expires: -1'); header('Pragma: no-cache'); header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); } header('Content-Type: text/css; charset=' . $this->_params['character_set']); } /** * Include CSS files specified by setFile(). * * @access public * * @return bool False if no files have been set. */ function output($realm='') { $realm = '' == $realm ? 'default' : $realm; if (empty($this->_css_files[$realm])) { $app->logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); return false; } foreach ($this->_css_files[$realm] as $file) { include $file; } } } ?>