Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / libraries / bookmark.lib.php
blobef126232bc1506e43e67f3b40c4533f01c65ee9a
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 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Defines the bookmark parameters for the current user
15 * @return array the bookmark parameters for the current user
16 * @access public
18 function PMA_Bookmark_getParams()
20 static $cfgBookmark = null;
22 if (null !== $cfgBookmark) {
23 return $cfgBookmark;
26 $cfgRelation = PMA_getRelationsParam();
28 if ($cfgRelation['bookmarkwork']) {
29 $cfgBookmark = array(
30 'user' => $GLOBALS['cfg']['Server']['user'],
31 'db' => $GLOBALS['cfg']['Server']['pmadb'],
32 'table' => $GLOBALS['cfg']['Server']['bookmarktable'],
34 } else {
35 $cfgBookmark = false;
38 return $cfgBookmark;
39 } // end of the 'PMA_Bookmark_getParams()' function
42 /**
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)
49 * @access public
51 * @global resource the controluser db connection handle
53 function PMA_Bookmark_getList($db)
55 global $controllink;
57 $cfgBookmark = PMA_Bookmark_getParams();
59 if (empty($cfgBookmark)) {
60 return array();
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']) . '\''
67 . ' ORDER BY label';
68 $per_user = $GLOBALS['dbi']->fetchResult(
69 $query, 'id', 'label', $controllink, PMA_DatabaseInterface::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) . '\''
75 . ' AND user = \'\''
76 . ' ORDER BY label';
77 $global = $GLOBALS['dbi']->fetchResult(
78 $query, 'id', 'label', $controllink, PMA_DatabaseInterface::QUERY_STORE
81 foreach ($global as $key => $val) {
82 $global[$key] = $val . ' (' . __('shared') . ')';
85 $ret = $global + $per_user;
87 asort($ret);
89 return $ret;
90 } // end of the 'PMA_Bookmark_getList()' function
93 /**
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
100 * of the owning user
101 * @param boolean $exact_user_match whether to ignore bookmarks with no user
103 * @return string the sql query
105 * @access public
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
113 global $controllink;
115 $cfgBookmark = PMA_Bookmark_getParams();
117 if (empty($cfgBookmark)) {
118 return '';
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 = \'\'';
130 $query .= ')';
133 $query .= ' AND ' . PMA_Util::backquote($id_field) . ' = ' . $id;
135 return $GLOBALS['dbi']->fetchValue($query, 0, 0, $controllink);
136 } // end of the 'PMA_Bookmark_get()' function
139 * Adds a bookmark
141 * @param array $bkm_fields the properties of the bookmark to add; here,
142 * $bkm_fields['bkm_sql_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
147 * @access public
149 * @global resource the controluser db connection handle
151 function PMA_Bookmark_save($bkm_fields, $all_users = false)
153 global $controllink;
155 $cfgBookmark = PMA_Bookmark_getParams();
157 if (empty($cfgBookmark)) {
158 return false;
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($bkm_fields['bkm_database']) . '\', '
165 . '\'' . ($all_users ? '' : PMA_Util::sqlAddSlashes($bkm_fields['bkm_user'])) . '\', '
166 . '\'' . PMA_Util::sqlAddSlashes(urldecode($bkm_fields['bkm_sql_query'])) . '\', '
167 . '\'' . PMA_Util::sqlAddSlashes($bkm_fields['bkm_label']) . '\')';
168 return $GLOBALS['dbi']->query($query, $controllink);
169 } // end of the 'PMA_Bookmark_save()' function
173 * Deletes a bookmark
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
180 * @access public
182 * @global resource the controluser db connection handle
184 function PMA_Bookmark_delete($db, $id)
186 global $controllink;
188 $cfgBookmark = PMA_Bookmark_getParams();
190 if (empty($cfgBookmark)) {
191 return false;
194 $query = 'DELETE FROM ' . PMA_Util::backquote($cfgBookmark['db'])
195 . '.' . PMA_Util::backquote($cfgBookmark['table'])
196 . ' WHERE (user = \'' . PMA_Util::sqlAddSlashes($cfgBookmark['user']) . '\''
197 . ' OR user = \'\')'
198 . ' AND id = ' . $id;
199 return $GLOBALS['dbi']->tryQuery($query, $controllink);
200 } // end of the 'PMA_Bookmark_delete()' function
204 * Bookmark Support
206 $GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();