From 7d916647e1b82fcc8a17b68d3ae0615af42f53e0 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 13 May 2016 18:24:08 +0200 Subject: Get baseconfig ready for modularization --- inc/defaultdata.inc.php | 4 -- inc/location.inc.php | 148 ------------------------------------------------ inc/module.inc.php | 17 ++++++ inc/util.inc.php | 13 +++++ 4 files changed, 30 insertions(+), 152 deletions(-) delete mode 100644 inc/location.inc.php (limited to 'inc') diff --git a/inc/defaultdata.inc.php b/inc/defaultdata.inc.php index fdde4982..2d993e72 100644 --- a/inc/defaultdata.inc.php +++ b/inc/defaultdata.inc.php @@ -199,10 +199,6 @@ class DefaultData 'validator' => 'list:IGNORE|BUMP|EXCLUSIVE' ), ); - foreach ($data as $entry) { - Database::exec("INSERT IGNORE INTO setting (setting, catid, defaultvalue, permissions, validator)" - . "VALUES (:setting, :catid, :defaultvalue, :permissions, :validator)", $entry); - } } } diff --git a/inc/location.inc.php b/inc/location.inc.php deleted file mode 100644 index 39ecdf67..00000000 --- a/inc/location.inc.php +++ /dev/null @@ -1,148 +0,0 @@ -fetch(PDO::FETCH_ASSOC)) { - $rows[] = $row; - } - return $rows; - } - - public static function getLocationsAssoc() - { - if (self::$assocLocationCache === false) { - $rows = self::queryLocations(); - $rows = self::buildTree($rows); - self::$assocLocationCache = self::flattenTreeAssoc($rows); - } - return self::$assocLocationCache; - } - - private static function flattenTreeAssoc($tree, $depth = 0) - { - $output = array(); - foreach ($tree as $node) { - $output[(int)$node['locationid']] = array( - 'parentlocationid' => (int)$node['parentlocationid'], - 'locationname' => $node['locationname'], - 'depth' => $depth - ); - if (!empty($node['children'])) { - $output += self::flattenTreeAssoc($node['children'], $depth + 1); - } - } - return $output; - } - - public static function getLocations($default = 0, $excludeId = 0, $addNoParent = false) - { - if (self::$flatLocationCache === false) { - $rows = self::queryLocations(); - $rows = self::buildTree($rows); - $rows = self::flattenTree($rows); - self::$flatLocationCache = $rows; - } else { - $rows = self::$flatLocationCache; - } - $del = false; - unset($row); - foreach ($rows as $key => &$row) { - if ($del === false && $row['locationid'] == $excludeId) { - $del = $row['depth']; - } elseif ($del !== false && $row['depth'] <= $del) { - $del = false; - } - if ($del !== false) { - unset($rows[$key]); - continue; - } - if ($row['locationid'] == $default) { - $row['selected'] = true; - } - } - if ($addNoParent) { - array_unshift($rows, array( - 'locationid' => 0, - 'locationname' => '-----', - 'selected' => $default == 0 - )); - } - return array_values($rows); - } - - public static function buildTree($elements, $parentId = 0) - { - $branch = array(); - $sort = array(); - foreach ($elements as $element) { - if ($element['locationid'] == 0 || $element['locationid'] == $parentId) - continue; - if ($element['parentlocationid'] == $parentId) { - $children = self::buildTree($elements, $element['locationid']); - if (!empty($children)) { - $element['children'] = $children; - } - $branch[] = $element; - $sort[] = $element['locationname']; - } - } - array_multisort($sort, SORT_ASC, $branch); - return $branch; - } - - private static function flattenTree($tree, $depth = 0) - { - $output = array(); - foreach ($tree as $node) { - $output[] = array( - 'locationid' => $node['locationid'], - 'locationname' => $node['locationname'], - 'locationpad' => str_repeat('--', $depth), - 'locationspan1' => $depth + 1, - 'locationspan2' => 10 - $depth, - 'depth' => $depth - ); - if (!empty($node['children'])) { - $output = array_merge($output, self::flattenTree($node['children'], $depth + 1)); - } - } - return $output; - } - - public static function extractIds($tree) - { - $ids = array(); - foreach ($tree as $node) { - $ids[] = $node['locationid']; - if (!empty($node['children'])) { - $ids = array_merge($ids, self::extractIds($node['children'])); - } - } - return $ids; - } - - public static function getFromIp($ip) - { - $locationId = false; - $long = sprintf('%u', ip2long($ip)); - $net = Database::simpleQuery('SELECT locationid FROM subnet' - . ' WHERE :ip BETWEEN startaddr AND endaddr', array('ip' => $long)); - while ($row = $net->fetch(PDO::FETCH_ASSOC)) { - $locations = self::getLocationsAssoc(); - $id = (int)$row['locationid']; - if (!isset($locations[$id])) continue; - if ($locationId !== false && $locations[$id]['depth'] <= $locations[$locationId]['depth']) continue; - $locationId = $id; - } - return $locationId; - } - -} diff --git a/inc/module.inc.php b/inc/module.inc.php index 894bc0f6..246505b5 100644 --- a/inc/module.inc.php +++ b/inc/module.inc.php @@ -19,6 +19,23 @@ class Module return false; return self::$modules[$name]; } + + /** + * Check whether given module is available, that is, all dependencies are + * met. If the module is available, it will be activated, so all it's classes + * are available through the auto-loader. + * + * @param string $moduleId module to check + * @return bool true if module is available and activated + */ + public static function isAvailable($moduleId) + { + $module = self::get($moduleId); + if ($module === false) + return false; + $module->activate(); + return !$module->hasMissingDependencies(); + } private static function resolveDepsByName($name) { diff --git a/inc/util.inc.php b/inc/util.inc.php index 5eed3ddc..c0b77f96 100644 --- a/inc/util.inc.php +++ b/inc/util.inc.php @@ -19,6 +19,19 @@ class Util Header('Content-Type: text/plain; charset=utf-8'); echo "--------------------\nFlagrant system error:\n$message\n--------------------\n\n"; if (defined('CONFIG_DEBUG') && CONFIG_DEBUG) { + global $SLX_ERRORS; +/* + 'errno' => $errno, + 'errstr' => $errstr, + 'errfile' => $errfile, + 'errline' => $errline, + */ + if (!empty($SLX_ERRORS)) { + foreach ($SLX_ERRORS as $error) { + echo "{$error['errstr']} ({$error['errfile']}:{$error['errline']}\n"; + } + echo "--------------------\n"; + } debug_print_backtrace(); echo "\n\nSome variables for your entertainment:\n"; print_r($GLOBALS); -- cgit v1.2.3-55-g7522