1<?php 2/** 3 * Mikio CSS/LESS Engine 4 * 5 * @link http://dokuwiki.org/template:mikio 6 * @author James Collins <james.collins@outlook.com.au> 7 * @license GPLv2 (http://www.gnu.org/licenses/gpl-2.0.html) 8 */ 9 10require(dirname(__FILE__) . '/inc/polyfill-ctype.php'); 11 12if(!class_exists('lessc')) { 13 require(dirname(__FILE__) . '/inc/stemmechanics/lesserphp/lessc.inc.php'); 14} 15 16if(!function_exists('getallheaders')) { 17 function getallheaders() { 18 $headers = []; 19 foreach($_SERVER as $name => $value) { 20 if(substr($name, 0, 5) == 'HTTP_') { 21 $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 22 } 23 } 24 return $headers; 25 } 26} 27 28if(!function_exists('platformSlashes')) { 29 function platformSlashes($path) { 30 return str_replace('/', DIRECTORY_SEPARATOR, $path); 31 } 32} 33 34try { 35 if(isset($_GET['css'])) { 36 $failed = false; 37 $cssFileList = platformSlashes(explode(',', $_GET['css'])); 38 $baseDir = platformSlashes(dirname(__FILE__) . '/'); 39 $css = ''; 40 41 foreach($cssFileList as $cssFileItem) { 42 $cssFile = platformSlashes(realpath($baseDir . $cssFileItem)); 43 44 if(strpos($cssFile, $baseDir) === 0 && file_exists($cssFile)) { 45 $css .= file_get_contents($cssFile); 46 } else { 47 $failed = true; 48 } 49 } 50 51 if(!$failed) { 52 $rawVars = Array(); 53 54 header('Content-Type: text/css; charset=utf-8'); 55 56 $less = new lessc(); 57 $less->setPreserveComments(false); 58 59 $vars = Array(); 60 if(isset($rawVars['replacements'])) { 61 foreach($rawVars['replacements'] as $key=>$val) { 62 if(substr($key, 0, 2) == '__' && substr($key, -2) == '__') { 63 $vars['ini_' . substr($key, 2, -2)] = $val; 64 } 65 } 66 } 67 68 if(count($vars) > 0) { 69 $less->setVariables($vars); 70 } 71 72 $css = $less->compile($css); 73 echo $css; 74 } else { 75 header('HTTP/1.1 404 Not Found'); 76 echo "The requested file could not be found"; 77 } 78 } else { 79 header('HTTP/1.1 404 Not Found'); 80 echo "The requested file could not be found"; 81 } 82} 83catch(Exception $e) { 84 header('Content-Type: text/css; charset=utf-8'); 85 echo ".error_in_mikio_plugin_with_less_file { 86/** 87" . $e . " 88**/ 89}"; 90} 91