File: /home/shaping-sar.ru/public_html/web_utils.php
<?php
function send404()
{
header("HTTP/1.1 404 Not Found");
include "404.php";
exit();
}
function serveFileDirectly($filePath)
{
if (file_exists($filePath)) {
if (ob_get_level()) {
ob_end_clean();
}
$mimeType = custom_mime_content_type($filePath);
header('Content-Description: File Transfer');
header('Content-Type: ' . $mimeType);
header('Content-Disposition: inline; filename="' . basename($filePath) . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
} else {
header("HTTP/1.1 404 Not Found");
echo "File not found.";
exit;
}
}
function custom_mime_content_type($filename)
{
$mime_types = array(
// текстовые форматы
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'csv' => 'text/csv',
'md' => 'text/markdown',
'rtf' => 'application/rtf',
// изображения
'png' => 'image/png',
'webp' => 'image/webp',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// аудио
'mp3' => 'audio/mpeg',
'wav' => 'audio/wav',
'ogg' => 'audio/ogg',
'm4a' => 'audio/mp4',
'flac' => 'audio/flac',
// видео
'mp4' => 'video/mp4',
'mov' => 'video/quicktime',
'qt' => 'video/quicktime',
'avi' => 'video/x-msvideo',
'wmv' => 'video/x-ms-wmv',
'flv' => 'video/x-flv',
'webm' => 'video/webm',
'mkv' => 'video/x-matroska',
// архивы и установочные пакеты
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'7z' => 'application/x-7z-compressed',
'tar' => 'application/x-tar',
'gz' => 'application/gzip',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// документы
'pdf' => 'application/pdf',
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'xls' => 'application/vnd.ms-excel',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'ppt' => 'application/vnd.ms-powerpoint',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
// Adobe
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// другие популярные
'swf' => 'application/x-shockwave-flash',
'log' => 'text/plain',
);
$ext = strtolower(array_pop(explode('.', $filename)));
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
} elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
} else {
return 'application/octet-stream';
}
}
function isPhpFile($path)
{
return is_file($path) && strtolower(pathinfo($path, PATHINFO_EXTENSION)) === 'php';
}
function log_request($logfile = 'request_log.txt', $timezone = 'Europe/Samara')
{
if (empty($_GET) && empty($_POST)) return;
date_default_timezone_set($timezone);
$arCookie = $_COOKIE;
$arCookie['PHPSESSID'] = null;
$log = [
'date' => date('Y-m-d H:i:s'),
'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
'method' => $_SERVER['REQUEST_METHOD'] ?? '',
'uri' => $_SERVER['REQUEST_URI'] ?? '',
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
'get' => $_GET,
'post' => $_POST,
'cookies' => $arCookie,
'referer' => $_SERVER['HTTP_REFERER'] ?? '',
'script_name' => $_SERVER['SCRIPT_NAME'],
// Если нужно хранить тело запроса (например, JSON)
'raw_input' => file_get_contents('php://input'),
];
$entry = json_encode($log, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n---\n";
file_put_contents($logfile, $entry, FILE_APPEND);
}