Zuckerberg's Meta Considered Sharing User Data with China, Whistleblower Alleges
Read more of this story at Slashdot.
I tried PrettyLittleThing's 'ugly' rebranded clothes and was baffled when someone said I looked like a crumpled McDonald's bag
Dad and two daughters charged with supplying cocaine and heroin
Justin Trudeau weeps as he gives emotional farewell speech in front of his family and makes one FINAL dig at Donald Trump - before his successor as Canadian PM continues war of words with fresh jibe at the US president
'Catwoman' Jocelyn Wildenstein's fiancé Lloyd Klein shares insight into his devastating battle with grief as he recalls heartbreaking moment he found the heiress dead beside him
Tyra Banks, 51, shows off her taut complexion with dramatic bleached eyebrows as she attends Balenciaga show at Paris Fashion Week
Strap in, get ready for more Rust drivers in Linux kernel
Rust is alive and well in the Linux kernel and is expected to translate into noticeable benefits shortly, though its integration with the largely C-oriented codebase still looks uneasy.…
Gene Hackman's daughter emerges after police revealed details about his distressing final days with dead wife
The bloodthirsty terror tactics of Mexico's cartels: How ruthless drug gangs punish their enemies and spread fear from mass beheadings to CANNIBALISM - after nine students were found with hands hacked off
Man charged with kidnap after teenager attacked and 'forced into vehicle'
Has Prince Edward finally recovered from the disaster of It's A Royal Knockout? As he celebrates his 61st birthday today, we see if his public image has eventually bounced back - 38 years after THAT storm off made headlines around the world
Person with Palestine flag who climbed Big Ben clock tower named as Essex man
eBPF. It doesn't stand for anything. But it might mean bank
Meta says it has managed to reduce the CPU cycles of its top services by 20 percent through its Strobelight profiling orchestration suite, which relies on the open source eBPF project.…
CodeSOD: Where is the Validation At?
As oft stated, the "right" way to validate emails is to do a bare minimum sanity check on format, and then send a verification message to the email address the user supplied; it's the only way to ensure that what they gave you isn't just syntactically valid, but is actually usable.
But even that simple approach leaves places to go wrong. Take a look at this code, from Lana.
public function getEmailValidationErrors($data): array { $errors = []; if (isset($data["email"]) && !empty($data["email"])) { if (!str_contains($data["email"], "@")) { $error["email"] = "FORM.CONTACT_DETAILS.ERRORS.NO_AT"; } if (!str_contains($data["email"], ".")) { $error["email"] = "FORM.CONTACT_DETAILS.ERRORS.NO_DOT"; } if (strrpos($data["email"], "@") > strrpos($data["email"], ".")) { $error["email"] = "FORM.CONTACT_DETAILS.ERRORS.NO_TLD"; } } if (isset($data["email1"]) && !empty($data["email1"])) { if (!str_contains($data["email1"], "@")) { $error["email1"] = "FORM.CONTACT_DETAILS.ERRORS.NO_AT"; } if (!str_contains($data["email1"], ".")) { $error["email1"] = "FORM.CONTACT_DETAILS.ERRORS.NO_DOT"; } if (strrpos($data["email1"], "@") > strrpos($data["email1"], ".")) { $error["email1"] = "FORM.CONTACT_DETAILS.ERRORS.NO_TLD"; } } if (isset($data["email2"]) && !empty($data["email2"])) { if (!str_contains($data["email2"], "@")) { $error["email2"] = "FORM.CONTACT_DETAILS.ERRORS.NO_AT"; } if (!str_contains($data["email2"], ".")) { $error["email2"] = "FORM.CONTACT_DETAILS.ERRORS.NO_DOT"; } if (strrpos($data["email2"], "@") > strrpos($data["email2"], ".")) { $error["email2"] = "FORM.CONTACT_DETAILS.ERRORS.NO_TLD"; } } if (isset($data["email3"]) && !empty($data["email3"])) { if (!str_contains($data["email3"], "@")) { $error["email3"] = "FORM.CONTACT_DETAILS.ERRORS.NO_AT"; } if (!str_contains($data["email3"], ".")) { $error["email3"] = "FORM.CONTACT_DETAILS.ERRORS.NO_DOT"; } if (strrpos($data["email3"], "@") > strrpos($data["email3"], ".")) { $error["email3"] = "FORM.CONTACT_DETAILS.ERRORS.NO_TLD"; } } return $errors; }Let's start with the obvious problem: repetition. This function doesn't validate simply one email, but four, by copy/pasting the same logic multiple times. Lana didn't supply the repeated blocks, just noted that they existed, so let's not pick on the bad names: "email1", etc.- that's just my placeholder. I assume it's different contact types for a customer, or similar.
Now, the other problems range from trivial to comical. First, the PHP function empty returns true if the variable has a zero/falsy value or is not set, which means it implies an isset, making the first branch redundant. That's trivial.
The way the checks get logged into the $error array, they can overwrite each other, meaning if you forget the "@" and the ".", it'll only complain about the ".", but if you forget the ".", it'll complain about not having a valid TLD (the "NO_DOT" error will never be output). That's silly.
Finally, the $errors array is the return value, but the $error array is where we store our errors, meaning this function doesn't return anything in the first place. And that means that it's a email validation function which doesn't do anything at all, which honestly- probably for the best.