This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/core/sanitize_php.php

70 lines
1.9 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
2024-02-21 03:01:01 +00:00
require_once "core/urls.php";
/*
* A small number of PHP-sanity things (eg don't silently ignore errors) to
* be included right at the very start of index.php and tests/bootstrap.php
*/
2024-01-15 15:08:22 +00:00
function die_nicely(string $title, string $body, int $code = 0): void
2020-07-06 16:46:20 +00:00
{
2024-02-21 03:01:01 +00:00
$data_href = get_base_href();
2020-07-06 16:46:20 +00:00
print("<!DOCTYPE html>
<html lang='en'>
<head>
<title>Shimmie</title>
2024-02-21 03:01:01 +00:00
<link rel='shortcut icon' href='$data_href/ext/static_files/static/favicon.ico'>
<link rel='stylesheet' href='$data_href/ext/static_files/style.css' type='text/css'>
<link rel='stylesheet' href='$data_href/ext/static_files/installer.css' type='text/css'>
2020-07-06 16:46:20 +00:00
</head>
<body>
2024-02-21 03:01:01 +00:00
<div id='installer'>
2020-07-06 16:46:20 +00:00
<h1>Shimmie</h1>
<h3>$title</h3>
2024-02-21 03:01:01 +00:00
<div class='container'>
2020-07-06 16:46:20 +00:00
$body
</div>
</div>
</body>
</html>");
if ($code != 0) {
http_response_code(500);
}
exit($code);
}
2022-10-28 00:45:35 +00:00
$min_php = "8.1";
if (version_compare(phpversion(), $min_php, ">=") === false) {
2020-07-07 16:07:23 +00:00
die_nicely("Not Supported", "
2020-07-06 16:46:20 +00:00
Shimmie does not support versions of PHP lower than $min_php
(PHP reports that it is version ".phpversion().").
", 1);
}
# ini_set('zend.assertions', '1'); // generate assertions
ini_set('assert.exception', '1'); // throw exceptions when failed
set_error_handler(function ($errNo, $errStr) {
// Should we turn ALL notices into errors? PHP allows a lot of
// terrible things to happen by default...
if (str_starts_with($errStr, 'Use of undefined constant ')) {
2023-01-11 11:15:26 +00:00
throw new \Exception("PHP Error#$errNo: $errStr");
} else {
return false;
}
});
ob_start();
if (PHP_SAPI === 'cli' || PHP_SAPI == 'phpdbg') {
if (isset($_SERVER['REMOTE_ADDR'])) {
die("CLI with remote addr? Confused, not taking the risk.");
}
$_SERVER['REMOTE_ADDR'] = "0.0.0.0";
2020-10-24 12:46:49 +00:00
$_SERVER['HTTP_HOST'] = "cli-command";
}