deadjournal/core/helpers/functions.php

77 lines
1.8 KiB
PHP
Raw Normal View History

2025-01-08 22:46:44 +05:00
<?php
// Simple HTML screening
function e($string)
{
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}
function isAdmin()
{
if (!ipAllowed()) {
return false;
} else {
return isset($_SESSION['user_id']);
}
}
function ipAllowed()
{
// If you declared $ALLOWED_IPS in config.php as global or static,
// retrieve them with "global" or by including them from config
global $ALLOWED_IPS;
$clientIp = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
foreach ($ALLOWED_IPS as $allowed) {
if (isIpInRange($clientIp, $allowed)) {
return true;
}
}
return false;
}
function isIpInRange($ip, $allowed)
{
// If the allowed is an IP with no slash, compare directly
if (strpos($allowed, '/') === false) {
return $ip === $allowed;
} else {
// It's a CIDR notation, e.g. 192.168.1.0/24
list($subnet, $mask) = explode('/', $allowed);
// Convert IPs to their numeric representation
$ipLong = ip2long($ip);
$subnetLong = ip2long($subnet);
// mask => 24 means 255.255.255.0
$mask = (int)$mask;
$maskLong = ~((1 << (32 - $mask)) - 1);
return ($ipLong & $maskLong) === ($subnetLong & $maskLong);
}
}
/**
* Split content at <!--more--> marker.
* Returns [ 'excerpt' => '...', 'hasMore' => bool ]
*/
function getExcerptWithMore(string $content): array
{
// Look for the marker
$parts = explode('<!--more-->', $content, 2);
if (count($parts) === 2) {
// We have an excerpt in $parts[0], and the remainder in $parts[1]
return [
'excerpt' => $parts[0],
'hasMore' => true,
];
} else {
// No marker found, so the entire post is $content
return [
'excerpt' => $content,
'hasMore' => false,
];
}
}