PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / 3.2.7
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress v3.2.7
4.17.2 4.17.1 4.17.0 4.16.19 4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / src / Classes / FileUploader.php
wp-user-avatar / src / Classes Last commit date
Installer 5 years ago AdminNotices.php 4 years ago AjaxHandler.php 4 years ago Autologin.php 4 years ago BuddyPressBbPress.php 5 years ago EditUserProfile.php 4 years ago ExtensionManager.php 4 years ago FileUploader.php 4 years ago FormPreviewHandler.php 5 years ago FormRepository.php 4 years ago FormShortcodeDefaults.php 5 years ago GDPR.php 5 years ago GlobalSiteAccess.php 4 years ago ImageUploader.php 4 years ago LoginAuth.php 4 years ago Miscellaneous.php 5 years ago ModifyRedirectDefaultLinks.php 5 years ago PPRESS_Session.php 4 years ago PROFILEPRESS_sql.php 5 years ago PasswordReset.php 5 years ago ProfileUrlRewrite.php 4 years ago RegistrationAuth.php 4 years ago SendEmail.php 5 years ago ShortcodeThemeFactory.php 5 years ago UserAvatar.php 4 years ago UserSignupLocationListingPage.php 5 years ago UsernameEmailRestrictLogin.php 4 years ago WelcomeEmailAfterSignup.php 5 years ago default-email-template.php 5 years ago index.php 5 years ago
FileUploader.php
197 lines
1 <?php
2
3 namespace ProfilePress\Core\Classes;
4
5 use WP_Error;
6
7 /**
8 * @package ProfilePress custom file upload PHP class
9 *
10 * @since 1.10
11 */
12 class FileUploader
13 {
14 /** init poop */
15 public static function init()
16 {
17 $result = array();
18
19 if (empty($_FILES)) return $result;
20
21 // remove registration and edit profile avatar and cover photo from files uploaded to be processed.
22 $skip = ['wpua-file', 'reg_avatar', 'eup_avatar', 'reg_cover_image', 'eup_cover_image'];
23
24 $valid_custom_usermeta = array_keys(ppress_custom_fields_key_value_pair(true));
25
26 foreach ($_FILES as $field_key => $uploaded_file_array) {
27
28 if ( ! in_array($field_key, $skip) && in_array($field_key, $valid_custom_usermeta) && ! empty($uploaded_file_array['name'])) {
29 $result[$field_key] = self::process($uploaded_file_array, $field_key);
30 }
31 }
32
33 return $result;
34 }
35
36 /**
37 * Upload the file
38 *
39 * @param $file
40 *
41 * @return bool|WP_Error
42 */
43 public static function process($file, $field_key)
44 {
45 /**
46 * Fires before image is process for validation and uploading
47 */
48 do_action('ppress_before_saving_file', $file, $field_key);
49
50 // validate all uploaded file but only return error for file with hidden "required-"field-key" field POSTed data.
51 // i.e if the file has the "required" shortcode attribute.
52 // added in "reg_custom_profile_field" in registration shortcode parser.
53
54 if ($file["error"] !== 0) {
55 self::error_file_logger($file, self::codeToMessage($file["error"]));
56 if (isset($_POST["required-{$field_key}"])) {
57 return new WP_Error(
58 'file_error',
59 apply_filters('ppress_file_unexpected_error',
60 esc_html__('Unexpected error with file upload, Please try again.', 'wp-user-avatar'),
61 $field_key
62 )
63 );
64 }
65
66 return false;
67 }
68
69 // filesize in megabyte. default is 10MB
70 $file_size = apply_filters('ppress_file_upload_size', 10, $field_key);
71
72 if ($file["size"] > ($file_size * 1000000)) {
73 return new WP_Error('file_too_large', apply_filters(
74 'ppress_file_too_large',
75 sprintf(
76 esc_html__('Uploaded file is greater than the allowed sized of %s', 'wp-user-avatar'),
77 "$file_size MB"
78 ),
79 $field_key
80 )
81 );
82 }
83
84 // array of allowed file extensions
85 $extensions = PROFILEPRESS_sql::get_field_option_values($field_key);
86
87 $allowed_extensions = array_filter(array_map('trim', explode(',', $extensions)), function ($value) {
88 return ! empty($value);
89 });
90
91 if ( ! is_array($allowed_extensions) || empty($allowed_extensions)) $allowed_extensions = [];
92
93 $filename = $file['name'];
94
95 $mime_check = ppress_check_type_and_ext($file, [], $allowed_extensions);
96
97 if (is_wp_error($mime_check)) {
98
99 return new WP_Error('invalid_file', $filename . ' ' . apply_filters('ppress_invalid_file_error', esc_html__('appears to be of an invalid file format. Please try again.', 'wp-user-avatar'), $field_key));
100 }
101
102 $file_upload_dir = apply_filters('ppress_file_upload_dir', PPRESS_FILE_UPLOAD_DIR, $field_key);
103
104 // ensure a safe filename
105 $file_name = preg_replace("/[^A-Z0-9._-]/i", "_", $filename);
106
107 // don't overwrite an existing file
108 $i = 0;
109 $file_exist_parts = pathinfo($file_name);
110 while (file_exists($file_upload_dir . $file_name)) {
111 $i++;
112 $file_name = $file_exist_parts["filename"] . "-" . $i . "." . $file_exist_parts["extension"];
113 }
114
115 // does file uploads folder exist? if NO, create it.
116 if ( ! file_exists($file_upload_dir)) {
117 mkdir($file_upload_dir, 0755);
118 }
119
120 // create index.php file in file uploads folder
121 if ( ! file_exists($file_upload_dir . '/index.php')) {
122 ppress_create_index_file($file_upload_dir);
123 }
124
125 // preserve file from temporary directory
126 $success = @move_uploaded_file($file["tmp_name"], $file_upload_dir . $file_name);
127
128 if ( ! $success) {
129 return new WP_Error ('save_error',
130 sprintf(__("Unable to save %s, please try again.", 'wp-user-avatar'), $file_name));
131 }
132
133 // set proper permissions on the new file
134 chmod($file_upload_dir . $file_name, 0644);
135
136 /**
137 * Fires after file have been saved
138 *
139 * @param string $file_name uploaded image url
140 */
141 do_action('ppress_after_saving_file', $file_name, $file_upload_dir);
142
143 return $file_name;
144 }
145
146 /**
147 * Error message of file upload converted from errorCode to readable message.
148 *
149 * @param int $code
150 *
151 * @return string
152 */
153 public static function codeToMessage($code)
154 {
155 switch ($code) {
156 case UPLOAD_ERR_INI_SIZE:
157 $message = __("The uploaded file exceeds the upload_max_filesize directive in php.ini", 'wp-user-avatar');
158 break;
159 case UPLOAD_ERR_FORM_SIZE:
160 $message = __("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 'wp-user-avatar');
161 break;
162 case UPLOAD_ERR_PARTIAL:
163 $message = __("The uploaded file was only partially uploaded", 'wp-user-avatar');
164 break;
165 case UPLOAD_ERR_NO_FILE:
166 $message = __("No file was uploaded", 'wp-user-avatar');
167 break;
168 case UPLOAD_ERR_NO_TMP_DIR:
169 $message = __("Missing a temporary folder", 'wp-user-avatar');
170 break;
171 case UPLOAD_ERR_CANT_WRITE:
172 $message = __("Failed to write file to disk", 'wp-user-avatar');
173 break;
174 case UPLOAD_ERR_EXTENSION:
175 $message = __("File upload stopped by extension", 'wp-user-avatar');
176 break;
177
178 default:
179 $message = "Unknown upload error";
180 break;
181 }
182
183 return $message;
184 }
185
186
187 /**
188 * @param array $file_array global $_File['field_key'] of the file.
189 * @param string $error_message
190 */
191 public static function error_file_logger($file_array, $error_message)
192 {
193 $error = $error_message . ' => ' . json_encode($file_array);
194
195 ppress_log_error($error);
196 }
197 }