Refactored ConfigFile class so that it is no longer a singleton
[phpmyadmin.git] / libraries / bookmark.lib.php
blob0c9ff69a76e28f46fbdf5a477eb849b8b149cd0a
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 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Defines the bookmark parameters for the current user
16 * @return array the bookmark parameters for the current user
17 * @access public
19 function PMA_Bookmark_getParams()
21 static $cfgBookmark = null;
23 if (null !== $cfgBookmark) {
24 return $cfgBookmark;
27 $cfgRelation = PMA_getRelationsParam();
29 if ($cfgRelation['bookmarkwork']) {
30 $cfgBookmark = array(
31 'user' => $GLOBALS['cfg']['Server']['user'],
32 'db' => $GLOBALS['cfg']['Server']['pmadb'],
33 'table' => $GLOBALS['cfg']['Server']['bookmarktable'],
35 } else {
36 $cfgBookmark = false;
39 return $cfgBookmark;
40 } // end of the 'PMA_Bookmark_getParams()' function
43 /**
44 * Gets the list of bookmarks defined for the current database
46 * @param string $db the current database name
48 * @return array the bookmarks list (key as index, label as value)
50 * @access public
52 * @global resource the controluser db connection handle
54 function PMA_Bookmark_getList($db)
56 global $controllink;
58 $cfgBookmark = PMA_Bookmark_getParams();
60 if (empty($cfgBookmark)) {
61 return array();
64 $query = 'SELECT label, id FROM '. PMA_Util::backquote($cfgBookmark['db'])
65 . '.' . PMA_Util::backquote($cfgBookmark['table'])
66 . ' WHERE dbase = \'' . PMA_Util::sqlAddSlashes($db) . '\''
67 . ' AND user = \'' . PMA_Util::sqlAddSlashes($cfgBookmark['user']) . '\''
68 . ' ORDER BY label';
69 $per_user = $GLOBALS['dbi']->fetchResult(
70 $query, 'id', 'label', $controllink, PMA_DatabaseInterface::QUERY_STORE
73 $query = 'SELECT label, id FROM '. PMA_Util::backquote($cfgBookmark['db'])
74 . '.' . PMA_Util::backquote($cfgBookmark['table'])
75 . ' WHERE dbase = \'' . PMA_Util::sqlAddSlashes($db) . '\''
76 . ' AND user = \'\''
77 . ' ORDER BY label';
78 $global = $GLOBALS['dbi']->fetchResult(
79 $query, 'id', 'label', $controllink, PMA_DatabaseInterface::QUERY_STORE
82 foreach ($global as $key => $val) {
83 $global[$key] = $val . ' (' . __('shared') . ')';
86 $ret = $global + $per_user;
88 asort($ret);
90 return $ret;
91 } // end of the 'PMA_Bookmark_getList()' function
94 /**
95 * Gets the sql command from a bookmark
97 * @param string $db the current database name
98 * @param mixed $id the id of the bookmark to get
99 * @param string $id_field which field to look up the $id
100 * @param boolean $action_bookmark_all true: get all bookmarks regardless
101 * of the owning user
102 * @param boolean $exact_user_match whether to ignore bookmarks with no user
104 * @return string the sql query
106 * @access public
108 * @global resource the controluser db connection handle
111 function PMA_Bookmark_get($db, $id, $id_field = 'id', $action_bookmark_all = false,
112 $exact_user_match = false
114 global $controllink;
116 $cfgBookmark = PMA_Bookmark_getParams();
118 if (empty($cfgBookmark)) {
119 return '';
122 $query = 'SELECT query FROM ' . PMA_Util::backquote($cfgBookmark['db'])
123 . '.' . PMA_Util::backquote($cfgBookmark['table'])
124 . ' WHERE dbase = \'' . PMA_Util::sqlAddSlashes($db) . '\'';
126 if (! $action_bookmark_all) {
127 $query .= ' AND (user = \''
128 . PMA_Util::sqlAddSlashes($cfgBookmark['user']) . '\'';
129 if (! $exact_user_match) {
130 $query .= ' OR user = \'\'';
132 $query .= ')';
135 $query .= ' AND ' . PMA_Util::backquote($id_field) . ' = ' . $id;
137 return $GLOBALS['dbi']->fetchValue($query, 0, 0, $controllink);
138 } // end of the 'PMA_Bookmark_get()' function
141 * Adds a bookmark
143 * @param array $bkm_fields the properties of the bookmark to add; here,
144 * $bkm_fields['bkm_sql_query'] is urlencoded
145 * @param boolean $all_users whether to make the bookmark available for all users
147 * @return boolean whether the INSERT succeeds or not
149 * @access public
151 * @global resource the controluser db connection handle
153 function PMA_Bookmark_save($bkm_fields, $all_users = false)
155 global $controllink;
157 $cfgBookmark = PMA_Bookmark_getParams();
159 if (empty($cfgBookmark)) {
160 return false;
163 $query = 'INSERT INTO ' . PMA_Util::backquote($cfgBookmark['db'])
164 . '.' . PMA_Util::backquote($cfgBookmark['table'])
165 . ' (id, dbase, user, query, label)'
166 . ' VALUES (NULL, \''
167 . PMA_Util::sqlAddSlashes($bkm_fields['bkm_database']) . '\', '
168 . '\''
169 . ($all_users ? '' : PMA_Util::sqlAddSlashes($bkm_fields['bkm_user']))
170 . '\', '
171 . '\''
172 . PMA_Util::sqlAddSlashes(urldecode($bkm_fields['bkm_sql_query']))
173 . '\', '
174 . '\'' . PMA_Util::sqlAddSlashes($bkm_fields['bkm_label']) . '\')';
175 return $GLOBALS['dbi']->query($query, $controllink);
176 } // end of the 'PMA_Bookmark_save()' function
180 * Deletes a bookmark
182 * @param string $db the current database name
183 * @param integer $id the id of the bookmark to get
185 * @return bool true if successful
187 * @access public
189 * @global resource the controluser db connection handle
191 function PMA_Bookmark_delete($db, $id)
193 global $controllink;
195 $cfgBookmark = PMA_Bookmark_getParams();
197 if (empty($cfgBookmark)) {
198 return false;
201 $query = 'DELETE FROM ' . PMA_Util::backquote($cfgBookmark['db'])
202 . '.' . PMA_Util::backquote($cfgBookmark['table'])
203 . ' WHERE (user = \'' . PMA_Util::sqlAddSlashes($cfgBookmark['user']) . '\''
204 . ' OR user = \'\')'
205 . ' AND id = ' . $id;
206 return $GLOBALS['dbi']->tryQuery($query, $controllink);
207 } // end of the 'PMA_Bookmark_delete()' function
211 * Bookmark Support
213 if (!defined('TESTSUITE')) {
214 $GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();