relation.lib.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used with the relation and pdf feature
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Executes a query as controluser if possible, otherwise as normal user
  13. *
  14. * @param string $sql the query to execute
  15. * @param boolean $show_error whether to display SQL error messages or not
  16. * @param int $options query options
  17. *
  18. * @return integer the result set, or false if no result set
  19. *
  20. * @access public
  21. *
  22. */
  23. function PMA_queryAsControlUser($sql, $show_error = true, $options = 0)
  24. {
  25. // Avoid caching of the number of rows affected; for example, this function
  26. // is called for tracking purposes but we want to display the correct number
  27. // of rows affected by the original query, not by the query generated for
  28. // tracking.
  29. $cache_affected_rows = false;
  30. if ($show_error) {
  31. $result = $GLOBALS['dbi']->query(
  32. $sql,
  33. $GLOBALS['controllink'],
  34. $options,
  35. $cache_affected_rows
  36. );
  37. } else {
  38. $result = @$GLOBALS['dbi']->tryQuery(
  39. $sql,
  40. $GLOBALS['controllink'],
  41. $options,
  42. $cache_affected_rows
  43. );
  44. } // end if... else...
  45. if ($result) {
  46. return $result;
  47. } else {
  48. return false;
  49. }
  50. } // end of the "PMA_queryAsControlUser()" function
  51. /**
  52. * Returns current relation parameters
  53. *
  54. * @return array $cfgRelation
  55. */
  56. function PMA_getRelationsParam()
  57. {
  58. // avoid breakage if pmadb got unconfigured after login
  59. if (! defined('TESTSUITE') && empty($GLOBALS['cfg']['Server']['pmadb'])) {
  60. unset($_SESSION['relation'][$GLOBALS['server']]);
  61. }
  62. if (empty($_SESSION['relation'][$GLOBALS['server']])) {
  63. $_SESSION['relation'][$GLOBALS['server']] = PMA_checkRelationsParam();
  64. }
  65. // just for BC but needs to be before PMA_getRelationsParamDiagnostic()
  66. // which uses it
  67. $GLOBALS['cfgRelation'] = $_SESSION['relation'][$GLOBALS['server']];
  68. return $_SESSION['relation'][$GLOBALS['server']];
  69. }
  70. /**
  71. * prints out diagnostic info for pma relation feature
  72. *
  73. * @param array $cfgRelation Relation configuration
  74. *
  75. * @return string
  76. */
  77. function PMA_getRelationsParamDiagnostic($cfgRelation)
  78. {
  79. $retval = '';
  80. $messages = array();
  81. $messages['error'] = '<font color="red"><strong>'
  82. . __('not OK')
  83. . '</strong></font>'
  84. . ' [ <a href="%s" target="documentation">'
  85. . __('Documentation')
  86. . '</a> ]';
  87. $messages['ok'] = '<font color="green"><strong>'
  88. . _pgettext('Correctly working', 'OK')
  89. . '</strong></font>';
  90. $messages['enabled'] = '<font color="green">' . __('Enabled') . '</font>';
  91. $messages['disabled'] = '<font color="red">' . __('Disabled') . '</font>';
  92. if (false === $GLOBALS['cfg']['Server']['pmadb']) {
  93. $retval .= 'PMA Database ... '
  94. . sprintf($messages['error'], 'pmadb')
  95. . '<br />' . "\n"
  96. . __('General relation features')
  97. . ' <font color="green">' . __('Disabled')
  98. . '</font>' . "\n";
  99. } else {
  100. $retval .= '<table>' . "\n";
  101. $retval .= PMA_getDiagMessageForParameter(
  102. 'pmadb',
  103. $GLOBALS['cfg']['Server']['pmadb'],
  104. $messages,
  105. 'pmadb'
  106. );
  107. $retval .= PMA_getDiagMessageForParameter(
  108. 'relation',
  109. isset($cfgRelation['relation']),
  110. $messages,
  111. 'relation'
  112. );
  113. $retval .= PMA_getDiagMessageForFeature(
  114. __('General relation features'),
  115. 'relwork',
  116. $messages
  117. );
  118. $retval .= PMA_getDiagMessageForParameter(
  119. 'table_info',
  120. isset($cfgRelation['table_info']),
  121. $messages,
  122. 'table_info'
  123. );
  124. $retval .= PMA_getDiagMessageForFeature(
  125. __('Display Features'),
  126. 'displaywork',
  127. $messages
  128. );
  129. $retval .= PMA_getDiagMessageForParameter(
  130. 'table_coords',
  131. isset($cfgRelation['table_coords']),
  132. $messages,
  133. 'table_coords'
  134. );
  135. $retval .= PMA_getDiagMessageForParameter(
  136. 'pdf_pages',
  137. isset($cfgRelation['pdf_pages']),
  138. $messages,
  139. 'pdf_pages'
  140. );
  141. $retval .= PMA_getDiagMessageForFeature(
  142. __('Creation of PDFs'),
  143. 'pdfwork',
  144. $messages
  145. );
  146. $retval .= PMA_getDiagMessageForParameter(
  147. 'column_info',
  148. isset($cfgRelation['column_info']),
  149. $messages,
  150. 'column_info'
  151. );
  152. $retval .= PMA_getDiagMessageForFeature(
  153. __('Displaying Column Comments'),
  154. 'commwork',
  155. $messages,
  156. false
  157. );
  158. $retval .= PMA_getDiagMessageForFeature(
  159. __('Browser transformation'),
  160. 'mimework',
  161. $messages
  162. );
  163. if ($cfgRelation['commwork'] && ! $cfgRelation['mimework']) {
  164. $retval .= '<tr><td colspan=2 class="left">';
  165. $retval .= __('Please see the documentation on how to update your column_comments table.');
  166. $retval .= '</td></tr>';
  167. }
  168. $retval .= PMA_getDiagMessageForParameter(
  169. 'bookmarktable',
  170. isset($cfgRelation['bookmark']),
  171. $messages,
  172. 'bookmark'
  173. );
  174. $retval .= PMA_getDiagMessageForFeature(
  175. __('Bookmarked SQL query'),
  176. 'bookmarkwork',
  177. $messages
  178. );
  179. $retval .= PMA_getDiagMessageForParameter(
  180. 'history',
  181. isset($cfgRelation['history']),
  182. $messages,
  183. 'history'
  184. );
  185. $retval .= PMA_getDiagMessageForFeature(
  186. __('SQL history'),
  187. 'historywork',
  188. $messages
  189. );
  190. $retval .= PMA_getDiagMessageForParameter(
  191. 'designer_coords',
  192. isset($cfgRelation['designer_coords']),
  193. $messages,
  194. 'designer_coords'
  195. );
  196. $retval .= PMA_getDiagMessageForFeature(
  197. __('Designer'),
  198. 'designerwork',
  199. $messages
  200. );
  201. $retval .= PMA_getDiagMessageForParameter(
  202. 'recent',
  203. isset($cfgRelation['recent']),
  204. $messages,
  205. 'recent'
  206. );
  207. $retval .= PMA_getDiagMessageForFeature(
  208. __('Persistent recently used tables'),
  209. 'recentwork',
  210. $messages
  211. );
  212. $retval .= PMA_getDiagMessageForParameter(
  213. 'table_uiprefs',
  214. isset($cfgRelation['table_uiprefs']),
  215. $messages,
  216. 'table_uiprefs'
  217. );
  218. $retval .= PMA_getDiagMessageForFeature(
  219. __('Persistent tables\' UI preferences'),
  220. 'uiprefswork',
  221. $messages
  222. );
  223. $retval .= PMA_getDiagMessageForParameter(
  224. 'tracking',
  225. isset($cfgRelation['tracking']),
  226. $messages,
  227. 'tracking'
  228. );
  229. $retval .= PMA_getDiagMessageForFeature(
  230. __('Tracking'),
  231. 'trackingwork',
  232. $messages
  233. );
  234. $retval .= PMA_getDiagMessageForParameter(
  235. 'userconfig',
  236. isset($cfgRelation['userconfig']),
  237. $messages,
  238. 'userconfig'
  239. );
  240. $retval .= PMA_getDiagMessageForFeature(
  241. __('User preferences'),
  242. 'userconfigwork',
  243. $messages
  244. );
  245. $retval .= PMA_getDiagMessageForParameter(
  246. 'users',
  247. isset($cfgRelation['users']),
  248. $messages,
  249. 'users'
  250. );
  251. $retval .= PMA_getDiagMessageForParameter(
  252. 'usergroups',
  253. isset($cfgRelation['usergroups']),
  254. $messages,
  255. 'usergroups'
  256. );
  257. $retval .= PMA_getDiagMessageForFeature(
  258. __('Configurable menus'),
  259. 'menuswork',
  260. $messages
  261. );
  262. $retval .= PMA_getDiagMessageForParameter(
  263. 'navigationhiding',
  264. isset($cfgRelation['navigationhiding']),
  265. $messages,
  266. 'navigationhiding'
  267. );
  268. $retval .= PMA_getDiagMessageForFeature(
  269. __('Hide/show navigation items'),
  270. 'navwork',
  271. $messages
  272. );
  273. $retval .= PMA_getDiagMessageForParameter(
  274. 'savedsearches',
  275. isset($cfgRelation['savedsearches']),
  276. $messages,
  277. 'savedsearches'
  278. );
  279. $retval .= PMA_getDiagMessageForFeature(
  280. __('Saving Query-By-Example searches'),
  281. 'savedsearcheswork',
  282. $messages
  283. );
  284. $retval .= '</table>' . "\n";
  285. $retval .= '<p>' . __('Quick steps to setup advanced features:') . '</p>';
  286. $retval .= '<ul>';
  287. $retval .= '<li>';
  288. $retval .= __(
  289. 'Create the needed tables with the '
  290. . '<code>examples/create_tables.sql</code>.'
  291. );
  292. $retval .= ' ' . PMA_Util::showDocu('setup', 'linked-tables');
  293. $retval .= '</li>';
  294. $retval .= '<li>';
  295. $retval .= __('Create a pma user and give access to these tables.');
  296. $retval .= ' ' . PMA_Util::showDocu('config', 'cfg_Servers_controluser');
  297. $retval .= '</li>';
  298. $retval .= '<li>';
  299. $retval .= __(
  300. 'Enable advanced features in configuration file '
  301. . '(<code>config.inc.php</code>), for example by '
  302. . 'starting from <code>config.sample.inc.php</code>.'
  303. );
  304. $retval .= ' ' . PMA_Util::showDocu('setup', 'quick-install');
  305. $retval .= '</li>';
  306. $retval .= '<li>';
  307. $retval .= __(
  308. 'Re-login to phpMyAdmin to load the updated configuration file.'
  309. );
  310. $retval .= '</li>';
  311. $retval .= '</ul>';
  312. }
  313. return $retval;
  314. }
  315. /**
  316. * prints out one diagnostic message for a feature
  317. *
  318. * @param string $feature_name feature name in a message string
  319. * @param string $relation_parameter the $GLOBALS['cfgRelation'] parameter to check
  320. * @param array $messages utility messages
  321. * @param boolean $skip_line whether to skip a line after the message
  322. *
  323. * @return string
  324. */
  325. function PMA_getDiagMessageForFeature($feature_name,
  326. $relation_parameter, $messages, $skip_line = true
  327. ) {
  328. $retval = ' <tr><td colspan=2 class="right">' . $feature_name . ': ';
  329. if ($GLOBALS['cfgRelation'][$relation_parameter]) {
  330. $retval .= $messages['enabled'];
  331. } else {
  332. $retval .= $messages['disabled'];
  333. }
  334. $retval .= '</td></tr>';
  335. if ($skip_line) {
  336. $retval .= '<tr><td>&nbsp;</td></tr>';
  337. }
  338. return $retval;
  339. }
  340. /**
  341. * prints out one diagnostic message for a configuration parameter
  342. *
  343. * @param string $parameter config parameter name to display
  344. * @param boolean $relationParameterSet whether this parameter is set
  345. * @param array $messages utility messages
  346. * @param string $docAnchor anchor in documentation
  347. *
  348. * @return string
  349. */
  350. function PMA_getDiagMessageForParameter($parameter,
  351. $relationParameterSet, $messages, $docAnchor
  352. ) {
  353. $retval = '<tr><th class="left">';
  354. $retval .= '$cfg[\'Servers\'][$i][\'' . $parameter . '\'] ... ';
  355. $retval .= '</th><td class="right">';
  356. if ($relationParameterSet) {
  357. $retval .= $messages['ok'];
  358. } else {
  359. $retval .= sprintf(
  360. $messages['error'],
  361. PMA_Util::getDocuLink('config', 'cfg_Servers_' . $docAnchor)
  362. );
  363. }
  364. $retval .= '</td></tr>' . "\n";
  365. return $retval;
  366. }
  367. /**
  368. * Defines the relation parameters for the current user
  369. * just a copy of the functions used for relations ;-)
  370. * but added some stuff to check what will work
  371. *
  372. * @access protected
  373. * @return array the relation parameters for the current user
  374. */
  375. function PMA_checkRelationsParam()
  376. {
  377. $cfgRelation = array();
  378. $cfgRelation['relwork'] = false;
  379. $cfgRelation['displaywork'] = false;
  380. $cfgRelation['bookmarkwork'] = false;
  381. $cfgRelation['pdfwork'] = false;
  382. $cfgRelation['commwork'] = false;
  383. $cfgRelation['mimework'] = false;
  384. $cfgRelation['historywork'] = false;
  385. $cfgRelation['recentwork'] = false;
  386. $cfgRelation['uiprefswork'] = false;
  387. $cfgRelation['trackingwork'] = false;
  388. $cfgRelation['designerwork'] = false;
  389. $cfgRelation['userconfigwork'] = false;
  390. $cfgRelation['menuswork'] = false;
  391. $cfgRelation['navwork'] = false;
  392. $cfgRelation['allworks'] = false;
  393. $cfgRelation['savedsearcheswork'] = false;
  394. $cfgRelation['user'] = null;
  395. $cfgRelation['db'] = null;
  396. if ($GLOBALS['server'] == 0
  397. || empty($GLOBALS['cfg']['Server']['pmadb'])
  398. || ! $GLOBALS['dbi']->selectDb($GLOBALS['cfg']['Server']['pmadb'], $GLOBALS['controllink'])
  399. ) {
  400. // No server selected -> no bookmark table
  401. // we return the array with the falses in it,
  402. // to avoid some 'Unitialized string offset' errors later
  403. $GLOBALS['cfg']['Server']['pmadb'] = false;
  404. return $cfgRelation;
  405. }
  406. $cfgRelation['user'] = $GLOBALS['cfg']['Server']['user'];
  407. $cfgRelation['db'] = $GLOBALS['cfg']['Server']['pmadb'];
  408. // Now I just check if all tables that i need are present so I can for
  409. // example enable relations but not pdf...
  410. // I was thinking of checking if they have all required columns but I
  411. // fear it might be too slow
  412. $tab_query = 'SHOW TABLES FROM '
  413. . PMA_Util::backquote(
  414. $GLOBALS['cfg']['Server']['pmadb']
  415. );
  416. $tab_rs = PMA_queryAsControlUser(
  417. $tab_query, false, PMA_DatabaseInterface::QUERY_STORE
  418. );
  419. if (! $tab_rs) {
  420. // query failed ... ?
  421. //$GLOBALS['cfg']['Server']['pmadb'] = false;
  422. return $cfgRelation;
  423. }
  424. while ($curr_table = @$GLOBALS['dbi']->fetchRow($tab_rs)) {
  425. if ($curr_table[0] == $GLOBALS['cfg']['Server']['bookmarktable']) {
  426. $cfgRelation['bookmark'] = $curr_table[0];
  427. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['relation']) {
  428. $cfgRelation['relation'] = $curr_table[0];
  429. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_info']) {
  430. $cfgRelation['table_info'] = $curr_table[0];
  431. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_coords']) {
  432. $cfgRelation['table_coords'] = $curr_table[0];
  433. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['designer_coords']) {
  434. $cfgRelation['designer_coords'] = $curr_table[0];
  435. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['column_info']) {
  436. $cfgRelation['column_info'] = $curr_table[0];
  437. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['pdf_pages']) {
  438. $cfgRelation['pdf_pages'] = $curr_table[0];
  439. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['history']) {
  440. $cfgRelation['history'] = $curr_table[0];
  441. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['recent']) {
  442. $cfgRelation['recent'] = $curr_table[0];
  443. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_uiprefs']) {
  444. $cfgRelation['table_uiprefs'] = $curr_table[0];
  445. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['tracking']) {
  446. $cfgRelation['tracking'] = $curr_table[0];
  447. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['userconfig']) {
  448. $cfgRelation['userconfig'] = $curr_table[0];
  449. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['users']) {
  450. $cfgRelation['users'] = $curr_table[0];
  451. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['usergroups']) {
  452. $cfgRelation['usergroups'] = $curr_table[0];
  453. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['navigationhiding']) {
  454. $cfgRelation['navigationhiding'] = $curr_table[0];
  455. } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['savedsearches']) {
  456. $cfgRelation['savedsearches'] = $curr_table[0];
  457. }
  458. } // end while
  459. $GLOBALS['dbi']->freeResult($tab_rs);
  460. if (isset($cfgRelation['relation'])) {
  461. $cfgRelation['relwork'] = true;
  462. if (isset($cfgRelation['table_info'])) {
  463. $cfgRelation['displaywork'] = true;
  464. }
  465. }
  466. if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
  467. $cfgRelation['pdfwork'] = true;
  468. }
  469. if (isset($cfgRelation['column_info'])) {
  470. $cfgRelation['commwork'] = true;
  471. $cfgRelation['mimework'] = true;
  472. }
  473. if (isset($cfgRelation['history'])) {
  474. $cfgRelation['historywork'] = true;
  475. }
  476. if (isset($cfgRelation['recent'])) {
  477. $cfgRelation['recentwork'] = true;
  478. }
  479. if (isset($cfgRelation['table_uiprefs'])) {
  480. $cfgRelation['uiprefswork'] = true;
  481. }
  482. if (isset($cfgRelation['tracking'])) {
  483. $cfgRelation['trackingwork'] = true;
  484. }
  485. if (isset($cfgRelation['userconfig'])) {
  486. $cfgRelation['userconfigwork'] = true;
  487. }
  488. // we do not absolutely need that the internal relations or the PDF
  489. // schema feature be activated
  490. if (isset($cfgRelation['designer_coords'])) {
  491. $cfgRelation['designerwork'] = true;
  492. }
  493. if (isset($cfgRelation['bookmark'])) {
  494. $cfgRelation['bookmarkwork'] = true;
  495. }
  496. if (isset($cfgRelation['users']) && isset($cfgRelation['usergroups'])) {
  497. $cfgRelation['menuswork'] = true;
  498. }
  499. if (isset($cfgRelation['navigationhiding'])) {
  500. $cfgRelation['navwork'] = true;
  501. }
  502. if (isset($cfgRelation['savedsearches'])) {
  503. $cfgRelation['savedsearcheswork'] = true;
  504. }
  505. if ($cfgRelation['relwork'] && $cfgRelation['displaywork']
  506. && $cfgRelation['pdfwork'] && $cfgRelation['commwork']
  507. && $cfgRelation['mimework'] && $cfgRelation['historywork']
  508. && $cfgRelation['recentwork'] && $cfgRelation['uiprefswork']
  509. && $cfgRelation['trackingwork'] && $cfgRelation['userconfigwork']
  510. && $cfgRelation['bookmarkwork'] && $cfgRelation['designerwork']
  511. && $cfgRelation['menuswork'] && $cfgRelation['navwork']
  512. && $cfgRelation['savedsearcheswork']
  513. ) {
  514. $cfgRelation['allworks'] = true;
  515. }
  516. return $cfgRelation;
  517. } // end of the 'PMA_getRelationsParam()' function
  518. /**
  519. * Gets all Relations to foreign tables for a given table or
  520. * optionally a given column in a table
  521. *
  522. * @param string $db the name of the db to check for
  523. * @param string $table the name of the table to check for
  524. * @param string $column the name of the column to check for
  525. * @param string $source the source for foreign key information
  526. *
  527. * @return array db,table,column
  528. *
  529. * @access public
  530. */
  531. function PMA_getForeigners($db, $table, $column = '', $source = 'both')
  532. {
  533. $cfgRelation = PMA_getRelationsParam();
  534. $foreign = array();
  535. if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
  536. $rel_query = '
  537. SELECT `master_field`,
  538. `foreign_db`,
  539. `foreign_table`,
  540. `foreign_field`
  541. FROM ' . PMA_Util::backquote($cfgRelation['db'])
  542. . '.' . PMA_Util::backquote($cfgRelation['relation']) . '
  543. WHERE `master_db` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
  544. AND `master_table` = \'' . PMA_Util::sqlAddSlashes($table) . '\' ';
  545. if (strlen($column)) {
  546. $rel_query .= ' AND `master_field` = '
  547. . '\'' . PMA_Util::sqlAddSlashes($column) . '\'';
  548. }
  549. $foreign = $GLOBALS['dbi']->fetchResult(
  550. $rel_query, 'master_field', null, $GLOBALS['controllink']
  551. );
  552. }
  553. if (($source == 'both' || $source == 'foreign') && strlen($table)) {
  554. $showCreateTableQuery = 'SHOW CREATE TABLE '
  555. . PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table);
  556. $show_create_table = $GLOBALS['dbi']->fetchValue(
  557. $showCreateTableQuery, 0, 1
  558. );
  559. $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
  560. foreach ($analyzed_sql[0]['foreign_keys'] as $one_key) {
  561. // The analyzer may return more than one column name in the
  562. // index list or the ref_index_list; if this happens,
  563. // the current logic just discards the whole index; having
  564. // more than one index field is currently unsupported (see FAQ 3.6)
  565. if (count($one_key['index_list']) == 1) {
  566. foreach ($one_key['index_list'] as $i => $field) {
  567. // If a foreign key is defined in the 'internal' source (pmadb)
  568. // and as a native foreign key, we won't get it twice
  569. // if $source='both' because we use $field as key
  570. // The parser looks for a CONSTRAINT clause just before
  571. // the FOREIGN KEY clause. It finds it (as output from
  572. // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
  573. // versions like 3.23.58.
  574. // In those cases, the FOREIGN KEY parsing will put numbers
  575. // like -1, 0, 1... instead of the constraint number.
  576. if (isset($one_key['constraint'])) {
  577. $foreign[$field]['constraint'] = $one_key['constraint'];
  578. }
  579. if (isset($one_key['ref_db_name'])) {
  580. $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
  581. } else {
  582. $foreign[$field]['foreign_db'] = $db;
  583. }
  584. $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
  585. $foreign[$field]['foreign_field']
  586. = $one_key['ref_index_list'][$i];
  587. if (isset($one_key['on_delete'])) {
  588. $foreign[$field]['on_delete'] = $one_key['on_delete'];
  589. }
  590. if (isset($one_key['on_update'])) {
  591. $foreign[$field]['on_update'] = $one_key['on_update'];
  592. }
  593. }
  594. }
  595. }
  596. }
  597. /**
  598. * Emulating relations for some information_schema and data_dictionary tables
  599. */
  600. $isInformationSchema = strtolower($db) == 'information_schema';
  601. $is_data_dictionary = PMA_DRIZZLE && strtolower($db) == 'data_dictionary';
  602. if (($isInformationSchema || $is_data_dictionary)
  603. && ($source == 'internal' || $source == 'both')
  604. ) {
  605. if ($isInformationSchema) {
  606. $relations_key = 'information_schema_relations';
  607. include_once './libraries/information_schema_relations.lib.php';
  608. } else {
  609. $relations_key = 'data_dictionary_relations';
  610. include_once './libraries/data_dictionary_relations.lib.php';
  611. }
  612. if (isset($GLOBALS[$relations_key][$table])) {
  613. foreach ($GLOBALS[$relations_key][$table] as $field => $relations) {
  614. if ((! strlen($column) || $column == $field)
  615. && (! isset($foreign[$field]) || ! strlen($foreign[$field]))
  616. ) {
  617. $foreign[$field] = $relations;
  618. }
  619. }
  620. }
  621. }
  622. return $foreign;
  623. } // end of the 'PMA_getForeigners()' function
  624. /**
  625. * Gets the display field of a table
  626. *
  627. * @param string $db the name of the db to check for
  628. * @param string $table the name of the table to check for
  629. *
  630. * @return string field name
  631. *
  632. * @access public
  633. */
  634. function PMA_getDisplayField($db, $table)
  635. {
  636. $cfgRelation = PMA_getRelationsParam();
  637. /**
  638. * Try to fetch the display field from DB.
  639. */
  640. if ($cfgRelation['displaywork']) {
  641. $disp_query = '
  642. SELECT `display_field`
  643. FROM ' . PMA_Util::backquote($cfgRelation['db'])
  644. . '.' . PMA_Util::backquote($cfgRelation['table_info']) . '
  645. WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
  646. AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'';
  647. $row = $GLOBALS['dbi']->fetchSingleRow(
  648. $disp_query, 'ASSOC', $GLOBALS['controllink']
  649. );
  650. if (isset($row['display_field'])) {
  651. return $row['display_field'];
  652. }
  653. }
  654. /**
  655. * Emulating the display field for some information_schema tables.
  656. */
  657. if ($db == 'information_schema') {
  658. switch ($table) {
  659. case 'CHARACTER_SETS':
  660. return 'DESCRIPTION';
  661. case 'TABLES':
  662. return 'TABLE_COMMENT';
  663. }
  664. }
  665. /**
  666. * No Luck...
  667. */
  668. return false;
  669. } // end of the 'PMA_getDisplayField()' function
  670. /**
  671. * Gets the comments for all columns of a table or the db itself
  672. *
  673. * @param string $db the name of the db to check for
  674. * @param string $table the name of the table to check for
  675. *
  676. * @return array [column_name] = comment
  677. *
  678. * @access public
  679. */
  680. function PMA_getComments($db, $table = '')
  681. {
  682. $comments = array();
  683. if ($table != '') {
  684. // MySQL native column comments
  685. $columns = $GLOBALS['dbi']->getColumns($db, $table, null, true);
  686. if ($columns) {
  687. foreach ($columns as $column) {
  688. if (! empty($column['Comment'])) {
  689. $comments[$column['Field']] = $column['Comment'];
  690. }
  691. }
  692. }
  693. } else {
  694. $comments[] = PMA_getDbComment($db);
  695. }
  696. return $comments;
  697. } // end of the 'PMA_getComments()' function
  698. /**
  699. * Gets the comment for a db
  700. *
  701. * @param string $db the name of the db to check for
  702. *
  703. * @return string comment
  704. *
  705. * @access public
  706. */
  707. function PMA_getDbComment($db)
  708. {
  709. $cfgRelation = PMA_getRelationsParam();
  710. $comment = '';
  711. if ($cfgRelation['commwork']) {
  712. // pmadb internal db comment
  713. $com_qry = "
  714. SELECT `comment`
  715. FROM " . PMA_Util::backquote($cfgRelation['db'])
  716. . "." . PMA_Util::backquote($cfgRelation['column_info']) . "
  717. WHERE db_name = '" . PMA_Util::sqlAddSlashes($db) . "'
  718. AND table_name = ''
  719. AND column_name = '(db_comment)'";
  720. $com_rs = PMA_queryAsControlUser(
  721. $com_qry, true, PMA_DatabaseInterface::QUERY_STORE
  722. );
  723. if ($com_rs && $GLOBALS['dbi']->numRows($com_rs) > 0) {
  724. $row = $GLOBALS['dbi']->fetchAssoc($com_rs);
  725. $comment = $row['comment'];
  726. }
  727. $GLOBALS['dbi']->freeResult($com_rs);
  728. }
  729. return $comment;
  730. } // end of the 'PMA_getDbComment()' function
  731. /**
  732. * Gets the comment for a db
  733. *
  734. * @access public
  735. *
  736. * @return string comment
  737. */
  738. function PMA_getDbComments()
  739. {
  740. $cfgRelation = PMA_getRelationsParam();
  741. $comments = array();
  742. if ($cfgRelation['commwork']) {
  743. // pmadb internal db comment
  744. $com_qry = "
  745. SELECT `db_name`, `comment`
  746. FROM " . PMA_Util::backquote($cfgRelation['db'])
  747. . "." . PMA_Util::backquote($cfgRelation['column_info']) . "
  748. WHERE `column_name` = '(db_comment)'";
  749. $com_rs = PMA_queryAsControlUser(
  750. $com_qry, true, PMA_DatabaseInterface::QUERY_STORE
  751. );
  752. if ($com_rs && $GLOBALS['dbi']->numRows($com_rs) > 0) {
  753. while ($row = $GLOBALS['dbi']->fetchAssoc($com_rs)) {
  754. $comments[$row['db_name']] = $row['comment'];
  755. }
  756. }
  757. $GLOBALS['dbi']->freeResult($com_rs);
  758. }
  759. return $comments;
  760. } // end of the 'PMA_getDbComments()' function
  761. /**
  762. * Set a database comment to a certain value.
  763. *
  764. * @param string $db the name of the db
  765. * @param string $comment the value of the column
  766. *
  767. * @return boolean true, if comment-query was made.
  768. *
  769. * @access public
  770. */
  771. function PMA_setDbComment($db, $comment = '')
  772. {
  773. $cfgRelation = PMA_getRelationsParam();
  774. if (! $cfgRelation['commwork']) {
  775. return false;
  776. }
  777. if (strlen($comment)) {
  778. $upd_query = 'INSERT INTO '
  779. . PMA_Util::backquote($cfgRelation['db']) . '.'
  780. . PMA_Util::backquote($cfgRelation['column_info'])
  781. . ' (`db_name`, `table_name`, `column_name`, `comment`)'
  782. . ' VALUES (\''
  783. . PMA_Util::sqlAddSlashes($db)
  784. . "', '', '(db_comment)', '"
  785. . PMA_Util::sqlAddSlashes($comment)
  786. . "') "
  787. . ' ON DUPLICATE KEY UPDATE '
  788. . "`comment` = '" . PMA_Util::sqlAddSlashes($comment) . "'";
  789. } else {
  790. $upd_query = 'DELETE FROM '
  791. . PMA_Util::backquote($cfgRelation['db']) . '.'
  792. . PMA_Util::backquote($cfgRelation['column_info'])
  793. . ' WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
  794. AND `table_name` = \'\'
  795. AND `column_name` = \'(db_comment)\'';
  796. }
  797. if (isset($upd_query)) {
  798. return PMA_queryAsControlUser($upd_query);
  799. }
  800. return false;
  801. } // end of 'PMA_setDbComment()' function
  802. /**
  803. * Set a SQL history entry
  804. *
  805. * @param string $db the name of the db
  806. * @param string $table the name of the table
  807. * @param string $username the username
  808. * @param string $sqlquery the sql query
  809. *
  810. * @return void
  811. *
  812. * @access public
  813. */
  814. function PMA_setHistory($db, $table, $username, $sqlquery)
  815. {
  816. // Prevent to run this automatically on Footer class destroying in testsuite
  817. if (defined('TESTSUITE')
  818. || strlen($sqlquery) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']
  819. ) {
  820. return;
  821. }
  822. $cfgRelation = PMA_getRelationsParam();
  823. if (! isset($_SESSION['sql_history'])) {
  824. $_SESSION['sql_history'] = array();
  825. }
  826. $key = md5($sqlquery . $db . $table);
  827. if (isset($_SESSION['sql_history'][$key])) {
  828. unset($_SESSION['sql_history'][$key]);
  829. }
  830. $_SESSION['sql_history'][$key] = array(
  831. 'db' => $db,
  832. 'table' => $table,
  833. 'sqlquery' => $sqlquery,
  834. );
  835. if (count($_SESSION['sql_history']) > $GLOBALS['cfg']['QueryHistoryMax']) {
  836. // history should not exceed a maximum count
  837. array_shift($_SESSION['sql_history']);
  838. }
  839. if (! $cfgRelation['historywork'] || ! $GLOBALS['cfg']['QueryHistoryDB']) {
  840. return;
  841. }
  842. PMA_queryAsControlUser(
  843. 'INSERT INTO '
  844. . PMA_Util::backquote($cfgRelation['db']) . '.'
  845. . PMA_Util::backquote($cfgRelation['history']) . '
  846. (`username`,
  847. `db`,
  848. `table`,
  849. `timevalue`,
  850. `sqlquery`)
  851. VALUES
  852. (\'' . PMA_Util::sqlAddSlashes($username) . '\',
  853. \'' . PMA_Util::sqlAddSlashes($db) . '\',
  854. \'' . PMA_Util::sqlAddSlashes($table) . '\',
  855. NOW(),
  856. \'' . PMA_Util::sqlAddSlashes($sqlquery) . '\')'
  857. );
  858. } // end of 'PMA_setHistory()' function
  859. /**
  860. * Gets a SQL history entry
  861. *
  862. * @param string $username the username
  863. *
  864. * @return array list of history items
  865. *
  866. * @access public
  867. */
  868. function PMA_getHistory($username)
  869. {
  870. $cfgRelation = PMA_getRelationsParam();
  871. /**
  872. * if db-based history is disabled but there exists a session-based
  873. * history, use it
  874. */
  875. if (! $GLOBALS['cfg']['QueryHistoryDB'] && isset($_SESSION['sql_history'])) {
  876. return array_reverse($_SESSION['sql_history']);
  877. }
  878. if (! $cfgRelation['historywork']) {
  879. return false;
  880. }
  881. $hist_query = '
  882. SELECT `db`,
  883. `table`,
  884. `sqlquery`
  885. FROM ' . PMA_Util::backquote($cfgRelation['db'])
  886. . '.' . PMA_Util::backquote($cfgRelation['history']) . '
  887. WHERE `username` = \'' . PMA_Util::sqlAddSlashes($username) . '\'
  888. ORDER BY `id` DESC';
  889. return $GLOBALS['dbi']->fetchResult(
  890. $hist_query, null, null, $GLOBALS['controllink']
  891. );
  892. } // end of 'PMA_getHistory()' function
  893. /**
  894. * purges SQL history
  895. *
  896. * deletes entries that exceeds $cfg['QueryHistoryMax'], oldest first, for the
  897. * given user
  898. *
  899. * @param string $username the username
  900. *
  901. * @return void
  902. *
  903. * @access public
  904. */
  905. function PMA_purgeHistory($username)
  906. {
  907. $cfgRelation = PMA_getRelationsParam();
  908. if (! $GLOBALS['cfg']['QueryHistoryDB'] || ! $cfgRelation['historywork']) {
  909. return;
  910. }
  911. if (! $cfgRelation['historywork']) {
  912. return;
  913. }
  914. $search_query = '
  915. SELECT `timevalue`
  916. FROM ' . PMA_Util::backquote($cfgRelation['db'])
  917. . '.' . PMA_Util::backquote($cfgRelation['history']) . '
  918. WHERE `username` = \'' . PMA_Util::sqlAddSlashes($username) . '\'
  919. ORDER BY `timevalue` DESC
  920. LIMIT ' . $GLOBALS['cfg']['QueryHistoryMax'] . ', 1';
  921. if ($max_time = $GLOBALS['dbi']->fetchValue($search_query, 0, 0, $GLOBALS['controllink'])) {
  922. PMA_queryAsControlUser(
  923. 'DELETE FROM '
  924. . PMA_Util::backquote($cfgRelation['db']) . '.'
  925. . PMA_Util::backquote($cfgRelation['history']) . '
  926. WHERE `username` = \'' . PMA_Util::sqlAddSlashes($username) . '\'
  927. AND `timevalue` <= \'' . $max_time . '\''
  928. );
  929. }
  930. } // end of 'PMA_purgeHistory()' function
  931. /**
  932. * Prepares the dropdown for one mode
  933. *
  934. * @param array $foreign the keys and values for foreigns
  935. * @param string $data the current data of the dropdown
  936. * @param string $mode the needed mode
  937. *
  938. * @return array the <option value=""><option>s
  939. *
  940. * @access protected
  941. */
  942. function PMA_buildForeignDropdown($foreign, $data, $mode)
  943. {
  944. $reloptions = array();
  945. // id-only is a special mode used when no foreign display column
  946. // is available
  947. if ($mode == 'id-content' || $mode == 'id-only') {
  948. // sort for id-content
  949. if ($GLOBALS['cfg']['NaturalOrder']) {
  950. uksort($foreign, 'strnatcasecmp');
  951. } else {
  952. ksort($foreign);
  953. }
  954. } elseif ($mode == 'content-id') {
  955. // sort for content-id
  956. if ($GLOBALS['cfg']['NaturalOrder']) {
  957. natcasesort($foreign);
  958. } else {
  959. asort($foreign);
  960. }
  961. }
  962. foreach ($foreign as $key => $value) {
  963. if ($GLOBALS['PMA_String']->strlen($value) <= $GLOBALS['cfg']['LimitChars']
  964. ) {
  965. $vtitle = '';
  966. $value = htmlspecialchars($value);
  967. } else {
  968. $vtitle = htmlspecialchars($value);
  969. $value = htmlspecialchars(
  970. substr($value, 0, $GLOBALS['cfg']['LimitChars']) . '...'
  971. );
  972. }
  973. $reloption = '<option value="' . htmlspecialchars($key) . '"';
  974. if ($vtitle != '') {
  975. $reloption .= ' title="' . $vtitle . '"';
  976. }
  977. if ((string) $key == (string) $data) {
  978. $reloption .= ' selected="selected"';
  979. }
  980. if ($mode == 'content-id') {
  981. $reloptions[] = $reloption . '>'
  982. . $value . '&nbsp;-&nbsp;' . htmlspecialchars($key) . '</option>';
  983. } elseif ($mode == 'id-content') {
  984. $reloptions[] = $reloption . '>'
  985. . htmlspecialchars($key) . '&nbsp;-&nbsp;' . $value . '</option>';
  986. } elseif ($mode == 'id-only') {
  987. $reloptions[] = $reloption . '>'
  988. . htmlspecialchars($key) . '</option>';
  989. }
  990. } // end foreach
  991. return $reloptions;
  992. } // end of 'PMA_buildForeignDropdown' function
  993. /**
  994. * Outputs dropdown with values of foreign fields
  995. *
  996. * @param array $disp_row array of the displayed row
  997. * @param string $foreign_field the foreign field
  998. * @param string $foreign_display the foreign field to display
  999. * @param string $data the current data of the dropdown (field in row)
  1000. * @param int $max maximum number of items in the dropdown
  1001. *
  1002. * @return string the <option value=""><option>s
  1003. *
  1004. * @access public
  1005. */
  1006. function PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, $data,
  1007. $max = null
  1008. ) {
  1009. if (null === $max) {
  1010. $max = $GLOBALS['cfg']['ForeignKeyMaxLimit'];
  1011. }
  1012. $foreign = array();
  1013. // collect the data
  1014. foreach ($disp_row as $relrow) {
  1015. $key = $relrow[$foreign_field];
  1016. // if the display field has been defined for this foreign table
  1017. if ($foreign_display) {
  1018. $value = $relrow[$foreign_display];
  1019. } else {
  1020. $value = '';
  1021. } // end if ($foreign_display)
  1022. $foreign[$key] = $value;
  1023. } // end foreach
  1024. // put the dropdown sections in correct order
  1025. $top = array();
  1026. $bottom = array();
  1027. if ($foreign_display) {
  1028. if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'], 'array')) {
  1029. if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][0])) {
  1030. $top = PMA_buildForeignDropdown(
  1031. $foreign,
  1032. $data,
  1033. $GLOBALS['cfg']['ForeignKeyDropdownOrder'][0]
  1034. );
  1035. }
  1036. if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][1])) {
  1037. $bottom = PMA_buildForeignDropdown(
  1038. $foreign,
  1039. $data,
  1040. $GLOBALS['cfg']['ForeignKeyDropdownOrder'][1]
  1041. );
  1042. }
  1043. } else {
  1044. $top = PMA_buildForeignDropdown($foreign, $data, 'id-content');
  1045. $bottom = PMA_buildForeignDropdown($foreign, $data, 'content-id');
  1046. }
  1047. } else {
  1048. $top = PMA_buildForeignDropdown($foreign, $data, 'id-only');
  1049. }
  1050. // beginning of dropdown
  1051. $ret = '<option value="">&nbsp;</option>';
  1052. $top_count = count($top);
  1053. if ($max == -1 || $top_count < $max) {
  1054. $ret .= implode('', $top);
  1055. if ($foreign_display && $top_count > 0) {
  1056. // this empty option is to visually mark the beginning of the
  1057. // second series of values (bottom)
  1058. $ret .= '<option value="">&nbsp;</option>';
  1059. }
  1060. }
  1061. if ($foreign_display) {
  1062. $ret .= implode('', $bottom);
  1063. }
  1064. return $ret;
  1065. } // end of 'PMA_foreignDropdown()' function
  1066. /**
  1067. * Gets foreign keys in preparation for a drop-down selector
  1068. *
  1069. * @param array $foreigners array of the foreign keys
  1070. * @param string $field the foreign field name
  1071. * @param bool $override_total whether to override the total
  1072. * @param string $foreign_filter a possible filter
  1073. * @param string $foreign_limit a possible LIMIT clause
  1074. *
  1075. * @return array data about the foreign keys
  1076. *
  1077. * @access public
  1078. */
  1079. function PMA_getForeignData(
  1080. $foreigners, $field, $override_total, $foreign_filter, $foreign_limit
  1081. ) {
  1082. // we always show the foreign field in the drop-down; if a display
  1083. // field is defined, we show it besides the foreign field
  1084. $foreign_link = false;
  1085. if ($foreigners && isset($foreigners[$field])) {
  1086. $foreigner = $foreigners[$field];
  1087. $foreign_db = $foreigner['foreign_db'];
  1088. $foreign_table = $foreigner['foreign_table'];
  1089. $foreign_field = $foreigner['foreign_field'];
  1090. // Count number of rows in the foreign table. Currently we do
  1091. // not use a drop-down if more than ForeignKeyMaxLimit rows in the
  1092. // foreign table,
  1093. // for speed reasons and because we need a better interface for this.
  1094. //
  1095. // We could also do the SELECT anyway, with a LIMIT, and ensure that
  1096. // the current value of the field is one of the choices.
  1097. $the_total = PMA_Table::countRecords($foreign_db, $foreign_table, true);
  1098. if ($override_total == true
  1099. || $the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']
  1100. ) {
  1101. // foreign_display can be false if no display field defined:
  1102. $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
  1103. $f_query_main = 'SELECT ' . PMA_Util::backquote($foreign_field)
  1104. . (
  1105. ($foreign_display == false)
  1106. ? ''
  1107. : ', ' . PMA_Util::backquote($foreign_display)
  1108. );
  1109. $f_query_from = ' FROM ' . PMA_Util::backquote($foreign_db)
  1110. . '.' . PMA_Util::backquote($foreign_table);
  1111. $f_query_filter = empty($foreign_filter) ? '' : ' WHERE '
  1112. . PMA_Util::backquote($foreign_field)
  1113. . ' LIKE "%' . PMA_Util::sqlAddSlashes($foreign_filter, true) . '%"'
  1114. . (
  1115. ($foreign_display == false)
  1116. ? ''
  1117. : ' OR ' . PMA_Util::backquote($foreign_display)
  1118. . ' LIKE "%' . PMA_Util::sqlAddSlashes($foreign_filter, true)
  1119. . '%"'
  1120. );
  1121. $f_query_order = ($foreign_display == false) ? '' :' ORDER BY '
  1122. . PMA_Util::backquote($foreign_table) . '.'
  1123. . PMA_Util::backquote($foreign_display);
  1124. $f_query_limit = isset($foreign_limit) ? $foreign_limit : '';
  1125. if (!empty($foreign_filter)) {
  1126. $res = $GLOBALS['dbi']->query(
  1127. 'SELECT COUNT(*)' . $f_query_from . $f_query_filter
  1128. );
  1129. if ($res) {
  1130. $the_total = $GLOBALS['dbi']->fetchValue($res);
  1131. @$GLOBALS['dbi']->freeResult($res);
  1132. } else {
  1133. $the_total = 0;
  1134. }
  1135. }
  1136. $disp = $GLOBALS['dbi']->query(
  1137. $f_query_main . $f_query_from . $f_query_filter
  1138. . $f_query_order . $f_query_limit
  1139. );
  1140. if ($disp && $GLOBALS['dbi']->numRows($disp) > 0) {
  1141. // If a resultset has been created, pre-cache it in the $disp_row
  1142. // array. This helps us from not needing to use mysql_data_seek by
  1143. // accessing a pre-cached PHP array. Usually those resultsets are
  1144. // not that big, so a performance hit should not be expected.
  1145. $disp_row = array();
  1146. while ($single_disp_row = @$GLOBALS['dbi']->fetchAssoc($disp)) {
  1147. $disp_row[] = $single_disp_row;
  1148. }
  1149. @$GLOBALS['dbi']->freeResult($disp);
  1150. }
  1151. } else {
  1152. $disp_row = null;
  1153. $foreign_link = true;
  1154. }
  1155. } // end if $foreigners
  1156. $foreignData = array();
  1157. $foreignData['foreign_link'] = $foreign_link;
  1158. $foreignData['the_total'] = isset($the_total) ? $the_total : null;
  1159. $foreignData['foreign_display'] = (
  1160. isset($foreign_display) ? $foreign_display : null
  1161. );
  1162. $foreignData['disp_row'] = isset($disp_row) ? $disp_row : null;
  1163. $foreignData['foreign_field'] = isset($foreign_field) ? $foreign_field : null;
  1164. return $foreignData;
  1165. } // end of 'PMA_getForeignData()' function
  1166. /**
  1167. * Finds all related tables
  1168. *
  1169. * @param array $all_tables All the involved tables
  1170. * @param string $master The master table to form the LEFT JOIN clause
  1171. *
  1172. * @return string LEFT JOIN
  1173. * @access private
  1174. */
  1175. function PMA_getRelatives($all_tables, $master)
  1176. {
  1177. $fromclause = '';
  1178. $emerg = '';
  1179. // The list of tables that we still couldn't connect
  1180. $remaining_tables = $all_tables;
  1181. unset($remaining_tables[$master]);
  1182. // The list of allready connected tables
  1183. $known_tables = array();
  1184. $known_tables[$master] = $master;
  1185. $run = 0;
  1186. while (count($remaining_tables) > 0) {
  1187. // Whether to go from master to foreign or vice versa
  1188. if ($run % 2 == 0) {
  1189. $from = 'master';
  1190. $to = 'foreign';
  1191. } else {
  1192. $from = 'foreign';
  1193. $to = 'master';
  1194. }
  1195. $in_know = '(\'' . implode('\', \'', $known_tables) . '\')';
  1196. $in_left = '(\'' . implode('\', \'', $remaining_tables) . '\')';
  1197. $rel_query = 'SELECT *'
  1198. . ' FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
  1199. . '.' . PMA_Util::backquote($GLOBALS['cfgRelation']['relation'])
  1200. . ' WHERE ' . $from . '_db = \''
  1201. . PMA_Util::sqlAddSlashes($GLOBALS['db']) . '\''
  1202. . ' AND ' . $to . '_db = \''
  1203. . PMA_Util::sqlAddSlashes($GLOBALS['db']) . '\''
  1204. . ' AND ' . $from . '_table IN ' . $in_know
  1205. . ' AND ' . $to . '_table IN ' . $in_left;
  1206. $relations = @$GLOBALS['dbi']->query($rel_query, $GLOBALS['controllink']);
  1207. while ($row = $GLOBALS['dbi']->fetchAssoc($relations)) {
  1208. $found_table = $row[$to . '_table'];
  1209. if (isset($remaining_tables[$found_table])) {
  1210. $fromclause
  1211. .= "\n" . ' LEFT JOIN '
  1212. . PMA_Util::backquote($GLOBALS['db']) . '.'
  1213. . PMA_Util::backquote($row[$to . '_table']) . ' ON '
  1214. . PMA_Util::backquote($row[$from . '_table']) . '.'
  1215. . PMA_Util::backquote($row[$from . '_field']) . ' = '
  1216. . PMA_Util::backquote($row[$to . '_table']) . '.'
  1217. . PMA_Util::backquote($row[$to . '_field']) . ' ';
  1218. $known_tables[$found_table] = $found_table;
  1219. unset($remaining_tables[$found_table]);
  1220. }
  1221. } // end while
  1222. $run++;
  1223. if ($run > 5) {
  1224. foreach ($remaining_tables as $table) {
  1225. $emerg .= ', ' . PMA_Util::backquote($table);
  1226. unset($remaining_tables[$table]);
  1227. }
  1228. }
  1229. } // end while
  1230. $fromclause = $emerg . $fromclause;
  1231. return $fromclause;
  1232. } // end of the "PMA_getRelatives()" function
  1233. /**
  1234. * Rename a field in relation tables
  1235. *
  1236. * usually called after a column in a table was renamed
  1237. *
  1238. * @param string $db database name
  1239. * @param string $table table name
  1240. * @param string $field old field name
  1241. * @param string $new_name new field name
  1242. *
  1243. * @return void
  1244. */
  1245. function PMA_REL_renameField($db, $table, $field, $new_name)
  1246. {
  1247. $cfgRelation = PMA_getRelationsParam();
  1248. if ($cfgRelation['displaywork']) {
  1249. $table_query = 'UPDATE '
  1250. . PMA_Util::backquote($cfgRelation['db']) . '.'
  1251. . PMA_Util::backquote($cfgRelation['table_info'])
  1252. . ' SET display_field = \'' . PMA_Util::sqlAddSlashes($new_name) . '\''
  1253. . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
  1254. . ' AND table_name = \'' . PMA_Util::sqlAddSlashes($table) . '\''
  1255. . ' AND display_field = \'' . PMA_Util::sqlAddSlashes($field) . '\'';
  1256. PMA_queryAsControlUser($table_query);
  1257. }
  1258. if ($cfgRelation['relwork']) {
  1259. $table_query = 'UPDATE '
  1260. . PMA_Util::backquote($cfgRelation['db']) . '.'
  1261. . PMA_Util::backquote($cfgRelation['relation'])
  1262. . ' SET master_field = \'' . PMA_Util::sqlAddSlashes($new_name) . '\''
  1263. . ' WHERE master_db = \'' . PMA_Util::sqlAddSlashes($db) . '\''
  1264. . ' AND master_table = \'' . PMA_Util::sqlAddSlashes($table) . '\''
  1265. . ' AND master_field = \'' . PMA_Util::sqlAddSlashes($field) . '\'';
  1266. PMA_queryAsControlUser($table_query);
  1267. $table_query = 'UPDATE '
  1268. . PMA_Util::backquote($cfgRelation['db']) . '.'
  1269. . PMA_Util::backquote($cfgRelation['relation'])
  1270. . ' SET foreign_field = \'' . PMA_Util::sqlAddSlashes($new_name) . '\''
  1271. . ' WHERE foreign_db = \'' . PMA_Util::sqlAddSlashes($db) . '\''
  1272. . ' AND foreign_table = \'' . PMA_Util::sqlAddSlashes($table) . '\''
  1273. . ' AND foreign_field = \'' . PMA_Util::sqlAddSlashes($field) . '\'';
  1274. PMA_queryAsControlUser($table_query);
  1275. } // end if relwork
  1276. }
  1277. /**
  1278. * Performs SQL query used for renaming table.
  1279. *
  1280. * @param string $table Relation table to use
  1281. * @param string $source_db Source database name
  1282. * @param string $target_db Target database name
  1283. * @param string $source_table Source table name
  1284. * @param string $target_table Target table name
  1285. * @param string $db_field Name of database field
  1286. * @param string $table_field Name of table field
  1287. *
  1288. * @return void
  1289. */
  1290. function PMA_REL_renameSingleTable($table,
  1291. $source_db, $target_db,
  1292. $source_table, $target_table,
  1293. $db_field, $table_field
  1294. ) {
  1295. $query = 'UPDATE '
  1296. . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
  1297. . PMA_Util::backquote($GLOBALS['cfgRelation'][$table])
  1298. . ' SET '
  1299. . $db_field . ' = \'' . PMA_Util::sqlAddSlashes($target_db) . '\', '
  1300. . $table_field . ' = \'' . PMA_Util::sqlAddSlashes($target_table) . '\''
  1301. . ' WHERE '
  1302. . $db_field . ' = \'' . PMA_Util::sqlAddSlashes($source_db) . '\''
  1303. . ' AND '
  1304. . $table_field . ' = \'' . PMA_Util::sqlAddSlashes($source_table) . '\'';
  1305. PMA_queryAsControlUser($query);
  1306. }
  1307. /**
  1308. * Rename a table in relation tables
  1309. *
  1310. * usually called after table has been moved
  1311. *
  1312. * @param string $source_db Source database name
  1313. * @param string $target_db Target database name
  1314. * @param string $source_table Source table name
  1315. * @param string $target_table Target table name
  1316. *
  1317. * @return void
  1318. */
  1319. function PMA_REL_renameTable($source_db, $target_db, $source_table, $target_table)
  1320. {
  1321. // Move old entries from PMA-DBs to new table
  1322. if ($GLOBALS['cfgRelation']['commwork']) {
  1323. PMA_REL_renameSingleTable(
  1324. 'column_info',
  1325. $source_db, $target_db,
  1326. $source_table, $target_table,
  1327. 'db_name', 'table_name'
  1328. );
  1329. }
  1330. // updating bookmarks is not possible since only a single table is
  1331. // moved, and not the whole DB.
  1332. if ($GLOBALS['cfgRelation']['displaywork']) {
  1333. PMA_REL_renameSingleTable(
  1334. 'table_info',
  1335. $source_db, $target_db,
  1336. $source_table, $target_table,
  1337. 'db_name', 'table_name'
  1338. );
  1339. }
  1340. if ($GLOBALS['cfgRelation']['relwork']) {
  1341. PMA_REL_renameSingleTable(
  1342. 'relation',
  1343. $source_db, $target_db,
  1344. $source_table, $target_table,
  1345. 'foreign_db', 'foreign_table'
  1346. );
  1347. PMA_REL_renameSingleTable(
  1348. 'relation',
  1349. $source_db, $target_db,
  1350. $source_table, $target_table,
  1351. 'master_db', 'master_table'
  1352. );
  1353. }
  1354. /**
  1355. * @todo Can't get moving PDFs the right way. The page numbers
  1356. * always get screwed up independently from duplication because the
  1357. * numbers do not seem to be stored on a per-database basis. Would
  1358. * the author of pdf support please have a look at it?
  1359. */
  1360. if ($GLOBALS['cfgRelation']['pdfwork']) {
  1361. PMA_REL_renameSingleTable(
  1362. 'table_coords',
  1363. $source_db, $target_db,
  1364. $source_table, $target_table,
  1365. 'db_name', 'table_name'
  1366. );
  1367. }
  1368. if ($GLOBALS['cfgRelation']['designerwork']) {
  1369. PMA_REL_renameSingleTable(
  1370. 'designer_coords',
  1371. $source_db, $target_db,
  1372. $source_table, $target_table,
  1373. 'db_name', 'table_name'
  1374. );
  1375. }
  1376. }
  1377. /**
  1378. * Create a PDF page
  1379. *
  1380. * @param string $newpage name of the new PDF page
  1381. * @param array $cfgRelation Relation configuration
  1382. * @param string $db database name
  1383. *
  1384. * @return string $pdf_page_number
  1385. */
  1386. function PMA_REL_createPage($newpage, $cfgRelation, $db)
  1387. {
  1388. if (! isset($newpage) || $newpage == '') {
  1389. $newpage = __('no description');
  1390. }
  1391. $ins_query = 'INSERT INTO '
  1392. . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
  1393. . PMA_Util::backquote($cfgRelation['pdf_pages'])
  1394. . ' (db_name, page_descr)'
  1395. . ' VALUES (\''
  1396. . PMA_Util::sqlAddSlashes($db) . '\', \''
  1397. . PMA_Util::sqlAddSlashes($newpage) . '\')';
  1398. PMA_queryAsControlUser($ins_query, false);
  1399. return $GLOBALS['dbi']->insertId(
  1400. isset($GLOBALS['controllink']) ? $GLOBALS['controllink'] : ''
  1401. );
  1402. }
  1403. /**
  1404. * Get child table references for a table column.
  1405. *
  1406. * @param string $db name of master table db.
  1407. * @param string $table name of master table.
  1408. * @param string $column name of master table column.
  1409. *
  1410. * @return array $child_references
  1411. */
  1412. function PMA_getChildReferences($db, $table, $column)
  1413. {
  1414. $child_references = array();
  1415. $i=0;
  1416. $rel_query = 'SELECT `column_name`,'
  1417. . ' `table_name`,'
  1418. . '`table_schema`'
  1419. . ' FROM `information_schema`.`key_column_usage`'
  1420. . ' WHERE `referenced_column_name` = \''
  1421. . PMA_Util::sqlAddSlashes($column) . '\''
  1422. . ' AND `referenced_table_name` = \''
  1423. . PMA_Util::sqlAddSlashes($table) . '\''
  1424. . ' AND `referenced_table_schema` = \''
  1425. . PMA_Util::sqlAddSlashes($db) . '\'';
  1426. $result = $GLOBALS['dbi']->tryQuery($rel_query, $GLOBALS['controllink']);
  1427. if ($result == true) {
  1428. while (($row = $GLOBALS['dbi']->fetchAssoc($result))) {
  1429. $child_references[$i++] = $row;
  1430. }
  1431. }
  1432. return $child_references;
  1433. }
  1434. /**
  1435. * Check child table references and foreign key for a table column.
  1436. *
  1437. * @param string $db name of master table db.
  1438. * @param string $table name of master table.
  1439. * @param string $column name of master table column.
  1440. *
  1441. * @return array $column_status telling about references if foreign key.
  1442. */
  1443. function PMA_checkChildForeignReferences($db, $table, $column)
  1444. {
  1445. $column_status = array();
  1446. $column_status['isEditable'] = false;
  1447. $column_status['isReferenced'] = false;
  1448. $column_status['isForeignKey'] = false;
  1449. $column_status['references'] = array();
  1450. $foreigners = PMA_getForeigners($db, $table, $column);
  1451. $child_references = PMA_getChildReferences($db, $table, $column);
  1452. if (sizeof($child_references, 0) > 0
  1453. || (! empty($foreigners[$column]) && sizeof($foreigners[$column], 0) > 0)
  1454. ) {
  1455. if (sizeof($child_references, 0) > 0) {
  1456. $column_status['isReferenced'] = true;
  1457. foreach ($child_references as $row => $columns) {
  1458. array_push(
  1459. $column_status['references'],
  1460. PMA_Util::backquote($columns['table_schema'])
  1461. . '.' . PMA_Util::backquote($columns['table_name'])
  1462. );
  1463. }
  1464. }
  1465. if (!empty($foreigners[$column]) && sizeof($foreigners[$column], 0) > 0) {
  1466. $column_status['isForeignKey'] = true;
  1467. }
  1468. } else {
  1469. $column_status['isEditable'] = true;
  1470. }
  1471. return $column_status;
  1472. }
  1473. ?>