Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1. <?php
  2. class Fedora {
  3. // Use cURL with the provided functions and return the result if the HTTP Code recieved matches the expected HTTP Code
  4. private function curlThis($curlOptions, $expectedHttpCode) {
  5. $returnValue = false;
  6. try {
  7. $curlHandle = curl_init();
  8. if ($curlHandle === false) {
  9. throw new Exception(
  10. "`curl_init()` returned `false`"
  11. );
  12. }
  13. $settingOptionsSucceeded = curl_setopt_array($curlHandle, $curlOptions);
  14. if ($settingOptionsSucceeded === false) {
  15. throw new Exception(
  16. sprintf(
  17. "`curl_setopt_array(...)` returned false. Error: %s. Info: %s",
  18. curl_error($curlHandle),
  19. print_r(curl_getinfo($curlHandle), true)
  20. ),
  21. curl_errno($curlHandle)
  22. );
  23. }
  24. $curlReturn = curl_exec($curlHandle);
  25. if ($curlReturn === false) {
  26. throw new Exception(
  27. sprintf(
  28. "`curl_exec(...)` returned false. Error: %s. Info: %s",
  29. curl_error($curlHandle),
  30. print_r(curl_getinfo($curlHandle), true)
  31. ),
  32. curl_errno($curlHandle)
  33. );
  34. }
  35. $httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
  36. if ($httpCode === false) {
  37. throw new Exception(
  38. sprintf(
  39. "`curl_getinfo(...)` returned false. Error: %s.",
  40. curl_error($curlHandle)
  41. ),
  42. curl_errno($curlHandle)
  43. );
  44. }
  45. if ($httpCode !== $expectedHttpCode) {
  46. throw new Exception(
  47. sprintf(
  48. "`curl_getinfo(...)` returned an unexpected http code (expected %s, but got %s). Error: %s. Complete info: %s",
  49. $expectedHttpCode,
  50. $httpCode,
  51. curl_error($curlHandle),
  52. print_r(curl_getinfo($curlHandle), true)
  53. ),
  54. curl_errno($curlHandle)
  55. );
  56. }
  57. $returnValue = $curlReturn;
  58. } catch (Exception $e) {
  59. trigger_error(
  60. sprintf(
  61. "(%d) %s",
  62. $e->getCode(),
  63. $e->getMessage()
  64. ),
  65. E_USER_ERROR
  66. );
  67. }
  68. return $returnValue;
  69. }
  70.  
  71. // Create a new empty object in Fedora Commons and return its pid
  72. private function createNewEmptyObject($prefix, $id) {
  73. $returnValue = false;
  74.  
  75. // Build URL
  76. $protocol = variable_get("fedora_protocol"); // 'http'
  77. $host = variable_get("fedora_host");
  78. $port = variable_get("fedora_port"); // '8082'
  79. $context = variable_get("fedora_context"); // 'fedora'
  80. $pid = $prefix . ":" . $id;
  81. $url = sprintf(
  82. "%s://%s:%d/%s/objects/%s",
  83. $protocol,
  84. $host,
  85. $port,
  86. $context,
  87. $pid
  88. );
  89.  
  90. // Build cURL options
  91. $userPassword = variable_get("fedora_username") . ":" . variable_get("fedora_password"); // username:password
  92. $verifyPeer = false; // false for ignoring self signed certificates
  93. $headers = array("Accept: text/xml", "Content-Type: text/xml");
  94. $curlOptions = array(
  95. CURLOPT_URL => $url,
  96. CURLOPT_HTTPHEADER => $headers,
  97. CURLOPT_USERPWD => $userPassword,
  98. CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
  99. CURLOPT_SSL_VERIFYPEER => $verifyPeer,
  100. CURLOPT_FOLLOWLOCATION => true,
  101. CURLOPT_RETURNTRANSFER => true,
  102. CURLOPT_POST => true
  103. );
  104.  
  105. // Try `cURL`ing
  106. $result = $this->curlThis($curlOptions, 201);
  107. if ($result === $pid) {
  108. $returnValue = $result;
  109. }
  110. return $returnValue;
  111. }
  112.  
  113. private function attachDatastream ($pid, $file, $datastreamID) {
  114.  
  115. $returnValue = false;
  116.  
  117. // Build URL
  118. $protocol = variable_get("fedora_protocol"); // "http"
  119. $host = variable_get("fedora_host");
  120. $port = variable_get("fedora_port"); // 8082
  121. $context = variable_get("fedora_context"); // fedora
  122. $url = sprintf(
  123. "%s://%s:%d/%s/objects/%s/datastreams/%s?controlGroup=M", // M stands for 'Managed Content'
  124. $protocol,
  125. $host,
  126. $port,
  127. $context,
  128. $pid,
  129. $datastreamID
  130. );
  131.  
  132. // Build cURL options
  133. $userPassword = variable_get("fedora_username") . ":" . variable_get("fedora_password"); // username:password
  134. $verifyPeer = false; // false for ignoring self signed certificates
  135. $headers = array("Accept: text/xml", "Content-Type: " . $file->filemime);
  136. $fileContents = file_get_contents("sites/default/files/images/" . $file->filename);
  137. $curlOptions = array(
  138. CURLOPT_URL => $url,
  139. CURLOPT_HTTPHEADER => $headers,
  140. CURLOPT_USERPWD => $userPassword,
  141. CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
  142. CURLOPT_SSL_VERIFYPEER => $verifyPeer,
  143. CURLOPT_FOLLOWLOCATION => true,
  144. CURLOPT_RETURNTRANSFER => true,
  145. CURLOPT_POST => true,
  146. CURLOPT_POSTFIELDS => $fileContents
  147. );
  148.  
  149. // Try `cURL`ing
  150. $result = $this->curlThis($curlOptions, 201);
  151. if ($result === $pid) {
  152. $returnValue = $result;
  153. }
  154. return $returnValue;
  155. }
  156.  
  157. public function ingest($namespace, $identifier, $file) {
  158. $pid = $this->createNewEmptyObject($namespace, $identifier);
  159. if ($pid) {
  160. $result = $this->attachDatastream($pid, $file, "IMAGE");
  161. if ($result) {
  162. error_log("Successfully ingested a file!");
  163. } else {
  164. error_log("FAILED ATTACHING DATASTREAM TO NEW OBJECT");
  165. }
  166. } else {
  167. error_log("FAILED CREATING NEW EMPTY OBJECT");
  168. }
  169.  
  170. }
  171. }
  172. $fedora = new Fedora();
  173. /*
  174. * $file is an object obtained by uploading a file into a drupal file system (at /drupal/sites/default/files/images/singe.jpg). It has the following attributes:
  175. * $file->filemime === "image/jpeg"
  176. * $file->filename === "singe.jpg"
  177. */
  178. $fedora->ingest('namespace', 'identifier', $file);
  179.  
  180. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement