* Config::get('var') instead of $config['var']

This commit is contained in:
Max Pozdeev 2009-11-30 20:59:16 +03:00
parent fcd91e7c72
commit 7ab0d27dd1
7 changed files with 83 additions and 67 deletions

View file

@ -11,7 +11,7 @@ set_exception_handler('myExceptionHandler');
require_once('./init.php');
require_once('./lang/class.default.php');
require_once('./lang/'.$config['lang'].'.php');
require_once('./lang/'.Config::get('lang').'.php');
$lang = new Lang();
if(isset($_GET['loadLists']))
@ -56,7 +56,7 @@ elseif(isset($_GET['loadTasks']))
elseif($sort == 2) $sqlSort .= "ddn ASC, duedate ASC, prio DESC, ow ASC";
else $sqlSort .= "ow ASC";
$tz = (int)_get('tz');
if((isset($config['autotz']) && $config['autotz']==0) || $tz<-720 || $tz>720 || $tz%30!=0) $tz = round(date('Z')/60);
if(Config::get('autotz')==0 || $tz<-720 || $tz>720 || $tz%30!=0) $tz = round(date('Z')/60);
$t = array();
$t['total'] = 0;
$t['list'] = array();
@ -79,7 +79,7 @@ elseif(isset($_GET['newTask']))
$title = trim(_post('title'));
$prio = 0;
$tags = '';
if(!isset($config['smartsyntax']) || $config['smartsyntax'] != 0)
if(Config::get('smartsyntax') != 0)
{
$a = parse_smartsyntax($title);
if($a === false) {
@ -94,9 +94,9 @@ elseif(isset($_GET['newTask']))
echo json_encode($t);
exit;
}
if(isset($config['autotag']) && $config['autotag']) $tags .= ','._post('tag');
if(Config::get('autotag')) $tags .= ','._post('tag');
$tz = (int)_post('tz');
if( (isset($config['autotz']) && $config['autotz']==0) || $tz<-720 || $tz>720 || $tz%30!=0 ) $tz = round(date('Z')/60);
if(Config::get('autotz')==0 || $tz<-720 || $tz>720 || $tz%30!=0 ) $tz = round(date('Z')/60);
$ow = 1 + (int)$db->sq("SELECT MAX(ow) FROM todolist WHERE list_id=$listId AND compl=0");
$db->ex("BEGIN");
$db->dq("INSERT INTO todolist (list_id,title,d_created,ow,prio) VALUES ($listId,?,?,$ow,$prio)", array($title, time()));
@ -134,9 +134,9 @@ elseif(isset($_GET['fullNewTask']))
exit;
}
$tags = trim(_post('tags'));
if(isset($config['autotag']) && $config['autotag']) $tags .= ','._post('tag');
if(Config::get('autotag')) $tags .= ','._post('tag');
$tz = (int)_post('tz');
if( (isset($config['autotz']) && $config['autotz']==0) || $tz<-720 || $tz>720 || $tz%30!=0 ) $tz = round(date('Z')/60);
if( Config::get('autotz')==0 || $tz<-720 || $tz>720 || $tz%30!=0 ) $tz = round(date('Z')/60);
$ow = 1 + (int)$db->sq("SELECT MAX(ow) FROM todolist WHERE list_id=$listId AND compl=0");
if(is_null($duedate)) $duedate = 'NULL'; else $duedate = $db->quote($duedate);
$db->ex("BEGIN");
@ -225,7 +225,7 @@ elseif(isset($_GET['editTask']))
}
$tags = trim(_post('tags'));
$tz = (int)_post('tz');
if( (isset($config['autotz']) && $config['autotz']==0) || $tz<-720 || $tz>720 || $tz%30!=0 ) $tz = round(date('Z')/60);
if( Config::get('autotz')==0 || $tz<-720 || $tz>720 || $tz%30!=0 ) $tz = round(date('Z')/60);
$db->ex("BEGIN");
$tag_ids = prepare_tags($tags, $listId);
$cur_ids = get_task_tags($id);
@ -284,7 +284,7 @@ elseif(isset($_POST['login']))
}
stop_gpc($_POST);
$password = _post('password');
if($password == $config['password']) {
if($password == Config::get('password')) {
$t['logged'] = 1;
session_regenerate_id(1);
$_SESSION['logged'] = 1;
@ -455,7 +455,7 @@ function prepareTaskRow($r, $tz)
function check_read_access($listId = null)
{
global $db;
if(!isset($config['password']) || $config['password'] == '') return true;
if(Config::get('password') == '') return true;
if(is_logged()) return true;
if($listId)
{
@ -468,8 +468,7 @@ function check_read_access($listId = null)
function check_write_access()
{
global $config;
if(!isset($config['password']) || $config['password'] == '') return;
if(Config::get('password') == '') return;
if(is_logged()) return;
echo json_encode( array('total'=>0, 'list'=>array(), 'denied'=>1) );
exit;
@ -556,14 +555,13 @@ function tag_size($qmin, $q, $step)
function parse_duedate($s)
{
global $config;
$y = $m = $d = 0;
if(preg_match("|^(\d+)-(\d+)-(\d+)\b|", $s, $ma)) {
$y = (int)$ma[1]; $m = (int)$ma[2]; $d = (int)$ma[3];
}
elseif(preg_match("|^(\d+)\/(\d+)\/(\d+)\b|", $s, $ma))
{
if($config['duedateformat'] == 4) {
if(Config::get('duedateformat') == 4) {
$d = (int)$ma[1]; $m = (int)$ma[2]; $y = (int)$ma[3];
} else {
$m = (int)$ma[1]; $d = (int)$ma[2]; $y = (int)$ma[3];
@ -580,7 +578,7 @@ function parse_duedate($s)
}
elseif(preg_match("|^(\d+)\/(\d+)\b|", $s, $ma))
{
if($config['duedateformat'] == 4) {
if(Config::get('duedateformat') == 4) {
$d = (int)$ma[1]; $m = (int)$ma[2];
} else {
$m = (int)$ma[1]; $d = (int)$ma[2];
@ -602,7 +600,7 @@ function parse_duedate($s)
function prepare_duedate($duedate, $tz)
{
global $lang, $config;
global $lang;
$a = array( 'class'=>'', 'str'=>'', 'formatted'=>'' );
if($duedate == '') {
@ -612,19 +610,19 @@ function prepare_duedate($duedate, $tz)
$at = explode('-', gmdate('Y-m-d', time() + $tz*60));
$diff = mktime(0,0,0,$ad[1],$ad[2],$ad[0]) - mktime(0,0,0,$at[1],$at[2],$at[0]);
if($diff < -604800 && $ad[0] == $at[0]) { $a['class'] = 'past'; $a['str'] = formatDate3($config['dateformatshort'], (int)$ad[0], (int)$ad[1], (int)$ad[2], $lang); }
elseif($diff < -604800) { $a['class'] = 'past'; $a['str'] = formatDate3($config['dateformat'], (int)$ad[0], (int)$ad[1], (int)$ad[2], $lang); }
if($diff < -604800 && $ad[0] == $at[0]) { $a['class'] = 'past'; $a['str'] = formatDate3(Config::get('dateformatshort'), (int)$ad[0], (int)$ad[1], (int)$ad[2], $lang); }
elseif($diff < -604800) { $a['class'] = 'past'; $a['str'] = formatDate3(Config::get('dateformat'), (int)$ad[0], (int)$ad[1], (int)$ad[2], $lang); }
elseif($diff < -86400) { $a['class'] = 'past'; $a['str'] = sprintf($lang->get('daysago'),ceil(abs($diff)/86400)); }
elseif($diff < 0) { $a['class'] = 'past'; $a['str'] = $lang->get('yesterday'); }
elseif($diff < 86400) { $a['class'] = 'today'; $a['str'] = $lang->get('today'); }
elseif($diff < 172800) { $a['class'] = 'today'; $a['str'] = $lang->get('tomorrow'); }
elseif($diff < 691200) { $a['class'] = 'soon'; $a['str'] = sprintf($lang->get('indays'),ceil($diff/86400)); }
elseif($ad[0] == $at[0]) { $a['class'] = 'future'; $a['str'] = formatDate3($config['dateformatshort'], (int)$ad[0], (int)$ad[1], (int)$ad[2], $lang); }
else { $a['class'] = 'future'; $a['str'] = formatDate3($config['dateformat'], (int)$ad[0], (int)$ad[1], (int)$ad[2], $lang); }
elseif($ad[0] == $at[0]) { $a['class'] = 'future'; $a['str'] = formatDate3(Config::get('dateformatshort'), (int)$ad[0], (int)$ad[1], (int)$ad[2], $lang); }
else { $a['class'] = 'future'; $a['str'] = formatDate3(Config::get('dateformat'), (int)$ad[0], (int)$ad[1], (int)$ad[2], $lang); }
if($config['duedateformat'] == 2) $a['formatted'] = (int)$ad[1].'/'.(int)$ad[2].'/'.$ad[0];
elseif($config['duedateformat'] == 3) $a['formatted'] = $ad[2].'.'.$ad[1].'.'.$ad[0];
elseif($config['duedateformat'] == 4) $a['formatted'] = $ad[2].'/'.$ad[1].'/'.$ad[0];
if(Config::get('duedateformat') == 2) $a['formatted'] = (int)$ad[1].'/'.(int)$ad[2].'/'.$ad[0];
elseif(Config::get('duedateformat') == 3) $a['formatted'] = $ad[2].'.'.$ad[1].'.'.$ad[0];
elseif(Config::get('duedateformat') == 4) $a['formatted'] = $ad[2].'/'.$ad[1].'/'.$ad[0];
else $a['formatted'] = $duedate;
return $a;

View file

@ -66,6 +66,7 @@ class Config
'mysql.db' => array('default'=>'mytinytodo', 'type'=>'s'),
'mysql.user' => array('default'=>'user', 'type'=>'s'),
'mysql.password' => array('default'=>'', 'type'=>'s'),
'prefix' => array('default'=>'', 'type'=>'s'),
'title' => array('default'=>'', 'type'=>'s'),
'lang' => array('default'=>'en', 'type'=>'s'),
'password' => array('default'=>'', 'type'=>'s'),
@ -80,14 +81,33 @@ class Config
'dateformatshort' => array('default'=>'j M', 'type'=>'s'),
);
public static function save($config)
public static $config;
public function loadConfig($config)
{
self::$config = $config;
}
public static function get($key)
{
if(isset(self::$config[$key])) return self::$config[$key];
elseif(isset(self::$params[$key])) return self::$params[$key]['default'];
else return null;
}
public static function set($key, $value)
{
self::$config[$key] = $value;
}
public static function save()
{
$s = '';
foreach(self::$params as $param=>$v)
{
if(!isset($config[$param])) $val = $v['default'];
elseif(isset($v['options']) && !in_array($config[$param], $v['options'])) $val = $v['default'];
else $val = $config[$param];
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') {
$s .= "\$config['$param'] = ".(int)$val.";\n";
}
@ -100,13 +120,6 @@ class Config
fwrite($f, "<?php\n\$config = array();\n$s?>");
fclose($f);
}
public static function get($key, $config)
{
if(isset($config[$key])) return $config[$key];
elseif(isset(self::$params[$key])) return self::$params[$key]['default'];
else return null;
}
}
function formatDate3($format, $ay, $am, $ad, $lang)

View file

@ -12,6 +12,9 @@ $config['mysql.db'] = "mytinytodo";
$config['mysql.user'] = "user";
$config['mysql.password'] = "";
# Tables prefix
$config['prefix'] = "mtt_";
# Language pack
$config['lang'] = "en";

View file

@ -9,7 +9,7 @@
$dontStartSession = 1;
require_once('./init.php');
require_once('./lang/class.default.php');
require_once('./lang/'.$config['lang'].'.php');
require_once('./lang/'.Config::get('lang').'.php');
$lang = new Lang();
$listId = (int)_get('list');
@ -35,7 +35,7 @@ while($r = $q->fetch_assoc($q))
if($r['prio']) $a[] = $lang->get('priority'). ": $r[prio]";
if($r['duedate'] != '') {
$ad = explode('-', $r['duedate']);
$a[] = $lang->get('due'). ": ".formatDate3($config['dateformat'], (int)$ad[0], (int)$ad[1], (int)$ad[2], $lang);
$a[] = $lang->get('due'). ": ".formatDate3(Config::get('dateformat'), (int)$ad[0], (int)$ad[1], (int)$ad[2], $lang);
}
if($r['tags'] != '') $a[] = $lang->get('tags'). ": ". str_replace(',', ', ', $r['tags']);
$r['_descr'] = nl2br($r['note']). ($a && $r['note']!='' ? "<br><br>" : ""). implode("<br>", $a);

View file

@ -8,19 +8,18 @@
require_once('init.php');
require_once('./lang/class.default.php');
require_once('./lang/'.$config['lang'].'.php');
require_once('./lang/'.Config::get('lang').'.php');
$lang = new Lang();
if($config['duedateformat'] == 2) $duedateformat = 'm/d/yy';
elseif($config['duedateformat'] == 3) $duedateformat = 'dd.mm.yy';
elseif($config['duedateformat'] == 4) $duedateformat = 'dd/mm/yy';
if(Config::get('duedateformat') == 2) $duedateformat = 'm/d/yy';
elseif(Config::get('duedateformat') == 3) $duedateformat = 'dd.mm.yy';
elseif(Config::get('duedateformat') == 4) $duedateformat = 'dd/mm/yy';
else $duedateformat = 'yy-mm-dd';
if(!isset($config['firstdayofweek']) || !is_int($config['firstdayofweek']) ||
$config['firstdayofweek']<0 || $config['firstdayofweek']>6) $config['firstdayofweek'] = 1;
if(!is_int(Config::get('firstdayofweek')) || Config::get('firstdayofweek')<0 || Config::get('firstdayofweek')>6) Config::set('firstdayofweek', 1);
if(isset($config['title']) && $config['title'] != '') $title = htmlarray($config['title']);
if(Config::get('title') != '') $title = htmlarray(Config::get('title'));
else $title = $lang->get('My Tiny Todolist');
@ -67,7 +66,7 @@ $().ready(function(){
echo "\tloadLists(1, 1);\n";
?>
preloadImg();
$("#duedate").datepicker({dateFormat: '<?php echo $duedateformat; ?>', firstDay: <?php echo $config['firstdayofweek']; ?>,
$("#duedate").datepicker({dateFormat: '<?php echo $duedateformat; ?>', firstDay: <?php echo Config::get('firstdayofweek'); ?>,
showOn: 'button', buttonImage: 'images/calendar.png', buttonImageOnly: true, changeMonth:true,
changeYear:true, constrainInput: false, duration:'', nextText:'&gt;', prevText:'&lt;', dayNamesMin:lang.daysMin,
dayNames:lang.daysLong, monthNamesShort:lang.monthsLong });

View file

@ -5,12 +5,16 @@ require_once('./db/config.php');
ini_set('display_errors', 'On');
if(!isset($config)) global $config;
Config::loadConfig($config);
unset($config);
# MySQL Database Connection
if($config['db'] == 'mysql')
if(Config::get('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']);
$db->connect(Config::get('mysql.host'), Config::get('mysql.user'), Config::get('mysql.password'), Config::get('mysql.db'));
$db->dq("SET NAMES utf8");
}
@ -23,10 +27,10 @@ else
}
$needAuth = (isset($config['password']) && $config['password'] != '') ? 1 : 0;
$needAuth = (Config::get('password') != '') ? 1 : 0;
if($needAuth && !isset($dontStartSession))
{
if(isset($config['session']) && $config['session'] == 'files')
if(Config::get('session') == 'files')
{
session_save_path(realpath('./tmp/sessions/'));
ini_set('session.gc_maxlifetime', '1209600'); # 14 days session file minimum lifetime
@ -48,8 +52,8 @@ function is_logged()
function timestampToDatetime($timestamp, $tz)
{
global $config, $lang;
$format = $config['dateformat'] .' '. ($config['clock'] == 12 ? 'g:i A' : 'H:i');
global $lang;
$format = Config::get('dateformat') .' '. (Config::get('clock') == 12 ? 'g:i A' : 'H:i');
$newformat = strtr($format, array('F'=>'%1', 'M'=>'%2'));
$adate = explode(',', gmdate('n,'.$newformat, $timestamp + $tz*60), 2);
$s = $adate[1];

View file

@ -19,20 +19,20 @@ if(isset($_POST['save']))
$t = array();
$langs = getLangs();
Config::$params['lang']['options'] = array_keys($langs);
$config['lang'] = _post('lang');
if(isset($_POST['password']) && $_POST['password'] != '') $config['password'] = $_POST['password'];
elseif(!_post('allowpassword')) $config['password'] = '';
$config['smartsyntax'] = (int)_post('smartsyntax');
$config['autotz'] = (int)_post('autotz');
$config['autotag'] = (int)_post('autotag');
$config['session'] = _post('session');
$config['firstdayofweek'] = (int)_post('firstdayofweek');
$config['duedateformat'] = (int)_post('duedateformat');
$config['clock'] = (int)_post('clock');
$config['dateformat'] = _post('dateformat');
$config['dateformatshort'] = _post('dateformatshort');
$config['title'] = trim(_post('title'));
Config::save($config);
Config::set('lang', _post('lang'));
if(isset($_POST['password']) && $_POST['password'] != '') Config::set('password', $_POST['password']);
elseif(!_post('allowpassword')) Config::set('password', '');
Config::set('smartsyntax', (int)_post('smartsyntax'));
Config::set('autotz', (int)_post('autotz'));
Config::set('autotag', (int)_post('autotag'));
Config::set('session', _post('session'));
Config::set('firstdayofweek', (int)_post('firstdayofweek'));
Config::set('duedateformat', (int)_post('duedateformat'));
Config::set('clock', (int)_post('clock'));
Config::set('dateformat', _post('dateformat'));
Config::set('dateformatshort', _post('dateformatshort'));
Config::set('title', trim(_post('title')));
Config::save();
$t['saved'] = 1;
echo json_encode($t);
exit;
@ -41,8 +41,7 @@ if(isset($_POST['save']))
function _c($key)
{
global $config;
return Config::get($key, $config);
return Config::get($key);
}
function getLangs()
@ -87,7 +86,7 @@ function selectOptions($a, $value, $default=null)
<tr>
<th>Language:</th>
<td> <SELECT name="lang"><?php $langs = getLangs(); echo selectOptions($langs, $config['lang']); ?></SELECT> </td>
<td> <SELECT name="lang"><?php $langs = getLangs(); echo selectOptions($langs, _c('lang')); ?></SELECT> </td>
</tr>
<tr>