-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlib.php
100 lines (76 loc) · 2.43 KB
/
lib.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
<?php
# Utilities
# Get system MIME types list
function system_extension_mime_types() {
$out = array();
$file = fopen('/etc/mime.types', 'r');
while(($line = fgets($file)) !== false) {
$line = trim(preg_replace('/#.*/', '', $line));
if(!$line) continue;
$parts = preg_split('/\s+/', $line);
if(count($parts) == 1) continue;
$type = array_shift($parts);
foreach($parts as $part) $out[$part] = $type;
}
fclose($file);
return $out;
}
# Get file MIME type by its extension
function system_extension_mime_type($file) {
static $types;
if(!isset($types)) $types = system_extension_mime_types();
$ext = pathinfo($file, PATHINFO_EXTENSION);
if(!$ext) $ext = $file;
$ext = strtolower($ext);
return isset($types[$ext]) ? $types[$ext] : null;
}
# generate random file prefix-id
function gen_id()
{
$id = '';
$abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$abc .= "abcdefghijklmnopqrstuvwxyz";
$abc .= "0123456789";
$abc .= '_-';
$max = strlen($abc);
for ($i=0; $i < 5; $i++) $id .= $abc[mt_rand(0, $max-1)];
return $id;
}
# get max file upload size from settings
function file_upload_max_size() {
static $max_size = -1;
if ($max_size < 0) {
// Start with post_max_size.
$post_max_size = parse_ini_bytes_size(ini_get('post_max_size'));
if ($post_max_size > 0) {
$max_size = $post_max_size;
}
// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
$upload_max = parse_ini_bytes_size(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
}
// Convert to GB
$max_size = $max_size / (1024 * 1024 * 1024);
if ( $max_size < 1 ) {
$max_size = round($max_size, 3);
}
else {
$max_size = round($max_size);
}
return $max_size . 'G';
}
# parse php.ini size value
function parse_ini_bytes_size($size) {
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
$size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
if ($unit) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
}
else {
return round($size);
}
}