Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficiency improvements to serve_module_asset() #205

Merged
merged 4 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions engine/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ private function serve_module_asset(): void {
try {
$asset_path = $this->sanitize_file_path($asset_path, '../modules/');

if (file_exists($asset_path)) {
if (is_file($asset_path)) {
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($asset_path)) {
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($asset_path)).' GMT', true, 304);
die;
}

$content_type = mime_content_type($asset_path);

if ($content_type === 'text/plain' || $content_type === 'text/html') {
Expand All @@ -133,14 +139,14 @@ private function serve_module_asset(): void {

// Make sure it's not a PHP file or api.json
if (strpos($content_type, 'php') !== false || $file_name === 'api.json') {
http_response_code(422);
http_response_code(403);
die();
}

header('Content-type: ' . $content_type);
$contents = file_get_contents($asset_path);
echo $contents;
die();
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($asset_path)) . ' GMT');
readfile($asset_path);
die;
}
} catch (Exception $e) {
die($e->getMessage());
Expand Down
4 changes: 2 additions & 2 deletions engine/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(?string $current_module = null) {
$this->port = (defined('PORT') ? PORT : '3306');
$this->current_module = $current_module;

$dsn = 'mysql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname;
$dsn = 'mysql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname . ';charset=' . $this->charset;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
Expand Down Expand Up @@ -1002,4 +1002,4 @@ private function add_limit_offset(string $sql, ?int $limit, ?int $offset): strin
return $sql;
}

}
}