-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.php
More file actions
247 lines (219 loc) · 6.78 KB
/
common.php
File metadata and controls
247 lines (219 loc) · 6.78 KB
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
<?php
/*
# Taskmaster
This file is part of the Taskmaster project. Taskmaster is a simple task, project, and information tracking application.
# Copyright
Copyright 2013-2015 David Rodgers - <https://github.com/caressofsteel/taskmaster>
Copyright 2012-2013 Alexander Reichardt - <https://github.com/alex-LE/yourTinyTodo>
Copyright 2009-2010 Max Pozdeev - <https://github.com/maxpozdeev/mytinytodo>
This project is distributed under the GNU General Public License. Please see the included COPYRIGHT and LICENSE-GPL3 for more information. Copyrights for portions of this file are retained by their owners.
*/
if(!defined('TASKMASTER_VERSION')) {
define('TASKMASTER_VERSION', 'v2.1');
}
function htmlarray($a, $exclude=null)
{
htmlarray_ref($a, $exclude);
return $a;
}
function htmlarray_ref(&$a, $exclude=null)
{
if(!$a) return;
if(!is_array($a)) {
$a = htmlspecialchars($a);
return;
}
reset($a);
if($exclude && !is_array($exclude)) $exclude = array($exclude);
foreach($a as $k=>$v)
{
if(is_array($v)) $a[$k] = htmlarray($v, $exclude);
elseif(!$exclude) $a[$k] = htmlspecialchars($v);
elseif(!in_array($k, $exclude)) $a[$k] = htmlspecialchars($v);
}
return;
}
function stop_gpc(&$arr)
{
if (!is_array($arr)) return 1;
//if (!get_magic_quotes_gpc()) return 1;
reset($arr);
foreach($arr as $k=>$v)
{
if(is_array($arr[$k])) stop_gpc($arr[$k]);
elseif(is_string($arr[$k])) $arr[$k] = stripslashes($v);
}
return 1;
}
function _post($param,$defvalue = '')
{
if(!isset($_POST[$param])) {
return $defvalue;
}
else {
return $_POST[$param];
}
}
function _get($param,$defvalue = '')
{
if(!isset($_GET[$param])) {
return $defvalue;
}
else {
return $_GET[$param];
}
}
class Config
{
public static $params = array(
'db' => array('default'=>'sqlite', 'type'=>'s'),
'mysql.host' => array('default'=>'localhost', 'type'=>'s'),
'mysql.db' => array('default'=>'taskmaster', 'type'=>'s'),
'mysql.user' => array('default'=>'user', 'type'=>'s'),
'mysql.password' => array('default'=>'', 'type'=>'s'),
'postgres.host' => array('default'=>'localhost', 'type'=>'s'),
'postgres.db' => array('default'=>'taskmaster', 'type'=>'s'),
'postgres.user' => array('default'=>'user', 'type'=>'s'),
'postgres.password' => array('default'=>'', 'type'=>'s'),
'prefix' => array('default'=>'', 'type'=>'s'),
'url' => array('default'=>'', 'type'=>'s'),
'taskmaster_url' => array('default'=>'', 'type'=>'s'),
'title' => array('default'=>'', 'type'=>'s'),
'template' => array('default'=>'', 'type'=>'s'),
'lang' => array('default'=>'en', 'type'=>'s'),
'password' => array('default'=>'', 'type'=>'s'),
'multiuser' => array('default'=>0, 'type'=>'i'),
'smartsyntax' => array('default'=>1, 'type'=>'i'),
'markdown' => array('default'=>0, 'type'=>'i'),
'timezone' => array('default'=>'UTC', 'type'=>'s'),
'autotag' => array('default'=>1, 'type'=>'i'),
'duedateformat' => array('default'=>1, 'type'=>'i'),
'firstdayofweek' => array('default'=>1, 'type'=>'i'),
'session' => array('default'=>'files', 'type'=>'s', 'options'=>array('files','default')),
'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'),
'showdate' => array('default'=>0, 'type'=>'i'),
'auth_bypass' => array('default'=>'none', 'type'=>'s'),
'debugmode' => array('default'=>0, 'type'=>'i'),
'timetable_day' => array('default'=>8, 'type'=>'i'),
);
public static $config;
public static 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(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";
}
else {
$s .= "\$config['$param'] = '".str_replace(array("\\","'"),array("\\\\","\\'"),$val)."';\n";
}
}
$f = fopen(TASKMASTERPATH. 'db/config.php', 'w');
if($f === false) throw new Exception("Error while saving config file");
fwrite($f, "<?php\n\$config = array();\n$s?>");
fclose($f);
}
}
function formatDate3($format, $ay, $am, $ad, $lang)
{
# F - month long, M - month short
# m - month 2-digit, n - month 1-digit
# d - day 2-digit, j - day 1-digit
$ml = $lang->get('months_long');
$ms = $lang->get('months_short');
$Y = $ay;
$y = $Y < 2010 ? '0'.($Y-2000) : $Y-2000;
$n = $am;
$m = $n < 10 ? '0'.$n : $n;
$F = $ml[$am-1];
$M = $ms[$am-1];
$j = $ad;
$d = $j < 10 ? '0'.$j : $j;
return strtr($format, array('Y'=>$Y, 'y'=>$y, 'F'=>$F, 'M'=>$M, 'n'=>$n, 'm'=>$m, 'd'=>$d, 'j'=>$j));
}
function url_dir($url)
{
if(false !== $p = strpos($url, '?')) $url = substr($url,0,$p); # to avoid parse errors on strange query strings
$p = parse_url($url, PHP_URL_PATH);
if($p == '') return '/';
if(substr($p,-1) == '/') return $p;
if(false !== $pos = strrpos($p,'/')) return substr($p,0,$pos+1);
return '/';
}
function escapeTags($s)
{
if(Config::get('markdown')) {
return $s;
}
$c1 = chr(1);
$c2 = chr(2);
$s = preg_replace("~<b>([\s\S]*?)</b>~i", "${c1}b${c2}\$1${c1}/b${c2}", $s);
$s = preg_replace("~<i>([\s\S]*?)</i>~i", "${c1}i${c2}\$1${c1}/i${c2}", $s);
$s = preg_replace("~<u>([\s\S]*?)</u>~i", "${c1}u${c2}\$1${c1}/u${c2}", $s);
$s = preg_replace("~<s>([\s\S]*?)</s>~i", "${c1}s${c2}\$1${c1}/s${c2}", $s);
$s = str_replace(array($c1, $c2), array('<','>'), htmlspecialchars($s));
return $s;
}
/* found in comments on http://www.php.net/manual/en/function.uniqid.php#94959 */
function generateUUID()
{
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
class DBConnection
{
protected static $instance;
/**
* @static
* @param $instance
* @return Database
*/
public static function init($instance)
{
self::$instance = $instance;
return $instance;
}
/**
* @static
* @return Database
*/
public static function instance()
{
if (!isset(self::$instance)) {
//$c = __CLASS__;
$c = 'DBConnection';
self::$instance = new $c;
}
return self::$instance;
}
}
function hashPassword($password, $salt)
{
return md5($password.$salt);
}