pmd_common.php 7.7 KB

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