improve messages
[phpmyadmin/crack.git] / libraries / relation.lib.php3
blobd49d2e13ec47228c39292776ea4eefb75ebff1c3
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Set of functions used with the relation and pdf feature
7 */
10 if (!defined('PMA_RELATION_LIB_INCLUDED')){
11 define('PMA_RELATION_LIB_INCLUDED', 1);
13 /**
14 * Executes a query as controluser if possible, otherwise as normal user
16 * @param string the query to execute
17 * @param boolean whether to display SQL error messages or not
19 * @return integer the result id
21 * @global string the URL of the page to show in case of error
22 * @global string the name of db to come back to
23 * @global integer the ressource id of DB connect as controluser
24 * @global array configuration infos about the relations stuff
26 * @access public
28 * @author Mike Beck <mikebeck@users.sourceforge.net>
30 function PMA_query_as_cu($sql, $show_error = TRUE) {
31 global $err_url_0, $db, $dbh, $cfgRelation;
33 if (isset($dbh)) {
34 PMA_mysql_select_db($cfgRelation['db'], $dbh);
35 $result = @PMA_mysql_query($sql, $dbh);
36 if (!$result && $show_error == TRUE) {
37 PMA_mysqlDie(mysql_error($dbh), $sql, '', $err_url_0);
39 PMA_mysql_select_db($db, $dbh);
40 } else {
41 PMA_mysql_select_db($cfgRelation['db']);
42 $result = @PMA_mysql_query($sql);
43 if ($result && $show_error == TRUE) {
44 PMA_mysqlDie('', $sql, '', $err_url_0);
46 PMA_mysql_select_db($db);
47 } // end if... else...
49 if ($result) {
50 return $result;
51 } else {
52 return FALSE;
54 } // end of the "PMA_query_as_cu()" function
57 /**
58 * Defines the relation parameters for the current user
59 * just a copy of the functions used for relations ;-)
60 * but added some stuff to check what will work
62 * @param boolean whether to check validity of settings or not
64 * @return array the relation parameters for the current user
66 * @global array the list of settings for servers
67 * @global integer the id of the current server
68 * @global string the URL of the page to show in case of error
69 * @global string the name of the current db
70 * @global string the name of the current table
71 * @global array configuration infos about the relations stuff
73 * @access public
75 * @author Mike Beck <mikebeck@users.sourceforge.net>
77 function PMA_getRelationsParam($verbose = FALSE)
79 global $cfg, $server, $err_url_0, $db, $table;
80 global $cfgRelation;
82 $cfgRelation = array();
83 $cfgRelation['relwork'] = FALSE;
84 $cfgRelation['displaywork'] = FALSE;
85 $cfgRelation['pdfwork'] = FALSE;
86 $cfgRelation['commwork'] = FALSE;
87 $cfgRelation['allworks'] = FALSE;
89 // No server selected -> no bookmark table
90 // we return the array with the FALSEs in it,
91 // to avoid some 'Unitialized string offset' errors later
92 if ($server == 0
93 || empty($cfg['Server'])
94 || empty($cfg['Server']['pmadb'])) {
95 if ($verbose == TRUE) {
96 echo 'PMA Database ... '
97 . '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font>'
98 . '[ <a href="Documentation.html#pmadb">' . $GLOBALS['strDocu'] . '</a> ]<br />' . "\n"
99 . $GLOBALS['strGeneralRelationFeat']
100 . ' <font color="green">' . $GLOBALS['strDisabled'] . '</font>' . "\n";
102 return $cfgRelation;
105 $cfgRelation['user'] = $cfg['Server']['user'];
106 $cfgRelation['db'] = $cfg['Server']['pmadb'];
108 // Now I just check if all tables that i need are present so I can for
109 // example enable relations but not pdf...
110 // I was thinking of checking if they have all required columns but I
111 // fear it might be too slow
112 // PMA_mysql_select_db($cfgRelation['db']);
114 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($cfgRelation['db']);
115 $tab_rs = PMA_query_as_cu($tab_query, FALSE);
117 while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
118 if ($curr_table[0] == $cfg['Server']['bookmarktable']) {
119 continue;
120 } else if ($curr_table[0] == $cfg['Server']['relation']) {
121 $cfgRelation['relation'] = $curr_table[0];
122 } else if ($curr_table[0] == $cfg['Server']['table_info']) {
123 $cfgRelation['table_info'] = $curr_table[0];
124 } else if ($curr_table[0] == $cfg['Server']['table_coords']) {
125 $cfgRelation['table_coords'] = $curr_table[0];
126 } else if ($curr_table[0] == $cfg['Server']['column_comments']) {
127 $cfgRelation['column_comments'] = $curr_table[0];
128 } else if ($curr_table[0] == $cfg['Server']['pdf_pages']) {
129 $cfgRelation['pdf_pages'] = $curr_table[0];
131 } // end while
132 if (isset($cfgRelation['relation'])) {
133 $cfgRelation['relwork'] = TRUE;
134 if (isset($cfgRelation['table_info'])) {
135 $cfgRelation['displaywork'] = TRUE;
137 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
138 $cfgRelation['pdfwork'] = TRUE;
140 if (isset($cfgRelation['column_comments'])) {
141 $cfgRelation['commwork'] = TRUE;
143 } // end if
145 if ($cfgRelation['relwork'] == TRUE && $cfgRelation['displaywork'] == TRUE
146 && $cfgRelation['pdfwork'] == TRUE && $cfgRelation['commwork'] == TRUE) {
147 $cfgRelation['allworks'] = TRUE;
149 if ($tab_rs) {
150 mysql_free_result($tab_rs);
151 } else {
152 $cfg['Server']['pmadb'] = FALSE;
155 if ($verbose == TRUE) {
156 $shit = '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font> [ <a href="Documentation.html#%s">' . $GLOBALS['strDocu'] . '</a> ]';
157 $hit = '<font color="green"><b>' . $GLOBALS['strOK'] . '</b></font>';
158 $enabled = '<font color="green">' . $GLOBALS['strEnabled'] . '</font>';
159 $disabled = '<font color="red">' . $GLOBALS['strDisabled'] . '</font>';
161 echo '<table>' . "\n";
162 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'pmadb\'] ... </th><td align="right">'
163 . (($cfg['Server']['pmadb'] == FALSE) ? sprintf($shit, 'pmadb') : $hit)
164 . '</td></tr>' . "\n";
165 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
167 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'relation\'] ... </th><td align="right">'
168 . ((isset($cfgRelation['relation'])) ? $hit : sprintf($shit, 'relation'))
169 . '</td></tr>' . "\n";
170 echo ' <tr><td colspan=2 align="center">'. $GLOBALS['strGeneralRelationFeat'] . ': '
171 . (($cfgRelation['relwork'] == TRUE) ? $enabled : $disabled)
172 . '</td></tr>' . "\n";
173 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
175 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'table_info\'] ... </th><td align="right">'
176 . (($cfgRelation['displaywork'] == FALSE) ? sprintf($shit, 'table_info') : $hit)
177 . '</td></tr>' . "\n";
178 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strDisplayFeat'] . ': '
179 . (($cfgRelation['displaywork'] == TRUE) ? $enabled : $disabled)
180 . '</td></tr>' . "\n";
181 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
183 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'table_coords\'] ... </th><td align="right">'
184 . ((isset($cfgRelation['table_coords'])) ? $hit : sprintf($shit, 'table_coords'))
185 . '</td></tr>' . "\n";
186 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'pdf_pages\'] ... </th><td align="right">'
187 . ((isset($cfgRelation['pdf_pages'])) ? $hit : sprintf($shit, 'table_coords'))
188 . '</td></tr>' . "\n";
189 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strCreatePdfFeat'] . ': '
190 . (($cfgRelation['pdfwork'] == TRUE) ? $enabled : $disabled)
191 . '</td></tr>' . "\n";
192 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
194 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'column_comments\'] ... </th><td align="right">'
195 . ((isset($cfgRelation['column_comments'])) ? $hit : sprintf($shit, 'col_com'))
196 . '</td></tr>' . "\n";
197 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strColComFeat'] . ': '
198 . (($cfgRelation['commwork'] == TRUE) ? $enabled : $disabled)
199 . '</td></tr>' . "\n";
200 echo '</table>' . "\n";
201 } // end if ($verbose == TRUE) {
203 return $cfgRelation;
204 } // end of the 'PMA_getRelationsParam()' function
208 * Gets all Relations to foreign tables for a given table or
209 * optionally a given column in a table
211 * @param string the name of the db to check for
212 * @param string the name of the table to check for
213 * @param string the name of the column to check for
215 * @return array db,table,column
217 * @global array the list of relations settings
218 * @global string the URL of the page to show in case of error
220 * @access public
222 * @author Mike Beck <mikebeck@users.sourceforge.net>
224 function PMA_getForeigners($db, $table, $column = '') {
225 global $cfgRelation, $err_url_0;
227 $rel_query = 'SELECT master_field, foreign_db, foreign_table, foreign_field'
228 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
229 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' '
230 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\' ';
231 if (!empty($column)) {
232 $rel_query .= ' AND master_field = \'' . PMA_sqlAddslashes($column) . '\'';
234 $relations = PMA_query_as_cu($rel_query);
235 $i = 0;
236 while ($relrow = @PMA_mysql_fetch_array($relations)) {
237 $field = $relrow['master_field'];
238 $foreign[$field]['foreign_db'] = $relrow['foreign_db'];
239 $foreign[$field]['foreign_table'] = $relrow['foreign_table'];
240 $foreign[$field]['foreign_field'] = $relrow['foreign_field'];
241 $i++;
242 } // end while
244 if (isset($foreign) && is_array($foreign)) {
245 return $foreign;
246 } else {
247 return FALSE;
249 } // end of the 'PMA_getForeigners()' function
253 * Gets the display field of a table
255 * @param string the name of the db to check for
256 * @param string the name of the table to check for
258 * @return string field name
260 * @global array the list of relations settings
262 * @access public
264 * @author Mike Beck <mikebeck@users.sourceforge.net>
266 function PMA_getDisplayField($db, $table) {
267 global $cfgRelation;
269 $disp_query = 'SELECT display_field FROM ' . PMA_backquote($cfgRelation['table_info'])
270 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
271 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
273 $disp_res = PMA_query_as_cu($disp_query);
274 $row = ($disp_res ? PMA_mysql_fetch_array($disp_res) : '');
275 if (isset($row['display_field'])) {
276 return $row['display_field'];
277 } else {
278 return FALSE;
280 } // end of the 'PMA_getDisplayField()' function
284 * Gets the comments for all rows of a table
286 * @param string the name of the db to check for
287 * @param string the name of the table to check for
289 * @return array [field_name] = comment
291 * @global array the list of relations settings
293 * @access public
295 * @author Mike Beck <mikebeck@users.sourceforge.net>
297 function PMA_getComments($db, $table) {
298 global $cfgRelation;
300 $com_qry = 'SELECT column_name, comment FROM ' . PMA_backquote($cfgRelation['column_comments'])
301 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
302 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
303 $com_rs = PMA_query_as_cu($com_qry);
305 while ($row = @PMA_mysql_fetch_array($com_rs)) {
306 $col = $row['column_name'];
307 $comment[$col] = $row['comment'];
308 } // end while
310 if (isset($comment) && is_array($comment)) {
311 return $comment;
312 } else {
313 return FALSE;
315 } // end of the 'PMA_getComments()' function
316 } // $__PMA_RELATION_LIB__