|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Common utilities |
|
5
|
|
|
*/ |
|
6
|
|
|
class Xhgui_Util |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Creates a simplified URL given a standard URL. |
|
10
|
|
|
* Does the following transformations: |
|
11
|
|
|
* |
|
12
|
|
|
* - Remove numeric values after =. |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $url |
|
15
|
|
|
* @return string |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function simpleUrl($url) |
|
18
|
|
|
{ |
|
19
|
|
|
$callable = Xhgui_Config::read('profiler.simple_url'); |
|
20
|
|
|
if (is_callable($callable)) { |
|
21
|
|
|
return call_user_func($callable, $url); |
|
22
|
|
|
} |
|
23
|
|
|
return preg_replace('/\=\d+/', '', $url); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Serialize data for storage |
|
28
|
|
|
* |
|
29
|
|
|
* @param $data |
|
30
|
|
|
* @param bool $profiles |
|
31
|
|
|
* |
|
32
|
|
|
* @return false|string |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function getDataForStorage($data, $profiles = true) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($profiles) { |
|
37
|
|
|
$serializer = Xhgui_Config::read('save.handler.serializer', 'json'); |
|
38
|
|
|
} else { |
|
39
|
|
|
$serializer = Xhgui_Config::read('save.handler.meta_serializer', 'php'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
switch ($serializer) { |
|
43
|
|
|
case 'json': |
|
44
|
|
|
return json_encode($data); |
|
45
|
|
|
break; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
case 'igbinary_serialize': |
|
48
|
|
|
case 'igbinary_unserialize': |
|
49
|
|
|
case 'igbinary': |
|
50
|
|
|
return igbinary_serialize($data); |
|
51
|
|
|
break; |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
case 'php': |
|
54
|
|
|
case 'var_export': |
|
55
|
|
|
return "<?php \n".var_export($data, true); |
|
56
|
|
|
break; |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get id for a record. |
|
62
|
|
|
* |
|
63
|
|
|
* By default this method will try to re-use request id from http server. |
|
64
|
|
|
* This is needed for some storage engines that don't have string/hashlike id generation. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $data |
|
67
|
|
|
* @param bool $useRequestId |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function getId(array $data = array(), $useRequestId = true) |
|
72
|
|
|
{ |
|
73
|
|
|
|
|
74
|
|
|
// in some cases, like during import, we might already have id |
|
75
|
|
|
if (!empty($data['id'])) { |
|
76
|
|
|
return $data['id']; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// mongo compatibility |
|
80
|
|
|
if (!empty($data['_id'])) { |
|
81
|
|
|
return $data['_id']; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($useRequestId) { |
|
85
|
|
|
foreach(array('REQUEST_ID', 'HTTP_REQUEST_ID', 'HTTP_X_REQUEST_ID', 'X_CORRELATION_ID', 'HTTP_X_CORRELATION_ID') as $header) { |
|
86
|
|
|
if (array_key_exists($header, $_SERVER) !== false) { |
|
87
|
|
|
return $_SERVER[$header]; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// try php 7+ function. |
|
93
|
|
|
if (function_exists('random_bytes')) { |
|
94
|
|
|
try { |
|
95
|
|
|
return bin2hex(random_bytes(16)); |
|
96
|
|
|
} catch (\Exception $e) { |
|
97
|
|
|
// entropy source is not available |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// try openssl. For purpose of this id we can ignore info if this value is strong or not |
|
102
|
|
|
if (function_exists('openssl_random_pseudo_bytes')) { |
|
103
|
|
|
/** @noinspection CryptographicallySecureRandomnessInspection */ |
|
104
|
|
|
return bin2hex(openssl_random_pseudo_bytes(16, $strong)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// fallback to most generic method. Make sure it has 32 characters :) |
|
108
|
|
|
return md5(uniqid('xhgui', true).microtime()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param array $data |
|
|
|
|
|
|
114
|
|
|
* @return mixed|string |
|
115
|
|
|
*/ |
|
116
|
|
|
public static function getMethod() { |
|
117
|
|
|
if(PHP_SAPI ==='cli') { |
|
118
|
|
|
return 'CLI'; |
|
119
|
|
|
} |
|
120
|
|
|
if (!empty($_SERVER['REQUEST_METHOD'])) { |
|
121
|
|
|
return $_SERVER['REQUEST_METHOD']; |
|
122
|
|
|
} |
|
123
|
|
|
return 'UNKNOWN'; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.