Skip to content

Commit 50f2193

Browse files
committed
Initial commit version 1.00
Initial version by Richard Bishop ([email protected])
1 parent de34054 commit 50f2193

File tree

1 file changed

+383
-0
lines changed

1 file changed

+383
-0
lines changed

ahsay-api-wrapper.php

Lines changed: 383 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,383 @@
1+
<?php
2+
3+
/*
4+
5+
Ahsay OBS API functions wrapper. Version 1.00
6+
7+
Copyright (c) Richard Bishop ([email protected]) 2008-2009.
8+
9+
This program is free software: you can redistribute it and/or modify
10+
it under the terms of the GNU General Public License as published by
11+
the Free Software Foundation, either version 3 of the License, or
12+
(at your option) any later version.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+
23+
Note:
24+
25+
Some API calls are known to be missing, I have no requirement for these
26+
and will add them when I have time. You are welcome to write the code
27+
for these functions yourself and submit the necessary code to me for
28+
inclusion in future releases of this library.
29+
30+
31+
You will require a copy of clsParseXML.php - this turns the XML returned
32+
by OBS into multi-dimension PHP associative array structures and makes
33+
things much easier to parse. Unfortunately there doesn't seem to be
34+
anywhere to download this from anymore - try searching Google for a copy.
35+
36+
*/
37+
38+
require_once 'clsParseXML.php';
39+
40+
class AhsayApiFunctions
41+
{
42+
public $server_name;
43+
public $server_port;
44+
public $server_user;
45+
public $server_pass;
46+
public $debug;
47+
public $error;
48+
49+
/*
50+
Note:
51+
All times (user added, backupset last run, completed etc) are in the form of Unix timestamps. In the case of Java this is the
52+
number of milliseconds since Jan 1st 1970; though PHP counts this as seconds since Jan 1st 1970. The solution is to disregard
53+
the final 3 digits of the value output by OBS
54+
*/
55+
56+
// Constructor
57+
public function AhsayApiFunctions($server, $port, $username, $password)
58+
{
59+
$this->server_name = $server;
60+
$this->server_port = $port;
61+
$this->server_user = $username;
62+
$this->server_pass = $password;
63+
$this->debug;
64+
}
65+
66+
// Enable/disable debugging
67+
public function debug($which)
68+
{
69+
$this->debug = $which;
70+
}
71+
72+
// Authenticate a user against OBS
73+
public function authenticateUser($username, $password)
74+
{
75+
$this->debuglog("Authenticate user $username");
76+
77+
$url = '/obs/api/AuthUser.do?';
78+
$url .= 'LoginName='.$username.'&Password='.$password;
79+
$result = $this->__runQuery($url);
80+
81+
// If that didn't happen
82+
if (substr($result, 0, 4) == 'Err:') {
83+
$this->debuglog("Authenticate user failed $result");
84+
$this->error = $result;
85+
86+
return false;
87+
} else {
88+
return 'OK';
89+
}
90+
}
91+
92+
// Get a particular user
93+
public function getUser($username)
94+
{
95+
$this->debuglog("Getting user '$username'");
96+
97+
$url = "/obs/api/GetUser.do?LoginName=$username";
98+
$result = $this->__runQuery($url);
99+
100+
// If that didn't happen
101+
if (substr($result, 0, 4) == 'Err:') {
102+
$this->debuglog("No user details found for '$username'");
103+
$this->error = $result;
104+
105+
return false;
106+
} else {
107+
return $this->xmlToArray($result);
108+
}
109+
}
110+
111+
// Get an array of all users
112+
public function getUsers()
113+
{
114+
$this->debuglog('Getting user list');
115+
116+
$url = '/obs/api/ListUsers.do';
117+
$result = $this->__runQuery($url);
118+
119+
// If that didn't happen
120+
if (substr($result, 0, 4) == 'Err:') {
121+
$this->debuglog("Doing getUsers() failed $result");
122+
$this->error = $result;
123+
124+
return false;
125+
} else {
126+
return $this->xmlToArray($result);
127+
}
128+
}
129+
130+
// Get all backup sets for a particular user
131+
public function getUserBackupSets($username)
132+
{
133+
$this->debuglog("Getting backup sets for user '$username'");
134+
135+
$url = "/obs/api/ListBackupSets.do?LoginName=$username";
136+
$result = $this->__runQuery($url);
137+
138+
// If that didn't happen
139+
if (substr($result, 0, 4) == 'Err:') {
140+
$this->debuglog("Problem during getUserBackupSets() for '$username'");
141+
$this->error = $result;
142+
143+
return false;
144+
} else {
145+
return $this->xmlToArray($result);
146+
}
147+
}
148+
149+
// Get storage statistics for a particular user
150+
public function getUserStorageStats($username, $date)
151+
{
152+
$this->debuglog("Getting backup jobs for user '$username'");
153+
154+
$url = "/obs/api/GetUserStorageStat.do?LoginName=$username&YearMonth=$date";
155+
$result = $this->__runQuery($url);
156+
157+
// If that didn't happen
158+
if (substr($result, 0, 4) == 'Err:') {
159+
$this->debuglog("Problem during getUserStorageStats() for '$username'");
160+
$this->error = $result;
161+
162+
return false;
163+
} else {
164+
return $this->xmlToArray($result);
165+
}
166+
}
167+
168+
// Get all backup jobs for a particular user
169+
public function getUserBackupJobs($username)
170+
{
171+
$this->debuglog("Getting backup jobs for user '$username'");
172+
173+
$url = "/obs/api/ListBackupJobs.do?LoginName=$username";
174+
$result = $this->__runQuery($url);
175+
176+
// If that didn't happen
177+
if (substr($result, 0, 4) == 'Err:') {
178+
$this->debuglog("Problem during getUserBackupJobs() for '$username'");
179+
$this->error = $result;
180+
181+
return false;
182+
} else {
183+
return $this->xmlToArray($result);
184+
}
185+
}
186+
187+
// Get all backup jobs for a particular user, limited to a particular backup set
188+
public function getBackupJobsForSet($username, $backupset)
189+
{
190+
$this->debuglog("Getting backup jobs for user '$username', for backup set with id '$backupset'");
191+
192+
$url = "/obs/api/ListBackupJobs.do?LoginName=$username";
193+
$result = $this->__runQuery($url);
194+
195+
// If that didn't happen
196+
if (substr($result, 0, 4) == 'Err:') {
197+
$this->debuglog("Problem during getBackupJobsForSet() for '$username', for backup set with id '$backupset'");
198+
$this->error = $result;
199+
200+
return false;
201+
} else {
202+
$data = $this->xmlToArray($result);
203+
204+
foreach ($data['BACKUPSETS'][0]['BACKUPSET'] as $set) {
205+
206+
// If this is the backupset we are interested in
207+
if ($set['ATTRIBUTES']['ID'] == $backupset) {
208+
return $set;
209+
}
210+
}
211+
212+
// If we get to here then that backup set obviously doesn't exist!
213+
$this->debuglog("Problem doing getBackupJobsForSet() - looks like set '$backupset' doesn't exist");
214+
215+
return false;
216+
}
217+
}
218+
219+
// Get the IDs of each backup job for this set in reverse order
220+
public function getBackupSetJobIds($username, $backupset, $rev = false)
221+
{
222+
$backup_sets = array();
223+
224+
$this->debuglog("Getting list of backup job ids for user '$username', for backup set with id '$backupset'");
225+
226+
// Get a list of all backup jobs for this backup set
227+
$jobs = $this->getBackupJobsForSet($username, $backupset);
228+
if ($jobs == false) {
229+
$this->debuglog("Could not run getUserBackupJobsForSet() in getBackupSetJobIds() for backup set id '$id'");
230+
231+
return false;
232+
}
233+
234+
// Go through each job id
235+
foreach ($jobs['BACKUPJOB'] as $job) {
236+
$backup_sets[] = $job['ATTRIBUTES']['ID'];
237+
}
238+
239+
// Sort in reverse?
240+
if ($rev != false) {
241+
rsort($backup_sets);
242+
} else {
243+
sort($backup_sets);
244+
}
245+
246+
return $backup_sets;
247+
}
248+
249+
// Get the ID of the most recent job for this backup set
250+
public function getMostRecentBackupJob($username, $backupset)
251+
{
252+
$this->debuglog("Running getMostRecentBackupJob() for backup set with id '$backupset'");
253+
254+
// Get a list of all backup jobs for this backup set (in reverse order)
255+
$jobs = $this->getBackupSetJobIds($username, $backupset, true);
256+
if ($jobs == false) {
257+
$this->debuglog("Could not run getBackupSetJobIds() in getMostRecentBackupJob() for backup set id '$id'");
258+
259+
return false;
260+
}
261+
262+
// Return just the most recent
263+
return $jobs[0];
264+
}
265+
266+
// Get all backup jobs for a particular user
267+
public function getUserBackupJobDetails($username, $backupset, $backupjob)
268+
{
269+
$this->debuglog("Getting backup job details for user '$username', job id '$backupjob'");
270+
271+
$url = "/obs/api/GetBackupJobReport.do?LoginName=$username&BackupSetID=$backupset&BackupJobID=$backupjob";
272+
$result = $this->__runQuery($url);
273+
274+
// If that didn't happen
275+
if (substr($result, 0, 4) == 'Err:') {
276+
$this->debuglog("Problem during getUserBackupJobDetails() for '$username', job id '$backupjob'");
277+
$this->error = $result;
278+
279+
return false;
280+
} else {
281+
return $this->xmlToArray($result);
282+
}
283+
}
284+
285+
// Get details on a particular backup set
286+
public function getUserBackupSet($username, $setid)
287+
{
288+
$this->debuglog("Getting details for backup set with id '$setid' for user '$username'");
289+
290+
$url = "/obs/api/GetBackupSet.do?LoginName=$username&BackupSetID=$setid";
291+
$result = $this->__runQuery($url);
292+
293+
// If that didn't happen
294+
if (substr($result, 0, 4) == 'Err:') {
295+
$this->debuglog("Problem during getUserBackupSet() for $username");
296+
$this->error = $result;
297+
298+
return false;
299+
} else {
300+
return $this->xmlToArray($result);
301+
}
302+
}
303+
304+
// Run an API query against OBS
305+
public function __runQuery($url)
306+
{
307+
308+
// If this URL already has a query string
309+
if (strstr($url, '?')) {
310+
$url .= '&SysUser='.$this->server_user.'&SysPwd='.$this->server_pass;
311+
} else {
312+
$url .= '?SysUser='.$this->server_user.'&SysPwd='.$this->server_pass;
313+
}
314+
315+
// Generate HTTP headers for this request
316+
$headers = 'GET '.$url." HTTP/1.1\r\n";
317+
$headers .= 'Host: '.$this->server_name."\r\n";
318+
$headers .= "Connection: close\r\n";
319+
$headers .= "\r\n\r\n";
320+
321+
$this->debuglog('Attempting connection to '.$this->server_name.' on port '.$this->server_port);
322+
323+
// Try to connect to the server
324+
if (!($fp = @fsockopen($this->server_name, $this->server_port, $errno, $errstr, 30))) {
325+
$this->debuglog('Connection failed. Error: '.$errstr.'('.$errno.')');
326+
327+
return 'Err: ConnectionFail';
328+
}
329+
330+
// Drop the HTTP headers to the server
331+
$this->debuglog('Sending HTTP headers to server');
332+
fwrite($fp, $headers);
333+
334+
// Keep getting data back
335+
$this->debuglog('Reading data from server');
336+
while (!feof($fp)) {
337+
$data = fgets($fp, 128);
338+
$result .= $data;
339+
}
340+
341+
// Was there any data
342+
if (!$result) {
343+
$this->debuglog('Server sent empty response');
344+
345+
return 'Err: NoDataReceived';
346+
}
347+
348+
$this->debuglog('Got all data from server');
349+
350+
// Strip the headers from the reply
351+
$b_start = strpos($result, "\r\n\r\n");
352+
$result = trim(substr($result, $b_start));
353+
354+
// Was there an error?
355+
if (substr($result, 0, 5) == '<err>') {
356+
$this->debuglog('Server sent error: '.$result);
357+
358+
return 'Err: '.$result;
359+
}
360+
361+
// Return whatever the server said
362+
return $result;
363+
}
364+
365+
// Convert XML to an array
366+
public function xmlToArray($xml)
367+
{
368+
$xmlparse = &new ParseXML();
369+
$data = $xmlparse->GetXMLDataTree($xml);
370+
371+
return $data;
372+
}
373+
374+
// Debug logging
375+
public function debuglog($message)
376+
{
377+
if ($this->debug) {
378+
printf("%s\n", $message);
379+
}
380+
}
381+
}
382+
383+
?>

0 commit comments

Comments
 (0)