/**
 * Handle the display of a group's Change Avatar page.
 */
function groups_screen_group_admin_avatar()
{
    if ('group-avatar' != bp_get_group_current_admin_tab()) {
        return false;
    }
    // If the logged-in user doesn't have permission or if avatar uploads are disabled, then stop here.
    if (!bp_is_item_admin() || bp_disable_group_avatar_uploads() || !buddypress()->avatar->show_avatars) {
        return false;
    }
    $bp = buddypress();
    // If the group admin has deleted the admin avatar.
    if (bp_is_action_variable('delete', 1)) {
        // Check the nonce.
        check_admin_referer('bp_group_avatar_delete');
        if (bp_core_delete_existing_avatar(array('item_id' => $bp->groups->current_group->id, 'object' => 'group'))) {
            bp_core_add_message(__('The group profile photo was deleted successfully!', 'buddypress'));
        } else {
            bp_core_add_message(__('There was a problem deleting the group profile photo. Please try again.', 'buddypress'), 'error');
        }
    }
    if (!isset($bp->avatar_admin)) {
        $bp->avatar_admin = new stdClass();
    }
    $bp->avatar_admin->step = 'upload-image';
    if (!empty($_FILES)) {
        // Check the nonce.
        check_admin_referer('bp_avatar_upload');
        // Pass the file to the avatar upload handler.
        if (bp_core_avatar_handle_upload($_FILES, 'groups_avatar_upload_dir')) {
            $bp->avatar_admin->step = 'crop-image';
            // Make sure we include the jQuery jCrop file for image cropping.
            add_action('wp_print_scripts', 'bp_core_add_jquery_cropper');
        }
    }
    // If the image cropping is done, crop the image and save a full/thumb version.
    if (isset($_POST['avatar-crop-submit'])) {
        // Check the nonce.
        check_admin_referer('bp_avatar_cropstore');
        $args = array('object' => 'group', 'avatar_dir' => 'group-avatars', 'item_id' => $bp->groups->current_group->id, 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h']);
        if (!bp_core_avatar_handle_crop($args)) {
            bp_core_add_message(__('There was a problem cropping the group profile photo.', 'buddypress'), 'error');
        } else {
            bp_core_add_message(__('The new group profile photo was uploaded successfully.', 'buddypress'));
        }
    }
    /**
     * Fires before the loading of the group Change Avatar page template.
     *
     * @since 1.0.0
     *
     * @param int $id ID of the group that is being displayed.
     */
    do_action('groups_screen_group_admin_avatar', $bp->groups->current_group->id);
    /**
     * Filters the template to load for a group's Change Avatar page.
     *
     * @since 1.0.0
     *
     * @param string $value Path to a group's Change Avatar template.
     */
    bp_core_load_template(apply_filters('groups_template_group_admin_avatar', 'groups/single/home'));
}
/**
 * Use the absolute path to an image to set an attachment type for a given item.
 *
 * @since 2.4.0
 *
 * @param  string $type        The attachment type to create (avatar or cover_image). Default: avatar.
 * @param  array  $args {
 *     @type int    $item_id   The ID of the object (Required). Default: 0.
 *     @type string $object    The object type (eg: group, user, blog) (Required). Default: 'user'.
 *     @type string $component The component for the object (eg: groups, xprofile, blogs). Default: ''.
 *     @type string $image     The absolute path to the image (Required). Default: ''.
 *     @type int    $crop_w    Crop width. Default: 0.
 *     @type int    $crop_h    Crop height. Default: 0.
 *     @type int    $crop_x    The horizontal starting point of the crop. Default: 0.
 *     @type int    $crop_y    The vertical starting point of the crop. Default: 0.
 * }
 * @return bool  True on success, false otherwise.
 */
function bp_attachments_create_item_type($type = 'avatar', $args = array())
{
    if (empty($type) || $type !== 'avatar' && $type !== 'cover_image') {
        return false;
    }
    $r = bp_parse_args($args, array('item_id' => 0, 'object' => 'user', 'component' => '', 'image' => '', 'crop_w' => 0, 'crop_h' => 0, 'crop_x' => 0, 'crop_y' => 0), 'create_item_' . $type);
    if (empty($r['item_id']) || empty($r['object']) || !file_exists($r['image']) || !@getimagesize($r['image'])) {
        return false;
    }
    // Make sure the file path is safe
    if (0 !== validate_file($r['image'])) {
        return false;
    }
    // Set the component if not already done
    if (empty($r['component'])) {
        if ('user' === $r['object']) {
            $r['component'] = 'xprofile';
        } else {
            $r['component'] = $r['object'] . 's';
        }
    }
    // Get allowed mimes for the Attachment type and check the image one is.
    $allowed_mimes = bp_attachments_get_allowed_mimes($type);
    $is_allowed = wp_check_filetype($r['image'], $allowed_mimes);
    // It's not an image.
    if (!$is_allowed['ext']) {
        return false;
    }
    // Init the Attachment data
    $attachment_data = array();
    if ('avatar' === $type) {
        // Set crop width for the avatar if not given
        if (empty($r['crop_w'])) {
            $r['crop_w'] = bp_core_avatar_full_width();
        }
        // Set crop height for the avatar if not given
        if (empty($r['crop_h'])) {
            $r['crop_h'] = bp_core_avatar_full_height();
        }
        if (is_callable($r['component'] . '_avatar_upload_dir')) {
            $dir_args = array($r['item_id']);
            // In case  of xprofile, we need an extra argument
            if ('xprofile' === $r['component']) {
                $dir_args = array(false, $r['item_id']);
            }
            $attachment_data = call_user_func_array($r['component'] . '_avatar_upload_dir', $dir_args);
        }
    } elseif ('cover_image' === $type) {
        $attachment_data = bp_attachments_uploads_dir_get();
        // The BP Attachments Uploads Dir is not set, stop.
        if (!$attachment_data) {
            return false;
        }
        // Default to members for xProfile
        $object_subdir = 'members';
        if ('xprofile' !== $r['component']) {
            $object_subdir = sanitize_key($r['component']);
        }
        // Set Subdir
        $attachment_data['subdir'] = $object_subdir . '/' . $r['item_id'] . '/cover-image';
        // Set Path
        $attachment_data['path'] = trailingslashit($attachment_data['basedir']) . $attachment_data['subdir'];
    }
    if (!isset($attachment_data['path']) || !isset($attachment_data['subdir'])) {
        return false;
    }
    // It's not a regular upload, we may need to create some folders
    if (!is_dir($attachment_data['path'])) {
        if (!wp_mkdir_p($attachment_data['path'])) {
            return false;
        }
    }
    // Set the image name and path
    $image_file_name = wp_unique_filename($attachment_data['path'], basename($r['image']));
    $image_file_path = $attachment_data['path'] . '/' . $image_file_name;
    // Copy the image file into the avatar dir
    if (!copy($r['image'], $image_file_path)) {
        return false;
    }
    // Init the response
    $created = false;
    // It's an avatar, we need to crop it.
    if ('avatar' === $type) {
        $created = bp_core_avatar_handle_crop(array('object' => $r['object'], 'avatar_dir' => trim(dirname($attachment_data['subdir']), '/'), 'item_id' => (int) $r['item_id'], 'original_file' => trailingslashit($attachment_data['subdir']) . $image_file_name, 'crop_w' => $r['crop_w'], 'crop_h' => $r['crop_h'], 'crop_x' => $r['crop_x'], 'crop_y' => $r['crop_y']));
        // It's a cover image we need to fit it to feature's dimensions
    } elseif ('cover_image' === $type) {
        $cover_image = bp_attachments_cover_image_generate_file(array('file' => $image_file_path, 'component' => $r['component'], 'cover_image_dir' => $attachment_data['path']));
        $created = !empty($cover_image['cover_file']);
    }
    // Remove copied file if it fails
    if (!$created) {
        @unlink($image_file_path);
    }
    // Return the response
    return $created;
}
 function save_course_settings()
 {
     $user_id = get_current_user_id();
     $course_id = $_POST['course_id'];
     $course_setting['vibe_course_auto_eval'] = $_POST['vibe_course_auto_eval'];
     $course_setting['vibe_duration'] = $_POST['vibe_duration'];
     $course_setting['vibe_pre_course'] = $_POST['vibe_pre_course'];
     $course_setting['vibe_course_drip'] = $_POST['vibe_course_drip'];
     $course_setting['vibe_course_drip_duration'] = $_POST['vibe_course_drip_duration'];
     $course_setting['vibe_course_certificate'] = $_POST['vibe_certificate'];
     $course_setting['vibe_course_passing_percentage'] = $_POST['vibe_course_passing_percentage'];
     $course_setting['vibe_certificate_template'] = $_POST['vibe_certificate_template'];
     $course_setting['vibe_badge'] = $_POST['vibe_badge'];
     $course_setting['vibe_course_badge_percentage'] = $_POST['vibe_course_badge_percentage'];
     $course_setting['vibe_course_badge_title'] = $_POST['vibe_course_badge_title'];
     $course_setting['vibe_course_badge'] = $_POST['vibe_course_badge'];
     $course_setting['vibe_max_students'] = $_POST['vibe_max_students'];
     $course_setting['vibe_start_date'] = $_POST['vibe_start_date'];
     $course_setting['vibe_course_retakes'] = $_POST['vibe_course_retakes'];
     $course_setting['vibe_group'] = $_POST['vibe_group'];
     $course_setting['vibe_forum'] = $_POST['vibe_forum'];
     $course_setting['vibe_course_instructions'] = $_POST['vibe_course_instructions'];
     $course_setting['vibe_course_message'] = $_POST['vibe_course_message'];
     $flag = 0;
     //Error Flag
     if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'create_course' . $user_id) || !current_user_can('edit_posts')) {
         _e('Security check Failed. Contact Administrator.', 'wplms-front-end');
         die;
     }
     if (!is_numeric($course_id) || get_post_type($course_id) != 'course') {
         _e('Invalid Course id, please edit a course', 'wplms-front-end');
         die;
     }
     $the_post = get_post($course_id);
     if ($the_post->post_author != $user_id && !current_user_can('manage_options')) {
         _e('Invalid Course Instructor', 'wplms-front-end');
         die;
     }
     if ($course_setting['vibe_badge'] == 'H') {
         $course_setting['vibe_course_badge'] = '';
     }
     foreach ($course_setting as $key => $value) {
         $prev_val = get_post_meta($course_id, $key, true);
         if ($prev_val != $value) {
             update_post_meta($course_id, $key, $value);
         }
     }
     if ($course_setting['vibe_group'] == 'add_new' && !$flag) {
         $the_course = get_post($course_id);
         $t = wp_get_attachment_image_src(get_post_thumbnail_id($course_id, 'thumbnail'));
         $f = wp_get_attachment_image_src(get_post_thumbnail_id($course_id, 'full'));
         $group_slug = $the_course->post_name;
         //groups_check_slug( sanitize_title( esc_attr( $the_course->post_name ) ) );
         $group_settings = array('creator_id' => $user_id, 'name' => $the_course->post_title, 'slug' => $group_slug, 'description' => $the_course->post_excerpt, 'status' => 'private', 'date_created' => current_time('mysql'));
         $group_settings = apply_filters('wplms_front_end_group_vars', $group_settings);
         if ($course_setting['vibe_forum'] == 'add_group_forum') {
             $group_settings['enable_forum'] = 1;
         }
         global $bp;
         $new_group_id = groups_create_group($group_settings);
         bp_core_avatar_handle_crop(array('object' => 'group', 'avatar_dir' => 'group-avatars', 'item_id' => $new_group_id, 'original_file' => $f[0], 'crop_x' => 0, 'crop_y' => 0, 'crop_w' => $f[1], 'crop_h' => $f[2]));
         groups_update_groupmeta($new_group_id, 'total_member_count', 1);
         groups_update_groupmeta($new_group_id, 'last_activity', gmdate("Y-m-d H:i:s"));
         update_post_meta($course_id, 'vibe_group', $new_group_id);
         if ($course_setting['vibe_forum'] == 'add_group_forum') {
             $forum_settings = array('post_title' => stripslashes($the_course->post_title), 'post_content' => stripslashes($the_course->post_excerpt), 'post_name' => $the_course->post_name, 'post_status' => 'private', 'post_type' => 'forum');
             $forum_settings = apply_filters('wplms_front_end_forum_vars', $forum_settings);
             $new_forum_id = wp_insert_post($forum_settings);
             //Linkage
             $linkage = vibe_get_option('linkage');
             if (isset($linkage) && $linkage) {
                 $course_linkage = wp_get_post_terms($course_id, 'linkage', array("fields" => "names"));
                 if (isset($course_linkage) && is_array($course_linkage)) {
                     wp_set_post_terms($new_forum_id, $course_linkage, 'linkage');
                 }
             }
             groups_update_groupmeta($new_group_id, 'forum_id', array($new_forum_id));
             update_post_meta($course_id, 'vibe_forum', $new_forum_id);
         }
     }
     if ($course_setting['vibe_forum'] == 'add_new' && !$flag) {
         $forum_settings = array('post_title' => stripslashes($the_post->post_title), 'post_content' => stripslashes($the_post->post_excerpt), 'post_name' => $the_post->post_name, 'post_status' => 'private', 'post_type' => 'forum');
         $forum_settings = apply_filters('wplms_front_end_forum_vars', $forum_settings);
         $new_forum_id = wp_insert_post($forum_settings);
         update_post_meta($course_id, 'vibe_forum', $new_forum_id);
     }
     if (isset($_POST['level']) && $_POST['level']) {
         $level = $_POST['level'];
         if (is_numeric($level)) {
             wp_set_post_terms($course_id, $level, 'level');
         }
     }
     if ($flag) {
         echo $message;
     } else {
         echo $course_id;
         do_action('wplms_course_settings_updated', $course_id);
     }
     die;
 }
