_pos = (int) $_POST['pos']; } elseif (isset($_GET['pos'])) { $this->_pos = (int) $_GET['pos']; } if (!isset($this->_pos)) { $this->_pos = $this->_getNavigationDbPos(); } // Get the active node if (isset($_POST['aPath'])) { $this->_aPath[0] = $this->_parsePath($_POST['aPath']); $this->_pos2_name[0] = $_POST['pos2_name']; $this->_pos2_value[0] = $_POST['pos2_value']; if (isset($_POST['pos3_name'])) { $this->_pos3_name[0] = $_POST['pos3_name']; $this->_pos3_value[0] = $_POST['pos3_value']; } } else { if (isset($_POST['n0_aPath'])) { $count = 0; while (isset($_POST['n' . $count . '_aPath'])) { $this->_aPath[$count] = $this->_parsePath( $_POST['n' . $count . '_aPath'] ); $index = 'n' . $count . '_pos2_'; $this->_pos2_name[$count] = $_POST[$index . 'name']; $this->_pos2_value[$count] = $_POST[$index . 'value']; $index = 'n' . $count . '_pos3_'; if (isset($_POST[$index])) { $this->_pos3_name[$count] = $_POST[$index . 'name']; $this->_pos3_value[$count] = $_POST[$index . 'value']; } $count++; } } } if (isset($_POST['vPath'])) { $this->_vPath[0] = $this->_parsePath($_POST['vPath']); } else { if (isset($_POST['n0_vPath'])) { $count = 0; while (isset($_POST['n' . $count . '_vPath'])) { $this->_vPath[$count] = $this->_parsePath( $_POST['n' . $count . '_vPath'] ); $count++; } } } if (isset($_POST['searchClause'])) { $this->_searchClause = $_POST['searchClause']; } if (isset($_POST['searchClause2'])) { $this->_searchClause2 = $_POST['searchClause2']; } // Initialise the tree by creating a root node $node = NodeFactory::getInstance('NodeDatabaseContainer', 'root'); $this->_tree = $node; if ($GLOBALS['cfg']['NavigationTreeEnableGrouping'] && $GLOBALS['cfg']['ShowDatabasesNavigationAsTree'] ) { $this->_tree->separator = $GLOBALS['cfg']['NavigationTreeDbSeparator']; $this->_tree->separator_depth = 10000; } } /** * Returns the database position for the page selector * * @return int */ private function _getNavigationDbPos() { $retval = 0; if (strlen($GLOBALS['db']) == 0) { return $retval; } /* * @todo describe a scenario where this code is executed */ if (!$GLOBALS['cfg']['Server']['DisableIS']) { $dbSeparator = $GLOBALS['dbi']->escapeString( $GLOBALS['cfg']['NavigationTreeDbSeparator'] ); $query = "SELECT (COUNT(DB_first_level) DIV %d) * %d "; $query .= "from ( "; $query .= " SELECT distinct SUBSTRING_INDEX(SCHEMA_NAME, "; $query .= " '%s', 1) "; $query .= " DB_first_level "; $query .= " FROM INFORMATION_SCHEMA.SCHEMATA "; $query .= " WHERE `SCHEMA_NAME` < '%s' "; $query .= ") t "; $retval = $GLOBALS['dbi']->fetchValue( sprintf( $query, (int)$GLOBALS['cfg']['FirstLevelNavigationItems'], (int)$GLOBALS['cfg']['FirstLevelNavigationItems'], $dbSeparator, $GLOBALS['dbi']->escapeString($GLOBALS['db']) ) ); return $retval; } $prefixMap = array(); if ($GLOBALS['dbs_to_test'] === false) { $handle = $GLOBALS['dbi']->tryQuery("SHOW DATABASES"); if ($handle !== false) { while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { if (strcasecmp($arr[0], $GLOBALS['db']) >= 0) { break; } $prefix = strstr( $arr[0], $GLOBALS['cfg']['NavigationTreeDbSeparator'], true ); if ($prefix === false) { $prefix = $arr[0]; } $prefixMap[$prefix] = 1; } } } else { $databases = array(); foreach ($GLOBALS['dbs_to_test'] as $db) { $query = "SHOW DATABASES LIKE '" . $db . "'"; $handle = $GLOBALS['dbi']->tryQuery($query); if ($handle === false) { continue; } while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { $databases[] = $arr[0]; } } sort($databases); foreach ($databases as $database) { if (strcasecmp($database, $GLOBALS['db']) >= 0) { break; } $prefix = strstr( $database, $GLOBALS['cfg']['NavigationTreeDbSeparator'], true ); if ($prefix === false) { $prefix = $database; } $prefixMap[$prefix] = 1; } } $navItems = (int)$GLOBALS['cfg']['FirstLevelNavigationItems']; $retval = floor((count($prefixMap) / $navItems)) * $navItems; return $retval; } /** * Converts an encoded path to a node in string format to an array * * @param string $string The path to parse * * @return array */ private function _parsePath($string) { $path = explode('.', $string); foreach ($path as $key => $value) { $path[$key] = base64_decode($value); } return $path; } /** * Generates the tree structure so that it can be rendered later * * @return Node|false The active node or false in case of failure */ private function _buildPath() { $retval = $this->_tree; // Add all databases unconditionally $data = $this->_tree->getData( 'databases', $this->_pos, $this->_searchClause ); $hiddenCounts = $this->_tree->getNavigationHidingData(); foreach ($data as $db) { $node = NodeFactory::getInstance('NodeDatabase', $db); if (isset($hiddenCounts[$db])) { $node->setHiddenCount($hiddenCounts[$db]); } $this->_tree->addChild($node); } // Whether build other parts of the tree depends // on whether we have any paths in $this->_aPath foreach ($this->_aPath as $key => $path) { $retval = $this->_buildPathPart( $path, $this->_pos2_name[$key], $this->_pos2_value[$key], isset($this->_pos3_name[$key]) ? $this->_pos3_name[$key] : '', isset($this->_pos3_value[$key]) ? $this->_pos3_value[$key] : '' ); } return $retval; } /** * Builds a branch of the tree * * @param array $path A paths pointing to the branch * of the tree that needs to be built * @param string $type2 The type of item being paginated on * the second level of the tree * @param int $pos2 The position for the pagination of * the branch at the second level of the tree * @param string $type3 The type of item being paginated on * the third level of the tree * @param int $pos3 The position for the pagination of * the branch at the third level of the tree * * @return Node|false The active node or false in case of failure */ private function _buildPathPart(array $path, $type2, $pos2, $type3, $pos3) { if (empty($pos2)) { $pos2 = 0; } if (empty($pos3)) { $pos3 = 0; } $retval = true; if (count($path) <= 1) { return $retval; } array_shift($path); // remove 'root' /* @var $db NodeDatabase */ $db = $this->_tree->getChild($path[0]); $retval = $db; if ($db === false) { return false; } $containers = $this->_addDbContainers($db, $type2, $pos2); array_shift($path); // remove db if ((count($path) <= 0 || !array_key_exists($path[0], $containers)) && count($containers) != 1 ) { return $retval; } if (count($containers) == 1) { $container = array_shift($containers); } else { $container = $db->getChild($path[0], true); if ($container === false) { return false; } } $retval = $container; if (count($container->children) <= 1) { $dbData = $db->getData( $container->real_name, $pos2, $this->_searchClause2 ); foreach ($dbData as $item) { switch ($container->real_name) { case 'events': $node = NodeFactory::getInstance( 'NodeEvent', $item ); break; case 'functions': $node = NodeFactory::getInstance( 'NodeFunction', $item ); break; case 'procedures': $node = NodeFactory::getInstance( 'NodeProcedure', $item ); break; case 'tables': $node = NodeFactory::getInstance( 'NodeTable', $item ); break; case 'views': $node = NodeFactory::getInstance( 'NodeView', $item ); break; default: break; } if (isset($node)) { if ($type2 == $container->real_name) { $node->pos2 = $pos2; } $container->addChild($node); } } } if (count($path) > 1 && $path[0] != 'tables') { $retval = false; return $retval; } array_shift($path); // remove container if (count($path) <= 0) { return $retval; } /* @var $table NodeTable */ $table = $container->getChild($path[0], true); if ($table === false) { if (!$db->getPresence('tables', $path[0])) { return false; } $node = NodeFactory::getInstance( 'NodeTable', $path[0] ); if ($type2 == $container->real_name) { $node->pos2 = $pos2; } $container->addChild($node); $table = $container->getChild($path[0], true); } $retval = $table; $containers = $this->_addTableContainers( $table, $pos2, $type3, $pos3 ); array_shift($path); // remove table if (count($path) <= 0 || !array_key_exists($path[0], $containers) ) { return $retval; } $container = $table->getChild($path[0], true); $retval = $container; $tableData = $table->getData( $container->real_name, $pos3 ); foreach ($tableData as $item) { switch ($container->real_name) { case 'indexes': $node = NodeFactory::getInstance( 'NodeIndex', $item ); break; case 'columns': $node = NodeFactory::getInstance( 'NodeColumn', $item ); break; case 'triggers': $node = NodeFactory::getInstance( 'NodeTrigger', $item ); break; default: break; } if (isset($node)) { $node->pos2 = $container->parent->pos2; if ($type3 == $container->real_name) { $node->pos3 = $pos3; } $container->addChild($node); } } return $retval; } /** * Adds containers to a node that is a table * * References to existing children are returned * if this function is called twice on the same node * * @param NodeTable $table The table node, new containers will be * attached to this node * @param int $pos2 The position for the pagination of * the branch at the second level of the tree * @param string $type3 The type of item being paginated on * the third level of the tree * @param int $pos3 The position for the pagination of * the branch at the third level of the tree * * @return array An array of new nodes */ private function _addTableContainers($table, $pos2, $type3, $pos3) { $retval = array(); if ($table->hasChildren(true) == 0) { if ($table->getPresence('columns')) { $retval['columns'] = NodeFactory::getInstance( 'NodeColumnContainer' ); } if ($table->getPresence('indexes')) { $retval['indexes'] = NodeFactory::getInstance( 'NodeIndexContainer' ); } if ($table->getPresence('triggers')) { $retval['triggers'] = NodeFactory::getInstance( 'NodeTriggerContainer' ); } // Add all new Nodes to the tree foreach ($retval as $node) { $node->pos2 = $pos2; if ($type3 == $node->real_name) { $node->pos3 = $pos3; } $table->addChild($node); } } else { foreach ($table->children as $node) { if ($type3 == $node->real_name) { $node->pos3 = $pos3; } $retval[$node->real_name] = $node; } } return $retval; } /** * Adds containers to a node that is a database * * References to existing children are returned * if this function is called twice on the same node * * @param NodeDatabase $db The database node, new containers will be * attached to this node * @param string $type The type of item being paginated on * the second level of the tree * @param int $pos2 The position for the pagination of * the branch at the second level of the tree * * @return array An array of new nodes */ private function _addDbContainers($db, $type, $pos2) { // Get items to hide $hidden = $db->getHiddenItems('group'); if (!$GLOBALS['cfg']['NavigationTreeShowTables'] && !in_array('tables', $hidden) ) { $hidden[] = 'tables'; } if (!$GLOBALS['cfg']['NavigationTreeShowViews'] && !in_array('views', $hidden) ) { $hidden[] = 'views'; } if (!$GLOBALS['cfg']['NavigationTreeShowFunctions'] && !in_array('functions', $hidden) ) { $hidden[] = 'functions'; } if (!$GLOBALS['cfg']['NavigationTreeShowProcedures'] && !in_array('procedures', $hidden) ) { $hidden[] = 'procedures'; } if (!$GLOBALS['cfg']['NavigationTreeShowEvents'] && !in_array('events', $hidden) ) { $hidden[] = 'events'; } $retval = array(); if ($db->hasChildren(true) == 0) { if (!in_array('tables', $hidden) && $db->getPresence('tables')) { $retval['tables'] = NodeFactory::getInstance( 'NodeTableContainer' ); } if (!in_array('views', $hidden) && $db->getPresence('views')) { $retval['views'] = NodeFactory::getInstance( 'NodeViewContainer' ); } if (!in_array('functions', $hidden) && $db->getPresence('functions')) { $retval['functions'] = NodeFactory::getInstance( 'NodeFunctionContainer' ); } if (!in_array('procedures', $hidden) && $db->getPresence('procedures')) { $retval['procedures'] = NodeFactory::getInstance( 'NodeProcedureContainer' ); } if (!in_array('events', $hidden) && $db->getPresence('events')) { $retval['events'] = NodeFactory::getInstance( 'NodeEventContainer' ); } // Add all new Nodes to the tree foreach ($retval as $node) { if ($type == $node->real_name) { $node->pos2 = $pos2; } $db->addChild($node); } } else { foreach ($db->children as $node) { if ($type == $node->real_name) { $node->pos2 = $pos2; } $retval[$node->real_name] = $node; } } return $retval; } /** * Recursively groups tree nodes given a separator * * @param mixed $node The node to group or null * to group the whole tree. If * passed as an argument, $node * must be of type CONTAINER * * @return void */ public function groupTree($node = null) { if (!isset($node)) { $node = $this->_tree; } $this->groupNode($node); foreach ($node->children as $child) { $this->groupTree($child); } } /** * Recursively groups tree nodes given a separator * * @param Node $node The node to group * * @return void */ public function groupNode($node) { if ($node->type != Node::CONTAINER || !$GLOBALS['cfg']['NavigationTreeEnableExpansion'] ) { return; } $separators = array(); if (is_array($node->separator)) { $separators = $node->separator; } else { if (strlen($node->separator)) { $separators[] = $node->separator; } } $prefixes = array(); if ($node->separator_depth > 0) { foreach ($node->children as $child) { $prefix_pos = false; foreach ($separators as $separator) { $sep_pos = mb_strpos($child->name, $separator); if ($sep_pos != false && $sep_pos != mb_strlen($child->name) && $sep_pos != 0 && ($prefix_pos == false || $sep_pos < $prefix_pos) ) { $prefix_pos = $sep_pos; } } if ($prefix_pos !== false) { $prefix = mb_substr($child->name, 0, $prefix_pos); if (!isset($prefixes[$prefix])) { $prefixes[$prefix] = 1; } else { $prefixes[$prefix]++; } } //Bug #4375: Check if prefix is the name of a DB, to create a group. foreach ($node->children as $otherChild) { if (array_key_exists($otherChild->name, $prefixes)) { $prefixes[$otherChild->name]++; } } } //Check if prefix is the name of a DB, to create a group. foreach ($node->children as $child) { if (array_key_exists($child->name, $prefixes)) { $prefixes[$child->name]++; } } } // It is not a group if it has only one item foreach ($prefixes as $key => $value) { if ($value == 1) { unset($prefixes[$key]); } } // rfe #1634 Don't group if there's only one group and no other items if (count($prefixes) == 1) { $keys = array_keys($prefixes); $key = $keys[0]; if ($prefixes[$key] == count($node->children) - 1) { unset($prefixes[$key]); } } if (count($prefixes)) { /** @var Node[] $groups */ $groups = array(); foreach ($prefixes as $key => $value) { // warn about large groups if ($value > 500 && !$this->_largeGroupWarning) { trigger_error( __( 'There are large item groups in navigation panel which ' . 'may affect the performance. Consider disabling item ' . 'grouping in the navigation panel.' ), E_USER_WARNING ); $this->_largeGroupWarning = true; } $groups[$key] = new Node( htmlspecialchars((string) $key), Node::CONTAINER, true ); $groups[$key]->separator = $node->separator; $groups[$key]->separator_depth = $node->separator_depth - 1; $groups[$key]->icon = Util::getImage( 'b_group' ); $groups[$key]->pos2 = $node->pos2; $groups[$key]->pos3 = $node->pos3; if ($node instanceof NodeTableContainer || $node instanceof NodeViewContainer ) { $tblGroup = '&tbl_group=' . urlencode($key); $groups[$key]->links = array( 'text' => $node->links['text'] . $tblGroup, 'icon' => $node->links['icon'] . $tblGroup, ); } $node->addChild($groups[$key]); foreach ($separators as $separator) { $separatorLength = strlen($separator); // FIXME: this could be more efficient foreach ($node->children as $child) { $keySeparatorLength = mb_strlen($key) + $separatorLength; $name_substring = mb_substr( $child->name, 0, $keySeparatorLength ); if (($name_substring != $key . $separator && $child->name != $key) || $child->type != Node::OBJECT ) { continue; } $class = get_class($child); $className = substr($class, strrpos($class, '\\') + 1); unset($class); $new_child = NodeFactory::getInstance( $className, mb_substr( $child->name, $keySeparatorLength ) ); if ($new_child instanceof NodeDatabase && $child->getHiddenCount() > 0 ) { $new_child->setHiddenCount($child->getHiddenCount()); } $new_child->real_name = $child->real_name; $new_child->icon = $child->icon; $new_child->links = $child->links; $new_child->pos2 = $child->pos2; $new_child->pos3 = $child->pos3; $groups[$key]->addChild($new_child); foreach ($child->children as $elm) { $new_child->addChild($elm); } $node->removeChild($child->name); } } } foreach ($prefixes as $key => $value) { $this->groupNode($groups[$key]); $groups[$key]->classes = "navGroup"; } } } /** * Renders a state of the tree, used in light mode when * either JavaScript and/or Ajax are disabled * * @return string HTML code for the navigation tree */ public function renderState() { $this->_buildPath(); $retval = $this->_quickWarp(); $retval .= '
'; $retval .= '