summaryrefslogtreecommitdiff
path: root/Blatech.php
blob: 305aea7e5c6042e8d3829c8d48540c836d78a0b1 (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
<?php

class Blatech
{
    protected $blatech  = null;

    public function __construct($config)
    {
        $this->blatech = parse_ini_file($config, true);
    }

    public function runCommand($message, $args)
    {
        $args = static::sanitiseArgs($args);
        $nick = ltrim(explode('!', $message[0])[0], ":");
        $channel = $message[2];
        $msg = explode(" ", ltrim(trim($message[3], "\r\n"), ":@"));

        if (!isset($msg[0])) {
            return null;
        } else {
            if (array_key_exists($msg[0], $this->blatech)) {
                $command = $this->blatech[$msg[0]]['command'];
                $stdin = $this->blatech[$msg[0]]['stdin'];
                $cargs = $this->blatech[$msg[0]]['args'];
                $path = $this->blatech[$msg[0]]["path"];
                $include_trigger_as_argument = $this->blatech[$msg[0]]['include_trigger_as_argument'];

                if ($include_trigger_as_argument) {
                    $args = $msg[0] . ' ' . $args;
                }

                if ($path) {
                    $path_check = is_readable($path);

                    if (!$path_check) {
                        return 'Cannot access directory at : ' . $path;
                    }

                    $cd = "cd " . $path . ' && ';
                } else {
                    $cd = "";
                }
                if ($stdin == true) {
                    // var_dump($cd . 'echo "' . $args .  '" | ' . $command);
                    return explode("\n", shell_exec($cd . 'echo "' . $args .  '" | ' . $command))[0];
                } else if ($cargs == true) {
                    return 'Command Line Arguments Not Supported Yet.';
                } else {
                    // var_dump($cd . 'echo "' . $args .  '" | ' . $command);
                    return explode("\n", shell_exec($cd . $command))[0];
                }
            }
        }
    }

    private static function sanitiseArgs($string)
    {
        return preg_replace('/[^a-z\d_+:@#~!?\\/"\'£$%^*(){}\-\][=,.< >]/iu', '', $string);
    }
}