Test Failed
Push — master ( 3d7f7b...79ebf7 )
by Justin
04:03
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
//start session
35
session_start();
36
37
require("system/core/init.php");
38
39
//reset OpCache in debug mode
40
if (CLEAR_OP_CACHE) {
41
	//http://php.net/manual/en/function.opcache-reset.php
42
	//http://php.net/manual/en/function.opcache-invalidate.php
43
	opcache_reset();
44
}
45
46
ob_start("ob_gzhandler");
47
48
//set css header
49
header("Content-Type: text/css");
50
51
Events::throwEvent("http_header");
52
53
Events::throwEvent("init_css");
54
55
$style = "";
56
57
//get required style
58
if (!isset($_REQUEST['style']) || empty($_REQUEST['style'])) {
59
	echo "No style set. Use css.php?style=your-style-name .";
60
	exit;
61
}
62
63
$style = $_REQUEST['style'];
64
$media = "ALL";
65
66
//check, if stlye name is valide
67
$validator = new Validator_Filename();
68
69
if (!$validator->isValide($style)) {
70
	echo "Invalide style name '" . htmlentities($style) . "' (only allowed characters: a-z, A-Z and 0-9)!";
71
	exit;
72
}
73
74
$style = $validator->validate($style);
75
76
//check, if style exists
77
if (!file_exists(STYLE_PATH . $style)) {
78
	echo "Style '" . $style . "' doesnt exists!";
79
	exit;
80
}
81
82
if (isset($_REQUEST['media']) && !empty($_REQUEST['media'])) {
83
	if (!$validator->isValide($_REQUEST['media'])) {
84
		echo "Invalide media '" . htmlentities($_REQUEST['media']) . "'!";
85
		exit;
86
	}
87
88
	$media = $validator->validate($_REQUEST['media']);
89
}
90
91
//create css builder
92
$css_builder = new CSSBuilder();
93
94
//get style cache path
95
$css_cache_path = $css_builder->getCachePath($style, $media);
96
97
//generate css file, if neccessary
98
if (!$css_builder->existsCache($style, $media)) {
99
	$css_builder->generateCSS($style, $media);
100
}
101
102
//http://blog.franky.ws/php-und-das-caching-via-http-header-etag/
103
104
$cache_strategy = Settings::get("css_cache_strategy", "expires_header");//etag / expires
105
106
//intelligent caching
107
if (file_exists($css_cache_path)) {
108
	if ($cache_strategy === "expires_header") {
109
		//https://www.electrictoolbox.com/php-caching-headers/
110
111
		//set expires header, so browser can cache this css file
112
		$seconds_to_cache = (int) Settings::get("css_cache_expires_header_ttl", "31536000");
113
		$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
114
		header("Expires: " . $ts);
115
		header("Pragma: cache");
116
		header("Cache-Control: max-age=" . $seconds_to_cache);
117
	} else if ($cache_strategy === "etag_header") {
118
		//get the last-modified-date of this very file
119
		$lastModified=filemtime($css_cache_path);
120
121
		//get a unique hash of this file (etag)
122
		$etagFile = md5_file($css_cache_path);
123
124
		//get the HTTP_IF_MODIFIED_SINCE header if set
125
		$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
126
127
		//get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
128
		$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
129
130
		//set last-modified header
131
		header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
132
133
		//set etag-header
134
		header("Etag: $etagFile");
135
136
		//make sure caching is turned on
137
		header('Cache-Control: public');
138
139
		//check if page has changed. If not, send 304 and exit
140
		if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified || $etagHeader == $etagFile) {
141
			header("HTTP/1.1 304 Not Modified");
142
			exit;
143
		}
144
	} else if ($cache_strategy === "none") {
145
		//dont set browser cache header
146
	} else {
147
		echo "Unknown css_cache_strategy '" . $cache_strategy . "'!";
148
		exit;
149
	}
150
}
151
152
//load css builder
153
$css_builder->load($style, $media);
154
155
//get css output
156
echo $css_builder->getBuffer();
157
158
//flush gzip cache
159
ob_end_flush();
160
161
Events::throwEvent("after_show_css");
162
163
?>
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...
164