From 4b666200c1d7afefd887a9d4cc04eefcc7f03372 Mon Sep 17 00:00:00 2001 From: Nikita Petko Date: Thu, 25 Nov 2021 20:28:41 +0000 Subject: [PATCH] Update IpHelper.php, config.php. Add WebsiteSettings.php ~ Please adapt!!!! ~ Added new methods to IpHelper ~ Added include for iphelper in config.php --- globals/Dependencies/Web/IpRange.php | 43 ++++ globals/Dependencies/Web/WebsiteSettings.php | 214 +++++++++++++++++++ globals/config.php | 1 + 3 files changed, 258 insertions(+) create mode 100644 globals/Dependencies/Web/WebsiteSettings.php diff --git a/globals/Dependencies/Web/IpRange.php b/globals/Dependencies/Web/IpRange.php index 04c3d3d..67646f7 100644 --- a/globals/Dependencies/Web/IpRange.php +++ b/globals/Dependencies/Web/IpRange.php @@ -84,6 +84,17 @@ namespace Alphaland\Web { return false; } + public static function IsIpInRangeList(string $ip, array $ranges) + { + foreach ($ranges as $range) { + if (self::IsIpInRange($ip, $range)) { + return true; + } + } + + return false; + } + /** * @param string $ip IP address * @param string $netmask Netmask @@ -105,6 +116,16 @@ namespace Alphaland\Web { return false; } + public static function IsIpInNetmaskList(string $ip, array $netmasks) + { + foreach ($netmasks as $netmask) { + if (self::IsIpInNetmask($ip, $netmask)) { + return true; + } + } + + return false; + } /** * @param string $ip IP address @@ -123,5 +144,27 @@ namespace Alphaland\Web { $subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned return ($ip & $mask) == $subnet; } + + public static function IsIpInCidrRangeList(string $ip, array $cidrs) + { + foreach ($cidrs as $cidr) { + if (self::IsIpInCidrRange($ip, $cidr)) { + return true; + } + } + + return false; + } + + public static function IsIpInCidrNetmaskOrRangeList(string $ip, array $cidrs) + { + foreach ($cidrs as $cidr) { + if (self::IsIpInCidrRange($ip, $cidr) || self::IsIpInNetmask($ip, $cidr) || self::IsIpInRange($ip, $cidr) || $ip === $cidr) { + return true; + } + } + + return false; + } } } diff --git a/globals/Dependencies/Web/WebsiteSettings.php b/globals/Dependencies/Web/WebsiteSettings.php new file mode 100644 index 0000000..36ffe4b --- /dev/null +++ b/globals/Dependencies/Web/WebsiteSettings.php @@ -0,0 +1,214 @@ +prepare("SELECT `value`, `type` FROM `websettings` WHERE `name` = :name"); + $query->bindParam(':name', $name); + + if (!$query->execute()) { + return $default; + } + $result = $query->fetch(PDO::FETCH_ASSOC); + + if ($result === false) { + return $default; + } + + return self::ConvertStringToValue($result['value'], $result['type']); + } + + /** + * Sets a website setting. + * + * @param string $name The name of the setting. + * @param mixed $value The value of the setting. + * @param string $type The type of the setting. + * + * @return bool Whether the setting was set. + */ + public static function UpdateSetting(string $name, $value, string $type = null): bool + { + if ($type === null) { + $type = gettype($value); + } + + if ($value === self::DOES_NOT_EXIST) return false; + + if (!in_array($type, self::$validTypes)) { + return false; + } + + if (!self::ValueIsValidType($value, $type)) { + return false; + } + + $remote = self::GetSetting($name, self::DOES_NOT_EXIST); + + if ($remote === $value) { + return false; + } + + $query = null; + + if ($remote === self::DOES_NOT_EXIST) { + $query = $GLOBALS['pdo']->prepare("INSERT INTO `websettings` (`name`, `value`, `type`) VALUES (:name, :value, :type)"); + } else { + $query = $GLOBALS['pdo']->prepare("UPDATE `websettings` SET `value` = :value, `type` = :type WHERE `name` = :name"); + } + + if (gettype($value) === 'NULL') { + $query->bindParam(':value', $value, PDO::PARAM_NULL); + } else { + $tmp = self::ConvertValueToString($value); + + $query->bindParam(':value', $tmp, PDO::PARAM_STR); + } + + $query->bindParam(':name', $name, PDO::PARAM_STR); + $query->bindParam(':type', $type, PDO::PARAM_STR); + + return $query->execute(); + } + + /** + * Deletes a website setting. + * + * @param string $name The name of the setting. + * + * @return bool Whether the setting was deleted. + */ + public static function DeleteSetting(string $name): bool + { + $query = $GLOBALS['pdo']->prepare("DELETE FROM `websettings` WHERE `name` = :name"); + $query->bindParam(':name', $name); + + return $query->execute(); + } + + /** + * Gets all website settings. + * + * @return array The website settings. + */ + public static function GetAllSettings(): array + { + $query = $GLOBALS['pdo']->prepare("SELECT `name`, `value`, `type` FROM `websettings`"); + + if (!$query->execute()) { + return []; + } + + $result = $query->fetchAll(PDO::FETCH_ASSOC); + + $settings = []; + + foreach ($result as $row) { + $settings[$row['name']] = self::ConvertStringToValue($row['value'], $row['type']); + } + + return $settings; + } + + public static function SettingExists(string $name): bool + { + $query = $GLOBALS['pdo']->prepare("SELECT `name` FROM `websettings` WHERE `name` = :name"); + $query->bindParam(':name', $name); + + if (!$query->execute()) { + return false; + } + + $result = $query->fetch(PDO::FETCH_ASSOC); + + return $result !== false; + } + + public static function DeleteAllSettings(): bool + { + $query = $GLOBALS['pdo']->prepare("DELETE FROM `websettings`"); + + return $query->execute(); + } + } +} diff --git a/globals/config.php b/globals/config.php index 43101f2..7c54f77 100644 --- a/globals/config.php +++ b/globals/config.php @@ -97,6 +97,7 @@ try include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Users/TwoFactor.php"; include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Moderation/UserModerationManager.php"; include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Common/HashingUtiltity.php"; + include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Web/IpRange.php"; //authenticator $authenticator = new PHPGangsta_GoogleAuthenticator();