2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used with the bookmark feature
8 if (! defined('PHPMYADMIN')) {
13 * Defines the bookmark parameters for the current user
15 * @return array the bookmark parameters for the current user
18 function PMA_Bookmark_getParams()
20 static $cfgBookmark = null;
22 if (null !== $cfgBookmark) {
26 $cfgRelation = PMA_getRelationsParam();
28 if ($cfgRelation['bookmarkwork']) {
30 'user' => $GLOBALS['cfg']['Server']['user'],
31 'db' => $GLOBALS['cfg']['Server']['pmadb'],
32 'table' => $GLOBALS['cfg']['Server']['bookmarktable'],
39 } // end of the 'PMA_Bookmark_getParams()' function
43 * Gets the list of bookmarks defined for the current database
45 * @param string $db the current database name
47 * @return array the bookmarks list (key as index, label as value)
51 * @global resource the controluser db connection handle
53 function PMA_Bookmark_getList($db)
57 $cfgBookmark = PMA_Bookmark_getParams();
59 if (empty($cfgBookmark)) {
63 $query = 'SELECT label, id FROM '. PMA_Util
::backquote($cfgBookmark['db'])
64 . '.' . PMA_Util
::backquote($cfgBookmark['table'])
65 . ' WHERE dbase = \'' . PMA_Util
::sqlAddSlashes($db) . '\''
66 . ' AND user = \'' . PMA_Util
::sqlAddSlashes($cfgBookmark['user']) . '\''
68 $per_user = PMA_DBI_fetch_result(
69 $query, 'id', 'label', $controllink, PMA_DBI_QUERY_STORE
72 $query = 'SELECT label, id FROM '. PMA_Util
::backquote($cfgBookmark['db'])
73 . '.' . PMA_Util
::backquote($cfgBookmark['table'])
74 . ' WHERE dbase = \'' . PMA_Util
::sqlAddSlashes($db) . '\''
77 $global = PMA_DBI_fetch_result(
78 $query, 'id', 'label', $controllink, PMA_DBI_QUERY_STORE
81 foreach ($global as $key => $val) {
82 $global[$key] = $val . ' (' . __('shared') . ')';
85 $ret = $global +
$per_user;
90 } // end of the 'PMA_Bookmark_getList()' function
94 * Gets the sql command from a bookmark
96 * @param string $db the current database name
97 * @param mixed $id the id of the bookmark to get
98 * @param string $id_field which field to look up the $id
99 * @param boolean $action_bookmark_all true: get all bookmarks regardless
101 * @param boolean $exact_user_match whether to ignore bookmarks with no user
103 * @return string the sql query
107 * @global resource the controluser db connection handle
110 function PMA_Bookmark_get($db, $id, $id_field = 'id', $action_bookmark_all = false,
111 $exact_user_match = false
115 $cfgBookmark = PMA_Bookmark_getParams();
117 if (empty($cfgBookmark)) {
121 $query = 'SELECT query FROM ' . PMA_Util
::backquote($cfgBookmark['db'])
122 . '.' . PMA_Util
::backquote($cfgBookmark['table'])
123 . ' WHERE dbase = \'' . PMA_Util
::sqlAddSlashes($db) . '\'';
125 if (!$action_bookmark_all) {
126 $query .= ' AND (user = \'' . PMA_Util
::sqlAddSlashes($cfgBookmark['user']) . '\'';
127 if (!$exact_user_match) {
128 $query .= ' OR user = \'\'';
133 $query .= ' AND ' . PMA_Util
::backquote($id_field) . ' = ' . $id;
135 return PMA_DBI_fetch_value($query, 0, 0, $controllink);
136 } // end of the 'PMA_Bookmark_get()' function
141 * @param array $fields the properties of the bookmark to add; here,
142 * $fields['query'] is urlencoded
143 * @param boolean $all_users whether to make the bookmark available for all users
145 * @return boolean whether the INSERT succeeds or not
149 * @global resource the controluser db connection handle
151 function PMA_Bookmark_save($fields, $all_users = false)
155 $cfgBookmark = PMA_Bookmark_getParams();
157 if (empty($cfgBookmark)) {
161 $query = 'INSERT INTO ' . PMA_Util
::backquote($cfgBookmark['db'])
162 . '.' . PMA_Util
::backquote($cfgBookmark['table'])
163 . ' (id, dbase, user, query, label)'
164 . ' VALUES (NULL, \'' . PMA_Util
::sqlAddSlashes($fields['dbase']) . '\', '
165 . '\'' . ($all_users ?
'' : PMA_Util
::sqlAddSlashes($fields['user'])) . '\', '
166 . '\'' . PMA_Util
::sqlAddSlashes(urldecode($fields['query'])) . '\', '
167 . '\'' . PMA_Util
::sqlAddSlashes($fields['label']) . '\')';
168 return PMA_DBI_query($query, $controllink);
169 } // end of the 'PMA_Bookmark_save()' function
175 * @param string $db the current database name
176 * @param integer $id the id of the bookmark to get
178 * @return bool true if successful
182 * @global resource the controluser db connection handle
184 function PMA_Bookmark_delete($db, $id)
188 $cfgBookmark = PMA_Bookmark_getParams();
190 if (empty($cfgBookmark)) {
194 $query = 'DELETE FROM ' . PMA_Util
::backquote($cfgBookmark['db'])
195 . '.' . PMA_Util
::backquote($cfgBookmark['table'])
196 . ' WHERE (user = \'' . PMA_Util
::sqlAddSlashes($cfgBookmark['user']) . '\''
198 . ' AND id = ' . $id;
199 return PMA_DBI_try_query($query, $controllink);
200 } // end of the 'PMA_Bookmark_delete()' function
206 $GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();