summaryrefslogtreecommitdiff
path: root/world/map/news.php
blob: 48ba5e001bcc7e2d1629f423ea655e00529da38e (plain) (blame)
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
123
124
125
126
127
128
129
130
<?php

// configuration variables
$min_version = '0.5.1';
$min_manaplus = '1.1.2.20';
$cur_version = '0.6.1';

// utility functions
function failure_headers()
{
    header('HTTP/1.0 403 Upgrade your client');
    common_headers();
}

function common_headers()
{
    header('Content-type: text/plain');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header('Pragma: no-cache');
}

function starts_with($haystack, $needle)
{
    // avoid allocation
    return strpos($haystack, $needle, 0) === 0;
}

function record_version($agent)
{
    $file = 'versions/' . date('Y-m-d') . '.txt';
    // is touching this really needed?
    touch($file);
    file_put_contents($file, '[' . date('H:i') . "] $agent\n", FILE_APPEND);
}

// response functions
function handle_tmw($agent)
{
    failure_headers();
    echo "##1 The client you're using is really old!\n",
         "##1 Please upgrade to a Mana or ManaPlus client.\n",
         "##1     TMW Staff\n \n";
}

function handle_mana($agent, $min_version)
{
    $version_pos = 5;
    $version_end = strpos($agent, ' ');
    $agent_version = substr($agent, $version_pos, $version_end - $version_pos);
    if (version_compare($agent_version, $min_version) < 0)
    {
        failure_headers();
        echo "##1 The client you're using is no longer\n".
             "##1 supported! Please upgrade to $min_version or\n".
             "##1 higher, or use ManaPlus!\n \n".
             "##1     TMW Staff\n \n";
    }
    else
    {
        common_headers();
    }
}

function handle_manaplus($agent, $min_version)
{
    $version_pos = strpos($agent, '4144 v') + 6;
    $version_end = strpos($agent, ')', $version_pos);
    $agent_version = substr($agent, $version_pos, $version_end - $version_pos);
    if (version_compare($agent_version, $min_version) < 0)
    {
        failure_headers();
        echo "##1 The client you're using is no longer\n",
             "##1 supported! Please upgrade to $min_version or\n",
             "##1 higher!\n \n",
             "##1     TMW Staff\n \n";
    }
    else
    {
        common_headers();
    }
}

function handle_other($agent)
{
    common_headers();
    echo "##1 I have no clue what client you're using. Good luck!\n";
}

function handle_browser($agent)
{
    common_headers();
    echo "##1 It looks like this is a web browser, not a game client\n";
}

// main body
$agent = $_SERVER['HTTP_USER_AGENT'];

if (starts_with($agent, 'TMW'))
{
    handle_tmw($agent);
    record_version($agent);
}
else if (starts_with($agent, 'Mana'))
{
    if (starts_with($agent, 'ManaPlus'))
    {
        handle_manaplus($agent, $min_manaplus);
    }
    else if (starts_with($agent, 'Mana/'))
    {
        handle_mana($agent, $min_version);
    }
    else
    {
        handle_other($agent);
    }
    record_version($agent);
}
else
{
    handle_browser($agent);
}

echo "##9 Latest client version: ##6$cur_version\n \n";
echo "##7 TMW Staff will never ask you for your password.\n";
echo "##7 Anyone doing so is trying to scam you.\n \n";

print file_get_contents('news.txt');
?>