Skip to content
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
3 changes: 1 addition & 2 deletions [core]/esx_identity/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ Config.DateFormat = "DD/MM/YYYY"
Config.MaxNameLength = 20 -- Max Name Length.
Config.MinHeight = 120 -- 120 cm lowest height
Config.MaxHeight = 220 -- 220 cm max height.
Config.LowestYear = 1900 -- 112 years old is the oldest you can be.
Config.HighestYear = 2005 -- 18 years old is the youngest you can be.
Config.MaxAge = 100 -- 100 years old is the oldest you can be.

Config.FullCharDelete = true -- Delete all reference to character.
Config.EnableDebugging = ESX.GetConfig().EnableDebug -- prints for debugging :)
50 changes: 28 additions & 22 deletions [core]/esx_identity/server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,37 @@ local function saveIdentityToDatabase(identifier, identity)
MySQL.update.await("UPDATE users SET firstname = ?, lastname = ?, dateofbirth = ?, sex = ?, height = ? WHERE identifier = ?", { identity.firstName, identity.lastName, identity.dateOfBirth, identity.sex, identity.height, identifier })
end

local function checkDOBFormat(str)
str = tostring(str)
if not string.match(str, "(%d%d)/(%d%d)/(%d%d%d%d)") then
return false
end
---@param year number Year
---@return boolean: true if the year is a leap year, false otherwise
local function isLeapYear(year)
return (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0)
end

local d, m, y = string.match(str, "(%d+)/(%d+)/(%d+)")
---@param dob string Date of Birth in the format DD/MM/YYYY
---@return boolean: true if the date is valid, false otherwise
local function checkDOBFormat(dob)
dob = tostring(dob)

m = tonumber(m)
d = tonumber(d)
y = tonumber(y)
local dayStr, monthStr, yearStr = dob:match("^(%d%d?)/(%d%d?)/(%d%d%d%d)$")
if not dayStr or not monthStr or not yearStr then
return false
end

if ((d <= 0) or (d > 31)) or ((m <= 0) or (m > 12)) or ((y <= Config.LowestYear) or (y > Config.HighestYear)) then
local day, month, year = tonumber(dayStr), tonumber(monthStr), tonumber(yearStr)
if not day or not month or not year then
return false
elseif m == 4 or m == 6 or m == 9 or m == 11 then
return d <= 30
elseif m == 2 then
if y % 400 == 0 or (y % 100 ~= 0 and y % 4 == 0) then
return d <= 29
else
return d <= 28
end
else
return d <= 31
end

local currentYear = os.date("*t").year
local minYear = currentYear - Config.MaxAge
local maxYear = currentYear - 18

if year < minYear or year > maxYear then return false end
if month < 1 or month > 12 then return false end

-- Days in each month (starting from January.)
local daysInMonth = { 31, isLeapYear(year) and 29 or 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
return day >= 1 and day <= daysInMonth[month]
end

local function formatDate(str)
Expand Down Expand Up @@ -243,7 +249,6 @@ end

ESX.RegisterServerCallback("esx_identity:registerIdentity", function(source, cb, data)
local xPlayer = ESX.GetPlayerFromId(source)
data.dateofbirth = formatDate(data.dateofbirth)

if not checkNameFormat(data.firstname) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_firstname_format"), "error")
Expand All @@ -265,6 +270,7 @@ end
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_height_format"), "error")
return cb(false)
end

if xPlayer then
if alreadyRegistered[xPlayer.identifier] then
xPlayer.showNotification(TranslateCap("already_registered"), "error")
Expand All @@ -274,7 +280,7 @@ end
playerIdentity[xPlayer.identifier] = {
firstName = formatName(data.firstname),
lastName = formatName(data.lastname),
dateOfBirth = data.dateofbirth,
dateOfBirth = formatDate(data.dateofbirth),
sex = data.sex,
height = data.height,
}
Expand Down
Loading