run PHP CodeSniffer, & update errors in .htaccess

This commit is contained in:
2025-07-01 05:29:43 -07:00
parent 1e13b6e945
commit 049742f346
16 changed files with 113 additions and 72 deletions

View File

@ -1,5 +1,5 @@
ErrorDocument 404 /assets/errors/404.php
ErrorDocument 403 /assets/errors/403.php
ErrorDocument 404 /inc/errors/404.php
ErrorDocument 403 /inc/errors/403.php
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_METHOD} !^POST$

View File

@ -6,4 +6,4 @@ require_once $_SERVER['DOCUMENT_ROOT'] . '/config.php';
$year = $detectedYear ?? date('Y');
$birthdate = new DateTimeImmutable($config['birthdate']);
$today = new DateTimeImmutable();
$age = $today->diff($birthdate)->y;
$age = $today->diff($birthdate)->y;

View File

@ -3,4 +3,4 @@ if (!defined('Such_a_good_girl')) {
die("Direct access not allowed.");
}
?>
<p class="copyright">&copy;<?php echo date("Y"); ?> <?php echo $config['copyright'] ?><br>Designed by: <a class="white-link" href="https://html5up.net">HTML5 UP</a></p>
<p class="copyright">&copy;<?php echo date("Y"); ?> <?php echo $config['copyright'] ?><br>Designed by: <a class="white-link" href="https://html5up.net">HTML5 UP</a></p>

View File

