/*
* Disable error reporting due to noticed thrown when directories are checked
* It will cause constant loading icon otherwise.
*/
error_reporting(0);
set_time_limit(0);
ob_start();
$this->load->model('setting/setting');
$this->load->language('extension/openbay');
$data = $this->request->post;
$data['user'] = $data['openbay_ftp_username'];
$data['pw'] = html_entity_decode($data['openbay_ftp_pw']);
$data['server'] = $data['openbay_ftp_server'];
$data['rootpath'] = $data['openbay_ftp_rootpath'];
$data['adminDir'] = $data['openbay_admin_directory'];
$data['beta'] = ((isset($data['openbay_ftp_beta']) && $data['openbay_ftp_beta'] == 1) ? 1 : 0);
if (empty($data['user'])) {
return array('connection' => false, 'msg' => $this->language->get('update_error_username'));
}
if (empty($data['pw'])) {
return array('connection' => false, 'msg' => $this->language->get('update_error_password'));
}
if (empty($data['server'])) {
return array('connection' => false, 'msg' => $this->language->get('update_error_server'));
}
if (empty($data['adminDir'])) {
return array('connection' => false, 'msg' => $this->language->get('update_error_admindir'));
}
$connection = @ftp_connect($data['server']);
$updatelog = "Connecting to server\n";
if ($connection != false) {
$updatelog .= "Connected\n";
$updatelog .= "Checking login details\n";
if (isset($data['openbay_ftp_pasv']) && $data['openbay_ftp_pasv'] == 1) {
ftp_pasv($connection, true);
$updatelog .= "Using pasv connection\n";
}
if (@ftp_login($connection, $data['user'], $data['pw'])) {
$updatelog .= "Logged in\n";
if (!empty($data['rootpath'])) {
$updatelog .= "Setting root path\n";
@ftp_chdir($connection, $data['rootpath']);
$directory_list = ftp_nlist($connection, $data['rootpath']);
}
$current_version = $this->config->get('openbay_version');
$send = array('version' => $current_version, 'ocversion' => VERSION, 'beta' => $data['beta']);
$files = $this->call('update/getList/', $send);
$updatelog .= "Requesting file list\n";
if ($this->lasterror == true) {
$updatelog .= $this->lastmsg;
return array('connection' => true, 'msg' => $this->lastmsg);
} else {
$updatelog .= "Received list of files\n";
foreach ($files['asset']['file'] as $file) {
$dir = '';
$dirLevel = 0;
if (isset($file['locations']['location']) && is_array($file['locations']['location'])) {
foreach ($file['locations']['location'] as $location) {
$updatelog .= "Current location: " . $dir . "\n";
// Added to allow OC security where the admin directory is renamed
if ($location == 'admin') {
$location = $data['adminDir'];
}
$dir .= $location . '/';
$updatelog .= "Trying to get to: " . $dir . "\n";
$updatelog .= "ftp_pwd output: " . ftp_pwd($connection) . "\n";
if (@ftp_chdir($connection, $location)) {
$dirLevel++;
} else {
if (@ftp_mkdir($connection, $location)) {
$updatelog .= "Created directory: " . $dir . "\n";
ftp_chdir($connection, $location);
$dirLevel++;
} else {
$updatelog .= "FAILED TO CREATE DIRECTORY: " . $dir . "\n";
}
}
}
}
$filedata = base64_decode($this->call('update/getFileContent/', array('file' => implode('/', $file['locations']['location']) . '/' . $file['name'], 'beta' => $data['beta'])));
$tmpFile = DIR_CACHE . 'openbay.tmp';
$fp = fopen($tmpFile, 'w');
fwrite($fp, $filedata);
fclose($fp);
if (ftp_put($connection, $file['name'], $tmpFile, FTP_BINARY)) {
$updatelog .= "Updated file: " . $dir . $file['name'] . "\n";
} else {
$updatelog .= "FAILED TO UPDATE FILE: " . $dir . $file['name'] . "\n";
}
unlink($tmpFile);
while ($dirLevel != 0) {
ftp_cdup($connection);
$dirLevel--;
}
}
$openbay_settings = $this->model_setting_setting->getSetting('openbaymanager');
$openbay_settings['openbay_version'] = $files['version'];
$this->model_setting_setting->editSetting('openbaymanager', $openbay_settings);
@ftp_close($connection);
/**
* Run the patch files
*/
$this->load->model('openbay/ebay_patch');
$this->model_openbay_ebay_patch->runPatch(false);
$this->load->model('openbay/amazon_patch');
$this->model_openbay_amazon_patch->runPatch(false);
$this->load->model('openbay/amazonus_patch');
$this->model_openbay_amazonus_patch->runPatch(false);
/**
* File remove operation (clean up old files)
*/
$updatelog .= "\n\n\nStarting Remove\n\n\n";
$connection = @ftp_connect($data['server']);
@ftp_login($connection, $data['user'], $data['pw']);
if (!empty($data['rootpath'])) {
@ftp_chdir($connection, $data['rootpath']);
$directory_list = ftp_nlist($connection, $data['rootpath']);
}
$filesUpdate = $files;
$files = $this->call('update/getRemoveList/', $send);
$updatelog .= "Remove Files: " . print_r($files, 1);
if (!empty($files['asset']) && is_array($files['asset'])) {
foreach ($files['asset'] as $file) {
$dir = '';
$dirLevel = 0;
$error = false;
if (!empty($file['locations'])) {
foreach ($file['locations']['location'] as $location) {
$dir .= $location . '/';
$updatelog .= "Current location: " . $dir . "\n";
// Added to allow OC security where the admin directory is renamed
if ($location == 'admin') {
$location = $data['adminDir'];
}
if (@ftp_chdir($connection, $location)) {
$updatelog .= $location . "/ found\n";
$dirLevel++;
} else {
// folder does not exist, therefore, file does not exist.
$updatelog .= "$location not found\n";
$error = true;
break;
}
}
}
if (!$error) {
//remove the file
$updatelog .= "File: " . $file['name'] . "\n";
$updatelog .= "Size:" . ftp_size($connection, $file['name']) . "\n";
if (@ftp_size($connection, $file['name']) != -1) {
@ftp_delete($connection, $file['name']);
$updatelog .= "Removed\n";
} else {
$updatelog .= "File not found\n";
}
}
while ($dirLevel != 0) {
ftp_cdup($connection);
$dirLevel--;
}
}
}
}
$updatelog .= "Update complete\n\n\n";
$output = ob_get_contents();
ob_end_clean();
$this->writeUpdateLog($updatelog . "\n\n\nErrors:\n" . $output);
return array('connection' => true, 'msg' => sprintf($this->language->get('update_success'), $filesUpdate['version']), 'version' => $filesUpdate['version']);
} else {
return array('connection' => false, 'msg' => $this->language->get('update_failed_user'));
}
} else {
return array('connection' => false, 'msg' => $this->language->get('update_failed_connect'));
}