/
settings.php
412 lines (362 loc) · 15.3 KB
/
settings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/*
This file is a part of myTinyTodo.
(C) Copyright 2009-2011,2020-2023 Max Pozdeev <maxpozdeev@gmail.com>
Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details.
*/
require_once('./init.php');
$lang = Lang::instance();
if ( !is_logged() ) {
die("Access denied!<br> Disable password protection or Log in.");
}
if(isset($_POST['save']))
{
check_token();
$t = array();
$langs = getLangs();
Config::$params['lang']['options'] = array_keys($langs);
Config::set('lang', _post('lang'));
// in Demo mode we can set only language by cookies
if (defined('MTT_DEMO')) {
setcookie('lang', Config::get('lang'), 0, url_dir(Config::get('url')=='' ? getRequestUri() : Config::getUrl('url')));
$t['saved'] = 1;
jsonExit($t);
}
if (isset($_POST['password']) && $_POST['password'] != '') Config::set('password', passwordHash($_POST['password'])) ;
elseif (!_post('allowpassword')) Config::set('password', '');
Config::set('smartsyntax', (int)_post('smartsyntax'));
// Do not set invalid timezone
try {
$tz = trim(_post('timezone'));
$testTZ = new DateTimeZone($tz); //will throw Exception on invalid timezone
Config::set('timezone', $tz);
}
catch (Exception $e) {
}
Config::set('autotag', (int)_post('autotag'));
Config::set('markup', (int)_post('markdown') == 0 ? 'v1' : 'markdown');
Config::set('firstdayofweek', (int)_post('firstdayofweek'));
Config::set('clock', (int)_post('clock'));
Config::set('dateformat', removeNewLines(_post('dateformat')) );
Config::set('dateformat2', removeNewLines(_post('dateformat2')) );
Config::set('dateformatshort', removeNewLines(_post('dateformatshort')) );
Config::set('title', removeNewLines(trim(_post('title'))) );
Config::set('showdate', (int)_post('showdate'));
Config::set('showtime', (int)_post('showtime'));
Config::set('showdateInline', (int)_post('showdateInline'));
Config::set('exactduedate', (int)_post('exactduedate'));
Config::set('appearance', removeNewLines(trim(_post('appearance'))) );
Config::set('newTaskCounter', (int)_post('newTaskCounter'));
Config::save();
$t['saved'] = 1;
jsonExit($t);
}
else if (isset($_POST['activate']))
{
check_token();
$t = array('saved'=>0);
// in Demo mode we do nothing
if (defined('MTT_DEMO')) {
$t['saved'] = 1;
jsonExit($t);
}
$activate = (int)_post('activate');
$ext = _post('ext');
$extBundles = MTTExtensionLoader::bundles();
$exts = array_keys($extBundles);
$a = Config::get('extensions');
if (!is_array($a)) $a = [];
if (in_array($ext, $exts)) {
if ($activate) {
try {
MTTExtensionLoader::loadExtension($ext);
$a[] = $ext;
}
catch (Exception $e) {
http_response_code(500);
logAndDie($e->getMessage());
}
}
else $a = array_diff($a, [$ext]);
Config::set('extensions', $a);
Config::save();
}
else if (!$activate && in_array($ext, $a)) {
$a = array_diff($a, [$ext]);
Config::set('extensions', $a);
Config::save();
}
$t['saved'] = 1;
jsonExit($t);
}
function _c($key)
{
return Config::get($key);
}
function getLangs()
{
$langDir = Lang::instance()->langDir();
if ( ! $h = opendir($langDir) ) {
return false;
}
$a = array();
while ( false !== ($file = readdir($h)) )
{
if ( preg_match('/(.+)\.json$/', $file, $m) ) {
$jsonText = file_get_contents($langDir. $file);
if (false === $jsonText) {
die("false ");
continue;
}
$a[$m[1]] = $m[1];
$j = json_decode($jsonText, true);
if ( isset($j['_header']['language']) && isset($j['_header']['original_name']) ) {
$a[$m[1]]= [
'name' => $j['_header']['original_name'],
'title' => $j['_header']['language']
];
}
}
}
closedir($h);
uasort($a, 'cmpLangs');
return $a;
}
function cmpLangs($a, $b) : int
{
//return strcmp( mb_strtoupper($a['name']), mb_strtoupper($b['name']) );
return strcasecmp($a['title'], $b['title']);
}
function selectOptions($a, $value, $default=null)
{
if(!$a) return '';
$s = '';
if($default !== null && !isset($a[$value])) $value = $default;
foreach($a as $k=>$v) {
$s .= '<option value="'.htmlspecialchars($k).'" '.($k===$value?'selected="selected"':'').'>'.htmlspecialchars($v).'</option>';
}
return $s;
}
/**
* @param array $a array of id=>array(name, optional title)
* @param mixed $key Key of OPTION to be selected
* @param mixed $default Default key if $key is not present in $a
*/
function selectOptionsA($a, $key, $default=null)
{
if(!$a) return '';
$s = '';
if ($default !== null && !isset($a[$key])) $key = $default;
else if ($default === null && !isset($a[$key])) {
$s .= '<option hidden disabled selected value></option>';
}
foreach($a as $k=>$v) {
if (!is_array($v)) {
$v = array('name' => $k);
}
$s .= '<option value="'.htmlspecialchars($k).'" '.($k===$key?'selected="selected"':'').
(isset($v['title']) ? ' title="'.htmlspecialchars($v['title']).'"' : '').
'>'.htmlspecialchars($v['name']).'</option>';
}
return $s;
}
function timezoneIdentifiers()
{
$zones = DateTimeZone::listIdentifiers();
$a = array();
foreach($zones as $v) {
$a[$v] = $v;
}
return $a;
}
function listExtensions()
{
$extBundles = MTTExtensionLoader::bundles();
$activatedExts = Config::get('extensions');
if (!is_array($activatedExts)) $activatedExts = [];
$a = [];
foreach ($extBundles as $ext => $meta) {
$out = htmlspecialchars($meta['name']. ' v'. $meta['version']). ' ';
if (in_array($ext, $activatedExts)) {
$out .= "<a href='#' data-settings-link='ext-deactivate' data-ext='". htmlspecialchars($ext). "'>". __('set_deactivate', true). '</a>';
$instance = MTTExtensionLoader::extensionInstance($ext);
if ($instance instanceof MTTExtensionSettingsInterface) {
$out .= " <a href='#' data-settings-link='ext-index' data-ext='". htmlspecialchars($ext). "'>". __('a_settings', true). "</a>";
}
$activatedExts = array_diff($activatedExts, [$ext]);
}
else {
$out .= "<a href='#' data-settings-link='ext-activate' data-ext='". htmlspecialchars($ext). "'>". __('set_activate', true). '</a>';
}
$a[] = $out;
}
// removed and not deactivated
foreach ($activatedExts as $ext) {
$out = "$ext <not found> "."<a href='#' data-settings-link='ext-deactivate' data-ext='". htmlspecialchars($ext). "'>". __('set_deactivate', true). '</a>';
$a[] = $out;
}
print( implode("<br>\n", $a) );
}
header('Content-type:text/html; charset=utf-8');
?>
<h3 class="page-title"><a class="mtt-back-button"></a><?php _e('set_header');?></h3>
<?php
if (isset($_GET['json'])) {
$j = Config::requestDefaultDomain();
if ($j['password'] != '') $j['password'] = "<not empty>";
$j = json_encode($j, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_INVALID_UTF8_SUBSTITUTE);
?>
<div class="mtt-settings-table">
<div class="tr">
<div class="th"> config.json </div>
<div class="td"><textarea class="in350"><?php echo htmlspecialchars($j);?> </textarea></div>
</div>
</div>
<?php
exit;
}
?>
<div id="settings_msg" style="display:none"></div>
<form id="settings_form" method="post" action="settings.php">
<div class="mtt-settings-table">
<div class="tr">
<div class="th"> <?php _e('set_title');?>: <div class="descr"><?php _e('set_title_descr');?></div></div>
<div class="td"> <input name="title" value="<?php echo htmlspecialchars(_c('title'));?>" class="in350" autocomplete="off" /> </div>
</div>
<div class="tr">
<div class="th"><?php _e('set_language');?>:</div>
<div class="td"> <select name="lang"><?php echo selectOptionsA(getLangs(), _c('lang')); ?></select> </div>
</div>
<div class="tr">
<div class="th"><?php _e('set_protection');?>:</div>
<div class="td">
<label><input type="radio" name="allowpassword" value="1" <?php if(_c('password')!='') echo 'checked="checked"'; ?> onclick='$(this.form).find("input[name=password]").attr("disabled",false)' /> <?php _e('set_enabled');?></label> <br/>
<label><input type="radio" name="allowpassword" value="0" <?php if(_c('password')=='') echo 'checked="checked"'; ?> onclick='$(this.form).find("input[name=password]").attr("disabled","disabled")' /> <?php _e('set_disabled');?></label> <br/>
</div></div>
<div class="tr">
<div class="th"><?php _e('set_newpass');?>: <div class="descr"><?php _e('set_newpass_descr');?></div></div>
<div class="td"><input type="password" name="password" autocomplete="new-password" <?php if(_c('password')=='') echo "disabled"; ?> /> </div>
</div>
<div class="tr">
<div class="th"><?php _e('set_smartsyntax');?>: <div class="descr"><?php _e('set_smartsyntax3_descr');?></div></div>
<div class="td">
<label><input type="radio" name="smartsyntax" value="1" <?php if(_c('smartsyntax')) echo 'checked="checked"'; ?> /> <?php _e('set_enabled');?></label> <br/>
<label><input type="radio" name="smartsyntax" value="0" <?php if(!_c('smartsyntax')) echo 'checked="checked"'; ?> /> <?php _e('set_disabled');?></label>
</div></div>
<div class="tr">
<div class="th"><?php _e('set_autotag');?>: <div class="descr"><?php _e('set_autotag_descr');?></div></div>
<div class="td">
<label><input type="radio" name="autotag" value="1" <?php if(_c('autotag')) echo 'checked="checked"'; ?> /> <?php _e('set_enabled');?></label> <br/>
<label><input type="radio" name="autotag" value="0" <?php if(!_c('autotag')) echo 'checked="checked"'; ?> /> <?php _e('set_disabled');?></label>
</div></div>
<div class="tr">
<div class="th"><?php _e('set_markdown');?>: <div class="descr"><?php _e('set_markdown_descr');?></div></div>
<div class="td">
<label><input type="radio" name="markdown" value="1" <?php if (_c('markup') != 'v1') echo 'checked="checked"'; ?> /> <?php _e('set_enabled');?></label> <br/>
<label><input type="radio" name="markdown" value="0" <?php if (_c('markup') == 'v1') echo 'checked="checked"'; ?> /> <?php _e('set_disabled');?></label>
</div></div>
<div class="tr">
<div class="th"><?php _e('set_timezone');?>:</div>
<div class="td">
<select name="timezone"><?php echo selectOptions(timezoneIdentifiers(), _c('timezone')); ?></select>
</div></div>
<div class="tr">
<div class="th"><?php _e('set_firstdayofweek');?>:</div>
<div class="td">
<select name="firstdayofweek"><?php echo selectOptions(__('days_long'), _c('firstdayofweek')); ?></select>
</div></div>
<div class="tr">
<div class="th"><?php _e('set_date');?>:</div>
<div class="td">
<input name="dateformat" size="8" value="<?php echo htmlspecialchars(_c('dateformat'));?>" />
<select onchange="if(this.value!=0) this.form.dateformat.value=this.value;">
<?php $ts = strtotime('2023-09-05 09:15:25');
echo selectOptions(array(
'F j, Y' => formatTime('F j, Y', $ts),
'M j, Y' => formatTime('M j, Y', $ts),
'j M Y' => formatTime('j M Y', $ts),
'j F Y' => formatTime('j F Y', $ts),
'n/j/Y' => formatTime('n/j/Y', $ts),
'd.m.Y' => formatTime('d.m.Y', $ts),
'j. F Y' => formatTime('j. F Y', $ts),
0 => __('set_custom')), _c('dateformat'), 0); ?>
</select>
</div></div>
<div class="tr">
<div class="th"><?php _e('set_date2');?>:</div>
<div class="td">
<input name="dateformat2" size="8" value="<?php echo htmlspecialchars(_c('dateformat2'));?>" />
<select onchange="if(this.value!=0) this.form.dateformat2.value=this.value;">
<?php echo selectOptions(array(
'Y-m-d' => 'yyyy-mm-dd ('. date('Y-m-d', $ts). ')',
'n/j/y' => 'm/d/yy ('. date('n/j/y', $ts). ')',
'd.m.y' => 'dd.mm.yy ('. date('d.m.y', $ts). ')',
'd/m/y' => 'dd/mm/yy ('. date('d/m/y', $ts). ')',
0 => __('set_custom')), _c('dateformat2'), 0); ?>
</select>
</div></div>
<div class="tr">
<div class="th"><?php _e('set_shortdate');?>:</div>
<div class="td">
<input name="dateformatshort" size="8" value="<?php echo htmlspecialchars(_c('dateformatshort'));?>" />
<select onchange="if(this.value!=0) this.form.dateformatshort.value=this.value;">
<?php echo selectOptions(array(
'M d' => formatTime('M d', $ts),
'j M' => formatTime('j M', $ts),
'n/j' => formatTime('n/j', $ts),
'd.m' => formatTime('d.m', $ts),
0 => __('set_custom')), _c('dateformatshort'), 0); ?>
</select>
</div></div>
<div class="tr">
<div class="th"><?php _e('set_clock');?>:</div>
<div class="td">
<select name="clock"><?php echo selectOptions(array(
12 => __('set_12hour'). ' ('. date('g:i A', $ts). ')',
24 => __('set_24hour'). ' ('. date('H:i', $ts). ')'), _c('clock')); ?>
</select>
</div></div>
<div class="tr">
<div class="th"><?php _e('set_showdate');?>:</div>
<div class="td">
<label><input type="radio" name="showdate" value="1" <?php if(_c('showdate')) echo 'checked="checked"'; ?> /> <?php _e('set_enabled');?></label> <br>
<label><input type="radio" name="showdate" value="0" <?php if(!_c('showdate')) echo 'checked="checked"'; ?> /> <?php _e('set_disabled');?></label> <br>
<label><input type="checkbox" name="showdateInline" value="1" <?php if(_c('showdateInline')) echo 'checked="checked"'; ?> /> <?php _e('set_showdate_inline');?></label> <br>
<label><input type="checkbox" name="showtime" value="1" <?php if(_c('showtime')) echo 'checked="checked"'; ?> /> <?php _e('set_showtime');?></label>
</div>
</div>
<div class="tr">
<div class="th"><?php _e('set_exactduedate');?>:</div>
<div class="td">
<label><input type="radio" name="exactduedate" value="1" <?php if(_c('exactduedate')) echo 'checked="checked"'; ?> /> <?php _e('set_enabled');?></label> <br/>
<label><input type="radio" name="exactduedate" value="0" <?php if(!_c('exactduedate')) echo 'checked="checked"'; ?> /> <?php _e('set_disabled');?></label>
</div></div>
<div class="tr">
<div class="th"><?php _e('set_appearance');?>:</div>
<div class="td">
<label><input type="radio" name="appearance" value="system" <?php if(_c('appearance') == 'system') echo 'checked="checked"'; ?> /> <?php _e('set_appearance_system');?></label> <br>
<label><input type="radio" name="appearance" value="light" <?php if(_c('appearance') == 'light') echo 'checked="checked"'; ?> /> <?php _e('set_appearance_light');?></label> <br>
<label><input type="radio" name="appearance" value="dark" <?php if(_c('appearance') == 'dark') echo 'checked="checked"'; ?> /> <?php _e('set_appearance_dark');?></label>
</div>
</div>
<div class="tr">
<div class="th"><?php _e('set_newtaskcounter_h');?>:</div>
<div class="td"><label><input type="checkbox" name="newTaskCounter" value="1" <?php if(_c('newTaskCounter')) echo 'checked="checked"'; ?> /> <?php _e('set_newtaskcounter');?></label></div>
</div>
<?php
if (!defined('MTT_DISABLE_EXT')) {
?>
<div class="tr">
<div class="th"><?php _e('set_extensions');?>:</div>
<div class="td extensions"> <?php listExtensions(); ?>
</div>
</div>
<?php
}
?>
<div class="tr form-bottom-buttons">
<button type="submit"><?php _e('set_submit'); ?></button>
<button type="button" class="mtt-back-button"><?php _e('set_cancel'); ?></button>
</div>
</div>
</form>