-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.php
212 lines (189 loc) · 5.61 KB
/
Data.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
<?php
namespace CleantalkSP\Common\Helpers;
/**
* Class Data
* Gather static functions to work with data in different ways
*
* @version 1.0.0
* @package CleantalkSP\Common\Helpers
* @author Cleantalk team (welcome@cleantalk.org)
* @copyright (C) CleanTalk team (http://cleantalk.org)
* @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
* @see https://github.com/CleanTalk/security-malware-firewall
*/
class Data
{
/**
* Function removing non UTF8 characters from array|string|object
* Recursive
*
* @param array|object|string $data
*
* @return array|object|string
* @psalm-suppress PossiblyUnusedMethod
*/
public static function removeNonUTF8($data)
{
// Array || object
if (is_array($data) || is_object($data)) {
foreach ( $data as $_key => &$val ) {
$val = static::removeNonUTF8($val); // Recursion
}
unset($val);
//String
} elseif ( ! preg_match('//u', $data)) {
$data = 'Nulled. Not UTF8 encoded or malformed.';
}
return $data;
}
/**
* Function convert anything to UTF8 and removes non UTF8 characters
* Recursive
*
* @param array|object|string $obj
* @param $data_codepage
*
* @return array|false|object|string|string[]|null
* @psalm-suppress PossiblyUnusedMethod
*/
public static function convertToUTF8($obj, $data_codepage = null)
{
// Array || object
if (is_array($obj) || is_object($obj)) {
foreach ($obj as $_key => &$val) {
$val = static::convertToUTF8($val, $data_codepage); // Recursion
}
unset($val);
//String
} elseif (
function_exists('mb_detect_encoding') &&
function_exists('mb_convert_encoding') &&
! preg_match('//u', $obj) // Check persistence of non-UTF8 characters
) {
$encoding = mb_detect_encoding($obj);
$encoding = $encoding ?: $data_codepage;
if ($encoding) {
$obj = mb_convert_encoding($obj, 'UTF-8', $encoding);
}
}
return $obj;
}
/**
* Converts from UTF8
* Recursive
*
* @param array|object|string $obj
* @param string $data_codepage
*
* @return array|false|object|string|string[]|null (array|object|string)
* @psalm-suppress PossiblyUnusedMethod
*/
public static function convertFromUTF8($obj, $data_codepage = null)
{
// Array || object
if (is_array($obj) || is_object($obj)) {
foreach ($obj as $_key => &$val) {
$val = self::convertFromUTF8($val, $data_codepage); // Recursion
}
unset($val);
//String
} elseif (
$data_codepage !== null &&
function_exists('mb_convert_encoding') &&
preg_match('//u', $obj) // Check persistence of UTF8 characters
) {
$obj = mb_convert_encoding($obj, $data_codepage, 'UTF-8');
}
return $obj;
}
/**
* Checks if the string is valid JSON type
*
* @param string $string
*
* @return bool
*/
public static function isJSON($string)
{
return is_string($string) &&
is_array(json_decode($string, true));
}
/**
* Get mime type from file or data
*
* @param string $data Path to file or data
* @param string $type Default mime type. Returns if function failed to detect type
*
* @return string
*/
public static function getMIMEType($data, $type = '')
{
// Clean input of null bytes
$data = str_replace(chr(0), '', $data);
if ( ! empty($data) && @file_exists($data)) {
$type = mime_content_type($data);
} elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_buffer($finfo, $data);
finfo_close($finfo);
}
return $type;
}
/**
* Recursively deletes the directory and its content
*
* @param $dir_path string The directory path to delete
*
* @return bool
*/
public static function removeDirectoryRecursively($dir_path)
{
if (is_dir($dir_path) && is_writable($dir_path)) {
$files = glob($dir_path . '/*');
if (! empty($files)) {
foreach ($files as $file) {
if (! static::remove($file)) {
return false;
}
}
}
//ignore rmd warns if folder contains . prefix in files/folders
return @rmdir($dir_path);
}
return true;
}
/**
* Deletes any type of data (files, directories, links...)
*
* @param $path string The filesystem path to delete anything
*
* @return bool
*/
public static function remove($path)
{
if (! is_writable($path)) {
return false;
}
if (is_file($path)) {
return @unlink($path);
}
if (is_dir($path)) {
return self::removeDirectoryRecursively($path);
}
return true;
}
/**
* Extract last part from URI or file path
*
* @param $url string
*
* @return string
* @psalm-suppress PossiblyUnusedMethod
*/
public static function getFilenameFromUrl($url)
{
$url = parse_url($url, PHP_URL_PATH);
$path_parts = explode('/', $url);
return end($path_parts);
}
}