user_preferences.lib.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Functions for displaying user preferences pages
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Common initialization for user preferences modification pages
  13. *
  14. * @return void
  15. */
  16. function PMA_userprefsPageInit()
  17. {
  18. $forms_all_keys = PMA_readUserprefsFieldNames($GLOBALS['forms']);
  19. $cf = ConfigFile::getInstance();
  20. $cf->resetConfigData(); // start with a clean instance
  21. $cf->setAllowedKeys($forms_all_keys);
  22. $cf->setCfgUpdateReadMapping(
  23. array(
  24. 'Server/hide_db' => 'Servers/1/hide_db',
  25. 'Server/only_db' => 'Servers/1/only_db'
  26. )
  27. );
  28. $cf->updateWithGlobalConfig($GLOBALS['cfg']);
  29. }
  30. /**
  31. * Loads user preferences
  32. *
  33. * Returns an array:
  34. * * config_data - path => value pairs
  35. * * mtime - last modification time
  36. * * type - 'db' (config read from pmadb) or 'session' (read from user session)
  37. *
  38. * @return array
  39. */
  40. function PMA_loadUserprefs()
  41. {
  42. $cfgRelation = PMA_getRelationsParam();
  43. if (! $cfgRelation['userconfigwork']) {
  44. // no pmadb table, use session storage
  45. if (! isset($_SESSION['userconfig'])) {
  46. $_SESSION['userconfig'] = array(
  47. 'db' => array(),
  48. 'ts' => time());
  49. }
  50. return array(
  51. 'config_data' => $_SESSION['userconfig']['db'],
  52. 'mtime' => $_SESSION['userconfig']['ts'],
  53. 'type' => 'session');
  54. }
  55. // load configuration from pmadb
  56. $query_table = PMA_Util::backquote($cfgRelation['db']) . '.'
  57. . PMA_Util::backquote($cfgRelation['userconfig']);
  58. $query = '
  59. SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts
  60. FROM ' . $query_table . '
  61. WHERE `username` = \'' . PMA_Util::sqlAddSlashes($cfgRelation['user']) . '\'';
  62. $row = PMA_DBI_fetch_single_row($query, 'ASSOC', $GLOBALS['controllink']);
  63. return array(
  64. 'config_data' => $row ? (array)json_decode($row['config_data']) : array(),
  65. 'mtime' => $row ? $row['ts'] : time(),
  66. 'type' => 'db');
  67. }
  68. /**
  69. * Saves user preferences
  70. *
  71. * @param array $config_array configuration array
  72. *
  73. * @return true|PMA_Message
  74. */
  75. function PMA_saveUserprefs(array $config_array)
  76. {
  77. $cfgRelation = PMA_getRelationsParam();
  78. $server = isset($GLOBALS['server'])
  79. ? $GLOBALS['server']
  80. : $GLOBALS['cfg']['ServerDefault'];
  81. $cache_key = 'server_' . $server;
  82. if (! $cfgRelation['userconfigwork']) {
  83. // no pmadb table, use session storage
  84. $_SESSION['userconfig'] = array(
  85. 'db' => $config_array,
  86. 'ts' => time());
  87. if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
  88. unset($_SESSION['cache'][$cache_key]['userprefs']);
  89. }
  90. return true;
  91. }
  92. // save configuration to pmadb
  93. $query_table = PMA_Util::backquote($cfgRelation['db']) . '.'
  94. . PMA_Util::backquote($cfgRelation['userconfig']);
  95. $query = '
  96. SELECT `username`
  97. FROM ' . $query_table . '
  98. WHERE `username` = \'' . PMA_Util::sqlAddSlashes($cfgRelation['user']) . '\'';
  99. $has_config = PMA_DBI_fetch_value($query, 0, 0, $GLOBALS['controllink']);
  100. $config_data = json_encode($config_array);
  101. if ($has_config) {
  102. $query = '
  103. UPDATE ' . $query_table . '
  104. SET `config_data` = \'' . PMA_Util::sqlAddSlashes($config_data) . '\'
  105. WHERE `username` = \'' . PMA_Util::sqlAddSlashes($cfgRelation['user']) . '\'';
  106. } else {
  107. $query = '
  108. INSERT INTO ' . $query_table . ' (`username`, `config_data`)
  109. VALUES (\'' . PMA_Util::sqlAddSlashes($cfgRelation['user']) . '\',
  110. \'' . PMA_Util::sqlAddSlashes($config_data) . '\')';
  111. }
  112. if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
  113. unset($_SESSION['cache'][$cache_key]['userprefs']);
  114. }
  115. if (!PMA_DBI_try_query($query, $GLOBALS['controllink'])) {
  116. $message = PMA_Message::error(__('Could not save configuration'));
  117. $message->addMessage('<br /><br />');
  118. $message->addMessage(
  119. PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink']))
  120. );
  121. return $message;
  122. }
  123. return true;
  124. }
  125. /**
  126. * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
  127. * (blacklist) and keys from user preferences form (whitelist)
  128. *
  129. * @param array $config_data path => value pairs
  130. *
  131. * @return array
  132. */
  133. function PMA_applyUserprefs(array $config_data)
  134. {
  135. $cfg = array();
  136. $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
  137. if (!$GLOBALS['cfg']['UserprefsDeveloperTab']) {
  138. // disallow everything in the Developers tab
  139. $blacklist['Error_Handler/display'] = true;
  140. $blacklist['Error_Handler/gather'] = true;
  141. $blacklist['DBG/sql'] = true;
  142. }
  143. $whitelist = array_flip(PMA_readUserprefsFieldNames());
  144. // whitelist some additional fields which are custom handled
  145. $whitelist['ThemeDefault'] = true;
  146. $whitelist['fontsize'] = true;
  147. $whitelist['lang'] = true;
  148. $whitelist['collation_connection'] = true;
  149. $whitelist['Server/hide_db'] = true;
  150. $whitelist['Server/only_db'] = true;
  151. foreach ($config_data as $path => $value) {
  152. if (! isset($whitelist[$path]) || isset($blacklist[$path])) {
  153. continue;
  154. }
  155. PMA_arrayWrite($path, $cfg, $value);
  156. }
  157. return $cfg;
  158. }
  159. /**
  160. * Reads user preferences field names
  161. *
  162. * @param array|null $forms
  163. *
  164. * @return array
  165. */
  166. function PMA_readUserprefsFieldNames(array $forms = null)
  167. {
  168. static $names;
  169. // return cached results
  170. if ($names !== null) {
  171. return $names;
  172. }
  173. if (is_null($forms)) {
  174. $forms = array();
  175. include 'libraries/config/user_preferences.forms.php';
  176. }
  177. $names = array();
  178. foreach ($forms as $formset) {
  179. foreach ($formset as $form) {
  180. foreach ($form as $k => $v) {
  181. $names[] = is_int($k) ? $v : $k;
  182. }
  183. }
  184. }
  185. return $names;
  186. }
  187. /**
  188. * Updates one user preferences option (loads and saves to database).
  189. *
  190. * No validation is done!
  191. *
  192. * @param string $path configuration
  193. * @param mixed $value value
  194. * @param mixed $default_value default value
  195. *
  196. * @return void
  197. */
  198. function PMA_persistOption($path, $value, $default_value)
  199. {
  200. $prefs = PMA_loadUserprefs();
  201. if ($value === $default_value) {
  202. if (isset($prefs['config_data'][$path])) {
  203. unset($prefs['config_data'][$path]);
  204. } else {
  205. return;
  206. }
  207. } else {
  208. $prefs['config_data'][$path] = $value;
  209. }
  210. PMA_saveUserprefs($prefs['config_data']);
  211. }
  212. /**
  213. * Redirects after saving new user preferences
  214. *
  215. * @param string $file_name
  216. * @param array $params
  217. * @param string $hash
  218. *
  219. * @return void
  220. */
  221. function PMA_userprefsRedirect($file_name,
  222. $params = null, $hash = null
  223. ) {
  224. // redirect
  225. $url_params = array('saved' => 1);
  226. if (is_array($params)) {
  227. $url_params = array_merge($params, $url_params);
  228. }
  229. if ($hash) {
  230. $hash = '#' . urlencode($hash);
  231. }
  232. PMA_sendHeaderLocation(
  233. $GLOBALS['cfg']['PmaAbsoluteUri'] . $file_name
  234. . PMA_generate_common_url($url_params, '&') . $hash
  235. );
  236. }
  237. /**
  238. * Shows form which allows to quickly load
  239. * settings stored in browser's local storage
  240. *
  241. * @return string
  242. */
  243. function PMA_userprefsAutoloadGetHeader()
  244. {
  245. $retval = '';
  246. if (isset($_REQUEST['prefs_autoload'])
  247. && $_REQUEST['prefs_autoload'] == 'hide'
  248. ) {
  249. $_SESSION['userprefs_autoload'] = true;
  250. } else {
  251. $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
  252. $return_url = htmlspecialchars(
  253. $script_name . '?' . http_build_query($_GET, '', '&')
  254. );
  255. $retval .= '<div id="prefs_autoload" class="notice" style="display:none">';
  256. $retval .= '<form action="prefs_manage.php" method="post">';
  257. $retval .= PMA_generate_common_hidden_inputs();
  258. $retval .= '<input type="hidden" name="json" value="" />';
  259. $retval .= '<input type="hidden" name="submit_import" value="1" />';
  260. $retval .= '<input type="hidden" name="return_url" value="' . $return_url . '" />';
  261. $retval .= __(
  262. 'Your browser has phpMyAdmin configuration for this domain. '
  263. . 'Would you like to import it for current session?'
  264. );
  265. $retval .= '<br />';
  266. $retval .= '<a href="#yes">' . __('Yes') . '</a>';
  267. $retval .= ' / ';
  268. $retval .= '<a href="#no">' . __('No') . '</a>';
  269. $retval .= '</form>';
  270. $retval .= '</div>';
  271. }
  272. return $retval;
  273. }
  274. ?>