Issues (2963)

LibreNMS/Alert/Transport/Osticket.php (1 issue)

1
<?php
2
/*
3
 * LibreNMS
4
 *
5
 * Copyright (c) 2016 Søren Friis Rosiak <[email protected]>
6
 * This program is free software: you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License as published by the
8
 * Free Software Foundation, either version 3 of the License, or (at your
9
 * option) any later version.  Please see LICENSE.txt at the top level of
10
 * the source code distribution for details.
11
 */
12
13
namespace LibreNMS\Alert\Transport;
14
15
use LibreNMS\Alert\Transport;
16
use LibreNMS\Config;
17
18
class Osticket extends Transport
19
{
20
    public function deliverAlert($obj, $opts)
21
    {
22
        if (! empty($this->config)) {
23
            $opts['url'] = $this->config['os-url'];
24
            $opts['token'] = $this->config['os-token'];
25
        }
26
27
        return $this->contactOsticket($obj, $opts);
28
    }
29
30
    public function contactOsticket($obj, $opts)
31
    {
32
        $url = $opts['url'];
33
        $token = $opts['token'];
34
        $email = '';
35
36
        foreach (parse_email(Config::get('email_from')) as $from => $from_name) {
37
            $email = $from_name . ' <' . $from . '>';
38
            break;
39
        }
40
41
        $protocol = [
42
            'name' => 'LibreNMS',
43
            'email' => $email,
44
            'subject' => ($obj['name'] ? $obj['name'] . ' on ' . $obj['hostname'] : $obj['title']),
45
            'message' => strip_tags($obj['msg']),
46
            'ip' => $_SERVER['REMOTE_ADDR'],
47
            'attachments' => [],
48
        ];
49
        $curl = curl_init();
50
        set_curl_proxy($curl);
51
        curl_setopt($curl, CURLOPT_URL, $url);
52
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
53
        curl_setopt($curl, CURLOPT_HTTPHEADER, [
54
            'Content-type' => 'application/json',
55
            'Expect:',
56
            'X-API-Key: ' . $token,
57
        ]);
58
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($protocol));
59
        $ret = curl_exec($curl);
0 ignored issues
show
The assignment to $ret is dead and can be removed.
Loading history...
60
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
61
62
        if ($code != 201) {
63
            var_dump('osTicket returned Error, retry later');
64
65
            return false;
66
        }
67
68
        return true;
69
    }
70
71
    public static function configTemplate()
72
    {
73
        return [
74
            'config' => [
75
                [
76
                    'title' => 'API URL',
77
                    'name' => 'os-url',
78
                    'descr' => 'osTicket API URL',
79
                    'type' => 'text',
80
                ],
81
                [
82
                    'title' => 'API Token',
83
                    'name' => 'os-token',
84
                    'descr' => 'osTicket API Token',
85
                    'type' => 'text',
86
                ],
87
            ],
88
            'validation' => [
89
                'os-url' => 'required|url',
90
                'os-token' => 'required|string',
91
            ],
92
        ];
93
    }
94
}
95