bookmark.lib.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 $controllink 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 = $GLOBALS['dbi']->fetchResult(
  59. $query, 'id', 'label', $controllink, PMA_DatabaseInterface::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 = $GLOBALS['dbi']->fetchResult(
  67. $query, 'id', 'label', $controllink, PMA_DatabaseInterface::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 $controllink 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 = \''
  106. . PMA_Util::sqlAddSlashes($cfgBookmark['user']) . '\'';
  107. if (! $exact_user_match) {
  108. $query .= ' OR user = \'\'';
  109. }
  110. $query .= ')';
  111. }
  112. $query .= ' AND ' . PMA_Util::backquote($id_field) . ' = ' . $id;
  113. return $GLOBALS['dbi']->fetchValue($query, 0, 0, $controllink);
  114. } // end of the 'PMA_Bookmark_get()' function
  115. /**
  116. * Adds a bookmark
  117. *
  118. * @param array $bkm_fields the properties of the bookmark to add; here,
  119. * $bkm_fields['bkm_sql_query'] is urlencoded
  120. * @param boolean $all_users whether to make the bookmark available for all users
  121. *
  122. * @return boolean whether the INSERT succeeds or not
  123. *
  124. * @access public
  125. *
  126. * @global resource $controllink the controluser db connection handle
  127. */
  128. function PMA_Bookmark_save($bkm_fields, $all_users = false)
  129. {
  130. global $controllink;
  131. $cfgBookmark = PMA_Bookmark_getParams();
  132. if (empty($cfgBookmark)) {
  133. return false;
  134. }
  135. $query = 'INSERT INTO ' . PMA_Util::backquote($cfgBookmark['db'])
  136. . '.' . PMA_Util::backquote($cfgBookmark['table'])
  137. . ' (id, dbase, user, query, label)'
  138. . ' VALUES (NULL, \''
  139. . PMA_Util::sqlAddSlashes($bkm_fields['bkm_database']) . '\', '
  140. . '\''
  141. . ($all_users ? '' : PMA_Util::sqlAddSlashes($bkm_fields['bkm_user']))
  142. . '\', '
  143. . '\''
  144. . PMA_Util::sqlAddSlashes(urldecode($bkm_fields['bkm_sql_query']))
  145. . '\', '
  146. . '\'' . PMA_Util::sqlAddSlashes($bkm_fields['bkm_label']) . '\')';
  147. return $GLOBALS['dbi']->query($query, $controllink);
  148. } // end of the 'PMA_Bookmark_save()' function
  149. /**
  150. * Deletes a bookmark
  151. *
  152. * @param integer $id the id of the bookmark to delete
  153. *
  154. * @return bool true if successful
  155. *
  156. * @access public
  157. *
  158. * @global resource $controllink the controluser db connection handle
  159. */
  160. function PMA_Bookmark_delete($id)
  161. {
  162. global $controllink;
  163. $cfgBookmark = PMA_Bookmark_getParams();
  164. if (empty($cfgBookmark)) {
  165. return false;
  166. }
  167. $query = 'DELETE FROM ' . PMA_Util::backquote($cfgBookmark['db'])
  168. . '.' . PMA_Util::backquote($cfgBookmark['table'])
  169. . ' WHERE (user = \'' . PMA_Util::sqlAddSlashes($cfgBookmark['user']) . '\''
  170. . ' OR user = \'\')'
  171. . ' AND id = ' . $id;
  172. return $GLOBALS['dbi']->tryQuery($query, $controllink);
  173. } // end of the 'PMA_Bookmark_delete()' function
  174. /**
  175. * Bookmark Support
  176. */
  177. if (!defined('TESTSUITE')) {
  178. $GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();
  179. }
  180. ?>