* general settings are stored in database

This commit is contained in:
Max Pozdeev 2021-07-28 18:05:14 +03:00
parent f0c8fa4d22
commit 34f837d8d2
4 changed files with 164 additions and 64 deletions

View file

@ -15,46 +15,4 @@ $config['mysql.password'] = "";
# Tables prefix
$config['prefix'] = "mtt_";
# These two parameters are used when mytinytodo index.php called not from installation directory
# 'url' - URL where index.php is called from (ex.: http://site.com/todo.php)
# 'mtt_url' - directory URL where mytinytodo is installed (with trailing slash) (ex.: http://site.com/lib/mytinytodo/)
$config['url'] = '';
$config['mtt_url'] = '';
# Language pack
$config['lang'] = "en";
# Specify password here to protect your tasks from modification,
# or leave empty that everyone could read/write todolist
$config['password'] = "";
# To disable smart syntax uncomment the line below
#$config['smartsyntax'] = 0;
# Default Time zone
$config['timezone'] = 'UTC';
# To disable auto adding selected tag comment out the line below or set value to 0
$config['autotag'] = 1;
# duedate calendar format: 1 => y-m-d (default), 2 => m/d/y, 3 => d.m.y
$config['duedateformat'] = 1;
# First day of week: 0-Sunday, 1-Monday, 2-Tuesday, .. 6-Saturday
$config['firstdayofweek'] = 1;
# select session handling mechanism: files or default (php default)
$config['session'] = 'files';
# Date/time formats
$config['clock'] = 24;
$config['dateformat'] = 'j M Y';
$config['dateformatshort'] = 'j M';
# Show task date in list
$config['showdate'] = 0;
# Use Markdown syntax for notes. Set to 'v1' to use old v1.6 syntax.
$config['markup'] = 'markdown';
?>

View file

