UserPreferences.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\UserPreferences class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use PhpMyAdmin\Config\ConfigFile;
  10. use PhpMyAdmin\Config\Forms\User\UserFormList;
  11. use PhpMyAdmin\Core;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Relation;
  14. use PhpMyAdmin\Template;
  15. use PhpMyAdmin\Url;
  16. use PhpMyAdmin\Util;
  17. /**
  18. * Functions for displaying user preferences pages
  19. *
  20. * @package PhpMyAdmin
  21. */
  22. class UserPreferences
  23. {
  24. /**
  25. * @var Relation $relation
  26. */
  27. private $relation;
  28. /**
  29. * Constructor
  30. */
  31. public function __construct()
  32. {
  33. $this->relation = new Relation();
  34. }
  35. /**
  36. * Common initialization for user preferences modification pages
  37. *
  38. * @param ConfigFile $cf Config file instance
  39. *
  40. * @return void
  41. */
  42. public function pageInit(ConfigFile $cf)
  43. {
  44. $forms_all_keys = UserFormList::getFields();
  45. $cf->resetConfigData(); // start with a clean instance
  46. $cf->setAllowedKeys($forms_all_keys);
  47. $cf->setCfgUpdateReadMapping(
  48. array(
  49. 'Server/hide_db' => 'Servers/1/hide_db',
  50. 'Server/only_db' => 'Servers/1/only_db'
  51. )
  52. );
  53. $cf->updateWithGlobalConfig($GLOBALS['cfg']);
  54. }
  55. /**
  56. * Loads user preferences
  57. *
  58. * Returns an array:
  59. * * config_data - path => value pairs
  60. * * mtime - last modification time
  61. * * type - 'db' (config read from pmadb) or 'session' (read from user session)
  62. *
  63. * @return array
  64. */
  65. public function load()
  66. {
  67. $cfgRelation = $this->relation->getRelationsParam();
  68. if (! $cfgRelation['userconfigwork']) {
  69. // no pmadb table, use session storage
  70. if (! isset($_SESSION['userconfig'])) {
  71. $_SESSION['userconfig'] = array(
  72. 'db' => array(),
  73. 'ts' => time());
  74. }
  75. return array(
  76. 'config_data' => $_SESSION['userconfig']['db'],
  77. 'mtime' => $_SESSION['userconfig']['ts'],
  78. 'type' => 'session');
  79. }
  80. // load configuration from pmadb
  81. $query_table = Util::backquote($cfgRelation['db']) . '.'
  82. . Util::backquote($cfgRelation['userconfig']);
  83. $query = 'SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts'
  84. . ' FROM ' . $query_table
  85. . ' WHERE `username` = \''
  86. . $GLOBALS['dbi']->escapeString($cfgRelation['user'])
  87. . '\'';
  88. $row = $GLOBALS['dbi']->fetchSingleRow($query, 'ASSOC', DatabaseInterface::CONNECT_CONTROL);
  89. return array(
  90. 'config_data' => $row ? json_decode($row['config_data'], true) : array(),
  91. 'mtime' => $row ? $row['ts'] : time(),
  92. 'type' => 'db');
  93. }
  94. /**
  95. * Saves user preferences
  96. *
  97. * @param array $config_array configuration array
  98. *
  99. * @return true|PhpMyAdmin\Message
  100. */
  101. public function save(array $config_array)
  102. {
  103. $cfgRelation = $this->relation->getRelationsParam();
  104. $server = isset($GLOBALS['server'])
  105. ? $GLOBALS['server']
  106. : $GLOBALS['cfg']['ServerDefault'];
  107. $cache_key = 'server_' . $server;
  108. if (! $cfgRelation['userconfigwork']) {
  109. // no pmadb table, use session storage
  110. $_SESSION['userconfig'] = array(
  111. 'db' => $config_array,
  112. 'ts' => time());
  113. if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
  114. unset($_SESSION['cache'][$cache_key]['userprefs']);
  115. }
  116. return true;
  117. }
  118. // save configuration to pmadb
  119. $query_table = Util::backquote($cfgRelation['db']) . '.'
  120. . Util::backquote($cfgRelation['userconfig']);
  121. $query = 'SELECT `username` FROM ' . $query_table
  122. . ' WHERE `username` = \''
  123. . $GLOBALS['dbi']->escapeString($cfgRelation['user'])
  124. . '\'';
  125. $has_config = $GLOBALS['dbi']->fetchValue(
  126. $query, 0, 0, DatabaseInterface::CONNECT_CONTROL
  127. );
  128. $config_data = json_encode($config_array);
  129. if ($has_config) {
  130. $query = 'UPDATE ' . $query_table
  131. . ' SET `timevalue` = NOW(), `config_data` = \''
  132. . $GLOBALS['dbi']->escapeString($config_data)
  133. . '\''
  134. . ' WHERE `username` = \''
  135. . $GLOBALS['dbi']->escapeString($cfgRelation['user'])
  136. . '\'';
  137. } else {
  138. $query = 'INSERT INTO ' . $query_table
  139. . ' (`username`, `timevalue`,`config_data`) '
  140. . 'VALUES (\''
  141. . $GLOBALS['dbi']->escapeString($cfgRelation['user']) . '\', NOW(), '
  142. . '\'' . $GLOBALS['dbi']->escapeString($config_data) . '\')';
  143. }
  144. if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
  145. unset($_SESSION['cache'][$cache_key]['userprefs']);
  146. }
  147. if (!$GLOBALS['dbi']->tryQuery($query, DatabaseInterface::CONNECT_CONTROL)) {
  148. $message = Message::error(__('Could not save configuration'));
  149. $message->addMessage(
  150. Message::rawError(
  151. $GLOBALS['dbi']->getError(DatabaseInterface::CONNECT_CONTROL)
  152. ),
  153. '<br /><br />'
  154. );
  155. return $message;
  156. }
  157. return true;
  158. }
  159. /**
  160. * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
  161. * (blacklist) and keys from user preferences form (whitelist)
  162. *
  163. * @param array $config_data path => value pairs
  164. *
  165. * @return array
  166. */
  167. public function apply(array $config_data)
  168. {
  169. $cfg = array();
  170. $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
  171. $whitelist = array_flip(UserFormList::getFields());
  172. // whitelist some additional fields which are custom handled
  173. $whitelist['ThemeDefault'] = true;
  174. $whitelist['lang'] = true;
  175. $whitelist['Server/hide_db'] = true;
  176. $whitelist['Server/only_db'] = true;
  177. $whitelist['2fa'] = true;
  178. foreach ($config_data as $path => $value) {
  179. if (! isset($whitelist[$path]) || isset($blacklist[$path])) {
  180. continue;
  181. }
  182. Core::arrayWrite($path, $cfg, $value);
  183. }
  184. return $cfg;
  185. }
  186. /**
  187. * Updates one user preferences option (loads and saves to database).
  188. *
  189. * No validation is done!
  190. *
  191. * @param string $path configuration
  192. * @param mixed $value value
  193. * @param mixed $default_value default value
  194. *
  195. * @return true|PhpMyAdmin\Message
  196. */
  197. public function persistOption($path, $value, $default_value)
  198. {
  199. $prefs = $this->load();
  200. if ($value === $default_value) {
  201. if (isset($prefs['config_data'][$path])) {
  202. unset($prefs['config_data'][$path]);
  203. } else {
  204. return true;
  205. }
  206. } else {
  207. $prefs['config_data'][$path] = $value;
  208. }
  209. return $this->save($prefs['config_data']);
  210. }
  211. /**
  212. * Redirects after saving new user preferences
  213. *
  214. * @param string $file_name Filename
  215. * @param array|null $params URL parameters
  216. * @param string $hash Hash value
  217. *
  218. * @return void
  219. */
  220. public function redirect($file_name,
  221. $params = null, $hash = null
  222. ) {
  223. // redirect
  224. $url_params = array('saved' => 1);
  225. if (is_array($params)) {
  226. $url_params = array_merge($params, $url_params);
  227. }
  228. if ($hash) {
  229. $hash = '#' . urlencode($hash);
  230. }
  231. Core::sendHeaderLocation('./' . $file_name
  232. . Url::getCommonRaw($url_params) . $hash
  233. );
  234. }
  235. /**
  236. * Shows form which allows to quickly load
  237. * settings stored in browser's local storage
  238. *
  239. * @return string
  240. */
  241. public function autoloadGetHeader()
  242. {
  243. if (isset($_REQUEST['prefs_autoload'])
  244. && $_REQUEST['prefs_autoload'] == 'hide'
  245. ) {
  246. $_SESSION['userprefs_autoload'] = true;
  247. return '';
  248. }
  249. $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
  250. $return_url = $script_name . '?' . http_build_query($_GET, '', '&');
  251. return Template::get('prefs_autoload')
  252. ->render(
  253. array(
  254. 'hidden_inputs' => Url::getHiddenInputs(),
  255. 'return_url' => $return_url,
  256. )
  257. );
  258. }
  259. }