Node.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Functionality for the navigation tree in the left frame
  5. *
  6. * @package PhpMyAdmin-Navigation
  7. */
  8. namespace PhpMyAdmin\Navigation\Nodes;
  9. use PhpMyAdmin\DatabaseInterface;
  10. use PhpMyAdmin\Relation;
  11. use PhpMyAdmin\Util;
  12. /**
  13. * The Node is the building block for the collapsible navigation tree
  14. *
  15. * @package PhpMyAdmin-Navigation
  16. */
  17. class Node
  18. {
  19. /**
  20. * @var int Defines a possible node type
  21. */
  22. const CONTAINER = 0;
  23. /**
  24. * @var int Defines a possible node type
  25. */
  26. const OBJECT = 1;
  27. /**
  28. * @var string A non-unique identifier for the node
  29. * This may be trimmed when grouping nodes
  30. */
  31. public $name = "";
  32. /**
  33. * @var string A non-unique identifier for the node
  34. * This will never change after being assigned
  35. */
  36. public $real_name = "";
  37. /**
  38. * @var int May be one of CONTAINER or OBJECT
  39. */
  40. public $type = Node::OBJECT;
  41. /**
  42. * @var bool Whether this object has been created while grouping nodes
  43. * Only relevant if the node is of type CONTAINER
  44. */
  45. public $is_group;
  46. /**
  47. * @var bool Whether to add a "display: none;" CSS
  48. * rule to the node when rendering it
  49. */
  50. public $visible = false;
  51. /**
  52. * @var Node A reference to the parent object of
  53. * this node, NULL for the root node.
  54. */
  55. public $parent;
  56. /**
  57. * @var Node[] An array of Node objects that are
  58. * direct children of this node
  59. */
  60. public $children = array();
  61. /**
  62. * @var Mixed A string used to group nodes, or an array of strings
  63. * Only relevant if the node is of type CONTAINER
  64. */
  65. public $separator = '';
  66. /**
  67. * @var int How many time to recursively apply the grouping function
  68. * Only relevant if the node is of type CONTAINER
  69. */
  70. public $separator_depth = 1;
  71. /**
  72. * @var string An IMG tag, used when rendering the node
  73. */
  74. public $icon;
  75. /**
  76. * @var array An array of A tags, used when rendering the node
  77. * The indexes in the array may be 'icon' and 'text'
  78. */
  79. public $links;
  80. /**
  81. * @var string HTML title
  82. */
  83. public $title;
  84. /**
  85. * @var string Extra CSS classes for the node
  86. */
  87. public $classes = '';
  88. /**
  89. * @var bool Whether this node is a link for creating new objects
  90. */
  91. public $isNew = false;
  92. /**
  93. * @var int The position for the pagination of
  94. * the branch at the second level of the tree
  95. */
  96. public $pos2 = 0;
  97. /**
  98. * @var int The position for the pagination of
  99. * the branch at the third level of the tree
  100. */
  101. public $pos3 = 0;
  102. /**
  103. * @var Relation $relation
  104. */
  105. protected $relation;
  106. /**
  107. * Initialises the class by setting the mandatory variables
  108. *
  109. * @param string $name An identifier for the new node
  110. * @param int $type Type of node, may be one of CONTAINER or OBJECT
  111. * @param bool $is_group Whether this object has been created
  112. * while grouping nodes
  113. */
  114. public function __construct($name, $type = Node::OBJECT, $is_group = false)
  115. {
  116. if (strlen($name)) {
  117. $this->name = $name;
  118. $this->real_name = $name;
  119. }
  120. if ($type === Node::CONTAINER) {
  121. $this->type = Node::CONTAINER;
  122. }
  123. $this->is_group = (bool)$is_group;
  124. $this->relation = new Relation();
  125. }
  126. /**
  127. * Adds a child node to this node
  128. *
  129. * @param Node $child A child node
  130. *
  131. * @return void
  132. */
  133. public function addChild($child)
  134. {
  135. $this->children[] = $child;
  136. $child->parent = $this;
  137. }
  138. /**
  139. * Returns a child node given it's name
  140. *
  141. * @param string $name The name of requested child
  142. * @param bool $real_name Whether to use the "real_name"
  143. * instead of "name" in comparisons
  144. *
  145. * @return false|Node The requested child node or false,
  146. * if the requested node cannot be found
  147. */
  148. public function getChild($name, $real_name = false)
  149. {
  150. if ($real_name) {
  151. foreach ($this->children as $child) {
  152. if ($child->real_name == $name) {
  153. return $child;
  154. }
  155. }
  156. } else {
  157. foreach ($this->children as $child) {
  158. if ($child->name == $name) {
  159. return $child;
  160. }
  161. }
  162. }
  163. return false;
  164. }
  165. /**
  166. * Removes a child node from this node
  167. *
  168. * @param string $name The name of child to be removed
  169. *
  170. * @return void
  171. */
  172. public function removeChild($name)
  173. {
  174. foreach ($this->children as $key => $child) {
  175. if ($child->name == $name) {
  176. unset($this->children[$key]);
  177. break;
  178. }
  179. }
  180. }
  181. /**
  182. * Retrieves the parents for a node
  183. *
  184. * @param bool $self Whether to include the Node itself in the results
  185. * @param bool $containers Whether to include nodes of type CONTAINER
  186. * @param bool $groups Whether to include nodes which have $group == true
  187. *
  188. * @return array An array of parent Nodes
  189. */
  190. public function parents($self = false, $containers = false, $groups = false)
  191. {
  192. $parents = array();
  193. if ($self
  194. && ($this->type != Node::CONTAINER || $containers)
  195. && (!$this->is_group || $groups)
  196. ) {
  197. $parents[] = $this;
  198. }
  199. $parent = $this->parent;
  200. while (isset($parent)) {
  201. if (($parent->type != Node::CONTAINER || $containers)
  202. && (!$parent->is_group || $groups)
  203. ) {
  204. $parents[] = $parent;
  205. }
  206. $parent = $parent->parent;
  207. }
  208. return $parents;
  209. }
  210. /**
  211. * Returns the actual parent of a node. If used twice on an index or columns
  212. * node, it will return the table and database nodes. The names of the returned
  213. * nodes can be used in SQL queries, etc...
  214. *
  215. * @return Node|false
  216. */
  217. public function realParent()
  218. {
  219. $retval = $this->parents();
  220. if (count($retval) <= 0) {
  221. return false;
  222. }
  223. return $retval[0];
  224. }
  225. /**
  226. * This function checks if the node has children nodes associated with it
  227. *
  228. * @param bool $count_empty_containers Whether to count empty child
  229. * containers as valid children
  230. *
  231. * @return bool Whether the node has child nodes
  232. */
  233. public function hasChildren($count_empty_containers = true)
  234. {
  235. $retval = false;
  236. if ($count_empty_containers) {
  237. if (count($this->children)) {
  238. $retval = true;
  239. }
  240. } else {
  241. foreach ($this->children as $child) {
  242. if ($child->type == Node::OBJECT || $child->hasChildren(false)) {
  243. $retval = true;
  244. break;
  245. }
  246. }
  247. }
  248. return $retval;
  249. }
  250. /**
  251. * Returns true if the node has some siblings (other nodes on the same tree
  252. * level, in the same branch), false otherwise.
  253. * The only exception is for nodes on
  254. * the third level of the tree (columns and indexes), for which the function
  255. * always returns true. This is because we want to render the containers
  256. * for these nodes
  257. *
  258. * @return bool
  259. */
  260. public function hasSiblings()
  261. {
  262. $retval = false;
  263. $paths = $this->getPaths();
  264. if (count($paths['aPath_clean']) > 3) {
  265. $retval = true;
  266. return $retval;
  267. }
  268. foreach ($this->parent->children as $child) {
  269. if ($child !== $this
  270. && ($child->type == Node::OBJECT || $child->hasChildren(false))
  271. ) {
  272. $retval = true;
  273. break;
  274. }
  275. }
  276. return $retval;
  277. }
  278. /**
  279. * Returns the number of child nodes that a node has associated with it
  280. *
  281. * @return int The number of children nodes
  282. */
  283. public function numChildren()
  284. {
  285. $retval = 0;
  286. foreach ($this->children as $child) {
  287. if ($child->type == Node::OBJECT) {
  288. $retval++;
  289. } else {
  290. $retval += $child->numChildren();
  291. }
  292. }
  293. return $retval;
  294. }
  295. /**
  296. * Returns the actual path and the virtual paths for a node
  297. * both as clean arrays and base64 encoded strings
  298. *
  299. * @return array
  300. */
  301. public function getPaths()
  302. {
  303. $aPath = array();
  304. $aPath_clean = array();
  305. foreach ($this->parents(true, true, false) as $parent) {
  306. $aPath[] = base64_encode($parent->real_name);
  307. $aPath_clean[] = $parent->real_name;
  308. }
  309. $aPath = implode('.', array_reverse($aPath));
  310. $aPath_clean = array_reverse($aPath_clean);
  311. $vPath = array();
  312. $vPath_clean = array();
  313. foreach ($this->parents(true, true, true) as $parent) {
  314. $vPath[] = base64_encode($parent->name);
  315. $vPath_clean[] = $parent->name;
  316. }
  317. $vPath = implode('.', array_reverse($vPath));
  318. $vPath_clean = array_reverse($vPath_clean);
  319. return array(
  320. 'aPath' => $aPath,
  321. 'aPath_clean' => $aPath_clean,
  322. 'vPath' => $vPath,
  323. 'vPath_clean' => $vPath_clean,
  324. );
  325. }
  326. /**
  327. * Returns the names of children of type $type present inside this container
  328. * This method is overridden by the PhpMyAdmin\Navigation\Nodes\NodeDatabase and PhpMyAdmin\Navigation\Nodes\NodeTable classes
  329. *
  330. * @param string $type The type of item we are looking for
  331. * ('tables', 'views', etc)
  332. * @param int $pos The offset of the list within the results
  333. * @param string $searchClause A string used to filter the results of the query
  334. *
  335. * @return array
  336. */
  337. public function getData($type, $pos, $searchClause = '')
  338. {
  339. $maxItems = $GLOBALS['cfg']['FirstLevelNavigationItems'];
  340. if (!$GLOBALS['cfg']['NavigationTreeEnableGrouping']
  341. || !$GLOBALS['cfg']['ShowDatabasesNavigationAsTree']
  342. ) {
  343. if (isset($GLOBALS['cfg']['Server']['DisableIS'])
  344. && !$GLOBALS['cfg']['Server']['DisableIS']
  345. ) {
  346. $query = "SELECT `SCHEMA_NAME` ";
  347. $query .= "FROM `INFORMATION_SCHEMA`.`SCHEMATA` ";
  348. $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause);
  349. $query .= "ORDER BY `SCHEMA_NAME` ";
  350. $query .= "LIMIT $pos, $maxItems";
  351. $retval = $GLOBALS['dbi']->fetchResult($query);
  352. return $retval;
  353. }
  354. if ($GLOBALS['dbs_to_test'] === false) {
  355. $retval = array();
  356. $query = "SHOW DATABASES ";
  357. $query .= $this->_getWhereClause('Database', $searchClause);
  358. $handle = $GLOBALS['dbi']->tryQuery($query);
  359. if ($handle === false) {
  360. return $retval;
  361. }
  362. $count = 0;
  363. if (!$GLOBALS['dbi']->dataSeek($handle, $pos)) {
  364. return $retval;
  365. }
  366. while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
  367. if ($count < $maxItems) {
  368. $retval[] = $arr[0];
  369. $count++;
  370. } else {
  371. break;
  372. }
  373. }
  374. return $retval;
  375. }
  376. $retval = array();
  377. $count = 0;
  378. foreach ($this->_getDatabasesToSearch($searchClause) as $db) {
  379. $query = "SHOW DATABASES LIKE '" . $db . "'";
  380. $handle = $GLOBALS['dbi']->tryQuery($query);
  381. if ($handle === false) {
  382. continue;
  383. }
  384. while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
  385. if ($this->_isHideDb($arr[0])) {
  386. continue;
  387. }
  388. if (in_array($arr[0], $retval)) {
  389. continue;
  390. }
  391. if ($pos <= 0 && $count < $maxItems) {
  392. $retval[] = $arr[0];
  393. $count++;
  394. }
  395. $pos--;
  396. }
  397. }
  398. sort($retval);
  399. return $retval;
  400. }
  401. $dbSeparator = $GLOBALS['cfg']['NavigationTreeDbSeparator'];
  402. if (isset($GLOBALS['cfg']['Server']['DisableIS'])
  403. && !$GLOBALS['cfg']['Server']['DisableIS']
  404. ) {
  405. $query = "SELECT `SCHEMA_NAME` ";
  406. $query .= "FROM `INFORMATION_SCHEMA`.`SCHEMATA`, ";
  407. $query .= "(";
  408. $query .= "SELECT DB_first_level ";
  409. $query .= "FROM ( ";
  410. $query .= "SELECT DISTINCT SUBSTRING_INDEX(SCHEMA_NAME, ";
  411. $query .= "'" . $GLOBALS['dbi']->escapeString($dbSeparator) . "', 1) ";
  412. $query .= "DB_first_level ";
  413. $query .= "FROM INFORMATION_SCHEMA.SCHEMATA ";
  414. $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause);
  415. $query .= ") t ";
  416. $query .= "ORDER BY DB_first_level ASC ";
  417. $query .= "LIMIT $pos, $maxItems";
  418. $query .= ") t2 ";
  419. $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause);
  420. $query .= "AND 1 = LOCATE(CONCAT(DB_first_level, ";
  421. $query .= "'" . $GLOBALS['dbi']->escapeString($dbSeparator) . "'), ";
  422. $query .= "CONCAT(SCHEMA_NAME, ";
  423. $query .= "'" . $GLOBALS['dbi']->escapeString($dbSeparator) . "')) ";
  424. $query .= "ORDER BY SCHEMA_NAME ASC";
  425. $retval = $GLOBALS['dbi']->fetchResult($query);
  426. return $retval;
  427. }
  428. if ($GLOBALS['dbs_to_test'] === false) {
  429. $query = "SHOW DATABASES ";
  430. $query .= $this->_getWhereClause('Database', $searchClause);
  431. $handle = $GLOBALS['dbi']->tryQuery($query);
  432. $prefixes = array();
  433. if ($handle !== false) {
  434. $prefixMap = array();
  435. $total = $pos + $maxItems;
  436. while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
  437. $prefix = strstr($arr[0], $dbSeparator, true);
  438. if ($prefix === false) {
  439. $prefix = $arr[0];
  440. }
  441. $prefixMap[$prefix] = 1;
  442. if (sizeof($prefixMap) == $total) {
  443. break;
  444. }
  445. }
  446. $prefixes = array_slice(array_keys($prefixMap), $pos);
  447. }
  448. $query = "SHOW DATABASES ";
  449. $query .= $this->_getWhereClause('Database', $searchClause);
  450. $query .= "AND (";
  451. $subClauses = array();
  452. foreach ($prefixes as $prefix) {
  453. $subClauses[] = " LOCATE('"
  454. . $GLOBALS['dbi']->escapeString($prefix) . $dbSeparator
  455. . "', "
  456. . "CONCAT(`Database`, '" . $dbSeparator . "')) = 1 ";
  457. }
  458. $query .= implode("OR", $subClauses) . ")";
  459. $retval = $GLOBALS['dbi']->fetchResult($query);
  460. return $retval;
  461. }
  462. $retval = array();
  463. $prefixMap = array();
  464. $total = $pos + $maxItems;
  465. foreach ($this->_getDatabasesToSearch($searchClause) as $db) {
  466. $query = "SHOW DATABASES LIKE '" . $db . "'";
  467. $handle = $GLOBALS['dbi']->tryQuery($query);
  468. if ($handle === false) {
  469. continue;
  470. }
  471. while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
  472. if ($this->_isHideDb($arr[0])) {
  473. continue;
  474. }
  475. $prefix = strstr($arr[0], $dbSeparator, true);
  476. if ($prefix === false) {
  477. $prefix = $arr[0];
  478. }
  479. $prefixMap[$prefix] = 1;
  480. if (sizeof($prefixMap) == $total) {
  481. break 2;
  482. }
  483. }
  484. }
  485. $prefixes = array_slice(array_keys($prefixMap), $pos);
  486. foreach ($this->_getDatabasesToSearch($searchClause) as $db) {
  487. $query = "SHOW DATABASES LIKE '" . $db . "'";
  488. $handle = $GLOBALS['dbi']->tryQuery($query);
  489. if ($handle === false) {
  490. continue;
  491. }
  492. while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
  493. if ($this->_isHideDb($arr[0])) {
  494. continue;
  495. }
  496. if (in_array($arr[0], $retval)) {
  497. continue;
  498. }
  499. foreach ($prefixes as $prefix) {
  500. $starts_with = strpos(
  501. $arr[0] . $dbSeparator,
  502. $prefix . $dbSeparator
  503. ) === 0;
  504. if ($starts_with) {
  505. $retval[] = $arr[0];
  506. break;
  507. }
  508. }
  509. }
  510. }
  511. sort($retval);
  512. return $retval;
  513. }
  514. /**
  515. * Returns the number of children of type $type present inside this container
  516. * This method is overridden by the PhpMyAdmin\Navigation\Nodes\NodeDatabase and PhpMyAdmin\Navigation\Nodes\NodeTable classes
  517. *
  518. * @param string $type The type of item we are looking for
  519. * ('tables', 'views', etc)
  520. * @param string $searchClause A string used to filter the results of the query
  521. *
  522. * @return int
  523. */
  524. public function getPresence($type = '', $searchClause = '')
  525. {
  526. if (!$GLOBALS['cfg']['NavigationTreeEnableGrouping']
  527. || !$GLOBALS['cfg']['ShowDatabasesNavigationAsTree']
  528. ) {
  529. if (isset($GLOBALS['cfg']['Server']['DisableIS'])
  530. && !$GLOBALS['cfg']['Server']['DisableIS']
  531. ) {
  532. $query = "SELECT COUNT(*) ";
  533. $query .= "FROM INFORMATION_SCHEMA.SCHEMATA ";
  534. $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause);
  535. $retval = (int)$GLOBALS['dbi']->fetchValue($query);
  536. return $retval;
  537. }
  538. if ($GLOBALS['dbs_to_test'] === false) {
  539. $query = "SHOW DATABASES ";
  540. $query .= $this->_getWhereClause('Database', $searchClause);
  541. $retval = $GLOBALS['dbi']->numRows(
  542. $GLOBALS['dbi']->tryQuery($query)
  543. );
  544. return $retval;
  545. }
  546. $retval = 0;
  547. foreach ($this->_getDatabasesToSearch($searchClause) as $db) {
  548. $query = "SHOW DATABASES LIKE '" . $db . "'";
  549. $retval += $GLOBALS['dbi']->numRows(
  550. $GLOBALS['dbi']->tryQuery($query)
  551. );
  552. }
  553. return $retval;
  554. }
  555. $dbSeparator = $GLOBALS['cfg']['NavigationTreeDbSeparator'];
  556. if (!$GLOBALS['cfg']['Server']['DisableIS']) {
  557. $query = "SELECT COUNT(*) ";
  558. $query .= "FROM ( ";
  559. $query .= "SELECT DISTINCT SUBSTRING_INDEX(SCHEMA_NAME, ";
  560. $query .= "'$dbSeparator', 1) ";
  561. $query .= "DB_first_level ";
  562. $query .= "FROM INFORMATION_SCHEMA.SCHEMATA ";
  563. $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause);
  564. $query .= ") t ";
  565. $retval = (int)$GLOBALS['dbi']->fetchValue($query);
  566. return $retval;
  567. }
  568. if ($GLOBALS['dbs_to_test'] !== false) {
  569. $prefixMap = array();
  570. foreach ($this->_getDatabasesToSearch($searchClause) as $db) {
  571. $query = "SHOW DATABASES LIKE '" . $db . "'";
  572. $handle = $GLOBALS['dbi']->tryQuery($query);
  573. if ($handle === false) {
  574. continue;
  575. }
  576. while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
  577. if ($this->_isHideDb($arr[0])) {
  578. continue;
  579. }
  580. $prefix = strstr($arr[0], $dbSeparator, true);
  581. if ($prefix === false) {
  582. $prefix = $arr[0];
  583. }
  584. $prefixMap[$prefix] = 1;
  585. }
  586. }
  587. $retval = count($prefixMap);
  588. return $retval;
  589. }
  590. $prefixMap = array();
  591. $query = "SHOW DATABASES ";
  592. $query .= $this->_getWhereClause('Database', $searchClause);
  593. $handle = $GLOBALS['dbi']->tryQuery($query);
  594. if ($handle !== false) {
  595. while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
  596. $prefix = strstr($arr[0], $dbSeparator, true);
  597. if ($prefix === false) {
  598. $prefix = $arr[0];
  599. }
  600. $prefixMap[$prefix] = 1;
  601. }
  602. }
  603. $retval = count($prefixMap);
  604. return $retval;
  605. }
  606. /**
  607. * Detemines whether a given database should be hidden according to 'hide_db'
  608. *
  609. * @param string $db database name
  610. *
  611. * @return boolean whether to hide
  612. */
  613. private function _isHideDb($db)
  614. {
  615. return !empty($GLOBALS['cfg']['Server']['hide_db'])
  616. && preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db);
  617. }
  618. /**
  619. * Get the list of databases for 'SHOW DATABASES LIKE' queries.
  620. * If a search clause is set it gets the highest priority while only_db gets
  621. * the next priority. In case both are empty list of databases determined by
  622. * GRANTs are used
  623. *
  624. * @param string $searchClause search clause
  625. *
  626. * @return array array of databases
  627. */
  628. private function _getDatabasesToSearch($searchClause)
  629. {
  630. if (!empty($searchClause)) {
  631. $databases = array(
  632. "%" . $GLOBALS['dbi']->escapeString($searchClause) . "%",
  633. );
  634. } elseif (!empty($GLOBALS['cfg']['Server']['only_db'])) {
  635. $databases = $GLOBALS['cfg']['Server']['only_db'];
  636. } elseif (!empty($GLOBALS['dbs_to_test'])) {
  637. $databases = $GLOBALS['dbs_to_test'];
  638. }
  639. sort($databases);
  640. return $databases;
  641. }
  642. /**
  643. * Returns the WHERE clause depending on the $searchClause parameter
  644. * and the hide_db directive
  645. *
  646. * @param string $columnName Column name of the column having database names
  647. * @param string $searchClause A string used to filter the results of the query
  648. *
  649. * @return string
  650. */
  651. private function _getWhereClause($columnName, $searchClause = '')
  652. {
  653. $whereClause = "WHERE TRUE ";
  654. if (!empty($searchClause)) {
  655. $whereClause .= "AND " . Util::backquote($columnName)
  656. . " LIKE '%";
  657. $whereClause .= $GLOBALS['dbi']->escapeString($searchClause);
  658. $whereClause .= "%' ";
  659. }
  660. if (!empty($GLOBALS['cfg']['Server']['hide_db'])) {
  661. $whereClause .= "AND " . Util::backquote($columnName)
  662. . " NOT REGEXP '"
  663. . $GLOBALS['dbi']->escapeString($GLOBALS['cfg']['Server']['hide_db'])
  664. . "' ";
  665. }
  666. if (!empty($GLOBALS['cfg']['Server']['only_db'])) {
  667. if (is_string($GLOBALS['cfg']['Server']['only_db'])) {
  668. $GLOBALS['cfg']['Server']['only_db'] = array(
  669. $GLOBALS['cfg']['Server']['only_db'],
  670. );
  671. }
  672. $whereClause .= "AND (";
  673. $subClauses = array();
  674. foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
  675. $subClauses[] = " " . Util::backquote($columnName)
  676. . " LIKE '"
  677. . $GLOBALS['dbi']->escapeString($each_only_db) . "' ";
  678. }
  679. $whereClause .= implode("OR", $subClauses) . ") ";
  680. }
  681. return $whereClause;
  682. }
  683. /**
  684. * Returns HTML for control buttons displayed infront of a node
  685. *
  686. * @return String HTML for control buttons
  687. */
  688. public function getHtmlForControlButtons()
  689. {
  690. return '';
  691. }
  692. /**
  693. * Returns CSS classes for a node
  694. *
  695. * @param boolean $match Whether the node matched loaded tree
  696. *
  697. * @return String with html classes.
  698. */
  699. public function getCssClasses($match)
  700. {
  701. if (!$GLOBALS['cfg']['NavigationTreeEnableExpansion']
  702. ) {
  703. return '';
  704. }
  705. $result = array('expander');
  706. if ($this->is_group || $match) {
  707. $result[] = 'loaded';
  708. }
  709. if ($this->type == Node::CONTAINER) {
  710. $result[] = 'container';
  711. }
  712. return implode(' ', $result);
  713. }
  714. /**
  715. * Returns icon for the node
  716. *
  717. * @param boolean $match Whether the node matched loaded tree
  718. *
  719. * @return String with image name
  720. */
  721. public function getIcon($match)
  722. {
  723. if (!$GLOBALS['cfg']['NavigationTreeEnableExpansion']
  724. ) {
  725. return '';
  726. } elseif ($match) {
  727. $this->visible = true;
  728. return Util::getImage('b_minus');
  729. }
  730. return Util::getImage('b_plus', __('Expand/Collapse'));
  731. }
  732. /**
  733. * Gets the count of hidden elements for each database
  734. *
  735. * @return array array containing the count of hidden elements for each database
  736. */
  737. public function getNavigationHidingData()
  738. {
  739. $cfgRelation = $this->relation->getRelationsParam();
  740. if ($cfgRelation['navwork']) {
  741. $navTable = Util::backquote($cfgRelation['db'])
  742. . "." . Util::backquote(
  743. $cfgRelation['navigationhiding']
  744. );
  745. $sqlQuery = "SELECT `db_name`, COUNT(*) AS `count` FROM " . $navTable
  746. . " WHERE `username`='"
  747. . $GLOBALS['dbi']->escapeString(
  748. $GLOBALS['cfg']['Server']['user']
  749. ) . "'"
  750. . " GROUP BY `db_name`";
  751. $counts = $GLOBALS['dbi']->fetchResult(
  752. $sqlQuery,
  753. 'db_name',
  754. 'count',
  755. DatabaseInterface::CONNECT_CONTROL
  756. );
  757. return $counts;
  758. }
  759. return null;
  760. }
  761. }