@ -30,9 +30,9 @@ if (preg_match('#^/archives/(\d{4})/#', $requestUri, $matches)) {
<div class="inner split">
<div>
<section>
<h2><?= htmlspecialchars($config['title']) ?>.</h2>
<h2><?php echo htmlspecialchars($config['title']) ?>.</h2>
<p>
She is a Chihuahua mix, and she is <?= htmlspecialchars($age) ?> years old.
She is a Chihuahua mix, and she is <?php echo htmlspecialchars($age) ?> years old.
We adopted her in 2019 from
<a class="white-link" href="https://www.newlifeasherhouse.org/" rel="noopener">Family Dogs New Life Shelter (Now, New Life Asher House)</a>.
</p>
@ -42,9 +42,9 @@ if (preg_match('#^/archives/(\d{4})/#', $requestUri, $matches)) {
<a class="white-link" href="https://git.oldgate.org/Sophia/PTD">Git Repository</a>
</p>
<h2>Contact</h2>
<p><a class="white-link" href="mailto:<?= htmlspecialchars($config['email']) ?>">Email</a></p>
<p><a class="white-link" href="mailto:<?php echo htmlspecialchars($config['email']) ?>">Email</a></p>
</section>
<?php include $_SERVER['DOCUMENT_ROOT'] . '/inc/base/copyright.php'; ?>
<?php require $_SERVER['DOCUMENT_ROOT'] . '/inc/base/copyright.php'; ?>
</div>
</div>
</footer>
@ -55,11 +55,11 @@ if (preg_match('#^/archives/(\d{4})/#', $requestUri, $matches)) {
<h2>Archives</h2>
<?php foreach ($years as $year): ?>
<?php $class = ($year == $currentYear) ? ' class="active-year"' : ''; ?>
<h2><a href="/archives/<?= $year ?>/"<?= $class ?>><?= $year ?></a></h2>
<h2><a href="/archives/<?php echo $year ?>/"<?php echo $class ?>><?php echo $year ?></a></h2>
<?php endforeach; ?>
</section>
<?php include $_SERVER['DOCUMENT_ROOT'] . '/inc/base/copyright.php'; ?>
<?php require $_SERVER['DOCUMENT_ROOT'] . '/inc/base/copyright.php'; ?>
</div>
</div>
</footer>
<script src="/assets/js/bundle.min.js"></script>
<script src="/assets/js/bundle.min.js"></script>

View File

@ -29,21 +29,21 @@ elseif (preg_match('#^/archives/(\d{4})#', $requestUri, $matches)) {
<meta charset="utf-8" />
<meta property="og:locale" content="en_US" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<title><?= htmlspecialchars($pageTitle) ?></title>
<title><?php echo htmlspecialchars($pageTitle) ?></title>
<link rel="preconnect" href="https://fonts.bunny.net">
<meta name="description" content="<?= htmlspecialchars($pageDesc) ?>">
<meta property="og:description" content="<?= htmlspecialchars($pageDesc) ?>">
<meta name="title" content="<?= htmlspecialchars($config['title']) ?>">
<meta name="keywords" content="<?= htmlspecialchars($config['keywords']) ?>">
<meta property="og:title" content="<?= htmlspecialchars($pageTitle) ?>">
<meta name="description" content="<?php echo htmlspecialchars($pageDesc) ?>">
<meta property="og:description" content="<?php echo htmlspecialchars($pageDesc) ?>">
<meta name="title" content="<?php echo htmlspecialchars($config['title']) ?>">
<meta name="keywords" content="<?php echo htmlspecialchars($config['keywords']) ?>">
<meta property="og:title" content="<?php echo htmlspecialchars($pageTitle) ?>">
<meta property="og:type" content="website" />
<meta property="og:site_name" content="<?= htmlspecialchars($config['sitename'] ?? $config['title']) ?>" />
<meta property="og:url" content="<?= htmlspecialchars($baseUrl . $_SERVER['REQUEST_URI']) ?>" />
<?php if ($randomImage): ?>
<meta property="og:image" content="<?= htmlspecialchars($baseUrl . $randomImage) ?>" />
<meta property="og:site_name" content="<?php echo htmlspecialchars($config['sitename'] ?? $config['title']) ?>" />
<meta property="og:url" content="<?php echo htmlspecialchars($baseUrl . $_SERVER['REQUEST_URI']) ?>" />
<?php if ($randomImage) : ?>
<meta property="og:image" content="<?php echo htmlspecialchars($baseUrl . $randomImage) ?>" />
<?php endif; ?>
<link rel="me" href="<?= htmlspecialchars($config['mastodon']) ?>" />
<link rel="me" href="mailto:<?= htmlspecialchars($config['email'] ?? 'admin@' . $_SERVER['HTTP_HOST']) ?>" />
<link rel="me" href="<?php echo htmlspecialchars($config['mastodon']) ?>" />
<link rel="me" href="mailto:<?php echo htmlspecialchars($config['email'] ?? 'admin@' . $_SERVER['HTTP_HOST']) ?>" />
<link rel="icon" type="image/webp" href="/images/site/favicon-96x96.webp" sizes="96x96" />
<link rel="icon" type="image/svg+xml" href="/images/site/favicon.svg" />
<link rel="shortcut icon" href="/images/site/favicon.ico" />

View File

@ -13,19 +13,28 @@ if (preg_match('#^/archives/(\d{4})(/|$)#', $_SERVER['REQUEST_URI'], $matches))
$preferredYear = (int)($detectedYear ?? date('Y'));
// Find the most recent year with images
$availableYears = array_values(array_filter(scandir($imagesRoot), function($f) use ($imagesRoot) {
return is_dir("$imagesRoot/$f") && preg_match('/^\d{4}$/', $f);
}));
$availableYears = array_values(
array_filter(
scandir($imagesRoot), function ($f) use ($imagesRoot) {
return is_dir("$imagesRoot/$f") && preg_match('/^\d{4}$/', $f);
}
)
);
rsort($availableYears, SORT_NUMERIC);
$year = null;
foreach ($availableYears as $y) {
if ((int)$y > $preferredYear) continue; // Skip future years
if ((int)$y > $preferredYear) { continue; // Skip future years
}
$monthsPath = "$imagesRoot/$y";
$months = array_values(array_filter(scandir($monthsPath), function($f) use ($monthsPath) {
return is_dir("$monthsPath/$f") && preg_match('/^\d{2}$/', $f);
}));
$months = array_values(
array_filter(
scandir($monthsPath), function ($f) use ($monthsPath) {
return is_dir("$monthsPath/$f") && preg_match('/^\d{2}$/', $f);
}
)
);
$hasImages = false;
foreach ($months as $month) {
@ -47,9 +56,13 @@ if (!$year) {
}
$monthsPath = "$imagesRoot/$year";
$months = array_values(array_filter(scandir($monthsPath), function($f) use ($monthsPath) {
return is_dir("$monthsPath/$f") && preg_match('/^\d{2}$/', $f);
}));
$months = array_values(
array_filter(
scandir($monthsPath), function ($f) use ($monthsPath) {
return is_dir("$monthsPath/$f") && preg_match('/^\d{2}$/', $f);
}
)
);
rsort($months, SORT_STRING);
$allImages = [];
@ -58,12 +71,14 @@ foreach ($months as $month) {
$previewDir = "$imagesRoot/$year/$month/previews";
$fullDir = "$imagesRoot/$year/$month/fulls";
if (!is_dir($previewDir)) continue;
if (!is_dir($previewDir)) { continue;
}
$files = glob("$previewDir/*.webp");
foreach ($files as $previewPath) {
$timestamp = getTimestampFromJson($previewPath, $fullDir);
if ($timestamp === false || !is_numeric($timestamp)) continue;
if ($timestamp === false || !is_numeric($timestamp)) { continue;
}
$allImages[] = [
'previewPath' => $previewPath,
@ -75,9 +90,11 @@ foreach ($months as $month) {
}
}
usort($allImages, function($a, $b) {
return $b['timestamp'] <=> $a['timestamp'];
});
usort(
$allImages, function ($a, $b) {
return $b['timestamp'] <=> $a['timestamp'];
}
);
$imageBlocks = '';
foreach ($allImages as &$img) {
@ -101,7 +118,8 @@ unset($img);
return $imageBlocks;
function getTimestampFromJson($previewPath, $fullDir) {
function getTimestampFromJson($previewPath, $fullDir)
{
$filename = basename($previewPath);
$jsonFile = $fullDir . '/' . pathinfo($filename, PATHINFO_FILENAME) . '.json';

View File

@ -4,4 +4,4 @@ if (!defined('Such_a_good_girl')) {
}
?>
<li><a href="#archives">Archives<svg width="18" height="18" class="icon solid" xmlns="http://www.w3.org/2000/svg"><path d="M9.846 7.385a.62.62 0 00-.615-.616H6.769a.62.62 0 00-.615.616.62.62 0 00.615.615h2.462a.62.62 0 00.615-.615zm5.539-1.847v9.231a.62.62 0 01-.616.616H1.231a.62.62 0 01-.616-.616V5.54a.62.62 0 01.616-.616h13.538a.62.62 0 01.616.615zM16 1.231v2.461a.62.62 0 01-.615.616H.615A.62.62 0 010 3.692V1.231A.62.62 0 01.615.615h14.77a.62.62 0 01.615.616z"/></svg></a></li>
<li><a href="#footer">About<svg width="18" height="18" class="icon solid" xmlns="http://www.w3.org/2000/svg"><path d="M10.667 13v-1.667a.33.33 0 00-.334-.333h-1V5.667A.33.33 0 009 5.333H5.667a.33.33 0 00-.334.334v1.666a.33.33 0 00.334.334h1V11h-1a.33.33 0 00-.334.333V13a.33.33 0 00.334.333h4.666a.33.33 0 00.334-.333zM9.333 3.667V2A.33.33 0 009 1.667H7A.33.33 0 006.667 2v1.667A.33.33 0 007 4h2a.33.33 0 00.333-.333zM16 8c0 4.417-3.583 8-8 8s-8-3.583-8-8 3.583-8 8-8 8 3.583 8 8z"/></svg></a></li>
<li><a href="#footer">About<svg width="18" height="18" class="icon solid" xmlns="http://www.w3.org/2000/svg"><path d="M10.667 13v-1.667a.33.33 0 00-.334-.333h-1V5.667A.33.33 0 009 5.333H5.667a.33.33 0 00-.334.334v1.666a.33.33 0 00.334.334h1V11h-1a.33.33 0 00-.334.333V13a.33.33 0 00.334.333h4.666a.33.33 0 00.334-.333zM9.333 3.667V2A.33.33 0 009 1.667H7A.33.33 0 006.667 2v1.667A.33.33 0 007 4h2a.33.33 0 00.333-.333zM16 8c0 4.417-3.583 8-8 8s-8-3.583-8-8 3.583-8 8-8 8 3.583 8 8z"/></svg></a></li>

View File

@ -10,7 +10,7 @@ if ($_SERVER["REQUEST_URI"] === "/favicon.ico") {
readfile($faviconPath);
exit();
}
require_once $_SERVER["DOCUMENT_ROOT"] . "/inc/errors/404.php";
include_once $_SERVER["DOCUMENT_ROOT"] . "/inc/errors/404.php";
exit();
}
function getAvailableArchiveYears(string $baseDir = "/images"): array
@ -41,10 +41,10 @@ if ($path === "") {
$year = $segments[1];
$content = $imageBlocks;
} else {
require_once $_SERVER["DOCUMENT_ROOT"] . "/inc/errors/404.php";
include_once $_SERVER["DOCUMENT_ROOT"] . "/inc/errors/404.php";
exit();
}
} else {
require_once $_SERVER["DOCUMENT_ROOT"] . "/inc/errors/404.php";
include_once $_SERVER["DOCUMENT_ROOT"] . "/inc/errors/404.php";
exit();
}
}

View File

@ -2,9 +2,11 @@
if (!defined('Such_a_good_girl')) {
die("Direct access not allowed.");
}
function getRandomImage($baseDir = null) {
function getRandomImage($baseDir = null)
{
$baseDir = $baseDir ?? $_SERVER['DOCUMENT_ROOT'] . '/images';
if (!is_dir($baseDir)) return null;
if (!is_dir($baseDir)) { return null;
}
$year = null;
if (preg_match('#^/archives/(\d{4})(/|$)#', $_SERVER['REQUEST_URI'], $matches)) {
$year = $matches[1];
@ -14,13 +16,17 @@ function getRandomImage($baseDir = null) {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($baseDir));
$images = [];
foreach ($rii as $f) {
if (!$f->isFile() || strtolower($f->getExtension()) !== 'webp') continue;
if (!$f->isFile() || strtolower($f->getExtension()) !== 'webp') { continue;
}
$p = $f->getPathname();
if (strpos($p, $baseDir . '/site/') !== false) continue;
if (strpos($p, $baseDir . '/site/') !== false) { continue;
}
$r = str_replace(['\\','/'], '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $p));
if (!preg_match("#^/images/{$year}/\d{2}/fulls/#", $r)) continue;
if (!preg_match("#^/images/{$year}/\d{2}/fulls/#", $r)) { continue;
}
[$w, $h] = @getimagesize($p);
if ($w > $h) $images[] = $r;
if ($w > $h) { $images[] = $r;
}
}
return $images ? $images[array_rand($images)] : null;
}
}

View File

@ -13,4 +13,4 @@ require_once $_SERVER['DOCUMENT_ROOT'] . '/config.php';
<h1>Error 403</h1>
<h2>You don't have permission to access this resource.</h2>
</body>
</html>
</html>

View File

@ -40,7 +40,8 @@ $sitemapPath = $scanDir . '/sitemap.xml';
$urls = [];
// Helper to check if path should be ignored
function isIgnored(string $relativePath, array $ignoreList): bool {
function isIgnored(string $relativePath, array $ignoreList): bool
{
foreach ($ignoreList as $ignore) {
// If ignore ends with '/', treat as directory prefix
if (str_ends_with($ignore, '/')) {
@ -59,7 +60,8 @@ function isIgnored(string $relativePath, array $ignoreList): bool {
// Scan directory recursively
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($scanDir));
foreach ($rii as $file) {
if ($file->isDir()) continue;
if ($file->isDir()) { continue;
}
$filePath = $file->getPathname();
@ -73,13 +75,16 @@ foreach ($rii as $file) {
}
// Ignore hidden/system files
if (strpos($relativePath, '/.') !== false) continue;
if (strpos($relativePath, '/.') !== false) { continue;
}
// Ignore files/directories in ignore list
if (isIgnored($relativePath, $ignoreList)) continue;
if (isIgnored($relativePath, $ignoreList)) { continue;
}
$ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
if (!in_array($ext, $includeExtensions)) continue;
if (!in_array($ext, $includeExtensions)) { continue;
}
$url = htmlspecialchars($baseUrl . $relativePath, ENT_XML1);
$lastMod = date('Y-m-d', filemtime($filePath));

View File

@ -2,7 +2,8 @@
define('Such_a_good_girl', true);
require_once $_SERVER['DOCUMENT_ROOT'] . '/config.php';
function check_upload_key() {
function check_upload_key()
{
global $config;
if (!isset($_GET['key']) || $_GET['key'] !== $config['uploadKey']) {
http_response_code(403);
@ -10,4 +11,4 @@ function check_upload_key() {
}
}
// make this use mysql? Maybe :3
// make this use mysql? Maybe :3

View File

@ -1,5 +1,6 @@
<?php
function send_discord_webhook(string $filename, DateTime $takenDate, string $year, string $month, string $copyright, string $hexcolor): void {
function send_discord_webhook(string $filename, DateTime $takenDate, string $year, string $month, string $copyright, string $hexcolor): void
{
global $config;
if (empty($config['discord_webhook_url'])) {
return;

View File

@ -1,5 +1,6 @@
<?php
function load_image(string $path, string $ext) {
function load_image(string $path, string $ext)
{
return match ($ext) {
'jpg', 'jpeg', 'jpe', 'jif', 'jfif', 'jfi' => @imagecreatefromjpeg($path),
'png' => @imagecreatefrompng($path),
@ -10,7 +11,8 @@ function load_image(string $path, string $ext) {
};
}
function fix_orientation(GdImage $img, array $exif): GdImage {
function fix_orientation(GdImage $img, array $exif): GdImage
{
if (!isset($exif['Orientation']) || !is_int($exif['Orientation'])) {
return $img;
}

View File

@ -1,10 +1,13 @@
<?php
function sanitize_filename($filename) {
function sanitize_filename($filename)
{
return preg_replace('/[^A-Za-z0-9_\-]/', '_', pathinfo($filename, PATHINFO_FILENAME));
}
function extract_exif_date($tmpFile) {
if (!function_exists('exif_read_data')) return null;
function extract_exif_date($tmpFile)
{
if (!function_exists('exif_read_data')) { return null;
}
$exif = @exif_read_data($tmpFile);
$dateString = $exif['DateTimeOriginal'] ?? $exif['DateTime'] ?? null;
return $dateString ? DateTime::createFromFormat('Y:m:d H:i:s', $dateString) : null;

View File

@ -18,17 +18,22 @@ $tmpFile = $_FILES['file']['tmp_name'];
$origName = basename($_FILES['file']['name']);
$ext = strtolower(pathinfo($origName, PATHINFO_EXTENSION));
if (!in_array(strtolower($ext), [
'jpg', 'jpeg', 'jpe', 'jif', 'jfif', 'jfi', 'png', 'gif', 'webp', 'bmp', 'dib'
])) {
if (!in_array(
strtolower($ext), [
'jpg', 'jpeg', 'jpe', 'jif', 'jfif', 'jfi', 'png', 'gif', 'webp', 'bmp', 'dib'
]
)
) {
http_response_code(400);
exit('Unsupported file type');
}
function parse_manual_date(string $manualDateStr) {
function parse_manual_date(string $manualDateStr)
{
$tz = new DateTimeZone($GLOBALS['config']['timezone'] ?? 'America/Los_Angeles');
$dt = DateTime::createFromFormat('Y-m-d\TH:i', $manualDateStr, $tz);
if ($dt === false) return null;
if ($dt === false) { return null;
}
return $dt;
}
@ -140,9 +145,9 @@ if (!file_put_contents($fullsDir . $jsonName, $json)) {
}
if (!empty($config['discord_webhook_enabled']) && $config['discord_webhook_enabled'] === 'true') {
require_once 'includes/discord_webhook.php';
include_once 'includes/discord_webhook.php';
send_discord_webhook($filename, $exifDate, $year, $month, $copyright, $hexcolor);
}
echo 'Upload successful';
echo 'Upload successful';