Open Journal Systems  3.3.0
swordappclient.php
1 <?php
2 
3 require("swordappservicedocument.php");
4 require("swordappentry.php");
5 require("swordappresponse.php");
6 require("swordappstatement.php");
7 require("swordapperrordocument.php");
8 require("swordapplibraryuseragent.php");
9 require("stream.php");
10 require_once("utils.php");
11 
13 
14  private $debug = false;
15  private $curl_opts = array();
16 
17  function SWORDAPPClient($curl_opts = array()) {
18  $this->curl_opts = $curl_opts;
19  }
20 
21  // Request a Service Document from the specified url, with the specified credentials,
22  // and on-behalf-of the specified user.
23  function servicedocument($sac_url, $sac_u, $sac_p, $sac_obo) {
24  // Get the service document
25  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
26 
27  $headers = $this->_init_headers();
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);
32  }
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);
37 
38  // Parse the result
39  if ($sac_status == 200) {
40  try {
41  $sac_sdresponse = new SWORDAPPServiceDocument($sac_url, $sac_status, $sac_resp);
42  } catch (Exception $e) {
43  throw new Exception("Error parsing service document (" . $e->getMessage() . ")");
44  }
45  } else {
46  $sac_sdresponse = new SWORDAPPServiceDocument($sac_url, $sac_status);
47  }
48 
49  // Return the Service Document object
50  return $sac_sdresponse;
51  }
52 
53  // Perform a deposit to the specified url, with the specified credentials,
54  // on-behalf-of the specified user, and with the given file and formatnamespace and noop setting
55  function deposit($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname,
56  $sac_packaging= '', $sac_contenttype = '', $sac_inprogress = false) {
57  // Perform the deposit
58  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
59 
60  curl_setopt($sac_curl, CURLOPT_POST, true);
61 
62  $headers = $this->_init_headers();
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);
68  }
69  if (!empty($sac_packaging)) {
70  array_push($headers, "Packaging: " . $sac_packaging);
71  }
72  if (!empty($sac_contenttype)) {
73  array_push($headers, "Content-Type: " . $sac_contenttype);
74  }
75  array_push($headers, "Content-Length: " . filesize($sac_fname));
76  if ($sac_inprogress) {
77  array_push($headers, "In-Progress: true");
78  } else {
79  array_push($headers, "In-Progress: false");
80  }
81 
82  // Set the Content-Disposition header
83  $index = strpos(strrev($sac_fname), '/');
84  if ($index !== false) {
85  $index = strlen($sac_fname) - $index;
86  $sac_fname_trimmed = substr($sac_fname, $index);
87  } else {
88  $sac_fname_trimmed = $sac_fname;
89  }
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);
93 
94  $sac_resp = curl_exec($sac_curl);
95  $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
96  curl_close($sac_curl);
97 
98  // Parse the result
99  $sac_dresponse = new SWORDAPPEntry($sac_status, $sac_resp);
100 
101  // Was it a successful result?
102  if (($sac_status >= 200) && ($sac_status < 300)) {
103  try {
104  // Get the deposit results
105  $sac_xml = @new SimpleXMLElement($sac_resp);
106  $sac_ns = $sac_xml->getNamespaces(true);
107 
108  // Build the deposit response object
109  $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
110  } catch (Exception $e) {
111  throw new Exception("Error parsing response entry (" . $e->getMessage() . ")");
112  }
113  } else {
114  try {
115  // Parse the result
116  $sac_dresponse = new SWORDAPPErrorDocument($sac_status, $sac_resp);
117 
118  // Get the deposit results
119  $sac_xml = @new SimpleXMLElement($sac_resp);
120  $sac_ns = $sac_xml->getNamespaces(true);
121 
122  // Build the deposit response object
123  $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
124  } catch (Exception $e) {
125  throw new Exception("Error parsing error document (" . $e->getMessage() . ")");
126  }
127  }
128 
129  // Return the deposit object
130  return $sac_dresponse;
131  }
132 
133  // Deposit a multipart package
134  function depositMultipart($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package,
135  $sac_inprogress = false) {
136  try {
137  return$this->depositMultipartByMethod($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package,
138  "POST", $sac_inprogress);
139  } catch (Exception $e) {
140  throw $e;
141  }
142  }
143 
144  // Function to create a resource by depositing an Atom entry
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);
148  }
149 
150  // Complete an incomplete deposit by posting the In-Progress header of false to an SE-IRI
151  function completeIncompleteDeposit($sac_url, $sac_u, $sac_p, $sac_obo) {
152  // Perform the deposit
153  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
154 
155  curl_setopt($sac_curl, CURLOPT_POST, true);
156 
157  $headers = $this->_init_headers();
158  global $sal_useragent;
159  array_push($headers, $sal_useragent);
160  if (!empty($sac_obo)) {
161  array_push($headers, "On-Behalf-Of: " . $sac_obo);
162  }
163  array_push($headers, "Content-Length: 0");
164  array_push($headers, "In-Progress: false");
165 
166  curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
167 
168  $sac_resp = curl_exec($sac_curl);
169  $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
170  curl_close($sac_curl);
171 
172  // Parse the result
173  $sac_response = new SWORDAPPResponse($sac_status, $sac_resp);
174 
175  // Return the response
176  return $sac_response;
177  }
178 
179  // Function to retrieve the content of a container
180  function retrieveContent($sac_url, $sac_u, $sac_p, $sac_obo, $sac_accept_packaging = "") {
181  // Retrieve the content
182  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
183 
184  $headers = $this->_init_headers();
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);
189  }
190  if (!empty($sac_accept_packaging)) {
191  array_push($headers, "Accept-Packaging: " . $sac_accept_packaging);
192  }
193  curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
194  $sac_resp = curl_exec($sac_curl);
195  curl_close($sac_curl);
196 
197  // Return the response
198  return $sac_resp;
199  }
200 
201  // Function to retrieve the entry content of a container
202  function retrieveDepositReceipt($sac_url, $sac_u, $sac_p, $sac_obo, $sac_accept_packaging = "") {
203  // Retrieve the content
204  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
205 
206  $headers = $this->_init_headers();
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);
211  }
212  if (!empty($sac_accept_packaging)) {
213  array_push($headers, "Accept-Packaging: " . $sac_accept_packaging);
214  }
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);
219 
220  // Parse the result
221  $sac_dresponse = new SWORDAPPEntry($sac_status, $sac_resp);
222 
223  // Parse the result
224  if (($sac_status >= 200) && ($sac_status < 300)) {
225  try {
226  // Get the deposit results
227  $sac_xml = @new SimpleXMLElement($sac_resp);
228  $sac_ns = $sac_xml->getNamespaces(true);
229 
230  // Build the deposit response object
231  $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
232  } catch (Exception $e) {
233  throw new Exception("Error parsing response entry (" . $e->getMessage() . ")");
234  }
235  } else {
236  try {
237  // Parse the result
238  $sac_dresponse = new SWORDAPPErrorDocument($sac_status, $sac_resp);
239 
240  // Get the deposit results
241  $sac_xml = @new SimpleXMLElement($sac_resp);
242  $sac_ns = $sac_xml->getNamespaces(true);
243 
244  // Build the deposit response object
245  $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
246  } catch (Exception $e) {
247  throw new Exception("Error parsing error document (" . $e->getMessage() . ")");
248  }
249  }
250 
251  // Return the deposit object
252  return $sac_dresponse;
253  }
254 
255  // Replace the file content of a resource
256  function replaceFileContent($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname,
257  $sac_packaging= '', $sac_contenttype = '', $sac_metadata_relevant = false) {
258  // Perform the deposit
259  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
260 
261  curl_setopt($sac_curl, CURLOPT_PUT, true);
262 
263  $headers = $this->_init_headers();
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);
269  }
270  if (!empty($sac_packaging)) {
271  array_push($headers, "Packaging: " . $sac_packaging);
272  }
273  if (!empty($sac_contenttype)) {
274  array_push($headers, "Content-Type: " . $sac_contenttype);
275  }
276  if ($sac_metadata_relevant) {
277  array_push($headers, "Metadata-Relevant: true");
278  } else {
279  array_push($headers, "Metadata-Relevant: false");
280  }
281 
282  // Set the Content-Disposition header
283  $index = strpos(strrev($sac_fname), '/');
284  if ($index !== false) {
285  $index = strlen($sac_fname) - $index;
286  $sac_fname_trimmed = substr($sac_fname, $index);
287  } else {
288  $sac_fname_trimmed = $sac_fname;
289  }
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);
294 
295  $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
296  curl_close($sac_curl);
297 
298  // Was it a successful result?
299  if ($sac_status != 204) {
300  throw new Exception("Error replacing file (HTTP code: " . $sac_status . ")");
301  } else {
302  return $sac_status;
303  }
304  }
305 
306  // Function to replace the metadata of a resource
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);
310  }
311 
312  // Replace a multipart package
313  function replaceMetadataAndFile($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package,
314  $sac_inprogress = false) {
315  return $this->depositMultipartByMethod($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package,
316  "PUT", $sac_inprogress);
317  }
318 
319  // Add a an extra file to the media resource
320  function addExtraFileToMediaResource($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname,
321  $sac_contenttype = '', $sac_metadata_relevant = false) {
322  // Perform the deposit
323  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
324 
325  curl_setopt($sac_curl, CURLOPT_POST, true);
326 
327  $headers = $this->_init_headers();
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);
333  }
334  if (!empty($sac_contenttype)) {
335  array_push($headers, "Content-Type: " . $sac_contenttype);
336  }
337  if ($sac_metadata_relevant) {
338  array_push($headers, "Metadata-Relevant: true");
339  } else {
340  array_push($headers, "Metadata-Relevant: false");
341  }
342  array_push($headers, "Content-Length: " . filesize($sac_fname));
343 
344  // Set the Content-Disposition header
345  $index = strpos(strrev($sac_fname), '/');
346  if ($index !== false) {
347  $index = strlen($sac_fname) - $index;
348  $sac_fname_trimmed = substr($sac_fname, $index);
349  } else {
350  $sac_fname_trimmed = $sac_fname;
351  }
352  array_push($headers, "Content-Disposition: attachment; filename=" . $sac_fname_trimmed);
353 
354  curl_setopt($sac_curl, CURLOPT_READDATA, fopen($sac_fname, 'rb'));
355  curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
356 
357  $sac_resp = curl_exec($sac_curl);
358  $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
359  curl_close($sac_curl);
360 
361  // Parse the result
362  $sac_dresponse = new SWORDAPPEntry($sac_status, $sac_resp);
363 
364  // Was it a successful result?
365  if (($sac_status >= 200) && ($sac_status < 300)) {
366  try {
367  // Get the deposit results
368  //$sac_xml = @new SimpleXMLElement($sac_resp);
369  //$sac_ns = $sac_xml->getNamespaces(true);
370 
371  // Build the deposit response object
372  //$sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
373  } catch (Exception $e) {
374  throw new Exception("Error parsing response entry (" . $e->getMessage() . ")");
375  }
376  } else {
377  try {
378  // Parse the result
379  //$sac_dresponse = new SWORDAPPErrorDocument($sac_status, $sac_resp);
380 
381  // Get the deposit results
382  //$sac_xml = @new SimpleXMLElement($sac_resp);
383  //$sac_ns = $sac_xml->getNamespaces(true);
384 
385  // Build the deposit response object
386  //$sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
387  } catch (Exception $e) {
388  throw new Exception("Error parsing error document (" . $e->getMessage() . ")");
389  }
390  }
391 
392  return $sac_dresponse;
393  }
394 
395  // Add a new package
396  function addExtraPackage($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname,
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);
400  }
401 
402  // Add a new Atom entry
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);
406  }
407 
408  // Add a new multipart package
409  function addExtraMultipartPackage($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package,
410  $sac_inprogress = false) {
411  return $this->depositMultipartByMethod($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package,
412  "POST", $sac_inprogress);
413  }
414 
415  // Function to delete a container (object)
416  function deleteContainer($sac_url, $sac_u, $sac_p, $sac_obo) {
417  // Perform the deposit
418  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
419 
420  curl_setopt($sac_curl, CURLOPT_CUSTOMREQUEST, "DELETE");
421 
422  $headers = $this->_init_headers();
423  global $sal_useragent;
424  array_push($headers, $sal_useragent);
425  if (!empty($sac_obo)) {
426  array_push($headers, "On-Behalf-Of: " . $sac_obo);
427  }
428 
429  curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
430 
431  $sac_resp = curl_exec($sac_curl);
432  $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
433  curl_close($sac_curl);
434 
435  return new SWORDAPPResponse($sac_status, $sac_resp);
436  }
437 
438  // Function to delete the content of a resource
439  function deleteResourceContent($sac_url, $sac_u, $sac_p, $sac_obo) {
440  return $this->deleteContainer($sac_url, $sac_u, $sac_p, $sac_obo);
441  }
442 
443  // Function to retrieve an Atom statement
444  function retrieveAtomStatement($sac_url, $sac_u, $sac_p, $sac_obo) {
445  // Get the Atom statement
446  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
447 
448  $headers = $this->_init_headers();
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);
453  }
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);
458 
459  // Parse the result
460  if ($sac_status == 200) {
461  try {
462  $sac_atomstatement = new SWORDAPPStatement($sac_status, $sac_resp);
463  } catch (Exception $e) {
464  throw new Exception("Error parsing statement (" . $e->getMessage() . ")");
465  }
466  } else {
467  $sac_atomstatement = new SWORDAPPStatement($sac_url, $sac_status);
468  }
469 
470  // Return the atom statement object
471  return $sac_atomstatement;
472  }
473 
474  // Function to retrieve an OAI-ORE statement - this just returns the xml,
475  // it does not marshall it into an object.
476  function retrieveOAIOREStatement($sac_url, $sac_u, $sac_p, $sac_obo) {
477  // Get the OAI-ORE statement
478  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
479 
480  $headers = $this->_init_headers();
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);
485  }
486  curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
487  $sac_resp = curl_exec($sac_curl);
488  curl_close($sac_curl);
489 
490  // Return the result
491  return $sac_resp;
492  }
493 
494  // Generic private method to initalise a curl transaction
495  private function curl_init($sac_url, $sac_user, $sac_password) {
496  // Initialise the curl object
497  $sac_curl = curl_init();
498 
499  // Return the content from curl, rather than outputting it
500  curl_setopt($sac_curl, CURLOPT_RETURNTRANSFER, true);
501 
502  // Set the debug option
503  curl_setopt($sac_curl, CURLOPT_VERBOSE, $this->debug);
504 
505  // Set the URL to connect to
506  curl_setopt($sac_curl, CURLOPT_URL, $sac_url);
507 
508  // If required, set authentication
509  if(!empty($sac_user) && !empty($sac_password)) {
510  curl_setopt($sac_curl, CURLOPT_USERPWD, $sac_user . ":" . $sac_password);
511  }
512 
513  // Set user-specified curl opts
514  foreach ($this->curl_opts as $opt => $val) {
515  curl_setopt($sac_curl, $opt, $val);
516  }
517 
518  // Return the initalised curl object
519  return $sac_curl;
520  }
521 
522  // A method for multipart deposit - method can be set - POST or PUT
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);
526 
527  $headers = $this->_init_headers();
528 
529  if ($sac_inprogress) {
530  array_push($headers, "In-Progress: true");
531  } else {
532  array_push($headers, "In-Progress: false");
533  }
534 
535  if (!empty($sac_obo)) {
536  array_push($headers, "On-Behalf-Of: " . $sac_obo);
537  }
538 
539  array_push($headers, "Content-Type: multipart/related; boundary=\"===============SWORDPARTS==\"; type=\"application/atom+xml\"");
540 
541  // Set the appropriate method
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));
546  } else {
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);
552 
553  array_push($headers, "Content-Length: " . filesize($sac_package));
554 
555  // Instantiate the streaming class
556  $my_class_inst = new StreamingClass();
557  $my_class_inst->data = fopen($sac_package, "r");
558  curl_setopt($sac_curl, CURLOPT_READFUNCTION, array($my_class_inst, "stream_function"));
559  }
560 
561  curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
562 
563  $sac_resp = curl_exec($sac_curl);
564  $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
565 
566  curl_close($sac_curl);
567 
568  // Parse the result
569  $sac_dresponse = new SWORDAPPEntry($sac_status, $sac_resp);
570 
571  // Was it a successful result?
572  if (($sac_status >= 200) && ($sac_status < 300)) {
573  try {
574  // Get the deposit results
575  $sac_xml = @new SimpleXMLElement($sac_resp);
576  $sac_ns = $sac_xml->getNamespaces(true);
577 
578  // Build the deposit response object
579  $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
580  } catch (Exception $e) {
581  throw new Exception("Error parsing response entry (" . $e->getMessage() . ")");
582  }
583  } else {
584  try {
585  // Parse the result
586  $sac_dresponse = new SWORDAPPErrorDocument($sac_status, $sac_resp);
587 
588  // Get the deposit results
589  $sac_xml = @new SimpleXMLElement($sac_resp);
590  $sac_ns = $sac_xml->getNamespaces(true);
591 
592  // Build the deposit response object
593  $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
594  } catch (Exception $e) {
595  throw new Exception("Error parsing error document (" . $e->getMessage() . ")");
596  }
597  }
598 
599  // Return the deposit object
600  return $sac_dresponse;
601  }
602 
603  // Function to deposit an Atom entry
604  private function depositAtomEntryByMethod($sac_url, $sac_u, $sac_p, $sac_obo,
605  $sac_method, $sac_fname, $sac_inprogress = false) {
606  // Perform the deposit
607  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
608 
609  $headers = $this->_init_headers();
610  global $sal_useragent;
611  array_push($headers, $sal_useragent);
612  if (!empty($sac_obo)) {
613  array_push($headers, "On-Behalf-Of: " . $sac_obo);
614  }
615  array_push($headers, "Content-Type: application/atom+xml;type=entry");
616  if ($sac_inprogress) {
617  array_push($headers, "In-Progress: true");
618  } else {
619  array_push($headers, "In-Progress: false");
620  }
621 
622  // Set the appropriate method
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));
627  } else {
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));
631  }
632 
633  curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
634 
635  $sac_resp = curl_exec($sac_curl);
636  $sac_status = curl_getinfo($sac_curl, CURLINFO_HTTP_CODE);
637  curl_close($sac_curl);
638 
639  // Parse the result
640  $sac_dresponse = new SWORDAPPEntry($sac_status, $sac_resp);
641 
642  // Was it a successful result?
643  if (($sac_status >= 200) && ($sac_status < 300)) {
644  try {
645  // Get the deposit results
646  $sac_xml = @new SimpleXMLElement($sac_resp);
647  $sac_ns = $sac_xml->getNamespaces(true);
648 
649  // Build the deposit response object
650  $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
651  } catch (Exception $e) {
652  throw new Exception("Error parsing response entry (" . $e->getMessage() . ")");
653  }
654  } else {
655  try {
656  // Parse the result
657  $sac_dresponse = new SWORDAPPErrorDocument($sac_status, $sac_resp);
658 
659  // Get the deposit results
660  $sac_xml = @new SimpleXMLElement($sac_resp);
661  $sac_ns = $sac_xml->getNamespaces(true);
662 
663  // Build the deposit response object
664  $sac_dresponse->buildhierarchy($sac_xml, $sac_ns);
665  } catch (Exception $e) {
666  throw new Exception("Error parsing error document (" . $e->getMessage() . ")");
667  }
668  }
669 
670  // Return the deposit object
671  return $sac_dresponse;
672  }
673 
674  // Request a URI with the specified credentials, and on-behalf-of the specified user.
675  // This is not specifically for SWORD, but for retrieving other associated URIs
676  private function get($sac_url, $sac_u, $sac_p, $sac_obo) {
677  // Get the service document
678  $sac_curl = $this->curl_init($sac_url, $sac_u, $sac_p);
679 
680  $headers = $this->_init_headers();
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);
685  }
686  curl_setopt($sac_curl, CURLOPT_HTTPHEADER, $headers);
687  $sac_resp = curl_exec($sac_curl);
688  curl_close($sac_curl);
689 
690  // Return the response
691  return $sac_resp;
692  }
693 
694  function _init_headers() {
695  if (array_key_exists(CURLOPT_HTTPHEADER, $this->curl_opts)) {
696  return $this->curl_opts[CURLOPT_HTTPHEADER];
697  }
698  return array();
699  }
700 }
701 
702 ?>
SWORDAPPClient\retrieveAtomStatement
retrieveAtomStatement($sac_url, $sac_u, $sac_p, $sac_obo)
Definition: swordappclient.php:444
SWORDAPPClient\deposit
deposit($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname, $sac_packaging='', $sac_contenttype='', $sac_inprogress=false)
Definition: swordappclient.php:55
SWORDAPPClient\depositAtomEntry
depositAtomEntry($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname, $sac_inprogress=false)
Definition: swordappclient.php:145
SWORDAPPServiceDocument
Definition: swordappservicedocument.php:5
SWORDAPPClient\replaceMetadataAndFile
replaceMetadataAndFile($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package, $sac_inprogress=false)
Definition: swordappclient.php:313
SWORDAPPClient\deleteResourceContent
deleteResourceContent($sac_url, $sac_u, $sac_p, $sac_obo)
Definition: swordappclient.php:439
SWORDAPPClient
Definition: swordappclient.php:12
SWORDAPPErrorDocument
Definition: swordapperrordocument.php:6
SWORDAPPClient\completeIncompleteDeposit
completeIncompleteDeposit($sac_url, $sac_u, $sac_p, $sac_obo)
Definition: swordappclient.php:151
SWORDAPPClient\deleteContainer
deleteContainer($sac_url, $sac_u, $sac_p, $sac_obo)
Definition: swordappclient.php:416
SWORDAPPClient\replaceFileContent
replaceFileContent($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname, $sac_packaging='', $sac_contenttype='', $sac_metadata_relevant=false)
Definition: swordappclient.php:256
SWORDAPPStatement
Definition: swordappstatement.php:6
SWORDAPPClient\addExtraMultipartPackage
addExtraMultipartPackage($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package, $sac_inprogress=false)
Definition: swordappclient.php:409
SWORDAPPClient\retrieveContent
retrieveContent($sac_url, $sac_u, $sac_p, $sac_obo, $sac_accept_packaging="")
Definition: swordappclient.php:180
SWORDAPPClient\addExtraPackage
addExtraPackage($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname, $sac_packaging='', $sac_contenttype, $sac_inprogress=false)
Definition: swordappclient.php:396
SWORDAPPClient\_init_headers
_init_headers()
Definition: swordappclient.php:694
SWORDAPPClient\replaceMetadata
replaceMetadata($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname, $sac_inprogress=false)
Definition: swordappclient.php:307
SWORDAPPEntry
Definition: swordappentry.php:6
SWORDAPPClient\SWORDAPPClient
SWORDAPPClient($curl_opts=array())
Definition: swordappclient.php:17
SWORDAPPClient\addExtraFileToMediaResource
addExtraFileToMediaResource($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname, $sac_contenttype='', $sac_metadata_relevant=false)
Definition: swordappclient.php:320
SWORDAPPClient\servicedocument
servicedocument($sac_url, $sac_u, $sac_p, $sac_obo)
Definition: swordappclient.php:23
SWORDAPPClient\retrieveOAIOREStatement
retrieveOAIOREStatement($sac_url, $sac_u, $sac_p, $sac_obo)
Definition: swordappclient.php:476
SWORDAPPClient\retrieveDepositReceipt
retrieveDepositReceipt($sac_url, $sac_u, $sac_p, $sac_obo, $sac_accept_packaging="")
Definition: swordappclient.php:202
SWORDAPPClient\addExtraAtomEntry
addExtraAtomEntry($sac_url, $sac_u, $sac_p, $sac_obo, $sac_fname, $sac_inprogress=false)
Definition: swordappclient.php:403
SWORDAPPClient\depositMultipart
depositMultipart($sac_url, $sac_u, $sac_p, $sac_obo, $sac_package, $sac_inprogress=false)
Definition: swordappclient.php:134
SWORDAPPResponse
Definition: swordappresponse.php:5
StreamingClass
Definition: stream.php:4