PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.0.18
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.0.18
1.2.73 1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 All 173 releases
userswp / includes / class-files.php

class-files.php in UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP 1.0.18, at includes/class-files.php

561 lines 20.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Files related functions
4 *
5 * This class defines all code necessary to upload files.
6 *
7 * @since 1.0.0
8 * @author GeoDirectory Team <info@wpgeodirectory.com>
9 */
10 class UsersWP_Files {
11
12 /**
13 * Handles file upload request.
14 *
15 * @since 1.0.0
16 * @package userswp
17 * @param object $field Field info object.
18 * @param array $files $_FILES array.
19 * @return bool|array Uploaded file url info array.
20 */
21 public function handle_file_upload($field, $files ) {
22
23 if ( isset( $files[ $field->htmlvar_name ] ) && ! empty( $files[ $field->htmlvar_name ] ) && ! empty( $files[ $field->htmlvar_name ]['name'] ) ) {
24
25 $extra_fields = unserialize($field->extra_fields);
26
27 $allowed_mime_types = array();
28 if (isset($extra_fields['uwp_file_types']) && !in_array("*", $extra_fields['uwp_file_types'])) {
29 $allowed_mime_types = $extra_fields['uwp_file_types'];
30 }
31
32 $allowed_mime_types = apply_filters('uwp_allowed_mime_types', $allowed_mime_types, $field->htmlvar_name);
33
34 $file_urls = array();
35 $files_to_upload = $this->uwp_prepare_files( $files[ $field->htmlvar_name ] );
36
37 $max_upload_size = $this->uwp_get_max_upload_size($field->form_type, $field->htmlvar_name);
38
39 if ( ! $max_upload_size ) {
40 $max_upload_size = 0;
41 }
42
43 foreach ( $files_to_upload as $file_key => $file_to_upload ) {
44
45 if (!empty($allowed_mime_types)) {
46 $ext = $this->uwp_get_file_type($file_to_upload['type']);
47
48 $allowed_error_text = implode(', ', $allowed_mime_types);
49 if ( !in_array( $ext , $allowed_mime_types ) )
50 return new WP_Error( 'validation-error', sprintf( __( 'Allowed files types are: %s', 'userswp' ), $allowed_error_text) );
51 }
52
53
54 if ( $file_to_upload['size'] > $max_upload_size) {
55 return new WP_Error( 'file-too-big', __( 'The uploaded file is too big. Maximum size allowed:'. $this->uwp_formatSizeUnits($max_upload_size), 'userswp' ) );
56 }
57
58
59 $error_result = apply_filters('uwp_handle_file_upload_error_checks', true, $field, $file_key, $file_to_upload);
60 if (is_wp_error($error_result)) {
61 return $error_result;
62 }
63
64 remove_filter( 'wp_handle_upload_prefilter', array($this, 'uwp_wp_media_restrict_file_types') );
65 if(in_array($field->htmlvar_name, array('uwp_banner_file','uwp_avatar_file'))){
66 add_filter( 'upload_dir', 'uwp_handle_multisite_profile_image', 10, 1 );
67 }
68 $uploaded_file = $this->uwp_upload_file( $file_to_upload, array( 'file_key' => $file_key ) );
69 add_filter( 'wp_handle_upload_prefilter', array($this, 'uwp_wp_media_restrict_file_types') );
70
71 if ( is_wp_error( $uploaded_file ) ) {
72
73 return new WP_Error( 'validation-error', $uploaded_file->get_error_message() );
74
75 } else {
76
77 $file_urls[] = array(
78 'url' => $uploaded_file->url,
79 'path' => $uploaded_file->path,
80 'size' => $uploaded_file->size,
81 'name' => $uploaded_file->name,
82 'type' => $uploaded_file->type,
83 );
84
85 }
86
87 }
88
89 return current( $file_urls );
90
91 }
92 return true;
93
94 }
95
96 /**
97 * Formats the file size into human readable form.
98 *
99 * @since 1.0.0
100 * @package userswp
101 * @param int $bytes Size in Bytes.
102 * @return string Size in human readable form.
103 */
104 public function uwp_formatSizeUnits($bytes)
105 {
106 if ($bytes >= 1073741824)
107 {
108 $bytes = number_format($bytes / 1073741824, 2) . ' GB';
109 }
110 elseif ($bytes >= 1048576)
111 {
112 $bytes = number_format($bytes / 1048576, 2) . ' MB';
113 }
114 elseif ($bytes >= 1024)
115 {
116 $bytes = number_format($bytes / 1024, 2) . ' kB';
117 }
118 elseif ($bytes > 1)
119 {
120 $bytes = $bytes . ' bytes';
121 }
122 elseif ($bytes == 1)
123 {
124 $bytes = $bytes . ' byte';
125 }
126 else
127 {
128 $bytes = '0 bytes';
129 }
130
131 return $bytes;
132 }
133
134 /**
135 * Formats the file size in KB.
136 *
137 * @since 1.0.0
138 * @package userswp
139 * @param int $bytes Size in Bytes.
140 * @return int Size in KB.
141 */
142 public function uwp_formatSizeinKb($bytes)
143 {
144 $kb = $bytes / 1024;
145 return $kb;
146 }
147
148 /**
149 * Gets the size is bytes for given value.
150 *
151 * @since 1.0.0
152 * @package userswp
153 * @param string $val Size in human readable form.
154 * @return int Value in bytes.
155 */
156 public function uwp_get_size_in_bytes($val) {
157 $val = trim($val);
158 $last = strtolower($val[strlen($val)-1]);
159 switch($last) {
160 // The 'G' modifier is available since PHP 5.1.0
161 case 'g':
162 $val *= (1024 * 1024 * 1024); //1073741824
163 break;
164 case 'm':
165 $val *= (1024 * 1024); //1048576
166 break;
167 case 'k':
168 $val *= 1024;
169 break;
170 }
171
172 return $val;
173 }
174
175 /**
176 * Processes the file upload.
177 *
178 * @since 1.0.0
179 * @package userswp
180 * @param array $file File info to upload.
181 * @param array $args File upload helper args.
182 * @return object Uploaded file info
183 */
184 public function uwp_upload_file( $file, $args = array() ) {
185
186 include_once ABSPATH . 'wp-admin/includes/file.php';
187 include_once ABSPATH . 'wp-admin/includes/media.php';
188
189 $args = wp_parse_args( $args, array(
190 'file_key' => '',
191 'file_label' => '',
192 'allowed_mime_types' => get_allowed_mime_types()
193 ) );
194
195 $uploaded_file = new stdClass();
196
197 if ( ! in_array( $file['type'], $args['allowed_mime_types'] ) ) {
198 if ( $args['file_label'] ) {
199 return new WP_Error( 'upload', sprintf( __( '"%s" (filetype %s) needs to be one of the following file types: %s', 'userswp' ), $args['file_label'], $file['type'], implode( ', ', array_keys( $args['allowed_mime_types'] ) ) ) );
200 } else {
201 return new WP_Error( 'upload', sprintf( __( 'Uploaded files need to be one of the following file types: %s', 'userswp' ), implode( ', ', array_keys( $args['allowed_mime_types'] ) ) ) );
202 }
203 } else {
204 $upload = wp_handle_upload( $file, apply_filters( 'uwp_handle_upload_overrides', array( 'test_form' => false ) ) );
205 if ( ! empty( $upload['error'] ) ) {
206 return new WP_Error( 'upload', $upload['error'] );
207 } else {
208 $uploaded_file->url = $upload['url'];
209 $uploaded_file->name = basename( $upload['file'] );
210 $uploaded_file->path = $upload['file'];
211 $uploaded_file->type = $upload['type'];
212 $uploaded_file->size = $file['size'];
213 $uploaded_file->extension = substr( strrchr( $uploaded_file->name, '.' ), 1 );
214 }
215 }
216
217
218 return $uploaded_file;
219 }
220
221 /**
222 * Prepares the files for upload
223 *
224 * @since 1.0.0
225 * @package userswp
226 * @param array $file_data Files to upload
227 * @return array Prepared files.
228 */
229 public function uwp_prepare_files( $file_data ) {
230 $files_to_upload = array();
231
232 if ( is_array( $file_data['name'] ) ) {
233 foreach ( $file_data['name'] as $file_data_key => $file_data_value ) {
234
235 if ( $file_data['name'][ $file_data_key ] ) {
236 $files_to_upload[] = array(
237 'name' => $file_data['name'][ $file_data_key ],
238 'type' => $file_data['type'][ $file_data_key ],
239 'tmp_name' => $file_data['tmp_name'][ $file_data_key ],
240 'error' => $file_data['error'][ $file_data_key ],
241 'size' => $file_data['size'][ $file_data_key ]
242 );
243 }
244 }
245 } else {
246 $files_to_upload[] = $file_data;
247 }
248
249 return $files_to_upload;
250 }
251
252 /**
253 * Validates the file uploads.
254 *
255 * @since 1.0.0
256 * @package userswp
257 * @param array $files $_FILES array
258 * @param string $type Form type.
259 * @param bool $url_only Return only the url or whole file info?
260 * @param array|bool $fields Form fields.
261 * @return array Validated data.
262 */
263 public function uwp_validate_uploads($files, $type, $url_only = true, $fields = false) {
264
265 $validated_data = array();
266
267 if (empty($files)) {
268 return $validated_data;
269 }
270
271 if (!$fields) {
272 global $wpdb;
273 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
274
275 if ($type == 'register') {
276 $fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $table_name . " WHERE form_type = %s AND field_type = 'file' AND is_active = '1' AND is_register_field = '1' ORDER BY sort_order ASC", array('account')));
277 } elseif ($type == 'account') {
278 $fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $table_name . " WHERE form_type = %s AND field_type = 'file' AND is_active = '1' AND is_register_only_field = '0' ORDER BY sort_order ASC", array('account')));
279 } else {
280 $fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $table_name . " WHERE form_type = %s AND field_type = 'file' AND is_active = '1' ORDER BY sort_order ASC", array($type)));
281 }
282 }
283
284
285 if (!empty($fields)) {
286 foreach ($fields as $field) {
287 if(isset($files[$field->htmlvar_name])) {
288
289 $file_urls = $this->handle_file_upload($field, $files);
290
291 if (is_wp_error($file_urls)) {
292 return $file_urls;
293 }
294
295 if ($url_only) {
296 $validated_data[$field->htmlvar_name] = $file_urls['url'];
297 } else {
298 $validated_data[$field->htmlvar_name] = $file_urls;
299 }
300 }
301
302 }
303 }
304
305 return $validated_data;
306 }
307
308 /**
309 * Displays the preview for images and links for other types above the field for existing uploads.
310 *
311 * @since 1.0.0
312 * @package userswp
313 * @param object $field Form field info.
314 * @param string $value Value of the field.
315 * @param bool $removable Is this value removable by user?
316 * @return string HTML output.
317 */
318 public function uwp_file_upload_preview($field, $value, $removable = true) {
319 $output = '';
320
321 $value = esc_html($value);
322
323 if ($field->htmlvar_name == "uwp_banner_file") {
324 $htmlvar = "uwp_account_banner_thumb";
325 } elseif ($field->htmlvar_name == "uwp_avatar_file") {
326 $htmlvar = "uwp_account_avatar_thumb";
327 } else {
328 $htmlvar = $field->htmlvar_name;
329 }
330
331 // If is current user's profile (profile.php)
332 if ( is_admin() && defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
333 $user_id = get_current_user_id();
334 // If is another user's profile page
335 } elseif (is_admin() && ! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) {
336 $user_id = $_GET['user_id'];
337 $user_id = (int) sanitize_text_field($user_id);
338 // Otherwise something is wrong.
339 } else {
340 $user_id = get_current_user_id();
341 }
342
343 if ($value) {
344
345 $upload_dir = wp_upload_dir();
346 if (substr( $value, 0, 4 ) !== "http") {
347 $value = $upload_dir['baseurl'].$value;
348 }
349
350 $file = basename( $value );
351 $filetype = wp_check_filetype($file);
352 $image_types = array('png', 'jpg', 'jpeg', 'gif');
353 if (in_array($filetype['ext'], $image_types)) {
354 $output .= '<div class="uwp_file_preview_wrap">';
355 $output .= '<a href="'.$value.'" class="uwp_upload_file_preview"><img style="max-width:100px;" src="'.$value.'" /></a>';
356 if ($removable) {
357 $output .= '<a onclick="return confirm(\'are you sure?\')" style="display: block;margin: 5px 0;" href="#" id="'.$htmlvar.'" data-htmlvar="'.$htmlvar.'" data-uid="'.$user_id.'" class="uwp_upload_file_remove">'. __( 'Remove Image' , 'userswp' ).'</a>';
358 }
359 $output .= '</div>';
360 ?>
361 <?php
362 } else {
363 $output .= '<div class="uwp_file_preview_wrap">';
364 $output .= '<a href="'.$value.'" class="uwp_upload_file_preview">'.$file.'</a>';
365 if ($removable) {
366 $output .= '<a onclick="return confirm(\'are you sure?\')" style="display: block;margin: 5px 0;" href="#" id="'.$htmlvar.'" data-htmlvar="'.$htmlvar.'" data-uid="'.$user_id.'" class="uwp_upload_file_remove">'. __( 'Remove File' , 'userswp' ).'</a>';
367 }
368 $output .= '</div>';
369 ?>
370 <?php
371 }
372 }
373 return $output;
374 }
375
376 /**
377 * restrict files to certain types
378 *
379 * @since 1.0.0
380 * @package userswp
381 * @param array $file File info.
382 * @return array Modified file info.
383 */
384 public function uwp_wp_media_restrict_file_types($file) {
385 // This bit is for the flash uploader
386 if ($file['type']=='application/octet-stream' && isset($file['tmp_name'])) {
387 $file_size = getimagesize($file['tmp_name']);
388 if (isset($file_size['error']) && $file_size['error']!=0) {
389 $file['error'] = "Unexpected Error: {$file_size['error']}";
390 return $file;
391 } else {
392 $file['type'] = $file_size['mime'];
393 }
394 }
395 list($category,$type) = explode('/',$file['type']);
396 if ('image'!=$category || !in_array($type,array('jpg','jpeg','gif','png'))) {
397 $file['error'] = "Sorry, you can only upload a .GIF, a .JPG, or a .PNG image file.";
398 } else if ($post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : false)) {
399 if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>0)
400 $file['error'] = "Sorry, you cannot upload more than one (1) image.";
401 }
402 return $file;
403 }
404
405 /**
406 * Check whether the uploads is from the profile page.
407 *
408 * @since 1.0.0
409 * @package userswp
410 * @return bool
411 */
412 public function uwp_doing_upload(){
413 return isset($_POST['uwp_profile_upload']) ? true : false;
414 }
415
416 /**
417 * Gets the maximum file upload size.
418 *
419 * @since 1.0.0
420 * @package userswp
421 * @param string|bool $form_type Form type.
422 * @param string|bool $field_htmlvar_name htmlvar_name key.
423 * @return int Allowed upload size.
424 */
425 public function uwp_get_max_upload_size($form_type = false, $field_htmlvar_name = false) {
426 if (is_multisite()) {
427 $network_setting_size = esc_attr( get_option( 'fileupload_maxk', 300 ) );
428 $max_upload_size = $this->uwp_get_size_in_bytes($network_setting_size.'k');
429 if ($max_upload_size > wp_max_upload_size()) {
430 $max_upload_size = wp_max_upload_size();
431 }
432 } else {
433 $max_upload_size = wp_max_upload_size();
434 }
435 $max_upload_size = apply_filters('uwp_get_max_upload_size', $max_upload_size, $form_type, $field_htmlvar_name);
436
437 return $max_upload_size;
438 }
439
440
441 /**
442 * Modifies the maximum file upload size based on the setting.
443 *
444 * @since 1.0.0
445 * @package userswp
446 *
447 * @param int $bytes Size in bytes.
448 * @param string $type File upload type.
449 *
450 * @return int Size in bytes.
451 */
452 public function uwp_modify_get_max_upload_size($bytes, $type) {
453
454 if ($type == 'avatar') {
455 $kb = uwp_get_option('profile_avatar_size', false);
456 if ($kb) {
457 $bytes = intval($kb) * 1024;
458 }
459 }
460
461 if ($type == 'banner') {
462 $kb = uwp_get_option('profile_banner_size', false);
463 if ($kb) {
464 $bytes = intval($kb) * 1024;
465 }
466 }
467
468 return $bytes;
469
470 }
471
472 /**
473 * Gets the file type using the extension.
474 * Ex: 'jpg' => 'image/jpeg'
475 *
476 * @since 1.0.0
477 * @package userswp
478 *
479 * @param string $ext Extension string. Ex: png, jpg
480 *
481 * @return string File type.
482 */
483 public function uwp_get_file_type($ext) {
484 $allowed_file_types = $this->allowed_mime_types();
485 $file_types = array();
486 foreach ( $allowed_file_types as $format => $types ) {
487 $file_types = array_merge($file_types, $types);
488 }
489 $file_types = array_flip($file_types);
490 return $file_types[$ext];
491 }
492
493 /**
494 * Returns allowed mime types in uploads
495 *
496 * @since 1.0.0
497 * @package userswp
498 *
499 * @return array Allowed mime types.
500 */
501 public function allowed_mime_types() {
502 return apply_filters( 'uwp_allowed_mime_types', array(
503 'Image' => array( // Image formats.
504 'jpg' => 'image/jpeg',
505 'jpe' => 'image/jpeg',
506 'jpeg' => 'image/jpeg',
507 'gif' => 'image/gif',
508 'png' => 'image/png',
509 'bmp' => 'image/bmp',
510 'ico' => 'image/x-icon',
511 ),
512 'Video' => array( // Video formats.
513 'asf' => 'video/x-ms-asf',
514 'avi' => 'video/avi',
515 'flv' => 'video/x-flv',
516 'mkv' => 'video/x-matroska',
517 'mp4' => 'video/mp4',
518 'mpeg' => 'video/mpeg',
519 'mpg' => 'video/mpeg',
520 'wmv' => 'video/x-ms-wmv',
521 '3gp' => 'video/3gpp',
522 ),
523 'Audio' => array( // Audio formats.
524 'ogg' => 'audio/ogg',
525 'mp3' => 'audio/mpeg',
526 'wav' => 'audio/wav',
527 'wma' => 'audio/x-ms-wma',
528 ),
529 'Text' => array( // Text formats.
530 'css' => 'text/css',
531 'csv' => 'text/csv',
532 'htm' => 'text/html',
533 'html' => 'text/html',
534 'txt' => 'text/plain',
535 'rtx' => 'text/richtext',
536 'vtt' => 'text/vtt',
537 ),
538 'Application' => array( // Application formats.
539 'doc' => 'application/msword',
540 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
541 'exe' => 'application/x-msdownload',
542 'js' => 'application/javascript',
543 'odt' => 'application/vnd.oasis.opendocument.text',
544 'pdf' => 'application/pdf',
545 'pot' => 'application/vnd.ms-powerpoint',
546 'ppt' => 'application/vnd.ms-powerpoint',
547 'pptx' => 'application/vnd.ms-powerpoint',
548 'psd' => 'application/octet-stream',
549 'rar' => 'application/rar',
550 'rtf' => 'application/rtf',
551 'swf' => 'application/x-shockwave-flash',
552 'tar' => 'application/x-tar',
553 'xls' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
554 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
555 'zip' => 'application/zip',
556 )
557 )
558 );
559 }
560
561 }