<?php
/**
 * $Horde: horde/css.php,v 1.31 2003/04/11 18:53:59 slusarz Exp $
 *
 * Copyright 2000-2003 Charles J. Hagenbuch <chuck@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL).  If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 */

@define('HORDE_BASE', dirname(__FILE__));

// Strings
require_once HORDE_BASE . '/lib/String.php';

// Browser detection library
require_once HORDE_BASE . '/lib/Browser.php';
$browser = &Browser::singleton();

// Registry
require_once HORDE_BASE . '/lib/Registry.php';
$registry = &Registry::singleton(HORDE_SESSION_NONE);

// Horde base libraries
require_once HORDE_BASE . '/lib/Horde.php';

// Figure out if we've been inlined, or called directly.
$send_headers = strstr($_SERVER['PHP_SELF'], 'css.php');

// Set initial $mtime of this script.
$mtime = getlastmod();

if (@file_exists(HORDE_BASE . '/config/conf.php')) {
    require HORDE_BASE . '/config/conf.php';
} else {
    $conf['css']['cached'] = false;
}

$theme = Horde::getFormData('theme');
if (Horde::getFormData('inherit') !== 'no') {
    if (@file_exists(HORDE_BASE . '/config/html.php')) {
        $file = HORDE_BASE . '/config/html.php';
    } else {
        $file = HORDE_BASE . '/config/html.php.dist';
    }
    if ($conf['css']['cached']) {
        $hmtime = filemtime($file);
        if ($hmtime > $mtime) {
            $mtime = $hmtime;
        }
    }
    require $file;
    if (!empty($theme) && @file_exists(HORDE_BASE . '/config/themes/html-' . $theme . '.php')) {
        $file = HORDE_BASE . '/config/themes/html-' . $theme . '.php';
        if ($conf['css']['cached']) {
            $hmtime = filemtime($file);
            if ($hmtime > $mtime) {
                $mtime = $hmtime;
            }
        }
        require $file;
    }
}

$apps = Horde::getFormData('app');
if (!empty($apps)) {
    if (!is_array($apps)) {
        $apps = array($apps);
    }
    foreach ($apps as $app) {
        $conf_file = $registry->applicationFilePath('%application%/config/conf.php', $app);
        if (@file_exists($conf_file)) {
            require $conf_file;
        }

        $file = '';
        if (@file_exists($registry->applicationFilePath('%application%/config/html.php', $app))) {
            $file = $registry->applicationFilePath('%application%/config/html.php', $app);
        } elseif (@file_exists($registry->applicationFilePath('%application%/config/html.php.dist', $app))) {
            $file = $registry->applicationFilePath('%application%/config/html.php.dist', $app);
        }
        if (!empty($file)) {
            if ($conf['css']['cached']) {
                $amtime = filemtime($file);
                if ($amtime > $mtime) {
                    $mtime = $amtime;
                }
            }
            require $file;
        }
        if (!empty($theme) && @file_exists($registry->applicationFilePath('%application%/config/themes/html-' . $theme . '.php', $app))) {
            $file = $registry->applicationFilePath('%application%/config/themes/html-' . $theme . '.php', $app);
            if ($conf['css']['cached']) {
                $amtime = filemtime($file);
                if ($amtime > $mtime) {
                    $mtime = $amtime;
                }
            }
            require $file;
        }
    }
}

if ($send_headers) {
    if ($conf['css']['cached']) {
        $mod_gmt = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
        header('Last-Modified: ' . $mod_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=iso-8859-1');
}

if (is_array($css)) {
    foreach ($css as $class => $params) {
        echo "$class{";
        if (is_array($params)) {
            foreach ($params as $key => $val) {
                echo "$key:$val;";
            }
        }
        echo '} ';
    }
}
