PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/public/phpmyadmin/libraries/RecentFavoriteTable.class.php

https://gitlab.com/qbarbosa/klindev
PHP | 392 lines | 243 code | 27 blank | 122 comment | 35 complexity | 197d1a5f35bd112b2053a87f2d579f21 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Recent and Favorite table list handling
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. require_once './libraries/Message.class.php';
  12. /**
  13. * Handles the recently used and favorite tables.
  14. *
  15. * @TODO Change the release version in table pma_recent
  16. * (#recent in documentation)
  17. *
  18. * @package PhpMyAdmin
  19. */
  20. class PMA_RecentFavoriteTable
  21. {
  22. /**
  23. * Reference to session variable containing recently used or favorite tables.
  24. *
  25. * @access private
  26. * @var array
  27. */
  28. private $_tables;
  29. /**
  30. * Defines type of action, Favorite or Recent table.
  31. *
  32. * @access private
  33. * @var string
  34. */
  35. private $_tableType;
  36. /**
  37. * PMA_RecentFavoriteTable instances.
  38. *
  39. * @access private
  40. * @var array
  41. */
  42. private static $_instances = array();
  43. /**
  44. * Creates a new instance of PMA_RecentFavoriteTable
  45. *
  46. * @param string $type the table type
  47. *
  48. * @access private
  49. */
  50. private function __construct($type)
  51. {
  52. $this->_tableType = $type;
  53. $server_id = $GLOBALS['server'];
  54. if (! isset($_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id])
  55. ) {
  56. $_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id]
  57. = $this->_getPmaTable() ? $this->getFromDb() : array();
  58. }
  59. $this->_tables
  60. =& $_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id];
  61. }
  62. /**
  63. * Returns class instance.
  64. *
  65. * @param string $type the table type
  66. *
  67. * @return PMA_RecentFavoriteTable
  68. */
  69. public static function getInstance($type)
  70. {
  71. if (! array_key_exists($type, self::$_instances)) {
  72. self::$_instances[$type] = new PMA_RecentFavoriteTable($type);
  73. }
  74. return self::$_instances[$type];
  75. }
  76. /**
  77. * Returns the recent/favorite tables array
  78. *
  79. * @return array
  80. */
  81. public function getTables()
  82. {
  83. return $this->_tables;
  84. }
  85. /**
  86. * Returns recently used tables or favorite from phpMyAdmin database.
  87. *
  88. * @return array
  89. */
  90. public function getFromDb()
  91. {
  92. // Read from phpMyAdmin database, if recent tables is not in session
  93. $sql_query
  94. = " SELECT `tables` FROM " . $this->_getPmaTable() .
  95. " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
  96. $return = array();
  97. $result = PMA_queryAsControlUser($sql_query, false);
  98. if ($result) {
  99. $row = $GLOBALS['dbi']->fetchArray($result);
  100. if (isset($row[0])) {
  101. $return = json_decode($row[0], true);
  102. }
  103. }
  104. return $return;
  105. }
  106. /**
  107. * Save recent/favorite tables into phpMyAdmin database.
  108. *
  109. * @return true|PMA_Message
  110. */
  111. public function saveToDb()
  112. {
  113. $username = $GLOBALS['cfg']['Server']['user'];
  114. $sql_query
  115. = " REPLACE INTO " . $this->_getPmaTable() . " (`username`, `tables`)" .
  116. " VALUES ('" . $username . "', '"
  117. . PMA_Util::sqlAddSlashes(
  118. json_encode($this->_tables)
  119. ) . "')";
  120. $success = $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['controllink']);
  121. if (! $success) {
  122. $error_msg = '';
  123. switch ($this->_tableType) {
  124. case 'recent':
  125. $error_msg = __('Could not save recent table!');
  126. break;
  127. case 'favorite':
  128. $error_msg = __('Could not save favorite table!');
  129. break;
  130. }
  131. $message = PMA_Message::error($error_msg);
  132. $message->addMessage('<br /><br />');
  133. $message->addMessage(
  134. PMA_Message::rawError(
  135. $GLOBALS['dbi']->getError($GLOBALS['controllink'])
  136. )
  137. );
  138. return $message;
  139. }
  140. return true;
  141. }
  142. /**
  143. * Trim recent.favorite table according to the
  144. * NumRecentTables/NumFavoriteTables configuration.
  145. *
  146. * @return boolean True if trimming occurred
  147. */
  148. public function trim()
  149. {
  150. $max = max(
  151. $GLOBALS['cfg']['Num' . ucfirst($this->_tableType) . 'Tables'], 0
  152. );
  153. $trimming_occurred = count($this->_tables) > $max;
  154. while (count($this->_tables) > $max) {
  155. array_pop($this->_tables);
  156. }
  157. return $trimming_occurred;
  158. }
  159. /**
  160. * Return HTML ul.
  161. *
  162. * @return string
  163. */
  164. public function getHtmlList()
  165. {
  166. $html = '';
  167. if (count($this->_tables)) {
  168. if ($this->_tableType == 'recent') {
  169. foreach ($this->_tables as $table) {
  170. $html .= '<li class="warp_link">';
  171. $recent_params = array(
  172. 'db' => $table['db'],
  173. 'table' => $table['table']
  174. );
  175. $recent_url = 'tbl_recent_favorite.php'
  176. . PMA_URL_getCommon($recent_params);
  177. $html .= '<a href="' . $recent_url . '">`'
  178. . htmlspecialchars($table['db']) . '`.`'
  179. . htmlspecialchars($table['table']) . '`</a>';
  180. $html .= '</li>';
  181. }
  182. } else {
  183. foreach ($this->_tables as $table) {
  184. $html .= '<li class="warp_link">';
  185. $html .= '<a class="ajax favorite_table_anchor" ';
  186. $fav_params = array(
  187. 'db' => $table['db'],
  188. 'ajax_request' => true,
  189. 'favorite_table' => $table['table'],
  190. 'remove_favorite' => true
  191. );
  192. $fav_rm_url = 'db_structure.php'
  193. . PMA_URL_getCommon($fav_params);
  194. $html .= 'href="' . $fav_rm_url
  195. . '" title="' . __("Remove from Favorites")
  196. . '" data-favtargetn="'
  197. . md5($table['db'] . "." . $table['table'])
  198. . '" >'
  199. . PMA_Util::getIcon('b_favorite.png')
  200. . '</a>';
  201. $fav_params = array(
  202. 'db' => $table['db'],
  203. 'table' => $table['table']
  204. );
  205. $table_url = 'tbl_recent_favorite.php'
  206. . PMA_URL_getCommon($fav_params);
  207. $html .= '<a href="' . $table_url . '">`'
  208. . htmlspecialchars($table['db']) . '`.`'
  209. . htmlspecialchars($table['table']) . '`</a>';
  210. $html .= '</li>';
  211. }
  212. }
  213. } else {
  214. $html .= '<li class="warp_link">'
  215. . ($this->_tableType == 'recent'
  216. ?__('There are no recent tables.')
  217. :__('There are no favorite tables.'))
  218. . '</li>';
  219. }
  220. return $html;
  221. }
  222. /**
  223. * Return HTML.
  224. *
  225. * @return string
  226. */
  227. public function getHtml()
  228. {
  229. $html = '<div class="drop_list">';
  230. if ($this->_tableType == 'recent') {
  231. $html .= '<span title="' . __('Recent tables')
  232. . '" class="drop_button">'
  233. . __('Recent') . '</span><ul id="pma_recent_list">';
  234. } else {
  235. $html .= '<span title="' . __('Favorite tables')
  236. . '" class="drop_button">'
  237. . __('Favorites') . '</span><ul id="pma_favorite_list">';
  238. }
  239. $html .= $this->getHtmlList();
  240. $html .= '</ul></div>';
  241. return $html;
  242. }
  243. /**
  244. * Add recently used or favorite tables.
  245. *
  246. * @param string $db database name where the table is located
  247. * @param string $table table name
  248. *
  249. * @return true|PMA_Message True if success, PMA_Message if not
  250. */
  251. public function add($db, $table)
  252. {
  253. // If table does not exist, do not add._getPmaTable()
  254. if (! $GLOBALS['dbi']->getColumns($db, $table)) {
  255. return true;
  256. }
  257. $table_arr = array();
  258. $table_arr['db'] = $db;
  259. $table_arr['table'] = $table;
  260. // add only if this is new table
  261. if (! isset($this->_tables[0]) || $this->_tables[0] != $table_arr) {
  262. array_unshift($this->_tables, $table_arr);
  263. $this->_tables = array_merge(array_unique($this->_tables, SORT_REGULAR));
  264. $this->trim();
  265. if ($this->_getPmaTable()) {
  266. return $this->saveToDb();
  267. }
  268. }
  269. return true;
  270. }
  271. /**
  272. * Removes recent/favorite tables that don't exist.
  273. *
  274. * @param string $db database
  275. * @param string $table table
  276. *
  277. * @return boolean|PMA_Message True if invalid and removed, False if not invalid,
  278. * PMA_Message if error while removing
  279. */
  280. public function removeIfInvalid($db, $table)
  281. {
  282. foreach ($this->_tables as $tbl) {
  283. if ($tbl['db'] == $db && $tbl['table'] == $table) {
  284. // TODO Figure out a better way to find the existence of a table
  285. if (! $GLOBALS['dbi']->getColumns($tbl['db'], $tbl['table'])) {
  286. return $this->remove($tbl['db'], $tbl['table']);
  287. }
  288. }
  289. }
  290. return false;
  291. }
  292. /**
  293. * Remove favorite tables.
  294. *
  295. * @param string $db database name where the table is located
  296. * @param string $table table name
  297. *
  298. * @return true|PMA_Message True if success, PMA_Message if not
  299. */
  300. public function remove($db, $table)
  301. {
  302. $table_arr = array();
  303. $table_arr['db'] = $db;
  304. $table_arr['table'] = $table;
  305. foreach ($this->_tables as $key => $value) {
  306. if ($value['db'] == $db && $value['table'] == $table) {
  307. unset($this->_tables[$key]);
  308. }
  309. }
  310. if ($this->_getPmaTable()) {
  311. return $this->saveToDb();
  312. }
  313. return true;
  314. }
  315. /**
  316. * Generate Html for sync Favorite tables anchor. (from localStorage to pmadb)
  317. *
  318. * @return string
  319. */
  320. public function getHtmlSyncFavoriteTables()
  321. {
  322. $retval = '';
  323. $server_id = $GLOBALS['server'];
  324. // Not to show this once list is synchronized.
  325. $is_synced = isset($_SESSION['tmpval']['favorites_synced'][$server_id]) ?
  326. true : false;
  327. if (!$is_synced) {
  328. $params = array('ajax_request' => true, 'favorite_table' => true,
  329. 'sync_favorite_tables' => true);
  330. $url = 'db_structure.php' . PMA_URL_getCommon($params);
  331. $retval = '<a class="hide" id="sync_favorite_tables"';
  332. $retval .= ' href="' . $url . '"></a>';
  333. }
  334. return $retval;
  335. }
  336. /**
  337. * Generate Html to update recent tables.
  338. *
  339. * @return string html
  340. */
  341. public static function getHtmlUpdateRecentTables()
  342. {
  343. $params = array('ajax_request' => true, 'recent_table' => true);
  344. $url = 'index.php' . PMA_URL_getCommon($params);
  345. $retval = '<a class="hide" id="update_recent_tables"';
  346. $retval .= ' href="' . $url . '"></a>';
  347. return $retval;
  348. }
  349. /**
  350. * Reutrn the name of the configuration storage table
  351. *
  352. * @return string pma table name
  353. */
  354. private function _getPmaTable()
  355. {
  356. $cfgRelation = PMA_getRelationsParam();
  357. if (! empty($cfgRelation['db'])
  358. && ! empty($cfgRelation[$this->_tableType])
  359. ) {
  360. return PMA_Util::backquote($cfgRelation['db']) . "."
  361. . PMA_Util::backquote($cfgRelation[$this->_tableType]);
  362. }
  363. return null;
  364. }
  365. }