00001 <?php
00002
00003
00004
00027 function PMA_query_as_cu($sql, $show_error = TRUE) {
00028 global $err_url_0, $db, $dbh, $cfgRelation;
00029
00030 if (isset($dbh)) {
00031 PMA_mysql_select_db($cfgRelation['db'], $dbh);
00032 $result = @PMA_mysql_query($sql, $dbh);
00033 if (!$result && $show_error == TRUE) {
00034 PMA_mysqlDie(mysql_error($dbh), $sql, '', $err_url_0);
00035 }
00036 PMA_mysql_select_db($db, $dbh);
00037 } else {
00038 PMA_mysql_select_db($cfgRelation['db']);
00039 $result = @PMA_mysql_query($sql);
00040 if ($result && $show_error == TRUE) {
00041 PMA_mysqlDie('', $sql, '', $err_url_0);
00042 }
00043 PMA_mysql_select_db($db);
00044 }
00045
00046 if ($result) {
00047 return $result;
00048 } else {
00049 return FALSE;
00050 }
00051 }
00052
00053
00074 function PMA_getRelationsParam($verbose = FALSE)
00075 {
00076 global $cfg, $server, $err_url_0, $db, $table;
00077 global $cfgRelation;
00078
00079 $cfgRelation = array();
00080 $cfgRelation['relwork'] = FALSE;
00081 $cfgRelation['displaywork'] = FALSE;
00082 $cfgRelation['bookmarkwork']= FALSE;
00083 $cfgRelation['pdfwork'] = FALSE;
00084 $cfgRelation['commwork'] = FALSE;
00085 $cfgRelation['mimework'] = FALSE;
00086 $cfgRelation['historywork'] = FALSE;
00087 $cfgRelation['allworks'] = FALSE;
00088
00089
00090
00091
00092 if ($server == 0
00093 || empty($cfg['Server'])
00094 || empty($cfg['Server']['pmadb'])) {
00095 if ($verbose == TRUE) {
00096 echo 'PMA Database ... '
00097 . '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font>'
00098 . '[ <a href="Documentation.html#pmadb">' . $GLOBALS['strDocu'] . '</a> ]<br />' . "\n"
00099 . $GLOBALS['strGeneralRelationFeat']
00100 . ' <font color="green">' . $GLOBALS['strDisabled'] . '</font>' . "\n";
00101 }
00102 return $cfgRelation;
00103 }
00104
00105 $cfgRelation['user'] = $cfg['Server']['user'];
00106 $cfgRelation['db'] = $cfg['Server']['pmadb'];
00107
00108
00109
00110
00111
00112
00113
00114 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($cfgRelation['db']);
00115 $tab_rs = PMA_query_as_cu($tab_query, FALSE);
00116
00117 while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
00118 if ($curr_table[0] == $cfg['Server']['bookmarktable']) {
00119 $cfgRelation['bookmark'] = $curr_table[0];
00120 } else if ($curr_table[0] == $cfg['Server']['relation']) {
00121 $cfgRelation['relation'] = $curr_table[0];
00122 } else if ($curr_table[0] == $cfg['Server']['table_info']) {
00123 $cfgRelation['table_info'] = $curr_table[0];
00124 } else if ($curr_table[0] == $cfg['Server']['table_coords']) {
00125 $cfgRelation['table_coords'] = $curr_table[0];
00126 } else if ($curr_table[0] == $cfg['Server']['column_info']) {
00127 $cfgRelation['column_info'] = $curr_table[0];
00128 } else if ($curr_table[0] == $cfg['Server']['pdf_pages']) {
00129 $cfgRelation['pdf_pages'] = $curr_table[0];
00130 } else if ($curr_table[0] == $cfg['Server']['history']) {
00131 $cfgRelation['history'] = $curr_table[0];
00132 }
00133 }
00134 if (isset($cfgRelation['relation'])) {
00135 $cfgRelation['relwork'] = TRUE;
00136 if (isset($cfgRelation['table_info'])) {
00137 $cfgRelation['displaywork'] = TRUE;
00138 }
00139 }
00140 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
00141 $cfgRelation['pdfwork'] = TRUE;
00142 }
00143 if (isset($cfgRelation['column_info'])) {
00144 $cfgRelation['commwork'] = TRUE;
00145
00146 if ($cfg['Server']['verbose_check']) {
00147 $mime_query = 'SHOW FIELDS FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
00148 $mime_rs = PMA_query_as_cu($mime_query, FALSE);
00149
00150 $mime_field_mimetype = FALSE;
00151 $mime_field_transformation = FALSE;
00152 $mime_field_transformation_options = FALSE;
00153 while ($curr_mime_field = @PMA_mysql_fetch_array($mime_rs)) {
00154 if ($curr_mime_field[0] == 'mimetype') {
00155 $mime_field_mimetype = TRUE;
00156 } else if ($curr_mime_field[0] == 'transformation') {
00157 $mime_field_transformation = TRUE;
00158 } else if ($curr_mime_field[0] == 'transformation_options') {
00159 $mime_field_transformation_options = TRUE;
00160 }
00161 }
00162
00163 if ($mime_field_mimetype == TRUE
00164 && $mime_field_transformation == TRUE
00165 && $mime_field_transformation_options == TRUE) {
00166 $cfgRelation['mimework'] = TRUE;
00167 }
00168 } else {
00169 $cfgRelation['mimework'] = TRUE;
00170 }
00171 }
00172
00173 if (isset($cfgRelation['history'])) {
00174 $cfgRelation['historywork'] = TRUE;
00175 }
00176
00177 if (isset($cfgRelation['bookmark'])) {
00178 $cfgRelation['bookmarkwork'] = TRUE;
00179 }
00180
00181 if ($cfgRelation['relwork'] == TRUE && $cfgRelation['displaywork'] == TRUE
00182 && $cfgRelation['pdfwork'] == TRUE && $cfgRelation['commwork'] == TRUE
00183 && $cfgRelation['mimework'] == TRUE && $cfgRelation['historywork'] == TRUE
00184 && $cfgRelation['bookmarkwork'] == TRUE) {
00185 $cfgRelation['allworks'] = TRUE;
00186 }
00187 if ($tab_rs) {
00188 mysql_free_result($tab_rs);
00189 } else {
00190 $cfg['Server']['pmadb'] = FALSE;
00191 }
00192
00193 if ($verbose == TRUE) {
00194 $shit = '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font> [ <a href="Documentation.html#%s">' . $GLOBALS['strDocu'] . '</a> ]';
00195 $hit = '<font color="green"><b>' . $GLOBALS['strOK'] . '</b></font>';
00196 $enabled = '<font color="green">' . $GLOBALS['strEnabled'] . '</font>';
00197 $disabled = '<font color="red">' . $GLOBALS['strDisabled'] . '</font>';
00198
00199 echo '<table>' . "\n";
00200 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'pmadb\'] ... </th><td align="right">'
00201 . (($cfg['Server']['pmadb'] == FALSE) ? sprintf($shit, 'pmadb') : $hit)
00202 . '</td></tr>' . "\n";
00203 echo ' <tr><td> </td></tr>' . "\n";
00204
00205 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'relation\'] ... </th><td align="right">'
00206 . ((isset($cfgRelation['relation'])) ? $hit : sprintf($shit, 'relation'))
00207 . '</td></tr>' . "\n";
00208 echo ' <tr><td colspan=2 align="center">'. $GLOBALS['strGeneralRelationFeat'] . ': '
00209 . (($cfgRelation['relwork'] == TRUE) ? $enabled : $disabled)
00210 . '</td></tr>' . "\n";
00211 echo ' <tr><td> </td></tr>' . "\n";
00212
00213 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'table_info\'] ... </th><td align="right">'
00214 . (($cfgRelation['displaywork'] == FALSE) ? sprintf($shit, 'table_info') : $hit)
00215 . '</td></tr>' . "\n";
00216 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strDisplayFeat'] . ': '
00217 . (($cfgRelation['displaywork'] == TRUE) ? $enabled : $disabled)
00218 . '</td></tr>' . "\n";
00219 echo ' <tr><td> </td></tr>' . "\n";
00220
00221 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'table_coords\'] ... </th><td align="right">'
00222 . ((isset($cfgRelation['table_coords'])) ? $hit : sprintf($shit, 'table_coords'))
00223 . '</td></tr>' . "\n";
00224 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'pdf_pages\'] ... </th><td align="right">'
00225 . ((isset($cfgRelation['pdf_pages'])) ? $hit : sprintf($shit, 'table_coords'))
00226 . '</td></tr>' . "\n";
00227 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strCreatePdfFeat'] . ': '
00228 . (($cfgRelation['pdfwork'] == TRUE) ? $enabled : $disabled)
00229 . '</td></tr>' . "\n";
00230 echo ' <tr><td> </td></tr>' . "\n";
00231
00232 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'column_info\'] ... </th><td align="right">'
00233 . ((isset($cfgRelation['column_info'])) ? $hit : sprintf($shit, 'col_com'))
00234 . '</td></tr>' . "\n";
00235 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strColComFeat'] . ': '
00236 . (($cfgRelation['commwork'] == TRUE) ? $enabled : $disabled)
00237 . '</td></tr>' . "\n";
00238 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strBookmarkQuery'] . ': '
00239 . (($cfgRelation['bookmarkwork'] == TRUE) ? $enabled : $disabled)
00240 . '</td></tr>' . "\n";
00241 echo ' <tr><th align="left">MIME ...</th><td align="right">'
00242 . (($cfgRelation['mimework'] == TRUE) ? $hit : sprintf($shit, 'col_com'))
00243 . '</td></tr>' . "\n";
00244
00245 if (($cfgRelation['commwork'] == TRUE) && ($cfgRelation['mimework'] != TRUE)) {
00246 echo '<tr><td colspan=2 align="left">' . $GLOBALS['strUpdComTab'] . '</td></tr>' . "\n";
00247 }
00248
00249 echo ' <tr><th align="left">$cfg[\'Servers\'][$i][\'history\'] ... </th><td align="right">'
00250 . ((isset($cfgRelation['history'])) ? $hit : sprintf($shit, 'history'))
00251 . '</td></tr>' . "\n";
00252 echo ' <tr><td colspan=2 align="center">' . $GLOBALS['strQuerySQLHistory'] . ': '
00253 . (($cfgRelation['historywork'] == TRUE) ? $enabled : $disabled)
00254 . '</td></tr>' . "\n";
00255
00256 echo '</table>' . "\n";
00257 }
00258
00259 return $cfgRelation;
00260 }
00261
00262
00281 function PMA_getForeigners($db, $table, $column = '', $source = 'both') {
00282 global $cfgRelation, $err_url_0;
00283
00284 if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
00285 $rel_query = 'SELECT master_field, foreign_db, foreign_table, foreign_field'
00286 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
00287 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' '
00288 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\' ';
00289 if (!empty($column)) {
00290 $rel_query .= ' AND master_field = \'' . PMA_sqlAddslashes($column) . '\'';
00291 }
00292 $relations = PMA_query_as_cu($rel_query);
00293 $i = 0;
00294 while ($relrow = @PMA_mysql_fetch_array($relations)) {
00295 $field = $relrow['master_field'];
00296 $foreign[$field]['foreign_db'] = $relrow['foreign_db'];
00297 $foreign[$field]['foreign_table'] = $relrow['foreign_table'];
00298 $foreign[$field]['foreign_field'] = $relrow['foreign_field'];
00299 $i++;
00300 }
00301 }
00302
00303 if (($source == 'both' || $source == 'innodb') && !empty($table)) {
00304 $show_create_table_query = 'SHOW CREATE TABLE '
00305 . PMA_backquote($db) . '.' . PMA_backquote($table);
00306 $show_create_table_res = PMA_mysql_query($show_create_table_query);
00307 list(,$show_create_table) = PMA_mysql_fetch_row($show_create_table_res);
00308
00309 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
00310
00311 foreach($analyzed_sql[0]['foreign_keys'] AS $one_key) {
00312
00313
00314
00315 foreach($one_key['index_list'] AS $i => $field) {
00316
00317
00318
00319
00320
00321 $foreign[$field]['constraint'] = $one_key['constraint'];
00322
00323 if (isset($one_key['ref_db_name'])) {
00324 $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
00325 } else {
00326 $foreign[$field]['foreign_db'] = $db;
00327 }
00328 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
00329 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
00330 if (isset($one_key['on_delete'])) {
00331 $foreign[$field]['on_delete'] = $one_key['on_delete'];
00332 }
00333 if (isset($one_key['on_update'])) {
00334 $foreign[$field]['on_update'] = $one_key['on_update'];
00335 }
00336 }
00337 }
00338 }
00339
00340 if (isset($foreign) && is_array($foreign)) {
00341 return $foreign;
00342 } else {
00343 return FALSE;
00344 }
00345 }
00346
00347
00362 function PMA_getDisplayField($db, $table) {
00363 global $cfgRelation;
00364
00365 $disp_query = 'SELECT display_field FROM ' . PMA_backquote($cfgRelation['table_info'])
00366 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
00367 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
00368
00369 $disp_res = PMA_query_as_cu($disp_query);
00370 $row = ($disp_res ? PMA_mysql_fetch_array($disp_res) : '');
00371 if (isset($row['display_field'])) {
00372 return $row['display_field'];
00373 } else {
00374 return FALSE;
00375 }
00376 }
00377
00378
00393 function PMA_getComments($db, $table = '') {
00394 global $cfgRelation;
00395
00396 if ($table != '') {
00397 $com_qry = 'SELECT column_name, ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['column_info'])
00398 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
00399 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
00400 $com_rs = PMA_query_as_cu($com_qry);
00401 } else {
00402 $com_qry = 'SELECT comment FROM ' . PMA_backquote($cfgRelation['column_info'])
00403 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
00404 . ' AND table_name = \'\''
00405 . ' AND column_name = \'(db_comment)\'';
00406 $com_rs = PMA_query_as_cu($com_qry);
00407 }
00408
00409 $i = 0;
00410 while ($row = @PMA_mysql_fetch_array($com_rs)) {
00411 $i++;
00412 $col = ($table != '' ? $row['column_name'] : $i);
00413
00414 if (strlen($row['comment']) > 0) {
00415 $comment[$col] = $row['comment'];
00416 }
00417
00418 }
00419
00420 if (isset($comment) && is_array($comment)) {
00421 return $comment;
00422 } else {
00423 return FALSE;
00424 }
00425 }
00426
00436 function PMA_handleSlashes($val) {
00437 return (get_magic_quotes_gpc() ? str_replace('\\"', '"', $val) : PMA_sqlAddslashes($val));
00438 }
00439
00455 function PMA_setComment($db, $table, $key, $value, $removekey = '') {
00456 global $cfgRelation;
00457
00458 if ($removekey != '' AND $removekey != $key) {
00459 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
00460 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
00461 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
00462 . ' AND column_name = \'' . PMA_sqlAddslashes($removekey) . '\'';
00463 $rmv_rs = PMA_query_as_cu($remove_query);
00464 unset($rmv_query);
00465 }
00466
00467 $test_qry = 'SELECT ' . PMA_backquote('comment') . ', mimetype, transformation, transformation_options FROM ' . PMA_backquote($cfgRelation['column_info'])
00468 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
00469 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
00470 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
00471 $test_rs = PMA_query_as_cu($test_qry);
00472
00473 if ($test_rs && mysql_num_rows($test_rs) > 0) {
00474 $row = @PMA_mysql_fetch_array($test_rs);
00475
00476 if (strlen($value) > 0 || strlen($row['mimetype']) > 0 || strlen($row['transformation']) > 0 || strlen($row['transformation_options']) > 0) {
00477 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info'])
00478 . ' SET ' . PMA_backquote('comment') . ' = \'' . PMA_sqlAddslashes($value) . '\''
00479 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
00480 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
00481 . ' AND column_name = \'' . PMA_sqlAddSlashes($key) . '\'';
00482 } else {
00483 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
00484 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
00485 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
00486 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
00487 }
00488 } else if (strlen($value) > 0) {
00489 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_info'])
00490 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
00491 . ' VALUES('
00492 . '\'' . PMA_sqlAddslashes($db) . '\','
00493 . '\'' . PMA_sqlAddslashes($table) . '\','
00494 . '\'' . PMA_sqlAddslashes($key) . '\','
00495 . '\'' . PMA_sqlAddslashes($value) . '\')';
00496 }
00497
00498 if (isset($upd_query)){
00499 $upd_rs = PMA_query_as_cu($upd_query);
00500 unset($upd_query);
00501 return true;
00502 } else {
00503 return false;
00504 }
00505 }
00506
00519 function PMA_setHistory($db, $table, $username, $sqlquery) {
00520 global $cfgRelation;
00521
00522 $hist_rs = PMA_query_as_cu('INSERT INTO ' . PMA_backquote($cfgRelation['history']) . ' ('
00523 . PMA_backquote('username') . ','
00524 . PMA_backquote('db') . ','
00525 . PMA_backquote('table') . ','
00526 . PMA_backquote('timevalue') . ','
00527 . PMA_backquote('sqlquery')
00528 . ') VALUES ('
00529 . '\'' . PMA_sqlAddslashes($username) . '\','
00530 . '\'' . PMA_sqlAddslashes($db) . '\','
00531 . '\'' . PMA_sqlAddslashes($table) . '\','
00532 . 'NOW(),'
00533 . '\'' . PMA_sqlAddslashes($sqlquery) . '\')');
00534 return true;
00535 }
00536
00546 function PMA_getHistory($username) {
00547 global $cfgRelation;
00548
00549 $hist_rs = PMA_query_as_cu('SELECT '
00550 . PMA_backquote('db') . ','
00551 . PMA_backquote('table') . ','
00552 . PMA_backquote('sqlquery')
00553 . ' FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE username = \'' . PMA_sqlAddslashes($username) . '\' ORDER BY id DESC');
00554
00555 $history = array();
00556
00557 while ($row = @PMA_mysql_fetch_array($hist_rs)) {
00558 $history[] = $row;
00559 }
00560
00561 return $history;
00562
00563 }
00564
00577 function PMA_purgeHistory($username) {
00578 global $cfgRelation, $cfg;
00579
00580 $purge_rs = PMA_query_as_cu('SELECT timevalue FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE username = \'' . PMA_sqlAddSlashes($username) . '\' ORDER BY timevalue DESC LIMIT ' . $cfg['QueryHistoryMax'] . ', 1');
00581 $i = 0;
00582 $row = @PMA_mysql_fetch_array($purge_rs);
00583
00584 if (is_array($row) && isset($row[0]) && $row[0] > 0) {
00585 $maxtime = $row[0];
00586
00587
00588 $remove_rs = PMA_query_as_cu('DELETE FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE timevalue <= "' . $maxtime . '"');
00589 }
00590
00591 return true;
00592 }
00593
00606 function PMA_foreignDropdown($disp, $foreign_field, $foreign_display, $data, $max = 100) {
00607 global $cfg;
00608
00609 $ret = '<option value=""></option>' . "\n";
00610
00611 $reloptions = array('content-id' => array(), 'id-content' => array());
00612 while ($relrow = @PMA_mysql_fetch_array($disp)) {
00613 $key = $relrow[$foreign_field];
00614 if (strlen($relrow[$foreign_display]) <= $cfg['LimitChars']) {
00615 $value = (($foreign_display != FALSE) ? htmlspecialchars($relrow[$foreign_display]) : '');
00616 $vtitle = '';
00617 } else {
00618 $vtitle = htmlspecialchars($relrow[$foreign_display]);
00619 $value = (($foreign_display != FALSE) ? htmlspecialchars(substr($vtitle, 0, $cfg['LimitChars']) . '...') : '');
00620 }
00621
00622 $reloption = '<option value="' . htmlspecialchars($key) . '"';
00623 if ($vtitle != '') {
00624 $reloption .= ' title="' . $vtitle . '"';
00625 }
00626
00627 if ($key == $data) {
00628 $reloption .= ' selected="selected"';
00629 }
00630
00631 $reloptions['id-content'][] = $reloption . '>' . $value . ' - ' . htmlspecialchars($key) . '</option>' . "\n";
00632 $reloptions['content-id'][] = $reloption . '>' . htmlspecialchars($key) . ' - ' . $value . '</option>' . "\n";
00633 }
00634
00635 if ($max == -1 || count($reloptions['content-id']) < $max) {
00636 $ret .= implode('', $reloptions['content-id']);
00637 if (count($reloptions['content-id']) > 0) {
00638 $ret .= '<option value=""></option>' . "\n";
00639 $ret .= '<option value=""></option>' . "\n";
00640 }
00641 }
00642
00643 $ret .= implode('', $reloptions['id-content']);
00644
00645 return $ret;
00646 }
00647
00648 ?>