UPGRADE NOTE:
Do not edit or add to this file if you wish to upgrade AbanteCart to newer
versions in the future. If you wish to customize AbanteCart for your
needs please refer to http://www.AbanteCart.com for more information.
------------------------------------------------------------------------------*/
if (!defined('DIR_CORE') || !IS_ADMIN) {
header('Location: static_pages/');
}
if (defined('IS_DEMO') && IS_DEMO) {
header('Location: static_pages/demo_mode.php');
}
/**
* Class ControllerPagesToolPackageInstaller
* @property ModelToolMPApi $model_tool_mp_api
*/
class ControllerPagesToolPackageInstaller extends AController {
private $data;
public function main() {
//clean temporary directory
$this->_clean_temp_dir();
$package_info = &$this->session->data['package_info'];
$extension_key = !$this->request->get['extension_key'] ? '' : trim($this->request->get['extension_key']);
$extension_key = !$this->request->post['extension_key'] ? $extension_key : trim($this->request->post['extension_key']);
$extension_key = $package_info['extension_key'] ? $package_info['extension_key'] : $extension_key;
$this->session->data['package_info'] = array();
$this->document->setTitle($this->language->get('heading_title'));
$this->document->initBreadcrumb(array(
'href' => $this->html->getSecureURL('index/home'),
'text' => $this->language->get('text_home'),
'separator' => FALSE ));
$this->document->addBreadcrumb(array(
'href' => $this->html->getSecureURL('tool/package_installer'),
'text' => $this->language->get('heading_title'),
'separator' => ' :: ',
'current' => true));
$form = new AForm('ST');
$form->setForm(
array( 'form_name' => 'installFrm' )
);
$this->data['form']['form_open'] = $form->getFieldHtml(
array(
'type' => 'form',
'name' => 'installFrm',
'action' => $this->html->getSecureURL('tool/package_installer/download'),
'attr' => 'data-confirm-exit="true" class="aform form-horizontal"'
));
$this->data['form']['fields']['input'] = $form->getFieldHtml(
array(
'type' => 'input',
'name' => 'extension_key',
'value' => $extension_key,
'attr' => 'autocomplete="off" ',
'placeholder' => $this->language->get('text_key_hint'),
'help_url' => $this->gen_help_url('extension_key'), ));
$this->data['form']['submit'] = $form->getFieldHtml(
array(
'type' => 'button',
'name' => 'submit',
'text' => $this->language->get('text_continue')));
if (isset($this->session->data['error'])) {
$this->view->assign('error_warning', $this->session->data['error']);
unset($package_info['package_dir'], $this->session->data['error']);
}
unset($this->session->data['error'], $this->session->data['success']);
$package_info['package_source'] = 'network';
$this->data['heading_title'] = $this->language->get('heading_title');
$this->_initTabs('key');
$this->view->assign('help_url', $this->gen_help_url(''));
$this->view->batchAssign($this->data);
$this->processTemplate('pages/tool/package_installer.tpl');
}
// method for uploading package via form
public function upload() {
//clean temporary directory
$this->_clean_temp_dir();
$this->session->data['package_info'] = array();
$package_info = &$this->session->data['package_info'];
$package_info['package_source'] = 'file';
// check destination
$package_info['tmp_dir'] = $this->_get_temp_dir();
if (!is_writable($package_info['tmp_dir'])) {
$this->session->data['error'] = $this->language->get('error_dir_permission') . ' ' . $package_info['tmp_dir'];
unset($this->session->data['package_info']);
$this->redirect($this->html->getSecureURL('tool/package_installer/upload'));
}
// process post
if ($this->request->is_POST()) {
if (is_uploaded_file($this->request->files['package_file']['tmp_name'])) {
if(!is_int(strpos($this->request->files['package_file']['name'],'.tar.gz'))){
unlink($this->request->files['package_file']['tmp_name']);
$this->session->data['error'] .= $this->language->get('error_archive_extension');
}else{
$result = move_uploaded_file($this->request->files['package_file']['tmp_name'],
$package_info['tmp_dir'] . $this->request->files['package_file']['name']);
if (!$result || $this->request->files['package_file']['error']) {
$this->session->data['error'] .= '
Error: ' . getTextUploadError($this->request->files['package_file']['error']);
} else {
$package_info['package_name'] = $this->request->files['package_file']['name'];
$package_info['package_size'] = $this->request->files['package_file']['size'];
$this->redirect($this->html->getSecureURL('tool/package_installer/agreement'));
}
}
}else{
if($this->request->post['package_url']){
$package_info['package_url'] = $this->request->post['package_url'];
$this->redirect($this->html->getSecureURL('tool/package_installer/download'));
}else{
$this->session->data['error'] .= '
Error: ' . getTextUploadError($this->request->files['package_file']['error']);
}
}
}
$this->document->setTitle($this->language->get('heading_title'));
$this->document->initBreadcrumb(array(
'href' => $this->html->getSecureURL('index/home'),
'text' => $this->language->get('text_home'),
'separator' => FALSE ));
$this->document->addBreadcrumb(array(
'href' => $this->html->getSecureURL('tool/package_installer'),
'text' => $this->language->get('heading_title'),
'separator' => ' :: ',
'current' => true));
$form = new AForm('ST');
$form->setForm(
array( 'form_name' => 'uploadFrm' )
);
$this->data['form']['form_open'] = $form->getFieldHtml(
array(
'type' => 'form',
'name' => 'uploadFrm',
'action' => $this->html->getSecureURL('tool/package_installer/upload'),
'attr' => 'data-confirm-exit="true" class="aform form-horizontal"'
));
$this->data['form']['fields']['upload_file'] = $form->getFieldHtml(
array(
'type' => 'file',
'name' => 'package_file',
'value' => '',
'attr' => ' autocomplete="off" ' ));
$this->data['form']['fields']['upload_url'] = $form->getFieldHtml(
array(
'type' => 'input',
'name' => 'package_url',
'value' => '',
'attr' => ' autocomplete="off" '));
$this->data['form']['submit'] = $form->getFieldHtml(
array(
'type' => 'button',
'name' => 'submit',
'text' => $this->language->get('text_continue')));
if (isset($this->session->data['error'])) {
$this->view->assign('error_warning', $this->session->data['error']);
unset($package_info['package_dir']);
}
unset($this->session->data['error']);
$this->data['heading_title'] = $this->language->get('heading_title');
$this->_initTabs('upload');
$this->data['upload'] = true;
$this->data['text_or'] = $this->language->get('text_or');
$this->view->assign('help_url', $this->gen_help_url(''));
$this->view->batchAssign($this->data);
$this->processTemplate('pages/tool/package_installer.tpl');
}
private function _initTabs($active = null) {
$this->data['tabs'] = array();
$this->data['tabs']['key'] = array(
'href' => $this->html->getSecureURL('tool/package_installer'),
'text' => $this->language->get('text_network_install'));
$this->data['tabs']['upload'] = array(
'href' => $this->html->getSecureURL('tool/package_installer/upload'),
'text' => $this->language->get('text_extension_upload'));
if (in_array($active, array_keys($this->data['tabs']))) {
$this->data['tabs'][$active]['active'] = 1;
} else {
$this->data['tabs']['key']['active'] = 1;
}
}
public function download() {
$package_info = &$this->session->data['package_info']; // for short code
$extension_key = trim($this->request->post_or_get('extension_key'));
if (!$extension_key && !$package_info['package_url']) {
$this->redirect($this->_get_begin_href());
}
if ($this->request->is_POST()) { // if does not agree with agreement of filesize
if ($this->request->post['disagree'] == 1) {
$this->_removeTempFiles();
unset($this->session->data['package_info']);
$this->redirect($this->html->getSecureURL('extension/extensions/extensions'));
} else {
$disclaimer = (int)$this->request->get['disclaimer'];
$this->session->data['installer_disclaimer'] = true; // prevent multiple show for disclaimer
}
}
if ( $this->_isCorePackage($extension_key) ) {
$disclaimer = true;
}
if (!$disclaimer && !$this->session->data['installer_disclaimer']) {
$this->view->assign('heading_title', $this->language->get('text_disclaimer_heading'));
$form = new AForm('ST');
$form->setForm(array( 'form_name' => 'Frm' ));
$this->data['form']['form_open'] = $form->getFieldHtml(array(
'type' => 'form',
'name' => 'Frm',
'action' => $this->html->getSecureURL('tool/package_installer/download'),
'attr' => 'data-confirm-exit="true" class="aform form-horizontal"',
));
$this->data['form']['hidden'][] = $form->getFieldHtml(array(
'id' => 'extension_key',
'type' => 'hidden',
'name' => 'extension_key',
'value' => $extension_key ));
$this->data['agreement_text'] = $this->language->get('text_disclaimer');
$this->data['form']['disagree_button'] = $form->getFieldHtml(array(
'type' => 'button',
'href' => $this->_get_begin_href(),
'text' => $this->language->get('text_interrupt') ));
$this->data['form']['submit'] = $form->getFieldHtml(array(
'type' => 'button',
'text' => $this->language->get('text_agree')));
$this->data['form']['agree'] = $form->getFieldHtml(array(
'type' => 'hidden',
'name' => 'disclaimer',
'value' => '0' ));
$this->view->batchAssign($this->data);
$this->processTemplate('pages/tool/package_installer_agreement.tpl');
return null;
}
$form = new AForm('ST');
$form->setForm(array( 'form_name' => 'retryFrm' ));
$this->data['form']['form_open'] = $form->getFieldHtml(array(
'type' => 'form',
'name' => 'retryFrm',
'action' => $this->html->getSecureURL('tool/package_installer/download') ));
$this->data['form']['hidden'][ ] = $form->getFieldHtml(array(
'id' => 'extension_key',
'type' => 'hidden',
'name' => 'extension_key',
'value' => $extension_key ));
$this->data['form']['hidden'][ ] = $form->getFieldHtml(array(
'id' => 'disclaimer',
'type' => 'hidden',
'name' => 'disclaimer',
'value' => '1' ));
$this->data['form']['cancel'] = $form->getFieldHtml(array(
'type' => 'button',
'href' => $this->_get_begin_href(),
'text' => $this->language->get('button_cancel') ));
$this->data['form']['retry'] = $form->getFieldHtml(array(
'type' => 'button',
'text' => $this->language->get('text_retry')
));
$this->view->assign('text_download_error', $this->language->get('text_download_error'));
$package_info['extension_key'] = $extension_key;
$package_info['tmp_dir'] = $this->_get_temp_dir();
if (!is_writable($package_info['tmp_dir'])) {
$this->session->data['error'] = $this->language->get('error_dir_permission') . ' ' . $package_info['tmp_dir'];
unset($this->session->data['package_info']);
$this->redirect($this->html->getSecureURL('tool/package_installer'));
}
//do condition for MP
$this->loadModel('tool/mp_api');
if($extension_key) {
if( substr($extension_key,0,4) == 'acmp' ){ // if prefix for new mp presents
$url = $this->model_tool_mp_api->getMPURL().'?rt=r/account/download/getdownloadbykey';
}else{ // for upgrades
$url = "/?option=com_abantecartrepository&format=raw";
}
$url .= "&remote_store_id=" . UNIQUE_ID;
$url .= "&remote_store_ip=" . $_SERVER ['SERVER_ADDR'];
$url .= "&remote_store_url=" . HTTP_SERVER;
$url .= "&store_version=" . VERSION;
$url .= "&extension_key=" . $extension_key;
} else {
$url = $package_info['package_url'];
}
$pmanager = new APackageManager();
$headers = $pmanager->getRemoteFileHeaders($url);
if (!$headers) {
$this->session->data['error'] = $pmanager->error;
$this->redirect($this->_get_begin_href());
}
if ($headers['Content-Type'] == 'application/json') {
$error = $pmanager->getRemoteFile($url, false);
$this->session->data['error'] = $error['error'];
$this->redirect($this->_get_begin_href());
} else {
$package_name = str_replace("attachment; filename=", "", $headers['Content-Disposition']);
$package_name = str_replace(array( '"', ';' ), '', $package_name);
if(!$package_name){
$package_name = parse_url($url);
if(pathinfo($package_name['path'],PATHINFO_EXTENSION)){
$package_name = pathinfo($package_name['path'],PATHINFO_BASENAME);
}else{
$package_name = '';
}
}
if (!$package_name) {
$this->session->data['error'] = $this->language->get('error_repository');
$this->redirect($this->_get_begin_href());
}
}
$package_info['package_url'] = $url;
$package_info['package_name'] = $package_name;
$package_info['package_size'] = $headers['Content-Length'];
// if file already downloaded - check size.
if (file_exists($package_info['tmp_dir'] . $package_name)) {
$filesize = filesize($package_info['tmp_dir'] . $package_name);
if ($filesize != $package_info['package_size']) {
@unlink($package_info['tmp_dir'] . $package_name);
} else {
if ($this->request->get['agree'] == '1') {
$this->redirect($this->html->getSecureURL('tool/package_installer/agreement'));
} else {
$already_downloaded = true;
$this->redirect($this->html->getSecureURL('tool/package_installer/agreement'));
}
}
}
$this->data['url'] = $this->html->getSecureURL('tool/package_download');
$this->data['redirect'] = $this->html->getSecureURL('tool/package_installer/agreement');
$this->document->initBreadcrumb(array(
'href' => $this->html->getSecureURL('index/home'),
'text' => $this->language->get('text_home'),
'separator' => FALSE ));
$this->document->addBreadcrumb(array(
'href' => $this->html->getSecureURL('tool/package_installer'),
'text' => $this->language->get('heading_title'),
'separator' => ' :: ',
'current' => true));
$this->data['heading_title'] = $this->language->get('heading_title_download');
$this->data['loading'] = sprintf($this->language->get('text_loading'), (round($package_info['package_size'] / 1024, 1)) . 'kb');
$package_info['install_mode'] = !$package_info['install_mode'] ? 'install' : $package_info['install_mode'];
if (!$already_downloaded) {
$this->data['pack_info'] .= str_replace('%file%', $package_name . ' (' . (round($package_info['package_size'] / 1024, 1)) . 'kb)', $this->language->get('text_preloading'));
}
$this->view->batchAssign($this->data);
$this->processTemplate('pages/tool/package_installer_download.tpl');
}
public function agreement() {
$package_info = &$this->session->data['package_info'];
// if we got decision
if ($this->request->is_POST()) {
if ($this->request->post['disagree'] == 1) { // if does not agree with agreement of filesize
$this->_removeTempFiles();
unset($this->session->data['package_info']);
$this->redirect($this->html->getSecureURL('extension/extensions/extensions'));
} elseif ($this->request->post['agree_incompatibility']) {
$package_info['confirm_version_incompatibility'] = true;
$this->redirect($this->html->getSecureURL('tool/package_installer/agreement'));
} elseif ($this->request->post['agree']) { // if agree
$this->redirect($this->html->getSecureURL('tool/package_installer/install'));
} elseif (!$this->request->post['agree'] && !isset($this->request->post['ftp_user'])) {
$this->_removeTempFiles('dir');
$this->redirect($this->_get_begin_href());
}
}
$this->loadLanguage('tool/package_installer');
$package_name = $package_info['package_name'];
if (!$package_name) { // if direct link - redirect to the begining
$this->redirect($this->_get_begin_href());
}
$pmanager = new APackageManager();
//unpack package
// if package not unpack - redirect to the begin and show error message
if (!$pmanager->unpack($package_info['tmp_dir'] . $package_name, $package_info['tmp_dir'])) {
$this->session->data['error'] = str_replace('%PACKAGE%', $package_info['tmp_dir'].$package_name, $this->language->get('error_unpack'));
$error = new AError ($pmanager->error);
$error->toLog()->toDebug();
$this->redirect($this->_get_begin_href());
}
$package_dirname = $package_info['package_dir'] = $this->_find_package_dir();
if(!$package_info['package_dir']){
$error = 'Error: Cannot to find package directory after unpacking archive. ';
$error = new AError ( $error );
$error->toLog ()->toDebug ();
}
if (!file_exists($package_info['tmp_dir'] . $package_dirname)) {
$this->session->data['error'] = str_replace('%PACKAGE%', $package_info['tmp_dir'] . $package_dirname, $this->language->get('error_pack_not_found'));
$this->redirect($this->_get_begin_href());
}
// so.. we need to know about install mode of this package
$config = simplexml_load_string(file_get_contents($package_info['tmp_dir'] . $package_dirname . '/package.xml'));
if (!$config) {
$this->session->data['error'] = $this->language->get('error_package_config');
$this->_removeTempFiles();
$this->redirect($this->_get_begin_href());
}
$package_info['package_id'] = (string)$config->id;
$package_info['package_type'] = (string)$config->type;
$package_info['package_priority'] = (string)$config->priority;
$package_info['package_version'] = (string)$config->version;
$package_info['package_content'] = '';
if ((string)$config->package_content->extensions) {
foreach ($config->package_content->extensions->extension as $item) {
if ((string)$item) {
$package_info['package_content']['extensions'][ ] = (string)$item;
}
}
$package_info['package_content']['total'] = sizeof($package_info['package_content']['extensions']);
}
if ((string)$config->package_content->core) {
foreach ($config->package_content->core->files->file as $item) {
if ((string)$item) {
$package_info['package_content']['core'][ ] = (string)$item;
}
}
}
if (!$package_info['package_content']
|| ($package_info['package_content']['core'] && $package_info['package_content']['extensions'])
) {
$this->session->data['error'] = $this->language->get('error_package_structure');
$this->_removeTempFiles();
$this->redirect($this->_get_begin_href());
}
//check cart version compability
if (!isset($package_info['confirm_version_incompatibility'])) {
if (!$this->_check_cart_version($config)) {
if($this->_isCorePackage()){
$this->session->data['error'] = $this->language->get('error_package_version_compatibility');
$this->redirect($this->html->getSecureURL('tool/package_installer'));
}else{
$this->redirect($this->html->getSecureURL('tool/package_installer/agreement'));
}
}
}
// if we were redirected
if ($this->request->is_GET()) {
//check write permissions
// find directory from app_root_dir
if ($package_info['package_content']['extensions']) {
$dst_dirs = $pmanager->getDestinationDirectories();
$ftp_mode = false;
// if even one destination directory is not writable - use ftp mode
if(!is_writable(DIR_EXT)){
$non_writables[] = DIR_EXT;
}
if ($dst_dirs) {
foreach ($dst_dirs as $dir) {
if (!is_writable(DIR_ROOT . '/' . $dir) && file_exists(DIR_ROOT . '/' . $dir)) {
$ftp_mode = true; // enable ftp-mode
$non_writables[ ] = DIR_ROOT . '/' . $dir;
}
}
}
} else {
foreach ($package_info['package_content']['core'] as $corefile) {
if( (!is_writable(DIR_ROOT . '/' . $corefile) && file_exists(DIR_ROOT . '/' . $corefile))
||
(!is_writable(pathinfo(DIR_ROOT . '/' . $corefile,PATHINFO_DIRNAME)) && is_dir(pathinfo(DIR_ROOT . '/' . $corefile,PATHINFO_DIRNAME)))
) {
$ftp_mode = true; // enable ftp-mode
$non_writables[ ] = DIR_ROOT . '/' . $corefile;
}
}
}
}
// if ftp mode and user give ftp parameters
if (isset($this->request->post['ftp_user']) && $this->request->is_POST()) {
$ftp_user = $this->request->post['ftp_user'];
$ftp_password = $this->request->post['ftp_password'];
$ftp_host = $this->request->post['ftp_host'];
//let's try to connect
if (!$pmanager->checkFTP($ftp_user, $ftp_password, $ftp_host)) {
$this->session->data['error'] = $pmanager->error;
$this->redirect($this->html->getSecureURL('tool/package_installer/agreement'));
}
$ftp_mode = false; // sign of ftp-form
$this->redirect($this->html->getSecureURL('tool/package_installer/install'));
} else {
if (!$package_info['tmp_dir']) {
$package_info['tmp_dir'] = $this->_get_temp_dir();
}
}
// if all fine show license agreement
if (!file_exists($package_info['tmp_dir'] . $package_dirname . "/license.txt") && !$ftp_mode) {
$this->redirect($this->html->getSecureURL('tool/package_installer/install'));
}
$this->data['license_text'] = '';
$this->document->initBreadcrumb(array(
'href' => $this->html->getSecureURL('index/home'),
'text' => $this->language->get('text_home'),
'separator' => FALSE ));
$this->document->addBreadcrumb(array(
'href' => $this->html->getSecureURL('tool/package_installer'),
'text' => $this->language->get('heading_title'),
'separator' => ' :: ',
'current' => true));
if (isset($this->session->data['error'])) {
$this->view->assign('error_warning', $this->session->data['error']);
unset($this->session->data['error']);
}
$form = new AForm('ST');
$form->setForm(array( 'form_name' => 'Frm' ));
$this->data['form']['form_open'] = $form->getFieldHtml(array(
'type' => 'form',
'name' => 'Frm',
'action' => $this->html->getSecureURL('tool/package_installer/agreement'),
'attr' => 'data-confirm-exit="true" class="aform form-horizontal"',
));
//version incompatibility confirmation
if ((isset($package_info['confirm_version_incompatibility']) && !$package_info['confirm_version_incompatibility'])) {
$this->data['agreement_text'] = $package_info['version_incompatibility_text'];
$this->data['form']['disagree_button'] = $form->getFieldHtml(array(
'type' => 'button',
'href' => $this->_get_begin_href(),
'text' => $this->language->get('text_interrupt') ));
$this->data['form']['submit'] = $form->getFieldHtml(array(
'type' => 'button',
'text' => $this->language->get('text_continue')));
$this->data['form']['agree'] = $form->getFieldHtml(array(
'type' => 'hidden',
'name' => 'agree_incompatibility',
'value' => '0' ));
$template = 'pages/tool/package_installer_agreement.tpl';
} // confirmation for ftp access to file system
elseif ($ftp_mode) {
$template = 'pages/tool/package_installer_ftp_form.tpl';
$ftp_user = $package_info['ftp_user'] ? $package_info['ftp_user'] : '';
$ftp_host = $package_info['ftp_host'] ? $package_info['ftp_host'] : '';
$this->data['form']['fuser'] = $form->getFieldHtml(array(
'type' => 'input',
'name' => 'ftp_user',
'value' => $ftp_user,
'require' => true,
'help_url' => $this->gen_help_url('ftp_user'),
'style' => 'medium-field'));
$this->data['form']['fpass'] = $form->getFieldHtml(array(
'type' => 'password',
'name' => 'ftp_password',
'require' => true,
'value' => '',
'style' => 'medium-field'));
$this->data['form']['fhost'] = $form->getFieldHtml(array(
'type' => 'input',
'name' => 'ftp_host',
'value' => $ftp_host,
'help_url' => $this->gen_help_url('ftp_host'),
'style' => 'medium-field' ));
$this->data['form']['submit'] = $form->getFieldHtml(array(
'type' => 'button',
'text' => $this->language->get('text_continue')
));
$this->data['fuser'] = $this->language->get('text_ftp_user');
$this->data['fpass'] = $this->language->get('text_ftp_password');
$this->data['fhost'] = $this->language->get('text_ftp_host');
$this->data['heading_title'] = $this->language->get('heading_title_ftp');
$this->data['warning_ftp'] = $this->language->get('warning_ftp');
$this->data['warning_ftp_details'] = 'Need write permission for: