Issues (2963)

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

1
<?php
2
/*
3
 * This program is free software: you can redistribute it and/or modify it
4
 * under the terms of the GNU General Public License as published by the
5
 * Free Software Foundation, either version 3 of the License, or (at your
6
 * option) any later version.  Please see LICENSE.txt at the top level of
7
 * the source code distribution for details.
8
*/
9
10
/**
11
 * SignalWire API Transport
12
 *
13
 * @author Igor Kuznetsov <[email protected]>
14
 * This is modifyed Twilio class from Andy Rosen <[email protected]>
15
 * @license GPL
16
 */
17
18
namespace LibreNMS\Alert\Transport;
19
20
use LibreNMS\Alert\Transport;
21
22
class Signalwire extends Transport
23
{
24
    public function deliverAlert($obj, $opts)
25
    {
26
        $signalwire_opts['spaceUrl'] = $this->config['signalwire-spaceUrl'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$signalwire_opts was never initialized. Although not strictly required by PHP, it is generally a good practice to add $signalwire_opts = array(); before regardless.
Loading history...
27
        $signalwire_opts['sid'] = $this->config['signalwire-project-id'];
28
        $signalwire_opts['token'] = $this->config['signalwire-token'];
29
        $signalwire_opts['sender'] = $this->config['signalwire-sender'];
30
        $signalwire_opts['to'] = $this->config['signalwire-to'];
31
32
        return $this->contactSignalwire($obj, $signalwire_opts);
33
    }
34
35
    public static function contactSignalwire($obj, $opts)
36
    {
37
        $params = [
38
            'spaceUrl' => $opts['spaceUrl'],
39
            'sid' => $opts['sid'],
40
            'token' => $opts['token'],
41
            'phone' => $opts['to'],
42
            'text' => $obj['title'],
43
            'sender' => $opts['sender'],
44
        ];
45
46
        $url = 'https://' . $params['spaceUrl'] . '.signalwire.com/api/laml/2010-04-01/Accounts/' . $params['sid'] . '/Messages.json';
47
48
        $data = [
49
            'From' => $params['sender'],
50
            'Body' => $params['text'],
51
            'To' => $params['phone'],
52
        ];
53
        $post = http_build_query($data);
54
55
        $curl = curl_init($url);
56
57
        // set_curl_proxy($curl);
58
59
        curl_setopt($curl, CURLOPT_POST, true);
60
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
61
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
62
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
63
        curl_setopt($curl, CURLOPT_USERPWD, $params['sid'] . ':' . $params['token']);
64
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
65
66
        curl_exec($curl);
67
68
        if (curl_getinfo($curl, CURLINFO_RESPONSE_CODE)) {
69
            return true;
70
        }
71
    }
72
73
    public static function configTemplate()
74
    {
75
        return [
76
            'config' => [
77
                [
78
                    'title' => 'Space URL',
79
                    'name' => 'signalwire-spaceUrl',
80
                    'descr' => 'SignalWire Space URL (Example: myspace).',
81
                    'type' => 'text',
82
                ],
83
                [
84
                    'title' => 'SignalWire Project ID',
85
                    'name' => 'signalwire-project-id',
86
                    'descr' => 'SignalWire Project ID  ',
87
                    'type' => 'text',
88
                ],
89
                [
90
                    'title' => 'Token',
91
                    'name' => 'signalwire-token',
92
                    'descr' => 'SignalWire Account Token ',
93
                    'type' => 'text',
94
                ],
95
                [
96
                    'title' => 'Mobile Number',
97
                    'name' => 'signalwire-to',
98
                    'descr' => 'Mobile number to SMS(Example: +14443332222)',
99
                    'type' => 'text',
100
                ],
101
                [
102
                    'title' => 'SignalWire SMS Number',
103
                    'name' => 'signalwire-sender',
104
                    'descr' => 'SignalWire sending number (Example: +12223334444)',
105
                    'type' => 'text',
106
                ],
107
            ],
108
            'validation' => [
109
                'signalwire-spaceUrl' => 'required|string',
110
                'signalwire-project-id' => 'required|string',
111
                'signalwire-token' => 'required|string',
112
                'signalwire-to' => 'required',
113
                'signalwire-sender' => 'required',
114
            ],
115
        ];
116
    }
117
}
118