Application->GetADODBConnection(); $this->modules_installed = $db->GetCol('SELECT CONCAT(\'/\',Path) AS Path, Name FROM '.TABLE_PREFIX.'Modules WHERE Loaded = 1','Name'); $this->scanModules(MODULES_PATH); } /** * Checks if config file is allowed for includion (if module of config is installed) * * @param string $config_path relative path from in-portal directory */ function configAllowed($config_path) { $module_found = false; foreach($this->modules_installed as $module_path) { if( substr($config_path, 0, strlen($module_path)) == $module_path ) { $module_found = true; break; } } return $module_found; } /** * Returns true if config exists and is allowed for reading * * @param string $prefix * @return bool */ function prefixRegistred($prefix) { return isset($this->configData[$prefix]) ? true : false; } /** * Read configs from all directories * on path specified * * @param string $folderPath * @access public */ function processFolder($folderPath, $cached) { $fh=opendir($folderPath); while(($sub_folder=readdir($fh))) { $full_path=$folderPath.'/'.$sub_folder; if( $this->isDir($full_path) && file_exists($this->getConfigName($full_path)) ) { if (filemtime($full_path) > $cached) { $this->CacheExpired = true; $file = $this->getConfigName($full_path); if ( defined('DEBUG_MODE') && dbg_ConstOn('DBG_PROFILE_INCLUDES')) { if ( in_array($file, get_required_files()) ) return; global $debugger; $debugger->IncludeLevel++; $before_time = getmicrotime(); $before_mem = memory_get_usage(); include_once(FULL_PATH.$file); $used_time = getmicrotime() - $before_time; $used_mem = memory_get_usage() - $before_mem; $debugger->IncludeLevel--; $debugger->IncludesData['file'][] = str_replace(FULL_PATH, '', $file); $debugger->IncludesData['mem'][] = $used_mem; $debugger->IncludesData['time'][] = $used_time; $debugger->IncludesData['level'][] = -1; } else { include_once($file); } $prefix=$config['Prefix']; $config['BasePath']=$full_path; $this->configData[$prefix] = $config; } } } } function ParseConfigClones($prefix) { $config = $this->configData[$prefix]; if(!getArrayValue($config, 'Clones')) { return; } unset($this->configData[$prefix]['Clones']); foreach($config['Clones'] as $clone_prefix => $clone_config) { $clone_config['Prefix'] = $clone_prefix; $this->configData[$clone_prefix] = array_merge_recursive2($this->configData[$prefix], $clone_config); $this->ParseConfigClones($clone_prefix); } } function ParseConfigs() { foreach ($this->configData as $prefix => $config) { $this->parseConfig($prefix); } } function findConfigFiles($folderPath) { $folderPath = str_replace(FULL_PATH, '', $folderPath); // this make sense, since $folderPath may NOT contain FULL_PATH $fh=opendir(FULL_PATH.$folderPath); while(($sub_folder=readdir($fh))) { $full_path=FULL_PATH.$folderPath.'/'.$sub_folder; if( $this->isDir($full_path)) { if ( file_exists(FULL_PATH.$this->getConfigName($folderPath.'/'.$sub_folder)) ) { $this->configFiles[] = $this->getConfigName($folderPath.'/'.$sub_folder); } $this->findConfigFiles($full_path); // if (filemtime($full_path) > $cached) { } } } } function includeConfigFiles() { $db =& $this->Application->GetADODBConnection(); foreach ($this->configFiles as $filename) { $config_found = file_exists(FULL_PATH.$filename) && $this->configAllowed($filename); if( defined('DEBUG_MODE') && DEBUG_MODE && dbg_ConstOn('DBG_PROFILE_INCLUDES')) { if ( in_array($filename, get_required_files()) ) return; global $debugger; $debugger->IncludeLevel++; $before_time = getmicrotime(); $before_mem = memory_get_usage(); if($config_found) include_once(FULL_PATH.$filename); $used_time = getmicrotime() - $before_time; $used_mem = memory_get_usage() - $before_mem; $debugger->IncludeLevel--; $debugger->IncludesData['file'][] = str_replace(FULL_PATH, '', $filename); $debugger->IncludesData['mem'][] = $used_mem; $debugger->IncludesData['time'][] = $used_time; $debugger->IncludesData['level'][] = -1; } else { if($config_found) include_once(FULL_PATH.$filename); } if($config_found) { $prefix = $config['Prefix']; $config['BasePath'] = dirname(FULL_PATH.$filename); $this->configData[$prefix] = $config; $this->ParseConfigClones($prefix); } } } function scanModules($folderPath) { global $debugger; if (defined('CACHE_CONFIGS_FILES')) { $conn =& $this->Application->GetADODBConnection(); $data = $conn->GetRow('SELECT Data, Cached FROM '.TABLE_PREFIX.'Cache WHERE VarName = "config_files"'); if ($data && $data['Cached'] > (time() - 3600) ) { $this->configFiles = unserialize($data['Data']); $files_cached = $data['Cached']; } else { $files_cached = 0; } } else { $files_cached = 0; } if (defined('CACHE_CONFIGS_DATA') && CACHE_CONFIGS_DATA) { $conn =& $this->Application->GetADODBConnection(); $data = $conn->GetRow('SELECT Data, Cached FROM '.TABLE_PREFIX.'Cache WHERE VarName = "config_data"'); if ($data && $data['Cached'] > (time() - 3600) ) { $this->configData = unserialize($data['Data']); $data_cached = $data['Cached']; } else { $data_cached = 0; } } else { $data_cached = 0; } if ( !defined('CACHE_CONFIGS_FILES') || $files_cached == 0 ) { $this->findConfigFiles($folderPath); } if ( !defined('CACHE_CONFIGS_DATA') || $data_cached == 0) { $this->includeConfigFiles(); } /*// && (time() - $cached) > 600) - to skip checking files modified dates if ( !defined('CACHE_CONFIGS') ) { $fh=opendir($folderPath); while(($sub_folder=readdir($fh))) { $full_path=$folderPath.'/'.$sub_folder.'/units'; if( $this->isDir($full_path) ) { $this->processFolder($full_path, $cached); } } }*/ if (defined('CACHE_CONFIGS_DATA') && $data_cached == 0) { $conn->Query('REPLACE '.TABLE_PREFIX.'Cache (VarName, Data, Cached) VALUES ("config_data", '.$conn->qstr(serialize($this->configData)).', '.time().')'); } $this->ParseConfigs(); if (defined('CACHE_CONFIGS_FILES') && $files_cached == 0) { $conn->Query('REPLACE '.TABLE_PREFIX.'Cache (VarName, Data, Cached) VALUES ("config_files", '.$conn->qstr(serialize($this->configFiles)).', '.time().')'); } unset($this->configFiles); // unset($this->configData); } /** * Register nessasary classes * * @param string $prefix * @access private */ function parseConfig($prefix) { $config =& $this->configData[$prefix]; $event_manager =& $this->Application->recallObject('EventManager'); $class_params=Array('ItemClass','ListClass','EventHandlerClass','TagProcessorClass'); foreach($class_params as $param_name) { if ( !(isset($config[$param_name]) ) ) continue; $class_info =& $config[$param_name]; $pseudo_class = $this->getPrefixByParamName($param_name,$prefix); $this->Application->registerClass( $class_info['class'], $config['BasePath'].'/'.$class_info['file'], $pseudo_class); $event_manager->registerBuildEvent($pseudo_class,$class_info['build_event']); // register classes on which current class depends $require_classes = getArrayValue($class_info, 'require_classes'); if($require_classes) { foreach($require_classes as $require_class) { $this->Application->Factory->registerDependency($class_info['class'], $require_class); } } } $register_classes = getArrayValue($config,'RegisterClasses'); if($register_classes) { foreach($register_classes as $class_info) { $this->Application->registerClass( $class_info['class'], $config['BasePath'].'/'.$class_info['file'], $class_info['pseudo']); } } $regular_events = getArrayValue($config, 'RegularEvents'); if($regular_events) { foreach($regular_events as $short_name => $regular_event_info) { $event_manager->registerRegularEvent( $short_name, $config['Prefix'].':'.$regular_event_info['EventName'], $regular_event_info['RunInterval'], $regular_event_info['Type'] ); } } if ( is_array(getArrayValue($config, 'Hooks')) ) { foreach ($config['Hooks'] as $hook) { $do_prefix = $hook['DoPrefix'] == '' ? $config['Prefix'] : $hook['DoPrefix']; if ( !is_array($hook['HookToEvent']) ) { $hook_events = Array( $hook['HookToEvent'] ); } else { $hook_events = $hook['HookToEvent']; } foreach ($hook_events as $hook_event) { $this->Application->registerHook($hook['HookToPrefix'], $hook['HookToSpecial'], $hook_event, $hook['Mode'], $do_prefix, $hook['DoSpecial'], $hook['DoEvent'], $hook['Conditional']); } } } if ( is_array(getArrayValue($config, 'AggregateTags')) ) { foreach ($config['AggregateTags'] as $aggregate_tag) { $aggregate_tag['LocalPrefix'] = $config['Prefix']; $this->Application->registerAggregateTag($aggregate_tag); } } if ( $this->Application->isDebugMode() && dbg_ConstOn('DBG_VALIDATE_CONFIGS') && isset($config['TableName']) ) { global $debugger; $tablename = $config['TableName']; $conn =& $this->Application->GetADODBConnection(); $res = $conn->Query("DESCRIBE $tablename"); foreach ($res as $field) { $f_name = $field['Field']; if (getArrayValue($config, 'Fields')) { if ( !array_key_exists ($f_name, $config['Fields']) ) { $debugger->appendHTML("Config Warning: Field $f_name exists in the database, but is not defined in config file for prefix ".$config['Prefix']."!"); safeDefine('DBG_RAISE_ON_WARNINGS', 1); } else { $options = $config['Fields'][$f_name]; if ($field['Null'] == '') { if ( $f_name != $config['IDField'] && !isset($options['not_null']) && !isset($options['required']) ) { $debugger->appendHTML("Config Error: Field $f_name in config for prefix ".$config['Prefix']." is NOT NULL in the database, but is not configured as not_null or required!"); safeDefine('DBG_RAISE_ON_WARNINGS', 1); } if ( isset($options['not_null']) && !isset($options['default']) ) { $debugger->appendHTML("Config Error: Field $f_name in config for prefix ".$config['Prefix']." is described as NOT NULL, but does not have DEFAULT value!"); safeDefine('DBG_RAISE_ON_WARNINGS', 1); } } } } } } } /** * Reads unit (specified by $prefix) * option specified by $option * * @param string $prefix * @param string $option * @return string * @access public */ function getUnitOption($prefix,$name) { return isset($this->configData[$prefix][$name]) ? $this->configData[$prefix][$name] : false; } /** * Read all unit with $prefix options * * @param string $prefix * @return Array * @access public */ function getUnitOptions($prefix) { return $this->prefixRegistred($prefix) ? $this->configData[$prefix] : false; } /** * Set's new unit option value * * @param string $prefix * @param string $name * @param string $value * @access public */ function setUnitOption($prefix,$name,$value) { $this->configData[$prefix][$name] = $value; } function getPrefixByParamName($paramName,$prefix) { $pseudo_class_map=Array( 'ItemClass'=>'%s', 'ListClass'=>'%s_List', 'EventHandlerClass'=>'%s_EventHandler', 'TagProcessorClass'=>'%s_TagProcessor' ); return sprintf($pseudo_class_map[$paramName],$prefix); } /** * Get's config file name based * on folder name supplied * * @param string $folderPath * @return string * @access private */ function getConfigName($folderPath) { return $folderPath.'/'.basename($folderPath).'_config.php'; } /** * is_dir ajustment to work with * directory listings too * * @param string $folderPath * @return bool * @access private */ function isDir($folderPath) { $base_name=basename($folderPath); $ret=!($base_name=='.'||$base_name=='..'); return $ret&&is_dir($folderPath); } } ?>