-
Notifications
You must be signed in to change notification settings - Fork 1
/
Helper.php
87 lines (77 loc) · 1.86 KB
/
Helper.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
<?php
namespace core;
use Lifo\IP\CIDR;
/**
* Global Helpers.
*/
class Helper
{
// Read conf.d/$name(-test).json
// We use the -test version for keeping our
// tests static and simple.
public static function config($name)
{
$files = [];
$cfg = self::client();
if (isset($cfg["test"])) {
$files[] = ROOT . "/conf.d/$name-test.json";
}
$files[] = ROOT . "/conf.d/$name.json";
foreach ($files as $file) {
if (file_exists($file)) {
$res = json_decode(file_get_contents($file), true);
if (! is_array($res)) {
user_error(sprintf("Helper::config(%s): File could not be decoded to array.", $file));
}
return $res;
}
}
user_error(sprintf("Helper::config(%s): File does not exist.", $name));
}
// Random value having a length of $len.
public static function rand($len)
{
$token = "";
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
$codeAlphabet.= "0123456789";
$max = strlen($codeAlphabet); // edited
for ($i=0; $i < $len; $i++) {
$token .= $codeAlphabet[random_int(0, $max-1)];
}
return $token;
}
// Generate uniqueid to generate up to million of unique ids (userstr could be: clientip, userid, nodename)
public static function unique($userstr)
{
return base_convert(md5(uniqid("", true) . time() . "1" . $userstr), 16, 36);
}
/** Check if $ip is in $cidrs (ranges) */
public static function in_range($ip, array $cidrs)
{
foreach ($cidrs as $cidr) {
if (CIDR::INTERSECT_YES === CIDR::cidr_intersect($ip, $cidr)) {
return true;
}
}
return false;
}
public static function client()
{
global $_CLIENT;
return $_CLIENT;
}
public static function client_new(array $replacement)
{
global $_CLIENT;
$_CLIENT = $replacement;
}
public static function prefix($txt)
{
return sprintf(
"%s-%s",
self::config("general")["syskey"],
$txt
);
}
}