/phpMyAdmin/libraries/pmd_common.php

https://bitbucket.org/subhan_12/mwi-panel · PHP · 262 lines · 188 code · 24 blank · 50 comment · 16 complexity · 1b38549af3140ee37a3e533da2868b32 MD5 · raw file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * @package PhpMyAdmin-Designer
  5. */
  6. /**
  7. *
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. $GLOBALS['PMD']['STYLE'] = 'default';
  13. $cfgRelation = PMA_getRelationsParam();
  14. /**
  15. * retrieves table info and stores it in $GLOBALS['PMD']
  16. *
  17. * @return array with table info
  18. */
  19. function get_tables_info()
  20. {
  21. $retval = array();
  22. $GLOBALS['PMD']['TABLE_NAME'] = array();// that foreach no error
  23. $GLOBALS['PMD']['OWNER'] = array();
  24. $GLOBALS['PMD']['TABLE_NAME_SMALL'] = array();
  25. $tables = PMA_DBI_get_tables_full($GLOBALS['db']);
  26. // seems to be needed later
  27. PMA_DBI_select_db($GLOBALS['db']);
  28. $i = 0;
  29. foreach ($tables as $one_table) {
  30. $GLOBALS['PMD']['TABLE_NAME'][$i]
  31. = $GLOBALS['db'] . "." . $one_table['TABLE_NAME'];
  32. $GLOBALS['PMD']['OWNER'][$i] = $GLOBALS['db'];
  33. $GLOBALS['PMD']['TABLE_NAME_SMALL'][$i] = $one_table['TABLE_NAME'];
  34. $GLOBALS['PMD_URL']['TABLE_NAME'][$i]
  35. = urlencode($GLOBALS['db'] . "." . $one_table['TABLE_NAME']);
  36. $GLOBALS['PMD_URL']['OWNER'][$i] = urlencode($GLOBALS['db']);
  37. $GLOBALS['PMD_URL']['TABLE_NAME_SMALL'][$i]
  38. = urlencode($one_table['TABLE_NAME']);
  39. $GLOBALS['PMD_OUT']['TABLE_NAME'][$i] = htmlspecialchars(
  40. $GLOBALS['db'] . "." . $one_table['TABLE_NAME'], ENT_QUOTES
  41. );
  42. $GLOBALS['PMD_OUT']['OWNER'][$i] = htmlspecialchars(
  43. $GLOBALS['db'], ENT_QUOTES
  44. );
  45. $GLOBALS['PMD_OUT']['TABLE_NAME_SMALL'][$i] = htmlspecialchars(
  46. $one_table['TABLE_NAME'], ENT_QUOTES
  47. );
  48. $GLOBALS['PMD']['TABLE_TYPE'][$i] = strtoupper($one_table['ENGINE']);
  49. $DF = PMA_getDisplayField($GLOBALS['db'], $one_table['TABLE_NAME']);
  50. if ($DF != '') {
  51. $retval[$GLOBALS['PMD_URL']["TABLE_NAME_SMALL"][$i]] = urlencode($DF);
  52. }
  53. $i++;
  54. }
  55. return $retval;
  56. }
  57. /**
  58. * retrieves table column info
  59. *
  60. * @return array table column nfo
  61. */
  62. function get_columns_info()
  63. {
  64. PMA_DBI_select_db($GLOBALS['db']);
  65. $tab_column = array();
  66. for ($i = 0, $cnt = count($GLOBALS['PMD']["TABLE_NAME"]); $i < $cnt; $i++) {
  67. $fields_rs = PMA_DBI_query(
  68. PMA_DBI_get_columns_sql(
  69. $GLOBALS['db'],
  70. $GLOBALS['PMD']["TABLE_NAME_SMALL"][$i],
  71. null,
  72. true
  73. ),
  74. null,
  75. PMA_DBI_QUERY_STORE
  76. );
  77. $tbl_name_i = $GLOBALS['PMD']['TABLE_NAME'][$i];
  78. $j = 0;
  79. while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
  80. $tab_column[$tbl_name_i]['COLUMN_ID'][$j] = $j;
  81. $tab_column[$tbl_name_i]['COLUMN_NAME'][$j] = $row['Field'];
  82. $tab_column[$tbl_name_i]['TYPE'][$j] = $row['Type'];
  83. $tab_column[$tbl_name_i]['NULLABLE'][$j] = $row['Null'];
  84. $j++;
  85. }
  86. }
  87. return $tab_column;
  88. }
  89. /**
  90. * returns JavaScript code for intializing vars
  91. *
  92. * @return string JavaScript code
  93. */
  94. function get_script_contr()
  95. {
  96. PMA_DBI_select_db($GLOBALS['db']);
  97. $con["C_NAME"] = array();
  98. $i = 0;
  99. $alltab_rs = PMA_DBI_query(
  100. 'SHOW TABLES FROM ' . PMA_Util::backquote($GLOBALS['db']),
  101. null,
  102. PMA_DBI_QUERY_STORE
  103. );
  104. while ($val = @PMA_DBI_fetch_row($alltab_rs)) {
  105. $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'internal');
  106. //echo "<br> internal ".$GLOBALS['db']." - ".$val[0]." - ";
  107. //print_r($row);
  108. if ($row !== false) {
  109. foreach ($row as $field => $value) {
  110. $con['C_NAME'][$i] = '';
  111. $con['DTN'][$i] = urlencode($GLOBALS['db'] . "." . $val[0]);
  112. $con['DCN'][$i] = urlencode($field);
  113. $con['STN'][$i] = urlencode(
  114. $value['foreign_db'] . "." . $value['foreign_table']
  115. );
  116. $con['SCN'][$i] = urlencode($value['foreign_field']);
  117. $i++;
  118. }
  119. }
  120. $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'foreign');
  121. //echo "<br> INNO ";
  122. //print_r($row);
  123. if ($row !== false) {
  124. foreach ($row as $field => $value) {
  125. $con['C_NAME'][$i] = '';
  126. $con['DTN'][$i] = urlencode($GLOBALS['db'].".".$val[0]);
  127. $con['DCN'][$i] = urlencode($field);
  128. $con['STN'][$i] = urlencode(
  129. $value['foreign_db'].".".$value['foreign_table']
  130. );
  131. $con['SCN'][$i] = urlencode($value['foreign_field']);
  132. $i++;
  133. }
  134. }
  135. }
  136. $ti = 0;
  137. $retval = array();
  138. for ($i = 0, $cnt = count($con["C_NAME"]); $i < $cnt; $i++) {
  139. $c_name_i = $con['C_NAME'][$i];
  140. $dtn_i = $con['DTN'][$i];
  141. $retval[$ti] = array();
  142. $retval[$ti][$c_name_i] = array();
  143. if (in_array($dtn_i, $GLOBALS['PMD_URL']["TABLE_NAME"])
  144. && in_array($con['STN'][$i], $GLOBALS['PMD_URL']["TABLE_NAME"])
  145. ) {
  146. $retval[$ti][$c_name_i][$dtn_i] = array();
  147. $retval[$ti][$c_name_i][$dtn_i][$con['DCN'][$i]] = array(
  148. 0 => $con['STN'][$i],
  149. 1 => $con['SCN'][$i]
  150. );
  151. }
  152. $ti++;
  153. }
  154. return $retval;
  155. }
  156. /**
  157. * Returns UNIQUE and PRIMARY indices
  158. *
  159. * @return array unique or primary indices
  160. */
  161. function get_pk_or_unique_keys()
  162. {
  163. return get_all_keys(true);
  164. }
  165. /**
  166. * returns all indices
  167. *
  168. * @param bool $unique_only whether to include only unique ones
  169. *
  170. * @return array indices
  171. */
  172. function get_all_keys($unique_only = false)
  173. {
  174. include_once './libraries/Index.class.php';
  175. $keys = array();
  176. foreach ($GLOBALS['PMD']['TABLE_NAME_SMALL'] as $I => $table) {
  177. $schema = $GLOBALS['PMD']['OWNER'][$I];
  178. // for now, take into account only the first index segment
  179. foreach (PMA_Index::getFromTable($table, $schema) as $index) {
  180. if ($unique_only && ! $index->isUnique()) {
  181. continue;
  182. }
  183. $columns = $index->getColumns();
  184. foreach ($columns as $column_name => $dummy) {
  185. $keys[$schema . '.' .$table . '.' . $column_name] = 1;
  186. }
  187. }
  188. }
  189. return $keys;
  190. }
  191. /**
  192. * Return script to create j_tab and h_tab arrays
  193. *
  194. * @return string
  195. */
  196. function get_script_tabs()
  197. {
  198. $script_tabs = 'var j_tabs = new Array();' . "\n"
  199. . 'var h_tabs = new Array();' . "\n" ;
  200. $retval = array(
  201. 'j_tabs' => array(),
  202. 'h_tabs' => array()
  203. );
  204. for ($i = 0, $cnt = count($GLOBALS['PMD']['TABLE_NAME']); $i < $cnt; $i++) {
  205. $j = 0;
  206. if (PMA_Util::isForeignKeySupported($GLOBALS['PMD']['TABLE_TYPE'][$i])) {
  207. $j = 1;
  208. }
  209. $retval['j_tabs'][$GLOBALS['PMD_URL']['TABLE_NAME'][$i]] = $j;
  210. $retval['h_tabs'][$GLOBALS['PMD_URL']['TABLE_NAME'][$i]] = 1;
  211. }
  212. return $retval;
  213. }
  214. /**
  215. * Returns table position
  216. *
  217. * @return array table positions and sizes
  218. */
  219. function get_tab_pos()
  220. {
  221. $cfgRelation = PMA_getRelationsParam();
  222. if (! $cfgRelation['designerwork']) {
  223. return null;
  224. }
  225. $query = "
  226. SELECT CONCAT_WS('.', `db_name`, `table_name`) AS `name`,
  227. `x` AS `X`,
  228. `y` AS `Y`,
  229. `v` AS `V`,
  230. `h` AS `H`
  231. FROM " . PMA_Util::backquote($cfgRelation['db'])
  232. . "." . PMA_Util::backquote($cfgRelation['designer_coords']);
  233. $tab_pos = PMA_DBI_fetch_result(
  234. $query, 'name', null, $GLOBALS['controllink'], PMA_DBI_QUERY_STORE
  235. );
  236. return count($tab_pos) ? $tab_pos : null;
  237. }
  238. ?>