3 require(
"swordappservicedocument.php");
4 require(
"swordappentry.php");
5 require(
"swordappresponse.php");
6 require(
"swordappstatement.php");
7 require(
"swordapperrordocument.php");
8 require(
"swordapplibraryuseragent.php");
10 require_once(
"utils.php");
14 private $debug =
false;
15 private $curl_opts = array();
18 $this->curl_opts = $curl_opts;
25 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
28 global $sal_useragent;
29 array_push($headers, $sal_useragent);
30 if (!empty($sac_obo)) {
31 array_push($headers,
"X-On-Behalf-Of: " . $sac_obo);
33 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
34 $sac_resp = curl_exec($sac_curl);
35 $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
36 curl_close($sac_curl);
39 if ($sac_status == 200) {
42 }
catch (Exception $e) {
43 throw new Exception(
"Error parsing service document (" . $e->getMessage() .
")");
50 return $sac_sdresponse;
55 function deposit($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname,
56 $sac_packaging=
'', $sac_contenttype =
'', $sac_inprogress =
false) {
58 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
60 curl_setopt($sac_curl, CURLOPT_POST,
true);
63 global $sal_useragent;
64 array_push($headers, $sal_useragent);
65 array_push($headers,
"Content-MD5: " . md5_file($sac_fname));
66 if (!empty($sac_obo)) {
67 array_push($headers,
"On-Behalf-Of: " . $sac_obo);
69 if (!empty($sac_packaging)) {
70 array_push($headers,
"Packaging: " . $sac_packaging);
72 if (!empty($sac_contenttype)) {
73 array_push($headers,
"Content-Type: " . $sac_contenttype);
75 array_push($headers,
"Content-Length: " . filesize($sac_fname));
76 if ($sac_inprogress) {
77 array_push($headers,
"In-Progress: true");
79 array_push($headers,
"In-Progress: false");
83 $index = strpos(strrev($sac_fname),
'/');
84 if ($index !==
false) {
85 $index = strlen($sac_fname) - $index;
86 $sac_fname_trimmed = substr($sac_fname, $index);
88 $sac_fname_trimmed = $sac_fname;
90 array_push($headers,
"Content-Disposition: attachment; filename=" . $sac_fname_trimmed);
91 curl_setopt($sac_curl, CURLOPT_READDATA, fopen($sac_fname,
'rb'));
92 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
94 $sac_resp = curl_exec($sac_curl);
95 $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
96 curl_close($sac_curl);
102 if (($sac_status >= 200) && ($sac_status < 300)) {
105 $sac_xml = @
new SimpleXMLElement($sac_resp);
106 $sac_ns = $sac_xml->getNamespaces(
true);
109 $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
110 }
catch (Exception $e) {
111 throw new Exception(
"Error parsing response entry (" . $e->getMessage() .
")");
119 $sac_xml = @
new SimpleXMLElement($sac_resp);
120 $sac_ns = $sac_xml->getNamespaces(
true);
123 $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
124 }
catch (Exception $e) {
125 throw new Exception(
"Error parsing error document (" . $e->getMessage() .
")");
130 return $sac_dresponse;
135 $sac_inprogress =
false) {
137 return$this->depositMultipartByMethod($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package,
138 "POST", $sac_inprogress);
139 }
catch (Exception $e) {
145 function depositAtomEntry($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname, $sac_inprogress =
false) {
146 return $this->depositAtomEntryByMethod($sac_url, $sac_u, $sac_p, $sac_obo,
147 "POST", $sac_fname, $sac_inprogress);
153 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
155 curl_setopt($sac_curl, CURLOPT_POST,
true);
158 global $sal_useragent;
159 array_push($headers, $sal_useragent);
160 if (!empty($sac_obo)) {
161 array_push($headers,
"On-Behalf-Of: " . $sac_obo);
163 array_push($headers,
"Content-Length: 0");
164 array_push($headers,
"In-Progress: false");
166 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
168 $sac_resp = curl_exec($sac_curl);
169 $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
170 curl_close($sac_curl);
176 return $sac_response;
180 function retrieveContent($sac_url, $sac_u, $sac_p, $sac_obo, $sac_accept_packaging =
"") {
182 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
185 global $sal_useragent;
186 array_push($headers, $sal_useragent);
187 if (!empty($sac_obo)) {
188 array_push($headers,
"X-On-Behalf-Of: " . $sac_obo);
190 if (!empty($sac_accept_packaging)) {
191 array_push($headers,
"Accept-Packaging: " . $sac_accept_packaging);
193 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
194 $sac_resp = curl_exec($sac_curl);
195 curl_close($sac_curl);
204 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
207 global $sal_useragent;
208 array_push($headers, $sal_useragent);
209 if (!empty($sac_obo)) {
210 array_push($headers,
"X-On-Behalf-Of: " . $sac_obo);
212 if (!empty($sac_accept_packaging)) {
213 array_push($headers,
"Accept-Packaging: " . $sac_accept_packaging);
215 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
216 $sac_resp = curl_exec($sac_curl);
217 $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
218 curl_close($sac_curl);
224 if (($sac_status >= 200) && ($sac_status < 300)) {
227 $sac_xml = @
new SimpleXMLElement($sac_resp);
228 $sac_ns = $sac_xml->getNamespaces(
true);
231 $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
232 }
catch (Exception $e) {
233 throw new Exception(
"Error parsing response entry (" . $e->getMessage() .
")");
241 $sac_xml = @
new SimpleXMLElement($sac_resp);
242 $sac_ns = $sac_xml->getNamespaces(
true);
245 $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
246 }
catch (Exception $e) {
247 throw new Exception(
"Error parsing error document (" . $e->getMessage() .
")");
252 return $sac_dresponse;
257 $sac_packaging=
'', $sac_contenttype =
'', $sac_metadata_relevant =
false) {
259 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
261 curl_setopt($sac_curl, CURLOPT_PUT,
true);
264 global $sal_useragent;
265 array_push($headers, $sal_useragent);
266 array_push($headers,
"Content-MD5: " . md5_file($sac_fname));
267 if (!empty($sac_obo)) {
268 array_push($headers,
"On-Behalf-Of: " . $sac_obo);
270 if (!empty($sac_packaging)) {
271 array_push($headers,
"Packaging: " . $sac_packaging);
273 if (!empty($sac_contenttype)) {
274 array_push($headers,
"Content-Type: " . $sac_contenttype);
276 if ($sac_metadata_relevant) {
277 array_push($headers,
"Metadata-Relevant: true");
279 array_push($headers,
"Metadata-Relevant: false");
283 $index = strpos(strrev($sac_fname),
'/');
284 if ($index !==
false) {
285 $index = strlen($sac_fname) - $index;
286 $sac_fname_trimmed = substr($sac_fname, $index);
288 $sac_fname_trimmed = $sac_fname;
290 array_push($headers,
"Content-Disposition: attachment; filename=" . $sac_fname_trimmed);
291 curl_setopt($sac_curl, CURLOPT_INFILE, fopen($sac_fname,
'rb'));
292 curl_setopt($sac_curl, CURLOPT_INFILESIZE, filesize($sac_fname));
293 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
295 $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
296 curl_close($sac_curl);
299 if ($sac_status != 204) {
300 throw new Exception(
"Error replacing file (HTTP code: " . $sac_status .
")");
307 function replaceMetadata($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname, $sac_inprogress =
false) {
308 return $this->depositAtomEntryByMethod($sac_url, $sac_u, $sac_p, $sac_obo,
309 "PUT", $sac_fname, $sac_inprogress);
314 $sac_inprogress =
false) {
315 return $this->depositMultipartByMethod($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package,
316 "PUT", $sac_inprogress);
321 $sac_contenttype =
'', $sac_metadata_relevant =
false) {
323 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
325 curl_setopt($sac_curl, CURLOPT_POST,
true);
328 global $sal_useragent;
329 array_push($headers, $sal_useragent);
330 array_push($headers,
"Content-MD5: " . md5_file($sac_fname));
331 if (!empty($sac_obo)) {
332 array_push($headers,
"On-Behalf-Of: " . $sac_obo);
334 if (!empty($sac_contenttype)) {
335 array_push($headers,
"Content-Type: " . $sac_contenttype);
337 if ($sac_metadata_relevant) {
338 array_push($headers,
"Metadata-Relevant: true");
340 array_push($headers,
"Metadata-Relevant: false");
342 array_push($headers,
"Content-Length: " . filesize($sac_fname));
345 $index = strpos(strrev($sac_fname),
'/');
346 if ($index !==
false) {
347 $index = strlen($sac_fname) - $index;
348 $sac_fname_trimmed = substr($sac_fname, $index);
350 $sac_fname_trimmed = $sac_fname;
352 array_push($headers,
"Content-Disposition: attachment; filename=" . $sac_fname_trimmed);
354 curl_setopt($sac_curl, CURLOPT_READDATA, fopen($sac_fname,
'rb'));
355 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
357 $sac_resp = curl_exec($sac_curl);
358 $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
359 curl_close($sac_curl);
365 if (($sac_status >= 200) && ($sac_status < 300)) {
373 }
catch (Exception $e) {
374 throw new Exception(
"Error parsing response entry (" . $e->getMessage() .
")");
387 }
catch (Exception $e) {
388 throw new Exception(
"Error parsing error document (" . $e->getMessage() .
")");
392 return $sac_dresponse;
397 $sac_packaging =
'', $sac_contenttype, $sac_inprogress =
false) {
398 return $this->
deposit($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname,
399 $sac_packaging, $sac_contenttype, $sac_inprogress);
403 function addExtraAtomEntry($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname, $sac_inprogress =
false) {
404 return $this->depositAtomEntryByMethod($sac_url, $sac_u, $sac_p, $sac_obo,
405 "POST", $sac_fname, $sac_inprogress);
410 $sac_inprogress =
false) {
411 return $this->depositMultipartByMethod($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package,
412 "POST", $sac_inprogress);
418 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
420 curl_setopt($sac_curl, CURLOPT_CUSTOMREQUEST,
"DELETE");
423 global $sal_useragent;
424 array_push($headers, $sal_useragent);
425 if (!empty($sac_obo)) {
426 array_push($headers,
"On-Behalf-Of: " . $sac_obo);
429 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
431 $sac_resp = curl_exec($sac_curl);
432 $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
433 curl_close($sac_curl);
446 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
449 global $sal_useragent;
450 array_push($headers, $sal_useragent);
451 if (!empty($sac_obo)) {
452 array_push($headers,
"X-On-Behalf-Of: " . $sac_obo);
454 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
455 $sac_resp = curl_exec($sac_curl);
456 $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
457 curl_close($sac_curl);
460 if ($sac_status == 200) {
463 }
catch (Exception $e) {
464 throw new Exception(
"Error parsing statement (" . $e->getMessage() .
")");
471 return $sac_atomstatement;
478 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
481 global $sal_useragent;
482 array_push($headers, $sal_useragent);
483 if (!empty($sac_obo)) {
484 array_push($headers,
"X-On-Behalf-Of: " . $sac_obo);
486 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
487 $sac_resp = curl_exec($sac_curl);
488 curl_close($sac_curl);
495 private function curl_init($sac_url, $sac_user, $sac_password) {
497 $sac_curl = curl_init();
500 curl_setopt($sac_curl, CURLOPT_RETURNTRANSFER,
true);
503 curl_setopt($sac_curl, CURLOPT_VERBOSE, $this->debug);
506 curl_setopt($sac_curl, CURLOPT_URL, $sac_url);
509 if(!empty($sac_user) && !empty($sac_password)) {
510 curl_setopt($sac_curl, CURLOPT_USERPWD, $sac_user .
":" . $sac_password);
514 foreach ($this->curl_opts as $opt => $val) {
515 curl_setopt($sac_curl, $opt, $val);
523 private function depositMultipartByMethod($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package, $sac_method,
524 $sac_inprogress =
false) {
525 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p, $sac_obo);
529 if ($sac_inprogress) {
530 array_push($headers,
"In-Progress: true");
532 array_push($headers,
"In-Progress: false");
535 if (!empty($sac_obo)) {
536 array_push($headers,
"On-Behalf-Of: " . $sac_obo);
539 array_push($headers,
"Content-Type: multipart/related; boundary=\"===============SWORDPARTS==\"; type=\"application/atom+xml\"");
542 if ($sac_method ==
"PUT") {
543 curl_setopt($sac_curl, CURLOPT_PUT,
true);
544 curl_setopt($sac_curl, CURLOPT_INFILE, fopen($sac_package,
'rb'));
545 curl_setopt($sac_curl, CURLOPT_INFILESIZE, filesize($sac_package));
547 curl_setopt($sac_curl, CURLOPT_POST,
true);
548 curl_setopt($sac_curl, CURLOPT_CONNECTTIMEOUT, 30);
549 curl_setopt($sac_curl, CURLOPT_LOW_SPEED_LIMIT, 1);
550 curl_setopt($sac_curl, CURLOPT_LOW_SPEED_TIME, 180);
551 curl_setopt($sac_curl, CURLOPT_NOSIGNAL, 1);
553 array_push($headers,
"Content-Length: " . filesize($sac_package));
557 $my_class_inst->data = fopen($sac_package,
"r");
558 curl_setopt($sac_curl, CURLOPT_READFUNCTION, array($my_class_inst,
"stream_function"));
561 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
563 $sac_resp = curl_exec($sac_curl);
564 $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
566 curl_close($sac_curl);
572 if (($sac_status >= 200) && ($sac_status < 300)) {
575 $sac_xml = @
new SimpleXMLElement($sac_resp);
576 $sac_ns = $sac_xml->getNamespaces(
true);
579 $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
580 }
catch (Exception $e) {
581 throw new Exception(
"Error parsing response entry (" . $e->getMessage() .
")");
589 $sac_xml = @
new SimpleXMLElement($sac_resp);
590 $sac_ns = $sac_xml->getNamespaces(
true);
593 $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
594 }
catch (Exception $e) {
595 throw new Exception(
"Error parsing error document (" . $e->getMessage() .
")");
600 return $sac_dresponse;
604 private function depositAtomEntryByMethod($sac_url, $sac_u, $sac_p, $sac_obo,
605 $sac_method, $sac_fname, $sac_inprogress =
false) {
607 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
610 global $sal_useragent;
611 array_push($headers, $sal_useragent);
612 if (!empty($sac_obo)) {
613 array_push($headers,
"On-Behalf-Of: " . $sac_obo);
615 array_push($headers,
"Content-Type: application/atom+xml;type=entry");
616 if ($sac_inprogress) {
617 array_push($headers,
"In-Progress: true");
619 array_push($headers,
"In-Progress: false");
623 if ($sac_method ==
"PUT") {
624 curl_setopt($sac_curl, CURLOPT_PUT,
true);
625 curl_setopt($sac_curl, CURLOPT_INFILE, fopen($sac_fname,
'rb'));
626 curl_setopt($sac_curl, CURLOPT_INFILESIZE, filesize($sac_fname));
628 curl_setopt($sac_curl, CURLOPT_POST,
true);
629 curl_setopt($sac_curl, CURLOPT_READDATA, fopen($sac_fname,
'rb'));
630 array_push($headers,
"Content-Length: " . filesize($sac_fname));
633 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
635 $sac_resp = curl_exec($sac_curl);
636 $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
637 curl_close($sac_curl);
643 if (($sac_status >= 200) && ($sac_status < 300)) {
646 $sac_xml = @
new SimpleXMLElement($sac_resp);
647 $sac_ns = $sac_xml->getNamespaces(
true);
650 $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
651 }
catch (Exception $e) {
652 throw new Exception(
"Error parsing response entry (" . $e->getMessage() .
")");
660 $sac_xml = @
new SimpleXMLElement($sac_resp);
661 $sac_ns = $sac_xml->getNamespaces(
true);
664 $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
665 }
catch (Exception $e) {
666 throw new Exception(
"Error parsing error document (" . $e->getMessage() .
")");
671 return $sac_dresponse;
676 private function get($sac_url, $sac_u, $sac_p, $sac_obo) {
678 $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
681 global $sal_useragent;
682 array_push($headers, $sal_useragent);
683 if (!empty($sac_obo)) {
684 array_push($headers,
"X-On-Behalf-Of: " . $sac_obo);
686 curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
687 $sac_resp = curl_exec($sac_curl);
688 curl_close($sac_curl);
695 if (array_key_exists(CURLOPT_HTTPHEADER, $this->curl_opts)) {
696 return $this->curl_opts[CURLOPT_HTTPHEADER];