/**
 * Handles the uploading and cropping of a user avatar. Displays the change avatar page.
 *
 * @package BuddyPress XProfile
 * @uses bp_is_my_profile() Checks to make sure the current user being viewed equals the logged in user
 * @uses bp_core_load_template() Looks for and loads a template file within the current member theme (folder/filename)
 */
function xprofile_screen_change_avatar()
{
    // Bail if not the correct screen
    if (!bp_is_my_profile() && !bp_current_user_can('bp_moderate')) {
        return false;
    }
    // Bail if there are action variables
    if (bp_action_variables()) {
        bp_do_404();
        return;
    }
    $bp = buddypress();
    if (!isset($bp->avatar_admin)) {
        $bp->avatar_admin = new stdClass();
    }
    $bp->avatar_admin->step = 'upload-image';
    if (!empty($_FILES)) {
        // Check the nonce
        check_admin_referer('bp_avatar_upload');
        // Pass the file to the avatar upload handler
        if (bp_core_avatar_handle_upload($_FILES, 'xprofile_avatar_upload_dir')) {
            $bp->avatar_admin->step = 'crop-image';
            // Make sure we include the jQuery jCrop file for image cropping
            add_action('wp_print_scripts', 'bp_core_add_jquery_cropper');
        }
    }
    // If the image cropping is done, crop the image and save a full/thumb version
    if (isset($_POST['avatar-crop-submit'])) {
        // Check the nonce
        check_admin_referer('bp_avatar_cropstore');
        $args = array('item_id' => bp_displayed_user_id(), 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h']);
        if (!bp_core_avatar_handle_crop($args)) {
            bp_core_add_message(__('There was a problem cropping your profile photo.', 'buddypress'), 'error');
        } else {
            /**
             * Fires right before the redirect, after processing a new avatar.
             *
             * @since 1.1.0
             * @since 2.3.4 Add two new parameters to inform about the user id and
             *              about the way the avatar was set (eg: 'crop' or 'camera')
             *
             * @param string $item_id Inform about the user id the avatar was set for
             * @param string $value Inform about the way the avatar was set ('crop')
             */
            do_action('xprofile_avatar_uploaded', (int) $args['item_id'], 'crop');
            bp_core_add_message(__('Your new profile photo was uploaded successfully.', 'buddypress'));
            bp_core_redirect(bp_displayed_user_domain());
        }
    }
    /**
     * Fires right before the loading of the XProfile change avatar screen template file.
     *
     * @since BuddyPress (1.0.0)
     */
    do_action('xprofile_screen_change_avatar');
    /**
     * Filters the template to load for the XProfile change avatar screen.
     *
     * @since BuddyPress (1.0.0)
     *
     * @param string $template Path to the XProfile change avatar template to load.
     */
    bp_core_load_template(apply_filters('xprofile_template_change_avatar', 'members/single/home'));
}
Example #5
0
/**
 * Catch and process group creation form submissions.
 */
function groups_action_create_group()
{
    global $bp;
    // If we're not at domain.org/groups/create/ then return false
    if (!bp_is_groups_component() || !bp_is_current_action('create')) {
        return false;
    }
    if (!is_user_logged_in()) {
        return false;
    }
    if (!bp_user_can_create_groups()) {
        bp_core_add_message(__('Sorry, you are not allowed to create groups.', 'buddypress'), 'error');
        bp_core_redirect(trailingslashit(bp_get_root_domain() . '/' . bp_get_groups_root_slug()));
    }
    // Make sure creation steps are in the right order
    groups_action_sort_creation_steps();
    // If no current step is set, reset everything so we can start a fresh group creation
    $bp->groups->current_create_step = bp_action_variable(1);
    if (!bp_get_groups_current_create_step()) {
        unset($bp->groups->current_create_step);
        unset($bp->groups->completed_create_steps);
        setcookie('bp_new_group_id', false, time() - 1000, COOKIEPATH);
        setcookie('bp_completed_create_steps', false, time() - 1000, COOKIEPATH);
        $reset_steps = true;
        $keys = array_keys($bp->groups->group_creation_steps);
        bp_core_redirect(bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/create/step/' . array_shift($keys) . '/');
    }
    // If this is a creation step that is not recognized, just redirect them back to the first screen
    if (bp_get_groups_current_create_step() && empty($bp->groups->group_creation_steps[bp_get_groups_current_create_step()])) {
        bp_core_add_message(__('There was an error saving group details. Please try again.', 'buddypress'), 'error');
        bp_core_redirect(bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/create/');
    }
    // Fetch the currently completed steps variable
    if (isset($_COOKIE['bp_completed_create_steps']) && !isset($reset_steps)) {
        $bp->groups->completed_create_steps = json_decode(base64_decode(stripslashes($_COOKIE['bp_completed_create_steps'])));
    }
    // Set the ID of the new group, if it has already been created in a previous step
    if (isset($_COOKIE['bp_new_group_id'])) {
        $bp->groups->new_group_id = (int) $_COOKIE['bp_new_group_id'];
        $bp->groups->current_group = groups_get_group(array('group_id' => $bp->groups->new_group_id));
        // Only allow the group creator to continue to edit the new group
        if (!bp_is_group_creator($bp->groups->current_group, bp_loggedin_user_id())) {
            bp_core_add_message(__('Only the group creator may continue editing this group.', 'buddypress'), 'error');
            bp_core_redirect(bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/create/');
        }
    }
    // If the save, upload or skip button is hit, lets calculate what we need to save
    if (isset($_POST['save'])) {
        // Check the nonce
        check_admin_referer('groups_create_save_' . bp_get_groups_current_create_step());
        if ('group-details' == bp_get_groups_current_create_step()) {
            if (empty($_POST['group-name']) || empty($_POST['group-desc']) || !strlen(trim($_POST['group-name'])) || !strlen(trim($_POST['group-desc']))) {
                bp_core_add_message(__('Please fill in all of the required fields', 'buddypress'), 'error');
                bp_core_redirect(bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/create/step/' . bp_get_groups_current_create_step() . '/');
            }
            $new_group_id = isset($bp->groups->new_group_id) ? $bp->groups->new_group_id : 0;
            if (!($bp->groups->new_group_id = groups_create_group(array('group_id' => $new_group_id, 'name' => $_POST['group-name'], 'description' => $_POST['group-desc'], 'slug' => groups_check_slug(sanitize_title(esc_attr($_POST['group-name']))), 'date_created' => bp_core_current_time(), 'status' => 'public')))) {
                bp_core_add_message(__('There was an error saving group details, please try again.', 'buddypress'), 'error');
                bp_core_redirect(bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/create/step/' . bp_get_groups_current_create_step() . '/');
            }
        }
        if ('group-settings' == bp_get_groups_current_create_step()) {
            $group_status = 'public';
            $group_enable_forum = 1;
            if (!isset($_POST['group-show-forum'])) {
                $group_enable_forum = 0;
            } else {
                // Create the forum if enable_forum = 1
                if (bp_is_active('forums') && !groups_get_groupmeta($bp->groups->new_group_id, 'forum_id')) {
                    groups_new_group_forum();
                }
            }
            if ('private' == $_POST['group-status']) {
                $group_status = 'private';
            } else {
                if ('hidden' == $_POST['group-status']) {
                    $group_status = 'hidden';
                }
            }
            if (!($bp->groups->new_group_id = groups_create_group(array('group_id' => $bp->groups->new_group_id, 'status' => $group_status, 'enable_forum' => $group_enable_forum)))) {
                bp_core_add_message(__('There was an error saving group details, please try again.', 'buddypress'), 'error');
                bp_core_redirect(bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/create/step/' . bp_get_groups_current_create_step() . '/');
            }
            // Set the invite status
            // Checked against a whitelist for security
            $allowed_invite_status = apply_filters('groups_allowed_invite_status', array('members', 'mods', 'admins'));
            $invite_status = !empty($_POST['group-invite-status']) && in_array($_POST['group-invite-status'], (array) $allowed_invite_status) ? $_POST['group-invite-status'] : 'members';
            groups_update_groupmeta($bp->groups->new_group_id, 'invite_status', $invite_status);
        }
        if ('group-invites' === bp_get_groups_current_create_step()) {
            if (!empty($_POST['friends'])) {
                foreach ((array) $_POST['friends'] as $friend) {
                    groups_invite_user(array('user_id' => $friend, 'group_id' => $bp->groups->new_group_id));
                }
            }
            groups_send_invites(bp_loggedin_user_id(), $bp->groups->new_group_id);
        }
        do_action('groups_create_group_step_save_' . bp_get_groups_current_create_step());
        do_action('groups_create_group_step_complete');
        // Mostly for clearing cache on a generic action name
        /**
         * Once we have successfully saved the details for this step of the creation process
         * we need to add the current step to the array of completed steps, then update the cookies
         * holding the information
         */
        $completed_create_steps = isset($bp->groups->completed_create_steps) ? $bp->groups->completed_create_steps : array();
        if (!in_array(bp_get_groups_current_create_step(), $completed_create_steps)) {
            $bp->groups->completed_create_steps[] = bp_get_groups_current_create_step();
        }
        // Reset cookie info
        setcookie('bp_new_group_id', $bp->groups->new_group_id, time() + 60 * 60 * 24, COOKIEPATH);
        setcookie('bp_completed_create_steps', base64_encode(json_encode($bp->groups->completed_create_steps)), time() + 60 * 60 * 24, COOKIEPATH);
        // If we have completed all steps and hit done on the final step we
        // can redirect to the completed group
        $keys = array_keys($bp->groups->group_creation_steps);
        if (count($bp->groups->completed_create_steps) == count($keys) && bp_get_groups_current_create_step() == array_pop($keys)) {
            unset($bp->groups->current_create_step);
            unset($bp->groups->completed_create_steps);
            // Once we compelete all steps, record the group creation in the activity stream.
            groups_record_activity(array('type' => 'created_group', 'item_id' => $bp->groups->new_group_id));
            do_action('groups_group_create_complete', $bp->groups->new_group_id);
            bp_core_redirect(bp_get_group_permalink($bp->groups->current_group));
        } else {
            /**
             * Since we don't know what the next step is going to be (any plugin can insert steps)
             * we need to loop the step array and fetch the next step that way.
             */
            foreach ($keys as $key) {
                if ($key == bp_get_groups_current_create_step()) {
                    $next = 1;
                    continue;
                }
                if (isset($next)) {
                    $next_step = $key;
                    break;
                }
            }
            bp_core_redirect(bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/create/step/' . $next_step . '/');
        }
    }
    // Remove invitations
    if ('group-invites' === bp_get_groups_current_create_step() && !empty($_REQUEST['user_id']) && is_numeric($_REQUEST['user_id'])) {
        if (!check_admin_referer('groups_invite_uninvite_user')) {
            return false;
        }
        $message = __('Invite successfully removed', 'buddypress');
        $error = false;
        if (!groups_uninvite_user((int) $_REQUEST['user_id'], $bp->groups->new_group_id)) {
            $message = __('There was an error removing the invite', 'buddypress');
            $error = 'error';
        }
        bp_core_add_message($message, $error);
        bp_core_redirect(bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/create/step/group-invites/');
    }
    // Group avatar is handled separately
    if ('group-avatar' == bp_get_groups_current_create_step() && isset($_POST['upload'])) {
        if (!isset($bp->avatar_admin)) {
            $bp->avatar_admin = new stdClass();
        }
        if (!empty($_FILES) && isset($_POST['upload'])) {
            // Normally we would check a nonce here, but the group save nonce is used instead
            // Pass the file to the avatar upload handler
            if (bp_core_avatar_handle_upload($_FILES, 'groups_avatar_upload_dir')) {
                $bp->avatar_admin->step = 'crop-image';
                // Make sure we include the jQuery jCrop file for image cropping
                add_action('wp_print_scripts', 'bp_core_add_jquery_cropper');
            }
        }
        // If the image cropping is done, crop the image and save a full/thumb version
        if (isset($_POST['avatar-crop-submit']) && isset($_POST['upload'])) {
            // Normally we would check a nonce here, but the group save nonce is used instead
            if (!bp_core_avatar_handle_crop(array('object' => 'group', 'avatar_dir' => 'group-avatars', 'item_id' => $bp->groups->current_group->id, 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h']))) {
                bp_core_add_message(__('There was an error saving the group profile photo, please try uploading again.', 'buddypress'), 'error');
            } else {
                bp_core_add_message(__('The group profile photo was uploaded successfully!', 'buddypress'));
            }
        }
    }
    bp_core_load_template(apply_filters('groups_template_create_group', 'groups/create'));
}
Example #6
0
/**
 * Handles the uploading and cropping of a user avatar. Displays the change avatar page.
 *
 * @package BuddyPress XProfile
 * @uses bp_is_my_profile() Checks to make sure the current user being viewed equals the logged in user
 * @uses bp_core_load_template() Looks for and loads a template file within the current member theme (folder/filename)
 */
function xprofile_screen_change_avatar()
{
    // Bail if not the correct screen
    if (!bp_is_my_profile() && !bp_current_user_can('bp_moderate')) {
        return false;
    }
    // Bail if there are action variables
    if (bp_action_variables()) {
        bp_do_404();
        return;
    }
    $bp = buddypress();
    if (!isset($bp->avatar_admin)) {
        $bp->avatar_admin = new stdClass();
    }
    $bp->avatar_admin->step = 'upload-image';
    if (!empty($_FILES)) {
        // Check the nonce
        check_admin_referer('bp_avatar_upload');
        // Pass the file to the avatar upload handler
        if (bp_core_avatar_handle_upload($_FILES, 'xprofile_avatar_upload_dir')) {
            $bp->avatar_admin->step = 'crop-image';
            // Make sure we include the jQuery jCrop file for image cropping
            add_action('wp_print_scripts', 'bp_core_add_jquery_cropper');
        }
    }
    // If the image cropping is done, crop the image and save a full/thumb version
    if (isset($_POST['avatar-crop-submit'])) {
        // Check the nonce
        check_admin_referer('bp_avatar_cropstore');
        $args = array('item_id' => bp_displayed_user_id(), 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h']);
        if (!bp_core_avatar_handle_crop($args)) {
            bp_core_add_message(__('There was a problem cropping your profile photo.', 'buddypress'), 'error');
        } else {
            do_action('xprofile_avatar_uploaded');
            bp_core_add_message(__('Your new profile photo was uploaded successfully.', 'buddypress'));
            bp_core_redirect(bp_displayed_user_domain());
        }
    }
    do_action('xprofile_screen_change_avatar');
    bp_core_load_template(apply_filters('xprofile_template_change_avatar', 'members/single/home'));
}
function tv_handle_avatar_upload_crop()
{
    global $bp, $wpdb;
    /* If the image cropping is done, crop the image and save a full/thumb version */
    if (isset($_POST['avatar_crop_submit'])) {
        if (!bp_core_avatar_handle_crop(array('original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h']))) {
            echo 'There was a problem cropping your avatar, please try uploading it again';
        } else {
            echo bp_core_fetch_avatar(array('item_id' => $_POST['user_id'], 'type' => 'full', 'width' => 150, 'height' => 150, 'html' => true));
            printf('<br><a href="#" data-confirm="%s" class="tutviet-button button avatar-delete">%s</a>', __('Are you sure?', 'tutviet'), __('Delete', 'tutviet'));
        }
        exit;
    }
}
function bp_ning_import_create_user($userdata)
{
    global $wpdb;
    $email = preg_replace('#(@.*)/#i', '$1', $userdata->email);
    // Check for existing member
    if ($user = get_user_by('email', $email)) {
        $bp_member = array();
        $bp_member['user_login'] = $user->user_login;
        $bp_member['user_name'] = $user->user_name;
        $bp_member['user_email'] = $user->user_email;
        $bp_member['display_name'] = $user->display_name;
        $bp_member['id'] = $user->ID;
        $bp_member['already_exists'] = 1;
        return $bp_member;
    }
    $username = strtolower(preg_replace("/\\s+/", '', $userdata->fullName));
    $username = str_replace('@', '', $username);
    $username = str_replace('.', '', $username);
    $username = str_replace(')', '', $username);
    $username = str_replace('(', '', $username);
    $username = str_replace("'", '', $username);
    $username = str_replace(':', '', $username);
    $username = preg_replace("/[^\t\n\r -]/", "", $username);
    $table = array('Š' => 'S', 'š' => 's', 'Đ' => 'Dj', 'đ' => 'dj', 'Ž' => 'Z', 'ž' => 'z', 'Č' => 'C', 'č' => 'c', 'Ć' => 'C', 'ć' => 'c', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'A', 'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'B', 'ß' => 'Ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'o', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ý' => 'y', 'ý' => 'y', 'þ' => 'b', 'ÿ' => 'y', 'Ŕ' => 'R', 'ŕ' => 'r');
    $username = strtr($username, $table);
    if (empty($username)) {
        $username = '******';
    }
    // Autogenerates username by adding an integer to the end of it
    if (username_exists($username)) {
        $i = 1;
        while (username_exists($username . $i)) {
            $i++;
        }
        $username = $username . $i;
    }
    // Autogenerate password
    $password = substr(md5(uniqid(microtime())), 0, 7);
    $bp_member = array("user_email" => $email, "user_name" => $userdata->fullName, "already_exists" => 0);
    // create user
    $args = array("user_login" => $username, "display_name" => $userdata->fullName, "nickname" => $userdata->fullName, "user_pass" => $password, "user_email" => $email);
    $bp_member['id'] = wp_insert_user($args);
    if (is_wp_error($bp_member['id'])) {
        var_dump($username, $userdata, $bp_member['id']);
        die;
    }
    $bp_member['user_login'] = $username;
    $bp_member['display_name'] = $userdata->fullName;
    $bp_member['password'] = $password;
    #echo "<br />" . $bp_member['id'] . ") $userdata->fullName created";
    $f = explode("?", $userdata->profilePhoto);
    $g = explode("members/", $f[0]);
    $oldfilepath = WP_CONTENT_DIR . '/ning-files/' . $f[0];
    if (!file_exists($oldfilepath)) {
        return;
    }
    $filename = $g[1];
    if (strpos($filename, '/')) {
        $fn = explode("/", $filename);
        $filename = array_pop($fn);
    }
    if (!preg_match('/png|gif|jpg|jpeg|bmp|PNG|GIF|JPG|JPEG|BMP/', $filename)) {
        return;
    }
    $newfilepath = BP_AVATAR_UPLOAD_PATH . '/' . $filename;
    if (!file_exists(BP_AVATAR_UPLOAD_PATH . '/avatars/')) {
        mkdir(BP_AVATAR_UPLOAD_PATH . '/avatars/');
    }
    if (!file_exists(BP_AVATAR_UPLOAD_PATH . '/avatars/' . $bp_member['id'])) {
        mkdir(BP_AVATAR_UPLOAD_PATH . '/avatars/' . $bp_member['id']);
    }
    copy($oldfilepath, $newfilepath);
    // Rudimentary squaring algorithm
    $size = getimagesize($newfilepath);
    $args = array('item_id' => $bp_member['id'], 'original_file' => '/' . $filename);
    if ($size[0] > $size[1]) {
        $diff = $size[0] - $size[1];
        $cropx = $diff / 2;
        $args['crop_w'] = $size[1];
        $args['crop_h'] = $size[1];
        $args['crop_x'] = $cropx;
    } else {
        $diff = $size[1] - $size[0];
        $cropy = $diff / 2;
        $args['crop_w'] = $size[0];
        $args['crop_h'] = $size[0];
        $args['crop_y'] = $cropy;
    }
    bp_core_avatar_handle_crop($args);
    // todo - find a good way to check for avatar import. bp_core_get_avatar()?
    // Store the Ning ID for association with content later on
    // update_user_meta( $bp_member['id'], 'ning_id', $userdata->contributerName );
    return $bp_member;
}
Example #9
0
function groups_action_create_group() {
	global $bp;

	/* If we're not at domain.org/groups/create/ then return false */
	if ( $bp->current_component != $bp->groups->slug || 'create' != $bp->current_action )
		return false;

	if ( !is_user_logged_in() )
		return false;

	/* Make sure creation steps are in the right order */
	groups_action_sort_creation_steps();

	/* If no current step is set, reset everything so we can start a fresh group creation */
	if ( !$bp->groups->current_create_step = $bp->action_variables[1] ) {

		unset( $bp->groups->current_create_step );
		unset( $bp->groups->completed_create_steps );

		setcookie( 'bp_new_group_id', false, time() - 1000, COOKIEPATH );
		setcookie( 'bp_completed_create_steps', false, time() - 1000, COOKIEPATH );

		$reset_steps = true;
		bp_core_redirect( $bp->root_domain . '/' . $bp->groups->slug . '/create/step/' . array_shift( array_keys( $bp->groups->group_creation_steps ) ) . '/' );
	}

	/* If this is a creation step that is not recognized, just redirect them back to the first screen */
	if ( $bp->action_variables[1] && !$bp->groups->group_creation_steps[$bp->action_variables[1]] ) {
		bp_core_add_message( __('There was an error saving group details. Please try again.', 'buddypress'), 'error' );
		bp_core_redirect( $bp->root_domain . '/' . $bp->groups->slug . '/create/' );
	}

	/* Fetch the currently completed steps variable */
	if ( isset( $_COOKIE['bp_completed_create_steps'] ) && !$reset_steps )
		$bp->groups->completed_create_steps = unserialize( stripslashes( $_COOKIE['bp_completed_create_steps'] ) );

	/* Set the ID of the new group, if it has already been created in a previous step */
	if ( isset( $_COOKIE['bp_new_group_id'] ) ) {
		$bp->groups->new_group_id = $_COOKIE['bp_new_group_id'];
		$bp->groups->current_group = new BP_Groups_Group( $bp->groups->new_group_id );
	}

	/* If the save, upload or skip button is hit, lets calculate what we need to save */
	if ( isset( $_POST['save'] ) ) {

		/* Check the nonce */
		check_admin_referer( 'groups_create_save_' . $bp->groups->current_create_step );

		if ( 'group-details' == $bp->groups->current_create_step ) {
			if ( empty( $_POST['group-name'] ) || empty( $_POST['group-desc'] ) || !strlen( trim( $_POST['group-name'] ) ) || !strlen( trim( $_POST['group-desc'] ) ) ) {
				bp_core_add_message( __( 'Please fill in all of the required fields', 'buddypress' ), 'error' );
				bp_core_redirect( $bp->root_domain . '/' . $bp->groups->slug . '/create/step/' . $bp->groups->current_create_step . '/' );
			}

			if ( !$bp->groups->new_group_id = groups_create_group( array( 'group_id' => $bp->groups->new_group_id, 'name' => $_POST['group-name'], 'description' => $_POST['group-desc'], 'slug' => groups_check_slug( sanitize_title( esc_attr( $_POST['group-name'] ) ) ), 'date_created' => gmdate( "Y-m-d H:i:s" ), 'status' => 'public' ) ) ) {
				bp_core_add_message( __( 'There was an error saving group details, please try again.', 'buddypress' ), 'error' );
				bp_core_redirect( $bp->root_domain . '/' . $bp->groups->slug . '/create/step/' . $bp->groups->current_create_step . '/' );
			}

			groups_update_groupmeta( $bp->groups->new_group_id, 'total_member_count', 1 );
			groups_update_groupmeta( $bp->groups->new_group_id, 'last_activity', gmdate( "Y-m-d H:i:s" ) );
		}

		if ( 'group-settings' == $bp->groups->current_create_step ) {
			$group_status = 'public';
			$group_enable_forum = 1;

			if ( !isset($_POST['group-show-forum']) ) {
				$group_enable_forum = 0;
			} else {
				/* Create the forum if enable_forum = 1 */
				if ( function_exists( 'bp_forums_setup' ) && '' == groups_get_groupmeta( $bp->groups->new_group_id, 'forum_id' ) ) {
					groups_new_group_forum();
				}
			}

			if ( 'private' == $_POST['group-status'] )
				$group_status = 'private';
			else if ( 'hidden' == $_POST['group-status'] )
				$group_status = 'hidden';

			if ( !$bp->groups->new_group_id = groups_create_group( array( 'group_id' => $bp->groups->new_group_id, 'status' => $group_status, 'enable_forum' => $group_enable_forum ) ) ) {
				bp_core_add_message( __( 'There was an error saving group details, please try again.', 'buddypress' ), 'error' );
				bp_core_redirect( $bp->root_domain . '/' . $bp->groups->slug . '/create/step/' . $bp->groups->current_create_step . '/' );
			}
		}

		if ( 'group-invites' == $bp->groups->current_create_step ) {
			groups_send_invites( $bp->loggedin_user->id, $bp->groups->new_group_id );
		}

		do_action( 'groups_create_group_step_save_' . $bp->groups->current_create_step );
		do_action( 'groups_create_group_step_complete' ); // Mostly for clearing cache on a generic action name

		/**
		 * Once we have successfully saved the details for this step of the creation process
		 * we need to add the current step to the array of completed steps, then update the cookies
		 * holding the information
		 */
		if ( !in_array( $bp->groups->current_create_step, (array)$bp->groups->completed_create_steps ) )
			$bp->groups->completed_create_steps[] = $bp->groups->current_create_step;

		/* Reset cookie info */
		setcookie( 'bp_new_group_id', $bp->groups->new_group_id, time()+60*60*24, COOKIEPATH );
		setcookie( 'bp_completed_create_steps', serialize( $bp->groups->completed_create_steps ), time()+60*60*24, COOKIEPATH );

		/* If we have completed all steps and hit done on the final step we can redirect to the completed group */
		if ( count( $bp->groups->completed_create_steps ) == count( $bp->groups->group_creation_steps ) && $bp->groups->current_create_step == array_pop( array_keys( $bp->groups->group_creation_steps ) ) ) {
			unset( $bp->groups->current_create_step );
			unset( $bp->groups->completed_create_steps );

			/* Once we compelete all steps, record the group creation in the activity stream. */
			groups_record_activity( array(
				'action' => apply_filters( 'groups_activity_created_group_action', sprintf( __( '%s created the group %s', 'buddypress'), bp_core_get_userlink( $bp->loggedin_user->id ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' ) ),
				'type' => 'created_group',
				'item_id' => $bp->groups->new_group_id
			) );

			do_action( 'groups_group_create_complete', $bp->groups->new_group_id );

			bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) );
		} else {
			/**
			 * Since we don't know what the next step is going to be (any plugin can insert steps)
			 * we need to loop the step array and fetch the next step that way.
			 */
			foreach ( (array)$bp->groups->group_creation_steps as $key => $value ) {
				if ( $key == $bp->groups->current_create_step ) {
					$next = 1;
					continue;
				}

				if ( $next ) {
					$next_step = $key;
					break;
				}
			}

			bp_core_redirect( $bp->root_domain . '/' . $bp->groups->slug . '/create/step/' . $next_step . '/' );
		}
	}

	/* Group avatar is handled separately */
	if ( 'group-avatar' == $bp->groups->current_create_step && isset( $_POST['upload'] ) ) {
		if ( !empty( $_FILES ) && isset( $_POST['upload'] ) ) {
			/* Normally we would check a nonce here, but the group save nonce is used instead */

			/* Pass the file to the avatar upload handler */
			if ( bp_core_avatar_handle_upload( $_FILES, 'groups_avatar_upload_dir' ) ) {
				$bp->avatar_admin->step = 'crop-image';

				/* Make sure we include the jQuery jCrop file for image cropping */
				add_action( 'wp', 'bp_core_add_jquery_cropper' );
			}
		}

		/* If the image cropping is done, crop the image and save a full/thumb version */
		if ( isset( $_POST['avatar-crop-submit'] ) && isset( $_POST['upload'] ) ) {
			/* Normally we would check a nonce here, but the group save nonce is used instead */

			if ( !bp_core_avatar_handle_crop( array( 'object' => 'group', 'avatar_dir' => 'group-avatars', 'item_id' => $bp->groups->current_group->id, 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h'] ) ) )
				bp_core_add_message( __( 'There was an error saving the group avatar, please try uploading again.', 'buddypress' ), 'error' );
			else
				bp_core_add_message( __( 'The group avatar was uploaded successfully!', 'buddypress' ) );
		}
	}

 	bp_core_load_template( apply_filters( 'groups_template_create_group', 'groups/create' ) );
}
Example #10
0
function bp_caver_avatar_handle_upload()
{
    global $bp;
    if ($_POST['encodedimg']) {
        $user_id = !empty($_POST['user_id']) ? $_POST['user_id'] : bp_displayed_user_id();
        $imgresponse = array();
        $uploaddir = bp_core_avatar_upload_path() . '/avatars';
        if (!file_exists($uploaddir)) {
            mkdir($uploaddir);
        }
        $img = $_POST['encodedimg'];
        $img = str_replace('data:' . $_POST['imgtype'] . ';base64,', '', $img);
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);
        $filepath = $uploaddir . '/' . $user_id;
        if (!file_exists($filepath)) {
            mkdir($filepath);
        }
        $imgname = wp_unique_filename($uploaddir, $_POST['imgname']);
        $fileurl = $filepath . '/' . $imgname;
        $siteurl = trailingslashit(get_blog_option(1, 'siteurl'));
        $url = str_replace(ABSPATH, $siteurl, $fileurl);
        $success = file_put_contents($fileurl, $data);
        $file = $_POST['imgsize'];
        $max_upload_size = bp_cover_get_max_media_size();
        if ($max_upload_size > $file) {
            if ($success) {
                $imgresponse[0] = "1";
                $imgresponse[1] = $fileurl;
                $size = getimagesize($fileurl);
                /* Check image size and shrink if too large */
                if ($size[0] > 150) {
                    $original_file = image_resize($fileurl, 150, 150, true);
                    //$ava_file = image_resize( $fileurl, 250, 250, true );
                    /* Check for thumbnail creation errors */
                    if (is_wp_error($original_file)) {
                        $imgresponse[0] = "0";
                        $imgresponse[1] = sprintf(__('Upload Failed! Error was: %s', 'bp-cover'), $original_file->get_error_message());
                        die;
                    }
                    $avatar_to_crop = str_replace(bp_core_avatar_upload_path(), '', $original_file);
                    bp_core_delete_existing_avatar(array('item_id' => $user_id, 'avatar_path' => bp_core_avatar_upload_path() . '/avatars/' . $user_id));
                    $crop_args = array('item_id' => $user_id, 'original_file' => $avatar_to_crop, 'crop_w' => 0, 'crop_h' => 0);
                    bp_core_avatar_handle_crop($crop_args);
                    //$url = str_replace(ABSPATH,$siteurl,$ava_file);
                    update_user_meta(bp_loggedin_user_id(), 'profile_avatar', $url);
                    do_action('xprofile_avatar_uploaded');
                } else {
                    $imgresponse[0] = "0";
                    $imgresponse[1] = __('Upload Failed! Your photo must be larger than 150px', 'bp-cover');
                }
            } else {
                $imgresponse[0] = "0";
                $imgresponse[1] = __('Upload Failed! Unable to write the image on server', 'bp-cover');
            }
        } else {
            $imgresponse[0] = "0";
            $imgresponse[1] = sprintf(__('The file you uploaded is too big. Please upload a file under %s', 'bp-cover'), size_format($max_upload_size));
        }
    } else {
        $imgresponse[0] = "0";
        $imgresponse[1] = __('Upload Failed! No image sent', 'bp-cover');
    }
    /* if everything is ok, we send back url to thumbnail and to full image */
    echo json_encode($imgresponse);
    die;
}
Example #11
0
function bp_core_screen_signup() {
	global $bp, $wpdb;

	if ( $bp->current_component != BP_REGISTER_SLUG )
		return false;

	/* If the user is logged in, redirect away from here */
	if ( is_user_logged_in() )
		bp_core_redirect( $bp->root_domain );

	/* If signups are disabled, just re-direct */
	if ( !bp_get_signup_allowed() )
		bp_core_redirect( $bp->root_domain );

	$bp->signup->step = 'request-details';

	/* If the signup page is submitted, validate and save */
	if ( isset( $_POST['signup_submit'] ) ) {

		/* Check the nonce */
		check_admin_referer( 'bp_new_signup' );

		require_once( ABSPATH . WPINC . '/registration.php' );

		/* Check the base account details for problems */
		$account_details = bp_core_validate_user_signup( $_POST['signup_username'], $_POST['signup_email'] );

		/* If there are errors with account details, set them for display */
		if ( !empty( $account_details['errors']->errors['user_name'] ) )
			$bp->signup->errors['signup_username'] = $account_details['errors']->errors['user_name'][0];

		if ( !empty( $account_details['errors']->errors['user_email'] ) )
			$bp->signup->errors['signup_email'] = $account_details['errors']->errors['user_email'][0];

		/* Check that both password fields are filled in */
		if ( empty( $_POST['signup_password'] ) || empty( $_POST['signup_password_confirm'] ) )
			$bp->signup->errors['signup_password'] = __( 'Please make sure you enter your password twice', 'buddypress' );

		/* Check that the passwords match */
		if ( ( !empty( $_POST['signup_password'] ) && !empty( $_POST['signup_password_confirm'] ) ) && $_POST['signup_password'] != $_POST['signup_password_confirm'] )
			$bp->signup->errors['signup_password'] = __( 'The passwords you entered do not match.', 'buddypress' );

		$bp->signup->username = $_POST['signup_username'];
		$bp->signup->email = $_POST['signup_email'];

		/* Now we've checked account details, we can check profile information */
		if ( function_exists( 'xprofile_check_is_required_field' ) ) {

			/* Make sure hidden field is passed and populated */
			if ( isset( $_POST['signup_profile_field_ids'] ) && !empty( $_POST['signup_profile_field_ids'] ) ) {

				/* Let's compact any profile field info into an array */
				$profile_field_ids = explode( ',', $_POST['signup_profile_field_ids'] );

				/* Loop through the posted fields formatting any datebox values then validate the field */
				foreach ( (array) $profile_field_ids as $field_id ) {
					if ( !isset( $_POST['field_' . $field_id] ) ) {
						if ( isset( $_POST['field_' . $field_id . '_day'] ) )
							$_POST['field_' . $field_id] = strtotime( $_POST['field_' . $field_id . '_day'] . $_POST['field_' . $field_id . '_month'] . $_POST['field_' . $field_id . '_year'] );
					}

					/* Create errors for required fields without values */
					if ( xprofile_check_is_required_field( $field_id ) && empty( $_POST['field_' . $field_id] ) )
						$bp->signup->errors['field_' . $field_id] = __( 'This is a required field', 'buddypress' );
				}

			/* This situation doesn't naturally occur so bounce to website root */
			} else {
				bp_core_redirect( $bp->root_domain );
			}
		}

		/* Finally, let's check the blog details, if the user wants a blog and blog creation is enabled */
		if ( isset( $_POST['signup_with_blog'] ) ) {
			$active_signup = $bp->site_options['registration'];

			if ( 'blog' == $active_signup || 'all' == $active_signup ) {
				$blog_details = bp_core_validate_blog_signup( $_POST['signup_blog_url'], $_POST['signup_blog_title'] );

				/* If there are errors with blog details, set them for display */
				if ( !empty( $blog_details['errors']->errors['blogname'] ) )
					$bp->signup->errors['signup_blog_url'] = $blog_details['errors']->errors['blogname'][0];

				if ( !empty( $blog_details['errors']->errors['blog_title'] ) )
					$bp->signup->errors['signup_blog_title'] = $blog_details['errors']->errors['blog_title'][0];
			}
		}

		do_action( 'bp_signup_validate' );

		/* Add any errors to the action for the field in the template for display. */
		if ( !empty( $bp->signup->errors ) ) {
			foreach ( (array)$bp->signup->errors as $fieldname => $error_message )
				add_action( 'bp_' . $fieldname . '_errors', create_function( '', 'echo "<div class=\"error\">' . $error_message . '</div>";' ) );
		} else {
			$bp->signup->step = 'save-details';

			/* No errors! Let's register those deets. */
			$active_signup = $bp->site_options['registration'];

			if ( 'none' != $active_signup ) {

				/* Let's compact any profile field info into usermeta */
				$profile_field_ids = explode( ',', $_POST['signup_profile_field_ids'] );

				/* Loop through the posted fields formatting any datebox values then add to usermeta */
				foreach ( (array) $profile_field_ids as $field_id ) {
					if ( !isset( $_POST['field_' . $field_id] ) ) {
						if ( isset( $_POST['field_' . $field_id . '_day'] ) )
							$_POST['field_' . $field_id] = strtotime( $_POST['field_' . $field_id . '_day'] . $_POST['field_' . $field_id . '_month'] . $_POST['field_' . $field_id . '_year'] );
					}

					if ( !empty( $_POST['field_' . $field_id] ) )
						$usermeta['field_' . $field_id] = $_POST['field_' . $field_id];
				}

				/* Store the profile field ID's in usermeta */
				$usermeta['profile_field_ids'] = $_POST['signup_profile_field_ids'];

				/* Hash and store the password */
				$usermeta['password'] = wp_hash_password( $_POST['signup_password'] );

				/* If the user decided to create a blog, save those details to usermeta */
				if ( 'blog' == $active_signup || 'all' == $active_signup ) {
					$usermeta['public'] = ( 'public' == $_POST['signup_blog_privacy'] ) ? true : false;
				}

				$usermeta = apply_filters( 'bp_signup_usermeta', $usermeta );

				/* Finally, sign up the user and/or blog */
				if ( isset( $_POST['signup_with_blog'] ) && bp_core_is_multisite() )
					bp_core_signup_blog( $blog_details['domain'], $blog_details['path'], $blog_details['blog_title'], $_POST['signup_username'], $_POST['signup_email'], $usermeta );
				else {
					bp_core_signup_user( $_POST['signup_username'], $_POST['signup_password'], $_POST['signup_email'], $usermeta );
				}

				$bp->signup->step = 'completed-confirmation';
			}

			do_action( 'bp_complete_signup' );
		}

	}

	$bp->avatar_admin->step = 'upload-image';

	/* If user has uploaded a new avatar */
	if ( !empty( $_FILES ) ) {

		/* Check the nonce */
		check_admin_referer( 'bp_avatar_upload' );

		$bp->signup->step = 'completed-confirmation';

		if ( bp_core_is_multisite() ) {
			/* Get the activation key */
			if ( !$bp->signup->key = $wpdb->get_var( $wpdb->prepare( "SELECT activation_key FROM {$wpdb->signups} WHERE user_login = %s AND user_email = %s", $_POST[ 'signup_username' ], $_POST[ 'signup_email' ] ) ) ) {
				bp_core_add_message( __( 'There was a problem uploading your avatar, please try uploading it again', 'buddypress' ) );
			} else {
				/* Hash the key to create the upload folder (added security so people don't sniff the activation key) */
				$bp->signup->avatar_dir = wp_hash( $bp->signup->key );
			}
		} else {
			$user_id = bp_core_get_userid( $_POST['signup_username'] );
			$bp->signup->avatar_dir = wp_hash( $user_id );
		}

		/* Pass the file to the avatar upload handler */
		if ( bp_core_avatar_handle_upload( $_FILES, 'bp_core_signup_avatar_upload_dir' ) ) {
			$bp->avatar_admin->step = 'crop-image';

			/* Make sure we include the jQuery jCrop file for image cropping */
			add_action( 'wp', 'bp_core_add_jquery_cropper' );
		}
	}

	/* If the image cropping is done, crop the image and save a full/thumb version */
	if ( isset( $_POST['avatar-crop-submit'] ) ) {

		/* Check the nonce */
		check_admin_referer( 'bp_avatar_cropstore' );

		/* Reset the avatar step so we can show the upload form again if needed */
		$bp->signup->step = 'completed-confirmation';
		$bp->avatar_admin->step = 'upload-image';

		if ( !bp_core_avatar_handle_crop( array( 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h'] ) ) )
			bp_core_add_message( __( 'There was a problem cropping your avatar, please try uploading it again', 'buddypress' ), 'error' );
		else
			bp_core_add_message( __( 'Your new avatar was uploaded successfully', 'buddypress' ) );
	}
	bp_core_load_template( 'registration/register' );
}
Example #12
0
/**
 * Ajax set an avatar for a given object and item id
 *
 * @since BuddyPress (2.3.0)
 *
 * @return  string a json object containing success data if the crop/capture succeeded
 *                 error message otherwise
 */
function bp_avatar_ajax_set()
{
    // Bail if not a POST action
    if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        wp_send_json_error();
    }
    // Check the nonce
    check_admin_referer('bp_avatar_cropstore', 'nonce');
    $avatar_data = wp_parse_args($_POST, array('crop_w' => bp_core_avatar_full_width(), 'crop_h' => bp_core_avatar_full_height(), 'crop_x' => 0, 'crop_y' => 0));
    if (empty($avatar_data['object']) || empty($avatar_data['item_id']) || empty($avatar_data['original_file'])) {
        wp_send_json_error();
    }
    // Capability check
    if (!bp_attachments_current_user_can('edit_avatar', $avatar_data)) {
        wp_send_json_error();
    }
    if (!empty($avatar_data['type']) && 'camera' === $avatar_data['type'] && 'user' === $avatar_data['object']) {
        $webcam_avatar = false;
        if (!empty($avatar_data['original_file'])) {
            $webcam_avatar = str_replace(array('data:image/png;base64,', ' '), array('', '+'), $avatar_data['original_file']);
            $webcam_avatar = base64_decode($webcam_avatar);
        }
        if (!bp_avatar_handle_capture($webcam_avatar, $avatar_data['item_id'])) {
            wp_send_json_error(array('feedback_code' => 1));
        } else {
            $return = array('avatar' => html_entity_decode(bp_core_fetch_avatar(array('object' => $avatar_data['object'], 'item_id' => $avatar_data['item_id'], 'html' => false, 'type' => 'full'))), 'feedback_code' => 2, 'item_id' => $avatar_data['item_id']);
            do_action('xprofile_screen_change_avatar');
            wp_send_json_success($return);
        }
        return;
    }
    $original_file = str_replace(bp_core_avatar_url(), '', $avatar_data['original_file']);
    // Set avatars dir & feedback part
    if ('user' === $avatar_data['object']) {
        $avatar_dir = 'avatars';
        // Defaults to object-avatars dir
    } else {
        $avatar_dir = sanitize_key($avatar_data['object']) . '-avatars';
    }
    // Crop args
    $r = array('item_id' => $avatar_data['item_id'], 'object' => $avatar_data['object'], 'avatar_dir' => $avatar_dir, 'original_file' => $original_file, 'crop_w' => $avatar_data['crop_w'], 'crop_h' => $avatar_data['crop_h'], 'crop_x' => $avatar_data['crop_x'], 'crop_y' => $avatar_data['crop_y']);
    // Handle crop
    if (bp_core_avatar_handle_crop($r)) {
        $return = array('avatar' => html_entity_decode(bp_core_fetch_avatar(array('object' => $avatar_data['object'], 'item_id' => $avatar_data['item_id'], 'html' => false, 'type' => 'full'))), 'feedback_code' => 2, 'item_id' => $avatar_data['item_id']);
        if ('user' === $avatar_data['object']) {
            do_action('xprofile_screen_change_avatar');
        }
        wp_send_json_success($return);
    } else {
        wp_send_json_error(array('feedback_code' => 1));
    }
}
function bfox_bp_plans_update_plan_avatar(BfoxReadingPlan $plan, $is_create = false)
{
    global $bp;
    bfox_bp_plans_must_own($plan);
    if (!$is_create) {
        /* If the group admin has deleted the admin avatar */
        if ('delete' == $bp->action_variables[0]) {
            /* Check the nonce */
            check_admin_referer('bfox_bp_plan_avatar_delete');
            if (bp_core_delete_existing_avatar(array('item_id' => $plan->id, 'object' => 'plan'))) {
                bp_core_add_message(__('Your avatar was deleted successfully!', 'buddypress'));
            } else {
                bp_core_add_message(__('There was a problem deleting that avatar, please try again.', 'buddypress'), 'error');
            }
            bp_core_redirect($plan->url() . 'avatar/');
        }
    }
    $bp->avatar_admin->step = 'upload-image';
    if (!empty($_FILES) && isset($_POST['upload'])) {
        if ($is_create) {
            check_admin_referer('plans_create_save_plan-avatar');
        } else {
            check_admin_referer('bp_avatar_upload');
        }
        /* Pass the file to the avatar upload handler */
        if (bp_core_avatar_handle_upload($_FILES, 'bfox_bp_plans_avatar_upload_dir')) {
            $bp->avatar_admin->step = 'crop-image';
            /* Make sure we include the jQuery jCrop file for image cropping */
            add_action('wp', 'bp_core_add_jquery_cropper');
        }
    }
    /* If the image cropping is done, crop the image and save a full/thumb version */
    if (isset($_POST['avatar-crop-submit'])) {
        if ($is_create) {
            check_admin_referer('plans_create_save_plan-avatar');
        } else {
            check_admin_referer('bp_avatar_cropstore');
        }
        if (!bp_core_avatar_handle_crop(array('object' => 'plan', 'avatar_dir' => 'plan-avatars', 'item_id' => $plan->id, 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h']))) {
            bp_core_add_message(__('There was a problem cropping the avatar, please try uploading it again', 'buddypress'));
        } else {
            bp_core_add_message(__('The new reading plan avatar was uploaded successfully!', 'buddypress'));
        }
    }
}
/**
 * Handles the uploading and cropping of a user avatar. Displays the change avatar page.
 *
 * @package BuddyPress XProfile
 * @uses bp_is_my_profile() Checks to make sure the current user being viewed equals the logged in user
 * @uses bp_core_load_template() Looks for and loads a template file within the current member theme (folder/filename)
 */
function xprofile_screen_change_avatar()
{
    global $bp;
    if (!bp_is_my_profile() && !is_super_admin()) {
        return false;
    }
    if (bp_action_variables()) {
        bp_do_404();
        return;
    }
    $bp->avatar_admin->step = 'upload-image';
    if (!empty($_FILES)) {
        // Check the nonce
        check_admin_referer('bp_avatar_upload');
        // Pass the file to the avatar upload handler
        if (bp_core_avatar_handle_upload($_FILES, 'xprofile_avatar_upload_dir')) {
            $bp->avatar_admin->step = 'crop-image';
            // Make sure we include the jQuery jCrop file for image cropping
            add_action('nxt_print_scripts', 'bp_core_add_jquery_cropper');
        }
    }
    // If the image cropping is done, crop the image and save a full/thumb version
    if (isset($_POST['avatar-crop-submit'])) {
        // Check the nonce
        check_admin_referer('bp_avatar_cropstore');
        if (!bp_core_avatar_handle_crop(array('item_id' => $bp->displayed_user->id, 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h']))) {
            bp_core_add_message(__('There was a problem cropping your avatar, please try uploading it again', 'buddypress'), 'error');
        } else {
            bp_core_add_message(__('Your new avatar was uploaded successfully!', 'buddypress'));
            do_action('xprofile_avatar_uploaded');
        }
    }
    do_action('xprofile_screen_change_avatar');
    bp_core_load_template(apply_filters('xprofile_template_change_avatar', 'members/single/home'));
}
function groups_screen_group_admin_avatar()
{
    global $bp;
    if (bp_is_groups_component() && bp_is_action_variable('group-avatar', 0)) {
        // If the logged-in user doesn't have permission or if avatar uploads are disabled, then stop here
        if (!$bp->is_item_admin || (int) bp_get_option('bp-disable-avatar-uploads')) {
            return false;
        }
        // If the group admin has deleted the admin avatar
        if (bp_is_action_variable('delete', 1)) {
            // Check the nonce
            check_admin_referer('bp_group_avatar_delete');
            if (bp_core_delete_existing_avatar(array('item_id' => $bp->groups->current_group->id, 'object' => 'group'))) {
                bp_core_add_message(__('Your avatar was deleted successfully!', 'buddypress'));
            } else {
                bp_core_add_message(__('There was a problem deleting that avatar, please try again.', 'buddypress'), 'error');
            }
        }
        $bp->avatar_admin->step = 'upload-image';
        if (!empty($_FILES)) {
            // Check the nonce
            check_admin_referer('bp_avatar_upload');
            // Pass the file to the avatar upload handler
            if (bp_core_avatar_handle_upload($_FILES, 'groups_avatar_upload_dir')) {
                $bp->avatar_admin->step = 'crop-image';
                // Make sure we include the jQuery jCrop file for image cropping
                add_action('wp_print_scripts', 'bp_core_add_jquery_cropper');
            }
        }
        // If the image cropping is done, crop the image and save a full/thumb version
        if (isset($_POST['avatar-crop-submit'])) {
            // Check the nonce
            check_admin_referer('bp_avatar_cropstore');
            if (!bp_core_avatar_handle_crop(array('object' => 'group', 'avatar_dir' => 'group-avatars', 'item_id' => $bp->groups->current_group->id, 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h']))) {
                bp_core_add_message(__('There was a problem cropping the avatar, please try uploading it again', 'buddypress'));
            } else {
                bp_core_add_message(__('The new group avatar was uploaded successfully!', 'buddypress'));
            }
        }
        do_action('groups_screen_group_admin_avatar', $bp->groups->current_group->id);
        bp_core_load_template(apply_filters('groups_template_group_admin_avatar', 'groups/single/home'));
    }
}
Example #16
0
/**
 * Ajax set an avatar for a given object and item id.
 *
 * @since 2.3.0
 *
 * @return  string|null A json object containing success data if the crop/capture succeeded
 *                      error message otherwise.
 */
function bp_avatar_ajax_set()
{
    // Bail if not a POST action
    if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        wp_send_json_error();
    }
    // Check the nonce
    check_admin_referer('bp_avatar_cropstore', 'nonce');
    $avatar_data = wp_parse_args($_POST, array('crop_w' => bp_core_avatar_full_width(), 'crop_h' => bp_core_avatar_full_height(), 'crop_x' => 0, 'crop_y' => 0));
    if (empty($avatar_data['object']) || empty($avatar_data['item_id']) || empty($avatar_data['original_file'])) {
        wp_send_json_error();
    }
    // Capability check
    if (!bp_attachments_current_user_can('edit_avatar', $avatar_data)) {
        wp_send_json_error();
    }
    if (!empty($avatar_data['type']) && 'camera' === $avatar_data['type'] && 'user' === $avatar_data['object']) {
        $webcam_avatar = false;
        if (!empty($avatar_data['original_file'])) {
            $webcam_avatar = str_replace(array('data:image/png;base64,', ' '), array('', '+'), $avatar_data['original_file']);
            $webcam_avatar = base64_decode($webcam_avatar);
        }
        if (!bp_avatar_handle_capture($webcam_avatar, $avatar_data['item_id'])) {
            wp_send_json_error(array('feedback_code' => 1));
        } else {
            $return = array('avatar' => html_entity_decode(bp_core_fetch_avatar(array('object' => $avatar_data['object'], 'item_id' => $avatar_data['item_id'], 'html' => false, 'type' => 'full'))), 'feedback_code' => 2, 'item_id' => $avatar_data['item_id']);
            /**
             * Fires if the new avatar was successfully captured.
             *
             * @since 1.1.0 Used to inform the avatar was successfully cropped
             * @since 2.3.4 Add two new parameters to inform about the user id and
             *              about the way the avatar was set (eg: 'crop' or 'camera')
             *              Move the action at the right place, once the avatar is set
             *
             * @param string $item_id Inform about the user id the avatar was set for
             * @param string $type    Inform about the way the avatar was set ('camera')
             */
            do_action('xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type']);
            wp_send_json_success($return);
        }
        return;
    }
    $original_file = str_replace(bp_core_avatar_url(), '', $avatar_data['original_file']);
    // Set avatars dir & feedback part
    if ('user' === $avatar_data['object']) {
        $avatar_dir = 'avatars';
        // Defaults to object-avatars dir
    } else {
        $avatar_dir = sanitize_key($avatar_data['object']) . '-avatars';
    }
    // Crop args
    $r = array('item_id' => $avatar_data['item_id'], 'object' => $avatar_data['object'], 'avatar_dir' => $avatar_dir, 'original_file' => $original_file, 'crop_w' => $avatar_data['crop_w'], 'crop_h' => $avatar_data['crop_h'], 'crop_x' => $avatar_data['crop_x'], 'crop_y' => $avatar_data['crop_y']);
    // Handle crop
    if (bp_core_avatar_handle_crop($r)) {
        $return = array('avatar' => html_entity_decode(bp_core_fetch_avatar(array('object' => $avatar_data['object'], 'item_id' => $avatar_data['item_id'], 'html' => false, 'type' => 'full'))), 'feedback_code' => 2, 'item_id' => $avatar_data['item_id']);
        if ('user' === $avatar_data['object']) {
            /**
             * Fires if the new avatar was successfully cropped.
             *
             * @since 1.1.0 Used to inform the avatar was successfully cropped
             * @since 2.3.4 Add two new parameters to inform about the user id and
             *              about the way the avatar was set (eg: 'crop' or 'camera')
             *              Move the action at the right place, once the avatar is set
             *
             * @param string $item_id Inform about the user id the avatar was set for
             * @param string $type Inform about the way the avatar was set ('crop')
             */
            do_action('xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type']);
        }
        wp_send_json_success($return);
    } else {
        wp_send_json_error(array('feedback_code' => 1));
    }
}
Example #17
0
/**
 * Load Link home page edit avatar template, handle form if submitted
 */
function bp_links_screen_link_admin_avatar()
{
    global $bp;
    if (!$bp->is_item_admin || 'link-avatar' != bp_links_admin_current_action_variable()) {
        return false;
    }
    // handle empty avatar admin property
    if (false === isset($bp->avatar_admin)) {
        $bp->avatar_admin = new stdClass();
    }
    // If the link admin has deleted the admin avatar
    if ('delete' == $bp->action_variables[1]) {
        /* Check the nonce */
        check_admin_referer('bp_link_avatar_delete');
        if (bp_core_delete_existing_avatar(array('item_id' => $bp->links->current_link->id, 'object' => 'link', 'avatar_dir' => 'link-avatars'))) {
            bp_core_add_message(__('Your avatar was deleted successfully!', 'buddypress-links'));
        } else {
            bp_core_add_message(sprintf('%s %s', __('There was a problem deleting that avatar', 'buddypress-links'), __('Please try again.', 'buddypress-links')), 'error');
        }
    }
    $bp->avatar_admin->step = 'upload-image';
    if (isset($_POST['avatar-crop-submit'])) {
        // Check the nonce
        check_admin_referer('bp_avatar_cropstore');
        // received crop coords, crop the image and save a full/thumb version
        if (bp_core_avatar_handle_crop(array('object' => 'link', 'avatar_dir' => 'link-avatars', 'item_id' => $bp->links->current_link->id, 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h']))) {
            bp_links_embed_handle_crop($bp->links->current_link);
            bp_core_add_message(__('The link avatar was uploaded successfully!', 'buddypress-links'));
        } else {
            bp_core_add_message(sprintf('%s %s', __('There was an error saving link avatar.', 'buddypress-links'), __('Please try again.', 'buddypress-links')), 'error');
        }
    } elseif (isset($_POST['upload']) || isset($_POST['embed-submit'])) {
        // Check the nonce
        check_admin_referer('bp_avatar_upload');
        // handle image uploading
        if (!empty($_POST['embed-submit']) && bp_links_embed_handle_upload($bp->links->current_link, $_POST['embed-html'])) {
            // we are good to crop
            $bp->avatar_admin->step = 'crop-image';
            // Make sure we include the jQuery jCrop file for image cropping
            add_action('wp_enqueue_scripts', 'bp_core_add_jquery_cropper');
        } elseif (isset($_POST['upload']) && !empty($_FILES)) {
            // Pass the file to the avatar upload handler
            if (bp_core_avatar_handle_upload($_FILES, 'bp_links_avatar_upload_dir')) {
                // we are good to crop
                $bp->avatar_admin->step = 'crop-image';
                // Make sure we include the jQuery jCrop file for image cropping
                add_action('wp_enqueue_scripts', 'bp_core_add_jquery_cropper');
            }
        }
    }
    do_action('bp_links_screen_link_admin_avatar', $bp->links->current_link->id);
    bp_links_load_template('single/home');
}