-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswordappclient.php
143 lines (124 loc) · 4.86 KB
/
swordappclient.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
<?php
require("swordappservicedocument.php");
require("swordappentry.php");
require("swordapperrordocument.php");
require("swordapplibraryversion.php");
require("swordexception.php");
class SWORDAPPClient {
// Request a servicedocument at the specified url, with the specified credentials,
// and on-behalf-of the specified user.
function servicedocument($sac_url, $sac_u, $sac_p, $sac_obo) {
// Get the service document
$sac_curl = curl_init();
curl_setopt($sac_curl, CURLOPT_RETURNTRANSFER, true);
// To see debugging infomation, un-comment the following line
//curl_setopt($sac_curl, CURLOPT_VERBOSE, 1);
curl_setopt($sac_curl, CURLOPT_URL, $sac_url);
if(!empty($sac_u) && !empty($sac_p)) {
curl_setopt($sac_curl, CURLOPT_USERPWD, $sac_u . ":" . $sac_p);
}
$headers = array();
global $sal_version;
array_push($headers, "User-Agent: SWORDAPP PHP library (version " . $sal_version . ") " .
"http://php.swordapp.org/");
if (!empty($sac_obo)) {
array_push($headers, "X-On-Behalf-Of: " . $sac_obo);
}
curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
$sac_resp = curl_exec($sac_curl);
$sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
curl_close($sac_curl);
// Parse the result
if ($sac_status == 200) {
try {
$sac_sdresponse = new SWORDAPPServiceDocument($sac_url, $sac_status, $sac_resp);
} catch (Exception $e) {
throw new Exception("Error parsing service document (" . $e->getMessage() . ")");
}
} else {
$sac_sdresponse = new SWORDAPPServiceDocument($sac_url, $sac_status);
}
// Return the servicedocument object
return $sac_sdresponse;
}
// Perform a deposit to the specified url, with the sepcified credentials,
// on-behlf-of the specified user, and with the given file and formatnamespace and noop setting
function deposit($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname,
$sac_packaging= '', $sac_contenttype = '',
$sac_noop = false, $sac_verbose = false) {
// Perform the deposit
$sac_curl = curl_init();
// To see debugging infomation, un-comment the following line
//curl_setopt($sac_curl, CURLOPT_VERBOSE, 1);
curl_setopt($sac_curl, CURLOPT_URL, $sac_url);
curl_setopt($sac_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($sac_curl, CURLOPT_POST, true);
if(!empty($sac_u) && !empty($sac_p)) {
curl_setopt($sac_curl, CURLOPT_USERPWD, $sac_u . ":" . $sac_p);
}
$headers = array();
global $sal_version;
array_push($headers, "User-Agent: SWORDAPP PHP library (version " . $sal_version . ") " .
"http://php.swordapp.org/");
$sac_md5 = md5_file($sac_fname);
array_push($headers, "Content-MD5: " . $sac_md5);
if (!empty($sac_obo)) {
array_push($headers, "X-On-Behalf-Of: " . $sac_obo);
}
if (!empty($sac_packaging)) {
array_push($headers, "X-Packaging: " . $sac_packaging);
}
if (!empty($sac_contenttype)) {
array_push($headers, "Content-Type: " . $sac_contenttype);
}
array_push($headers, "Content-Length: " . filesize($sac_fname));
if ($sac_noop == true) {
array_push($headers, "X-No-Op: true");
}
if ($sac_verbose == true) {
array_push($headers, "X-Verbose: true");
}
$index = strpos(strrev($sac_fname), '/');
if ($index) {
$index = strlen($sac_fname) - $index;
$sac_fname_trimmed = substr($sac_fname, $index);
} else {
$sac_fname_trimmed = $sac_fname;
}
array_push($headers, "Content-Disposition: filename=" . $sac_fname_trimmed);
curl_setopt($sac_curl, CURLOPT_READDATA, fopen($sac_fname, 'rb'));
curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
$sac_resp = curl_exec($sac_curl);
$sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
curl_close($sac_curl);
// Parse the result
$sac_dresponse = new SWORDAPPEntry($sac_status, $sac_resp);
// Was it a succesful result?
if (($sac_status >= 200) && ($sac_status < 300)) {
try {
// Get the deposit results
$sac_xml = @new SimpleXMLElement($sac_resp);
$sac_ns = $sac_xml->getNamespaces(true);
// Build the deposit response object
$sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
} catch (Exception $e) {
throw new SwordHttpException($sac_status, "Error parsing response entry (" . $e->getMessage() . ")", $sac_resp);
}
} else {
try {
// Parse the result
$sac_dresponse = new SWORDAPPErrorDocument($sac_status, $sac_resp);
// Get the deposit results
$sac_xml = @new SimpleXMLElement($sac_resp);
$sac_ns = $sac_xml->getNamespaces(true);
// Build the deposit response object
$sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
} catch (Exception $e) {
throw new SwordHttpException($sac_status, "Error parsing error document (" . $e->getMessage() . ")", $sac_resp);
}
}
// Return the deposit object
return $sac_dresponse;
}
}
?>