Test Failed
Push — master ( bec48f...521530 )
by Justin
40:57 queued 18:50
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
/**
21
 * Project: JuKuCMS
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
//start session
34
session_start();
35
36
require("system/core/init.php");
37
38
//reset OpCache in debug mode
39
if (CLEAR_OP_CACHE) {
40
	//http://php.net/manual/en/function.opcache-reset.php
41
	//http://php.net/manual/en/function.opcache-invalidate.php
42
	opcache_reset();
43
}
44
45
ob_start("ob_gzhandler");
46
47
//set javascript header
48
header("Content-Type: text/javascript");
49
50
Events::throwEvent("init_js");
51
52
$style = "";
53
54
//get required style
55
if (!isset($_REQUEST['style']) || empty($_REQUEST['style'])) {
56
	echo "No style set. Use js.php?style=your-style-name&amp;position=your-position .";
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 js builder
99
$js_builder = new JSBuilder();
100
101
//get style cache path
102
$js_cache_path = $js_builder->getCachePath($style, $media, $position);
103
104
//generate js file, if neccessary
105
if (!$js_builder->existsCache($style, $media, $position)) {
106
	$js_builder->generateJS($style, $media, $position);
107
}
108
109
$cache_strategy = Settings::get("js_cache_strategy", "expires_header");//etag / expires
110
111
//intelligent caching
112
if (file_exists($js_cache_path)) {
113
	if ($cache_strategy === "expires_header") {
114
		//https://www.electrictoolbox.com/php-caching-headers/
115
116
		//set expires header, so browser can cache this js file
117
		$seconds_to_cache = (int) Settings::get("js_cache_expires_header_ttl", "31536000");
118
		$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
119
		header("Expires: " . $ts);
120
		header("Pragma: cache");
121
		header("Cache-Control: max-age=" . $seconds_to_cache);
122
	} else if ($cache_strategy === "etag_header") {
123
		//get the last-modified-date of this very file
124
		$lastModified=filemtime($js_cache_path);
125
126
		//get a unique hash of this file (etag)
127
		$etagFile = md5_file($js_cache_path);
128
129
		//get the HTTP_IF_MODIFIED_SINCE header if set
130
		$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
131
132
		//get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
133
		$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
134
135
		//set last-modified header
136
		header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
137
138
		//set etag-header
139
		header("Etag: $etagFile");
140
141
		//make sure caching is turned on
142
		header('Cache-Control: public');
143
144
		//check if page has changed. If not, send 304 and exit
145
		if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified || $etagHeader == $etagFile) {
146
			header("HTTP/1.1 304 Not Modified");
147
			exit;
148
		}
149
	} else if ($cache_strategy === "none") {
150
		//dont set browser cache header
151
	} else {
152
		echo "Unknown js_cache_strategy '" . $cache_strategy . "'!";
153
		exit;
154
	}
155
}
156
157
//load js builder
158
$js_builder->load($style, $media, $position);
159
160
//get css output
161
echo $js_builder->getBuffer();
162
163
//flush gzip cache
164
ob_end_flush();
165
166
Events::throwEvent("after_show_js");
167
168
?>
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...
169