summaryrefslogtreecommitdiff
path: root/pages/api/ponytown.php
blob: bc40975127447533c1927c6191b6a1922206ee06 (plain)
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
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/util/session.inc"; global $isLoggedIn; global $_PROFILE; global $isLowerLoggedIn; global $app;
if (!$isLoggedIn && !$isLowerLoggedIn) header("Location: /-/login") and die();

$request_raw = file_get_contents('php://input');
$json_object = json_decode($request_raw, true);

$select = $_GET['id'] ?? null;

if (!isset($select)) {
    peh_error("System member not found", 404);
    return;
}

if (getMemberWithoutSystem($select) === null) {
    peh_error("System member not found", 404);
    return;
}

$member = getMemberWithoutSystem($select);

if ($isLowerLoggedIn && $member["_system"] !== $app["other"]["id"] && $member["_system"] !== "hrbom") {
    peh_error("System member not found", 404);
    return;
}

if (!isset($json_object[0]) || !isset($json_object[1])) {
    die("Missing data");
}

$errors = [];

foreach ([1, 2] as $_) {
    $input = $json_object[$_ - 1];

    $mime = explode(";", substr($input, 5))[0];
    $file = base64_decode(explode(",", explode(";", substr($input, 5))[1])[1]);

    $image = @imagecreatefromstring($file);
    $size = @getimagesizefromstring($file);

    if ($image === false) {
        $errors[] = "0x{$_}000000F: Failed to open image #" . $_ . ", it is probably not using a supported format";
    }

    if ($size === false) {
        $errors[] = "0x{$_}000000E: Failed to get metadata for image #" . $_ . ", it is probably corrupted";
    }

    if ($image === false || $size === false) continue;

    $foundColor = false;

    for ($i = 0; $i < $size[0]; $i++) {
        if (imagecolorat($image, $i, 0) !== 2130706432) {
            $foundColor = true;
        }
    }

    if (!$foundColor) {
        $errors[] = "0x{$_}000001A: Image #" . $_ . " seems to contain padding (based on the first row of pixels)";
    }

    $foundColor = false;

    for ($i = 0; $i < $size[1]; $i++) {
        if (imagecolorat($image, 0, $i) !== 2130706432) {
            $foundColor = true;
        }
    }

    if (!$foundColor) {
        $errors[] = "0x{$_}000001B: Image #" . $_ . " seems to contain padding (based on the first column of pixels)";
    }

    if ($_ === 1 && $size[0] > 70) {
        $errors[] = "0x{$_}000002A: Image #" . $_ . " is wider than it should, are you sure you set zoom to 1x? Maybe you inverted the files?";
    }

    if ($_ === 1 && $size[1] > 70) {
        $errors[] = "0x{$_}000002B: Image #" . $_ . " is higher than it should, are you sure you set zoom to 1x? Maybe you inverted the files?";
    }

    if ($_ === 2 && $size[0] > 40) {
        $errors[] = "0x{$_}000002A: Image #" . $_ . " is wider than it should, are you sure you set zoom to 1x? Maybe you inverted the files?";
    }

    if ($_ === 2 && $size[1] > 35) {
        $errors[] = "0x{$_}000002B: Image #" . $_ . " is higher than it should, are you sure you set zoom to 1x? Maybe you inverted the files?";
    }
}

if (count($errors) === 0 && isset($_GET["real"])) {
    foreach ([1, 2] as $_) {
        $input = $json_object[$_ - 1];

        $mime = explode(";", substr($input, 5))[0];
        $file = base64_decode(explode(",", explode(";", substr($input, 5))[1])[1]);

        $image = @imagecreatefromstring($file);

        imagealphablending($image, false);
        imagesavealpha($image, true);

        if ($_ === 1) {
            imagepng($image, $_SERVER['DOCUMENT_ROOT'] . "/assets/ponies/" . $member["id"] . ".png");
        } else {
            imagepng($image, $_SERVER['DOCUMENT_ROOT'] . "/assets/uploads/pt-" . $member["name"] . ".png");
        }
    }
}

createJob("UpdateAssets", [
    "type" => "ponytown"
]);

die(json_encode([
    "success" => count($errors) === 0,
    "errors" => $errors
]));