Bookmark.php 11 KB

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