-
Notifications
You must be signed in to change notification settings - Fork 1
/
prtg.class.php
139 lines (117 loc) · 3.14 KB
/
prtg.class.php
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
<?php
/**
* Class prtg
* @package satrobit\prtg-php
*/
class prtg {
private static $server;
private static $username;
private static $password;
private static $passhash;
/**
* @param string $server
* @param string $username
* @param string $password
*/
function __construct($server, $username, $password)
{
if (empty($server)) throw new Exception('Server parameter cannot be empty.');
self::$server = rtrim($server, '/\\');
self::$username = $username;
self::$password = $password;
self::$passhash = $this->getpasshash();
if (!self::$passhash) throw new Exception('Provided credentials are wrong.');
}
/**
* @param string $url
*
* @return string
*/
private function sendRequest($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* @param string $path
* @param array $parameters
* @param bool $auth
* @param bool $json
*
* @return array
*/
private function get($path, $parameters, $json = true, $auth = true)
{
if ($auth)
{
$parameters['username'] = self::$username;
$parameters['passhash'] = self::$passhash;
}
$baseUrl = self::$server;
$queryString = http_build_query($parameters);
$requestUrl = $baseUrl . '/' . $path . '?' . $queryString;
$response = $this->sendRequest($requestUrl);
if ($json) return json_decode($response, true);
return $response;
}
/**
* @return int
*/
public function getpasshash()
{
$response = $this->get('api/getpasshash.htm', ['username' => self::$username, 'password' => self::$password], false, false);
if (!is_numeric($response)) return false;
return $response;
}
/**
* @param int $sensorId
*
* @return array
*/
public function getsensordetails($sensorId)
{
$response = $this->get('api/getsensordetails.json', ['id' => $sensorId]);
if (is_null($response)) throw new Exception('Could not find the sensor.');
return $response;
}
/**
* @param int $sensorId
* @param string $sdate
* @param string $edate
* @param int $avg
*
* @return array
*/
public function historicdata($sensorId, $sdate, $edate, $avg = 0)
{
$response = $this->get('api/historicdata.json', ['id' => $sensorId, 'sdate' => $sdate, 'edate' => $edate, 'avg' => $avg]);
if (is_null($response)) throw new Exception('Could not find the sensor.');
return $response;
}
/**
* @param int $sensorId
* @param string $sdate
* @param string $edate
* @param int $graphid
* @param string $type
* @param int $avg
* @param int $height
* @param int $width
*
* @return string
*/
public function chart($sensorId, $sdate, $edate, $graphid, $type = 'svg', $avg = 15, $height = 270, $width = 850)
{
$response = $this->get('chart.' . $type, ['id' => $sensorId, 'sdate' => $sdate, 'edate' => $edate, 'avg' => $avg, 'graphid' => $graphid, 'height' => $height, 'width' => $width], false);
if ($response == 'Error creating chart.') throw new Exception('Error creating chart.');
return $response;
}
}
?>