/phpmyadmin/libraries/bookmark.lib.php

https://gitlab.com/luyxtran264/myproject · PHP · 289 lines · 170 code · 35 blank · 84 comment · 18 complexity · 7babf319fbb20aa3ac407587077641ca MD5 · raw file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used with the bookmark feature
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Defines the bookmark parameters for the current user
  10. *
  11. * @return array the bookmark parameters for the current user
  12. * @access public
  13. */
  14. function PMA_Bookmark_getParams()
  15. {
  16. static $cfgBookmark = null;
  17. if (null !== $cfgBookmark) {
  18. return $cfgBookmark;
  19. }
  20. $cfgRelation = PMA_getRelationsParam();
  21. if ($cfgRelation['bookmarkwork']) {
  22. $cfgBookmark = array(
  23. 'user' => $GLOBALS['cfg']['Server']['user'],
  24. 'db' => $cfgRelation['db'],
  25. 'table' => $cfgRelation['bookmark'],
  26. );
  27. } else {
  28. $cfgBookmark = false;
  29. }
  30. return $cfgBookmark;
  31. } // end of the 'PMA_Bookmark_getParams()' function
  32. /**
  33. * Gets the list of bookmarks defined for the current database
  34. *
  35. * @param string|bool $db the current database name or false
  36. *
  37. * @return array the bookmarks list (key as index, label as value),
  38. * or if param is empty, function will give more information,
  39. * array will be unindexed,
  40. * each struct: [db, id, label, shared, query]
  41. *
  42. * @access public
  43. *
  44. * @global resource $controllink the controluser db connection handle
  45. */
  46. function PMA_Bookmark_getList($db = false)
  47. {
  48. global $controllink;
  49. $cfgBookmark = PMA_Bookmark_getParams();
  50. if (empty($cfgBookmark)) {
  51. return array();
  52. }
  53. if ($db !== false) {
  54. $query = 'SELECT query, label, id FROM ' . PMA\libraries\Util::backquote(
  55. $cfgBookmark['db']
  56. ) . '.' . PMA\libraries\Util::backquote($cfgBookmark['table'])
  57. . ' WHERE dbase = \'' . PMA\libraries\Util::sqlAddSlashes($db) . '\''
  58. . ' AND user = \'' . PMA\libraries\Util::sqlAddSlashes($cfgBookmark['user'])
  59. . '\''
  60. . ' ORDER BY label';
  61. $per_user = $GLOBALS['dbi']->fetchResult(
  62. $query,
  63. 'id',
  64. null,
  65. $controllink,
  66. PMA\libraries\DatabaseInterface::QUERY_STORE
  67. );
  68. $query = 'SELECT query, label, id FROM ' . PMA\libraries\Util::backquote(
  69. $cfgBookmark['db']
  70. ) . '.' . PMA\libraries\Util::backquote($cfgBookmark['table'])
  71. . ' WHERE dbase = \'' . PMA\libraries\Util::sqlAddSlashes($db) . '\''
  72. . ' AND user = \'\''
  73. . ' ORDER BY label';
  74. $global = $GLOBALS['dbi']->fetchResult(
  75. $query,
  76. 'id',
  77. null,
  78. $controllink,
  79. PMA\libraries\DatabaseInterface::QUERY_STORE
  80. );
  81. foreach ($global as $key => $val) {
  82. $global[$key]['label'] = $val['label'] . ' (' . __('shared') . ')';
  83. }
  84. $ret = $global + $per_user;
  85. asort($ret);
  86. } else {
  87. $query = "SELECT `label`, `id`, `query`, `dbase` AS `db`,"
  88. . " IF (`user` = '', true, false) AS `shared`"
  89. . " FROM " . PMA\libraries\Util::backquote($cfgBookmark['db'])
  90. . "." . PMA\libraries\Util::backquote($cfgBookmark['table'])
  91. . " WHERE `user` = '' OR"
  92. . " `user` = '" . PMA\libraries\Util::sqlAddSlashes($cfgBookmark['user'])
  93. . "'";
  94. $ret = $GLOBALS['dbi']->fetchResult(
  95. $query,
  96. null,
  97. null,
  98. $controllink,
  99. PMA\libraries\DatabaseInterface::QUERY_STORE
  100. );
  101. }
  102. return $ret;
  103. } // end of the 'PMA_Bookmark_getList()' function
  104. /**
  105. * Gets the sql command from a bookmark
  106. *
  107. * @param string $db the current database name
  108. * @param mixed $id the id of the bookmark to get
  109. * @param string $id_field which field to look up the $id
  110. * @param boolean $action_bookmark_all true: get all bookmarks regardless
  111. * of the owning user
  112. * @param boolean $exact_user_match whether to ignore bookmarks with no user
  113. *
  114. * @return string the sql query
  115. *
  116. * @access public
  117. *
  118. * @global resource $controllink the controluser db connection handle
  119. *
  120. */
  121. function PMA_Bookmark_get($db, $id, $id_field = 'id', $action_bookmark_all = false,
  122. $exact_user_match = false
  123. ) {
  124. global $controllink;
  125. $cfgBookmark = PMA_Bookmark_getParams();
  126. if (empty($cfgBookmark)) {
  127. return '';
  128. }
  129. $query = 'SELECT query FROM ' . PMA\libraries\Util::backquote($cfgBookmark['db'])
  130. . '.' . PMA\libraries\Util::backquote($cfgBookmark['table'])
  131. . ' WHERE dbase = \'' . PMA\libraries\Util::sqlAddSlashes($db) . '\'';
  132. if (! $action_bookmark_all) {
  133. $query .= ' AND (user = \''
  134. . PMA\libraries\Util::sqlAddSlashes($cfgBookmark['user']) . '\'';
  135. if (! $exact_user_match) {
  136. $query .= ' OR user = \'\'';
  137. }
  138. $query .= ')';
  139. }
  140. $query .= ' AND ' . PMA\libraries\Util::backquote($id_field) . ' = ' . $id;
  141. return $GLOBALS['dbi']->fetchValue($query, 0, 0, $controllink);
  142. } // end of the 'PMA_Bookmark_get()' function
  143. /**
  144. * Adds a bookmark
  145. *
  146. * @param array $bkm_fields the properties of the bookmark to add; here,
  147. * $bkm_fields['bkm_sql_query'] is urlencoded
  148. * @param boolean $all_users whether to make the bookmark available for all users
  149. *
  150. * @return boolean whether the INSERT succeeds or not
  151. *
  152. * @access public
  153. *
  154. * @global resource $controllink the controluser db connection handle
  155. */
  156. function PMA_Bookmark_save($bkm_fields, $all_users = false)
  157. {
  158. global $controllink;
  159. $cfgBookmark = PMA_Bookmark_getParams();
  160. if (!(isset($bkm_fields['bkm_sql_query']) && isset($bkm_fields['bkm_label'])
  161. && mb_strlen($bkm_fields['bkm_sql_query']) > 0
  162. && mb_strlen($bkm_fields['bkm_label']) > 0)
  163. ) {
  164. return false;
  165. }
  166. $query = 'INSERT INTO ' . PMA\libraries\Util::backquote($cfgBookmark['db'])
  167. . '.' . PMA\libraries\Util::backquote($cfgBookmark['table'])
  168. . ' (id, dbase, user, query, label)'
  169. . ' VALUES (NULL, \''
  170. . PMA\libraries\Util::sqlAddSlashes($bkm_fields['bkm_database']) . '\', '
  171. . '\''
  172. . ($all_users
  173. ? ''
  174. : PMA\libraries\Util::sqlAddSlashes(
  175. $bkm_fields['bkm_user']
  176. ))
  177. . '\', '
  178. . '\''
  179. . PMA\libraries\Util::sqlAddSlashes(urldecode($bkm_fields['bkm_sql_query']))
  180. . '\', '
  181. . '\'' . PMA\libraries\Util::sqlAddSlashes($bkm_fields['bkm_label']) . '\')';
  182. return $GLOBALS['dbi']->query($query, $controllink);
  183. } // end of the 'PMA_Bookmark_save()' function
  184. /**
  185. * Deletes a bookmark
  186. *
  187. * @param integer $id the id of the bookmark to delete
  188. *
  189. * @return bool true if successful
  190. *
  191. * @access public
  192. *
  193. * @global resource $controllink the controluser db connection handle
  194. */
  195. function PMA_Bookmark_delete($id)
  196. {
  197. global $controllink;
  198. $cfgBookmark = PMA_Bookmark_getParams();
  199. if (empty($cfgBookmark)) {
  200. return false;
  201. }
  202. $query = 'DELETE FROM ' . PMA\libraries\Util::backquote($cfgBookmark['db'])
  203. . '.' . PMA\libraries\Util::backquote($cfgBookmark['table'])
  204. . ' WHERE (user = \''
  205. . PMA\libraries\Util::sqlAddSlashes($cfgBookmark['user']) . '\''
  206. . ' OR user = \'\')'
  207. . ' AND id = ' . $id;
  208. return $GLOBALS['dbi']->tryQuery($query, $controllink);
  209. } // end of the 'PMA_Bookmark_delete()' function
  210. /**
  211. * Returns the number of variables in a bookmark
  212. *
  213. * @param string $query bookmarked query
  214. *
  215. * @return number number of variables
  216. */
  217. function PMA_Bookmark_getVariableCount($query)
  218. {
  219. $matches = array();
  220. preg_match_all("/\[VARIABLE[0-9]*\]/", $query, $matches, PREG_SET_ORDER);
  221. return count($matches);
  222. }
  223. /**
  224. * Replace the placeholders in the bookmark query with variables
  225. *
  226. * @param string $query bookmarked query
  227. *
  228. * @return string query with variables applied
  229. */
  230. function PMA_Bookmark_applyVariables($query)
  231. {
  232. // remove comments that encloses a variable placeholder
  233. $query = preg_replace(
  234. '|/\*(.*\[VARIABLE[0-9]*\].*)\*/|imsU',
  235. '${1}',
  236. $query
  237. );
  238. // replace variable placeholders with values
  239. $number_of_variables = PMA_Bookmark_getVariableCount($query);
  240. for ($i = 1; $i <= $number_of_variables; $i++) {
  241. $var = '';
  242. if (! empty($_REQUEST['bookmark_variable'][$i])) {
  243. $var = PMA\libraries\Util::sqlAddSlashes(
  244. $_REQUEST['bookmark_variable'][$i]
  245. );
  246. }
  247. $query = str_replace('[VARIABLE' . $i . ']', $var, $query);
  248. // backward compatibility
  249. if ($i == 1) {
  250. $query = str_replace('[VARIABLE]', $var, $query);
  251. }
  252. }
  253. return $query;
  254. }