RecentTable.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. if (! defined('PHPMYADMIN')) {
  8. exit;
  9. }
  10. require_once './libraries/Message.class.php';
  11. /**
  12. * Handles the recently used tables.
  13. *
  14. * @TODO Change the release version in table pma_recent
  15. * (#recent in documentation)
  16. *
  17. * @package PhpMyAdmin
  18. */
  19. class PMA_RecentTable
  20. {
  21. /**
  22. * Defines the internal PMA table which contains recent tables.
  23. *
  24. * @access private
  25. * @var string
  26. */
  27. private $_pmaTable;
  28. /**
  29. * Reference to session variable containing recently used tables.
  30. *
  31. * @access public
  32. * @var array
  33. */
  34. public $tables;
  35. /**
  36. * PMA_RecentTable instance.
  37. *
  38. * @var PMA_RecentTable
  39. */
  40. private static $_instance;
  41. public function __construct()
  42. {
  43. if (strlen($GLOBALS['cfg']['Server']['pmadb'])
  44. && strlen($GLOBALS['cfg']['Server']['recent'])
  45. ) {
  46. $this->_pmaTable
  47. = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "."
  48. . PMA_Util::backquote($GLOBALS['cfg']['Server']['recent']);
  49. }
  50. $server_id = $GLOBALS['server'];
  51. if (! isset($_SESSION['tmp_user_values']['recent_tables'][$server_id])) {
  52. $_SESSION['tmp_user_values']['recent_tables'][$server_id]
  53. = isset($this->_pmaTable) ? $this->getFromDb() : array();
  54. }
  55. $this->tables =& $_SESSION['tmp_user_values']['recent_tables'][$server_id];
  56. }
  57. /**
  58. * Returns class instance.
  59. *
  60. * @return PMA_RecentTable
  61. */
  62. public static function getInstance()
  63. {
  64. if (is_null(self::$_instance)) {
  65. self::$_instance = new PMA_RecentTable();
  66. }
  67. return self::$_instance;
  68. }
  69. /**
  70. * Returns recently used tables from phpMyAdmin database.
  71. *
  72. * @return array
  73. */
  74. public function getFromDb()
  75. {
  76. // Read from phpMyAdmin database, if recent tables is not in session
  77. $sql_query
  78. = " SELECT `tables` FROM " . $this->_pmaTable .
  79. " WHERE `username` = '" . PMA_Util::sqlAddSlashes($GLOBALS['cfg']['Server']['user']) . "'";
  80. $return = array();
  81. $result = PMA_queryAsControlUser($sql_query, false);
  82. if ($result) {
  83. $row = PMA_DBI_fetch_array($result);
  84. if (isset($row[0])) {
  85. $return = json_decode($row[0], true);
  86. }
  87. }
  88. return $return;
  89. }
  90. /**
  91. * Save recent tables into phpMyAdmin database.
  92. *
  93. * @return true|PMA_Message
  94. */
  95. public function saveToDb()
  96. {
  97. $username = $GLOBALS['cfg']['Server']['user'];
  98. $sql_query
  99. = " REPLACE INTO " . $this->_pmaTable . " (`username`, `tables`)" .
  100. " VALUES ('" . $username . "', '"
  101. . PMA_Util::sqlAddSlashes(
  102. json_encode($this->tables)
  103. ) . "')";
  104. $success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']);
  105. if (! $success) {
  106. $message = PMA_Message::error(__('Could not save recent table'));
  107. $message->addMessage('<br /><br />');
  108. $message->addMessage(
  109. PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink']))
  110. );
  111. return $message;
  112. }
  113. return true;
  114. }
  115. /**
  116. * Trim recent table according to the NumRecentTables configuration.
  117. *
  118. * @return boolean True if trimming occurred
  119. */
  120. public function trim()
  121. {
  122. $max = max($GLOBALS['cfg']['NumRecentTables'], 0);
  123. $trimming_occured = count($this->tables) > $max;
  124. while (count($this->tables) > $max) {
  125. array_pop($this->tables);
  126. }
  127. return $trimming_occured;
  128. }
  129. /**
  130. * Return options for HTML select.
  131. *
  132. * @return string
  133. */
  134. public function getHtmlSelectOption()
  135. {
  136. // trim and save, in case where the configuration is changed
  137. if ($this->trim() && isset($this->_pmaTable)) {
  138. $this->saveToDb();
  139. }
  140. $html = '<option value="">(' . __('Recent tables') . ') ...</option>';
  141. if (count($this->tables)) {
  142. foreach ($this->tables as $table) {
  143. $html .= '<option value="'
  144. . htmlspecialchars(json_encode($table)) . '">'
  145. . htmlspecialchars(
  146. '`' . $table['db'] . '`.`' . $table['table'] . '`'
  147. )
  148. . '</option>';
  149. }
  150. } else {
  151. $html .= '<option value="">'
  152. . __('There are no recent tables')
  153. . '</option>';
  154. }
  155. return $html;
  156. }
  157. /**
  158. * Return HTML select.
  159. *
  160. * @return string
  161. */
  162. public function getHtmlSelect()
  163. {
  164. $html = '<select name="selected_recent_table" id="recentTable">';
  165. $html .= $this->getHtmlSelectOption();
  166. $html .= '</select>';
  167. return $html;
  168. }
  169. /**
  170. * Add recently used tables.
  171. *
  172. * @param string $db database name where the table is located
  173. * @param string $table table name
  174. *
  175. * @return true|PMA_Message True if success, PMA_Message if not
  176. */
  177. public function add($db, $table)
  178. {
  179. $table_arr = array();
  180. $table_arr['db'] = $db;
  181. $table_arr['table'] = $table;
  182. // add only if this is new table
  183. if (! isset($this->tables[0]) || $this->tables[0] != $table_arr) {
  184. array_unshift($this->tables, $table_arr);
  185. $this->tables = array_merge(array_unique($this->tables, SORT_REGULAR));
  186. $this->trim();
  187. if (isset($this->_pmaTable)) {
  188. return $this->saveToDb();
  189. }
  190. }
  191. return true;
  192. }
  193. }
  194. ?>