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
/**
21
 * Project: RocketCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 22.03.2018
25
 * Time: 17:33
26
 */
27
28
//define root path
29
define('ROOT_PATH', dirname(__FILE__) . "/");
30
31
error_reporting(E_ALL);
32
33
require("system/core/init.php");
34
35
//reset OpCache in debug mode
36
if (CLEAR_OP_CACHE) {
37
	//http://php.net/manual/en/function.opcache-reset.php
38
	//http://php.net/manual/en/function.opcache-invalidate.php
39
	opcache_reset();
40
}
41
42
ob_start("ob_gzhandler");
43
44
//set javascript header
45
header("Content-Type: text/javascript");
46
47
Events::throwEvent("http_header");
48
49
Events::throwEvent("init_js");
50
51
$style = "";
52
53
//get required style
54
if (!isset($_REQUEST['style']) || empty($_REQUEST['style'])) {
55
	echo "No style set. Use js.php?style=your-style-name&amp;position=your-position .";
56
	exit;
57
}
58
59
$style = $_REQUEST['style'];
60
$media = "ALL";
61
$position = "header";
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
if (isset($_REQUEST['position']) && !empty($_REQUEST['position'])) {
89
	if (!$validator->isValide($_REQUEST['position'])) {
90
		echo "Invalide position '" . htmlentities($_REQUEST['position']) . "'!";
91
		exit;
92
	}
93
94
	$position = $validator->validate($_REQUEST['position']);
95
}
96
97
//create js builder
98
$js_builder = new JSBuilder();
99
100
//get style cache path
101
$js_cache_path = $js_builder->getCachePath($style, $media, $position);
102
103
//generate js file, if neccessary
104
if (!$js_builder->existsCache($style, $media, $position)) {
105
	$js_builder->generateJS($style, $media, $position);
106
}
107
108
$cache_strategy = Settings::get("js_cache_strategy", "expires_header");//etag / expires
109
110
//intelligent caching
111
if (file_exists($js_cache_path)) {
112
	if ($cache_strategy === "expires_header") {
113
		//https://www.electrictoolbox.com/php-caching-headers/
114
115
		//set expires header, so browser can cache this js file
116
		$seconds_to_cache = (int) Settings::get("js_cache_expires_header_ttl", "31536000");
117
		$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
118
		header("Expires: " . $ts);
119
		header("Pragma: cache");
120
		header("Cache-Control: max-age=" . $seconds_to_cache);
121
	} else if ($cache_strategy === "etag_header") {
122
		//get the last-modified-date of this very file
123
		$lastModified=filemtime($js_cache_path);
124
125
		//get a unique hash of this file (etag)
126
		$etagFile = md5_file($js_cache_path);
127
128
		//get the HTTP_IF_MODIFIED_SINCE header if set
129
		$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
130
131
		//get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
132
		$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
133
134
		//set last-modified header
135
		header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
136
137
		//set etag-header
138
		header("Etag: $etagFile");
139
140
		//make sure caching is turned on
141
		header('Cache-Control: public');
142
143
		//check if page has changed. If not, send 304 and exit
144
		if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified || $etagHeader == $etagFile) {
145
			header("HTTP/1.1 304 Not Modified");
146
			exit;
147
		}
148
	} else if ($cache_strategy === "none") {
149
		//dont set browser cache header
150
	} else {
151
		echo "Unknown js_cache_strategy '" . $cache_strategy . "'!";
152
		exit;
153
	}
154
}
155
156
//load js builder
157
$js_builder->load($style, $media, $position);
158
159
//get css output
160
echo $js_builder->getBuffer();
161
162
//flush gzip cache
163
ob_end_flush();
164
165
//send logs to server
166
if (LOGGING_ENABLED) {
167
	Logger::send();
168
}
169
170
Events::throwEvent("after_show_js");
171
172
?>
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...
173