This is a number.
[phpmyadmin/crack.git] / pmd_common.php
blobd52ee67ca3771eb394b836d15d061b78c4375dda
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @author Ivan A Kirillov (Ivan.A.Kirillov@gmail.com)
5 * @version $Id$
6 * @package phpMyAdmin-Designer
7 */
9 /**
12 require_once './libraries/common.inc.php';
13 // not understand
14 require_once './libraries/header_http.inc.php';
16 $GLOBALS['PMD']['STYLE'] = 'default';
18 require_once './libraries/relation.lib.php';
19 $cfgRelation = PMA_getRelationsParam();
21 $GLOBALS['script_display_field'] =
22 '<script type="text/javascript">' . "\n" .
23 '// <![CDATA[' . "\n" .
24 'var display_field = new Array();' . "\n";
26 /**
27 * retrieves table info and stores it in $GLOBALS['PMD']
29 * @uses $GLOBALS['script_display_field']
30 * @uses $GLOBALS['PMD'] to fill it
31 * @uses $GLOBALS['db']
32 * @uses PMA_DBI_get_tables_full()
33 * @uses PMA_DBI_select_db()
34 * @uses PMA_getDisplayField()
35 * @uses urlencode()
36 * @uses htmlspecialchars()
37 * @uses strtoupper()
38 * @uses urlencode()
40 function get_tabs()
42 $GLOBALS['PMD']['TABLE_NAME'] = array();// that foreach no error
43 $GLOBALS['PMD']['OWNER'] = array();
44 $GLOBALS['PMD']['TABLE_NAME_SMALL'] = array();
46 $tables = PMA_DBI_get_tables_full($GLOBALS['db']);
47 // seems to be needed later
48 PMA_DBI_select_db($GLOBALS['db']);
49 $i = 0;
50 foreach ($tables as $one_table) {
51 $GLOBALS['PMD']['TABLE_NAME'][$i] = $GLOBALS['db'] . "." . $one_table['TABLE_NAME'];
52 $GLOBALS['PMD']['OWNER'][$i] = $GLOBALS['db'];
53 $GLOBALS['PMD']['TABLE_NAME_SMALL'][$i] = $one_table['TABLE_NAME'];
55 $GLOBALS['PMD_URL']['TABLE_NAME'][$i] = urlencode($GLOBALS['db'] . "." . $one_table['TABLE_NAME']);
56 $GLOBALS['PMD_URL']['OWNER'][$i] = urlencode($GLOBALS['db']);
57 $GLOBALS['PMD_URL']['TABLE_NAME_SMALL'][$i] = urlencode($one_table['TABLE_NAME']);
59 $GLOBALS['PMD_OUT']['TABLE_NAME'][$i] = htmlspecialchars($GLOBALS['db'] . "." . $one_table['TABLE_NAME'], ENT_QUOTES);
60 $GLOBALS['PMD_OUT']['OWNER'][$i] = htmlspecialchars($GLOBALS['db'], ENT_QUOTES);
61 $GLOBALS['PMD_OUT']['TABLE_NAME_SMALL'][$i] = htmlspecialchars($one_table['TABLE_NAME'], ENT_QUOTES);
63 $GLOBALS['PMD']['TABLE_TYPE'][$i] = strtoupper($one_table['ENGINE']);
65 $DF = PMA_getDisplayField($GLOBALS['db'], $one_table['TABLE_NAME']);
66 if ($DF != '') {
67 $GLOBALS['script_display_field'] .= " display_field['"
68 . $GLOBALS['PMD_URL']["TABLE_NAME_SMALL"][$i] . "'] = '"
69 . urlencode($DF) . "';\n";
72 $i++;
75 $GLOBALS['script_display_field'] .=
76 '// ]]>' . "\n" .
77 '</script>' . "\n";
78 // return $GLOBALS['PMD']; // many bases // not use ??????
81 /**
82 * retrieves table column info
84 * @uses $GLOBALS['db']
85 * @uses PMA_DBI_QUERY_STORE
86 * @uses PMA_DBI_select_db()
87 * @uses PMA_DBI_query()
88 * @uses PMA_DBI_num_rows()
89 * @uses PMA_backquote()
90 * @uses count()
91 * @return array table column nfo
93 function get_tab_info()
95 PMA_DBI_select_db($GLOBALS['db']);
96 $tab_column = array();
97 for ($i = 0; $i < count($GLOBALS['PMD']["TABLE_NAME"]); $i++) {
98 $fields_rs = PMA_DBI_query('SHOW FULL FIELDS FROM '.PMA_backquote($GLOBALS['PMD']["TABLE_NAME_SMALL"][$i]), NULL, PMA_DBI_QUERY_STORE);
99 $j = 0;
100 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
101 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['COLUMN_ID'][$j] = $j;
102 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['COLUMN_NAME'][$j] = $row['Field'];
103 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['TYPE'][$j] = $row['Type'];
104 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['NULLABLE'][$j] = $row['Null'];
105 $j++;
108 return $tab_column;
112 * returns JavaScript code for intializing vars
114 * @uses $GLOBALS['db']
115 * @uses PMA_DBI_QUERY_STORE
116 * @uses PMA_DBI_select_db()
117 * @uses PMA_DBI_query()
118 * @uses PMA_backquote()
119 * @uses PMA_DBI_fetch_row()
120 * @uses PMA_getForeigners()
121 * @uses urlencode()
122 * @uses count()
123 * @uses in_array()
124 * @return string JavaScript code
126 function get_script_contr()
128 PMA_DBI_select_db($GLOBALS['db']);
129 $con["C_NAME"] = array();
130 $i = 0;
131 $alltab_rs = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($GLOBALS['db']), NULL, PMA_DBI_QUERY_STORE);
132 while ($val = @PMA_DBI_fetch_row($alltab_rs)) {
133 $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'internal');
134 //echo "<br> internal ".$GLOBALS['db']." - ".$val[0]." - ";
135 //print_r($row);
136 if ($row !== false) {
137 foreach ($row as $field => $value) {
138 $con['C_NAME'][$i] = '';
139 $con['DTN'][$i] = urlencode($GLOBALS['db'] . "." . $val[0]);
140 $con['DCN'][$i] = urlencode($field);
141 $con['STN'][$i] = urlencode($value['foreign_db'] . "." . $value['foreign_table']);
142 $con['SCN'][$i] = urlencode($value['foreign_field']);
143 $i++;
146 $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'foreign');
147 //echo "<br> INNO ";
148 //print_r($row);
149 if ($row !== false) {
150 foreach ($row as $field => $value) {
151 $con['C_NAME'][$i] = '';
152 $con['DTN'][$i] = urlencode($GLOBALS['db'].".".$val[0]);
153 $con['DCN'][$i] = urlencode($field);
154 $con['STN'][$i] = urlencode($value['foreign_db'].".".$value['foreign_table']);
155 $con['SCN'][$i] = urlencode($value['foreign_field']);
156 $i++;
161 $ti = 0;
162 $script_contr =
163 '<script type="text/javascript">' . "\n" .
164 '// <![CDATA[' . "\n" .
165 'var contr = new Array();' . "\n";
166 for ($i = 0; $i < count($con["C_NAME"]); $i++) {
167 $js_var = ' contr[' . $ti . ']';
168 $script_contr .= $js_var . " = new Array();\n";
169 $js_var .= "['" . $con['C_NAME'][$i] . "']";
170 $script_contr .= $js_var . " = new Array();\n";
171 if (in_array($con['DTN'][$i], $GLOBALS['PMD_URL']["TABLE_NAME"])
172 && in_array($con['STN'][$i], $GLOBALS['PMD_URL']["TABLE_NAME"])) {
173 $js_var .= "['" . $con['DTN'][$i] . "']";
174 $script_contr .= $js_var . " = new Array();\n";
175 $m_col = array();//}
176 $js_var .= "['" . $con['DCN'][$i] . "']";
177 $script_contr .= $js_var . " = new Array();\n";//}
178 $script_contr .= $js_var . "[0] = '" . $con['STN'][$i] . "';\n"; //
179 $script_contr .= $js_var . "[1] = '" . $con['SCN'][$i] . "';\n"; //
181 $ti++;
183 $script_contr .=
184 '// ]]>' . "\n" .
185 '</script>' . "\n";
186 return $script_contr;
190 * @uses get_all_keys()
191 * @return array unique or primary indizes
193 function get_pk_or_unique_keys()
195 return get_all_keys(true);
199 * returns all indizes
201 * @uses $GLOBALS['PMD']
202 * @uses PMA_Index::getFromTable()
203 * @uses PMA_Index->isUnique()
204 * @uses PMA_Index->getColumns()
205 * @param boolean whether to include ony unique ones
206 * @return array indizes
208 function get_all_keys($unique_only = false)
210 require_once './libraries/Index.class.php';
212 $keys = array();
214 foreach ($GLOBALS['PMD']['TABLE_NAME_SMALL'] as $I => $table) {
215 $schema = $GLOBALS['PMD']['OWNER'][$I];
216 // for now, take into account only the first index segment
217 foreach (PMA_Index::getFromTable($table, $schema) as $index) {
218 if ($unique_only && ! $index->isUnique()) {
219 continue;
221 $column = key($index->getColumns());
222 $keys[$schema . '.' .$table . '.' . $column] = 1;
226 return $keys;
232 * @uses $GLOBALS['PMD']
233 * @uses count()
234 * @uses in_array()
235 * @return array ???
237 function get_script_tabs()
239 $script_tabs =
240 '<script type="text/javascript">' . "\n" .
241 '// <![CDATA[' . "\n" .
242 'var j_tabs = new Array();' . "\n";
243 for ($i = 0; $i < count($GLOBALS['PMD']['TABLE_NAME']); $i++) {
244 $script_tabs .= "j_tabs['" . $GLOBALS['PMD_URL']['TABLE_NAME'][$i] . "'] = '"
245 . (PMA_foreignkey_supported($GLOBALS['PMD']['TABLE_TYPE'][$i]) ? '1' : '0') . "';\n";
247 $script_tabs .=
248 '// ]]>' . "\n" .
249 '</script>' . "\n";
250 return $script_tabs;
254 * @uses $GLOBALS['controllink']
255 * @uses $cfgRelation['designerwork']
256 * @uses $cfgRelation['db']
257 * @uses $cfgRelation['designer_coords']
258 * @uses PMA_DBI_QUERY_STORE
259 * @uses PMA_getRelationsParam()
260 * @uses PMA_backquote()
261 * @uses PMA_DBI_fetch_result()
262 * @uses count()
263 * @return array table positions and sizes
265 function get_tab_pos()
267 $cfgRelation = PMA_getRelationsParam();
269 if (! $cfgRelation['designerwork']) {
270 return null;
273 $query = "
274 SELECT CONCAT_WS('.', `db_name`, `table_name`) AS `name`,
275 `x` AS `X`,
276 `y` AS `Y`,
277 `v` AS `V`,
278 `h` AS `H`
279 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['designer_coords']);
280 $tab_pos = PMA_DBI_fetch_result($query, 'name', null, $GLOBALS['controllink'], PMA_DBI_QUERY_STORE);
281 return count($tab_pos) ? $tab_pos : null;
285 * returns distinct values from $GLOBALS['PMD']['OWNER']
287 * @uses array_values()
288 * @uses array_unique()
289 * @uses $GLOBALS['PMD']['OWNER']
290 * @return array owner
292 function get_owners()
294 return array_values(array_unique($GLOBALS['PMD']['OWNER']));
297 get_tabs();