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

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