Comments
[phpmyadmin.git] / libraries / bookmark.lib.php
blob723b042b8b0109249c5498d551ef2faec9a67bc5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used with the bookmark feature
6 * @package phpMyAdmin
7 */
9 /**
10 * Defines the bookmark parameters for the current user
12 * @return array the bookmark parameters for the current user
13 * @access public
15 function PMA_Bookmark_getParams()
17 static $cfgBookmark = null;
19 if (null !== $cfgBookmark) {
20 return $cfgBookmark;
23 $cfgRelation = PMA_getRelationsParam();
25 if ($cfgRelation['bookmarkwork']) {
26 $cfgBookmark = array(
27 'user' => $GLOBALS['cfg']['Server']['user'],
28 'db' => $GLOBALS['cfg']['Server']['pmadb'],
29 'table' => $GLOBALS['cfg']['Server']['bookmarktable'],
31 } else {
32 $cfgBookmark = false;
35 return $cfgBookmark;
36 } // end of the 'PMA_Bookmark_getParams()' function
39 /**
40 * Gets the list of bookmarks defined for the current database
42 * @global resource the controluser db connection handle
44 * @param string the current database name
46 * @return array the bookmarks list (key as index, label as value)
48 * @access public
50 function PMA_Bookmark_getList($db)
52 global $controllink;
54 $cfgBookmark = PMA_Bookmark_getParams();
56 if (empty($cfgBookmark)) {
57 return array();
60 $query = 'SELECT label, id FROM '. PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
61 . ' WHERE dbase = \'' . PMA_sqlAddSlashes($db) . '\''
62 . ' AND user = \'' . PMA_sqlAddSlashes($cfgBookmark['user']) . '\''
63 . ' ORDER BY label';
64 $per_user = PMA_DBI_fetch_result($query, 'id', 'label', $controllink, PMA_DBI_QUERY_STORE);
66 $query = 'SELECT label, id FROM '. PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
67 . ' WHERE dbase = \'' . PMA_sqlAddSlashes($db) . '\''
68 . ' AND user = \'\''
69 . ' ORDER BY label';
70 $global = PMA_DBI_fetch_result($query, 'id', 'label', $controllink, PMA_DBI_QUERY_STORE);
72 foreach ($global as $key => $val) {
73 $global[$key] = $val . ' (' . __('shared') . ')';
76 $ret = $global + $per_user;
78 asort($ret);
80 return $ret;
81 } // end of the 'PMA_Bookmark_getList()' function
84 /**
85 * Gets the sql command from a bookmark
87 * @global resource the controluser db connection handle
89 * @param string the current database name
90 * @param mixed the id of the bookmark to get
91 * @param string which field to look up the $id
92 * @param boolean true: get all bookmarks regardless of the owning user
93 * @param boolean whether to ignore bookmarks with no user
95 * @return string the sql query
97 * @access public
99 function PMA_Bookmark_get($db, $id, $id_field = 'id', $action_bookmark_all = false, $exact_user_match = false)
101 global $controllink;
103 $cfgBookmark = PMA_Bookmark_getParams();
105 if (empty($cfgBookmark)) {
106 return '';
109 $query = 'SELECT query FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
110 . ' WHERE dbase = \'' . PMA_sqlAddSlashes($db) . '\'';
112 if (!$action_bookmark_all) {
113 $query .= ' AND (user = \'' . PMA_sqlAddSlashes($cfgBookmark['user']) . '\'';
114 if (!$exact_user_match) {
115 $query .= ' OR user = \'\'';
117 $query .= ')';
120 $query .= ' AND ' . PMA_backquote($id_field) . ' = ' . $id;
122 return PMA_DBI_fetch_value($query, 0, 0, $controllink);
123 } // end of the 'PMA_Bookmark_get()' function
126 * Adds a bookmark
128 * @global resource the controluser db connection handle
130 * @param array the properties of the bookmark to add; here,
131 * $fields['query'] is urlencoded
132 * @param boolean whether to make the bookmark available for all users
134 * @return boolean whether the INSERT succeeds or not
136 * @access public
138 function PMA_Bookmark_save($fields, $all_users = false)
140 global $controllink;
142 $cfgBookmark = PMA_Bookmark_getParams();
144 if (empty($cfgBookmark)) {
145 return false;
148 $query = 'INSERT INTO ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
149 . ' (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']) . '\')';
150 return PMA_DBI_query($query, $controllink);
151 } // end of the 'PMA_Bookmark_save()' function
155 * Deletes a bookmark
157 * @global resource the controluser db connection handle
159 * @param string the current database name
160 * @param integer the id of the bookmark to get
162 * @access public
164 function PMA_Bookmark_delete($db, $id)
166 global $controllink;
168 $cfgBookmark = PMA_Bookmark_getParams();
170 if (empty($cfgBookmark)) {
171 return false;
174 $query = 'DELETE FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
175 . ' WHERE (user = \'' . PMA_sqlAddSlashes($cfgBookmark['user']) . '\''
176 . ' OR user = \'\')'
177 . ' AND id = ' . $id;
178 return PMA_DBI_try_query($query, $controllink);
179 } // end of the 'PMA_Bookmark_delete()' function
183 * Bookmark Support
185 $GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();