From 34f837d8d2df97b787d2885ca97d1ef1f596674a Mon Sep 17 00:00:00 2001 From: Max Pozdeev Date: Wed, 28 Jul 2021 18:05:14 +0300 Subject: [PATCH] * general settings are stored in database --- src/db/config.php.default | 42 -------------- src/includes/class.config.php | 104 ++++++++++++++++++++++++++++++---- src/init.php | 8 ++- src/setup.php | 74 +++++++++++++++++++++--- 4 files changed, 164 insertions(+), 64 deletions(-) diff --git a/src/db/config.php.default b/src/db/config.php.default index b8896a4..4b18a0b 100644 --- a/src/db/config.php.default +++ b/src/db/config.php.default @@ -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'; - ?> diff --git a/src/includes/class.config.php b/src/includes/class.config.php index 2cc966e..54dea97 100644 --- a/src/includes/class.config.php +++ b/src/includes/class.config.php @@ -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) ); + } + } } ?> \ No newline at end of file diff --git a/src/init.php b/src/init.php index 647edae..59ca591 100644 --- a/src/init.php +++ b/src/init.php @@ -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 setup.php 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 = ''; diff --git a/src/setup.php b/src/setup.php index e0d57a8..aa1a56a 100644 --- a/src/setup.php +++ b/src/setup.php @@ -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 'myTinyTodo @VERSION Setup'; echo "myTinyTodo @VERSION Setup

"; # 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
"); } @@ -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("Error: ". 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("Error: ". 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

"); } # 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

Attention! 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 ##### ?> \ No newline at end of file