bookmark.lib.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used with the bookmark feature
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Defines the bookmark parameters for the current user
  13. *
  14. * @return array the bookmark parameters for the current user
  15. * @access public
  16. */
  17. function PMA_Bookmark_getParams()
  18. {
  19. static $cfgBookmark = null;
  20. if (null !== $cfgBookmark) {
  21. return $cfgBookmark;
  22. }
  23. $cfgRelation = PMA_getRelationsParam();
  24. if ($cfgRelation['bookmarkwork']) {
  25. $cfgBookmark = array(
  26. 'user' => $GLOBALS['cfg']['Server']['user'],
  27. 'db' => $GLOBALS['cfg']['Server']['pmadb'],
  28. 'table' => $GLOBALS['cfg']['Server']['bookmarktable'],
  29. );
  30. } else {
  31. $cfgBookmark = false;
  32. }
  33. return $cfgBookmark;
  34. } // end of the 'PMA_Bookmark_getParams()' function
  35. /**
  36. * Gets the list of bookmarks defined for the current database
  37. *
  38. * @param string $db the current database name
  39. *
  40. * @return array the bookmarks list (key as index, label as value)
  41. *
  42. * @access public
  43. *
  44. * @global resource the controluser db connection handle
  45. */
  46. function PMA_Bookmark_getList($db)
  47. {
  48. global $controllink;
  49. $cfgBookmark = PMA_Bookmark_getParams();
  50. if (empty($cfgBookmark)) {
  51. return array();
  52. }
  53. $query = 'SELECT label, id FROM '. PMA_Util::backquote($cfgBookmark['db'])
  54. . '.' . PMA_Util::backquote($cfgBookmark['table'])
  55. . ' WHERE dbase = \'' . PMA_Util::sqlAddSlashes($db) . '\''
  56. . ' AND user = \'' . PMA_Util::sqlAddSlashes($cfgBookmark['user']) . '\''
  57. . ' ORDER BY label';
  58. $per_user = PMA_DBI_fetch_result(
  59. $query, 'id', 'label', $controllink, PMA_DBI_QUERY_STORE
  60. );
  61. $query = 'SELECT label, id FROM '. PMA_Util::backquote($cfgBookmark['db'])
  62. . '.' . PMA_Util::backquote($cfgBookmark['table'])
  63. . ' WHERE dbase = \'' . PMA_Util::sqlAddSlashes($db) . '\''
  64. . ' AND user = \'\''
  65. . ' ORDER BY label';
  66. $global = PMA_DBI_fetch_result(
  67. $query, 'id', 'label', $controllink, PMA_DBI_QUERY_STORE
  68. );
  69. foreach ($global as $key => $val) {
  70. $global[$key] = $val . ' (' . __('shared') . ')';
  71. }
  72. $ret = $global + $per_user;
  73. asort($ret);
  74. return $ret;
  75. } // end of the 'PMA_Bookmark_getList()' function
  76. /**
  77. * Gets the sql command from a bookmark
  78. *
  79. * @param string $db the current database name
  80. * @param mixed $id the id of the bookmark to get
  81. * @param string $id_field which field to look up the $id
  82. * @param boolean $action_bookmark_all true: get all bookmarks regardless
  83. * of the owning user
  84. * @param boolean $exact_user_match whether to ignore bookmarks with no user
  85. *
  86. * @return string the sql query
  87. *
  88. * @access public
  89. *
  90. * @global resource the controluser db connection handle
  91. *
  92. */
  93. function PMA_Bookmark_get($db, $id, $id_field = 'id', $action_bookmark_all = false,
  94. $exact_user_match = false
  95. ) {
  96. global $controllink;
  97. $cfgBookmark = PMA_Bookmark_getParams();
  98. if (empty($cfgBookmark)) {
  99. return '';
  100. }
  101. $query = 'SELECT query FROM ' . PMA_Util::backquote($cfgBookmark['db'])
  102. . '.' . PMA_Util::backquote($cfgBookmark['table'])
  103. . ' WHERE dbase = \'' . PMA_Util::sqlAddSlashes($db) . '\'';
  104. if (!$action_bookmark_all) {
  105. $query .= ' AND (user = \'' . PMA_Util::sqlAddSlashes($cfgBookmark['user']) . '\'';
  106. if (!$exact_user_match) {
  107. $query .= ' OR user = \'\'';
  108. }
  109. $query .= ')';
  110. }
  111. $query .= ' AND ' . PMA_Util::backquote($id_field) . ' = ' . $id;
  112. return PMA_DBI_fetch_value($query, 0, 0, $controllink);
  113. } // end of the 'PMA_Bookmark_get()' function
  114. /**
  115. * Adds a bookmark
  116. *
  117. * @param array $fields the properties of the bookmark to add; here,
  118. * $fields['query'] is urlencoded
  119. * @param boolean $all_users whether to make the bookmark available for all users
  120. *
  121. * @return boolean whether the INSERT succeeds or not
  122. *
  123. * @access public
  124. *
  125. * @global resource the controluser db connection handle
  126. */
  127. function PMA_Bookmark_save($fields, $all_users = false)
  128. {
  129. global $controllink;
  130. $cfgBookmark = PMA_Bookmark_getParams();
  131. if (empty($cfgBookmark)) {
  132. return false;
  133. }
  134. $query = 'INSERT INTO ' . PMA_Util::backquote($cfgBookmark['db'])
  135. . '.' . PMA_Util::backquote($cfgBookmark['table'])
  136. . ' (id, dbase, user, query, label)'
  137. . ' VALUES (NULL, \'' . PMA_Util::sqlAddSlashes($fields['dbase']) . '\', '
  138. . '\'' . ($all_users ? '' : PMA_Util::sqlAddSlashes($fields['user'])) . '\', '
  139. . '\'' . PMA_Util::sqlAddSlashes(urldecode($fields['query'])) . '\', '
  140. . '\'' . PMA_Util::sqlAddSlashes($fields['label']) . '\')';
  141. return PMA_DBI_query($query, $controllink);
  142. } // end of the 'PMA_Bookmark_save()' function
  143. /**
  144. * Deletes a bookmark
  145. *
  146. * @param string $db the current database name
  147. * @param integer $id the id of the bookmark to get
  148. *
  149. * @return bool true if successful
  150. *
  151. * @access public
  152. *
  153. * @global resource the controluser db connection handle
  154. */
  155. function PMA_Bookmark_delete($db, $id)
  156. {
  157. global $controllink;
  158. $cfgBookmark = PMA_Bookmark_getParams();
  159. if (empty($cfgBookmark)) {
  160. return false;
  161. }
  162. $query = 'DELETE FROM ' . PMA_Util::backquote($cfgBookmark['db'])
  163. . '.' . PMA_Util::backquote($cfgBookmark['table'])
  164. . ' WHERE (user = \'' . PMA_Util::sqlAddSlashes($cfgBookmark['user']) . '\''
  165. . ' OR user = \'\')'
  166. . ' AND id = ' . $id;
  167. return PMA_DBI_try_query($query, $controllink);
  168. } // end of the 'PMA_Bookmark_delete()' function
  169. /**
  170. * Bookmark Support
  171. */
  172. $GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();
  173. ?>