public function scan_for_modules($options = false)
{
$params = $options;
if (is_string($params)) {
$params = parse_str($params, $params2);
$params = $options = $params2;
}
$args = func_get_args();
$function_cache_id = '';
foreach ($args as $k => $v) {
$function_cache_id = $function_cache_id . serialize($k) . serialize($v) . serialize($params);
}
$list_as_element = false;
$cache_id = $function_cache_id = __FUNCTION__ . crc32($function_cache_id);
if (isset($options['dir_name'])) {
$dir_name = $options['dir_name'];
//$list_as_element = true;
$cache_group = 'elements/global';
} else {
$dir_name = normalize_path(modules_path());
$list_as_element = false;
$cache_group = 'modules/global';
}
if (isset($options['is_elements']) and $options['is_elements'] != false) {
$list_as_element = true;
} else {
$list_as_element = false;
}
$skip_save = false;
if (isset($options['skip_save']) and $options['skip_save'] != false) {
$skip_save = true;
}
$modules_remove_old = false;
if (isset($options['cache_group'])) {
$cache_group = $options['cache_group'];
}
if (isset($options['reload_modules']) == true) {
$modules_remove_old = true;
}
if ($modules_remove_old or isset($options['cleanup_db']) == true) {
if ($this->app->user_manager->is_admin() == true) {
$this->app->cache_manager->delete('categories');
$this->app->cache_manager->delete('categories_items');
$this->app->cache_manager->delete('db');
$this->app->cache_manager->delete('modules');
}
}
if (isset($options['skip_cache']) == false and isset($options['no_cache']) == false) {
$cache_content = $this->app->cache_manager->get($cache_id, $cache_group);
if ($cache_content != false) {
return $cache_content;
}
}
if (isset($options['glob'])) {
$glob_patern = $options['glob'];
} else {
$glob_patern = '*config.php';
}
if (defined('INI_SYSTEM_CHECK_DISABLED') == false) {
define('INI_SYSTEM_CHECK_DISABLED', ini_get('disable_functions'));
}
if (!strstr(INI_SYSTEM_CHECK_DISABLED, 'ini_set')) {
ini_set('memory_limit', '160M');
ini_set('set_time_limit', 0);
}
if (!strstr(INI_SYSTEM_CHECK_DISABLED, 'set_time_limit')) {
set_time_limit(600);
}
$dir = rglob($glob_patern, 0, $dir_name);
$dir_name_mods = modules_path();
$dir_name_mods2 = elements_path();
if (!empty($dir)) {
$configs = array();
foreach ($dir as $key => $value) {
$skip_module = false;
if (isset($options['skip_admin']) and $options['skip_admin'] == true) {
if (strstr($value, 'admin')) {
$skip_module = true;
}
}
if ($skip_module == false) {
$config = array();
$value = normalize_path($value, false);
$moduleDir = $mod_name = str_replace('_config.php', '', $value);
$moduleDir = $mod_name = str_replace('config.php', '', $moduleDir);
$moduleDir = $mod_name = str_replace('index.php', '', $moduleDir);
$moduleDir = $mod_name_dir = str_replace($dir_name_mods, '', $moduleDir);
$moduleDir = $mod_name_dir = str_replace($dir_name_mods2, '', $moduleDir);
$def_icon = modules_path() . 'default.png';
ob_start();
$is_mw_ignore = dirname($value) . DS . '.mwignore';
if (!is_file($is_mw_ignore)) {
include $value;
}
$content = ob_get_contents();
ob_end_clean();
if ($list_as_element == true) {
$moduleDir = str_replace(elements_path(), '', $moduleDir);
} else {
$moduleDir = str_replace(modules_path(), '', $moduleDir);
}
$replace_root = MW_ROOTPATH . DS . 'userfiles' . DS . 'modules' . DS;
$moduleDir = str_replace($replace_root, '', $moduleDir);
$replace_root = dirname(dirname(MW_PATH)) . DS . 'userfiles' . DS . 'modules' . DS;
$moduleDir = str_replace($replace_root, '', $moduleDir);
$moduleDir = rtrim($moduleDir, '\\');
$moduleDir = rtrim($moduleDir, '/');
$moduleDir = str_replace('\\', '/', $moduleDir);
$moduleDir = str_replace(modules_path(), '', $moduleDir);
$config['module'] = $moduleDir;
$config['module'] = rtrim($config['module'], '\\');
$config['module'] = rtrim($config['module'], '/');
$config['module_base'] = str_replace('admin/', '', $moduleDir);
$main_try_icon = false;
if (is_dir($mod_name)) {
$bname = basename($mod_name);
$t1 = modules_path() . $config['module'] . DS . $bname;
$try_icon = $t1 . '.png';
$main_try_icon = modules_path() . $config['module'] . DS . 'icon.png';
} else {
$try_icon = $mod_name . '.png';
}
$try_icon = normalize_path($try_icon, false);
if ($main_try_icon and is_file($main_try_icon)) {
$config['icon'] = $this->app->url_manager->link_to_file($main_try_icon);
} elseif (is_file($try_icon)) {
$config['icon'] = $this->app->url_manager->link_to_file($try_icon);
} else {
$config['icon'] = $this->app->url_manager->link_to_file($def_icon);
}
if (isset($config['ui'])) {
$config['ui'] = intval($config['ui']);
} else {
$config['ui'] = 0;
}
if (isset($config['is_system'])) {
$config['is_system'] = intval($config['is_system']);
} else {
$config['is_system'] = 0;
}
if (isset($config['ui_admin'])) {
$config['ui_admin'] = intval($config['ui_admin']);
} else {
$config['ui_admin'] = 0;
}
if (isset($config['name']) and $skip_save !== true and $skip_module == false) {
if (trim($config['module']) != '') {
if ($list_as_element == true) {
$this->app->layouts_manager->save($config);
} else {
$config['installed'] = 'auto';
$tablesData = false;
$schemaFileName = $moduleDir . 'schema.json';
if (isset($config['tables']) && is_array($config['tables']) && !empty($config['tables'])) {
$tablesData = $config['tables'];
} elseif (isset($config['tables']) && is_callable($config['tables'])) {
call_user_func($config['tables']);
unset($config['tables']);
} elseif (file_exists($schemaFileName)) {
$json = file_get_contents($schemaFileName);
$json = @json_decode($json, true);
$tablesData = $json;
}
$this->save($config);
if ($tablesData) {
(new DbUtils())->build_tables($tablesData);
}
}
}
}
$configs[] = $config;
}
}
if ($skip_save == true) {
return $configs;
}
$cfg_ordered = array();
$cfg_ordered2 = array();
$cfg = $configs;
foreach ($cfg as $k => $item) {
if (isset($item['position'])) {
$cfg_ordered2[$item['position']][] = $item;
unset($cfg[$k]);
}
}
ksort($cfg_ordered2);
foreach ($cfg_ordered2 as $k => $item) {
foreach ($item as $ite) {
$cfg_ordered[] = $ite;
}
}
if ($modules_remove_old == true) {
$table = 'options';
$uninstall_lock = $this->get('ui=any');
if (is_array($uninstall_lock) and !empty($uninstall_lock)) {
foreach ($uninstall_lock as $value) {
$ism = $this->exists($value['module']);
if ($ism == false) {
$this->delete_module($value['id']);
$mn = $value['module'];
$table_options = $this->tables['options'];
$this->app->database_manager->delete_by_id($table_options, $mn, 'option_group');
}
}
}
}
$c2 = array_merge($cfg_ordered, $cfg);
$this->app->cache_manager->save($c2, $cache_id, $cache_group);
return $c2;
}
}