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

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