Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Sun, Feb 2, 8:58 PM

in-portal

Index: trunk/core/kernel/utility/unit_config_reader.php
===================================================================
--- trunk/core/kernel/utility/unit_config_reader.php (revision 1833)
+++ trunk/core/kernel/utility/unit_config_reader.php (revision 1834)
@@ -1,388 +1,412 @@
<?php
class kUnitConfigReader extends kBase {
/**
* Configs readed
*
* @var Array
* @access private
*/
var $configData=Array();
var $configFiles=Array();
var $CacheExpired = false;
/**
* Module names found during
* config reading
*
* @var Array
*/
- var $modules=Array();
+ var $modules_installed = Array();
/**
* Scan kernel and user classes
* for available configs
*
* @access protected
*/
function Init($prefix,$special)
{
parent::Init($prefix,$special);
+
+ $db =& $this->Application->GetADODBConnection();
+ $this->modules_installed = $db->GetCol('SELECT CONCAT(\'/\',Path) AS Path, Name FROM '.TABLE_PREFIX.'Modules','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;
+ }
+
+ /**
* 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(DOC_ROOT.BASE_PATH.$file);
$used_time = getmicrotime() - $before_time;
$used_mem = memory_get_usage() - $before_mem;
$debugger->IncludeLevel--;
$debugger->IncludesData['file'][] = str_replace(DOC_ROOT.BASE_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 ParseConfigs()
{
foreach ($this->configData as $prefix => $config)
{
$this->parseConfig($prefix);
}
}
function findConfigFiles($folderPath)
{
$folderPath = str_replace(DOC_ROOT.BASE_PATH, '', $folderPath);
$fh=opendir(DOC_ROOT.BASE_PATH.$folderPath);
while(($sub_folder=readdir($fh)))
{
$full_path=DOC_ROOT.BASE_PATH.$folderPath.'/'.$sub_folder;
if( $this->isDir($full_path))
{
if ( file_exists(DOC_ROOT.BASE_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(DOC_ROOT.BASE_PATH.$filename);
+ $config_found = file_exists(DOC_ROOT.BASE_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(DOC_ROOT.BASE_PATH.$filename);
$used_time = getmicrotime() - $before_time;
$used_mem = memory_get_usage() - $before_mem;
$debugger->IncludeLevel--;
$debugger->IncludesData['file'][] = str_replace(DOC_ROOT.BASE_PATH, '', $filename);
$debugger->IncludesData['mem'][] = $used_mem;
$debugger->IncludesData['time'][] = $used_time;
$debugger->IncludesData['level'][] = -1;
}
else
{
if($config_found) include_once(DOC_ROOT.BASE_PATH.$filename);
}
if($config_found)
{
$prefix = $config['Prefix'];
$config['BasePath'] = dirname(DOC_ROOT.BASE_PATH.$filename);
$this->configData[$prefix] = $config;
}
}
}
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')) {
$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);
}
}
}*/
$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().')');
}
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().')');
}
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 = 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']);
//$event_manager->registerBuildEvent($class_info['pseudo'],$class_info['build_event']);
}
}
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("<b class='debug_error'>Config Warning: </b>Field $f_name exists in the database, but is not defined in config file for prefix <b>".$config['Prefix']."</b>!");
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("<b class='debug_error'>Config Error: </b>Field $f_name in config for prefix <b>".$config['Prefix']."</b> 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("<b class='debug_error'>Config Error: </b>Field $f_name in config for prefix <b>".$config['Prefix']."</b> 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 isset($this->configData[$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);
}
}
?>
\ No newline at end of file
Property changes on: trunk/core/kernel/utility/unit_config_reader.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.5
\ No newline at end of property
+1.6
\ No newline at end of property

Event Timeline