From 8a89b2845ec5d9e6ee539e070218740e62f6c705 Mon Sep 17 00:00:00 2001 From: Max Pozdeev Date: Sun, 27 Sep 2009 21:45:57 +0400 Subject: [PATCH] + setup will ask to select db type on first install and will overwrite config file * changes in config file --- src/common.php | 35 ++++++++++++++++++ src/db/config.php.default | 16 ++++++--- src/index.php | 4 --- src/init.php | 16 ++------- src/setup.php | 75 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 121 insertions(+), 25 deletions(-) diff --git a/src/common.php b/src/common.php index f73bf80..f9b843b 100644 --- a/src/common.php +++ b/src/common.php @@ -58,5 +58,40 @@ function _get($param,$defvalue = '') } } +function saveConfig($config) +{ + $params = array( + 'db' => array('default'=>'sqlite', 'type'=>'s'), + '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'), + 'lang' => array('default'=>'en', 'type'=>'s'), + 'password' => array('default'=>'', 'type'=>'s'), + 'allowread' => array('default'=>0, 'type'=>'i'), + 'smartsyntax' => array('default'=>1, 'type'=>'i'), + 'autotz' => array('default'=>1, 'type'=>'i'), + 'autotag' => array('default'=>1, 'type'=>'i'), + 'duedateformat' => array('default'=>1, 'type'=>'i'), + 'firstdayofweek' => array('default'=>1, 'type'=>'i'), + 'session' => array('default'=>'files', 'type'=>'s'), + ); + + $s = ''; + foreach($params as $param=>$v) + { + $val = isset($config[$param]) ? $config[$param] : $v['default']; + if($v['type']=='i') { + $s .= "\$config['$param'] = ".(int)$val.";\n"; + } + else { + $s .= "\$config['$param'] = '".str_replace(array("\\","'"),array("\\\\","\\'"),$val)."';\n"; + } + } + $f = fopen('./db/config.php', 'w'); + if($f === false) throw new Exception("Error while saving config file"); + fwrite($f, ""); + fclose($f); +} ?> \ No newline at end of file diff --git a/src/db/config.php.default b/src/db/config.php.default index 4027271..645bb03 100644 --- a/src/db/config.php.default +++ b/src/db/config.php.default @@ -3,8 +3,14 @@ # Configuration goes here $config = array(); -# Uncomment this line if you want to use Mysql, in format: array("host","login","password","database"); -#$config['mysql'] = array("localhost","user","password", "mytinytodo"); +# Database type: sqlite or mysql +$config['db'] = 'sqlite'; + +# Specify these settings if you selected above to use Mysql +$config['mysql.host'] = "localhost"; +$config['mysql.db'] = "mytinytodo"; +$config['mysql.user'] = "user"; +$config['mysql.password'] = ""; # Language pack $config['lang'] = "en"; @@ -13,9 +19,9 @@ $config['lang'] = "en"; # or leave empty that everyone could read/write todolist $config['password'] = ""; -# Restrict access for the others when password above is set: -# "no" - No access, "read" - Read only -$config['allow'] = "no"; +# Allow readonly access for the others when password above is set: +# 0 - No access, 1 - Read only +$config['allowread'] = 0; # To disable smart syntax uncomment the line below #$config['smartsyntax'] = 0; diff --git a/src/index.php b/src/index.php index 6c6fb10..f19fdba 100644 --- a/src/index.php +++ b/src/index.php @@ -11,10 +11,6 @@ require_once('./lang/class.default.php'); require_once('./lang/'.$config['lang'].'.php'); $lang = new Lang(); -if(!$needAuth) $tabDisabled = ''; -elseif(!is_logged() && canAllRead()) $tabDisabled = ', selected:1, disabled: [0]'; -elseif(!is_logged()) $tabDisabled = ', disabled: [0,1]'; -else $tabDisabled = ''; $sort = 0; if(isset($_COOKIE['sort']) && $_COOKIE['sort'] != '') $sort = (int)$_COOKIE['sort']; diff --git a/src/init.php b/src/init.php index e936ea5..1e0231c 100644 --- a/src/init.php +++ b/src/init.php @@ -6,23 +6,14 @@ require_once('./db/config.php'); ini_set('display_errors', 'On'); # MySQL Database Connection -if(isset($config['mysql'])) +if($config['db'] == 'mysql') { require_once('class.db.mysql.php'); $db = new Database_Mysql; - $db->connect($config['mysql'][0], $config['mysql'][1], $config['mysql'][2], $config['mysql'][3]); + $db->connect($config['mysql.host'], $config['mysql.user'], $config['mysql.password'], $config['mysql.db']); $db->dq("SET NAMES utf8"); } -# SQLite2 Database -elseif(isset($config['sqlite']) && 2 == $config['sqlite']) - -{ - require_once('class.db.sqlite.php'); - $db = new Database_Sqlite; - $db->connect('./db/todolist.db'); -} - # SQLite3 (pdo_sqlite) else { @@ -32,7 +23,6 @@ else } - $needAuth = (isset($config['password']) && $config['password'] != '') ? 1 : 0; if($needAuth) { @@ -55,7 +45,7 @@ function canAllRead() { global $config; if(!isset($config['password']) || $config['password'] == '') return true; - if(isset($config['allow']) && $config['allow'] == "read") return true; + if(isset($config['allowread']) && $config['allowread']) return true; return false; } diff --git a/src/setup.php b/src/setup.php index 82cb3ca..1abd966 100644 --- a/src/setup.php +++ b/src/setup.php @@ -6,6 +6,23 @@ Licensed under the GNU GPL v3 license. See file COPYRIGHT for details. */ +# Check old config file (prior v1.3) +require_once('./db/config.php'); +if(!isset($config['db'])) +{ + if(isset($config['mysql'])) { + $config['db'] = 'mysql'; + $config['mysql.host'] = $config['mysql'][0]; + $config['mysql.db'] = $config['mysql'][3]; + $config['mysql.user'] = $config['mysql'][1]; + $config['mysql.password'] = $config['mysql'][2]; + } else { + $config['db'] = 'sqlite'; + } + if(isset($config['allow']) && $config['allow'] == 'read') $config['allowread'] = 1; + #saveConfig($config); +} + require_once('./init.php'); if($needAuth && !is_logged()) { @@ -16,18 +33,44 @@ $dbtype = ($dbclass == 'database_mysql') ? 'mysql' : 'sqlite'; $lastVer = '1.3'; echo ''; -echo "myTinyTodo v$lastVer Setup

