helper.php
2.77 KB
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
<?php if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class NF_FU_Helper
*
* The Static Helper Class
*
* Provides helper functionality for File Uploads
*/
final class NF_FU_Helper {
public static function random_string( $length = 10 ) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$random_string = '';
for ( $i = 0; $i < $length; $i++ ) {
$random_string .= $characters[ rand( 0, strlen( $characters ) - 1 ) ];
}
return $random_string;
}
/**
* Get the max chunk size for uploads to the server and services.
*
* @return bool|mixed|void
*/
public static function get_max_chunk_size() {
return apply_filters( 'ninja_forms_upload_max_chunk_size', round( self::max_upload_bytes_int() * 0.9 ) );
}
/**
* Get the lowest integer in MB for upload max size.
*
* @return float|int|mixed
*/
public static function max_upload_bytes_int() {
$u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
$p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) );
return min( $u_bytes, $p_bytes );
}
/**
* Get the lowest integer in MB for upload max size.
*
* @return float|int|mixed
*/
public static function max_upload_mb_int() {
$max = self::max_upload_bytes_int();
$max = $max / MB_IN_BYTES;
return $max;
}
/**
* Fix for overflowing signed 32 bit integers,
* works for sizes up to 2^32-1 bytes (4 GiB - 1):
*
* @param $size
*
* @return float
*/
public static function fix_integer_overflow( $size ) {
if ( $size < 0 ) {
$size += 2.0 * ( PHP_INT_MAX + 1 );
}
return $size;
}
public static function get_file_size( $file_path, $clear_stat_cache = false ) {
if ( $clear_stat_cache ) {
if ( version_compare( PHP_VERSION, '5.3.0' ) >= 0 ) {
clearstatcache( true, $file_path );
} else {
clearstatcache();
}
}
return self::fix_integer_overflow( filesize( $file_path ) );
}
/**
* Are we on the FU page?
*
* @param string $tab
* @param array $args
*
* @return bool
*/
public static function is_page( $tab = '', $args = array() ) {
global $pagenow;
if ( 'admin.php' !== $pagenow ) {
return false;
}
$defaults = array( 'page' => 'ninja-forms-uploads' );
if ( $tab ) {
$defaults['tab'] = $tab;
}
$args = array_merge( $args, $defaults );
foreach ( $args as $key => $value ) {
if ( ! isset( $_GET[ $key ] ) ) {
return false;
}
if ( false !== $value && $value !== $_GET[ $key ] ) {
return false;
}
}
return true;
}
public static function remove_directory_from_file( $file ) {
$file_dir = dirname( $file );
if ( $file_dir != '.' ) {
$file = str_replace( $file_dir, '', $file ); // Stop traversal exploits
}
$file = ltrim( $file, DIRECTORY_SEPARATOR );
return $file;
}
} // End Class WPN_Helper