-
-
Save Zodiase/65d58030deb9ad772371 to your computer and use it in GitHub Desktop.
CSS Porter
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 | |
/** | |
* CSS Porter | |
* this file loads and prints css files requested | |
*/ | |
/*= app name for printing =*/ | |
$appName = 'CSS Porter ver 1.0'; | |
/*= relative path to the folder containing the css files =*/ | |
$cssFolder = './../css'; | |
/*= preset debug mode =*/ | |
$debugMode = ($_GET['debug'] === 'yes') ? true : false; | |
/*= output buffer start =*/ | |
ob_start(); | |
/*= set output header =*/ | |
header('Content-Type: text/css'); | |
header('Content-Disposition: inline; filename="container.css"'); | |
header('Cache-Control: no-cache'); | |
header('Pragma: no-cache'); | |
/*= print title =*/ | |
if ($debugMode) | |
printf("/* %s */\n", $appName); | |
/*= fetch raw request =*/ | |
/*= if no css file list request detected, halt =*/ | |
if (!array_key_exists('l', $_GET)) | |
die('/* no request */'); | |
$rawRequestString = $_GET['l']; | |
/*= force direct to the folder containing this php file =*/ | |
chdir(dirname(__FILE__)); | |
/*= css file folder check =*/ | |
if (!file_exists($cssFolder) || !is_dir($cssFolder)) | |
die('/* css directory not found */'); | |
/*= force direct to directory containing css files =*/ | |
chdir($cssFolder); | |
/*= print raw requests =*/ | |
if ($debugMode) | |
printf("/* raw request: %s */\n", $rawRequestString); | |
/*= turn requested file list (seperated by ',') into an array =*/ | |
$requestList = explode(',', $rawRequestString); | |
/*= traversal all requested items and setup a hash table for files to be loaded =*/ | |
/*= rawFiles is an array of css file contents with their filename as keys =*/ | |
$rawFiles = array(); | |
/*= printList is an array of css filenames in which order css file contents should be printed =*/ | |
$printList = array(); | |
if ($debugMode) | |
print("/* requested css files: */\n"); | |
foreach ($requestList as $value) { | |
$requestItem = trim(basename($value)); | |
if ($requestItem === '') continue; | |
if ($debugMode) | |
printf("/* %s, */\n", $requestItem); | |
if (!array_key_exists($requestItem, $rawFiles)) | |
$printList[] = $requestItem; | |
$rawFiles[$requestItem] = false; | |
} | |
if ($debugMode) | |
print("/* loading requested files: */\n"); | |
foreach ($rawFiles as $key => $value) { | |
$standardFileName = "{$key}.css"; | |
$preferedFileName = "{$key}.min.css"; | |
if (file_exists($preferedFileName) && is_file($preferedFileName)) { | |
$rawFiles[$key] = cssCompress(file_get_contents($preferedFileName), $compression); | |
if ($debugMode) | |
printf("/* ./%s (md5: %s) */\n", $preferedFileName, md5($rawFiles[$key])); | |
} else if (file_exists($standardFileName) && is_file($standardFileName)) { | |
$rawFiles[$key] = cssCompress(file_get_contents($standardFileName), $compression); | |
if ($debugMode) | |
printf("/* ./%s (md5: %s) */\n", $standardFileName, md5($rawFiles[$key])); | |
} else { | |
if ($debugMode) | |
printf("/* ./%s %s */\n", $standardFileName, 'not found'); | |
} | |
} | |
for ($i = 0; $i < count($printList); ++$i) { | |
$key = $printList[$i]; | |
$content = $rawFiles[$key]; | |
printf("\n/* %s */\n%s\n", $key, $content); | |
} | |
ob_end_flush(); | |
function cssCompress($cssContent) | |
{ | |
// remove comment blocks | |
$cssContent = preg_replace('/\/\*.*\*\//uUs', '', $cssContent); | |
// remove white spaces | |
$cssContent = preg_replace('/\s*(\n|\r)\s*/um', '', $cssContent); | |
return $cssContent; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment