This repository was archived by the owner on Aug 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.php
More file actions
171 lines (171 loc) · 5.37 KB
/
Server.php
File metadata and controls
171 lines (171 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace RedCat\Stylize;
/*
* Css Preprocessor using Scss syntax ( SASS 3.x ) ported to PHP with added supports: php imbrication, php mixin, mixin autoload, extend autoload, font autoload - derived from leafo-scssphp
*
* @package Stylize
* @version 2
* @link http://github.com/redcatphp/Stylize/
* @author Jo Surikat <jo@surikat.pro>
* @website http://redcatphp.com
*/
class Server{
protected $cacheDir = '.tmp/stylize/';
protected $enableCache;
protected $compiler;
protected $extraImports = [];
protected $importPaths = [];
function __construct($from=null,$cache=true){
$this->setCache($cache);
$this->compiler = new Compiler();
if(isset($from))
$this->setImportPaths($from);
}
function setCache($enable){
$this->enableCache = (bool)$enable;
}
function serveFrom($file,$from=null,$extraImports=null,$salt = ''){
if(isset($from))
$this->setImportPaths($from);
if($extraImports)
$this->addExtraImports($extraImports);
$this->serve($file,$salt);
}
function addExtraImports($extraImports){
foreach((array)$extraImports as $ei){
if(!in_array($ei,$this->extraImports))
$this->extraImports[] = $ei;
}
}
function serve($in,$salt = '') {
if(strpos($in, '..')!==false)
return;
set_time_limit(0);
foreach($this->importPaths as $dir){
$input = self::joinPath($dir, $in);
if(is_file($input)&&is_readable($input)){
$output = $this->cacheName($salt . $input);
header('Content-Type:text/css; charset=utf-8');
if (!$this->enableCache||$this->needsCompile($input, $output)) {
try {
if($this->enableCache)
$this->cachingHeader($output);
echo $this->compile($input, $output, $in);
}
catch (\Exception $e) {
header('HTTP/1.1 500 Internal Server Error');
echo 'Parse error: ' . $e->getMessage() . "\n";
if($e=error_get_last())
printf("%s in eval php: %s in %s:%s",self::errorType($e['type']),$e['message'],$e['file'],$e['line']);
}
} else {
header('X-SCSS-Cache: true');
$this->cachingHeader($output);
echo file_get_contents($output);
}
return;
}
}
header('HTTP/1.0 404 Not Found');
echo "/* CSS NOT FOUND */\n";
}
function cacheName($fname){
return self::joinPath($this->cacheDir, md5($fname) . '.css');
}
function importsCacheName($out) {
return $out . '.imports';
}
function needsCompile($in, $out) {
if (!is_file($out)) return true;
$mtime = filemtime($out);
if (filemtime($in) > $mtime) return true;
$icache = $this->importsCacheName($out);
if (is_readable($icache)) {
$imports = unserialize(file_get_contents($icache));
foreach ($imports as $import=>$mtime)
if (($mt=@filemtime($import)) > $mtime||!$mt) return true;
}
return false;
}
function compile($in, $out,$input=null) {
$start = microtime(true);
if($input){
$compile = '';
foreach($this->extraImports as $ei){
$compile .= '@import "'.$ei.'";';
}
$compile .= '@import "'.$input.'";';
$css = $this->compiler->compile($compile);
}
else{
$css = $this->compiler->compile(file_get_contents($in), $in);
}
$elapsed = round((microtime(true) - $start), 4);
$v = Compiler::SCSS_VERSION;
$v2 = Compiler::STYLIZE_VERSION;
$t = @date('r');
$css = "/* compiled by Stylize $v2 ( based on Leafo/ScssPhp $v - Sass 3.x implementation in PHP ) on $t (${elapsed}s) */\n\n" . $css;
if(!is_dir($this->cacheDir))
@mkdir($this->cacheDir,0777,true);
file_put_contents($out, $css, LOCK_EX);
file_put_contents($this->importsCacheName($out),
serialize($this->compiler->getParsedFiles()),LOCK_EX);
return $css;
}
function cachingHeader($output){
if(!is_file($output))
return;
$this->fileCache($output);
}
function fileCache($output){
$mtime = filemtime($output);
$etag = $this->fileEtag($output);
header('Last-Modified: '.gmdate('D, d M Y H:i:s',$mtime).' GMT');
header('Etag: '.$etag);
if(!$this->isModified($mtime,$etag)){
header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified');
exit;
}
}
function fileEtag($file){
$s = stat($file);
return sprintf('%x-%s', $s['size'], base_convert(str_pad($s['mtime'], 16, "0"),10,16));
}
function isModified($mtime,$etag){
return !((isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])&&@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])>=$mtime)
||(isset($_SERVER['HTTP_IF_NONE_MATCH'])&&$_SERVER['HTTP_IF_NONE_MATCH'] == $etag));
}
function addImportPath($dir){
$this->compiler->addImportPath($dir);
}
function setImportPaths($dir){
$this->importPaths = (array)$dir;
$this->compiler->setImportPaths($dir);
}
function setCacheDir($dir){
$this->cacheDir = $dir;
}
static function joinPath($left, $right) {
return rtrim($left, '/\\') . DIRECTORY_SEPARATOR . ltrim($right, '/\\');
}
static function errorType($code){
static $errorType = [
E_ERROR => 'error',
E_WARNING => 'warning',
E_PARSE => 'parsing error',
E_NOTICE => 'notice',
E_CORE_ERROR => 'core error',
E_CORE_WARNING => 'core warning',
E_COMPILE_ERROR => 'compile error',
E_COMPILE_WARNING => 'compile warning',
E_USER_ERROR => 'user error',
E_USER_WARNING => 'user warning',
E_USER_NOTICE => 'user notice',
E_STRICT => 'strict standard error',
E_RECOVERABLE_ERROR => 'recoverable error',
E_DEPRECATED => 'deprecated error',
E_USER_DEPRECATED => 'user deprecated error',
];
return isset($errorType[$code])?$errorType[$code]:null;
}
}