private function install_from_market($item)
 {
     if (isset($item['url']) and !isset($item['download'])) {
         $item['download'] = $item['url'];
     } elseif (isset($item['download_url']) and !isset($item['download'])) {
         $item['download'] = $item['download_url'];
     }
     $download_target = false;
     if (isset($item['download']) and !isset($item['size'])) {
         $url = $item['download'];
         $download_target = $this->temp_dir . md5($url) . basename($url);
         $download_target_extract_lock = $this->temp_dir . md5($url) . basename($url) . '.unzip_lock';
         $this->_log_msg('Downloading from marketplace');
         //if (!is_file($download_target)){
         $dl = $this->http()->url($url)->download($download_target);
         //}
     } else {
         if (isset($item['download']) and isset($item['size'])) {
             $expected = intval($item['size']);
             $download_link = $item['download'];
             $ext = get_file_extension($download_link);
             if ($ext != 'zip') {
                 return;
             }
             if ($download_link != false and $expected > 0) {
                 $text = $download_link;
                 $regex = '/\\b((?:[\\w\\d]+\\:\\/\\/)?(?:[\\w\\-\\d]+\\.)+[\\w\\-\\d]+(?:\\/[\\w\\-\\d]+)*(?:\\/|\\.[\\w\\-\\d]+)?(?:\\?[\\w\\-\\d]+\\=[\\w\\-\\d]+\\&?)?(?:\\#[\\w\\-\\d]*)?)\\b/';
                 preg_match_all($regex, $text, $matches, PREG_SET_ORDER);
                 foreach ($matches as $match) {
                     if (isset($match[0])) {
                         $url = $download_link;
                         $download_target = $this->temp_dir . basename($download_link);
                         $download_target_extract_lock = $this->temp_dir . basename($download_link) . '.unzip_lock';
                         $expectd_item_size = $item['size'];
                         if (!is_file($download_target) or filesize($download_target) != $item['size']) {
                             $dl = $this->http()->url($url)->download($download_target);
                             if ($dl == false) {
                                 if (is_file($download_target) and filesize($download_target) != $item['size']) {
                                     $fs = filesize($download_target);
                                     return array('size' => $fs, 'expected_size' => $expected, 'try_again' => "true", 'warning' => "Only " . $fs . ' bytes downloaded of total ' . $expected);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($download_target != false and is_file($download_target)) {
         $where_to_unzip = MW_ROOTPATH;
         if (isset($item['item_type'])) {
             if ($item['item_type'] == 'module') {
                 $where_to_unzip = modules_path();
             } elseif ($item['item_type'] == 'module_template') {
                 $where_to_unzip = modules_path();
             } elseif ($item['item_type'] == 'template') {
                 $where_to_unzip = templates_path();
             } elseif ($item['item_type'] == 'element') {
                 $where_to_unzip = elements_path();
             }
             if (isset($item['install_path']) and $item['install_path'] != false) {
                 if ($item['item_type'] == 'module_template') {
                     $where_to_unzip = $where_to_unzip . DS . $item['install_path'] . DS . 'templates' . DS;
                 } else {
                     $where_to_unzip = $where_to_unzip . DS . $item['install_path'];
                 }
             }
             $where_to_unzip = str_replace('..', '', $where_to_unzip);
             $where_to_unzip = normalize_path($where_to_unzip, true);
             $this->_log_msg('Unzipping in ' . $where_to_unzip);
             $unzip = new \Microweber\Utils\Unzip();
             $target_dir = $where_to_unzip;
             $result = $unzip->extract($download_target, $target_dir, $preserve_filepath = true);
             $new_composer = $target_dir . 'composer.json';
             if (is_file($new_composer)) {
                 // $this->composer_merge($new_composer);
             }
             $num_files = count($result);
             return array('files' => $result, 'location' => $where_to_unzip, 'success' => "Item is installed. {$num_files} files extracted in {$where_to_unzip}");
         }
     }
 }
Beispiel #2
0
 public function load($module_name, $attrs = array())
 {
     $is_element = false;
     $custom_view = false;
     if (isset($attrs['view'])) {
         $custom_view = $attrs['view'];
         $custom_view = trim($custom_view);
         $custom_view = str_replace('\\', '/', $custom_view);
         $attrs['view'] = $custom_view = str_replace('..', '', $custom_view);
     }
     if ($custom_view != false and strtolower($custom_view) == 'admin') {
         if ($this->app->user_manager->is_admin() == false) {
             mw_error('Not logged in as admin');
         }
     }
     $module_name = trim($module_name);
     $module_name = str_replace('\\', '/', $module_name);
     $module_name = str_replace('..', '', $module_name);
     // prevent hack of the directory
     $module_name = reduce_double_slashes($module_name);
     $module_namei = $module_name;
     if (strstr($module_name, 'admin')) {
         $module_namei = str_ireplace('\\admin', '', $module_namei);
         $module_namei = str_ireplace('/admin', '', $module_namei);
     }
     //$module_namei = str_ireplace($search, $replace, $subject)e
     $uninstall_lock = $this->app->modules->get('one=1&ui=any&module=' . $module_namei);
     if (isset($uninstall_lock["installed"]) and $uninstall_lock["installed"] != '' and intval($uninstall_lock["installed"]) != 1) {
         return '';
     }
     if (!defined('ACTIVE_TEMPLATE_DIR')) {
         $this->app->content_manager->define_constants();
     }
     $module_in_template_dir = ACTIVE_TEMPLATE_DIR . 'modules/' . $module_name . '';
     $module_in_template_dir = normalize_path($module_in_template_dir, 1);
     $module_in_template_file = ACTIVE_TEMPLATE_DIR . 'modules/' . $module_name . '.php';
     $module_in_template_file = normalize_path($module_in_template_file, false);
     $try_file1 = false;
     $mod_d = $module_in_template_dir;
     $mod_d1 = normalize_path($mod_d, 1);
     $try_file1zz = $mod_d1 . 'index.php';
     $in_dir = false;
     if ($custom_view == true) {
         $try_file1zz = $mod_d1 . trim($custom_view) . '.php';
     } else {
         $try_file1zz = $mod_d1 . 'index.php';
     }
     if (is_dir($module_in_template_dir) and is_file($try_file1zz)) {
         $try_file1 = $try_file1zz;
         $in_dir = true;
     } elseif (is_file($module_in_template_file)) {
         $try_file1 = $module_in_template_file;
         $in_dir = false;
     } else {
         $module_in_default_dir = modules_path() . $module_name . '';
         $module_in_default_dir = normalize_path($module_in_default_dir, 1);
         // d($module_in_default_dir);
         $module_in_default_file = modules_path() . $module_name . '.php';
         $module_in_default_file_custom_view = modules_path() . $module_name . '_' . $custom_view . '.php';
         $element_in_default_file = elements_path() . $module_name . '.php';
         $element_in_default_file = normalize_path($element_in_default_file, false);
         //
         $module_in_default_file = normalize_path($module_in_default_file, false);
         if (is_file($module_in_default_file)) {
             $in_dir = false;
             if ($custom_view == true and is_file($module_in_default_file_custom_view)) {
                 $try_file1 = $module_in_default_file_custom_view;
             } else {
                 $try_file1 = $module_in_default_file;
             }
         } else {
             if (is_dir($module_in_default_dir)) {
                 $in_dir = true;
                 $mod_d1 = normalize_path($module_in_default_dir, 1);
                 if ($custom_view == true) {
                     $try_file1 = $mod_d1 . trim($custom_view) . '.php';
                 } else {
                     $try_file1 = $mod_d1 . 'index.php';
                 }
             } elseif (is_file($element_in_default_file)) {
                 $in_dir = false;
                 $is_element = true;
                 $try_file1 = $element_in_default_file;
             }
         }
     }
     //
     if (isset($try_file1) != false and $try_file1 != false and is_file($try_file1)) {
         if (isset($attrs) and is_array($attrs) and !empty($attrs)) {
             $attrs2 = array();
             foreach ($attrs as $attrs_k => $attrs_v) {
                 $attrs_k2 = substr($attrs_k, 0, 5);
                 if (strtolower($attrs_k2) == 'data-') {
                     $attrs_k21 = substr($attrs_k, 5);
                     $attrs2[$attrs_k21] = $attrs_v;
                 } elseif (!isset($attrs['data-' . $attrs_k])) {
                     $attrs2['data-' . $attrs_k] = $attrs_v;
                 }
                 $attrs2[$attrs_k] = $attrs_v;
             }
             $attrs = $attrs2;
         }
         $config['path_to_module'] = $config['mp'] = $config['path'] = normalize_path(dirname($try_file1) . '/', true);
         $config['the_module'] = $module_name;
         $config['module'] = $module_name;
         $module_name_dir = dirname($module_name);
         $config['module_name'] = $module_name_dir;
         $config['module_name_url_safe'] = $this->module_name_encode($module_name);
         $find_base_url = $this->app->url_manager->current(1);
         if ($pos = strpos($find_base_url, ':' . $module_name) or $pos = strpos($find_base_url, ':' . $config['module_name_url_safe'])) {
             $find_base_url = substr($find_base_url, 0, $pos) . ':' . $config['module_name_url_safe'];
         }
         $config['url'] = $find_base_url;
         $config['url_main'] = $config['url_base'] = strtok($find_base_url, '?');
         if ($in_dir != false) {
             $mod_api = str_replace('/admin', '', $module_name);
         } else {
             $mod_api = str_replace('/admin', '', $module_name_dir);
         }
         $config['module_api'] = $this->app->url_manager->site('api/' . $mod_api);
         $config['module_view'] = $this->app->url_manager->site('module/' . $module_name);
         $config['ns'] = str_replace('/', '\\', $module_name);
         $config['module_class'] = $this->module_css_class($module_name);
         $config['url_to_module'] = $this->app->url_manager->link_to_file($config['path_to_module']);
         if (isset($attrs['id'])) {
             $attrs['id'] = str_replace('__MODULE_CLASS_NAME__', $config['module_class'], $attrs['id']);
             $template = false;
         }
         //$config['url_to_module'] = rtrim($config['url_to_module'], '///');
         $lic = $this->app->modules->license($module_name);
         //  $lic = 'valid';
         if ($lic != false) {
             $config['license'] = $lic;
         }
         if (isset($attrs['module-id']) and $attrs['module-id'] != false) {
             $attrs['id'] = $attrs['module-id'];
         }
         if (!isset($attrs['id'])) {
             global $mw_mod_counter;
             $mw_mod_counter++;
             //  $seg_clean = $this->app->url_manager->segment(0);
             $seg_clean = $this->app->url_manager->segment(0, url_current());
             if (defined('IS_HOME')) {
                 $seg_clean = '';
             }
             $seg_clean = str_replace('%20', '-', $seg_clean);
             $seg_clean = str_replace(' ', '-', $seg_clean);
             $seg_clean = str_replace('.', '', $seg_clean);
             $attrs1 = crc32(serialize($attrs) . $seg_clean . $mw_mod_counter);
             $attrs1 = str_replace('%20', '-', $attrs1);
             $attrs1 = str_replace(' ', '-', $attrs1);
             $attrs['id'] = $config['module_class'] . '-' . $attrs1;
         }
         if (isset($attrs['id']) and strstr($attrs['id'], '__MODULE_CLASS_NAME__')) {
             $attrs['id'] = str_replace('__MODULE_CLASS_NAME__', $config['module_class'], $attrs['id']);
             //$attrs['id'] = ('__MODULE_CLASS__' . '-' . $attrs1);
         }
         $l1 = new \Microweber\View($try_file1);
         $l1->config = $config;
         $l1->app = $this->app;
         if (!isset($attrs['module'])) {
             $attrs['module'] = $module_name;
         }
         if (!isset($attrs['parent-module'])) {
             $attrs['parent-module'] = $module_name;
         }
         if (!isset($attrs['parent-module-id'])) {
             $attrs['parent-module-id'] = $attrs['id'];
         }
         //            $mw_restore_get = mw_var('mw_restore_get');
         //            if ($mw_restore_get != false and is_array($mw_restore_get)) {
         //                $l1->_GET = $mw_restore_get;
         //                $_GET = $mw_restore_get;
         //            }
         if (defined('MW_MODULE_ONDROP')) {
             if (!isset($attrs['ondrop'])) {
                 $attrs['ondrop'] = true;
             }
         }
         $l1->params = $attrs;
         if ($config) {
             $this->current_module = $config;
         }
         if ($attrs) {
             $this->current_module_params = $attrs;
         }
         if (isset($attrs['view']) && trim($attrs['view']) == 'empty') {
             $module_file = EMPTY_MOD_STR;
         } elseif (isset($attrs['view']) && trim($attrs['view']) == 'admin') {
             $module_file = $l1->__toString();
         } else {
             if (isset($attrs['display']) && trim($attrs['display']) == 'custom') {
                 $module_file = $l1->__get_vars();
                 return $module_file;
             } else {
                 if (isset($attrs['format']) && trim($attrs['format']) == 'json') {
                     $module_file = $l1->__get_vars();
                     header("Content-type: application/json");
                     exit(json_encode($module_file));
                 } else {
                     $module_file = $l1->__toString();
                 }
             }
         }
         //	$l1 = null;
         unset($l1);
         if ($lic != false and isset($lic["error"]) and $lic["error"] == 'no_license_found') {
             $lic_l1_try_file1 = MW_ADMIN_VIEWS_DIR . 'activate_license.php';
             $lic_l1 = new \Microweber\View($lic_l1_try_file1);
             $lic_l1->config = $config;
             $lic_l1->params = $attrs;
             $lic_l1e_file = $lic_l1->__toString();
             unset($lic_l1);
             $module_file = $lic_l1e_file . $module_file;
         }
         // $mw_loaded_mod_memory[$function_cache_id] = $module_file;
         return $module_file;
     } else {
         //define($cache_content, FALSE);
         // $mw_loaded_mod_memory[$function_cache_id] = false;
         return false;
     }
 }
Beispiel #3
0
 public function scan_for_elements($options = array())
 {
     if (is_string($options)) {
         $params = parse_str($options, $params2);
         $options = $params2;
     }
     $options['is_elements'] = 1;
     $options['dir_name'] = normalize_path(elements_path());
     if (isset($options['cleanup_db'])) {
         $this->app->layouts_manager->delete_all();
     }
     return $this->scan_for_modules($options);
 }