Node.php 26 KB

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