Skip to content

Instantly share code, notes, and snippets.

@fabd
Created October 7, 2015 19:59
Show Gist options
  • Save fabd/6d0955861e65779f1b4d to your computer and use it in GitHub Desktop.
Save fabd/6d0955861e65779f1b4d to your computer and use it in GitHub Desktop.
A modified css.php file to allow for easier editing of the stylesheets when working on a MyBB theme. Bypasses the cached files and uses *.css files you can edit in your editor of choice, and instantly see results.
<?php
/**
* MyBB 1.8
* Copyright 2014 MyBB Group, All Rights Reserved
*
* Website: http://www.mybb.com
* License: http://www.mybb.com/about/license
*
*/
/**
* Modified css.php from MyBB 1.8.6, by github.com/fabd
*
* The purpose of this hack is to allow easier editing while developing a MyBB
* theme (on a local MyBB installation).
*
* Edit the FLAT_FILES_PATH below to point to a subdirectory of the MyBB
* installation. Any css files found there that match the name of a stylesheet
* referenced by the current page, will be returned instead of the cached file.
*
* If your theme folder does not contain a file matching the requested css
* file, then the file is either pulled from the cache/ folder as it normally
* is or it is pulled from the database (when MyBB creates css.php?id= urls).
*
* A special comment is inserted at the top of the CSS files to help you figure
* out if a css file is not found, or is not named properly, or is missing from
* the cache altogether (usually if the cache/ folder is empty, load up
* global.css in the admin and Save it once to regenerate the cache).
*
* Setup:
*
* 1. create a subfolder
* $ mkdir theme
* $ chmod 777 theme
*
* 2. add this redirect rule to the .htaccess, which enables to bypass the
* cache/ folder
*
* RewriteRule ^(.*)\.css$ /css.php?file=$1 [L]
*
* 3. create the css files in the theme/ folder, to get started with the
* current theme, just copy/paste the stylesheet contents from the
* Templates & Style section in the Admin.
*
* 4. when your theme is complete, simply copy/paste the contents of the css
* files back into the admin, export the theme and import it in your
* production forum.
*
*
* Suggestion:
*
* Create a git repo, a "theme" branch that contains this modified css.php
* file and your css edits. That way you can checkout into your master
* branch so that you can keep this modified css file and the theme files
* in a special branch, which will not be deployed in production.
*
*/
define("IN_MYBB", 1);
define("NO_ONLINE", 1);
define('THIS_SCRIPT', 'css.php');
require_once "./inc/init.php";
require_once MYBB_ROOT . $config['admin_dir'] . '/inc/functions_themes.php';
// chose a subdirectory where we edit stylesheets
define('FLAT_FILES_PATH', MYBB_ROOT.'theme/');
function exit_with_http_404($message = '')
{
header("HTTP/1.0 404 Not Found");
echo $message ? '(css.php): '.$message : '';
exit;
}
function _theme_file_real_path($baseName)
{
return FLAT_FILES_PATH.$baseName;
}
// return contents of a css file if present in our theme folder, otherwise false
function _theme_file_get_contents($baseName)
{
return file_get_contents(_theme_file_real_path($baseName));
}
$stylesheet_comment = '';
// if 'file' parameter is present, this is a redirect from .htaccess
if ($fileName = $mybb->get_input('file', MyBB::INPUT_STRING))
{
$fileName = MYBB_ROOT.$fileName.'.css';
$baseName = basename($fileName);
// use our flat file if it is found in our theme folder
if (false !== ($stylesheet = _theme_file_get_contents($baseName)))
{
// help us figure out what is happening
$stylesheet_comment = sprintf('/* (css.php) Using stylesheet "%s" found in FLAT_FILES_PATH */', $baseName);
}
// otherwise return the cache file as is
else if (false !== ($stylesheet = file_get_contents($fileName)))
{
$stylesheet_comment = sprintf('/* (css.php) Stylesheet not found in FLAT_FILES_PATH, using cached "%s" */', $fileName);
}
else
{
exit_with_http_404(sprintf('(css.php) Not found in FLAT_FILES_PATH, and no cached file for "%s"', $fileName));
}
}
// this part is when MyBB explicitly uses css.php with a theme stylesheet id
else if ($sid = $mybb->get_input('stylesheet', MyBB::INPUT_INT))
{
// grab the stylesheet name from its id, and its contents in case we bypass the flat file
$query = $db->simple_select("themestylesheets", "name, stylesheet", "sid=".$sid, $options);
if ($data = $db->fetch_array($query))
{
$baseName = $data['name'];
}
else
{
exit_with_http_404(sprintf("Invalid stylesheet id '%s'", $sid));
}
// should we use the flat file, or the contents from the database?
if (false !== ($stylesheet = _theme_file_get_contents($baseName)))
{
$stylesheet_comment = sprintf('/* (css.php) Using "%s" */', _theme_file_real_path($baseName));
}
else
{
$options = array(
"limit" => 1
);
//$query = $db->simple_select("themestylesheets", "stylesheet", "sid=".$stylesheet, $options);
//$stylesheet = $db->fetch_field($query, "stylesheet");
$stylesheet = $data['stylesheet'];
$plugins->run_hooks("css_start");
if(!empty($mybb->settings['minifycss']))
{
$stylesheet = minify_stylesheet($stylesheet);
}
$plugins->run_hooks("css_end");
$stylesheet_comment = sprintf('/* (css.php) Stylesheet not found in FLAT_FILES_PATH, using "%s" from database */', $baseName);
}
}
header("Content-type: text/css");
echo $stylesheet_comment."\n\n".$stylesheet;
@fabd
Copy link
Author

fabd commented Oct 7, 2015

As explained in the file comments, you'll need to add this rule to the .htaccess file:

RewriteRule ^(.*)\.css$   /css.php?file=$1 [L]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment