File size: 1,957 Bytes
36ddefa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

ob_start();
require_once 'MySQL_funcs.php';
require 'MySQL_config.php';
ob_end_clean();

session_start();

$content = getStandaloneFile('dynmap_config.json');
if (isset($content)) {
    $config = json_decode($content, true);
    $msginterval = $config['webchat-interval'];
} else {
    $msginterval = 2000;
}

if (isset($_SESSION['lastchat'])) {
    $lastchat = $_SESSION['lastchat'];
} else {
    $lastchat = 0;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && $lastchat < time()) {
    $micro = microtime(true);
    $timestamp = round($micro * 1000.0);

    $data = json_decode(trim(file_get_contents('php://input')));
    $data->timestamp = $timestamp;
    $data->ip = $_SERVER['REMOTE_ADDR'];
    if (isset($_SESSION['userid'])) {
        $uid = $_SESSION['userid'];
        if (strcmp($uid, '-guest-')) {
            $data->userid = $uid;
        }
    }
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $proxy = array_map('trim', explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
        $data->ip = $proxy[0];
    }
    $content = getStandaloneFile('dynmap_webchat.json');
    $gotold = false;
    if (isset($content)) {
        $old_messages = json_decode($content, true);
        $gotold = true;
    }

    if (!empty($old_messages)) {
        foreach ($old_messages as $message) {
            if (($timestamp - $config['updaterate'] - 10000) < $message['timestamp']) {
                $new_messages[] = $message;
            }
        }
    }
    $new_messages[] = $data;

    if ($gotold) {
        updateStandaloneFile('dynmap_webchat.json', json_encode($new_messages));
    } else {
        insertStandaloneFile('dynmap_webchat.json', json_encode($new_messages));
    }

    $_SESSION['lastchat'] = time() + $msginterval;
    echo "{ \"error\" : \"none\" }";
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST' && $lastchat > time()) {
    header('HTTP/1.1 403 Forbidden');
} else {
    echo "{ \"error\" : \"none\" }";
}
cleanupDb();