Issues (762)

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: RocketCMS
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
$position = "header";
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
if (isset($_REQUEST['position']) && !empty($_REQUEST['position'])) {
90
	if (!$validator->isValide($_REQUEST['position'])) {
91
		echo "Invalide position '" . htmlentities($_REQUEST['position']) . "'!";
92
		exit;
93
	}
94
95
	$position = $validator->validate($_REQUEST['position']);
96
}
97
98
//create css builder
99
$css_builder = new CSSBuilder();
100
101
//get style cache path
102
$css_cache_path = $css_builder->getCachePath($style, $media, $position);
103
104
//generate css file, if neccessary
105
if (!$css_builder->existsCache($style, $media, $position)) {
106
	$css_builder->generateCSS($style, $media, $position);
107
}
108
109
//http://blog.franky.ws/php-und-das-caching-via-http-header-etag/
110
111
$cache_strategy = Settings::get("css_cache_strategy", "expires_header");//etag / expires
112
113
//intelligent caching
114
if (file_exists($css_cache_path)) {
115
	if ($cache_strategy === "expires_header") {
116
		//https://www.electrictoolbox.com/php-caching-headers/
117
118
		//set expires header, so browser can cache this css file
119
		$seconds_to_cache = (int) Settings::get("css_cache_expires_header_ttl", "31536000");
120
		$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
121
		header("Expires: " . $ts);
122
		header("Pragma: cache");
123
		header("Cache-Control: max-age=" . $seconds_to_cache);
124
	} else if ($cache_strategy === "etag_header") {
125
		//get the last-modified-date of this very file
126
		$lastModified=filemtime($css_cache_path);
127
128
		//get a unique hash of this file (etag)
129
		$etagFile = md5_file($css_cache_path);
130
131
		//get the HTTP_IF_MODIFIED_SINCE header if set
132
		$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
133
134
		//get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
135
		$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
136
137
		//set last-modified header
138
		header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
139
140
		//set etag-header
141
		header("Etag: $etagFile");
142
143
		//make sure caching is turned on
144
		header('Cache-Control: public');
145
146
		//check if page has changed. If not, send 304 and exit
147
		if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified || $etagHeader == $etagFile) {
148
			header("HTTP/1.1 304 Not Modified");
149
			exit;
150
		}
151
	} else if ($cache_strategy === "none") {
152
		//dont set browser cache header
153
	} else {
154
		echo "Unknown css_cache_strategy '" . $cache_strategy . "'!";
155
		exit;
156
	}
157
}
158
159
//load css builder
160
$css_builder->load($style, $media, $position);
161
162
//get css output
163
echo $css_builder->getBuffer();
164
165
//flush gzip cache
166
ob_end_flush();
167
168
//send logs to server
169
if (LOGGING_ENABLED) {
170
	Logger::send();
171
}
172
173
Events::throwEvent("after_show_css");
174
175
?>
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...
176