/
Util.php
89 lines (80 loc) · 2.57 KB
/
Util.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
<?php
namespace Filehosting\Helpers;
use Filehosting\Exceptions\ConfigException;
class Util
{
/**
* Reads JSON config file and returns array with config settings
* If config file doesn't exist or there is an error, throws ConfigException
*
* @param $JSONpath
* @return array
* @throws ConfigException
*/
public static function readJSON($JSONpath): array
{
if (!file_exists($JSONpath)) {
throw new ConfigException('Файл конфигурации не существует');
}
$fileContent = file_get_contents($JSONpath);
$fileContent = json_decode($fileContent, true);
if ($fileContent == null) {
throw new ConfigException('Ошибка в файле конфигурации. Ошибка: ' . json_last_error_msg());
}
return $fileContent;
}
/**
* Returns random string token
*
* @param int $length
* @return string
*/
public static function generateToken($length = 16): string
{
return $token = bin2hex(random_bytes($length));
}
/**
* Transliterates filename
* If file extension is not safe to store on server, changes it to .txt
*
* @param string $normalizedFilename
* @return null|string|string[]
*/
public static function generateSafeFilename(string $normalizedFilename)
{
$safeName = transliterator_transliterate('Any-Latin; Latin-ASCII', $normalizedFilename);
// normalizes file's name one more time because after transliteration it can contain more characters than allowed
$safeName = self::normalizeFilename($safeName);
return preg_replace('/.(htaccess|php|html|phtml)$/', '.txt', $safeName);
}
/**
* Checks if a filename contains more than 150 characters
* If it does, shortens it
*
* @param $filename
* @return bool|string
*/
public static function normalizeFilename($filename)
{
$ext = self::getFileExtension($filename);
if (mb_strlen($ext) > 10) {
$filename = substr($filename, 0, 150);
} elseif (mb_strlen($filename) > 150) {
$filename = substr($filename, 0, 150 - (mb_strlen($ext) + 1)) . '.' . $ext;
}
return $filename;
}
/**
*
* @param string $filename
* @return string
*/
public static function getFileExtension(string $filename)
{
if (!strpos($filename, '.')) {
return '';
}
preg_match('/.([^\.]+$)/i', $filename, $extension);
return $extension[1];
}
}