2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used with the bookmark feature
13 require_once './libraries/relation.lib.php';
16 * Defines the bookmark parameters for the current user
18 * @uses $GLOBALS['server']
19 * @uses PMA_getRelationsParam()
20 * @uses $GLOBALS['cfg']['Server']['user']
21 * @uses $GLOBALS['cfg']['Server']['pmadb']
22 * @uses $GLOBALS['cfg']['Server']['bookmarktable']
23 * @return array the bookmark parameters for the current user
26 function PMA_Bookmark_getParams()
28 static $cfgBookmark = null;
30 if (null !== $cfgBookmark) {
34 $cfgRelation = PMA_getRelationsParam();
36 if ($cfgRelation['bookmarkwork']) {
38 'user' => $GLOBALS['cfg']['Server']['user'],
39 'db' => $GLOBALS['cfg']['Server']['pmadb'],
40 'table' => $GLOBALS['cfg']['Server']['bookmarktable'],
47 } // end of the 'PMA_Bookmark_getParams()' function
51 * Gets the list of bookmarks defined for the current database
53 * @uses PMA_backquote()
54 * @uses PMA_sqlAddslashes()
55 * @uses PMA_DBI_fetch_result()
56 * @uses PMA_DBI_QUERY_STORE
57 * @uses PMA_Bookmark_getParams()
58 * @global resource the controluser db connection handle
60 * @param string the current database name
62 * @return array the bookmarks list (key as index, label as value)
66 function PMA_Bookmark_getList($db)
70 $cfgBookmark = PMA_Bookmark_getParams();
72 if (empty($cfgBookmark)) {
76 $query = 'SELECT label, id FROM '. PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
77 . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
78 . ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
81 return PMA_DBI_fetch_result($query, 'id', 'label', $controllink, PMA_DBI_QUERY_STORE
);
82 } // end of the 'PMA_Bookmark_getList()' function
86 * Gets the sql command from a bookmark
88 * @uses PMA_backquote()
89 * @uses PMA_sqlAddslashes()
90 * @uses PMA_DBI_fetch_value()
91 * @uses PMA_Bookmark_getParams()
92 * @global resource the controluser db connection handle
94 * @param string the current database name
95 * @param mixed the id of the bookmark to get
96 * @param string which field to look up the $id
97 * @param boolean TRUE: get all bookmarks regardless of the owning user
98 * @param boolean whether to ignore bookmarks with no user
100 * @return string the sql query
104 function PMA_Bookmark_get($db, $id, $id_field = 'id', $action_bookmark_all = FALSE, $exact_user_match = FALSE)
108 $cfgBookmark = PMA_Bookmark_getParams();
110 if (empty($cfgBookmark)) {
114 $query = 'SELECT query FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
115 . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\'';
117 if (!$action_bookmark_all) {
118 $query .= ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\'';
119 if (!$exact_user_match) {
120 $query .= ' OR user = \'\'';
125 $query .= ' AND ' . PMA_backquote($id_field) . ' = ' . $id;
127 return PMA_DBI_fetch_value($query, 0, 0, $controllink);
128 } // end of the 'PMA_Bookmark_get()' function
133 * @uses PMA_backquote()
134 * @uses PMA_sqlAddslashes()
135 * @uses PMA_DBI_query()
136 * @uses PMA_Bookmark_getParams()
137 * @global resource the controluser db connection handle
139 * @param array the properties of the bookmark to add; here,
140 * $fields['query'] is urlencoded
141 * @param boolean whether to make the bookmark available for all users
143 * @return boolean whether the INSERT succeeds or not
147 function PMA_Bookmark_save($fields, $all_users = false)
151 $cfgBookmark = PMA_Bookmark_getParams();
153 if (empty($cfgBookmark)) {
157 $query = 'INSERT INTO ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
158 . ' (id, dbase, user, query, label) VALUES (NULL, \'' . PMA_sqlAddslashes($fields['dbase']) . '\', \'' . ($all_users ?
'' : PMA_sqlAddslashes($fields['user'])) . '\', \'' . PMA_sqlAddslashes(urldecode($fields['query'])) . '\', \'' . PMA_sqlAddslashes($fields['label']) . '\')';
159 return PMA_DBI_query($query, $controllink);
160 } // end of the 'PMA_Bookmark_save()' function
166 * @uses PMA_backquote()
167 * @uses PMA_sqlAddslashes()
168 * @uses PMA_DBI_try_query()
169 * @uses PMA_Bookmark_getParams()
170 * @global resource the controluser db connection handle
172 * @param string the current database name
173 * @param integer the id of the bookmark to get
177 function PMA_Bookmark_delete($db, $id)
181 $cfgBookmark = PMA_Bookmark_getParams();
183 if (empty($cfgBookmark)) {
187 $query = 'DELETE FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
188 . ' WHERE (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
190 . ' AND id = ' . $id;
191 return PMA_DBI_try_query($query, $controllink);
192 } // end of the 'PMA_Bookmark_delete()' function
198 $GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();