Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- date_default_timezone_set('Europe/Sofia');
- $name = trim($_GET['name']);
- $gender = trim($_GET['gender']);
- $pin = trim($_GET['pin']);
- if (!preg_match('/^[A-Z][a-z]+\s[A-Z][a-z]+$/', $name) || ($gender != 'male' && $gender != 'female') ||
- !preg_match('/^\d{10}$/', $pin)
- ) {
- die('<h2>Incorrect data</h2>');
- }
- $sexDigit = (int)$pin[8];
- if (($gender == 'male' && ($sexDigit % 2) !== 0) ||
- ($gender == 'female' && ($sexDigit % 2) === 0)
- ) {
- die('<h2>Incorrect data</h2>');
- }
- $year = substr($pin, 0, 2);
- $month = substr($pin, 2, 2);
- $dayOfMonth = substr($pin, 4, 2);
- if ((int)$month > 40) {
- $year = '20' . $year;
- $month = (int)$month - 40;
- } elseif ((int)$month > 20) {
- $year = '18' . $year;
- $month = (int)$month - 20;
- } else {
- $year = '19' . $year;
- }
- $dateStr = $month . '/' . $dayOfMonth . '/' . $year;
- $date = new DateTime($dateStr);
- if (!$date || ($date->format('n') != $month)) {
- die('<h2>Incorrect data</h2>');
- }
- $multipliers = [2, 4, 8, 5, 10, 9, 7, 3, 6];
- $totalSum = 0;
- foreach ($multipliers as $key => $multiplier) {
- $digit = (int)$pin[$key];
- $totalSum += ($digit * $multiplier);
- }
- $totalSum = ($totalSum % 11) % 10;
- $checksum = (int)$pin[9];
- if ($checksum !== $totalSum) {
- die('<h2>Incorrect data</h2>');
- }
- $data = array('name' => $name, 'gender' => $gender, 'pin' => $pin);
- echo json_encode($data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement