function process_registration_upload($array, $upload_status, $params = array()) { $username = isset($params['username']) ? $params['username'] : ''; /* File upload conditions */ $this->allowed_extensions = array("image/gif", "image/jpeg", "image/png"); $this->allowed_exts = array('gif', 'png', 'jpeg', 'jpg'); $this->allowed_non_image_extensions = apply_filters('upme_non_image_extensions', array()); $this->allowed_non_image_exts = apply_filters('upme_non_image_exts', array()); $settings = get_option('upme_options'); // Set default to 500KB $this->max_size = 512000; $this->image_height = 0; $this->image_width = 0; // Setting Max File Size set from admin if (isset($settings['avatar_max_size']) && $settings['avatar_max_size'] > 0) { $this->max_size = $settings['avatar_max_size'] * 1024 * 1024; } if (isset($_FILES)) { foreach ($_FILES as $key => $array) { extract($array); if ($name) { $clean_file = true; if (in_array($type, $this->allowed_extensions)) { // Security Check Start // Checking for Image size. If this is a valid image (not tempered) then this function will return width and height and other values in return. $image_data = @getimagesize($tmp_name); if (!isset($image_data[0]) || !isset($image_data[1])) { $clean_file = false; } else { $this->image_height = $image_data[1]; $this->image_width = $image_data[0]; } // Security Check End } $clean_key = $key; /* UPME action for adding restrictions before uploading files */ $before_upload_profile_files_params = array(); do_action('upme_register_before_upload_profile_files', $username, $clean_key, $before_upload_profile_files_params); /* END action */ $field_label = $this->upme_fileds_meta_value_array[$clean_key]; // $upload_status 1 - Validation and $upload_status 2 - Uploading if ($upload_status == '1') { if (!in_array($type, $this->allowed_extensions) && !in_array($type, $this->allowed_non_image_extensions)) { $this->errors[$clean_key] = sprintf(__('The file you have selected for %s has a file extension that is not allowed. Please choose a different file.', 'upme'), $field_label) . '<br/>'; } elseif ($size > $this->max_size) { $this->errors[$clean_key] = sprintf(__('The file you have selected for %s exceeds the maximum allowed file size.', 'upme'), $field_label) . '<br/>'; } elseif ($clean_file == false) { $this->errors[$clean_key] = sprintf(__('The file you selected for %s appears to be corrupt or not a real image file.', 'upme'), $field_label) . '<br/>'; } elseif (!preg_match("/.(" . implode("|", $this->allowed_exts) . ")\$/i", $name) && !preg_match("/.(" . implode("|", $this->allowed_non_image_exts) . ")\$/i", $name)) { $this->errors[$clean_key] = sprintf(__('The file you have selected for %s has a file extension that is not allowed. Please choose a different file.', 'upme'), $field_label) . '<br/>'; } else { $upload_file_custom_validation_params = array('username' => $username, 'key' => $key, 'height' => $this->image_height, 'width' => $this->image_width, 'field_label' => $field_label); $custom_errors = apply_filters('upme_registration_upload_file_custom_validation', array('status' => false, 'msg' => ''), $upload_file_custom_validation_params); if ($custom_errors['status']) { $this->errors[$clean_key] = $custom_errors['msg']; } } } else { if ($upload_status == '2') { /* Upload image */ // Checking for valid uploads folder if ($upload_dir = upme_get_uploads_folder_details()) { $target_path = $upload_dir['basedir'] . "/upme/"; // Checking for upload directory, if not exists then new created. if (!is_dir($target_path)) { mkdir($target_path, 0777); } $base_name = sanitize_file_name(basename($name)); $target_path = $target_path . time() . '_' . $base_name; $nice_url = $upload_dir['baseurl'] . "/upme/"; $nice_url = $nice_url . time() . '_' . $base_name; move_uploaded_file($tmp_name, $target_path); /* Clean the previous file allocated for the current upload field */ $current_field_url = get_user_meta($this->userid, $clean_key, true); if ('' != $current_field_url) { upme_delete_uploads_folder_files($current_field_url); } /* Now we have the nice url */ /* Store in usermeta */ update_user_meta($this->userid, $clean_key, $nice_url); } } } /* UPME action for removing restrictions after uploading files */ $after_upload_profile_files_params = array(); do_action('upme_registration_after_upload_profile_files', $username, $clean_key, $after_upload_profile_files_params); /* END action */ } } } }
function pic($id, $size) { // Check the existance of image path in upload folder and remove the data // in case its not available $user_pic = get_the_author_meta('user_pic', $id); if ($upload_dir = upme_get_uploads_folder_details()) { $upme_upload_path = $upload_dir['basedir'] . "/upme/"; $upme_upload_url = $upload_dir['baseurl'] . "/upme/"; $user_pic_path = str_replace($upme_upload_url, $upme_upload_path, $user_pic); if (!file_exists($user_pic_path)) { delete_user_meta($id, 'user_pic'); $user_pic = ''; } } if ($user_pic != '') { return '<img id="upme-avatar-user_pic" src="' . $user_pic . '" class="avatar avatar-50" />'; } else { return get_avatar($id, $size); } }
function upme_initialize_upload_box() { global $current_user, $upme_save; $id = $_GET['upme_id']; $meta = isset($_GET['upme_meta']) ? $_GET['upme_meta'] : ''; $disabled = isset($_GET['upme_disabled']) ? $_GET['upme_disabled'] : ''; $settings = get_option('upme_options'); $display = '<html> <head> ' . upme_crop_iframe_head() . ' <style type="text/css"> html{ overflow: hidden; } </style> </head> <body> <form id="upme-crop-frm" action="" method="post" enctype="multipart/form-data">'; $display .= ' <div class="upme-crop-wrap">'; $display .= ' <div class="upme-wrap">'; $display .= ' <div class="upme-field upme-separator upme-edit upme-clearfix" style="display: block;">' . __('Update Profile Picture', 'upme') . '</div>'; $profile_pic_url = get_the_author_meta($meta, $id); if (is_array($upme_save->errors) && count($upme_save->errors) != 0) { if (($id == $current_user->ID || current_user_can('edit_users')) && is_numeric($id)) { $display .= upme_display_upload_box($id, $meta, $disabled, $profile_pic_url, 'block'); $display .= upme_display_crop_box($id, $meta, $profile_pic_url, 'none'); } } elseif (isset($_POST['upme-upload-submit-' . $id]) || isset($_POST['upme-crop-request-' . $id])) { // Display crop area on file upload or crop link click if (($id == $current_user->ID || current_user_can('edit_users')) && is_numeric($id)) { $display .= upme_display_crop_box($id, $meta, $profile_pic_url, 'block'); } } elseif (isset($_POST['upme-crop-submit-' . $id])) { // Crop the image on area selection and submit $data_x1 = isset($_POST['upme-crop-x1']) ? $_POST['upme-crop-x1'] : 0; $data_y1 = isset($_POST['upme-crop-y1']) ? $_POST['upme-crop-y1'] : 0; $data_width = isset($_POST['upme-crop-width']) ? $_POST['upme-crop-width'] : 50; $data_height = isset($_POST['upme-crop-height']) ? $_POST['upme-crop-height'] : 50; $src = get_the_author_meta($meta, $id); $upme_upload_path = ''; $upme_upload_url = ''; if ($upload_dir = upme_get_uploads_folder_details()) { $upme_upload_path = $upload_dir['basedir'] . "/upme/"; $upme_upload_url = $upload_dir['baseurl'] . "/upme/"; $src = str_replace($upme_upload_url, $upme_upload_path, $src); } if (is_readable($src)) { $result = wp_crop_image($src, $data_x1, $data_y1, $data_width, $data_height, $data_width, $data_height); if (!is_wp_error($result)) { $cropped_path = str_replace($upme_upload_path, $upme_upload_url, $result); update_user_meta($id, $meta, $cropped_path); $display .= upme_display_upload_box($id, $meta, $disabled, $profile_pic_url, 'block'); } } update_crop_image_display($id, $meta, $cropped_path); } elseif (isset($_POST['upme-crop-save-' . $id])) { $src = get_the_author_meta($meta, $id); update_crop_image_display($id, $meta, $src); } else { if (($id == $current_user->ID || current_user_can('edit_users')) && is_numeric($id)) { $display .= upme_display_upload_box($id, $meta, $disabled, $profile_pic_url, 'block'); $display .= upme_display_crop_box($id, $meta, $profile_pic_url, 'none'); } } $display .= ' </div>'; $display .= ' </div>'; $display .= ' </form> </body> </html>'; echo $display; exit; }
function upme_delete_uploads_folder_files($image_url) { if ($upload_dir = upme_get_uploads_folder_details()) { $image_folder_link = $upload_dir['baseurl'] . "/upme/"; $image_name = str_replace($image_folder_link, '', $image_url); $upme_upload_path = $upload_dir['basedir'] . "/upme/"; if (unlink($upme_upload_path . $image_name)) { return true; } } return false; }