time(), ]; $MC->add('passport_data_'.$nonce, $passport_data); } $redirect_url = BASE_URL.'example.php'; header('Location: '.$redirect_url); exit; } $ssid = $_COOKIE['tg_ssid']; if (!$ssid) { // Generate ssid if not exists $ssid = bin2hex(openssl_random_pseudo_bytes(32)); setcookie('tg_ssid', $ssid, time() + 864000, '', '', true, true); } $ssid_hash = hash_hmac('sha256', 'ssid'.$ssid, HMAC_SECRET); $nonce = hash_hmac('sha256', 'nonce'.$ssid, HMAC_SECRET); $callback_url = BASE_URL.'example.php?set_ssid='.$ssid.'_'.$ssid_hash; // Logout case if ($_GET['logout']) { setcookie('tg_ssid', '', 1, '', '', true, true); $MC->delete('passport_data_'.$nonce); $redirect_url = BASE_URL.'example.php'; header('Location: '.$redirect_url); exit; } $passport_data = $MC->get('passport_data_'.$nonce); if ($passport_data['pending']) { $html = <<Hello, anonymous!

Waiting for passport data...


Cancel

HTML; $js = <<{$first_name} {$last_name}!"; } else { $html .= "

Hello, {$first_name} {$last_name}!

"; } if (isset($passport_data['data'])) { foreach ($passport_data['data'] as $password_value) { $type = $password_value['type']; $fields_html = ''; if ($password_value['data']) { foreach ($password_value['data'] as $data_key => $data_val) { $data_key_html = htmlspecialchars($data_key); $data_val_html = htmlspecialchars($data_val); $fields_html .= <<
{$data_key_html}
{$data_val_html}
HTML; } } if ($password_value['front_side']) { $passport_file = $password_value['front_side']; $file_url = htmlspecialchars($passport_file['file_url']); $fields_html .= <<
front_side
HTML; } if ($password_value['reverse_side']) { $passport_file = $password_value['reverse_side']; $file_url = htmlspecialchars($passport_file['file_url']); $fields_html .= <<
reverse_side
HTML; } if ($password_value['selfie']) { $passport_file = $password_value['selfie']; $file_url = htmlspecialchars($passport_file['file_url']); $fields_html .= <<
selfie
HTML; } if ($password_value['files']) { $files_html = ''; foreach ($password_value['files'] as $passport_file) { $file_url = htmlspecialchars($passport_file['file_url']); $files_html .= <<
HTML; } $fields_html .= <<
files
{$files_html}
HTML; } if ($password_value['translation']) { $files_html = ''; foreach ($password_value['translation'] as $passport_file) { $file_url = htmlspecialchars($passport_file['file_url']); $files_html .= <<
HTML; } $fields_html .= <<
translation
{$files_html}
HTML; } if ($password_value['phone_number']) { $phone_number = htmlspecialchars($password_value['phone_number']); $fields_html .= <<
{$phone_number}
HTML; } if ($password_value['email']) { $email = htmlspecialchars($password_value['email']); $fields_html .= <<
{$email}
HTML; } $html .= << {$type} {$fields_html} HTML; } } $html .= "

Log out

"; } else { $options_str = json_encode([ 'bot_id' => BOT_ID, 'scope' => ['data' => [ ['type' => 'personal_details', 'native_names' => true], ['type' => 'id_document', 'selfie' => true], 'address', ['type' => 'address_document', 'translation' => true], 'phone_number', 'email' ], 'v' => 1], 'public_key' => BOT_PUBLIC_KEY, 'nonce' => $nonce, 'callback_url' => $callback_url, ]); $html .= <<Hello, anonymous!
HTML; $js = << Telegram Passport Example
{$html}
HTML; ?>