summaryrefslogtreecommitdiff
path: root/public/scripts.php
blob: 0f80be49f77cc93fd927ce0549b694d75db9e01b (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/utils.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../vendor/autoload.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php';

use Raudius\Luar\Luar;

function parse_lua_script($value)
{
    if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Literal) {
        return $value->getValue();
    } else if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Table) {
        return parse_lua_script($value->getValue());
    }

    if (is_array($value)) {
        $out = [];
        foreach ($value as $k => $v) {
            if ($k == 'handle') {
                continue;
            }
            $out[$k] = parse_lua_script($v);
        }
        return $out;
    }

    return $value;
}

if (empty(LUA_SCRIPT_DIRECTORY)) {
    http_response_code(403);
    exit;
}

$command = null;
$scripts = [];

// loading lua scripts
$luar = new Luar();
$parsedown = new Parsedown();
$file_paths = glob(LUA_SCRIPT_DIRECTORY . '/*.lua');
foreach ($file_paths as $file_path) {
    $script = file_get_contents($file_path);

    $result = $luar->eval($script);

    $script = parse_lua_script($result);
    $script['description_text'] = str_replace(PHP_EOL, ' ', $script['description']);

    array_push($scripts, $script);
}

if ($cmd_id = $_GET['id'] ?? false) {
    $command = array_find(
        $scripts,
        fn($x) =>
        $x['name'] == $cmd_id || (isset($x['aliases']) ? in_array($cmd_id, $x['aliases']) : false)
    );

    if (!$command) {
        http_response_code(404);
        exit;
    }
}

if ($command) {
    if (isset($command['description'])) {
        $command['description_formatted'] = $parsedown->text($command['description']);
    }
}
?>
<html>

<head>
    <title><?= isset($command) ? "!{$command['name']} - Script" : 'Scripts' ?> - <?= BOT_USERNAME_FORMATTED ?></title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <link rel="stylesheet" href="/static/style.css">
    <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon">
</head>

<body>
    <main>
        <?php html_navigation_bar() ?>
        <content>
            <div class="row gap-8 background-colorful p-4" style="box-shadow: 0 2px 1px rgba(0, 0, 0, 0.25);z-index:1;">
                <img src="/static/img/icons/script.png" alt="">
                <h2>Scripts</h2>
            </div>
            <div class="row">
                <?php if (!empty($scripts)): ?>
                    <!-- SIDE BAR -->
                    <div class="wiki-sidebar">
                        <?php foreach ($scripts as $script): ?>
                            <p><a href="/!<?= $script['name'] ?>"
                                    title="<?= substr($script['description_text'] ?? '', 0, 64) . (strlen($script['description_text']) > 64 ? '...' : '') ?>"><?= $script['name'] ?></a>
                            </p>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>

                <!-- WIKI CONTENT -->
                <div class="wiki-content">
                    <?php if (isset($command)): ?>
                        <h1>&excl;<?= $command['name'] ?><a href="/!/<?= $command['name'] ?>.lua"><img
                                    src="/static/img/icons/code.png" alt="[code]" title="Check the source code"></a></h1>
                        <table class="vertical">
                            <tr>
                                <th>Delay</th>
                                <td><?= format_timestamp($command['delay_sec']) ?></td>
                            </tr>
                            <?php if (!empty($command['subcommands'])): ?>
                                <tr>
                                    <th>Subcommands</th>
                                    <td>
                                        <?= implode(', ', array_map(fn($x) => "<code>$x</code>", $command['subcommand'])) ?>
                                    </td>
                                </tr>
                            <?php endif; ?>
                            <?php if (!empty($command['aliases'])): ?>
                                <tr>
                                    <th>Aliases</th>
                                    <td>
                                        <?= implode(', ', array_map(fn($x) => "<code>$x</code>", $command['aliases'])) ?>
                                    </td>
                                </tr>
                            <?php endif; ?>
                            <tr>
                                <th>Minimal rights</th>
                                <td><?= $command['minimal_rights'] ?></td>
                            </tr>
                        </table>
                        <div>
                            <?php if (isset($command['description_formatted'])): ?>
                                <?= $command['description_formatted'] ?>
                            <?php else: ?>
                                <p><i>No description.</i></p>
                            <?php endif; ?>
                        </div>
                    <?php else: ?>
                        <p><i>Select a script from the list on the left.</i></p>
                    <?php endif; ?>
                </div>
            </div>
        </content>
    </main>

    <?php html_footer() ?>
</body>

</html>