/**
* Check if the remote server environment matches our expectations.
*
* @throws Exception
*/
private function checkRemoteServerEnvironment()
{
$session = $this->container->session;
$baseUrl = $session->get('transfer.url', '', 'akeeba');
$baseUrl = rtrim($baseUrl, '/');
$downloader = new Download($this->container);
$rawData = $downloader->getFromURL($baseUrl . '/kickstart.php?task=serverinfo');
if ($rawData == false) {
// Cannot access Kickstart on the remote server
throw new RuntimeException(JText::_('COM_AKEEBA_TRANSFER_ERR_CANNOTRUNKICKSTART'));
}
// Try to get the raw JSON data
$pos = strpos($rawData, '###');
if ($pos === false) {
// Invalid AJAX data, no leading ###
throw new RuntimeException(JText::_('COM_AKEEBA_TRANSFER_ERR_CANNOTRUNKICKSTART'));
}
// Remove the leading ###
$rawData = substr($rawData, $pos + 3);
$pos = strpos($rawData, '###');
if ($pos === false) {
// Invalid AJAX data, no trailing ###
throw new RuntimeException(JText::_('COM_AKEEBA_TRANSFER_ERR_CANNOTRUNKICKSTART'));
}
// Remove the trailing ###
$rawData = substr($rawData, 0, $pos);
// Get the JSON response
$data = @json_decode($rawData, true);
if (empty($data)) {
// Invalid AJAX data, can't decode this stuff
throw new RuntimeException(JText::_('COM_AKEEBA_TRANSFER_ERR_CANNOTRUNKICKSTART'));
}
// Does the server have enough disk space?
$freeSpace = $data['freeSpace'];
$requiredSize = $this->getApproximateSpaceRequired();
if ($requiredSize['size'] > $freeSpace) {
$unit = array('b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb');
$freeSpaceString = @round($freeSpace / pow(1024, $i = floor(log($freeSpace, 1024))), 2) . ' ' . $unit[$i];
throw new RuntimeException(JText::sprintf('COM_AKEEBA_TRANSFER_ERR_NOTENOUGHSPACE', $freeSpaceString, $requiredSize['string']));
}
// Can I write to remote files?
$canWrite = $data['canWrite'];
$canWriteTemp = $data['canWriteTemp'];
if (!$canWrite && !$canWriteTemp) {
throw new RuntimeException(JText::_('COM_AKEEBA_TRANSFER_ERR_CANNOTWRITEREMOTEFILES'));
}
if ($canWrite) {
$session->set('transfer.targetPath', '', 'akeeba');
} else {
$session->set('transfer.targetPath', 'kicktemp', 'akeeba');
}
$session->set('transfer.remoteTimeLimit', $data['maxExecTime'], 'akeeba');
}