/**
* Retrieve the export as an XML string.
* @param $pluginUrl string the url to be requested for export.
* @param $postParams array additional post parameters
* @return string
*/
protected function getXmlOnExport($pluginUrl, $postParams = array())
{
// Prepare HTTP session.
$curlCh = curl_init();
curl_setopt($curlCh, CURLOPT_POST, true);
// Create a cookie file (required for log-in).
$cookies = tempnam(sys_get_temp_dir(), 'curlcookies');
// Log in.
$loginUrl = $this->baseUrl . '/index.php/test/login/signIn';
// Bug #8518 safety work-around
if ($this->password[0] == '@') {
die('CURL parameters may not begin with @.');
}
$loginParams = array('username' => 'admin', 'password' => $this->password);
curl_setopt($curlCh, CURLOPT_URL, $loginUrl);
curl_setopt($curlCh, CURLOPT_POSTFIELDS, $loginParams);
curl_setopt($curlCh, CURLOPT_COOKIEJAR, $cookies);
self::assertTrue(curl_exec($curlCh));
// Request export document.
$exportUrl = $this->baseUrl . '/index.php/test/manager/importexport/plugin/' . $pluginUrl;
curl_setopt($curlCh, CURLOPT_URL, $exportUrl);
// Bug #8518 safety work-around
foreach ($postParams as $paramValue) {
if ($paramValue[0] == '@') {
die('CURL parameters may not begin with @.');
}
}
curl_setopt($curlCh, CURLOPT_POSTFIELDS, $postParams);
curl_setopt($curlCh, CURLOPT_HTTPHEADER, array('Accept: application/xml, application/x-gtar, */*'));
curl_setopt($curlCh, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlCh, CURLOPT_HEADER, true);
$response = curl_exec($curlCh);
do {
list($header, $response) = explode("\r\n\r\n", $response, 2);
} while (PKPString::regexp_match('#HTTP/.*100#', $header));
// Check whether we got a tar file.
if (PKPString::regexp_match('#Content-Type: application/x-gtar#', $header)) {
// Save the data to a temporary file.
$tempfile = tempnam(sys_get_temp_dir(), 'tst');
file_put_contents($tempfile, $response);
// Recursively extract tar file.
$result = $this->extractTarFile($tempfile);
unlink($tempfile);
} else {
$matches = null;
PKPString::regexp_match_get('#filename="([^"]+)"#', $header, $matches);
self::assertTrue(isset($matches[1]));
$result = array($matches[1] => $response);
}
// Destroy HTTP session.
curl_close($curlCh);
unlink($cookies);
return $result;
}