<commit_before><?php
if (!isset($_GET['f']) ||
		!isset($_GET['t'])) {
	header('HTTP/1.1 400 Bad Request');
	die();
}

require(__DIR__ . '/../constants.php');

/**
 * Check if a file can be served by ext.php. A valid file is under a
 * EXTENSIONS_PATH/extension_name/static/ directory.
 *
 * You should sanitize path by using the realpath() function.
 *
 * @param $path the path to the file we want to serve.
 * @return true if it can be served, false else.
 *
 */
function is_valid_path($path) {
	// It must be under the extension path.
	$real_ext_path = realpath(EXTENSIONS_PATH);

	//Windows compatibility
	$real_ext_path = str_replace('\\', '/', $real_ext_path);
	$path = str_replace('\\', '/', $path);

	$in_ext_path = (substr($path, 0, strlen($real_ext_path)) === $real_ext_path);
	if (!$in_ext_path) {
		return false;
	}

	// File to serve must be under a `ext_dir/static/` directory.
	$path_relative_to_ext = substr($path, strlen($real_ext_path) + 1);
	$path_splitted = explode('/', $path_relative_to_ext);
	if (count($path_splitted) < 3 || $path_splitted[1] !== 'static') {
		return false;
	}

	return true;
}

$file_name = urldecode($_GET['f']);
$file_type = $_GET['t'];

$absolute_filename = realpath(EXTENSIONS_PATH . '/' . $file_name);

if (!is_valid_path($absolute_filename)) {
	header('HTTP/1.1 400 Bad Request');
	die();
}

switch ($file_type) {
case 'css':
	header('Content-Type: text/css; charset=UTF-8');
	header('Content-Disposition: inline; filename="' . $file_name . '"');
	break;
case 'js':
	header('Content-Type: application/javascript; charset=UTF-8');
	header('Content-Disposition: inline; filename="' . $file_name . '"');
	break;
default:
	header('HTTP/1.1 400 Bad Request');
	die();
}

$mtime = @filemtime($absolute_filename);
if ($mtime === false) {
	header('HTTP/1.1 404 Not Found');
	die();
}

require(LIB_PATH . '/http-conditional.php');

if (!httpConditional($mtime, 604800, 2)) {
	readfile($absolute_filename);
}<commit_msg>Add support for images to the ext.php endpoint (#2465)<commit_after><?php
if (!isset($_GET['f']) ||
		!isset($_GET['t'])) {
	header('HTTP/1.1 400 Bad Request');
	die();
}

require(__DIR__ . '/../constants.php');

/**
 * Check if a file can be served by ext.php. A valid file is under a
 * EXTENSIONS_PATH/extension_name/static/ directory.
 *
 * You should sanitize path by using the realpath() function.
 *
 * @param $path the path to the file we want to serve.
 * @return true if it can be served, false else.
 *
 */
function is_valid_path($path) {
	// It must be under the extension path.
	$real_ext_path = realpath(EXTENSIONS_PATH);

	//Windows compatibility
	$real_ext_path = str_replace('\\', '/', $real_ext_path);
	$path = str_replace('\\', '/', $path);

	$in_ext_path = (substr($path, 0, strlen($real_ext_path)) === $real_ext_path);
	if (!$in_ext_path) {
		return false;
	}

	// File to serve must be under a `ext_dir/static/` directory.
	$path_relative_to_ext = substr($path, strlen($real_ext_path) + 1);
	$path_splitted = explode('/', $path_relative_to_ext);
	if (count($path_splitted) < 3 || $path_splitted[1] !== 'static') {
		return false;
	}

	return true;
}

$file_name = urldecode($_GET['f']);
$file_type = $_GET['t'];

$absolute_filename = realpath(EXTENSIONS_PATH . '/' . $file_name);

if (!is_valid_path($absolute_filename)) {
	header('HTTP/1.1 400 Bad Request');
	die();
}

switch ($file_type) {
case 'css':
	header('Content-Type: text/css; charset=UTF-8');
	header('Content-Disposition: inline; filename="' . $file_name . '"');
	break;
case 'js':
	header('Content-Type: application/javascript; charset=UTF-8');
	header('Content-Disposition: inline; filename="' . $file_name . '"');
	break;
case 'png':
	header('Content-Type: image/png');
	header('Content-Disposition: inline; filename="' . $file_name . '"');
	break;
case 'jpeg':
case 'jpg':
	header('Content-Type: image/jpeg');
	header('Content-Disposition: inline; filename="' . $file_name . '"');
	break;
case 'gif':
	header('Content-Type: image/gif');
	header('Content-Disposition: inline; filename="' . $file_name . '"');
	break;
case 'svg':
	header('Content-Type: image/svg+xml');
	header('Content-Disposition: inline; filename="' . $file_name . '"');
	break;
default:
	header('HTTP/1.1 400 Bad Request');
	die();
}

$mtime = @filemtime($absolute_filename);
if ($mtime === false) {
	header('HTTP/1.1 404 Not Found');
	die();
}

require(LIB_PATH . '/http-conditional.php');

if (!httpConditional($mtime, 604800, 2)) {
	readfile($absolute_filename);
}
{'fraction_non_alphanumeric': 0.19005130492973454, 'fraction_numerical': 0.015614543832255186, 'mean_word_length': 4.748717948717949, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5055077', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1584, 'n_words': 384}