lang
[phpmyadmin/crack.git] / libraries / relation.lib.php3
blobd8c30d213af3324bc1f48a92573c83c12f6fa78f
1 <?php
2 /* $Id$ */
4 /**
5 * Set of functions used with the relation and pdf feature
6 */
9 if (!defined('PMA_RELATION_LIB_INCLUDED')){
10 define('PMA_RELATION_LIB_INCLUDED', 1);
12 /**
13 * Executes a query as controluser if possible, otherwise as normal user
15 * @param string the query to execute
16 * @param boolean whether to display SQL error messages or not
18 * @return integer the result id
20 * @global string the URL of the page to show in case of error
21 * @global string the name of db to come back to
22 * @global integer the ressource id of DB connect as controluser
23 * @global array configuration infos about the relations stuff
25 * @access public
27 * @author Mike Beck <mikebeck@users.sourceforge.net>
29 function PMA_query_as_cu($sql, $show_error = TRUE) {
30 global $err_url_0, $db, $dbh, $cfgRelation;
32 if (isset($dbh)) {
33 PMA_mysql_select_db($cfgRelation['db'], $dbh);
34 $result = @PMA_mysql_query($sql, $dbh);
35 if (!$result && $show_error == TRUE) {
36 PMA_mysqlDie(mysql_error($dbh), $sql, '', $err_url_0);
38 PMA_mysql_select_db($db, $dbh);
39 } else {
40 PMA_mysql_select_db($cfgRelation['db']);
41 $result = @PMA_mysql_query($sql);
42 if ($result && $show_error == TRUE) {
43 PMA_mysqlDie('', $sql, '', $err_url_0);
45 PMA_mysql_select_db($db);
46 } // end if... else...
48 if ($result) {
49 return $result;
50 } else {
51 return FALSE;
53 } // end of the "PMA_query_as_cu()" function
56 /**
57 * Defines the relation parameters for the current user
58 * just a copy of the functions used for relations ;-)
59 * but added some stuff to check what will work
61 * @return array the relation parameters for the current user
63 * @global array the list of settings for servers
64 * @global integer the id of the current server
65 * @global string the URL of the page to show in case of error
66 * @global string the name of the current db
67 * @global string the name of the current table
69 * @access public
71 * @author Mike Beck <mikebeck@users.sourceforge.net>
73 function PMA_getRelationsParam($verbose=FALSE)
75 global $cfg, $server, $err_url_0, $db, $table;
77 $cfgRelation = array();
78 $cfgRelation['relwork'] = FALSE;
79 $cfgRelation['displaywork'] = FALSE;
80 $cfgRelation['pdfwork'] = FALSE;
81 $cfgRelation['commwork'] = FALSE;
82 $cfgRelation['allworks'] = FALSE;
84 // No server selected -> no bookmark table
85 // we return the array with the FALSEs in it,
86 // to avoid some 'Unitialized string offset' errors later
87 if ($server == 0
88 || empty($cfg['Server'])
89 || empty($cfg['Server']['pmadb'])) {
90 return $cfgRelation;
93 $cfgRelation['user'] = $cfg['Server']['user'];
94 $cfgRelation['db'] = $cfg['Server']['pmadb'];
96 // Now I just check if all tables that i need are present so I can for
97 // example enable relations but not pdf...
98 // I was thinking of checking if they have all required columns but I
99 // fear it might be too slow
100 // PMA_mysql_select_db($cfgRelation['db']);
102 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($cfgRelation['db']);
103 $tab_rs = PMA_query_as_cu($tab_query, FALSE);
105 while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
106 if ($curr_table[0] == $cfg['Server']['bookmarktable']) {
107 continue;
108 } else if ($curr_table[0] == $cfg['Server']['relation']) {
109 $cfgRelation['relation'] = $curr_table[0];
110 } else if ($curr_table[0] == $cfg['Server']['table_info']) {
111 $cfgRelation['table_info'] = $curr_table[0];
112 } else if ($curr_table[0] == $cfg['Server']['table_coords']) {
113 $cfgRelation['table_coords'] = $curr_table[0];
114 } else if ($curr_table[0] == $cfg['Server']['column_comments']) {
115 $cfgRelation['column_comments'] = $curr_table[0];
116 } else if ($curr_table[0] == $cfg['Server']['pdf_pages']) {
117 $cfgRelation['pdf_pages'] = $curr_table[0];
119 } // end while
120 if (isset($cfgRelation['relation'])) {
121 $cfgRelation['relwork'] = TRUE;
122 if (isset($cfgRelation['table_info'])) {
123 $cfgRelation['displaywork'] = TRUE;
125 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
126 $cfgRelation['pdfwork'] = TRUE;
128 if (isset($cfgRelation['column_comments'])) {
129 $cfgRelation['commwork'] = TRUE;
131 } // end if
133 if($cfgRelation['relwork'] == TRUE && $cfgRelation['displaywork'] == TRUE
134 && $cfgRelation['pdfwork'] == TRUE && $cfgRelation['commwork'] == TRUE){
135 $cfgRelation['allworks'] = TRUE;
137 if ($tab_rs) {
138 mysql_free_result($tab_rs);
139 } else {
140 $cfg['Server']['pmadb'] = FALSE;
142 if($verbose==TRUE){
143 $shit='<font color="red">not OK</font> [ <a href="Documentation.html#%s">' . $GLOBALS['strDocu'] . '</a> ]';
144 $hit ='<font color="green">OK</font>';
145 echo 'checking PMA Database ...',$cfg['Server']['pmadb'] == FALSE?sprintf($shit,'pmadb'):$hit,'<br />';
146 echo 'checking relation Table ...',isset($cfgRelation['relation'])?$hit:sprintf($shit,'relation'),'<br />';
147 echo '<b>',$cfgRelation['relwork']==TRUE?'General Relationfeatures enabled':'General Relationfeatures disabled','</b><br />';
148 echo 'checking table_info ...',$cfgRelation['displaywork'] == FALSE?sprintf($shit,'table_info'):$hit,'<br />';
149 echo '<b>',$cfgRelation['displaywork']==TRUE?'Displayfeatures enabled':'Displayfeatures disabled','</b><br />';
150 echo 'checking table_coords ...',isset($cfgRelation['table_coords'])?$hit:sprintf($shit,'table_coords'),'<br />';
151 echo 'checking pdf_pages ...',isset($cfgRelation['pdf_pages'])?$hit:sprintf($shit,'table_coords'),'<br />';
152 echo '<b>',$cfgRelation['pdfwork']==TRUE?'Creation of PDFs enabled':'Creation of PDFs disabled','</b><br />';
153 echo 'checking column_comments ...',isset($cfgRelation['column_comments'])?$hit:sprintf($shit,'col_com'),'<br />';
154 echo '<b>',$cfgRelation['commwork']==TRUE?'Displaying Column Comments enabled':'Displaying Column Comments disabled','</b><br />';
158 return $cfgRelation;
159 } // end of the 'PMA_getRelationsParam()' function
163 * Gets all Relations to foreign tables for a given table or
164 * optionally a given column in a table
166 * @param string the name of the db to check for
167 * @param string the name of the table to check for
168 * @param string the name of the column to check for
170 * @return array db,table,column
172 * @global array the list of relations settings
173 * @global string the URL of the page to show in case of error
175 * @access public
177 * @author Mike Beck <mikebeck@users.sourceforge.net>
179 function PMA_getForeigners($db, $table, $column = '') {
180 global $cfgRelation, $err_url_0;
182 $rel_query = 'SELECT master_field, foreign_db, foreign_table, foreign_field'
183 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
184 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' '
185 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\' ';
186 if (!empty($column)) {
187 $rel_query .= ' AND master_field = \'' . PMA_sqlAddslashes($column) . '\'';
189 $relations = PMA_query_as_cu($rel_query);
190 $i = 0;
191 while ($relrow = @PMA_mysql_fetch_array($relations)) {
192 $field = $relrow['master_field'];
193 $foreign[$field]['foreign_db'] = $relrow['foreign_db'];
194 $foreign[$field]['foreign_table'] = $relrow['foreign_table'];
195 $foreign[$field]['foreign_field'] = $relrow['foreign_field'];
196 $i++;
197 } // end while
199 if (isset($foreign) && is_array($foreign)) {
200 return $foreign;
201 } else {
202 return FALSE;
204 } // end of the 'PMA_getForeigners()' function
208 * Gets the display field of a table
210 * @param string the name of the db to check for
211 * @param string the name of the table to check for
213 * @return string field name
215 * @global array the list of relations settings
217 * @access public
219 * @author Mike Beck <mikebeck@users.sourceforge.net>
221 function PMA_getDisplayField($db, $table) {
222 global $cfgRelation;
224 $disp_query = 'SELECT display_field FROM ' . PMA_backquote($cfgRelation['table_info'])
225 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
226 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
228 $disp_res = PMA_query_as_cu($disp_query);
229 $row = ($disp_res ? PMA_mysql_fetch_array($disp_res) : '');
230 if (isset($row['display_field'])) {
231 return $row['display_field'];
232 } else {
233 return FALSE;
235 } // end of the 'PMA_getDisplayField()' function
239 * Gets the comments for all rows of a table
241 * @param string the name of the db to check for
242 * @param string the name of the table to check for
244 * @return array [field_name] = comment
246 * @global array the list of relations settings
248 * @access public
250 * @author Mike Beck <mikebeck@users.sourceforge.net>
252 function PMA_getComments($db, $table) {
253 global $cfgRelation;
255 $com_qry = 'SELECT column_name, comment FROM ' . PMA_backquote($cfgRelation['column_comments'])
256 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
257 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
258 $com_rs = PMA_query_as_cu($com_qry);
260 while ($row = @PMA_mysql_fetch_array($com_rs)) {
261 $col = $row['column_name'];
262 $comment[$col] = $row['comment'];
263 } // end while
265 if (isset($comment) && is_array($comment)) {
266 return $comment;
267 } else {
268 return FALSE;
270 } // end of the 'PMA_getComments()' function
271 } // $__PMA_RELATION_LIB__