-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.php
349 lines (313 loc) · 11.4 KB
/
functions.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
<?PHP
class class_cache {
var $cache_changed=array();
var $cacheid;
var $splitted=false;
function write_cache($flocachevar, $flocachevalue) {
$flocachevarstring = '$this->cache';
$vararray = explode(",",$flocachevar);
$changedvar = $vararray[0];
if ($this->splitted) {
$flocachevarstring.="['{$vararray[0]}']";
$changedvar = $vararray[0];
array_shift($vararray);
}
foreach ($vararray as $key => $flocachevar) {
$flocachevarstring .="->".$this->escape_cachevar($flocachevar);
}
if (eval("$flocachevarstring = \" ".strtr($flocachevalue,array('"'=>'\"'))."\";")==NULL) { //space workaround for $this->object = "";
array_push($this->cache_changed, $changedvar);
return true;
}
else return false;
}
function get_cache($flocachevar, $asstring=TRUE) {
$flocachevarstring = '$this->cache';
$vararray = explode(",",$flocachevar);
if ($this->splitted) {
if (!isset($this->cache[$vararray[0]])) $this->load_splitcache($vararray[0]);
$flocachevarstring.="['{$vararray[0]}']";
array_shift($vararray);
}
foreach ($vararray as $key => $flocachevar) {
$flocachevarstring .="->".$this->escape_cachevar($flocachevar);
}
if (!eval("if (!isset($flocachevarstring)) { return false; } else { \$var = $flocachevarstring; return true;} ")) return false;
if ($asstring) { return "".trim($var); } //trim workaround for $this->object = "";
else { return get_object_vars($this->trim_objectvalues($var, $var)); }
}
function escape_cachevar($var) {
if (preg_match('/^[0-9]+/', $var)) $var = "number".$var;
return strtr($var, array('%'=>'procent', ' ' => '_', "'"=>"apostrophe", '"'=>"twoapostrophe", '#'=>'rhomb', '.'=>'point'));
}
function trim_objectvalues($trimvar, $returnvar, $echo="") {
foreach (get_object_vars($trimvar) as $varkey => $varvalue) {
if (!strlen("".$varvalue)) { $this->trim_objectvalues($varvalue, $returnvar, "$echo->$varkey"); }
else { eval("\$returnvar{$echo}->{$varkey} = trim(\"\".\$varvalue);"); }
}
return $returnvar;
}
function save_cache() {
if (!count($this->cache_changed)) return true;
if (!$this->splitted) {
if (MYSQL_QUERY("UPDATE cache_main SET cachedata='".mysql_real_escape_string($this->cache->asXML())."', lastchanged='".date("U")."' WHERE cacheid='{$this->cacheid}'")) return true;
return false;
}
else {
foreach ($this->cache as $splitcache => $val) {
if (in_array($splitcache, $this->cache_changed)) {
if (!MYSQL_QUERY("UPDATE cache_main SET cachedata='".mysql_real_escape_string($this->cache[$splitcache]->asXML())."', lastchanged='".date("U")."' WHERE cacheid='{$this->splitted}:{$splitcache}'")) {
$error = true;
}
}
}
if ($error) return false;
return true;
}
}
function remove_cache() {
if (MYSQL_QUERY("DELETE FROM cache_main WHERE cacheid='{$this->cacheid}'")) return true;
return false;
}
function export_cache($file) {
if ($this->cache->asXML($file)) return true;
return false;
}
function load_cachefile($file) {
if (!is_file($file)) return false;
$this->cache = simplexml_load_file($file);
if (!$this->cache) {
$this->cache = new SimpleXMLElement("<Cache></Cache>");
return false;
}
return true;
}
function load_cache($id="maincache") {
$this->cacheid=$id;
if ($cache = MYSQL_FETCH_ARRAY(MYSQL_QUERY("SELECT cachedata FROM cache_main WHERE cacheid='{$this->cacheid}' LIMIT 1"))) {
$this->cache = simplexml_load_string($cache['cachedata']);
if (!$this->cache) {
$this->cache = new SimpleXMLElement("<Cache></Cache>");
}
} else {
$this->cache = new SimpleXMLElement("<Cache></Cache>");
MYSQL_QUERY("INSERT INTO cache_main (cacheid, cachedata, lastchanged) VALUES('{$this->cacheid}', '".mysql_real_escape_string($this->cache->asXML())."', '".date("U")."')");
}
}
function load_splitcache($splitid) {
if ($cache = MYSQL_FETCH_ARRAY(MYSQL_QUERY("SELECT cachedata FROM cache_main WHERE cacheid='{$this->splitted}:{$splitid}' LIMIT 1"))) {
$splitcache = simplexml_load_string($cache['cachedata']);
if (!$splitcache) {
$splitcache = new SimpleXMLElement("<Cache></Cache>");
}
} else {
$splitcache = new SimpleXMLElement("<Cache></Cache>");
MYSQL_QUERY("INSERT INTO cache_main (cacheid, cachedata, lastchanged) VALUES('{$this->splitted}:{$splitid}', '".mysql_real_escape_string($splitcache->asXML())."', '".date("U")."')");
}
$this->cache[$splitid]=$splitcache;
}
}
//cache
$flocache = new class_cache;
$flocache->splitted="maincache";
// $flocache->load_cache("maincache");
function create_characterkey() {
$set = array("a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z","1","2","3","4","5","6","7","8","9");
$str = '';
for($i = 1; $i <= mt_rand(20,32); ++$i) {
$str .= $set[mt_rand(0, count($set)-1)];
}
return strtolower($str);
}
function get_geltstring($gelt, $amount=0) {
if (!$amount) return number_format($gelt, 0, '.', ',');
else return $amount."x".number_format($gelt, 0, '.', ',');
}
function get_searchstring($string, $regexp=1) {
global $florensia;
$string = urldecode($string);
/*
$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
$string = stripslashes($string);
*/
if ($regexp) {
$string = quotemeta($string);
$string = strtr($string, array('\*'=>'.*', '\?'=>'.'));
}
else $string = strtr($string, array('*'=>'%', '?'=>'_'));
// $string = strtr($string, array("'"=>"\'")); //global
return mysql_real_escape_string($string);
}
/*
function bbc_code($entry) {
$entry = preg_replace("#\[b\](.*)\[/b\]#Uis","<b>\\1</b>",$entry);
$entry = preg_replace("#\[i\](.*)\[/i\]#Uis","<i>\\1</i>",$entry);
$entry = preg_replace("#\[u\](.*)\[/u\]#Uis","<u>\\1</u>",$entry);
if ($_GET['sid']=="a_news_update") {
$entry = preg_replace("#\[img\](.*)\[/img\]#Uis","<img src=\"\\1\" border=\"0\" alt=\"\\1\">",$entry);
}
//$entry = preg_replace("\[mail\]([^\[]+)\[/mail\]","<a href=\"mailto:\\1\">\\1</a>",$entry);
$entry = preg_replace("#\[url\]http://(.*)\[/url\]#Uis","<a href=\"http://\\1\" target=\"_blank\">\\1</a>",$entry);
$entry = preg_replace("#\[url\](.*)\[/url\]#Uis","<a href=\"http://\\1\" target=\"_blank\">\\1</a>",$entry);
$entry = preg_replace("#\[url=http://([^]]+)\](.*)\[/url\]#Uis","<a href=\"http://\\1\" target=\"_blank\">\\2</a>",$entry);
$entry = preg_replace("#\[url=([^]]+)\](.*)\[/url\]#Uis","<a href=\"http://\\1\" target=\"_blank\">\\2</a>",$entry);
$entry = preg_replace("#\[color=(\#[0-9a-z]{6})\](.*)\[/color\]#Uis","<span style=\"color:\\1;\">\\2</span>",$entry);
$entry = preg_replace("#\[size=(-?)([0-9]+)\](.*)\[/size\]#Uis","<font size=\"\\1\\2\">\\3</font>",$entry);
return $entry;
}
function bbc_code_decode($entry) {
$entry = preg_replace("#<b>(.*)</b>#Uis","[b]\\1[/b]",$entry);
$entry = preg_replace("#<i>(.*)</i>#Uis","[i]\\1[/i]",$entry);
$entry = preg_replace("#<u>(.*)</u>#Uis","[u]\\1[/u]",$entry);
$entry = preg_replace("#<img src=\"([^\\\"]+)\"[^>]+>#Uis", "[img]\\1[/img]", $entry);
//$entry = preg_replace("\[img\]([^\[]+)\[/img\]","<img src=\"\\1\" border=\"0\">",$entry);
//$entry = preg_replace("\[mail\]([^\[]+)\[/mail\]","<a href=\"mailto:\\1\">\\1</a>",$entry);
$entry = preg_replace("#<a href=\"http://([^\\\"]+)\"[^>]*>(.*)</a>#Uis","[url=http://\\1]\\2[/url]",$entry);
$entry = preg_replace("#<a href=\"([^\\\"]+)\"[^>]+>(.*)</a>#Uis","[url=http://\\1]\\2[/url]",$entry);
$entry = preg_replace("#\<span style=\"color:(\#[0-9a-z]{6});\">(.*)</span>#Uis","[color=\\1]\\2[/color]",$entry);
$entry = preg_replace("#\<font size=\"(-?)([0-9]+)\">(.*)</font>#Uis","[size=\\1\\2]\\3[/size]",$entry);
return $entry;
}
*/
function bin2float ($bin) {
$dual = base_convert($bin,10,2);
while (strlen($dual)<32) {
$dual = "0$dual";
}
//echo "$bin<br />";
//echo "Dual: $dual - (".strlen($dual)."bits)<br />";
$sign = substr($dual, 0,1);
if ($sign=="1") $sign="-";
else $sign="";
$mantisse = substr($dual, 1,8);
$fractional = substr($dual, 9);
$decmantisse = base_convert($mantisse,2,10);
$exp = $decmantisse-127;
// echo "Sign: $sign<br />";
// echo "Mantisse: $mantisse<br />";
// echo "Fractional: $fractional<br />";
// echo "Exponent: $exp<br /><br />";
if ($exp==128 && $fractional=="11111111111111111111111") return $sign."∞";
elseif ($exp==128 && $fractional!="11111111111111111111111") return "-";
if ($exp==0) {
$frontfractional=1;
$backfractional=$fractional;
}
elseif ($exp<0) {
$frontfractional = 0;
$backfractional = "1$fractional";
for ($i=-1; $i>$exp; $i--) {
$backfractional = "0$backfractional";
}
}
else {
$frontfractional = "1".substr($fractional,0,$exp);
$backfractional = substr($fractional,$exp);
}
$norm_number = explode(',',"$frontfractional,$backfractional");
for ($i=0; $i<=1; $i++) {
$split_number = substr(chunk_split($norm_number[$i],1, ","), 0, -1);
$split_number = explode(",", $split_number);
if ($i==0) $hoch=count($split_number)-1;
else $hoch=-1;
foreach ($split_number as $key => $number) {
$erg += $number*pow(2,$hoch);
$hoch--;
}
}
return $sign.round($erg,2);
}
/* */
function imagetype($img, $width=0, $heigh=0) {
$info = getimagesize($img);
if (($info[2]==1 OR $info[2]==2 OR $info[2]==3) && ($width==0 OR $width>=$info[0]) && ($height==0 OR $height>=$info[1])) return true;
else return false;
}
function popup($value, $settings="STICKY, MOUSEOFF", $otherscripts="") {
if (strlen($settings)) $settings = ", {$settings}";
return "onmouseover=\"overlib('".str_replace(array("\n", "\r", "'", "'", '"'), array("", "", "\'", "\'", """), $value)."'{$settings}); {$otherscripts}\" onmouseout=\"return nd();\"";
}
function switchlayer($layer=array()) {
foreach ($layer as $layerid => $layerpictures) {
$layernames .= $comma.$layerid;
$comma = ",";
}
foreach ($layer as $layerid => $layerpicture) {
$layerdiv .= "<a href='javascript:switchlayer(\"{$layernames}\",\"{$layerid}\")'>$layerpicture</a>";
}
return "<div>$layerdiv</div>";
}
function string2timestamp($string) {
$duration = 0;
$temporal = 0;
foreach (str_split(strtolower($string)) as $character) {
if (preg_match("/^[0-9]$/", $character)) {
if ($temporal == 0) {
$temporal = $character;
} else {
$temporal = "{$temporal}{$character}";
}
}
else {
switch ($character) {
case "s": {
$duration = $duration + 1;
$temporal = 0;
break;
}
case "m": {
$duration = $duration + $temporal*60;
$temporal = 0;
break;
}
case "h": {
$duration = $duration + $temporal*3600;
$temporal = 0;
break;
}
case "d": {
$duration = $duration + $temporal*86400;
$temporal = 0;
break;
}
case "w": {
$duration = $duration + $temporal*604800;
$temporal = 0;
break;
}
case "q": {
$duration = $duration + $temporal*18144000;
$temporal = 0;
break;
}
case "y": {
$duration = $duration + $temporal*217728000;
$temporal = 0;
break;
}
}
}
}
return $duration;
}
function timetamp2string($span, $min=false) {
$d = bcdiv($span,86400,0);
$span = $span%86400;
$h = bcdiv($span,3600,0);
$span = $span%3600;
$m = bcdiv($span,60,0);
$s = $span%60;
$return = "";
if ($d) $return .= $d."d ";
if ($h OR $d) $return .= $h."h ";
if ($m OR $h OR $d) $return .= $m."m ";
if ($s OR $m OR $d OR $h) $return .= $s."s ";
if (strlen($return) < 1) $return = "0s";
if ($min) {
$return = explode($min, $return);
if (count($return)<2) return "<1{$min}";
else return $return[0].$min;
}
return trim($return);
}
?>