2024-06-21 03:06:31 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
2024-06-23 10:58:47 +00:00
|
|
|
use Auth0\SDK\Exception\ConfigurationException;
|
|
|
|
use BitBadger\PDODocument\{Configuration, Definition, DocumentException, Mode};
|
2024-06-21 23:01:12 +00:00
|
|
|
use Dotenv\Dotenv;
|
2024-06-23 10:58:47 +00:00
|
|
|
use MyPrayerJournal\{Auth, Request, Table};
|
2024-06-21 23:01:12 +00:00
|
|
|
|
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
/** The version of this application */
|
|
|
|
const MPJ_VERSION = '4.0.0-alpha1';
|
|
|
|
|
|
|
|
(Dotenv::createImmutable(__DIR__))->load();
|
|
|
|
|
2024-06-22 16:30:26 +00:00
|
|
|
if (php_sapi_name() != 'cli') {
|
|
|
|
session_start();
|
|
|
|
|
2024-06-22 20:58:33 +00:00
|
|
|
$auth0_user = Auth::user();
|
|
|
|
if (!is_null($auth0_user)) {
|
|
|
|
$_SESSION['user_id'] = $auth0_user['sub'];
|
2024-06-22 16:30:26 +00:00
|
|
|
}
|
2024-06-23 00:23:37 +00:00
|
|
|
|
|
|
|
$_REQUEST['time_zone'] = $_SERVER['HTTP_X_TIME_ZONE'] ?? 'Etc/UTC';
|
2024-06-21 23:01:12 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 03:22:56 +00:00
|
|
|
Configuration::$pdoDSN = 'sqlite:' . implode(DIRECTORY_SEPARATOR, [__DIR__, 'data', 'mpj.db']);
|
|
|
|
Configuration::$mode = Mode::SQLite;
|
|
|
|
Definition::ensureTable(Table::REQUEST);
|
2024-06-23 10:58:47 +00:00
|
|
|
Definition::ensureFieldIndex(Table::REQUEST, 'user', ['userId']);
|
2024-06-22 03:22:56 +00:00
|
|
|
|
2024-06-22 20:58:33 +00:00
|
|
|
$_PATCH = [];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] ?? '' == 'PATCH') parse_str(file_get_contents('php://input'), $_PATCH);
|
|
|
|
|
2024-06-21 23:01:12 +00:00
|
|
|
/**
|
2024-06-22 20:58:33 +00:00
|
|
|
* Return a 404 and exit
|
2024-06-21 23:01:12 +00:00
|
|
|
*/
|
2024-06-22 20:58:33 +00:00
|
|
|
function not_found(): never
|
2024-06-21 23:01:12 +00:00
|
|
|
{
|
2024-06-22 20:58:33 +00:00
|
|
|
http_response_code(404);
|
|
|
|
die('Not found');
|
2024-06-21 23:01:12 +00:00
|
|
|
}
|
2024-06-23 00:23:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a 303 redirect ("see other" - redirects to a GET)
|
|
|
|
*
|
|
|
|
* @param string $url The URL to which the browser should be redirected
|
|
|
|
*/
|
|
|
|
function see_other(string $url): never
|
|
|
|
{
|
|
|
|
header('Location: ' . (str_starts_with($url, 'http') ? '/' : $url));
|
|
|
|
http_response_code(303);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a header that instructs the browser to close an open modal dialog
|
|
|
|
*
|
|
|
|
* @param string $name The name of the dialog to be closed
|
|
|
|
*/
|
|
|
|
function hide_modal(string $name): void
|
|
|
|
{
|
|
|
|
header("X-Hide-Modal: $name");
|
|
|
|
}
|
2024-06-23 10:58:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the user, HTTP method, and request
|
|
|
|
*
|
|
|
|
* @param string $id The ID of the prayer request to retrieve
|
|
|
|
* @param array $methods The allowable HTTP methods
|
|
|
|
* @param bool $redirect Whether to redirect not-logged-on users (optional, defaults to true)
|
|
|
|
* @return Request The request (failures will not return)
|
|
|
|
* @throws ConfigurationException If any is encountered
|
|
|
|
* @throws DocumentException If any is encountered
|
|
|
|
*/
|
|
|
|
function validate_request(string $id, array $methods, bool $redirect = true): Request
|
|
|
|
{
|
|
|
|
if (sizeof(array_filter($methods, fn($it) => $_SERVER['REQUEST_METHOD'] == $it)) == 0) not_found();
|
|
|
|
|
|
|
|
Auth::requireUser($redirect);
|
|
|
|
|
|
|
|
$req = Request::byId($id);
|
|
|
|
if (!$req) not_found();
|
|
|
|
|
|
|
|
return $req;
|
|
|
|
}
|