PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.35
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.35
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 1.0.23 All 172 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.2.35, at includes/class-files.php

625 lines 23.5 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_fields_allowed_mime_types', $allowed_mime_types, $field->htmlvar_name);
33
34 $file_urls = array();
35 $files_to_upload = $this->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->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, 'wp_media_restrict_file_types') );
65 if(in_array($field->htmlvar_name, array('avatar', 'banner'))){
66 add_filter( 'upload_dir', 'uwp_handle_multisite_profile_image', 10, 1 );
67 }
68 $uploaded_file = $this->upload_file( $file_to_upload, array( 'file_key' => $file_key ) );
69 add_filter( 'wp_handle_upload_prefilter', array($this, '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 $val = substr($val, 0, -1);
160 switch($last) {
161 // The 'G' modifier is available since PHP 5.1.0
162 case 'g':
163 $val *= (1024 * 1024 * 1024); //1073741824
164 break;
165 case 'm':
166 $val *= (1024 * 1024); //1048576
167 break;
168 case 'k':
169 $val *= 1024;
170 break;
171 }
172
173 return $val;
174 }
175
176 /**
177 * Processes the file upload.
178 *
179 * @since 1.0.0
180 * @package userswp
181 * @param array $file File info to upload.
182 * @param array $args File upload helper args.
183 * @return object Uploaded file info
184 */
185 public function upload_file( $file, $args = array() ) {
186
187 include_once ABSPATH . 'wp-admin/includes/file.php';
188 include_once ABSPATH . 'wp-admin/includes/media.php';
189 include_once ABSPATH . 'wp-admin/includes/image.php';
190
191 $args = wp_parse_args( $args, array(
192 'file_key' => '',
193 'file_label' => '',
194 'allowed_mime_types' => get_allowed_mime_types()
195 ) );
196
197 $uploaded_file = new stdClass();
198
199 if ( ! in_array( $file['type'], $args['allowed_mime_types'] ) ) {
200 if ( $args['file_label'] ) {
201 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'] ) ) ) );
202 } else {
203 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'] ) ) ) );
204 }
205 } else {
206 $upload = wp_handle_upload( $file, apply_filters( 'uwp_handle_upload_overrides', array( 'test_form' => false ) ) );
207
208 if ( ! empty( $upload['error'] ) ) {
209 return new WP_Error( 'upload', $upload['error'] );
210 } else {
211 if ( ! empty( $upload['type'] ) && $upload['type'] != 'image/png' && strpos( $upload['type'], 'image/' ) === 0 ) {
212 // Fetch additional metadata from EXIF/IPTC.
213 $exif_meta = wp_read_image_metadata( $upload['file'] );
214
215 if ( ! empty( $exif_meta ) && is_array( $exif_meta ) && ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) {
216 $editor = wp_get_image_editor( $upload['file'] );
217
218 if ( ! empty( $editor ) && ! is_wp_error( $editor ) ) {
219 // Rotate the whole original image if there is EXIF data and "orientation" is not 1.
220 $rotated = $editor->maybe_exif_rotate();
221 $rotated = $rotated === true ? $editor->save( $editor->generate_filename( 'rotated' ) ) : false;
222
223 if ( ! empty( $rotated ) && ! is_wp_error( $rotated ) && ! empty( $rotated['path'] ) ) {
224 $upload['url'] = str_replace( basename( $upload['url'] ), basename( $rotated['path'] ), $upload['url'] );
225 $upload['file'] = $rotated['path'];
226 }
227 }
228 }
229 }
230
231 $uploaded_file->url = $upload['url'];
232 $uploaded_file->name = basename( $upload['file'] );
233 $uploaded_file->path = $upload['file'];
234 $uploaded_file->type = $upload['type'];
235 $uploaded_file->size = $file['size'];
236 $uploaded_file->extension = substr( strrchr( $uploaded_file->name, '.' ), 1 );
237 }
238 }
239
240 return $uploaded_file;
241 }
242
243 /**
244 * Prepares the files for upload
245 *
246 * @since 1.0.0
247 * @package userswp
248 * @param array $file_data Files to upload
249 * @return array Prepared files.
250 */
251 public function prepare_files( $file_data ) {
252 $files_to_upload = array();
253
254 if ( is_array( $file_data['name'] ) ) {
255 foreach ( $file_data['name'] as $file_data_key => $file_data_value ) {
256
257 if ( $file_data['name'][ $file_data_key ] ) {
258 $files_to_upload[] = array(
259 'name' => $file_data['name'][ $file_data_key ],
260 'type' => $file_data['type'][ $file_data_key ],
261 'tmp_name' => $file_data['tmp_name'][ $file_data_key ],
262 'error' => $file_data['error'][ $file_data_key ],
263 'size' => $file_data['size'][ $file_data_key ]
264 );
265 }
266 }
267 } else {
268 $files_to_upload[] = $file_data;
269 }
270
271 return $files_to_upload;
272 }
273
274 /**
275 * Validates the file uploads.
276 *
277 * @since 1.0.0
278 * @package userswp
279 * @param array $files $_FILES array
280 * @param string $type Form type.
281 * @param bool $url_only Return only the url or whole file info?
282 * @param array|bool $fields Form fields.
283 * @return array Validated data.
284 */
285 public function validate_uploads($files, $type, $url_only = true, $fields = false) {
286
287 $validated_data = array();
288
289 if (empty($files)) {
290 return $validated_data;
291 }
292
293 if (!$fields) {
294 global $wpdb;
295 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
296
297 if ($type == 'register') {
298 $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')));
299 } elseif ($type == 'account') {
300 $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')));
301 } else {
302 $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)));
303 }
304 }
305
306 if ( ! empty( $fields ) ) {
307 foreach ( $fields as $field ) {
308 if ( isset( $files[ $field->htmlvar_name ] ) && ! empty( $files[ $field->htmlvar_name ]['name'] ) ) {
309 $file_urls = $this->handle_file_upload( $field, $files );
310
311 if ( is_wp_error( $file_urls ) ) {
312 return $file_urls;
313 }
314
315 if ( $url_only ) {
316 $validated_data[$field->htmlvar_name] = $file_urls['url'];
317 } else {
318 $validated_data[$field->htmlvar_name] = $file_urls;
319 }
320 }
321 }
322 }
323
324 return $validated_data;
325 }
326
327 /**
328 * Displays the preview for images and links for other types above the field for existing uploads.
329 *
330 * @since 1.0.0
331 * @package userswp
332 * @param object $field Form field info.
333 * @param string $value Value of the field.
334 * @param bool $removable Is this value removable by user?
335 * @return string HTML output.
336 */
337 public function file_upload_preview($field, $value, $removable = true) {
338 $output = '';
339
340 $value = esc_html($value);
341
342 if ($field->htmlvar_name == "banner") {
343 $htmlvar = "banner_thumb";
344 } elseif ($field->htmlvar_name == "avatar") {
345 $htmlvar = "avatar_thumb";
346 } else {
347 $htmlvar = $field->htmlvar_name;
348 }
349
350 // If is current user's profile (profile.php)
351 if ( is_admin() && defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
352 $user_id = get_current_user_id();
353 // If is another user's profile page
354 } elseif (is_admin() && ! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) {
355 $user_id = absint( $_GET['user_id'] );
356 // Otherwise something is wrong.
357 } else {
358 $user_id = get_current_user_id();
359 }
360
361 if ($value) {
362
363 $upload_dir = wp_upload_dir();
364 if (substr( $value, 0, 4 ) !== "http") {
365 $value = $upload_dir['baseurl'].$value;
366 }
367
368 $file = basename( $value );
369 $filetype = wp_check_filetype($file);
370 $image_types = array('png', 'jpg', 'jpeg', 'gif');
371 if (in_array($filetype['ext'], $image_types)) {
372 $output .= '<div class="uwp_file_preview_wrap">';
373 $output .= '<a href="'.$value.'" class="uwp_upload_file_preview"><img style="max-width:100px;" src="'.$value.'" /></a>';
374 if ($removable) {
375 $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>';
376 }
377 $output .= '</div>';
378 ?>
379 <?php
380 } else {
381 $output .= '<div class="uwp_file_preview_wrap">';
382 $output .= '<a href="'.$value.'" class="uwp_upload_file_preview">'.$file.'</a>';
383 if ($removable) {
384 $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>';
385 }
386 $output .= '</div>';
387 ?>
388 <?php
389 }
390 }
391 return $output;
392 }
393
394 /**
395 * restrict files to certain types
396 *
397 * @since 1.0.0
398 * @package userswp
399 * @param array $file File info.
400 * @return array Modified file info.
401 */
402 public function wp_media_restrict_file_types($file) {
403 // This bit is for the flash uploader
404 if ($file['type']=='application/octet-stream' && isset($file['tmp_name'])) {
405 $file_size = getimagesize($file['tmp_name']);
406 if (isset($file_size['error']) && $file_size['error']!=0) {
407 $file['error'] = "Unexpected Error: {$file_size['error']}";
408 return $file;
409 } else {
410 $file['type'] = $file_size['mime'];
411 }
412 }
413 list($category,$type) = explode('/',$file['type']);
414 if ('image'!=$category || !in_array($type,array('jpg','jpeg','gif','png'))) {
415 $file['error'] = "Sorry, you can only upload a .GIF, a .JPG, or a .PNG image file.";
416 } else if ($post_id = (isset($_REQUEST['post_id']) ? absint($_REQUEST['post_id']) : false)) {
417 if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>0)
418 $file['error'] = "Sorry, you cannot upload more than one (1) image.";
419 }
420 return $file;
421 }
422
423 /**
424 * Check whether the uploads is from the profile page.
425 *
426 * @since 1.0.0
427 * @package userswp
428 * @return bool
429 */
430 public function doing_upload(){
431 return isset($_POST['uwp_profile_upload']) ? true : false;
432 }
433
434 /**
435 * Gets the maximum file upload size.
436 *
437 * @since 1.0.0
438 * @package userswp
439 * @param string|bool $form_type Form type.
440 * @param string|bool $field_htmlvar_name htmlvar_name key.
441 * @return int Allowed upload size.
442 */
443 public function uwp_get_max_upload_size($form_type = false, $field_htmlvar_name = false) {
444 if (is_multisite()) {
445 $network_setting_size = esc_attr( get_option( 'fileupload_maxk', 300 ) );
446 $max_upload_size = $this->uwp_get_size_in_bytes($network_setting_size.'k');
447 if ($max_upload_size > wp_max_upload_size()) {
448 $max_upload_size = wp_max_upload_size();
449 }
450 } else {
451 $max_upload_size = wp_max_upload_size();
452 }
453 $max_upload_size = apply_filters('uwp_get_max_upload_size', $max_upload_size, $form_type, $field_htmlvar_name);
454
455 return $max_upload_size;
456 }
457
458
459 /**
460 * Modifies the maximum file upload size based on the setting.
461 *
462 * @since 1.0.0
463 * @package userswp
464 *
465 * @param int $bytes Size in bytes.
466 * @param string $type File upload type.
467 *
468 * @return int Size in bytes.
469 */
470 public function uwp_modify_get_max_upload_size($bytes, $type) {
471
472 if ($type == 'avatar') {
473 $kb = uwp_get_option('profile_avatar_size', false);
474 if ($kb) {
475 $bytes = intval($kb) * 1024;
476 }
477 }
478
479 if ($type == 'banner') {
480 $kb = uwp_get_option('profile_banner_size', false);
481 if ($kb) {
482 $bytes = intval($kb) * 1024;
483 }
484 }
485
486 return $bytes;
487
488 }
489
490 /**
491 * Gets the file type using the extension.
492 * Ex: 'jpg' => 'image/jpeg'
493 *
494 * @since 1.0.0
495 * @package userswp
496 *
497 * @param string $ext Extension string. Ex: png, jpg
498 *
499 * @return string File type.
500 */
501 public function get_file_type($ext) {
502 $allowed_file_types = $this->allowed_mime_types();
503 $file_types = array();
504 foreach ( $allowed_file_types as $format => $types ) {
505 $file_types = array_merge($file_types, $types);
506 }
507 $file_types = array_flip($file_types);
508 return $file_types[$ext];
509 }
510
511 /**
512 * Returns allowed mime types in uploads
513 *
514 * @since 1.0.0
515 * @package userswp
516 *
517 * @return array Allowed mime types.
518 */
519 public function allowed_mime_types() {
520 return apply_filters( 'uwp_allowed_mime_types', array(
521 'Image' => array( // Image formats.
522 'jpg' => 'image/jpeg',
523 'jpe' => 'image/jpeg',
524 'jpeg' => 'image/jpeg',
525 'gif' => 'image/gif',
526 'png' => 'image/png',
527 'bmp' => 'image/bmp',
528 'ico' => 'image/x-icon',
529 ),
530 'Video' => array( // Video formats.
531 'asf' => 'video/x-ms-asf',
532 'avi' => 'video/avi',
533 'flv' => 'video/x-flv',
534 'mkv' => 'video/x-matroska',
535 'mp4' => 'video/mp4',
536 'mpeg' => 'video/mpeg',
537 'mpg' => 'video/mpeg',
538 'wmv' => 'video/x-ms-wmv',
539 '3gp' => 'video/3gpp',
540 ),
541 'Audio' => array( // Audio formats.
542 'ogg' => 'audio/ogg',
543 'mp3' => 'audio/mpeg',
544 'wav' => 'audio/wav',
545 'wma' => 'audio/x-ms-wma',
546 ),
547 'Text' => array( // Text formats.
548 'css' => 'text/css',
549 'csv' => 'text/csv',
550 'htm' => 'text/html',
551 'html' => 'text/html',
552 'txt' => 'text/plain',
553 'rtx' => 'text/richtext',
554 'vtt' => 'text/vtt',
555 ),
556 'Application' => array( // Application formats.
557 'doc' => 'application/msword',
558 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
559 'exe' => 'application/x-msdownload',
560 'js' => 'application/javascript',
561 'odt' => 'application/vnd.oasis.opendocument.text',
562 'pdf' => 'application/pdf',
563 'pot' => 'application/vnd.ms-powerpoint',
564 'ppt' => 'application/vnd.ms-powerpoint',
565 'pptx' => 'application/vnd.ms-powerpoint',
566 'psd' => 'application/octet-stream',
567 'rar' => 'application/rar',
568 'rtf' => 'application/rtf',
569 'swf' => 'application/x-shockwave-flash',
570 'tar' => 'application/x-tar',
571 'xls' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
572 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
573 'zip' => 'application/zip',
574 )
575 )
576 );
577 }
578
579 /**
580 * Initiate the WordPress file system and provide fallback if needed.
581 *
582 * @since 1.2.2
583 * @package userswp
584 * @return bool|string Returns the file system class on success. False on failure.
585 */
586 public static function uwp_init_filesystem() {
587
588 if ( ! function_exists( 'get_filesystem_method' ) ) {
589 require_once( ABSPATH . "/wp-admin/includes/file.php" );
590 }
591 $access_type = get_filesystem_method();
592 if ( $access_type === 'direct' ) {
593 /* you can safely run request_filesystem_credentials() without any issues and don't need to worry about passing in a URL */
594 $creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() );
595
596 /* initialize the API */
597 if ( ! WP_Filesystem( $creds ) ) {
598 /* any problems and we exit */
599 return false;
600 }
601
602 global $wp_filesystem;
603
604 return $wp_filesystem;
605 /* do our file manipulations below */
606 } elseif ( defined( 'FTP_USER' ) ) {
607 $creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() );
608
609 /* initialize the API */
610 if ( ! WP_Filesystem( $creds ) ) {
611 /* any problems and we exit */
612 return false;
613 }
614
615 global $wp_filesystem;
616
617 return $wp_filesystem;
618
619 } else {
620 return false;
621 }
622
623 }
624
625 }