-
Notifications
You must be signed in to change notification settings - Fork 3
/
ahsay-api-wrapper.php
461 lines (365 loc) · 14.6 KB
/
ahsay-api-wrapper.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
/*
PHP API wrapper for AhsayOBS. Version 1.20
Copyright (C) 2016
Hannes Van de Vel ([email protected]),
Richard Bishop ([email protected]),
Christophe Aubry ([email protected]).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
class AhsayApiWrapper
{
public $serverAddress;
public $serverAdminUsername;
public $serverAdminPassword;
public $serverVersion;
public $debug;
public $error;
/*
Note:
All times (user added, backupset last run, completed etc) are in the form
of Unix timestamps. In the case of Java this is the number of milliseconds
since Jan 1st 1970; though PHP counts this as seconds since Jan 1st 1970.
The solution is to disregard the final 3 digits of the value output by OBS
*/
// Constructor
public function __construct($address, $username, $password, $version)
{
$this->serverAddress = rtrim($address, '/'); // Remove trailing slash
$this->serverAdminUsername = $username;
$this->serverAdminPassword = $password;
$this->serverVersion = $version; // Choose between Ahsay OBS v6 or v7
$this->debug;
}
// Enable/disable debugging
public function debug($which)
{
$this->debug = $which;
}
// Get Ahsay OBS License informations
public function getLicense()
{
$this->debugLog("Get Ahsay OBS license informations");
$url = "/GetLicense.do";
$result = $this->runQuery($url);
// If that didn't happen
$this->errorHandler($result, "Failed to get Ahsay OBS License informations.");
return $this->decodeResult($result);
}
// Authenticate a user against OBS
public function authenticateUser($username, $password)
{
$this->debuglog("Authenticate user $username");
$url = "/AuthUser.do?LoginName=$username&Password=$password";
$result = $this->runQuery($url);
// If that didn't happen
$this->errorHandler($result, "Authenticate user failed.");
return 'OK';
}
// Get a particular user
public function getUser($username)
{
$this->debuglog("Getting user '$username'");
$url = "/GetUser.do?LoginName=$username";
$result = $this->runQuery($url);
// If that didn't happen
$this->errorHandler($result, "No user details found for '$username'.");
return $this->decodeResult($result);
}
// Get an array of all users
public function getUsers()
{
$this->debuglog("Getting user list");
$url = "/ListUsers.do";
$result = $this->runQuery($url);
// If that didn't happen
$this->errorHandler($result, "Problem during getUsers().");
return $this->decodeResult($result);
}
// Get all backup sets for a particular user
public function getUserBackupSets($username)
{
$this->debuglog("Getting backup sets for user '$username'");
$url = "/ListBackupSets.do?LoginName=$username";
$result = $this->runQuery($url);
// If that didn't happen
$this->errorHandler($result, "Problem during getUserBackupSets() for '$username'.");
return $this->decodeResult($result);
}
// Get storage statistics for a particular user
public function getUserStorageStats($username, $date)
{
$this->debuglog("Getting storage stats for user '$username'");
$url = "/GetUserStorageStat.do?LoginName=$username&YearMonth=$date";
$this->debuglog($url);
$result = $this->runQuery($url);
// If that didn't happen
$this->errorHandler($result, "Problem during getUserStorageStats() for '$username'.");
return $this->decodeResult($result);
}
// Get all backup jobs for a particular user
public function getUserBackupJobs($username)
{
$this->debuglog("Getting backup jobs for user '$username'");
$url = "/ListBackupJobs.do?LoginName=$username";
$result = $this->runQuery($url);
// If that didn't happen
$this->errorHandler($result, "Problem during getUserBackupJobs() for '$username'.");
return $this->decodeResult($result);
}
// Get all backup jobs for a particular user, limited to a particular backup set
public function getBackupJobsForSet($username, $backupset)
{
$this->debuglog("Getting backup jobs for user '$username', for backup set with id '$backupset'");
$url = "/ListBackupJobs.do?LoginName=$username";
$result = $this->runQuery($url);
// If that didn't happen
$this->errorHandler($result, "Problem during getBackupJobsForSet() for '$username', for backup set with id '$backupset'.");
$data = $this->decodeResult($result);
if ($this->serverVersion == '6') {
foreach ($data->children() as $set) {
// If this is the backupset we are interested in
if ($set['ID'] == $backupset) {
$this->debuglog(sizeof($set)." job(s) found for set '$backupset'");
// Go through each job id
foreach ($set->BackupJob as $job) {
$backupJobs[] = (string) $job['ID'];
}
return $backupJobs;
}
}
} elseif ($this->serverVersion == '7') {
foreach ($data->Data as $set) {
// If this is the backupset we are interested in
if ($set->BackupSetID == $backupset) {
$this->debuglog(sizeof($set->BackupJob)." job(s) found for set '$backupset'");
return $set->BackupJob;
}
}
}
// If we get to here then that backup set obviously doesn't exist!
$this->errorHandler($result, "Problem doing getBackupJobsForSet() - looks like set '$backupset' doesn't exist");
}
// Get the IDs of each backup job for this set in reverse order
public function getBackupSetJobIds($username, $backupset, $rev)
{
if(isset($rev) === false) {
$rev = false;
}
$this->debuglog("Getting list of backup job ids for user '$username', for backup set with id '$backupset'");
// Get a list of all backup jobs for this backup set
$jobs = $this->getBackupJobsForSet($username, $backupset);
if (sizeof($jobs) <= 0) {
throw new Exception("Could not run getUserBackupJobsForSet() in getBackupSetJobIds() for backup set id '$backupset'.");
}
// Sort in reverse?
if ($rev) {
rsort($jobs);
}
if (!$rev) {
sort($jobs);
}
return $jobs;
}
// Get the ID of the most recent job for this backup set
public function getMostRecentBackupJob($username, $backupset)
{
$this->debuglog("Running getMostRecentBackupJob() for backup set with id '$backupset'");
// Get a list of all backup jobs for this backup set (in reverse order)
$jobs = $this->getBackupSetJobIds($username, $backupset, true);
if (!$jobs) {
throw new Exception("Could not run getBackupSetJobIds() in getMostRecentBackupJob() for backup set id '$backupset'.");
}
// Return just the most recent
return $jobs[0];
}
// Get all backup jobs for a particular user
public function getUserBackupJobDetails($username, $backupset, $backupjob, $summary)
{
if(isset($summary) === false) {
$summary = false;
}
$destinationid = $this->getDestinationID($username, $backupset, $backupjob);
if(isset($destinationid) === false) {
$destinationid='0';
}
$this->debuglog("Getting backup job details for user '$username', job id '$backupjob'");
if($summary === false) {
$url = "/GetBackupJobReport.do?LoginName=$username&BackupSetID=$backupset&BackupJobID=$backupjob&DestinationID=$destinationid";
} else {
$url = "/GetBackupJobReportSummary.do?LoginName=$username&BackupSetID=$backupset&BackupJobID=$backupjob&DestinationID=$destinationid";
}
$result = $this->runQuery($url);
// If that didn't happen
$this->errorHandler($result, "Problem during getUserBackupJobDetails() for '$username', job id '$backupjob'. $result");
return $this->decodeResult($result);
}
// Get details on a particular backup set
public function getUserBackupSet($username, $backupset)
{
$this->debuglog("Getting details for backup set with id '$setid' for user '$username'");
$url = "/GetBackupSet.do?LoginName=$username&BackupSetID=$backupset";
$result = $this->runQuery($url);
// If that didn't happen
$this->errorHandler($result, "Problem during getUserBackupSet() for $username. $result");
return $this->decodeResult($result);
}
// Get status of a user backup job for a particular date in (yyyy-MM-dd format)
public function listBackupJobStatus($username, $date)
{
if (isset($date) === false) {
$date = date("Y-m-d");
}
$this->debuglog("Getting status for backup job that run on the '$date' for user '$username'");
$url = "/ListBackupJobStatus.do?LoginName=$username&BackupDate=$date";
$result = $this->runQuery($url);
if(json_decode($result)->Status == 'Error') {
if(json_decode($result)->Message == '[Error] No Backup Job on that day') {
return 'No Backup Job on that day';
}
}
// If that didn't happen
$this->errorHandler($result, "Problem during listBackupJobStatus() for $username. $result");
return $this->decodeResult($result);
}
// Retrieve the DestinationID of a backup job for a particular backupset of a given user
public function getDestinationID($username, $backupset, $backupjob)
{
$date = substr($backupjob, 0, 10);
$this->debuglog("Getting the destination of the backup job '$backupjob' for user '$username'");
$data = $this->listBackupJobStatus($username, $date);
if ($this->serverVersion === '6') {
foreach($data->children() as $backupjobstatus) {
if (strval($backupjobstatus['BackupSetID']) === $backupset && strval($backupjobstatus['ID']) === $backupjob) {
$this->debuglog("Destination found for backup set '$backupset' and backup job '$backupjob'");
$result = $backupjobstatus['DestinationID'];
}
return $result;
}
} elseif ($this->serverVersion === '7') {
foreach($data->Data as $backupjobstatus) {
if ($backupjobstatus->BackupSetID === $backupset && $backupjobstatus->ID === $backupjob) {
$this->debuglog("Destination found for backup set '$backupset' and backup job '$backupjob'");
$result = $backupjobstatus->DestinationID;
}
}
return $result;
}
}
// Run an API query against OBS
public function runQuery($url)
{
try {
if ($this->serverVersion === '6') {
$url = $this->serverAddress.'/obs/api'.$url;
// If this URL already has a query string
if (strstr($url, '?')) {
$url .= '&SysUser='.$this->serverAdminUsername.'&SysPwd='.$this->serverAdminPassword;
}
if (!strstr($url, '?')) {
$url .= '?SysUser='.$this->serverAdminUsername.'&SysPwd='.$this->serverAdminPassword;
}
$this->debuglog("Trying $url");
$result = file_get_contents($url);
} elseif ($this->serverVersion === '7') {
if (strstr($url, '?')) {
$args=explode('&', substr($url, strpos($url, '?') +1));
$url = $this->serverAddress.'/obs/api/json'.strstr($url, '?', TRUE);
foreach ($args as $arg) {
list($k, $v) = explode('=', $arg);
$postData[$k] = $v;
}
} elseif (!strstr($url, '?')) {
$url = $this->serverAddress.'/obs/api/json'. $url;
$postData = array();
}
$postData['SysUser'] = $this->serverAdminUsername;
$postData['SysPwd'] = $this->serverAdminPassword;
$curlreq = curl_init($url);
curl_setopt_array($curlreq, array (
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array (
'Authorization: Ahsay OBS JSON API v7.X',
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($postData)
)
);
$this->debuglog("Trying $url");
$result = curl_exec($curlreq);
} else {
throw new Exception("Please specify server version between 6 or 7 !\r\n");
}
return $result;
}
catch (Exception $e) {
throw new Exception("Error accessing API: ".$e->GetMessage());
}
}
// Decode XML from Ahsay OBS 6.X API and JSON from Ahsay version 7.X API
public function decodeResult($result)
{
try {
if ($this->serverVersion === '6') {
return simplexml_load_string($result);
} elseif ($this->serverVersion === '7') {
return json_decode($result);
} else {
throw new Exception("Please specify server version between 6 or 7 !\e\n");
}
}
catch (Exception $e) {
throw new Exception("Error accessing API: ".$e->GetMessage());
}
}
// Handle error returned from the OBS API
public function errorHandler($result, $message)
{
try {
if ($this->serverVersion === '6') {
if (substr($result, 1, 3) === 'err') {
throw new Exception($message . "\r\n" . $result . "\r\n");
}
} elseif ($this->serverVersion === '7') {
$status=json_decode($result, TRUE);
if ($status['Status'] === 'Error') {
throw new Exception($message . "\r\n" . $result . "\r\n");
}
} else {
throw new Exception("Please specify server version between 6 or 7 !\r\n");
}
}
catch (Exception $e) {
throw new Exception("Error accessing API: ".$e->GetMessage());
}
}
public function formatBytes($bytes, $precision = 2)
{
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
// Uncomment one of the following alternatives
$bytes /= pow(1024, $pow);
// $bytes /= (1 << (10 * $pow));
return round($bytes, $precision).' '.$units[$pow];
}
// Debug logging
public function debuglog($message)
{
if ($this->debug) {
printf("%s\n", $message);
}
}
}