/** * Load config file * * @param string $path the configuration file path * @param boolean $load_user_configuration_dir do we have to parse all user configuration files ? No for upgrade for example... * * @return array [ badges , files ] */ function config_load($load_user_configuration_dir = true) { $badges = false; $files = false; // Read config file $config = get_config_file(); if (is_null($config)) { return array($badges, $files); } // Get badges $badges = $config['badges']; // Set user constant foreach ($config['globals'] as $cst => $val) { if ($cst == strtoupper($cst)) { @define($cst, $val); } } // Set unset constants load_default_constants(); // Set time limit @set_time_limit(MAX_SEARCH_LOG_TIME + 2); // Append files from the USER_CONFIGURATION_DIR if ($load_user_configuration_dir === true) { if (is_dir(PML_CONFIG_BASE . DIRECTORY_SEPARATOR . USER_CONFIGURATION_DIR)) { $dir = PML_CONFIG_BASE . DIRECTORY_SEPARATOR . USER_CONFIGURATION_DIR; $userfiles = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD), '/^.+\\.(json|php)$/i', RecursiveRegexIterator::GET_MATCH); foreach ($userfiles as $userfile) { $filepath = realpath($userfile[0]); $c = get_config_file($filepath); if (!is_null($c)) { foreach ($c as $k => $v) { $fileid = get_slug(str_replace(PML_CONFIG_BASE, '', $filepath) . '/' . $k); $config['files'][$fileid] = $v; $config['files'][$fileid]['included_from'] = $filepath; } } } } } // Oups, there is no file... abort if (!isset($config['files'])) { return array($badges, $files); } // Try to generate the files tree if there are globs... $files_tmp = $config['files']; $files = array(); foreach ($files_tmp as $fileid => $file) { $path = $file['path']; $count = max(1, @(int) $file['count']); $gpaths = glob($path, GLOB_MARK | GLOB_NOCHECK); if (count($gpaths) == 0) { } else { if (count($gpaths) == 1) { $files[$fileid] = $file; $files[$fileid]['path'] = $gpaths[0]; } else { $new_paths = array(); $i = 1; foreach ($gpaths as $path) { $new_paths[$path] = filemtime($path); } // The most recent file will be the first arsort($new_paths, SORT_NUMERIC); // The first file id is the ID of the configuration file then others files are suffixed with _2, _3, etc... foreach ($new_paths as $path => $lastmodified) { $ext = $i > 1 ? '_' . $i : ''; $files[$fileid . $ext] = $file; $files[$fileid . $ext]['oid'] = $fileid; $files[$fileid . $ext]['odisplay'] = $files[$fileid . $ext]['display']; $files[$fileid . $ext]['path'] = $path; $files[$fileid . $ext]['display'] .= ' > ' . basename($path); if ($i >= $count) { break; } $i++; } } } } // Remove forbidden files if (Sentinel::isAuthSet()) { // authentication is enabled on this instance $username = Sentinel::getCurrentUsername(); $final = array(); // Anonymous access only if (is_null($username)) { foreach ($files as $fileid => $file) { $a = $fileid; // glob file if (isset($files[$fileid]['oid'])) { $a = $files[$fileid]['oid']; } if (Sentinel::isLogAnonymous($a)) { $final[$fileid] = $file; } } } else { foreach ($files as $fileid => $file) { $a = $fileid; // glob file if (isset($files[$fileid]['oid'])) { $a = $files[$fileid]['oid']; } if (Sentinel::userCanOnLogs($a, 'r', true, $username) || Sentinel::isLogAnonymous($a)) { $final[$fileid] = $file; } } } $files = $final; } // Fix missing values with defaults foreach ($files as $fileid => $file) { foreach (array('max' => LOGS_MAX, 'refresh' => LOGS_REFRESH, 'notify' => NOTIFICATION) as $fix => $value) { if (!isset($file[$fix])) { $files[$fileid][$fix] = $value; } } } // Finally sort files if (!function_exists('display_asc')) { function display_asc($a, $b) { return strcmp($a["display"], $b["display"]); } } if (!function_exists('display_desc')) { function display_desc($a, $b) { return strcmp($b["display"], $a["display"]); } } if (!function_exists('display_insensitive_asc')) { function display_insensitive_asc($a, $b) { return strcmp($a["display"], $b["display"]); } } if (!function_exists('display_insensitive_desc')) { function display_insensitive_desc($a, $b) { return strcmp($b["display"], $a["display"]); } } switch (trim(str_replace(array('-', '_', ' ', 'nsensitive'), '', SORT_LOG_FILES))) { case 'display': case 'displayasc': usort($files, 'display_asc'); break; case 'displayi': case 'displayiasc': usort($files, 'display_insensitive_asc'); break; case 'displaydesc': usort($files, 'display_desc'); break; case 'displayidesc': usort($files, 'display_insensitive_desc'); break; default: # do not sort break; } return array($badges, $files); }