Bookmark.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. * @param string $user Current user
  194. * @return array the bookmark parameters for the current user
  195. * @access public
  196. */
  197. public static function getParams($user)
  198. {
  199. static $cfgBookmark = null;
  200. if (null !== $cfgBookmark) {
  201. return $cfgBookmark;
  202. }
  203. $relation = new Relation();
  204. $cfgRelation = $relation->getRelationsParam();
  205. if ($cfgRelation['bookmarkwork']) {
  206. $cfgBookmark = array(
  207. 'user' => $user,
  208. 'db' => $cfgRelation['db'],
  209. 'table' => $cfgRelation['bookmark'],
  210. );
  211. } else {
  212. $cfgBookmark = false;
  213. }
  214. return $cfgBookmark;
  215. }
  216. /**
  217. * Creates a Bookmark object from the parameters
  218. *
  219. * @param DatabaseInterface $dbi DatabaseInterface object
  220. * @param string $user Current user
  221. * @param array $bkm_fields the properties of the bookmark to add; here,
  222. * $bkm_fields['bkm_sql_query'] is urlencoded
  223. * @param boolean $all_users whether to make the bookmark
  224. * available for all users
  225. *
  226. * @return Bookmark|false
  227. */
  228. public static function createBookmark(
  229. DatabaseInterface $dbi,
  230. $user,
  231. array $bkm_fields,
  232. $all_users = false
  233. ) {
  234. if (!(isset($bkm_fields['bkm_sql_query'])
  235. && strlen($bkm_fields['bkm_sql_query']) > 0
  236. && isset($bkm_fields['bkm_label'])
  237. && strlen($bkm_fields['bkm_label']) > 0)
  238. ) {
  239. return false;
  240. }
  241. $bookmark = new Bookmark($dbi, $user);
  242. $bookmark->_database = $bkm_fields['bkm_database'];
  243. $bookmark->_label = $bkm_fields['bkm_label'];
  244. $bookmark->_query = $bkm_fields['bkm_sql_query'];
  245. $bookmark->_user = $all_users ? '' : $bkm_fields['bkm_user'];
  246. return $bookmark;
  247. }
  248. /**
  249. * Gets the list of bookmarks defined for the current database
  250. *
  251. * @param DatabaseInterface $dbi DatabaseInterface object
  252. * @param string $user Current user
  253. * @param string|bool $db the current database name or false
  254. *
  255. * @return Bookmark[] the bookmarks list
  256. *
  257. * @access public
  258. */
  259. public static function getList(DatabaseInterface $dbi, $user, $db = false)
  260. {
  261. $cfgBookmark = self::getParams($user);
  262. if (empty($cfgBookmark)) {
  263. return array();
  264. }
  265. $query = "SELECT * FROM " . Util::backquote($cfgBookmark['db'])
  266. . "." . Util::backquote($cfgBookmark['table'])
  267. . " WHERE ( `user` = ''"
  268. . " OR `user` = '" . $dbi->escapeString($cfgBookmark['user']) . "' )";
  269. if ($db !== false) {
  270. $query .= " AND dbase = '" . $dbi->escapeString($db) . "'";
  271. }
  272. $query .= " ORDER BY label ASC";
  273. $result = $dbi->fetchResult(
  274. $query,
  275. null,
  276. null,
  277. DatabaseInterface::CONNECT_CONTROL,
  278. DatabaseInterface::QUERY_STORE
  279. );
  280. if (! empty($result)) {
  281. $bookmarks = array();
  282. foreach ($result as $row) {
  283. $bookmark = new Bookmark($dbi, $user);
  284. $bookmark->_id = $row['id'];
  285. $bookmark->_database = $row['dbase'];
  286. $bookmark->_user = $row['user'];
  287. $bookmark->_label = $row['label'];
  288. $bookmark->_query = $row['query'];
  289. $bookmarks[] = $bookmark;
  290. }
  291. return $bookmarks;
  292. }
  293. return array();
  294. }
  295. /**
  296. * Retrieve a specific bookmark
  297. *
  298. * @param DatabaseInterface $dbi DatabaseInterface object
  299. * @param string $user Current user
  300. * @param string $db the current database name
  301. * @param mixed $id an identifier of the bookmark to get
  302. * @param string $id_field which field to look up the identifier
  303. * @param boolean $action_bookmark_all true: get all bookmarks regardless
  304. * of the owning user
  305. * @param boolean $exact_user_match whether to ignore bookmarks with no user
  306. *
  307. * @return Bookmark the bookmark
  308. *
  309. * @access public
  310. *
  311. */
  312. public static function get(
  313. DatabaseInterface $dbi,
  314. $user,
  315. $db,
  316. $id,
  317. $id_field = 'id',
  318. $action_bookmark_all = false,
  319. $exact_user_match = false
  320. ) {
  321. $cfgBookmark = self::getParams($user);
  322. if (empty($cfgBookmark)) {
  323. return null;
  324. }
  325. $query = "SELECT * FROM " . Util::backquote($cfgBookmark['db'])
  326. . "." . Util::backquote($cfgBookmark['table'])
  327. . " WHERE dbase = '" . $dbi->escapeString($db) . "'";
  328. if (! $action_bookmark_all) {
  329. $query .= " AND (user = '"
  330. . $dbi->escapeString($cfgBookmark['user']) . "'";
  331. if (! $exact_user_match) {
  332. $query .= " OR user = ''";
  333. }
  334. $query .= ")";
  335. }
  336. $query .= " AND " . Util::backquote($id_field)
  337. . " = '" . $dbi->escapeString($id) . "' LIMIT 1";
  338. $result = $dbi->fetchSingleRow($query, 'ASSOC', DatabaseInterface::CONNECT_CONTROL);
  339. if (! empty($result)) {
  340. $bookmark = new Bookmark($dbi, $user);
  341. $bookmark->_id = $result['id'];
  342. $bookmark->_database = $result['dbase'];
  343. $bookmark->_user = $result['user'];
  344. $bookmark->_label = $result['label'];
  345. $bookmark->_query = $result['query'];
  346. return $bookmark;
  347. }
  348. return null;
  349. }
  350. }