Exemple #1
0
 public function install()
 {
     //init controller data
     $this->extensions->hk_InitData($this, __FUNCTION__);
     if (!$this->user->canModify('extension/extensions')) {
         $this->session->data['error'] = $this->language->get('error_permission');
         $this->redirect($this->html->getSecureURL('extension/extensions/' . $this->session->data['extension_filter']));
     } else {
         $validate = $this->extension_manager->validate($this->request->get['extension']);
         if (!$validate) {
             $this->session->data['error'] = implode('<br>', $this->extension_manager->errors);
             $this->redirect($this->html->getSecureURL('extension/extensions/' . $this->session->data['extension_filter']));
         }
         $config = getExtensionConfigXml($this->request->get['extension']);
         if ($config === false) {
             $filename = DIR_EXT . str_replace('../', '', $this->request->get['extension']) . '/config.xml';
             $err = sprintf($this->language->get('error_could_not_load_config'), $this->request->get['extension'], $filename);
             $this->session->data['error'] = $err;
         } else {
             $this->extension_manager->install($this->request->get['extension'], $config);
         }
         $this->redirect($this->html->getSecureURL('extension/extensions/edit', '&extension=' . $this->request->get['extension']));
     }
 }
Exemple #2
0
 public function license()
 {
     //init controller data
     $this->extensions->hk_InitData($this, __FUNCTION__);
     $this->loadLanguage('extension/extensions');
     // first of all we need check dependencies
     $config = getExtensionConfigXml($this->request->get['extension']);
     $result = $this->extension_manager->validateDependencies($this->request->get['extension'], $config);
     if ($result) {
         // if all fine show license agreement
         if (file_exists(DIR_EXT . $this->request->get['extension'] . "/license.txt")) {
             $this->data['license_text'] = file_get_contents(DIR_EXT . $this->request->get['extension'] . "/license.txt");
             $this->data['license_text'] = htmlentities($this->data['license_text'], ENT_QUOTES, 'UTF-8');
             $this->data['license_text'] = nl2br($this->data['license_text']);
         }
     } else {
         $error_text = $this->extension_manager->errors;
         end($error_text);
         $this->data['error_text'] = current($error_text);
     }
     //update controller data
     $this->extensions->hk_UpdateData($this, __FUNCTION__);
     $this->load->library('json');
     $this->response->setOutput(AJson::encode($this->data));
 }
 /**
  * @param string $ext
  * @param int $store_id
  */
 public function __construct($ext, $store_id = 0)
 {
     $this->registry = Registry::getInstance();
     $this->name = (string) $ext;
     $this->store_id = (int) $store_id;
     $this->config = getExtensionConfigXml($ext);
     if (!$this->config) {
         $filename = DIR_EXT . str_replace('../', '', $this->name) . '/config.xml';
         $err = sprintf('Error: Could not load config for <b>%s</b> ( ' . $filename . ')!', $this->name);
         foreach (libxml_get_errors() as $error) {
             $err .= "  " . $error->message;
         }
         $error = new AError($err);
         $error->toLog()->toDebug();
         $this->error[] = $err;
         return null;
     }
     return null;
 }
 /**
  * @param array $data
  * @return bool
  * @throws AException
  */
 public function generateExtension($data = array())
 {
     $project_xml = $config_xml = array();
     //when clone template
     if (has_value($data['clone_method'])) {
         $project_xml['clone_method'] = $data['clone_method'];
         $project_xml['proto_template'] = $data['proto_template'];
     }
     $extension_name = $data['extension_txt_id'] = $this->_prepareData('extension_txt_id', $data['extension_txt_id']);
     if (!$data['extension_txt_id']) {
         $this->error['extension_txt_id'] = 'Extension text Id required.';
         return false;
     }
     $project_xml['extension_txt_id'] = $config_xml['extension_txt_id'] = $extension_name;
     if (file_exists(DIR_EXT . $extension_name)) {
         $this->error[] = 'Extension with text id "' . $extension_name . '" is already exists! Delete it first and refresh page.';
         //return false;
     }
     $data['extension_type'] = $data['extension_type'] == 'other' ? 'extension' : $data['extension_type'];
     $project_xml['extension_type'] = $config_xml['extension_type'] = $data['extension_type'];
     $project_xml['extension_title'] = $data['extension_title'];
     $extension_directory = DIR_EXT . $extension_name;
     $data['header_comment'] = $this->_prepareData('header_comment', $data['header_comment']);
     $project_xml['header_comment'] = $data['header_comment'];
     $data['header_comment'] = "<?php\n" . $data['header_comment'];
     $data['header_comment'] .= "\n\n";
     $data['header_comment'] .= "if (! defined ( 'DIR_CORE' )) {\nheader ( 'Location: static_pages/' );\n}\n\n";
     $project_xml['header_comment'] = $data['header_comment'];
     if ($data['extension_admin_language_files']) {
         $data['extension_admin_language_files'] = $this->_prepareData('extension_admin_language_files', $data['extension_admin_language_files']);
         $project_xml['extension_admin_language_files'] = $data['extension_admin_language_files'];
     }
     if ($data['extension_storefront_language_files']) {
         $data['extension_storefront_language_files'] = $this->_prepareData('extension_storefront_language_files', $data['extension_storefront_language_files']);
         $project_xml['extension_storefront_language_files'] = $data['extension_storefront_language_files'];
     }
     // generating directory and files
     if (!is_dir($extension_directory)) {
         mkdir($extension_directory, 0777);
     }
     if ($data['hook_file']) {
         $project_xml['hook_file'] = $data['hook_file'] = $this->_prepareData('hook_file', $data['hook_file']);
         if (!file_exists($extension_directory . '/core/' . $data['hook_file'])) {
             if (!is_dir($extension_directory . '/core')) {
                 mkdir($extension_directory . '/core', 0777);
             }
             $t = explode('_', $extension_name);
             foreach ($t as &$r) {
                 $r = ucfirst($r);
             }
             unset($r);
             $class_name = implode('', $t);
             $content = "class Extension" . $class_name . " extends Extension {\n\n }\n";
             $hook_class_name = "Extension" . $class_name;
             $project_xml['hook_class_name'] = $hook_class_name;
             file_put_contents($extension_directory . '/core/' . $data['hook_file'], $data['header_comment'] . $content);
         }
     }
     $project_xml['route'] = $data['route'];
     //GENERATING PHP FILES
     // do by letters M.V.C. order
     //MODELS
     $models = array('admin' => array(), 'storefront' => array());
     foreach ($this->sections as $section) {
         if (!isset($data[$section . '_model_routes'])) {
             continue;
         }
         foreach ($data[$section . '_model_routes'] as $k => $route) {
             $file = trim($data[$section . '_model_files'][$k], '\\/ ');
             if ($route && $file) {
                 $route = trim($route, '\\/ ');
                 $file = substr($file, -4) != '.php' ? $file . '.php' : $file;
                 $content = $data['header_comment'];
                 // build class name
                 $class_name = 'Model';
                 $rt = str_replace('/', '_', $route);
                 $rt = explode('_', $rt);
                 foreach ($rt as &$r) {
                     $r = ucfirst($r);
                 }
                 unset($r);
                 $class_name .= implode('', $rt);
                 $rt = str_replace('.php', '', $file);
                 $rt = explode('_', $rt);
                 foreach ($rt as &$r) {
                     $r = ucfirst($r);
                 }
                 unset($r);
                 $class_name .= implode('', $rt);
                 $content .= "class " . $class_name . " extends Model {\n\n\t\t\tpublic \$data = array ();\n\t\t\tprivate \$error = array ();\n }\n";
                 $dir = $extension_directory . '/' . $section . '/model/' . $route;
                 if (!is_dir($dir)) {
                     mkdir($dir, 0777, true);
                 }
                 if (!file_exists($dir . '/' . $file)) {
                     file_put_contents($dir . '/' . $file, $content);
                 }
                 $rt = str_replace('.php', '', $file);
                 $models[$section][] = $route . '/' . $rt;
                 $project_xml['models'][$section][] = array('route' => $route, 'file' => $rt);
             }
         }
     }
     // VIEWS (tpl-files)
     $views = array('admin' => array(), 'storefront' => array());
     foreach ($this->sections as $section) {
         foreach (array('page', 'response') as $ctrl_type) {
             if (!isset($data[$section . '_' . $ctrl_type . '_view_routes'])) {
                 continue;
             }
             foreach ($data[$section . '_' . $ctrl_type . '_view_routes'] as $k => $route) {
                 $file = trim($data[$section . '_' . $ctrl_type . '_view_files'][$k], '\\/ ');
                 if ($route && $file) {
                     $route = trim($route, '\\/ ');
                     $file = substr($file, -4) != '.tpl' ? $file . '.tpl' : $file;
                     $route_prefix = $this->getRtPrefixByControllerType($ctrl_type);
                     $template_name = $data['extension_type'] == 'template' ? $extension_name : 'default';
                     $dir = $extension_directory . '/' . $section . '/view/' . $template_name . '/template/' . $route_prefix . '/' . $route;
                     if (!is_dir($dir)) {
                         mkdir($dir, 0777, true);
                     }
                     if (!file_exists($dir . '/' . $file)) {
                         file_put_contents($dir . '/' . $file, '');
                     }
                     $views[$section][$ctrl_type][] = $route_prefix . '/' . $route . '/' . $file;
                     $project_xml['views'][$section][] = array('route' => $route_prefix . '/' . $route, 'file' => $file);
                 }
             }
         }
     }
     // Controllers
     $controllers = array('admin' => array(), 'storefront' => array());
     foreach ($this->sections as $section) {
         foreach (array('page', 'response', 'block', 'form', 'common', 'api', 'task') as $ctrl_type) {
             if (!isset($data[$section . '_' . $ctrl_type . '_controller_routes'])) {
                 continue;
             }
             foreach ($data[$section . '_' . $ctrl_type . '_controller_routes'] as $k => $route) {
                 $file = trim($data[$section . '_' . $ctrl_type . '_controller_files'][$k], '\\/ ');
                 if ($route && $file) {
                     $route = trim($route, '\\/ ');
                     $file = rtrim($file, '.php');
                     $content = $data['header_comment'];
                     $route_prefix = $this->getRtPrefixByControllerType($ctrl_type);
                     // build class name
                     $class_name = 'Controller' . ucfirst($route_prefix);
                     $rt = str_replace('/', '_', $route);
                     $rt = explode('_', $rt);
                     foreach ($rt as &$r) {
                         $r = ucfirst($r);
                     }
                     unset($r);
                     $class_name .= implode('', $rt);
                     $rt = str_replace('.php', '', $file);
                     $rt = explode('_', $rt);
                     foreach ($rt as &$r) {
                         $r = ucfirst($r);
                     }
                     unset($r);
                     $class_name .= implode('', $rt);
                     $content .= "class " . $class_name . " extends AController {\n\n\t\tpublic \$data = array ();\n\t\tprivate \$error = array ();\n }\n";
                     $dir = $extension_directory . '/' . $section . '/controller/' . $route_prefix . '/' . $route;
                     if (!is_dir($dir)) {
                         mkdir($dir, 0777, true);
                     }
                     if (!file_exists($dir . '/' . $file . '.php')) {
                         file_put_contents($dir . '/' . $file . '.php', $content);
                     }
                     $rt = str_replace('.php', '', $file);
                     $controllers[$section][$ctrl_type][] = $route_prefix . '/' . $route . '/' . $rt;
                     $project_xml['controllers'][$section][] = array('route' => $route_prefix . '/' . $route, 'file' => $file);
                 }
             }
         }
     }
     unset($content);
     // LANGUAGE files for extension translates
     $languages = array('admin' => array(), 'storefront' => array());
     //NOTE! unacceptable extension without at least one admin language file
     if (!$data['extension_admin_language_files']) {
         $data['extension_admin_language_files'] = array('english');
     }
     foreach ($this->sections as $section) {
         if (!isset($data['extension_' . $section . '_language_files'])) {
             continue;
         }
         foreach ($data['extension_' . $section . '_language_files'] as $language_name) {
             if ($language_name) {
                 $language_name = strtolower($language_name);
                 $file = $extension_name . '.xml';
                 $languages[$section][$extension_name . '/' . $extension_name] = $extension_name . '/' . $extension_name;
                 $project_xml['languages'][$section][] = $language_name . '/' . $extension_name . '/' . $extension_name;
                 if (file_exists($extension_directory . '/' . $section . '/language/' . strtolower($language_name) . '/' . $extension_name . '/' . $file)) {
                     continue;
                 }
                 $dir = $extension_directory . '/' . $section . '/language/' . $language_name . '/' . $extension_name;
                 if (!is_dir($dir)) {
                     mkdir($dir, 0777, true);
                 }
                 if ($section == 'admin') {
                     $this->_save_base_language_xml($data, $language_name);
                 } else {
                     if (!file_exists($dir . '/' . $file)) {
                         file_put_contents($dir . '/' . $file, '');
                     }
                 }
             }
         }
     }
     // COMMON PART
     if ($data['install_php']) {
         $content = $data['header_comment'] . "\n\n\n";
         $install_content = '';
         //build layout.xml for template clone
         if ($project_xml['extension_type'] == 'template') {
             $this->load->model('tool/developer_tools_layout_xml');
             $this->model_tool_developer_tools_layout_xml->saveXml($data['extension_txt_id'], 'default');
             $install_content = "\$file = DIR_EXT . '/" . $data['extension_txt_id'] . "/layout.xml';\n" . "\$layout = new ALayoutManager('default');\n" . "\$layout->loadXml(array('file' => \$file));\n";
         }
         if (!is_file($extension_directory . '/install.php')) {
             file_put_contents($extension_directory . '/install.php', $content . $install_content);
         }
         if ($project_xml['extension_type'] == 'template') {
             $uninstall_content = "\$extension_id = '" . $data['extension_txt_id'] . "';\n" . "// delete template layouts\n" . "\$layout = new ALayoutManager(\$extension_id);\n" . "\$layout->deleteTemplateLayouts();";
         }
         if (!is_file($extension_directory . '/uninstall.php')) {
             file_put_contents($extension_directory . '/uninstall.php', $content . $uninstall_content);
         }
         unset($content, $install_content, $uninstall_content);
     }
     $project_xml['install_php'] = $config_xml['install_php'] = (int) $data['install_php'];
     if ($data['install_sql']) {
         if (!is_file($extension_directory . '/install.sql')) {
             file_put_contents($extension_directory . '/install.sql', "");
         }
         if (!is_file($extension_directory . '/uninstall.sql')) {
             file_put_contents($extension_directory . '/uninstall.sql', "");
         }
     }
     $project_xml['install_sql'] = $config_xml['install_sql'] = (int) $data['install_sql'];
     $data['help_note'] = trim($data['help_note']);
     $project_xml['help_note'] = $data['help_note'];
     $config_xml['note'] = $data['help_note'] ? true : false;
     $project_xml['help_url'] = (string) $data['help_url'];
     // extension icon
     $project_xml['icon_default'] = $data['icon_default'] = (int) $data['icon_default'];
     if (!$data['icon_default'] && $this->request->files['icon']) {
         if ($this->request->files['icon']['error']) {
             $this->error[] = getTextUploadError($this->request->files['icon']['error']);
         }
         if ($this->request->files['icon']['type'] == 'image/png' && $this->request->files['icon']['size'] > 0) {
             if (!is_dir($extension_directory . '/image')) {
                 mkdir($extension_directory . '/image', 0777);
             }
             move_uploaded_file($this->request->files['icon']["tmp_name"], $extension_directory . '/image/icon.png');
         }
         $project_xml['icon'] = 'icon.png';
     }
     /*if($this->request->files['preview']){
     
     			foreach($this->request->files['preview']['name'] as $k=>$item){
     				if($this->request->files['preview']['error'][$k]){
     					$this->error[] = getTextUploadError($this->request->files['preview']['error'][$k]);
     				}
     
     				if($this->request->files['preview']['type'][$k]=='image/png' && $this->request->files['preview']['size'][$k]>0){
     					if(!is_dir($extension_directory.'/image')){
     						mkdir($extension_directory.'/image',0777);
     					}
     					move_uploaded_file($this->request->files['preview']["tmp_name"][$k],$extension_directory.'/image/preview'.$k.'.png');
     					$project_xml['preview'][] = 'preview'.$k.'.png';
     					$config_xml['preview'][] = 'preview'.$k.'.png';
     				}
     			}
     		} */
     $project_xml['version'] = $config_xml['version'] = $data['version'];
     $project_xml['category'] = $config_xml['category'] = $data['extension_category'];
     $data['cartversions'] = array_unique((array) $data['cartversions']);
     $project_xml['cartversions'] = $config_xml['cartversions'] = $data['cartversions'];
     $project_xml['priority'] = $config_xml['priority'] = (int) $data['priority'];
     /*if($data['dependency']){
     			$project_xml['dependencies'] = array();
     			foreach($data['dependency']['name'] as $k => $dep_name){
     				if($dep_name){
     					$project_xml['dependencies']['item'][] = array('@attributes' => array(
     							'prior_version' => $data['dependency']['prior_versions'][$k],
     							'version'       => $data['dependency']['versions'][$k]),
     																   '@value'      => $dep_name);
     				}
     			}
     			$config_xml['dependencies'] = $project_xml['dependencies'];
     		}*/
     $config_xml['settings'] = array('item' => array('@attributes' => array('id' => $data['extension_txt_id'] . "_status"), 'type' => 'checkbox', 'default_value' => '0'));
     $config_xml['note'] = $data['help_note'] ? true : false;
     if ($data['help_file']) {
         $data['help_file'] = trim((string) $data['help_file']);
         $project_xml['help_file'] = $data['help_file'];
         if (!is_dir(DIR_EXT . $extension_name . "/help")) {
             mkdir(DIR_EXT . $extension_name . "/help", 0777);
         }
         file_put_contents(DIR_EXT . $extension_name . "/help/" . $extension_name . '.txt', $data['help_file']);
         $data['help_file'] = "help/" . $extension_name . '.txt';
     }
     $config_xml['install_sql'] = $data['install_sql'];
     $config_xml['install_php'] = $data['install_php'];
     $this->saveConfigXml($config_xml);
     // change mode recurcive
     $this->_chmod_R($extension_directory, 0777, 0777);
     $this->_replicate_default_dir_tree_($project_xml);
     // when cloning template check clone_method var
     // save project xml
     $this->saveProjectXml($project_xml);
     $this->saveMainFileByProjectConfig($project_xml);
     $this->session->data['dev_tools_prj_id'] = $project_xml['extension_txt_id'] . '_v' . $project_xml['version'];
     //for cloning of template need to install
     if ($project_xml['clone_method']) {
         //1. refresh extensions list to write new into db, table extensions
         $ex = new ExtensionsApi();
         unset($ex);
         $em = new AExtensionManager();
         $em->install($project_xml['extension_txt_id'], getExtensionConfigXml($project_xml['extension_txt_id']));
         //enable
         $em->editSetting($project_xml['extension_txt_id'], array($project_xml['extension_txt_id'] . '_status' => 1));
     }
     return true;
 }
 /**
  * @param string $extension_id
  * @param string $type
  * @param string $version
  * @param string $install_mode
  * @return bool
  */
 public function installExtension($extension_id = '', $type = '', $version = '', $install_mode = 'install')
 {
     $type = !$type ? $this->session->data['package_info']['package_type'] : $type;
     $version = !$version ? $this->session->data['package_info']['package_version'] : $version;
     $extension_id = !$extension_id ? $this->session->data['package_info']['package_id'] : $extension_id;
     $package_dirname = $this->session->data['package_info']['package_dir'];
     switch ($type) {
         case 'extension':
         case 'extensions':
         case 'template':
         case 'payment':
         case 'shipping':
         case 'language':
             // if extensions is not installed yet - install it
             if ($install_mode == 'install') {
                 $validate = $this->extension_manager->validate($extension_id);
                 $validateErrors = $this->extension_manager->errors;
                 if (!$validate) {
                     $this->error = implode('<br>', $validateErrors);
                     $err = new AError($this->error);
                     $err->toLog()->toDebug();
                     return false;
                 }
                 $result = $this->extension_manager->install($extension_id, getExtensionConfigXml($extension_id));
                 if ($result === false) {
                     return false;
                 }
             } elseif ($install_mode == 'upgrade') {
                 $install_upgrade_history = new ADataset('install_upgrade_history', 'admin');
                 $install_upgrade_history->addRows(array('date_added' => date("Y-m-d H:i:s", time()), 'name' => $extension_id, 'version' => $version, 'backup_file' => '', 'backup_date' => '', 'type' => 'upgrade', 'user' => $this->user->getUsername()));
                 $config = simplexml_load_string(file_get_contents($this->session->data['package_info']['tmp_dir'] . $package_dirname . '/code/extensions/' . $extension_id . '/config.xml'));
                 $config = !$config ? getExtensionConfigXml($extension_id) : $config;
                 // running sql upgrade script if it exists
                 if (isset($config->upgrade->sql)) {
                     $file = $this->session->data['package_info']['tmp_dir'] . $package_dirname . '/code/extensions/' . $extension_id . '/' . (string) $config->upgrade->sql;
                     $file = !file_exists($file) ? DIR_EXT . $extension_id . '/' . (string) $config->upgrade->sql : $file;
                     if (file_exists($file)) {
                         $this->db->performSql($file);
                     }
                 }
                 // running php install script if it exists
                 if (isset($config->upgrade->trigger)) {
                     $file = $this->session->data['package_info']['tmp_dir'] . $package_dirname . '/code/extensions/' . $extension_id . '/' . (string) $config->upgrade->trigger;
                     $file = !file_exists($file) ? DIR_EXT . $extension_id . '/' . (string) $config->upgrade->sql : $file;
                     if (file_exists($file)) {
                         /** @noinspection PhpIncludeInspection */
                         include $file;
                     }
                 }
                 $this->extension_manager->editSetting($extension_id, array('license_key' => $this->session->data['package_info']['installation_key'], 'version' => $version));
             }
             break;
         default:
             $this->error = 'Unknown extension type: "' . $type . '"';
             $err = new AError($this->error);
             $err->toLog()->toDebug();
             return false;
             break;
     }
     return true;
 }
 /**
  *  is dependendants installed?
  * @param string $extension_txt_id
  * @return bool
  */
 public function checkDependants($extension_txt_id)
 {
     $extensions = $this->extensions->getInstalled('exts');
     foreach ($extensions as $extension) {
         if ($extension == $extension_txt_id) {
             continue;
         }
         $config = getExtensionConfigXml($extension);
         if (!isset($config->dependencies->item)) {
             continue;
         }
         foreach ($config->dependencies->item as $item) {
             $required = (bool) $item['required'];
             $item = (string) $item;
             if ($item == $extension_txt_id && $required) {
                 $this->errors[] = sprintf('<b>%s</b> extension cannot be uninstalled: <b>%s</b> extension depends on it. Please uninstall it first.', $extension_txt_id, $extension);
                 return false;
             }
         }
     }
     return true;
 }