Files
phpmyadmin/libraries/user_preferences.lib.php
Crack 68d9c40113 moved Import by LOAD DATA settings to CSV category
moved PMA_array_* functions to core.lib.php
moved PMA_read_userprefs_fieldnames() to user_preferences.lib.php
server/database/table filename templates are saved to pmadb when "remember template" is checked
2010-07-04 21:56:46 +02:00

174 lines
4.9 KiB
PHP

<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Functions for displaying user preferences pages
*
* @package phpMyAdmin
*/
/**
* Loads user preferences
*
* Returns false or an array:
* * config_data - path => value pairs
* * mtime - last modification time
*
* @uses PMA_array_merge_recursive
* @uses PMA_backquote()
* @uses PMA_DBI_fetch_single_row()
* @uses PMA_getRelationsParam()
* @uses PMA_sqlAddslashes()
* @uses $GLOBALS['controllink']
* @return false|array
*/
function PMA_load_userprefs()
{
$cfgRelation = PMA_getRelationsParam();
if (!$cfgRelation['userconfigwork']) {
return false;
}
$query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
$query = '
SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts
FROM ' . $query_table . '
WHERE `username` = \'' . PMA_sqlAddslashes($cfgRelation['user']) . '\'';
$row = PMA_DBI_fetch_single_row($query, 'ASSOC', $GLOBALS['controllink']);
if (!$row) {
return false;
}
$config_data = unserialize($row['config_data']);
return array(
'config_data' => $config_data,
'mtime' => $row['ts']);
}
/**
* Saves user preferences
*
* @uses $GLOBALS['controllink']
* @uses $_SESSION['cache']['userprefs']
* @uses ConfigFile::getConfigArray()
* @uses ConfigFile::getInstance()
* @uses PMA_backquote()
* @uses PMA_DBI_fetch_value
* @uses PMA_DBI_getError()
* @uses PMA_DBI_try_query()
* @uses PMA_Message::addMessage()
* @uses PMA_Message::error()
* @uses PMA_Message::rawError()
* @uses PMA_sqlAddslashes()
* @uses PMA_getRelationsParam()
* @param array $config_data
* @return true|PMA_Message
*/
function PMA_save_userprefs(array $config_array)
{
$cfgRelation = PMA_getRelationsParam();
$config_data = serialize($config_array);
$query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
$query = '
SELECT `username`
FROM ' . $query_table . '
WHERE `username` = \'' . PMA_sqlAddslashes($cfgRelation['user']) . '\'';
$has_config = PMA_DBI_fetch_value($query, 0, 0, $GLOBALS['controllink']);
if ($has_config) {
$query = '
UPDATE ' . $query_table . '
SET `config_data` = \'' . PMA_sqlAddslashes($config_data) . '\'
WHERE `username` = \'' . PMA_sqlAddslashes($cfgRelation['user']) . '\'';
} else {
$query = '
INSERT INTO ' . $query_table . ' (`username`, `config_data`)
VALUES (\'' . PMA_sqlAddslashes($cfgRelation['user']) . '\',
\'' . PMA_sqlAddslashes($config_data) . '\')';
}
if (isset($_SESSION['cache']['userprefs'])) {
unset($_SESSION['cache']['userprefs']);
}
if (!PMA_DBI_try_query($query, $GLOBALS['controllink'])) {
$message = PMA_Message::error(__('Could not save configuration'));
$message->addMessage('<br /><br />');
$message->addMessage(PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink'])));
return $message;
}
return true;
}
/**
* Returns a user preferences array filtered by $cfg['UserprefsDisallow']
* (blacklist) and keys from user preferences form (whitelist)
*
* @uses PMA_array_write()
* @uses PMA_read_userprefs_fieldnames()
* @param array $config_data path => value pairs
* @return array
*/
function PMA_apply_userprefs(array $config_data)
{
$cfg = array();
$blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
$whitelist = array_flip(PMA_read_userprefs_fieldnames());
foreach ($config_data as $path => $value) {
if (!isset($whitelist[$path]) || isset($blacklist[$path])) {
continue;
}
PMA_array_write($path, $cfg, $value);
}
return $cfg;
}
/**
* Reads user preferences field names
*
* @param array|null $forms
* @return array
*/
function PMA_read_userprefs_fieldnames(array $forms = null)
{
static $names;
// return cached results
if ($names !== null) {
return $names;
}
if (is_null($forms)) {
$forms = array();
include 'libraries/config/user_preferences.forms.php';
}
$names = array();
foreach ($forms as $formset) {
foreach ($formset as $form) {
foreach ($form as $k => $v) {
$names[] = is_int($k) ? $v : $k;
}
}
}
return $names;
}
/**
* Updates one user preferences option (loads and saves to database).
*
* No validation is done!
*
* @param string $cfg_name
* @param mixed $value
* @return void
*/
function PMA_persist_option($path, $value, $default_value)
{
$prefs = PMA_load_userprefs();
if ($value === $default_value) {
if (isset($prefs['config_data'][$path])) {
unset($prefs['config_data'][$path]);
} else {
return;
}
} else {
$prefs['config_data'][$path] = $value;
}
PMA_save_userprefs($prefs['config_data']);
}
?>