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

562 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 $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 uwp_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 uwp_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 uwp_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
286 if (!empty($fields)) {
287 foreach ($fields as $field) {
288 if(isset($files[$field->htmlvar_name])) {
289
290 $file_urls = $this->handle_file_upload($field, $files);
291
292 if (is_wp_error($file_urls)) {
293 return $file_urls;
294 }
295
296 if ($url_only) {
297 $validated_data[$field->htmlvar_name] = $file_urls['url'];
298 } else {
299 $validated_data[$field->htmlvar_name] = $file_urls;
300 }
301 }
302
303 }
304 }
305
306 return $validated_data;
307 }
308
309 /**
310 * Displays the preview for images and links for other types above the field for existing uploads.
311 *
312 * @since 1.0.0
313 * @package userswp
314 * @param object $field Form field info.
315 * @param string $value Value of the field.
316 * @param bool $removable Is this value removable by user?
317 * @return string HTML output.
318 */
319 public function uwp_file_upload_preview($field, $value, $removable = true) {
320 $output = '';
321
322 $value = esc_html($value);
323
324 if ($field->htmlvar_name == "uwp_banner_file") {
325 $htmlvar = "uwp_account_banner_thumb";
326 } elseif ($field->htmlvar_name == "uwp_avatar_file") {
327 $htmlvar = "uwp_account_avatar_thumb";
328 } else {
329 $htmlvar = $field->htmlvar_name;
330 }
331
332 // If is current user's profile (profile.php)
333 if ( is_admin() && defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
334 $user_id = get_current_user_id();
335 // If is another user's profile page
336 } elseif (is_admin() && ! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) {
337 $user_id = $_GET['user_id'];
338 $user_id = (int) sanitize_text_field($user_id);
339 // Otherwise something is wrong.
340 } else {
341 $user_id = get_current_user_id();
342 }
343
344 if ($value) {
345
346 $upload_dir = wp_upload_dir();
347 if (substr( $value, 0, 4 ) !== "http") {
348 $value = $upload_dir['baseurl'].$value;
349 }
350
351 $file = basename( $value );
352 $filetype = wp_check_filetype($file);
353 $image_types = array('png', 'jpg', 'jpeg', 'gif');
354 if (in_array($filetype['ext'], $image_types)) {
355 $output .= '<div class="uwp_file_preview_wrap">';
356 $output .= '<a href="'.$value.'" class="uwp_upload_file_preview"><img style="max-width:100px;" src="'.$value.'" /></a>';
357 if ($removable) {
358 $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>';
359 }
360 $output .= '</div>';
361 ?>
362 <?php
363 } else {
364 $output .= '<div class="uwp_file_preview_wrap">';
365 $output .= '<a href="'.$value.'" class="uwp_upload_file_preview">'.$file.'</a>';
366 if ($removable) {
367 $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>';
368 }
369 $output .= '</div>';
370 ?>
371 <?php
372 }
373 }
374 return $output;
375 }
376
377 /**
378 * restrict files to certain types
379 *
380 * @since 1.0.0
381 * @package userswp
382 * @param array $file File info.
383 * @return array Modified file info.
384 */
385 public function uwp_wp_media_restrict_file_types($file) {
386 // This bit is for the flash uploader
387 if ($file['type']=='application/octet-stream' && isset($file['tmp_name'])) {
388 $file_size = getimagesize($file['tmp_name']);
389 if (isset($file_size['error']) && $file_size['error']!=0) {
390 $file['error'] = "Unexpected Error: {$file_size['error']}";
391 return $file;
392 } else {
393 $file['type'] = $file_size['mime'];
394 }
395 }
396 list($category,$type) = explode('/',$file['type']);
397 if ('image'!=$category || !in_array($type,array('jpg','jpeg','gif','png'))) {
398 $file['error'] = "Sorry, you can only upload a .GIF, a .JPG, or a .PNG image file.";
399 } else if ($post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : false)) {
400 if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>0)
401 $file['error'] = "Sorry, you cannot upload more than one (1) image.";
402 }
403 return $file;
404 }
405
406 /**
407 * Check whether the uploads is from the profile page.
408 *
409 * @since 1.0.0
410 * @package userswp
411 * @return bool
412 */
413 public function uwp_doing_upload(){
414 return isset($_POST['uwp_profile_upload']) ? true : false;
415 }
416
417 /**
418 * Gets the maximum file upload size.
419 *
420 * @since 1.0.0
421 * @package userswp
422 * @param string|bool $form_type Form type.
423 * @param string|bool $field_htmlvar_name htmlvar_name key.
424 * @return int Allowed upload size.
425 */
426 public function uwp_get_max_upload_size($form_type = false, $field_htmlvar_name = false) {
427 if (is_multisite()) {
428 $network_setting_size = esc_attr( get_option( 'fileupload_maxk', 300 ) );
429 $max_upload_size = $this->uwp_get_size_in_bytes($network_setting_size.'k');
430 if ($max_upload_size > wp_max_upload_size()) {
431 $max_upload_size = wp_max_upload_size();
432 }
433 } else {
434 $max_upload_size = wp_max_upload_size();
435 }
436 $max_upload_size = apply_filters('uwp_get_max_upload_size', $max_upload_size, $form_type, $field_htmlvar_name);
437
438 return $max_upload_size;
439 }
440
441
442 /**
443 * Modifies the maximum file upload size based on the setting.
444 *
445 * @since 1.0.0
446 * @package userswp
447 *
448 * @param int $bytes Size in bytes.
449 * @param string $type File upload type.
450 *
451 * @return int Size in bytes.
452 */
453 public function uwp_modify_get_max_upload_size($bytes, $type) {
454
455 if ($type == 'avatar') {
456 $kb = uwp_get_option('profile_avatar_size', false);
457 if ($kb) {
458 $bytes = intval($kb) * 1024;
459 }
460 }
461
462 if ($type == 'banner') {
463 $kb = uwp_get_option('profile_banner_size', false);
464 if ($kb) {
465 $bytes = intval($kb) * 1024;
466 }
467 }
468
469 return $bytes;
470
471 }
472
473 /**
474 * Gets the file type using the extension.
475 * Ex: 'jpg' => 'image/jpeg'
476 *
477 * @since 1.0.0
478 * @package userswp
479 *
480 * @param string $ext Extension string. Ex: png, jpg
481 *
482 * @return string File type.
483 */
484 public function uwp_get_file_type($ext) {
485 $allowed_file_types = $this->allowed_mime_types();
486 $file_types = array();
487 foreach ( $allowed_file_types as $format => $types ) {
488 $file_types = array_merge($file_types, $types);
489 }
490 $file_types = array_flip($file_types);
491 return $file_types[$ext];
492 }
493
494 /**
495 * Returns allowed mime types in uploads
496 *
497 * @since 1.0.0
498 * @package userswp
499 *
500 * @return array Allowed mime types.
501 */
502 public function allowed_mime_types() {
503 return apply_filters( 'uwp_allowed_mime_types', array(
504 'Image' => array( // Image formats.
505 'jpg' => 'image/jpeg',
506 'jpe' => 'image/jpeg',
507 'jpeg' => 'image/jpeg',
508 'gif' => 'image/gif',
509 'png' => 'image/png',
510 'bmp' => 'image/bmp',
511 'ico' => 'image/x-icon',
512 ),
513 'Video' => array( // Video formats.
514 'asf' => 'video/x-ms-asf',
515 'avi' => 'video/avi',
516 'flv' => 'video/x-flv',
517 'mkv' => 'video/x-matroska',
518 'mp4' => 'video/mp4',
519 'mpeg' => 'video/mpeg',
520 'mpg' => 'video/mpeg',
521 'wmv' => 'video/x-ms-wmv',
522 '3gp' => 'video/3gpp',
523 ),
524 'Audio' => array( // Audio formats.
525 'ogg' => 'audio/ogg',
526 'mp3' => 'audio/mpeg',
527 'wav' => 'audio/wav',
528 'wma' => 'audio/x-ms-wma',
529 ),
530 'Text' => array( // Text formats.
531 'css' => 'text/css',
532 'csv' => 'text/csv',
533 'htm' => 'text/html',
534 'html' => 'text/html',
535 'txt' => 'text/plain',
536 'rtx' => 'text/richtext',
537 'vtt' => 'text/vtt',
538 ),
539 'Application' => array( // Application formats.
540 'doc' => 'application/msword',
541 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
542 'exe' => 'application/x-msdownload',
543 'js' => 'application/javascript',
544 'odt' => 'application/vnd.oasis.opendocument.text',
545 'pdf' => 'application/pdf',
546 'pot' => 'application/vnd.ms-powerpoint',
547 'ppt' => 'application/vnd.ms-powerpoint',
548 'pptx' => 'application/vnd.ms-powerpoint',
549 'psd' => 'application/octet-stream',
550 'rar' => 'application/rar',
551 'rtf' => 'application/rtf',
552 'swf' => 'application/x-shockwave-flash',
553 'tar' => 'application/x-tar',
554 'xls' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
555 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
556 'zip' => 'application/zip',
557 )
558 )
559 );
560 }
561
562 }