"; +echo "myTinyTodo v$lastVer Setup

"; # determine current installed version $ver = get_ver($db, $dbtype); if(!$ver) { - # install database - if(!isset($_POST['install'])) { + # Which DB to select + if(!isset($_POST['installdb']) && !isset($_POST['install'])) + { + exitMessage("
Select database type to use:

+

+
+ + + + +
Host:
Database:
User:
Password:

"); + } + elseif(isset($_POST['installdb'])) + { + # Save configuration + $dbtype = ($_POST['installdb'] == 'mysql') ? 'mysql' : 'sqlite'; + $config['db'] = $dbtype; + if($dbtype == 'mysql') { + $config['mysql.host'] = _post('mysql_host'); + $config['mysql.db'] = _post('mysql_db'); + $config['mysql.user'] = _post('mysql_user'); + $config['mysql.password'] = _post('mysql_password'); + } + if(!testConnect($error)) { + exitMessage("Database connection error: $error"); + } + saveConfig($config); exitMessage("This will create myTinyTodo database
"); } + # install database if($dbtype == 'mysql') { try @@ -202,6 +245,27 @@ function has_field_mysql($db, $table, $field) return false; } +function testConnect(&$error) +{ + global $config; + try { + if($config['db'] == 'mysql') + { + require_once('class.db.mysql.php'); + $db = new Database_Mysql; + $db->connect($config['mysql.host'], $config['mysql.user'], $config['mysql.password'], $config['mysql.db']); + } else + { + if(false === $f = @fopen('./db/todolist.db', 'a+')) throw new Exception("database file is not readable/writable"); + else fclose($f); + } + } catch(Exception $e) { + $error = $e->getMessage(); + return 0; + } + return 1; +} + ### 1.1-1.2 ########## function update_11_12($db, $dbtype) { @@ -267,6 +331,11 @@ function update_task_tags($id, $tag_ids) ### 1.2-1.3 ########## function update_12_13($db, $dbtype) { + # update config + global $config; + saveConfig($config); + + # and then db $db->ex("BEGIN"); if($dbtype=='mysql') {