Bookmark.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Handles bookmarking SQL queries
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use PhpMyAdmin\DatabaseInterface;
  10. use PhpMyAdmin\Relation;
  11. use PhpMyAdmin\Util;
  12. /**
  13. * Handles bookmarking SQL queries
  14. *
  15. * @package PhpMyAdmin
  16. */
  17. class Bookmark
  18. {
  19. /**
  20. * ID of the bookmark
  21. *
  22. * @var int
  23. */
  24. private $_id;
  25. /**
  26. * Database the bookmark belongs to
  27. *
  28. * @var string
  29. */
  30. private $_database;
  31. /**
  32. * The user to whom the bookmark belongs, empty for public bookmarks
  33. *
  34. * @var string
  35. */
  36. private $_user;
  37. /**
  38. * Label of the bookmark
  39. *
  40. * @var string
  41. */
  42. private $_label;
  43. /**
  44. * SQL query that is bookmarked
  45. *
  46. * @var string
  47. */
  48. private $_query;
  49. /**
  50. * @var DatabaseInterface
  51. */
  52. private $dbi;
  53. /**
  54. * Current user
  55. *
  56. * @var string
  57. */
  58. private $user;
  59. public function __construct(DatabaseInterface $dbi, $user)
  60. {
  61. $this->dbi = $dbi;
  62. $this->user = $user;
  63. }
  64. /**
  65. * Returns the ID of the bookmark
  66. *
  67. * @return int
  68. */
  69. public function getId()
  70. {
  71. return $this->_id;
  72. }
  73. /**
  74. * Returns the database of the bookmark
  75. *
  76. * @return string
  77. */
  78. public function getDatabase()
  79. {
  80. return $this->_database;
  81. }
  82. /**
  83. * Returns the user whom the bookmark belongs to
  84. *
  85. * @return string
  86. */
  87. public function getUser()
  88. {
  89. return $this->_user;
  90. }
  91. /**
  92. * Returns the label of the bookmark
  93. *
  94. * @return string
  95. */
  96. public function getLabel()
  97. {
  98. return $this->_label;
  99. }
  100. /**
  101. * Returns the query
  102. *
  103. * @return string
  104. */
  105. public function getQuery()
  106. {
  107. return $this->_query;
  108. }
  109. /**
  110. * Adds a bookmark
  111. *
  112. * @return boolean whether the INSERT succeeds or not
  113. *
  114. * @access public
  115. */
  116. public function save()
  117. {
  118. $cfgBookmark = self::getParams($this->user);
  119. if (empty($cfgBookmark)) {
  120. return false;
  121. }
  122. $query = "INSERT INTO " . Util::backquote($cfgBookmark['db'])
  123. . "." . Util::backquote($cfgBookmark['table'])
  124. . " (id, dbase, user, query, label) VALUES (NULL, "
  125. . "'" . $this->dbi->escapeString($this->_database) . "', "
  126. . "'" . $this->dbi->escapeString($this->_user) . "', "
  127. . "'" . $this->dbi->escapeString($this->_query) . "', "
  128. . "'" . $this->dbi->escapeString($this->_label) . "')";
  129. return $this->dbi->query($query, DatabaseInterface::CONNECT_CONTROL);
  130. }
  131. /**
  132. * Deletes a bookmark
  133. *
  134. * @return bool true if successful
  135. *
  136. * @access public
  137. */
  138. public function delete()
  139. {
  140. $cfgBookmark = self::getParams($this->user);
  141. if (empty($cfgBookmark)) {
  142. return false;
  143. }
  144. $query = "DELETE FROM " . Util::backquote($cfgBookmark['db'])
  145. . "." . Util::backquote($cfgBookmark['table'])
  146. . " WHERE id = " . $this->_id;
  147. return $this->dbi->tryQuery($query, DatabaseInterface::CONNECT_CONTROL);
  148. }
  149. /**
  150. * Returns the number of variables in a bookmark
  151. *
  152. * @return number number of variables
  153. */
  154. public function getVariableCount()
  155. {
  156. $matches = array();
  157. preg_match_all("/\[VARIABLE[0-9]*\]/", $this->_query, $matches, PREG_SET_ORDER);
  158. return count($matches);
  159. }
  160. /**
  161. * Replace the placeholders in the bookmark query with variables
  162. *
  163. * @param array $variables array of variables
  164. *
  165. * @return string query with variables applied
  166. */
  167. public function applyVariables(array $variables)
  168. {
  169. // remove comments that encloses a variable placeholder
  170. $query = preg_replace(
  171. '|/\*(.*\[VARIABLE[0-9]*\].*)\*/|imsU',
  172. '${1}',
  173. $this->_query
  174. );
  175. // replace variable placeholders with values
  176. $number_of_variables = $this->getVariableCount();
  177. for ($i = 1; $i <= $number_of_variables; $i++) {
  178. $var = '';
  179. if (! empty($variables[$i])) {
  180. $var = $this->dbi->escapeString($variables[$i]);
  181. }
  182. $query = str_replace('[VARIABLE' . $i . ']', $var, $query);
  183. // backward compatibility
  184. if ($i == 1) {
  185. $query = str_replace('[VARIABLE]', $var, $query);
  186. }
  187. }
  188. return $query;
  189. }
  190. /**
  191. * Defines the bookmark parameters for the current user
  192. *
  193. * @return array the bookmark parameters for the current user
  194. * @access public
  195. */
  196. public static function getParams($user)
  197. {
  198. static $cfgBookmark = null;
  199. if (null !== $cfgBookmark) {
  200. return $cfgBookmark;
  201. }
  202. $relation = new Relation();
  203. $cfgRelation = $relation->getRelationsParam();
  204. if ($cfgRelation['bookmarkwork']) {
  205. $cfgBookmark = array(
  206. 'user' => $user,
  207. 'db' => $cfgRelation['db'],
  208. 'table' => $cfgRelation['bookmark'],
  209. );
  210. } else {
  211. $cfgBookmark = false;
  212. }
  213. return $cfgBookmark;
  214. }
  215. /**
  216. * Creates a Bookmark object from the parameters
  217. *
  218. * @param array $bkm_fields the properties of the bookmark to add; here,
  219. * $bkm_fields['bkm_sql_query'] is urlencoded
  220. * @param boolean $all_users whether to make the bookmark available
  221. * for all users
  222. *
  223. * @return Bookmark|false
  224. */
  225. public static function createBookmark(
  226. DatabaseInterface $dbi,
  227. $user,
  228. array $bkm_fields,
  229. $all_users = false
  230. ) {
  231. if (!(isset($bkm_fields['bkm_sql_query'])
  232. && strlen($bkm_fields['bkm_sql_query']) > 0
  233. && isset($bkm_fields['bkm_label'])
  234. && strlen($bkm_fields['bkm_label']) > 0)
  235. ) {
  236. return false;
  237. }
  238. $bookmark = new Bookmark($dbi, $user);
  239. $bookmark->_database = $bkm_fields['bkm_database'];
  240. $bookmark->_label = $bkm_fields['bkm_label'];
  241. $bookmark->_query = $bkm_fields['bkm_sql_query'];
  242. $bookmark->_user = $all_users ? '' : $bkm_fields['bkm_user'];
  243. return $bookmark;
  244. }
  245. /**
  246. * Gets the list of bookmarks defined for the current database
  247. *
  248. * @param string|bool $db the current database name or false
  249. *
  250. * @return Bookmark[] the bookmarks list
  251. *
  252. * @access public
  253. */
  254. public static function getList(DatabaseInterface $dbi, $user, $db = false)
  255. {
  256. $cfgBookmark = self::getParams($user);
  257. if (empty($cfgBookmark)) {
  258. return array();
  259. }
  260. $query = "SELECT * FROM " . Util::backquote($cfgBookmark['db'])
  261. . "." . Util::backquote($cfgBookmark['table'])
  262. . " WHERE ( `user` = ''"
  263. . " OR `user` = '" . $dbi->escapeString($cfgBookmark['user']) . "' )";
  264. if ($db !== false) {
  265. $query .= " AND dbase = '" . $dbi->escapeString($db) . "'";
  266. }
  267. $query .= " ORDER BY label ASC";
  268. $result = $dbi->fetchResult(
  269. $query,
  270. null,
  271. null,
  272. DatabaseInterface::CONNECT_CONTROL,
  273. DatabaseInterface::QUERY_STORE
  274. );
  275. if (! empty($result)) {
  276. $bookmarks = array();
  277. foreach ($result as $row) {
  278. $bookmark = new Bookmark($dbi, $user);
  279. $bookmark->_id = $row['id'];
  280. $bookmark->_database = $row['dbase'];
  281. $bookmark->_user = $row['user'];
  282. $bookmark->_label = $row['label'];
  283. $bookmark->_query = $row['query'];
  284. $bookmarks[] = $bookmark;
  285. }
  286. return $bookmarks;
  287. }
  288. return array();
  289. }
  290. /**
  291. * Retrieve a specific bookmark
  292. *
  293. * @param string $db the current database name
  294. * @param mixed $id an identifier of the bookmark to get
  295. * @param string $id_field which field to look up the identifier
  296. * @param boolean $action_bookmark_all true: get all bookmarks regardless
  297. * of the owning user
  298. * @param boolean $exact_user_match whether to ignore bookmarks with no user
  299. *
  300. * @return Bookmark the bookmark
  301. *
  302. * @access public
  303. *
  304. */
  305. public static function get(
  306. DatabaseInterface $dbi,
  307. $user,
  308. $db,
  309. $id,
  310. $id_field = 'id',
  311. $action_bookmark_all = false,
  312. $exact_user_match = false
  313. ) {
  314. $cfgBookmark = self::getParams($user);
  315. if (empty($cfgBookmark)) {
  316. return null;
  317. }
  318. $query = "SELECT * FROM " . Util::backquote($cfgBookmark['db'])
  319. . "." . Util::backquote($cfgBookmark['table'])
  320. . " WHERE dbase = '" . $dbi->escapeString($db) . "'";
  321. if (! $action_bookmark_all) {
  322. $query .= " AND (user = '"
  323. . $dbi->escapeString($cfgBookmark['user']) . "'";
  324. if (! $exact_user_match) {
  325. $query .= " OR user = ''";
  326. }
  327. $query .= ")";
  328. }
  329. $query .= " AND " . Util::backquote($id_field)
  330. . " = " . $dbi->escapeString($id) . " LIMIT 1";
  331. $result = $dbi->fetchSingleRow($query, 'ASSOC', DatabaseInterface::CONNECT_CONTROL);
  332. if (! empty($result)) {
  333. $bookmark = new Bookmark($dbi, $user);
  334. $bookmark->_id = $result['id'];
  335. $bookmark->_database = $result['dbase'];
  336. $bookmark->_user = $result['user'];
  337. $bookmark->_label = $result['label'];
  338. $bookmark->_query = $result['query'];
  339. return $bookmark;
  340. }
  341. return null;
  342. }
  343. }