NodeTable.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * Functionality for the navigation tree
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Navigation\Nodes;
  7. use PhpMyAdmin\Html\Generator;
  8. use PhpMyAdmin\Url;
  9. use PhpMyAdmin\Util;
  10. use function in_array;
  11. use function intval;
  12. use function strpos;
  13. /**
  14. * Represents a columns node in the navigation tree
  15. */
  16. class NodeTable extends NodeDatabaseChild
  17. {
  18. /** @var array IMG tags, used when rendering the node */
  19. public $icon;
  20. /**
  21. * Initialises the class
  22. *
  23. * @param string $name An identifier for the new node
  24. * @param int $type Type of node, may be one of CONTAINER or OBJECT
  25. * @param bool $isGroup Whether this object has been created
  26. * while grouping nodes
  27. */
  28. public function __construct($name, $type = Node::OBJECT, $isGroup = false)
  29. {
  30. parent::__construct($name, $type, $isGroup);
  31. $this->icon = [];
  32. $this->addIcon(
  33. Util::getScriptNameForOption(
  34. $GLOBALS['cfg']['NavigationTreeDefaultTabTable'],
  35. 'table'
  36. )
  37. );
  38. $this->addIcon(
  39. Util::getScriptNameForOption(
  40. $GLOBALS['cfg']['NavigationTreeDefaultTabTable2'],
  41. 'table'
  42. )
  43. );
  44. $title = (string) Util::getTitleForTarget(
  45. $GLOBALS['cfg']['DefaultTabTable']
  46. );
  47. $this->title = $title;
  48. $scriptName = Util::getScriptNameForOption(
  49. $GLOBALS['cfg']['DefaultTabTable'],
  50. 'table'
  51. );
  52. $firstIconLink = Util::getScriptNameForOption(
  53. $GLOBALS['cfg']['NavigationTreeDefaultTabTable'],
  54. 'table'
  55. );
  56. $secondIconLink = Util::getScriptNameForOption(
  57. $GLOBALS['cfg']['NavigationTreeDefaultTabTable2'],
  58. 'table'
  59. );
  60. $this->links = [
  61. 'text' => $scriptName . (strpos($scriptName, '?') === false ? '?' : '&')
  62. . 'server=' . $GLOBALS['server']
  63. . '&amp;db=%2$s&amp;table=%1$s'
  64. . '&amp;pos=0',
  65. 'icon' => [
  66. $firstIconLink . (strpos($firstIconLink, '?') === false ? '?' : '&')
  67. . 'server=' . $GLOBALS['server']
  68. . '&amp;db=%2$s&amp;table=%1$s',
  69. $secondIconLink . (strpos($secondIconLink, '?') === false ? '?' : '&')
  70. . 'server=' . $GLOBALS['server']
  71. . '&amp;db=%2$s&amp;table=%1$s',
  72. ],
  73. 'title' => $this->title,
  74. ];
  75. $this->classes = 'table';
  76. }
  77. /**
  78. * Returns the number of children of type $type present inside this container
  79. * This method is overridden by the PhpMyAdmin\Navigation\Nodes\NodeDatabase
  80. * and PhpMyAdmin\Navigation\Nodes\NodeTable classes
  81. *
  82. * @param string $type The type of item we are looking for
  83. * ('columns' or 'indexes')
  84. * @param string $searchClause A string used to filter the results of the query
  85. *
  86. * @return int
  87. */
  88. public function getPresence($type = '', $searchClause = '')
  89. {
  90. global $dbi;
  91. $retval = 0;
  92. $db = $this->realParent()->realName;
  93. $table = $this->realName;
  94. switch ($type) {
  95. case 'columns':
  96. if (! $GLOBALS['cfg']['Server']['DisableIS']) {
  97. $db = $dbi->escapeString($db);
  98. $table = $dbi->escapeString($table);
  99. $query = 'SELECT COUNT(*) ';
  100. $query .= 'FROM `INFORMATION_SCHEMA`.`COLUMNS` ';
  101. $query .= "WHERE `TABLE_NAME`='" . $table . "' ";
  102. $query .= "AND `TABLE_SCHEMA`='" . $db . "'";
  103. $retval = (int) $dbi->fetchValue($query);
  104. } else {
  105. $db = Util::backquote($db);
  106. $table = Util::backquote($table);
  107. $query = 'SHOW COLUMNS FROM ' . $table . ' FROM ' . $db . '';
  108. $retval = (int) $dbi->numRows(
  109. $dbi->tryQuery($query)
  110. );
  111. }
  112. break;
  113. case 'indexes':
  114. $db = Util::backquote($db);
  115. $table = Util::backquote($table);
  116. $query = 'SHOW INDEXES FROM ' . $table . ' FROM ' . $db;
  117. $retval = (int) $dbi->numRows(
  118. $dbi->tryQuery($query)
  119. );
  120. break;
  121. case 'triggers':
  122. if (! $GLOBALS['cfg']['Server']['DisableIS']) {
  123. $db = $dbi->escapeString($db);
  124. $table = $dbi->escapeString($table);
  125. $query = 'SELECT COUNT(*) ';
  126. $query .= 'FROM `INFORMATION_SCHEMA`.`TRIGGERS` ';
  127. $query .= 'WHERE `EVENT_OBJECT_SCHEMA` '
  128. . Util::getCollateForIS() . "='" . $db . "' ";
  129. $query .= 'AND `EVENT_OBJECT_TABLE` '
  130. . Util::getCollateForIS() . "='" . $table . "'";
  131. $retval = (int) $dbi->fetchValue($query);
  132. } else {
  133. $db = Util::backquote($db);
  134. $table = $dbi->escapeString($table);
  135. $query = 'SHOW TRIGGERS FROM ' . $db . " WHERE `Table` = '" . $table . "'";
  136. $retval = (int) $dbi->numRows(
  137. $dbi->tryQuery($query)
  138. );
  139. }
  140. break;
  141. default:
  142. break;
  143. }
  144. return $retval;
  145. }
  146. /**
  147. * Returns the names of children of type $type present inside this container
  148. * This method is overridden by the PhpMyAdmin\Navigation\Nodes\NodeDatabase
  149. * and PhpMyAdmin\Navigation\Nodes\NodeTable classes
  150. *
  151. * @param string $type The type of item we are looking for
  152. * ('tables', 'views', etc)
  153. * @param int $pos The offset of the list within the results
  154. * @param string $searchClause A string used to filter the results of the query
  155. *
  156. * @return array
  157. */
  158. public function getData($type, $pos, $searchClause = '')
  159. {
  160. global $dbi;
  161. $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
  162. $retval = [];
  163. $db = $this->realParent()->realName;
  164. $table = $this->realName;
  165. switch ($type) {
  166. case 'columns':
  167. if (! $GLOBALS['cfg']['Server']['DisableIS']) {
  168. $db = $dbi->escapeString($db);
  169. $table = $dbi->escapeString($table);
  170. $query = 'SELECT `COLUMN_NAME` AS `name` ';
  171. $query .= ',`COLUMN_KEY` AS `key` ';
  172. $query .= ',`DATA_TYPE` AS `type` ';
  173. $query .= ',`COLUMN_DEFAULT` AS `default` ';
  174. $query .= ",IF (`IS_NULLABLE` = 'NO', '', 'nullable') AS `nullable` ";
  175. $query .= 'FROM `INFORMATION_SCHEMA`.`COLUMNS` ';
  176. $query .= "WHERE `TABLE_NAME`='" . $table . "' ";
  177. $query .= "AND `TABLE_SCHEMA`='" . $db . "' ";
  178. $query .= 'ORDER BY `COLUMN_NAME` ASC ';
  179. $query .= 'LIMIT ' . intval($pos) . ', ' . $maxItems;
  180. $retval = $dbi->fetchResult($query);
  181. break;
  182. }
  183. $db = Util::backquote($db);
  184. $table = Util::backquote($table);
  185. $query = 'SHOW COLUMNS FROM ' . $table . ' FROM ' . $db;
  186. $handle = $dbi->tryQuery($query);
  187. if ($handle === false) {
  188. break;
  189. }
  190. $count = 0;
  191. if ($dbi->dataSeek($handle, $pos)) {
  192. while ($arr = $dbi->fetchArray($handle)) {
  193. if ($count >= $maxItems) {
  194. break;
  195. }
  196. $retval[] = [
  197. 'name' => $arr['Field'],
  198. 'key' => $arr['Key'],
  199. 'type' => Util::extractColumnSpec($arr['Type'])['type'],
  200. 'default' => $arr['Default'],
  201. 'nullable' => ($arr['Null'] === 'NO' ? '' : 'nullable'),
  202. ];
  203. $count++;
  204. }
  205. }
  206. break;
  207. case 'indexes':
  208. $db = Util::backquote($db);
  209. $table = Util::backquote($table);
  210. $query = 'SHOW INDEXES FROM ' . $table . ' FROM ' . $db;
  211. $handle = $dbi->tryQuery($query);
  212. if ($handle === false) {
  213. break;
  214. }
  215. $count = 0;
  216. while ($arr = $dbi->fetchArray($handle)) {
  217. if (in_array($arr['Key_name'], $retval)) {
  218. continue;
  219. }
  220. if ($pos <= 0 && $count < $maxItems) {
  221. $retval[] = $arr['Key_name'];
  222. $count++;
  223. }
  224. $pos--;
  225. }
  226. break;
  227. case 'triggers':
  228. if (! $GLOBALS['cfg']['Server']['DisableIS']) {
  229. $db = $dbi->escapeString($db);
  230. $table = $dbi->escapeString($table);
  231. $query = 'SELECT `TRIGGER_NAME` AS `name` ';
  232. $query .= 'FROM `INFORMATION_SCHEMA`.`TRIGGERS` ';
  233. $query .= 'WHERE `EVENT_OBJECT_SCHEMA` '
  234. . Util::getCollateForIS() . "='" . $db . "' ";
  235. $query .= 'AND `EVENT_OBJECT_TABLE` '
  236. . Util::getCollateForIS() . "='" . $table . "' ";
  237. $query .= 'ORDER BY `TRIGGER_NAME` ASC ';
  238. $query .= 'LIMIT ' . intval($pos) . ', ' . $maxItems;
  239. $retval = $dbi->fetchResult($query);
  240. break;
  241. }
  242. $db = Util::backquote($db);
  243. $table = $dbi->escapeString($table);
  244. $query = 'SHOW TRIGGERS FROM ' . $db . " WHERE `Table` = '" . $table . "'";
  245. $handle = $dbi->tryQuery($query);
  246. if ($handle === false) {
  247. break;
  248. }
  249. $count = 0;
  250. if ($dbi->dataSeek($handle, $pos)) {
  251. while ($arr = $dbi->fetchArray($handle)) {
  252. if ($count >= $maxItems) {
  253. break;
  254. }
  255. $retval[] = $arr['Trigger'];
  256. $count++;
  257. }
  258. }
  259. break;
  260. default:
  261. break;
  262. }
  263. return $retval;
  264. }
  265. /**
  266. * Returns the type of the item represented by the node.
  267. *
  268. * @return string type of the item
  269. */
  270. protected function getItemType()
  271. {
  272. return 'table';
  273. }
  274. /**
  275. * Add an icon to navigation tree
  276. *
  277. * @param string $page Page name to redirect
  278. *
  279. * @return void
  280. */
  281. private function addIcon($page)
  282. {
  283. if (empty($page)) {
  284. return;
  285. }
  286. switch ($page) {
  287. case Url::getFromRoute('/table/structure'):
  288. $this->icon[] = Generator::getImage('b_props', __('Structure'));
  289. break;
  290. case Url::getFromRoute('/table/search'):
  291. $this->icon[] = Generator::getImage('b_search', __('Search'));
  292. break;
  293. case Url::getFromRoute('/table/change'):
  294. $this->icon[] = Generator::getImage('b_insrow', __('Insert'));
  295. break;
  296. case Url::getFromRoute('/table/sql'):
  297. $this->icon[] = Generator::getImage('b_sql', __('SQL'));
  298. break;
  299. case Url::getFromRoute('/sql'):
  300. $this->icon[] = Generator::getImage('b_browse', __('Browse'));
  301. break;
  302. }
  303. }
  304. }