|
1
|
|
|
<?php |
|
2
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Base64 Handler library. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* PHP Version 7 |
|
10
|
|
|
* |
|
11
|
|
|
* LICENSE: This source file is subject to the MIT license that is available |
|
12
|
|
|
* through the world-wide-web at the following URI: |
|
13
|
|
|
* http://opensource.org/licenses/mit-license.php |
|
14
|
|
|
* |
|
15
|
|
|
* @category Src |
|
16
|
|
|
* @package Normeno\Base64Hanlder |
|
17
|
|
|
* @author Nicolas Ormeno <[email protected]> |
|
18
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
|
19
|
|
|
* @link https://github.com/normeno/base64_handler |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Normeno\Base64Handler; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Utils Class |
|
25
|
|
|
* |
|
26
|
|
|
* @category src |
|
27
|
|
|
* @package Normeno\Base64Handler |
|
28
|
|
|
* @author Nicolas Ormeno <[email protected]> |
|
29
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
|
30
|
|
|
* @link https://github.com/normeno/base64_handler |
|
31
|
|
|
*/ |
|
32
|
|
|
class Utils |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Clear string |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $str base64 string |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool|string |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function clearString(string $str) |
|
42
|
|
|
{ |
|
43
|
|
|
try { |
|
44
|
|
|
$explode = explode('base64,', $str); |
|
45
|
|
|
return array_key_exists(1, $explode) ? $explode[1] : $explode[0]; |
|
46
|
|
|
} catch (\Exception $e) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $path |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function isValidPath(string $path) |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
|
|
return file_exists($path) ? true : false; |
|
60
|
|
|
} catch (\Exception $e) { |
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get base64's extension |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $base64 Base64 string |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool|array |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function getExtFromBase64(string $base64) |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
|
|
$base64 = Utils::clearString($base64); |
|
76
|
|
|
$decoded = base64_decode($base64); |
|
77
|
|
|
$fopen = finfo_open(); |
|
78
|
|
|
|
|
79
|
|
|
$mime = finfo_buffer($fopen, $decoded, FILEINFO_MIME_TYPE); |
|
80
|
|
|
finfo_close($fopen); |
|
81
|
|
|
|
|
82
|
|
|
$ext = explode('/', $mime)[1]; |
|
83
|
|
|
$type = self::getTypeFromExt($ext); |
|
84
|
|
|
|
|
85
|
|
|
$resp = [ |
|
86
|
|
|
'mime' => $mime, |
|
87
|
|
|
'ext' => $ext, |
|
88
|
|
|
'type' => $type |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
return $resp; |
|
92
|
|
|
} catch (\Exception $e) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get file's extension |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $path file path |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function getExtFromFile(string $path) |
|
105
|
|
|
{ |
|
106
|
|
|
$resp = [ |
|
107
|
|
|
'mime' => null, |
|
108
|
|
|
'ext' => null, |
|
109
|
|
|
'type' => null |
|
110
|
|
|
]; |
|
111
|
|
|
|
|
112
|
|
|
try { |
|
113
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
114
|
|
|
|
|
115
|
|
|
if (finfo_file($finfo, $path)) { |
|
116
|
|
|
$ext = explode('.', $path); |
|
117
|
|
|
$ext = end($ext); |
|
118
|
|
|
|
|
119
|
|
|
$resp = [ |
|
120
|
|
|
'mime' => finfo_file($finfo, $path), |
|
121
|
|
|
'ext' => $ext, |
|
122
|
|
|
'type' => self::getTypeFromExt($ext) |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $resp; |
|
127
|
|
|
} catch (\Exception $e) { |
|
128
|
|
|
return $resp; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Know if file is image or file |
|
134
|
|
|
* |
|
135
|
|
|
* @param $ext |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function getTypeFromExt($ext) |
|
139
|
|
|
{ |
|
140
|
|
|
$images = ['png', 'jpeg', 'jpg', 'bpm', 'gif']; |
|
141
|
|
|
$files = ['doc', 'docx', 'xls', 'xlsx', 'pdf', 'svg', 'svg+xml']; |
|
142
|
|
|
|
|
143
|
|
|
if (in_array($ext, $images)) { |
|
144
|
|
|
return 'image'; |
|
145
|
|
|
} else if (in_array($ext, $files)) { |
|
146
|
|
|
return 'file'; |
|
147
|
|
|
} else { |
|
148
|
|
|
return 'unknown'; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |