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