NavigationTree.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Functionality for the navigation tree
  5. *
  6. * @package PhpMyAdmin-Navigation
  7. */
  8. namespace PhpMyAdmin\Navigation;
  9. use PhpMyAdmin\Navigation\Nodes\Node;
  10. use PhpMyAdmin\Navigation\Nodes\NodeDatabase;
  11. use PhpMyAdmin\Navigation\Nodes\NodeTable;
  12. use PhpMyAdmin\Navigation\Nodes\NodeTableContainer;
  13. use PhpMyAdmin\Navigation\Nodes\NodeViewContainer;
  14. use PhpMyAdmin\RecentFavoriteTable;
  15. use PhpMyAdmin\Response;
  16. use PhpMyAdmin\Util;
  17. use PhpMyAdmin\Url;
  18. require_once 'libraries/check_user_privileges.inc.php';
  19. /**
  20. * Displays a collapsible of database objects in the navigation frame
  21. *
  22. * @package PhpMyAdmin-Navigation
  23. */
  24. class NavigationTree
  25. {
  26. /**
  27. * @var Node Reference to the root node of the tree
  28. */
  29. private $_tree;
  30. /**
  31. * @var array The actual paths to all expanded nodes in the tree
  32. * This does not include nodes created after the grouping
  33. * of nodes has been performed
  34. */
  35. private $_aPath = array();
  36. /**
  37. * @var array The virtual paths to all expanded nodes in the tree
  38. * This includes nodes created after the grouping of
  39. * nodes has been performed
  40. */
  41. private $_vPath = array();
  42. /**
  43. * @var int Position in the list of databases,
  44. * used for pagination
  45. */
  46. private $_pos;
  47. /**
  48. * @var array The names of the type of items that are being paginated on
  49. * the second level of the navigation tree. These may be
  50. * tables, views, functions, procedures or events.
  51. */
  52. private $_pos2_name = array();
  53. /**
  54. * @var array The positions of nodes in the lists of tables, views,
  55. * routines or events used for pagination
  56. */
  57. private $_pos2_value = array();
  58. /**
  59. * @var array The names of the type of items that are being paginated
  60. * on the second level of the navigation tree.
  61. * These may be columns or indexes
  62. */
  63. private $_pos3_name = array();
  64. /**
  65. * @var array The positions of nodes in the lists of columns or indexes
  66. * used for pagination
  67. */
  68. private $_pos3_value = array();
  69. /**
  70. * @var string The search clause to use in SQL queries for
  71. * fetching databases
  72. * Used by the asynchronous fast filter
  73. */
  74. private $_searchClause = '';
  75. /**
  76. * @var string The search clause to use in SQL queries for
  77. * fetching nodes
  78. * Used by the asynchronous fast filter
  79. */
  80. private $_searchClause2 = '';
  81. /**
  82. * @var bool Whether a warning was raised for large item groups
  83. * which can affect performance.
  84. */
  85. private $_largeGroupWarning = false;
  86. /**
  87. * Initialises the class
  88. */
  89. public function __construct()
  90. {
  91. // Save the position at which we are in the database list
  92. if (isset($_POST['pos'])) {
  93. $this->_pos = (int) $_POST['pos'];
  94. } elseif (isset($_GET['pos'])) {
  95. $this->_pos = (int) $_GET['pos'];
  96. }
  97. if (!isset($this->_pos)) {
  98. $this->_pos = $this->_getNavigationDbPos();
  99. }
  100. // Get the active node
  101. if (isset($_POST['aPath'])) {
  102. $this->_aPath[0] = $this->_parsePath($_POST['aPath']);
  103. $this->_pos2_name[0] = $_POST['pos2_name'];
  104. $this->_pos2_value[0] = $_POST['pos2_value'];
  105. if (isset($_POST['pos3_name'])) {
  106. $this->_pos3_name[0] = $_POST['pos3_name'];
  107. $this->_pos3_value[0] = $_POST['pos3_value'];
  108. }
  109. } else {
  110. if (isset($_POST['n0_aPath'])) {
  111. $count = 0;
  112. while (isset($_POST['n' . $count . '_aPath'])) {
  113. $this->_aPath[$count] = $this->_parsePath(
  114. $_POST['n' . $count . '_aPath']
  115. );
  116. $index = 'n' . $count . '_pos2_';
  117. $this->_pos2_name[$count] = $_POST[$index . 'name'];
  118. $this->_pos2_value[$count] = $_POST[$index . 'value'];
  119. $index = 'n' . $count . '_pos3_';
  120. if (isset($_POST[$index])) {
  121. $this->_pos3_name[$count] = $_POST[$index . 'name'];
  122. $this->_pos3_value[$count] = $_POST[$index . 'value'];
  123. }
  124. $count++;
  125. }
  126. }
  127. }
  128. if (isset($_POST['vPath'])) {
  129. $this->_vPath[0] = $this->_parsePath($_POST['vPath']);
  130. } else {
  131. if (isset($_POST['n0_vPath'])) {
  132. $count = 0;
  133. while (isset($_POST['n' . $count . '_vPath'])) {
  134. $this->_vPath[$count] = $this->_parsePath(
  135. $_POST['n' . $count . '_vPath']
  136. );
  137. $count++;
  138. }
  139. }
  140. }
  141. if (isset($_POST['searchClause'])) {
  142. $this->_searchClause = $_POST['searchClause'];
  143. }
  144. if (isset($_POST['searchClause2'])) {
  145. $this->_searchClause2 = $_POST['searchClause2'];
  146. }
  147. // Initialise the tree by creating a root node
  148. $node = NodeFactory::getInstance('NodeDatabaseContainer', 'root');
  149. $this->_tree = $node;
  150. if ($GLOBALS['cfg']['NavigationTreeEnableGrouping']
  151. && $GLOBALS['cfg']['ShowDatabasesNavigationAsTree']
  152. ) {
  153. $this->_tree->separator = $GLOBALS['cfg']['NavigationTreeDbSeparator'];
  154. $this->_tree->separator_depth = 10000;
  155. }
  156. }
  157. /**
  158. * Returns the database position for the page selector
  159. *
  160. * @return int
  161. */
  162. private function _getNavigationDbPos()
  163. {
  164. $retval = 0;
  165. if (strlen($GLOBALS['db']) == 0) {
  166. return $retval;
  167. }
  168. /*
  169. * @todo describe a scenario where this code is executed
  170. */
  171. if (!$GLOBALS['cfg']['Server']['DisableIS']) {
  172. $dbSeparator = $GLOBALS['dbi']->escapeString(
  173. $GLOBALS['cfg']['NavigationTreeDbSeparator']
  174. );
  175. $query = "SELECT (COUNT(DB_first_level) DIV %d) * %d ";
  176. $query .= "from ( ";
  177. $query .= " SELECT distinct SUBSTRING_INDEX(SCHEMA_NAME, ";
  178. $query .= " '%s', 1) ";
  179. $query .= " DB_first_level ";
  180. $query .= " FROM INFORMATION_SCHEMA.SCHEMATA ";
  181. $query .= " WHERE `SCHEMA_NAME` < '%s' ";
  182. $query .= ") t ";
  183. $retval = $GLOBALS['dbi']->fetchValue(
  184. sprintf(
  185. $query,
  186. (int)$GLOBALS['cfg']['FirstLevelNavigationItems'],
  187. (int)$GLOBALS['cfg']['FirstLevelNavigationItems'],
  188. $dbSeparator,
  189. $GLOBALS['dbi']->escapeString($GLOBALS['db'])
  190. )
  191. );
  192. return $retval;
  193. }
  194. $prefixMap = array();
  195. if ($GLOBALS['dbs_to_test'] === false) {
  196. $handle = $GLOBALS['dbi']->tryQuery("SHOW DATABASES");
  197. if ($handle !== false) {
  198. while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
  199. if (strcasecmp($arr[0], $GLOBALS['db']) >= 0) {
  200. break;
  201. }
  202. $prefix = strstr(
  203. $arr[0],
  204. $GLOBALS['cfg']['NavigationTreeDbSeparator'],
  205. true
  206. );
  207. if ($prefix === false) {
  208. $prefix = $arr[0];
  209. }
  210. $prefixMap[$prefix] = 1;
  211. }
  212. }
  213. } else {
  214. $databases = array();
  215. foreach ($GLOBALS['dbs_to_test'] as $db) {
  216. $query = "SHOW DATABASES LIKE '" . $db . "'";
  217. $handle = $GLOBALS['dbi']->tryQuery($query);
  218. if ($handle === false) {
  219. continue;
  220. }
  221. while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
  222. $databases[] = $arr[0];
  223. }
  224. }
  225. sort($databases);
  226. foreach ($databases as $database) {
  227. if (strcasecmp($database, $GLOBALS['db']) >= 0) {
  228. break;
  229. }
  230. $prefix = strstr(
  231. $database,
  232. $GLOBALS['cfg']['NavigationTreeDbSeparator'],
  233. true
  234. );
  235. if ($prefix === false) {
  236. $prefix = $database;
  237. }
  238. $prefixMap[$prefix] = 1;
  239. }
  240. }
  241. $navItems = (int)$GLOBALS['cfg']['FirstLevelNavigationItems'];
  242. $retval = floor((count($prefixMap) / $navItems)) * $navItems;
  243. return $retval;
  244. }
  245. /**
  246. * Converts an encoded path to a node in string format to an array
  247. *
  248. * @param string $string The path to parse
  249. *
  250. * @return array
  251. */
  252. private function _parsePath($string)
  253. {
  254. $path = explode('.', $string);
  255. foreach ($path as $key => $value) {
  256. $path[$key] = base64_decode($value);
  257. }
  258. return $path;
  259. }
  260. /**
  261. * Generates the tree structure so that it can be rendered later
  262. *
  263. * @return Node|false The active node or false in case of failure
  264. */
  265. private function _buildPath()
  266. {
  267. $retval = $this->_tree;
  268. // Add all databases unconditionally
  269. $data = $this->_tree->getData(
  270. 'databases',
  271. $this->_pos,
  272. $this->_searchClause
  273. );
  274. $hiddenCounts = $this->_tree->getNavigationHidingData();
  275. foreach ($data as $db) {
  276. $node = NodeFactory::getInstance('NodeDatabase', $db);
  277. if (isset($hiddenCounts[$db])) {
  278. $node->setHiddenCount($hiddenCounts[$db]);
  279. }
  280. $this->_tree->addChild($node);
  281. }
  282. // Whether build other parts of the tree depends
  283. // on whether we have any paths in $this->_aPath
  284. foreach ($this->_aPath as $key => $path) {
  285. $retval = $this->_buildPathPart(
  286. $path,
  287. $this->_pos2_name[$key],
  288. $this->_pos2_value[$key],
  289. isset($this->_pos3_name[$key]) ? $this->_pos3_name[$key] : '',
  290. isset($this->_pos3_value[$key]) ? $this->_pos3_value[$key] : ''
  291. );
  292. }
  293. return $retval;
  294. }
  295. /**
  296. * Builds a branch of the tree
  297. *
  298. * @param array $path A paths pointing to the branch
  299. * of the tree that needs to be built
  300. * @param string $type2 The type of item being paginated on
  301. * the second level of the tree
  302. * @param int $pos2 The position for the pagination of
  303. * the branch at the second level of the tree
  304. * @param string $type3 The type of item being paginated on
  305. * the third level of the tree
  306. * @param int $pos3 The position for the pagination of
  307. * the branch at the third level of the tree
  308. *
  309. * @return Node|false The active node or false in case of failure
  310. */
  311. private function _buildPathPart(array $path, $type2, $pos2, $type3, $pos3)
  312. {
  313. if (empty($pos2)) {
  314. $pos2 = 0;
  315. }
  316. if (empty($pos3)) {
  317. $pos3 = 0;
  318. }
  319. $retval = true;
  320. if (count($path) <= 1) {
  321. return $retval;
  322. }
  323. array_shift($path); // remove 'root'
  324. /* @var $db NodeDatabase */
  325. $db = $this->_tree->getChild($path[0]);
  326. $retval = $db;
  327. if ($db === false) {
  328. return false;
  329. }
  330. $containers = $this->_addDbContainers($db, $type2, $pos2);
  331. array_shift($path); // remove db
  332. if ((count($path) <= 0 || !array_key_exists($path[0], $containers))
  333. && count($containers) != 1
  334. ) {
  335. return $retval;
  336. }
  337. if (count($containers) == 1) {
  338. $container = array_shift($containers);
  339. } else {
  340. $container = $db->getChild($path[0], true);
  341. if ($container === false) {
  342. return false;
  343. }
  344. }
  345. $retval = $container;
  346. if (count($container->children) <= 1) {
  347. $dbData = $db->getData(
  348. $container->real_name,
  349. $pos2,
  350. $this->_searchClause2
  351. );
  352. foreach ($dbData as $item) {
  353. switch ($container->real_name) {
  354. case 'events':
  355. $node = NodeFactory::getInstance(
  356. 'NodeEvent',
  357. $item
  358. );
  359. break;
  360. case 'functions':
  361. $node = NodeFactory::getInstance(
  362. 'NodeFunction',
  363. $item
  364. );
  365. break;
  366. case 'procedures':
  367. $node = NodeFactory::getInstance(
  368. 'NodeProcedure',
  369. $item
  370. );
  371. break;
  372. case 'tables':
  373. $node = NodeFactory::getInstance(
  374. 'NodeTable',
  375. $item
  376. );
  377. break;
  378. case 'views':
  379. $node = NodeFactory::getInstance(
  380. 'NodeView',
  381. $item
  382. );
  383. break;
  384. default:
  385. break;
  386. }
  387. if (isset($node)) {
  388. if ($type2 == $container->real_name) {
  389. $node->pos2 = $pos2;
  390. }
  391. $container->addChild($node);
  392. }
  393. }
  394. }
  395. if (count($path) > 1 && $path[0] != 'tables') {
  396. $retval = false;
  397. return $retval;
  398. }
  399. array_shift($path); // remove container
  400. if (count($path) <= 0) {
  401. return $retval;
  402. }
  403. /* @var $table NodeTable */
  404. $table = $container->getChild($path[0], true);
  405. if ($table === false) {
  406. if (!$db->getPresence('tables', $path[0])) {
  407. return false;
  408. }
  409. $node = NodeFactory::getInstance(
  410. 'NodeTable',
  411. $path[0]
  412. );
  413. if ($type2 == $container->real_name) {
  414. $node->pos2 = $pos2;
  415. }
  416. $container->addChild($node);
  417. $table = $container->getChild($path[0], true);
  418. }
  419. $retval = $table;
  420. $containers = $this->_addTableContainers(
  421. $table,
  422. $pos2,
  423. $type3,
  424. $pos3
  425. );
  426. array_shift($path); // remove table
  427. if (count($path) <= 0
  428. || !array_key_exists($path[0], $containers)
  429. ) {
  430. return $retval;
  431. }
  432. $container = $table->getChild($path[0], true);
  433. $retval = $container;
  434. $tableData = $table->getData(
  435. $container->real_name,
  436. $pos3
  437. );
  438. foreach ($tableData as $item) {
  439. switch ($container->real_name) {
  440. case 'indexes':
  441. $node = NodeFactory::getInstance(
  442. 'NodeIndex',
  443. $item
  444. );
  445. break;
  446. case 'columns':
  447. $node = NodeFactory::getInstance(
  448. 'NodeColumn',
  449. $item
  450. );
  451. break;
  452. case 'triggers':
  453. $node = NodeFactory::getInstance(
  454. 'NodeTrigger',
  455. $item
  456. );
  457. break;
  458. default:
  459. break;
  460. }
  461. if (isset($node)) {
  462. $node->pos2 = $container->parent->pos2;
  463. if ($type3 == $container->real_name) {
  464. $node->pos3 = $pos3;
  465. }
  466. $container->addChild($node);
  467. }
  468. }
  469. return $retval;
  470. }
  471. /**
  472. * Adds containers to a node that is a table
  473. *
  474. * References to existing children are returned
  475. * if this function is called twice on the same node
  476. *
  477. * @param NodeTable $table The table node, new containers will be
  478. * attached to this node
  479. * @param int $pos2 The position for the pagination of
  480. * the branch at the second level of the tree
  481. * @param string $type3 The type of item being paginated on
  482. * the third level of the tree
  483. * @param int $pos3 The position for the pagination of
  484. * the branch at the third level of the tree
  485. *
  486. * @return array An array of new nodes
  487. */
  488. private function _addTableContainers($table, $pos2, $type3, $pos3)
  489. {
  490. $retval = array();
  491. if ($table->hasChildren(true) == 0) {
  492. if ($table->getPresence('columns')) {
  493. $retval['columns'] = NodeFactory::getInstance(
  494. 'NodeColumnContainer'
  495. );
  496. }
  497. if ($table->getPresence('indexes')) {
  498. $retval['indexes'] = NodeFactory::getInstance(
  499. 'NodeIndexContainer'
  500. );
  501. }
  502. if ($table->getPresence('triggers')) {
  503. $retval['triggers'] = NodeFactory::getInstance(
  504. 'NodeTriggerContainer'
  505. );
  506. }
  507. // Add all new Nodes to the tree
  508. foreach ($retval as $node) {
  509. $node->pos2 = $pos2;
  510. if ($type3 == $node->real_name) {
  511. $node->pos3 = $pos3;
  512. }
  513. $table->addChild($node);
  514. }
  515. } else {
  516. foreach ($table->children as $node) {
  517. if ($type3 == $node->real_name) {
  518. $node->pos3 = $pos3;
  519. }
  520. $retval[$node->real_name] = $node;
  521. }
  522. }
  523. return $retval;
  524. }
  525. /**
  526. * Adds containers to a node that is a database
  527. *
  528. * References to existing children are returned
  529. * if this function is called twice on the same node
  530. *
  531. * @param NodeDatabase $db The database node, new containers will be
  532. * attached to this node
  533. * @param string $type The type of item being paginated on
  534. * the second level of the tree
  535. * @param int $pos2 The position for the pagination of
  536. * the branch at the second level of the tree
  537. *
  538. * @return array An array of new nodes
  539. */
  540. private function _addDbContainers($db, $type, $pos2)
  541. {
  542. // Get items to hide
  543. $hidden = $db->getHiddenItems('group');
  544. if (!$GLOBALS['cfg']['NavigationTreeShowTables']
  545. && !in_array('tables', $hidden)
  546. ) {
  547. $hidden[] = 'tables';
  548. }
  549. if (!$GLOBALS['cfg']['NavigationTreeShowViews']
  550. && !in_array('views', $hidden)
  551. ) {
  552. $hidden[] = 'views';
  553. }
  554. if (!$GLOBALS['cfg']['NavigationTreeShowFunctions']
  555. && !in_array('functions', $hidden)
  556. ) {
  557. $hidden[] = 'functions';
  558. }
  559. if (!$GLOBALS['cfg']['NavigationTreeShowProcedures']
  560. && !in_array('procedures', $hidden)
  561. ) {
  562. $hidden[] = 'procedures';
  563. }
  564. if (!$GLOBALS['cfg']['NavigationTreeShowEvents']
  565. && !in_array('events', $hidden)
  566. ) {
  567. $hidden[] = 'events';
  568. }
  569. $retval = array();
  570. if ($db->hasChildren(true) == 0) {
  571. if (!in_array('tables', $hidden) && $db->getPresence('tables')) {
  572. $retval['tables'] = NodeFactory::getInstance(
  573. 'NodeTableContainer'
  574. );
  575. }
  576. if (!in_array('views', $hidden) && $db->getPresence('views')) {
  577. $retval['views'] = NodeFactory::getInstance(
  578. 'NodeViewContainer'
  579. );
  580. }
  581. if (!in_array('functions', $hidden) && $db->getPresence('functions')) {
  582. $retval['functions'] = NodeFactory::getInstance(
  583. 'NodeFunctionContainer'
  584. );
  585. }
  586. if (!in_array('procedures', $hidden) && $db->getPresence('procedures')) {
  587. $retval['procedures'] = NodeFactory::getInstance(
  588. 'NodeProcedureContainer'
  589. );
  590. }
  591. if (!in_array('events', $hidden) && $db->getPresence('events')) {
  592. $retval['events'] = NodeFactory::getInstance(
  593. 'NodeEventContainer'
  594. );
  595. }
  596. // Add all new Nodes to the tree
  597. foreach ($retval as $node) {
  598. if ($type == $node->real_name) {
  599. $node->pos2 = $pos2;
  600. }
  601. $db->addChild($node);
  602. }
  603. } else {
  604. foreach ($db->children as $node) {
  605. if ($type == $node->real_name) {
  606. $node->pos2 = $pos2;
  607. }
  608. $retval[$node->real_name] = $node;
  609. }
  610. }
  611. return $retval;
  612. }
  613. /**
  614. * Recursively groups tree nodes given a separator
  615. *
  616. * @param mixed $node The node to group or null
  617. * to group the whole tree. If
  618. * passed as an argument, $node
  619. * must be of type CONTAINER
  620. *
  621. * @return void
  622. */
  623. public function groupTree($node = null)
  624. {
  625. if (!isset($node)) {
  626. $node = $this->_tree;
  627. }
  628. $this->groupNode($node);
  629. foreach ($node->children as $child) {
  630. $this->groupTree($child);
  631. }
  632. }
  633. /**
  634. * Recursively groups tree nodes given a separator
  635. *
  636. * @param Node $node The node to group
  637. *
  638. * @return void
  639. */
  640. public function groupNode($node)
  641. {
  642. if ($node->type != Node::CONTAINER
  643. || !$GLOBALS['cfg']['NavigationTreeEnableExpansion']
  644. ) {
  645. return;
  646. }
  647. $separators = array();
  648. if (is_array($node->separator)) {
  649. $separators = $node->separator;
  650. } else {
  651. if (strlen($node->separator)) {
  652. $separators[] = $node->separator;
  653. }
  654. }
  655. $prefixes = array();
  656. if ($node->separator_depth > 0) {
  657. foreach ($node->children as $child) {
  658. $prefix_pos = false;
  659. foreach ($separators as $separator) {
  660. $sep_pos = mb_strpos($child->name, $separator);
  661. if ($sep_pos != false
  662. && $sep_pos != mb_strlen($child->name)
  663. && $sep_pos != 0
  664. && ($prefix_pos == false || $sep_pos < $prefix_pos)
  665. ) {
  666. $prefix_pos = $sep_pos;
  667. }
  668. }
  669. if ($prefix_pos !== false) {
  670. $prefix = mb_substr($child->name, 0, $prefix_pos);
  671. if (!isset($prefixes[$prefix])) {
  672. $prefixes[$prefix] = 1;
  673. } else {
  674. $prefixes[$prefix]++;
  675. }
  676. }
  677. //Bug #4375: Check if prefix is the name of a DB, to create a group.
  678. foreach ($node->children as $otherChild) {
  679. if (array_key_exists($otherChild->name, $prefixes)) {
  680. $prefixes[$otherChild->name]++;
  681. }
  682. }
  683. }
  684. //Check if prefix is the name of a DB, to create a group.
  685. foreach ($node->children as $child) {
  686. if (array_key_exists($child->name, $prefixes)) {
  687. $prefixes[$child->name]++;
  688. }
  689. }
  690. }
  691. // It is not a group if it has only one item
  692. foreach ($prefixes as $key => $value) {
  693. if ($value == 1) {
  694. unset($prefixes[$key]);
  695. }
  696. }
  697. // rfe #1634 Don't group if there's only one group and no other items
  698. if (count($prefixes) == 1) {
  699. $keys = array_keys($prefixes);
  700. $key = $keys[0];
  701. if ($prefixes[$key] == count($node->children) - 1) {
  702. unset($prefixes[$key]);
  703. }
  704. }
  705. if (count($prefixes)) {
  706. /** @var Node[] $groups */
  707. $groups = array();
  708. foreach ($prefixes as $key => $value) {
  709. // warn about large groups
  710. if ($value > 500 && !$this->_largeGroupWarning) {
  711. trigger_error(
  712. __(
  713. 'There are large item groups in navigation panel which '
  714. . 'may affect the performance. Consider disabling item '
  715. . 'grouping in the navigation panel.'
  716. ),
  717. E_USER_WARNING
  718. );
  719. $this->_largeGroupWarning = true;
  720. }
  721. $groups[$key] = new Node(
  722. htmlspecialchars((string) $key),
  723. Node::CONTAINER,
  724. true
  725. );
  726. $groups[$key]->separator = $node->separator;
  727. $groups[$key]->separator_depth = $node->separator_depth - 1;
  728. $groups[$key]->icon = Util::getImage(
  729. 'b_group'
  730. );
  731. $groups[$key]->pos2 = $node->pos2;
  732. $groups[$key]->pos3 = $node->pos3;
  733. if ($node instanceof NodeTableContainer
  734. || $node instanceof NodeViewContainer
  735. ) {
  736. $tblGroup = '&amp;tbl_group=' . urlencode($key);
  737. $groups[$key]->links = array(
  738. 'text' => $node->links['text'] . $tblGroup,
  739. 'icon' => $node->links['icon'] . $tblGroup,
  740. );
  741. }
  742. $node->addChild($groups[$key]);
  743. foreach ($separators as $separator) {
  744. $separatorLength = strlen($separator);
  745. // FIXME: this could be more efficient
  746. foreach ($node->children as $child) {
  747. $keySeparatorLength = mb_strlen($key) + $separatorLength;
  748. $name_substring = mb_substr(
  749. $child->name,
  750. 0,
  751. $keySeparatorLength
  752. );
  753. if (($name_substring != $key . $separator
  754. && $child->name != $key)
  755. || $child->type != Node::OBJECT
  756. ) {
  757. continue;
  758. }
  759. $class = get_class($child);
  760. $className = substr($class, strrpos($class, '\\') + 1);
  761. unset($class);
  762. $new_child = NodeFactory::getInstance(
  763. $className,
  764. mb_substr(
  765. $child->name,
  766. $keySeparatorLength
  767. )
  768. );
  769. if ($new_child instanceof NodeDatabase
  770. && $child->getHiddenCount() > 0
  771. ) {
  772. $new_child->setHiddenCount($child->getHiddenCount());
  773. }
  774. $new_child->real_name = $child->real_name;
  775. $new_child->icon = $child->icon;
  776. $new_child->links = $child->links;
  777. $new_child->pos2 = $child->pos2;
  778. $new_child->pos3 = $child->pos3;
  779. $groups[$key]->addChild($new_child);
  780. foreach ($child->children as $elm) {
  781. $new_child->addChild($elm);
  782. }
  783. $node->removeChild($child->name);
  784. }
  785. }
  786. }
  787. foreach ($prefixes as $key => $value) {
  788. $this->groupNode($groups[$key]);
  789. $groups[$key]->classes = "navGroup";
  790. }
  791. }
  792. }
  793. /**
  794. * Renders a state of the tree, used in light mode when
  795. * either JavaScript and/or Ajax are disabled
  796. *
  797. * @return string HTML code for the navigation tree
  798. */
  799. public function renderState()
  800. {
  801. $this->_buildPath();
  802. $retval = $this->_quickWarp();
  803. $retval .= '<div class="clearfloat"></div>';
  804. $retval .= '<ul>';
  805. $retval .= $this->_fastFilterHtml($this->_tree);
  806. if ($GLOBALS['cfg']['NavigationTreeEnableExpansion']
  807. ) {
  808. $retval .= $this->_controls();
  809. }
  810. $retval .= '</ul>';
  811. $retval .= $this->_getPageSelector($this->_tree);
  812. $this->groupTree();
  813. $retval .= "<div id='pma_navigation_tree_content'><ul>";
  814. $children = $this->_tree->children;
  815. usort(
  816. $children,
  817. array('PhpMyAdmin\\Navigation\\NavigationTree', 'sortNode')
  818. );
  819. $this->_setVisibility();
  820. for ($i = 0, $nbChildren = count($children); $i < $nbChildren; $i++) {
  821. if ($i == 0) {
  822. $retval .= $this->_renderNode($children[0], true, 'first');
  823. } else {
  824. if ($i + 1 != $nbChildren) {
  825. $retval .= $this->_renderNode($children[$i], true);
  826. } else {
  827. $retval .= $this->_renderNode($children[$i], true, 'last');
  828. }
  829. }
  830. }
  831. $retval .= "</ul></div>";
  832. return $retval;
  833. }
  834. /**
  835. * Renders a part of the tree, used for Ajax
  836. * requests in light mode
  837. *
  838. * @return string HTML code for the navigation tree
  839. */
  840. public function renderPath()
  841. {
  842. $node = $this->_buildPath();
  843. if ($node === false) {
  844. $retval = false;
  845. } else {
  846. $this->groupTree();
  847. $retval = "<div class='list_container hide'>";
  848. if (!empty($this->_searchClause) || !empty($this->_searchClause2)) {
  849. $retval .= "<ul class='search_results'>";
  850. } else {
  851. $retval .= "<ul>";
  852. }
  853. $listContent = $this->_fastFilterHtml($node);
  854. $listContent .= $this->_getPageSelector($node);
  855. $children = $node->children;
  856. usort(
  857. $children,
  858. array('PhpMyAdmin\\Navigation\\NavigationTree', 'sortNode')
  859. );
  860. for ($i = 0, $nbChildren = count($children); $i < $nbChildren; $i++) {
  861. if ($i + 1 != $nbChildren) {
  862. $listContent .= $this->_renderNode($children[$i], true);
  863. } else {
  864. $listContent .= $this->_renderNode($children[$i], true, 'last');
  865. }
  866. }
  867. $retval .= $listContent;
  868. $retval .= "</ul>";
  869. if (!$GLOBALS['cfg']['ShowDatabasesNavigationAsTree']) {
  870. $retval .= "<span class='hide loaded_db'>";
  871. $parents = $node->parents(true);
  872. $retval .= urlencode($parents[0]->real_name);
  873. $retval .= "</span>";
  874. if (empty($listContent)) {
  875. $retval .= "<div style='margin:0.75em'>";
  876. $retval .= __('No tables found in database.');
  877. $retval .= "</div>";
  878. }
  879. }
  880. $retval .= "</div>";
  881. }
  882. if (!empty($this->_searchClause) || !empty($this->_searchClause2)) {
  883. $results = 0;
  884. if (!empty($this->_searchClause2)) {
  885. if (is_object($node->realParent())) {
  886. $results = $node->realParent()
  887. ->getPresence(
  888. $node->real_name,
  889. $this->_searchClause2
  890. );
  891. }
  892. } else {
  893. $results = $this->_tree->getPresence(
  894. 'databases',
  895. $this->_searchClause
  896. );
  897. }
  898. $results = sprintf(
  899. _ngettext(
  900. '%s result found',
  901. '%s results found',
  902. $results
  903. ),
  904. $results
  905. );
  906. Response::getInstance()
  907. ->addJSON(
  908. 'results',
  909. $results
  910. );
  911. }
  912. return $retval;
  913. }
  914. /**
  915. * Renders the parameters that are required on the client
  916. * side to know which page(s) we will be requesting data from
  917. *
  918. * @param Node $node The node to create the pagination parameters for
  919. *
  920. * @return string
  921. */
  922. private function _getPaginationParamsHtml($node)
  923. {
  924. $retval = '';
  925. $paths = $node->getPaths();
  926. if (isset($paths['aPath_clean'][2])) {
  927. $retval .= "<span class='hide pos2_name'>";
  928. $retval .= $paths['aPath_clean'][2];
  929. $retval .= "</span>";
  930. $retval .= "<span class='hide pos2_value'>";
  931. $retval .= htmlspecialchars($node->pos2);
  932. $retval .= "</span>";
  933. }
  934. if (isset($paths['aPath_clean'][4])) {
  935. $retval .= "<span class='hide pos3_name'>";
  936. $retval .= $paths['aPath_clean'][4];
  937. $retval .= "</span>";
  938. $retval .= "<span class='hide pos3_value'>";
  939. $retval .= htmlspecialchars($node->pos3);
  940. $retval .= "</span>";
  941. }
  942. return $retval;
  943. }
  944. /**
  945. * Finds whether given tree matches this tree.
  946. *
  947. * @param array $tree Tree to check
  948. * @param array $paths Paths to check
  949. *
  950. * @return boolean
  951. */
  952. private function _findTreeMatch(array $tree, array $paths)
  953. {
  954. $match = false;
  955. foreach ($tree as $path) {
  956. $match = true;
  957. foreach ($paths as $key => $part) {
  958. if (!isset($path[$key]) || $part != $path[$key]) {
  959. $match = false;
  960. break;
  961. }
  962. }
  963. if ($match) {
  964. break;
  965. }
  966. }
  967. return $match;
  968. }
  969. /**
  970. * Renders a single node or a branch of the tree
  971. *
  972. * @param Node $node The node to render
  973. * @param bool $recursive Bool: Whether to render a single node or a branch
  974. * @param string $class An additional class for the list item
  975. *
  976. * @return string HTML code for the tree node or branch
  977. */
  978. private function _renderNode($node, $recursive, $class = '')
  979. {
  980. $retval = '';
  981. $paths = $node->getPaths();
  982. if ($node->hasSiblings()
  983. || $node->realParent() === false
  984. ) {
  985. $response = Response::getInstance();
  986. if ($node->type == Node::CONTAINER
  987. && count($node->children) == 0
  988. && ! $response->isAjax()
  989. ) {
  990. return '';
  991. }
  992. $retval .= '<li class="' . trim($class . ' ' . $node->classes) . '">';
  993. $sterile = array(
  994. 'events',
  995. 'triggers',
  996. 'functions',
  997. 'procedures',
  998. 'views',
  999. 'columns',
  1000. 'indexes',
  1001. );
  1002. $parentName = '';
  1003. $parents = $node->parents(false, true);
  1004. if (count($parents)) {
  1005. $parentName = $parents[0]->real_name;
  1006. }
  1007. // if node name itself is in sterile, then allow
  1008. if ($node->is_group
  1009. || (!in_array($parentName, $sterile) && !$node->isNew)
  1010. || (in_array($node->real_name, $sterile))
  1011. ) {
  1012. $retval .= "<div class='block'>";
  1013. $iClass = '';
  1014. if ($class == 'first') {
  1015. $iClass = " class='first'";
  1016. }
  1017. $retval .= "<i$iClass></i>";
  1018. if (strpos($class, 'last') === false) {
  1019. $retval .= "<b></b>";
  1020. }
  1021. $match = $this->_findTreeMatch(
  1022. $this->_vPath,
  1023. $paths['vPath_clean']
  1024. );
  1025. $retval .= '<a class="' . $node->getCssClasses($match) . '"';
  1026. $retval .= " href='#'>";
  1027. $retval .= "<span class='hide aPath'>";
  1028. $retval .= $paths['aPath'];
  1029. $retval .= "</span>";
  1030. $retval .= "<span class='hide vPath'>";
  1031. $retval .= $paths['vPath'];
  1032. $retval .= "</span>";
  1033. $retval .= "<span class='hide pos'>";
  1034. $retval .= $this->_pos;
  1035. $retval .= "</span>";
  1036. $retval .= $this->_getPaginationParamsHtml($node);
  1037. if ($GLOBALS['cfg']['ShowDatabasesNavigationAsTree']
  1038. || $parentName != 'root'
  1039. ) {
  1040. $retval .= $node->getIcon($match);
  1041. }
  1042. $retval .= "</a>";
  1043. $retval .= "</div>";
  1044. } else {
  1045. $retval .= "<div class='block'>";
  1046. $iClass = '';
  1047. if ($class == 'first') {
  1048. $iClass = " class='first'";
  1049. }
  1050. $retval .= "<i$iClass></i>";
  1051. $retval .= $this->_getPaginationParamsHtml($node);
  1052. $retval .= "</div>";
  1053. }
  1054. $linkClass = '';
  1055. $haveAjax = array(
  1056. 'functions',
  1057. 'procedures',
  1058. 'events',
  1059. 'triggers',
  1060. 'indexes',
  1061. );
  1062. $parent = $node->parents(false, true);
  1063. $isNewView = $parent[0]->real_name == 'views' && $node->isNew === true;
  1064. if ($parent[0]->type == Node::CONTAINER
  1065. && (in_array($parent[0]->real_name, $haveAjax) || $isNewView)
  1066. ) {
  1067. $linkClass = ' ajax';
  1068. }
  1069. if ($node->type == Node::CONTAINER) {
  1070. $retval .= "<i>";
  1071. }
  1072. $divClass = '';
  1073. if (isset($node->links['icon']) && !empty($node->links['icon'])) {
  1074. $iconLinks = $node->links['icon'];
  1075. $icons = $node->icon;
  1076. if (!is_array($iconLinks)) {
  1077. $iconLinks = array($iconLinks);
  1078. $icons = array($icons);
  1079. }
  1080. if (count($icons) > 1) {
  1081. $divClass = 'double';
  1082. }
  1083. }
  1084. $retval .= "<div class='block " . $divClass . "'>";
  1085. if (isset($node->links['icon']) && !empty($node->links['icon'])) {
  1086. $args = array();
  1087. foreach ($node->parents(true) as $parent) {
  1088. $args[] = urlencode($parent->real_name);
  1089. }
  1090. foreach ($icons as $key => $icon) {
  1091. $link = $this->encryptQueryParams(vsprintf($iconLinks[$key], $args));
  1092. if ($linkClass != '') {
  1093. $retval .= "<a class='$linkClass' href='$link'>";
  1094. $retval .= "{$icon}</a>";
  1095. } else {
  1096. $retval .= "<a href='$link'>{$icon}</a>";
  1097. }
  1098. }
  1099. } else {
  1100. $retval .= "<u>{$node->icon}</u>";
  1101. }
  1102. $retval .= "</div>";
  1103. if (isset($node->links['text'])) {
  1104. $args = array();
  1105. foreach ($node->parents(true) as $parent) {;
  1106. $args[] = urlencode($parent->real_name);
  1107. }
  1108. $link = $this->encryptQueryParams(vsprintf($node->links['text'], $args));
  1109. $title = isset($node->links['title']) ? $node->links['title'] : '';
  1110. if ($node->type == Node::CONTAINER) {
  1111. $retval .= "&nbsp;<a class='hover_show_full' href='$link'>";
  1112. $retval .= htmlspecialchars($node->name);
  1113. $retval .= "</a>";
  1114. } else {
  1115. $retval .= "<a class='hover_show_full$linkClass' href='$link'";
  1116. $retval .= " title='$title'>";
  1117. $retval .= htmlspecialchars($node->real_name);
  1118. $retval .= "</a>";
  1119. }
  1120. } else {
  1121. $retval .= "&nbsp;{$node->name}";
  1122. }
  1123. $retval .= $node->getHtmlForControlButtons();
  1124. if ($node->type == Node::CONTAINER) {
  1125. $retval .= "</i>";
  1126. }
  1127. $retval .= '<div class="clearfloat"></div>';
  1128. $wrap = true;
  1129. } else {
  1130. $node->visible = true;
  1131. $wrap = false;
  1132. $retval .= $this->_getPaginationParamsHtml($node);
  1133. }
  1134. if ($recursive) {
  1135. $hide = '';
  1136. if (!$node->visible) {
  1137. $hide = " style='display: none;'";
  1138. }
  1139. $children = $node->children;
  1140. usort(
  1141. $children,
  1142. array('PhpMyAdmin\\Navigation\\NavigationTree', 'sortNode')
  1143. );
  1144. $buffer = '';
  1145. $extra_class = '';
  1146. for ($i = 0, $nbChildren = count($children); $i < $nbChildren; $i++) {
  1147. if ($i + 1 == $nbChildren) {
  1148. $extra_class = ' last';
  1149. }
  1150. $buffer .= $this->_renderNode(
  1151. $children[$i],
  1152. true,
  1153. $children[$i]->classes . $extra_class
  1154. );
  1155. }
  1156. if (!empty($buffer)) {
  1157. if ($wrap) {
  1158. $retval .= "<div$hide class='list_container'><ul>";
  1159. }
  1160. $retval .= $this->_fastFilterHtml($node);
  1161. $retval .= $this->_getPageSelector($node);
  1162. $retval .= $buffer;
  1163. if ($wrap) {
  1164. $retval .= "</ul></div>";
  1165. }
  1166. }
  1167. }
  1168. if ($node->hasSiblings()) {
  1169. $retval .= "</li>";
  1170. }
  1171. return $retval;
  1172. }
  1173. /**
  1174. * Renders a database select box like the pre-4.0 navigation panel
  1175. *
  1176. * @return string HTML code
  1177. */
  1178. public function renderDbSelect()
  1179. {
  1180. $this->_buildPath();
  1181. $retval = $this->_quickWarp();
  1182. $this->_tree->is_group = false;
  1183. $retval .= '<div id="pma_navigation_select_database">';
  1184. // Provide for pagination in database select
  1185. $retval .= Util::getListNavigator(
  1186. $this->_tree->getPresence('databases', ''),
  1187. $this->_pos,
  1188. array('server' => $GLOBALS['server']),
  1189. 'navigation.php',
  1190. 'frame_navigation',
  1191. $GLOBALS['cfg']['FirstLevelNavigationItems'],
  1192. 'pos',
  1193. array('dbselector')
  1194. );
  1195. $children = $this->_tree->children;
  1196. $url_params = array(
  1197. 'server' => $GLOBALS['server'],
  1198. );
  1199. $retval .= '<div id="pma_navigation_db_select">';
  1200. $retval .= '<form action="index.php">';
  1201. $retval .= Url::getHiddenFields($url_params);
  1202. $retval .= '<select name="db" class="hide" id="navi_db_select">'
  1203. . '<option value="" dir="' . $GLOBALS['text_dir'] . '">'
  1204. . '(' . __('Databases') . ') ...</option>' . "\n";
  1205. $selected = $GLOBALS['db'];
  1206. foreach ($children as $node) {
  1207. if ($node->isNew) {
  1208. continue;
  1209. }
  1210. $paths = $node->getPaths();
  1211. if (isset($node->links['text'])) {
  1212. $title = isset($node->links['title']) ? '' : $node->links['title'];
  1213. $retval .= '<option value="'
  1214. . htmlspecialchars($node->real_name) . '"'
  1215. . ' title="' . htmlspecialchars($title) . '"'
  1216. . ' apath="' . $paths['aPath'] . '"'
  1217. . ' vpath="' . $paths['vPath'] . '"'
  1218. . ' pos="' . $this->_pos . '"';
  1219. if ($node->real_name == $selected) {
  1220. $retval .= ' selected="selected"';
  1221. }
  1222. $retval .= '>' . htmlspecialchars($node->real_name);
  1223. $retval .= '</option>';
  1224. }
  1225. }
  1226. $retval .= '</select></form>';
  1227. $retval .= '</div></div>';
  1228. $retval .= '<div id="pma_navigation_tree_content"><ul>';
  1229. $children = $this->_tree->children;
  1230. usort(
  1231. $children,
  1232. array('PhpMyAdmin\\Navigation\\NavigationTree', 'sortNode')
  1233. );
  1234. $this->_setVisibility();
  1235. for ($i = 0, $nbChildren = count($children); $i < $nbChildren; $i++) {
  1236. if ($i == 0) {
  1237. $retval .= $this->_renderNode($children[0], true, 'first');
  1238. } else {
  1239. if ($i + 1 != $nbChildren) {
  1240. $retval .= $this->_renderNode($children[$i], true);
  1241. } else {
  1242. $retval .= $this->_renderNode($children[$i], true, 'last');
  1243. }
  1244. }
  1245. }
  1246. $retval .= '</ul></div>';
  1247. return $retval;
  1248. }
  1249. /**
  1250. * Makes some nodes visible based on the which node is active
  1251. *
  1252. * @return void
  1253. */
  1254. private function _setVisibility()
  1255. {
  1256. foreach ($this->_vPath as $path) {
  1257. $node = $this->_tree;
  1258. foreach ($path as $value) {
  1259. $child = $node->getChild($value);
  1260. if ($child !== false) {
  1261. $child->visible = true;
  1262. $node = $child;
  1263. }
  1264. }
  1265. }
  1266. }
  1267. /**
  1268. * Generates the HTML code for displaying the fast filter for tables
  1269. *
  1270. * @param Node $node The node for which to generate the fast filter html
  1271. *
  1272. * @return string LI element used for the fast filter
  1273. */
  1274. private function _fastFilterHtml($node)
  1275. {
  1276. $retval = '';
  1277. $filter_db_min
  1278. = (int)$GLOBALS['cfg']['NavigationTreeDisplayDbFilterMinimum'];
  1279. $filter_item_min
  1280. = (int)$GLOBALS['cfg']['NavigationTreeDisplayItemFilterMinimum'];
  1281. if ($node === $this->_tree
  1282. && $this->_tree->getPresence() >= $filter_db_min
  1283. ) {
  1284. $url_params = array(
  1285. 'pos' => 0,
  1286. );
  1287. $retval .= '<li class="fast_filter db_fast_filter">';
  1288. $retval .= '<form class="ajax fast_filter">';
  1289. $retval .= Url::getHiddenInputs($url_params);
  1290. $retval .= '<input class="searchClause" type="text"';
  1291. $retval .= ' name="searchClause" accesskey="q"';
  1292. $retval .= " placeholder='"
  1293. . __("Type to filter these, Enter to search all");
  1294. $retval .= "' />";
  1295. $retval .= '<span title="' . __('Clear fast filter') . '">X</span>';
  1296. $retval .= "</form>";
  1297. $retval .= "</li>";
  1298. return $retval;
  1299. }
  1300. if (($node->type == Node::CONTAINER
  1301. && ($node->real_name == 'tables'
  1302. || $node->real_name == 'views'
  1303. || $node->real_name == 'functions'
  1304. || $node->real_name == 'procedures'
  1305. || $node->real_name == 'events'))
  1306. && method_exists($node->realParent(), 'getPresence')
  1307. && $node->realParent()->getPresence($node->real_name) >= $filter_item_min
  1308. ) {
  1309. $paths = $node->getPaths();
  1310. $url_params = array(
  1311. 'pos' => $this->_pos,
  1312. 'aPath' => $paths['aPath'],
  1313. 'vPath' => $paths['vPath'],
  1314. 'pos2_name' => $node->real_name,
  1315. 'pos2_value' => 0,
  1316. );
  1317. $retval .= "<li class='fast_filter'>";
  1318. $retval .= "<form class='ajax fast_filter'>";
  1319. $retval .= Url::getHiddenFields($url_params);
  1320. $retval .= "<input class='searchClause' type='text'";
  1321. $retval .= " name='searchClause2'";
  1322. $retval .= " placeholder='"
  1323. . __("Type to filter these, Enter to search all") . "' />";
  1324. $retval .= "<span title='" . __('Clear fast filter') . "'>X</span>";
  1325. $retval .= "</form>";
  1326. $retval .= "</li>";
  1327. }
  1328. return $retval;
  1329. }
  1330. /**
  1331. * Creates the code for displaying the controls
  1332. * at the top of the navigation tree
  1333. *
  1334. * @return string HTML code for the controls
  1335. */
  1336. private function _controls()
  1337. {
  1338. // always iconic
  1339. $showIcon = true;
  1340. $showText = false;
  1341. $retval = '<!-- CONTROLS START -->';
  1342. $retval .= '<li id="navigation_controls_outer">';
  1343. $retval .= '<div id="navigation_controls">';
  1344. $retval .= Util::getNavigationLink(
  1345. '#',
  1346. $showText,
  1347. __('Collapse all'),
  1348. $showIcon,
  1349. 's_collapseall',
  1350. 'pma_navigation_collapse'
  1351. );
  1352. $syncImage = 's_unlink';
  1353. $title = __('Link with main panel');
  1354. if ($GLOBALS['cfg']['NavigationLinkWithMainPanel']) {
  1355. $syncImage = 's_link';
  1356. $title = __('Unlink from main panel');
  1357. }
  1358. $retval .= Util::getNavigationLink(
  1359. '#',
  1360. $showText,
  1361. $title,
  1362. $showIcon,
  1363. $syncImage,
  1364. 'pma_navigation_sync'
  1365. );
  1366. $retval .= '</div>';
  1367. $retval .= '</li>';
  1368. $retval .= '<!-- CONTROLS ENDS -->';
  1369. return $retval;
  1370. }
  1371. /**
  1372. * Generates the HTML code for displaying the list pagination
  1373. *
  1374. * @param Node $node The node for whose children the page
  1375. * selector will be created
  1376. *
  1377. * @return string
  1378. */
  1379. private function _getPageSelector($node)
  1380. {
  1381. $retval = '';
  1382. if ($node === $this->_tree) {
  1383. $retval .= Util::getListNavigator(
  1384. $this->_tree->getPresence('databases', $this->_searchClause),
  1385. $this->_pos,
  1386. array('server' => $GLOBALS['server']),
  1387. 'navigation.php',
  1388. 'frame_navigation',
  1389. $GLOBALS['cfg']['FirstLevelNavigationItems'],
  1390. 'pos',
  1391. array('dbselector')
  1392. );
  1393. } else {
  1394. if ($node->type == Node::CONTAINER && !$node->is_group) {
  1395. $paths = $node->getPaths();
  1396. $level = isset($paths['aPath_clean'][4]) ? 3 : 2;
  1397. $_url_params = array(
  1398. 'aPath' => $paths['aPath'],
  1399. 'vPath' => $paths['vPath'],
  1400. 'pos' => $this->_pos,
  1401. 'server' => $GLOBALS['server'],
  1402. 'pos2_name' => $paths['aPath_clean'][2],
  1403. );
  1404. if ($level == 3) {
  1405. $pos = $node->pos3;
  1406. $_url_params['pos2_value'] = $node->pos2;
  1407. $_url_params['pos3_name'] = $paths['aPath_clean'][4];
  1408. } else {
  1409. $pos = $node->pos2;
  1410. }
  1411. $num = $node->realParent()
  1412. ->getPresence(
  1413. $node->real_name,
  1414. $this->_searchClause2
  1415. );
  1416. $retval .= Util::getListNavigator(
  1417. $num,
  1418. $pos,
  1419. $_url_params,
  1420. 'navigation.php',
  1421. 'frame_navigation',
  1422. $GLOBALS['cfg']['MaxNavigationItems'],
  1423. 'pos' . $level . '_value'
  1424. );
  1425. }
  1426. }
  1427. return $retval;
  1428. }
  1429. /**
  1430. * Called by usort() for sorting the nodes in a container
  1431. *
  1432. * @param Node $a The first element used in the comparison
  1433. * @param Node $b The second element used in the comparison
  1434. *
  1435. * @return int See strnatcmp() and strcmp()
  1436. */
  1437. static public function sortNode($a, $b)
  1438. {
  1439. if ($a->isNew) {
  1440. return -1;
  1441. }
  1442. if ($b->isNew) {
  1443. return 1;
  1444. }
  1445. if ($GLOBALS['cfg']['NaturalOrder']) {
  1446. return strnatcasecmp($a->name, $b->name);
  1447. }
  1448. return strcasecmp($a->name, $b->name);
  1449. }
  1450. /**
  1451. * Display quick warp links, contain Recents and Favorites
  1452. *
  1453. * @return string HTML code
  1454. */
  1455. private function _quickWarp()
  1456. {
  1457. $retval = '<div class="pma_quick_warp">';
  1458. if ($GLOBALS['cfg']['NumRecentTables'] > 0) {
  1459. $retval .= RecentFavoriteTable::getInstance('recent')
  1460. ->getHtml();
  1461. }
  1462. if ($GLOBALS['cfg']['NumFavoriteTables'] > 0) {
  1463. $retval .= RecentFavoriteTable::getInstance('favorite')
  1464. ->getHtml();
  1465. }
  1466. $retval .= '<div class="clearfloat"></div>';
  1467. $retval .= '</div>';
  1468. return $retval;
  1469. }
  1470. /**
  1471. * @param string $link
  1472. *
  1473. * @return string
  1474. */
  1475. private function encryptQueryParams($link)
  1476. {
  1477. global $PMA_Config;
  1478. if (! $PMA_Config->get('URLQueryEncryption')) {
  1479. return $link;
  1480. }
  1481. $url = parse_url($link);
  1482. parse_str(htmlspecialchars_decode($url['query']), $query);
  1483. return $url['path'] . '?' . htmlspecialchars(Url::buildHttpQuery($query));
  1484. }
  1485. }