@ -8,45 +8,102 @@
class Config
{
public static $params = array(
public static $noDatabase = false;
private static $dbparams = array(
# Database type: sqlite or mysql
'db' => array('default'=>'sqlite', 'type'=>'s'),
# Specify these settings if you selected above to use Mysql
'mysql.host' => array('default'=>'localhost', 'type'=>'s'),
'mysql.db' => array('default'=>'mytinytodo', 'type'=>'s'),
'mysql.user' => array('default'=>'user', 'type'=>'s'),
'mysql.password' => array('default'=>'', 'type'=>'s'),
# Tables prefix
'prefix' => array('default'=>'', 'type'=>'s'),
# Use mysqli driver for mysql db. Will use PDO if set to 0.
'mysqli' => array('default'=>1, 'type'=>'i')
);
public static $params = array(
# These two parameters are used when mytinytodo index.php called not from installation directory
# 'url' - URL where index.php is called from (ex.: http://site.com/todo.php)
# 'mtt_url' - directory URL where mytinytodo is installed (with trailing slash) (ex.: http://site.com/lib/mytinytodo/)
'url' => array('default'=>'', 'type'=>'s'),
'mtt_url' => array('default'=>'', 'type'=>'s'),
# Top title
'title' => array('default'=>'', 'type'=>'s'),
# Language pack
'lang' => array('default'=>'en', 'type'=>'s'),
# Password to protect your tasks from modification,
# leave empty that everyone could read/write todolist
'password' => array('default'=>'', 'type'=>'s'),
# Smart Syntax enabled flag
'smartsyntax' => array('default'=>1, 'type'=>'i'),
# Default Time zone
'timezone' => array('default'=>'UTC', 'type'=>'s'),
# To disable auto adding selected tag set value to 0
'autotag' => array('default'=>1, 'type'=>'i'),
# duedate calendar format: 1 => y-m-d (default), 2 => m/d/y, 3 => d.m.y
'duedateformat' => array('default'=>1, 'type'=>'i'),
# First day of week: 0-Sunday, 1-Monday, 2-Tuesday, .. 6-Saturday
'firstdayofweek' => array('default'=>1, 'type'=>'i'),
# select session handling mechanism: files or default (php default)
'session' => array('default'=>'files', 'type'=>'s', 'options'=>array('files','default')),
# Date/time formats
'clock' => array('default'=>24, 'type'=>'i', 'options'=>array(12,24)),
'dateformat' => array('default'=>'j M Y', 'type'=>'s'),
'dateformat2' => array('default'=>'n/j/y', 'type'=>'s'),
'dateformatshort' => array('default'=>'j M', 'type'=>'s'),
'template' => array('default'=>'default', 'type'=>'s'),
# Show task date in list
'showdate' => array('default'=>0, 'type'=>'i'),
# Use Markdown syntax for notes. Set to 'v1' to use old v1.6 syntax.
'markup' => array('default'=>'markdown', 'type'=>'s'),
'mysqli' => array('default'=>1, 'type'=>'i')
);
public static $config;
private static $config;
public static function loadConfig($config)
public static function loadDbConfig($config)
{
self::$config = $config;
}
public static function load()
{
if (self::$noDatabase) {
return;
}
$db = DBConnection::instance();
$json = $db->sq("SELECT param_value FROM {$db->prefix}settings WHERE param_key = 'config.json'");
if (!$json) return;
$j = json_decode($json, true);
foreach ($j as $key=>$val) {
// Ignore params for database config
if ( !isset(self::$dbparams[$key]) ) {
self::$config[$key] = $val;
}
}
}
public static function get($key)
{
if(isset(self::$config[$key])) return self::$config[$key];
elseif(isset(self::$params[$key])) return self::$params[$key]['default'];
if (isset(self::$config[$key])) return self::$config[$key];
elseif (isset(self::$params[$key])) return self::$params[$key]['default'];
elseif (isset(self::$dbparams[$key])) return self::$dbparams[$key]['default'];
else return null;
}
@ -64,15 +121,15 @@ class Config
self::$config[$key] = $value;
}
public static function save()
public static function saveDbConfig()
{
$s = '';
foreach(self::$params as $param=>$v)
foreach (self::$dbparams as $param => $v)
{
if(!isset(self::$config[$param])) $val = $v['default'];
elseif(isset($v['options']) && !in_array(self::$config[$param], $v['options'])) $val = $v['default'];
if ( !isset(self::$config[$param]) ) $val = $v['default'];
elseif ( isset($v['options']) && !in_array(self::$config[$param], $v['options']) ) $val = $v['default'];
else $val = self::$config[$param];
if($v['type']=='i') {
if ($v['type']=='i') {
$s .= "\$config['$param'] = ".(int)$val.";\n";
}
else {
@ -92,6 +149,31 @@ class Config
}
}
public static function save()
{
$j = array();
foreach (self::$params as $param => $v)
{
if ( !isset(self::$config[$param]) ) $val = $v['default'];
elseif ( isset($v['options']) && !in_array(self::$config[$param], $v['options'])) $val = $v['default'];
else $val = self::$config[$param];
if ($v['type']=='i') $val = (int)$val;
else $val = strval($val);
$j[$param] = $val;
}
$json = json_encode($j, JSON_PRETTY_PRINT);
$db = DBConnection::instance();
$keyExists = $db->sq("SELECT COUNT(param_key) FROM {$db->prefix}settings WHERE param_key = 'config.json'");
if ($keyExists) {
$db->ex("UPDATE {$db->prefix}settings SET param_value = ? WHERE param_key = 'config.json'", array($json) );
}
else {
$db->ex("INSERT INTO {$db->prefix}settings (param_key,param_value) VALUES ('config.json',?)", array($json) );
}
}
}
?>

View file

@ -36,11 +36,9 @@ require_once(MTTINC. 'class.config.php');
require_once(MTTPATH. 'db/config.php');
if(!isset($config)) global $config;
Config::loadConfig($config);
Config::loadDbConfig($config);
unset($config);
date_default_timezone_set(Config::get('timezone'));
# MySQL Database Connection
if (Config::get('db') == 'mysql')
{
@ -73,6 +71,10 @@ else {
die("Not installed. Run <a href=setup.php>setup.php</a> first.");
}
DBConnection::setPrefix(Config::get('prefix'));
Config::load();
date_default_timezone_set(Config::get('timezone'));
//User can override language setting by cookies or query
$forceLang = '';

View file

@ -26,6 +26,9 @@ if (!isset($config['db']))
if ($config['db'] != '')
{
require_once('./includes/class.config.php');
Config::$noDatabase = true; //will not load settings from database in init.php
require_once('./init.php');
if ( !is_logged() )
{
@ -41,19 +44,20 @@ else
require_once(MTTINC. 'common.php');
require_once(MTTINC. 'class.dbconnection.php');
require_once(MTTINC. 'class.config.php');
Config::loadConfig($config);
Config::$noDatabase = true;
Config::loadDbConfig($config);
unset($config);
$db = null;
$dbtype = '';
}
$lastVer = '1.4';
$lastVer = '1.7';
echo '<html><head><meta name="robots" content="noindex,nofollow"><title>myTinyTodo @VERSION Setup</title></head><body>';
echo "<big><b>myTinyTodo @VERSION Setup</b></big><br><br>";
# determine current installed version
$ver = get_ver($db, $dbtype);
$ver = $db ? get_ver($db, $dbtype) : '';
if (!$ver)
{
@ -88,7 +92,7 @@ if (!$ver)
if(!is_writable('./db/config.php')) {
exitMessage("Config file ('db/config.php') is not writable.");
}
Config::save();
Config::saveDbConfig();
exitMessage("This will create myTinyTodo database <form method=post><input type=hidden name=install value=1><input type=submit value=' Install '></form>");
}
@ -156,6 +160,13 @@ if (!$ver)
) CHARSET=utf8 ");
$db->ex(
"CREATE TABLE {$db->prefix}settings (
`param_key` VARCHAR(100) NOT NULL default '',
`param_value` TEXT,
UNIQUE KEY `param_key` (`param_key`)
) CHARSET=utf8mb4 ");
} catch (Exception $e) {
exitMessage("<b>Error:</b> ". htmlarray($e->getMessage()));
}
@ -219,6 +230,15 @@ if (!$ver)
$db->ex("CREATE INDEX tag2task_task_id ON {$db->prefix}tag2task (task_id)");
$db->ex("CREATE INDEX tag2task_list_id ON {$db->prefix}tag2task (list_id)"); /* for tagcloud */
$db->ex(
"CREATE TABLE {$db->prefix}settings (
param_key VARCHAR(100) NOT NULL default '',
param_value TEXT
) ");
$db->ex("CREATE UNIQUE INDEX settings_key ON {$db->prefix}settings (param_key COLLATE NOCASE)");
} catch (Exception $e) {
exitMessage("<b>Error:</b> ". htmlarray($e->getMessage()));
}
@ -227,6 +247,8 @@ if (!$ver)
# create default list
$db->ex( "INSERT INTO {$db->prefix}lists (uuid,name,d_created,taskview) VALUES (?,?,?,?)", array(generateUUID(), 'Todo', time(), 1) );
Config::save();
Config::saveDbConfig();
}
elseif($ver == $lastVer)
{
@ -234,25 +256,31 @@ elseif($ver == $lastVer)
}
else
{
if(!in_array($ver, array('1.3.0','1.3.1'))) {
if(!in_array($ver, array('1.3.0','1.3.1','1.4'))) {
exitMessage("Can not update. Unsupported database version ($ver).");
}
if(!isset($_POST['update'])) {
exitMessage("Update database v$ver
exitMessage("Update database v$ver to v$lastVer<br><br>
<form name=frm method=post><input type=hidden name=update value=1><input type=hidden name=tz value=-1><input type=submit value=' Update '></form>
<script type=\"text/javascript\">var tz = -1 * (new Date()).getTimezoneOffset(); document.frm.tz.value = tz;</script>
");
}
# update process
if($ver == '1.3.1')
if ($ver == '1.4')
{
update_14_17($db, $dbtype);
}
elseif ($ver == '1.3.1')
{
update_131_14($db, $dbtype);
update_14_17($db, $dbtype);
}
if($ver == '1.3.0')
elseif ($ver == '1.3.0')
{
update_130_131($db, $dbtype);
update_131_14($db, $dbtype);
update_14_17($db, $dbtype);
}
}
echo "Done<br><br> <b>Attention!</b> Delete this file for security reasons.";
@ -286,6 +314,8 @@ function get_ver(Database_Abstract $db, $dbtype)
if(!has_field_sqlite($db, $db->prefix.'todolist', 'd_edited')) return $v;
}
$v = '1.4';
if (!$db->tableExists($db->prefix.'settings')) return $v;
$v = '1.7';
return $v;
}
@ -658,5 +688,33 @@ function v14_addTaskTags($taskId, $tagIds, $listId)
}
### end of 1.4 #####
### update v1.4 to v1.7 ##########
function update_14_17(Database_Abstract $db, $dbtype)
{
$db->ex("BEGIN");
if($dbtype=='mysql')
{
$db->ex(
"CREATE TABLE {$db->prefix}settings (
`param_key` VARCHAR(100) NOT NULL default '',
`param_value` TEXT,
UNIQUE KEY `param_key` (`param_key`)
) CHARSET=utf8mb4 ");
}
else #sqlite
{
$db->ex(
"CREATE TABLE {$db->prefix}settings (
param_key VARCHAR(100) NOT NULL default '',
param_value TEXT
) ");
$db->ex("CREATE UNIQUE INDEX settings_key ON {$db->prefix}settings (param_key COLLATE NOCASE)");
}
$db->ex("COMMIT");
Config::save();
Config::saveDbConfig();
}
### end of 1.7 #####
?>