Open Journal Systems  3.3.0
MedraWebservice.inc.php
1 <?php
2 
20 import('lib.pkp.classes.xml.XMLNode');
21 
22 define('MEDRA_WS_ENDPOINT_DEV', 'https://www-medra-dev.medra.org/servlet/ws/medraWS');
23 define('MEDRA_WS_ENDPOINT', 'https://www.medra.org/servlet/ws/medraWS');
24 define('MEDRA_WS_RESPONSE_OK', 200);
25 
27 
29  var $_auth;
30 
33 
34 
41  function __construct($endpoint, $login, $password) {
42  $this->_endpoint = $endpoint;
43  $this->_auth = "$login:$password";
44  }
45 
46 
47  //
48  // Public Web Service Actions
49  //
54  function upload($xml) {
55  $attachmentId = $this->_getContentId('metadata');
56  $attachment = array($attachmentId => $xml);
57  $arg = "<med:contentID href=\"$attachmentId\" />";
58  return $this->_doRequest('upload', $arg, $attachment);
59  }
60 
64  function viewMetadata($doi) {
65  $doi = $this->_escapeXmlEntities($doi);
66  $arg = "<med:doi>$doi</med:doi>";
67  return $this->_doRequest('viewMetadata', $arg);
68  }
69 
70 
71  //
72  // Internal helper methods.
73  //
81  function _doRequest($action, $arg, $attachment = null) {
82  // Build the multipart SOAP message from scratch.
83  $soapMessage =
84  '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' .
85  'xmlns:med="http://www.medra.org">' .
86  '<SOAP-ENV:Header/>' .
87  '<SOAP-ENV:Body>' .
88  "<med:$action>$arg</med:$action>" .
89  '</SOAP-ENV:Body>' .
90  '</SOAP-ENV:Envelope>';
91 
92  $soapMessageId = $this->_getContentId($action);
93  if ($attachment) {
94  assert(count($attachment) == 1);
95  $request =
96  "--MIME_boundary\r\n" .
97  $this->_getMimePart($soapMessageId, $soapMessage) .
98  "--MIME_boundary\r\n" .
99  $this->_getMimePart(key($attachment), current($attachment)) .
100  "--MIME_boundary--\r\n";
101  $contentType = 'multipart/related; type="text/xml"; boundary="MIME_boundary"';
102  } else {
103  $request = $soapMessage;
104  $contentType = 'text/xml';
105  }
106 
107  // Prepare HTTP session.
108  import('lib.pkp.classes.helpers.PKPCurlHelper');
109  $curlCh = PKPCurlHelper::getCurlObject();
110 
111  curl_setopt($curlCh, CURLOPT_RETURNTRANSFER, true);
112  curl_setopt($curlCh, CURLOPT_POST, true);
113 
114  // Set up basic authentication.
115  curl_setopt($curlCh, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
116  curl_setopt($curlCh, CURLOPT_USERPWD, $this->_auth);
117 
118  // Make SOAP request.
119  curl_setopt($curlCh, CURLOPT_URL, $this->_endpoint);
120  $extraHeaders = array(
121  'SOAPAction: "' . $action . '"',
122  'Content-Type: ' . $contentType,
123  'UserAgent: OJS-mEDRA'
124  );
125  curl_setopt($curlCh, CURLOPT_HTTPHEADER, $extraHeaders);
126  curl_setopt($curlCh, CURLOPT_POSTFIELDS, $request);
127 
128  $result = true;
129  $response = curl_exec($curlCh);
130 
131  // We do not localize our error messages as they are all
132  // fatal errors anyway and must be analyzed by technical staff.
133  if ($response === false) {
134  $result = 'OJS-mEDRA: Expected string response.';
135  }
136 
137  if ($result === true && ($status = curl_getinfo($curlCh, CURLINFO_HTTP_CODE)) != MEDRA_WS_RESPONSE_OK) {
138  $result = 'OJS-mEDRA: Expected ' . MEDRA_WS_RESPONSE_OK . ' response code, got ' . $status . ' instead.';
139  }
140 
141  curl_close($curlCh);
142 
143  // Check SOAP response by simple string manipulation rather
144  // than instantiating a DOM.
145  if (is_string($response)) {
146  $matches = array();
147  PKPString::regexp_match_get('#<faultstring>([^<]*)</faultstring>#', $response, $matches);
148  if (empty($matches)) {
149  if ($attachment) {
150  assert(PKPString::regexp_match('#<returnCode>success</returnCode>#', $response));
151  } else {
152  $parts = explode("\r\n\r\n", $response);
153  $result = array_pop($parts);
154  $result = PKPString::regexp_replace('/>[^>]*$/', '>', $result);
155  }
156  } else {
157  $result = 'mEDRA: ' . $status . ' - ' . $matches[1];
158  }
159  } else {
160  $result = 'OJS-mEDRA: Expected string response.';
161  }
162 
163  return $result;
164  }
165 
172  function _getMimePart($contentId, $content) {
173  return
174  "Content-Type: text/xml; charset=utf-8\r\n" .
175  "Content-ID: <${contentId}>\r\n" .
176  "\r\n" .
177  $content . "\r\n";
178  }
179 
185  function _getContentId($prefix) {
186  return $prefix . md5(uniqid()) . '@medra.org';
187  }
188 
193  function _escapeXmlEntities($string) {
194  return XMLNode::xmlentities($string);
195  }
196 }
PKPString\regexp_match_get
static regexp_match_get($pattern, $subject, &$matches)
Definition: PKPString.inc.php:256
MedraWebservice\$_auth
$_auth
Definition: MedraWebservice.inc.php:32
PKPString\regexp_replace
static regexp_replace($pattern, $replacement, $subject, $limit=-1)
Definition: PKPString.inc.php:279
MedraWebservice\_getContentId
_getContentId($prefix)
Definition: MedraWebservice.inc.php:191
MedraWebservice\_escapeXmlEntities
_escapeXmlEntities($string)
Definition: MedraWebservice.inc.php:199
PKPCurlHelper\getCurlObject
static getCurlObject($url=null, $useProxySettings=true)
Definition: PKPCurlHelper.inc.php:23
MedraWebservice
A wrapper for the mEDRA web service 2.0.
Definition: MedraWebservice.inc.php:26
MedraWebservice\_getMimePart
_getMimePart($contentId, $content)
Definition: MedraWebservice.inc.php:178
MedraWebservice\__construct
__construct($endpoint, $login, $password)
Definition: MedraWebservice.inc.php:47
MedraWebservice\viewMetadata
viewMetadata($doi)
Definition: MedraWebservice.inc.php:70
MedraWebservice\_doRequest
_doRequest($action, $arg, $attachment=null)
Definition: MedraWebservice.inc.php:87
MedraWebservice\$_endpoint
$_endpoint
Definition: MedraWebservice.inc.php:38
PKPString\regexp_match
static regexp_match($pattern, $subject)
Definition: PKPString.inc.php:245
MedraWebservice\upload
upload($xml)
Definition: MedraWebservice.inc.php:60
XMLNode\xmlentities
static xmlentities($string, $quote_style=ENT_QUOTES)
Definition: XMLNode.inc.php:242