This repository has been archived by the owner on Dec 19, 2020. It is now read-only.
forked from alex-LE/yourTinyTodo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.php
202 lines (182 loc) · 5.32 KB
/
common.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
<?php
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'=>'mytinytodo', 'type'=>'s'),
'mysql.user' => array('default'=>'user', 'type'=>'s'),
'mysql.password' => array('default'=>'', 'type'=>'s'),
'prefix' => array('default'=>'', 'type'=>'s'),
'url' => array('default'=>'', 'type'=>'s'),
'mtt_url' => array('default'=>'', 'type'=>'s'),
'title' => array('default'=>'', 'type'=>'s'),
'lang' => array('default'=>'en', 'type'=>'s'),
'password' => array('default'=>'', 'type'=>'s'),
'smartsyntax' => array('default'=>1, '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'),
);
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(MTTPATH. '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)
{
$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;
public static function init($instance)
{
self::$instance = $instance;
return $instance;
}
public static function instance()
{
if (!isset(self::$instance)) {
//$c = __CLASS__;
$c = 'DBConnection';
self::$instance = new $c;
}
return self::$instance;
}
}
?>