Test Failed
Push — master ( 62e539...2c88a2 )
by Justin
41:32 queued 38:00
created
Severity
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
/**
20
 * Shows CSS
21
 *
22
 * Project: JuKuCMS
23
 * License: Apache 2.0 license
24
 * User: Justin
25
 * Date: 22.03.2018
26
 * Time: 13:25
27
 */
28
29
//define root path
30
define('ROOT_PATH', dirname(__FILE__) . "/");
31
32
error_reporting(E_ALL);
33
34
require("system/core/init.php");
35
36
//reset OpCache in debug mode
37
if (CLEAR_OP_CACHE) {
38
	//http://php.net/manual/en/function.opcache-reset.php
39
	//http://php.net/manual/en/function.opcache-invalidate.php
40
	opcache_reset();
41
}
42
43
ob_start("ob_gzhandler");
44
45
//set css header
46
header("Content-Type: text/css");
47
48
Events::throwEvent("http_header");
49
50
Events::throwEvent("init_css");
51
52
$style = "";
53
54
//get required style
55
if (!isset($_REQUEST['style']) || empty($_REQUEST['style'])) {
56
	echo "No style set. Use css.php?style=your-style-name .";
57
	exit;
58
}
59
60
$style = $_REQUEST['style'];
61
$media = "ALL";
62
63
//check, if stlye name is valide
64
$validator = new Validator_Filename();
65
66
if (!$validator->isValide($style)) {
67
	echo "Invalide style name '" . htmlentities($style) . "' (only allowed characters: a-z, A-Z and 0-9)!";
68
	exit;
69
}
70
71
$style = $validator->validate($style);
72
73
//check, if style exists
74
if (!file_exists(STYLE_PATH . $style)) {
75
	echo "Style '" . $style . "' doesnt exists!";
76
	exit;
77
}
78
79
if (isset($_REQUEST['media']) && !empty($_REQUEST['media'])) {
80
	if (!$validator->isValide($_REQUEST['media'])) {
81
		echo "Invalide media '" . htmlentities($_REQUEST['media']) . "'!";
82
		exit;
83
	}
84
85
	$media = $validator->validate($_REQUEST['media']);
86
}
87
88
//create css builder
89
$css_builder = new CSSBuilder();
90
91
//get style cache path
92
$css_cache_path = $css_builder->getCachePath($style, $media);
93
94
//generate css file, if neccessary
95
if (!$css_builder->existsCache($style, $media)) {
96
	$css_builder->generateCSS($style, $media);
97
}
98
99
//http://blog.franky.ws/php-und-das-caching-via-http-header-etag/
100
101
$cache_strategy = Settings::get("css_cache_strategy", "expires_header");//etag / expires
102
103
//intelligent caching
104
if (file_exists($css_cache_path)) {
105
	if ($cache_strategy === "expires_header") {
106
		//https://www.electrictoolbox.com/php-caching-headers/
107
108
		//set expires header, so browser can cache this css file
109
		$seconds_to_cache = (int) Settings::get("css_cache_expires_header_ttl", "31536000");
110
		$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
111
		header("Expires: " . $ts);
112
		header("Pragma: cache");
113
		header("Cache-Control: max-age=" . $seconds_to_cache);
114
	} else if ($cache_strategy === "etag_header") {
115
		//get the last-modified-date of this very file
116
		$lastModified=filemtime($css_cache_path);
117
118
		//get a unique hash of this file (etag)
119
		$etagFile = md5_file($css_cache_path);
120
121
		//get the HTTP_IF_MODIFIED_SINCE header if set
122
		$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
123
124
		//get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
125
		$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
126
127
		//set last-modified header
128
		header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
129
130
		//set etag-header
131
		header("Etag: $etagFile");
132
133
		//make sure caching is turned on
134
		header('Cache-Control: public');
135
136
		//check if page has changed. If not, send 304 and exit
137
		if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified || $etagHeader == $etagFile) {
138
			header("HTTP/1.1 304 Not Modified");
139
			exit;
140
		}
141
	} else if ($cache_strategy === "none") {
142
		//dont set browser cache header
143
	} else {
144
		echo "Unknown css_cache_strategy '" . $cache_strategy . "'!";
145
		exit;
146
	}
147
}
148
149
//load css builder
150
$css_builder->load($style, $media);
151
152
//get css output
153
echo $css_builder->getBuffer();
154
155
//flush gzip cache
156
ob_end_flush();
157
158
Events::throwEvent("after_show_css");
159
160
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
161