-
-
Save fmtarif/6726924 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# LESS compiler | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} ^(.*)\.css | |
RewriteCond %1.less -f | |
RewriteRule ^(.*)\.css lessphp/less.php?f=$1.less |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Automatically compile LESS files | |
* | |
* Features: | |
* - Uses the system temp directory to ensure it is writable | |
* - Gzip compression | |
* - Compile only if not modified | |
* - Respect If-Modified-Since header | |
* - @todo Add caching for gzipped version | |
* - @todo Add caching header | |
* | |
* Installation: | |
* 1. Download lessphp and extract to $DOCUMENT_ROOT/lessphp | |
* @link http://leafo.net/lessphp/ | |
* | |
* 2. Add this script in the lessphp directory | |
* | |
* 3.1. Add RewriteRule in htaccess | |
* RewriteCond %{REQUEST_FILENAME} !-f | |
* RewriteCond %{REQUEST_FILENAME} ^(.*)\.css | |
* RewriteCond %1.less -f | |
* RewriteRule ^(.*)\.css lessphp/less.php?f=$1.less | |
* | |
* 3.2. If htaccess is not possible, replace CSS links from | |
* /css/style.less | |
* to | |
* /lessphp/less.php?f=css/style.less | |
* | |
* | |
* @link https://gist.github.com/4127137 | |
*/ | |
if (empty($_GET['f']) || !preg_match('/\.less$/', $_GET['f'])) { | |
header('HTTP/1.0 400 Bad Request'); | |
die(); | |
} | |
$cache_dir = sys_get_temp_dir() . '/lessphp/' . $_SERVER['SERVER_NAME']; // will store files in /tmp/lessphp/example.com/css/style.css | |
$doc_root = dirname(dirname(__FILE__)); | |
$less_file = "$doc_root/{$_GET['f']}"; | |
$css_file = $cache_dir . '/' . preg_replace('/\.less/', '.css', $_GET['f']); | |
$enable_gzip = !empty($_SERVER['HTTP_ACCEPT_ENCODING']) && in_array('gzip', explode(',', $_SERVER['HTTP_ACCEPT_ENCODING'])); | |
if (!is_file($less_file)) { | |
header('HTTP/1.0 404 Not Found'); | |
die(); | |
} | |
if (!is_dir(dirname($css_file))) { | |
mkdir(dirname($css_file), 0755, true); | |
} | |
require 'lessc.inc.php'; | |
$less = new lessc; | |
$less->setFormatter("compressed"); | |
try { | |
// Compiles only if $less_file mtime != $css_file mtime | |
$less->checkedCompile($less_file, $css_file); | |
} catch (Exception $e) { | |
header('HTTP/1.0 500 Internal Server Error'); | |
echo $e->getMessage(); | |
die(); | |
} | |
$fp = fopen($css_file, 'r'); | |
$stat = fstat($fp); | |
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $stat['mtime']) { | |
header('HTTP/1.0 304 Not Modified'); | |
} else { | |
header('Cache-Control: must-revalidate'); | |
header('Content-Type: text/css; charset=utf-8'); | |
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $stat['mtime']) . ' GMT'); | |
if ($enable_gzip) { | |
header('Content-Encoding: gzip'); | |
ob_start("ob_gzhandler"); | |
} | |
fpassthru($fp); | |
} | |
fclose($fp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment