Пример #1
0
function cimy_update_ExtraFields_new_me()
{
    global $wpdb, $wpdb_data_table, $user_ID, $max_length_value, $fields_name_prefix, $cimy_uef_file_types, $user_level, $cimy_uef_domain;
    include_once ABSPATH . '/wp-admin/includes/user.php';
    // if updating meta-data from registration post then exit
    if (isset($_POST['cimy_post'])) {
        return;
    }
    if (isset($_POST['user_id'])) {
        $get_user_id = $_POST['user_id'];
        if (!current_user_can('edit_user', $get_user_id)) {
            return;
        }
    } else {
        return;
    }
    //echo "asd";
    if (!function_exists('get_cimyFields')) {
        return;
    }
    $get_user_id = intval($get_user_id);
    $profileuser = get_user_to_edit($get_user_id);
    $user_login = $profileuser->user_login;
    $user_displayname = $profileuser->display_name;
    $extra_fields = get_cimyFields(false, true);
    $query = "UPDATE " . $wpdb_data_table . " SET VALUE=CASE FIELD_ID";
    $i = 0;
    $field_ids = "";
    $mail_changes = "";
    foreach ($extra_fields as $thisField) {
        $field_id = $thisField["ID"];
        $name = $thisField["NAME"];
        $type = $thisField["TYPE"];
        $label = $thisField["LABEL"];
        $rules = $thisField["RULES"];
        $unique_id = $fields_name_prefix . $field_id;
        $input_name = $fields_name_prefix . esc_attr($name);
        $field_id_data = $input_name . "_" . $field_id . "_data";
        $advanced_options = cimy_uef_parse_advanced_options($rules["advanced_options"]);
        cimy_insert_ExtraFields_if_not_exist($get_user_id, $field_id);
        // if the current user LOGGED IN has not enough permissions to see the field, skip it
        // apply only for EXTRA FIELDS
        if ($rules['show_level'] == 'view_cimy_extra_fields') {
            if (!current_user_can($rules['show_level'])) {
                continue;
            }
        } else {
            if ($user_level < $rules['show_level']) {
                continue;
            }
        }
        // if show_level == anonymous then do NOT ovverride other show_xyz rules
        if ($rules['show_level'] == -1) {
            // if flag to show the field in the profile is NOT activated, skip it
            if (!$rules['show_in_profile']) {
                continue;
            }
        }
        $prev_value = $wpdb->escape(stripslashes($_POST[$input_name . "_" . $field_id . "_prev_value"]));
        if (cimy_uef_is_field_disabled($type, $rules['edit'], $prev_value)) {
            continue;
        }
        if (isset($_POST[$input_name]) && !in_array($type, $cimy_uef_file_types)) {
            if ($type == "dropdown-multi") {
                $field_value = stripslashes(implode(",", $_POST[$input_name]));
            } else {
                $field_value = stripslashes($_POST[$input_name]);
            }
            if ($type == "picture-url") {
                $field_value = str_replace('../', '', $field_value);
            }
            if (isset($rules['max_length'])) {
                $field_value = substr($field_value, 0, $rules['max_length']);
            } else {
                $field_value = substr($field_value, 0, $max_length_value);
            }
            $field_value = $wpdb->escape($field_value);
            if ($i > 0) {
                $field_ids .= ", ";
            } else {
                $i = 1;
            }
            $field_ids .= $field_id;
            $query .= " WHEN " . $field_id . " THEN ";
            switch ($type) {
                case 'dropdown':
                case 'dropdown-multi':
                    $ret = cimy_dropDownOptions($label, $field_value);
                    $label = $ret['label'];
                case 'picture-url':
                case 'textarea':
                case 'textarea-rich':
                case 'password':
                case 'text':
                    $value = "'" . $field_value . "'";
                    $prev_value = "'" . $prev_value . "'";
                    break;
                case 'checkbox':
                    $value = $field_value == '1' ? "'YES'" : "'NO'";
                    $prev_value = $prev_value == "YES" ? "'YES'" : "'NO'";
                    break;
                case 'radio':
                    $value = $field_value == $field_id ? "'selected'" : "''";
                    $prev_value = "'" . $prev_value . "'";
                    break;
            }
            $query .= $value;
        } else {
            $rules = $thisField['RULES'];
            if (in_array($type, $cimy_uef_file_types)) {
                if ($type == "avatar") {
                    // since avatars are drawn max to 512px then we can save bandwith resizing, do it!
                    $rules['equal_to'] = 512;
                }
                if (isset($_POST[$input_name . '_del'])) {
                    $delete_file = true;
                } else {
                    $delete_file = false;
                }
                if (isset($_POST[$input_name . "_" . $field_id . "_prev_value"])) {
                    $old_file = stripslashes($_POST[$input_name . "_" . $field_id . "_prev_value"]);
                } else {
                    $old_file = false;
                }
                $field_value = cimy_manage_upload($input_name, $user_login, $rules, $old_file, $delete_file, $type, !empty($advanced_options["filename"]) ? $advanced_options["filename"] : "");
                if (!empty($field_value) || $delete_file) {
                    if ($i > 0) {
                        $field_ids .= ", ";
                    } else {
                        $i = 1;
                    }
                    $field_ids .= $field_id;
                    $value = "'" . $field_value . "'";
                    $prev_value = "'" . $prev_value . "'";
                    $query .= " WHEN " . $field_id . " THEN ";
                    $query .= $value;
                } else {
                    $prev_value = $value;
                    $file_on_server = cimy_uef_get_dir_or_filename($user_login, $old_file, false);
                    if ($type == "picture" || $type == "avatar") {
                        cimy_uef_crop_image($file_on_server, $field_id_data);
                    }
                }
            }
            if ($type == 'checkbox') {
                // if can be editable then write NO
                // there is no way to understand if was YES or NO previously
                // without adding other hidden inputs so write always
                if ($i > 0) {
                    $field_ids .= ", ";
                } else {
                    $i = 1;
                }
                $field_ids .= $field_id;
                $field_value = "NO";
                $value = "'" . $field_value . "'";
                $prev_value = $prev_value == "YES" ? "'YES'" : "'NO'";
                $query .= " WHEN " . $field_id . " THEN ";
                $query .= $value;
            }
            if ($type == 'dropdown-multi') {
                // if can be editable then write ''
                // there is no way to understand if was YES or NO previously
                // without adding other hidden inputs so write always
                if ($i > 0) {
                    $field_ids .= ", ";
                } else {
                    $i = 1;
                }
                $field_ids .= $field_id;
                $field_value = '';
                $value = "'" . $field_value . "'";
                $prev_value = "'" . $prev_value . "'";
                $ret = cimy_dropDownOptions($label, $field_value);
                $label = $ret['label'];
                $query .= " WHEN " . $field_id . " THEN ";
                $query .= $value;
            }
        }
        if ($rules["email_admin"] && $value != $prev_value && $type != "registration-date") {
            $mail_changes .= sprintf(__("%s previous value: %s new value: %s", $cimy_uef_domain), $label, stripslashes($prev_value), stripslashes($value));
            $mail_changes .= "\r\n";
        }
    }
    if ($i > 0) {
        $query .= " ELSE FIELD_ID END WHERE FIELD_ID IN(" . $field_ids . ") AND USER_ID = " . $get_user_id;
        // $query WILL BE: UPDATE <table> SET VALUE=CASE FIELD_ID WHEN <field_id1> THEN <value1> [WHEN ... THEN ...] ELSE FIELD_ID END WHERE FIELD_ID IN(<field_id1>, [<field_id2>...]) AND USER_ID=<user_id>
        $wpdb->query($query);
    }
    // mail only if set and if there is something to mail
    if (!empty($mail_changes)) {
        $admin_email = get_option('admin_email');
        $mail_subject = sprintf(__("%s (%s) has changed one or more fields", $cimy_uef_domain), $user_displayname, $user_login);
        wp_mail($admin_email, $mail_subject, $mail_changes);
    }
}
function cimy_registration_check($user_login, $user_email, $errors)
{
    global $wpdb, $rule_canbeempty, $rule_email, $rule_maxlen, $fields_name_prefix, $wp_fields_name_prefix, $rule_equalto_case_sensitive, $apply_equalto_rule, $cimy_uef_domain, $cimy_uef_file_types, $rule_equalto_regex, $user_level, $cimy_uef_file_images_types, $wp_hidden_fields, $rule_maxlen_is_str;
    if (cimy_is_at_least_wordpress35()) {
        cimy_switch_to_blog();
    }
    $options = cimy_get_options();
    if (!in_array("username", $options["wp_hidden_fields"])) {
        // ok username is empty, we are replacing it with the email, don't bother
        if (isset($errors->errors['empty_username'])) {
            unset($errors->errors['empty_username']);
        }
        // remove username exists error only if email exists error is there covering for us
        if (isset($errors->errors['username_exists']) && isset($errors->errors['email_exists'])) {
            unset($errors->errors['username_exists']);
        }
    }
    // code for confirmation email check
    if (!is_multisite() && $options["confirm_email"]) {
        $errors = cimy_check_user_on_signups($errors, $user_login, $user_email);
    }
    // avoid to save stuff if user is being added from: /wp-admin/user-new.php and shit WP 3.1 changed the value just to create new bugs :@
    if (!empty($_POST["action"]) && ($_POST["action"] == "adduser" || $_POST["action"] == "createuser")) {
        return $errors;
    }
    $my_user_level = $user_level;
    // -1 == anonymous
    if (!is_user_logged_in()) {
        $my_user_level = -1;
    }
    $extra_fields = get_cimyFields(false, true);
    $wp_fields = get_cimyFields(true);
    $from_profile = false;
    if (!empty($_POST["from"]) && $_POST["from"] == "profile") {
        $from_profile = true;
    }
    $i = 1;
    // do first for the WP fields then for EXTRA fields
    while ($i <= 2) {
        if ($i == 1) {
            $fields = $wp_fields;
            $prefix = $wp_fields_name_prefix;
        } else {
            $fields = $extra_fields;
            $prefix = $fields_name_prefix;
        }
        foreach ($fields as $thisField) {
            $field_id = $thisField['ID'];
            $name = $thisField['NAME'];
            $rules = $thisField['RULES'];
            $type = $thisField['TYPE'];
            $label = esc_html($thisField['LABEL']);
            $description = $thisField['DESCRIPTION'];
            $unique_id = $prefix . $field_id;
            // Usernames cannot be changed after the registration
            if ($i == 1 && $name == "USERNAME" && $from_profile) {
                continue;
            }
            // use WP input name for the username (always) or when updating the profile
            if ($i == 1 && ($name == "USERNAME" || $from_profile)) {
                $input_name = $wp_hidden_fields[strtolower($name)]['post_name'];
            } else {
                $input_name = $prefix . esc_attr($name);
            }
            $field_id_data = $input_name . "_" . $field_id . "_data";
            // if the current user LOGGED IN has not enough permissions to see the field, skip it
            if ($rules['show_level'] == 'view_cimy_extra_fields') {
                if (!current_user_can($rules['show_level'])) {
                    continue;
                }
            } else {
                if ($my_user_level < $rules['show_level']) {
                    continue;
                }
            }
            // if show_level == anonymous then do NOT ovverride other show_xyz rules
            if ($rules['show_level'] == -1) {
                // if we are updating the profile check correct rule
                if ($from_profile) {
                    // if flag to show the field in the profile is NOT activated, skip it
                    if (!$rules['show_in_profile']) {
                        continue;
                    }
                } else {
                    // we are registering new user
                    // if flag to show the field in the registration is NOT activated, skip it
                    if (!$rules['show_in_reg']) {
                        continue;
                    }
                }
            }
            // uploading a file is not supported when confirmation email is enabled (on MS is turned on by default yes)
            if ((is_multisite() || $options["confirm_email"]) && in_array($type, $cimy_uef_file_types)) {
                continue;
            }
            if ($from_profile) {
                if ($i == 1) {
                    // Do not bother with the rules if encountered an empty password field on profile update
                    if ($type == "password") {
                        continue;
                    }
                } else {
                    $old_value = $_POST[$input_name . "_" . $field_id . "_prev_value"];
                    // Hey, no need to check for rules if anyway I can't edit due to low permissions, neeeext!
                    if (cimy_uef_is_field_disabled($type, $rules['edit'], $old_value)) {
                        continue;
                    }
                }
            }
            if (isset($_POST[$input_name])) {
                if ($type == "dropdown-multi" && is_array($_POST[$input_name])) {
                    $value = stripslashes(implode(",", $_POST[$input_name]));
                } else {
                    $value = stripslashes($_POST[$input_name]);
                }
            } else {
                $value = "";
            }
            if ($type == "dropdown") {
                $ret = cimy_dropDownOptions($label, $value);
                $label = esc_html($ret['label']);
                $html = $ret['html'];
            }
            // upload of a file, avatar or picture
            if (in_array($type, $cimy_uef_file_types)) {
                // confirmation page
                if (!empty($_POST["register_confirmation"]) && $_POST["register_confirmation"] == 2) {
                    $file_size = $_POST[$field_id_data . "_size"];
                    $file_type1 = $_POST[$field_id_data . "_type"];
                    // this can be faked!
                    $old_file = "";
                    $del_old_file = "";
                } else {
                    if (!empty($_FILES[$input_name])) {
                        // filesize in Byte transformed in KiloByte
                        $file_size = $_FILES[$input_name]['size'] / 1024;
                        $file_type1 = $_FILES[$input_name]['type'];
                        // this can be faked!
                        $value = $_FILES[$input_name]['name'];
                        $old_file = $from_profile && !empty($_POST[$input_name . "_" . $field_id . "_prev_value"]) ? $_POST[$input_name . "_" . $field_id . "_prev_value"] : '';
                        $del_old_file = $from_profile && !empty($_POST[$input_name . "_del"]) ? $_POST[$input_name . "_del"] : '';
                    } else {
                        $file_size = 0;
                        $file_type1 = "";
                        $value = "";
                        $old_file = $from_profile && !empty($_POST[$input_name . "_" . $field_id . "_prev_value"]) ? $_POST[$input_name . "_" . $field_id . "_prev_value"] : '';
                        $del_old_file = $from_profile && !empty($_POST[$input_name . "_del"]) ? $_POST[$input_name . "_del"] : '';
                    }
                }
            }
            switch ($type) {
                case 'checkbox':
                    $value == 1 ? $value = "YES" : ($value = "NO");
                    break;
                case 'radio':
                    intval($value) == intval($field_id) ? $value = "YES" : ($value = "NO");
                    break;
            }
            // if the flag can be empty is NOT set OR the field is not empty then other check can be useful, otherwise skip all
            if (!$rules['can_be_empty'] || !empty($value)) {
                if ($i == 1 && $input_name == $prefix . "PASSWORD2") {
                    if ($value != $_POST[$prefix . "PASSWORD"]) {
                        $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . ' ' . __('does not match.', $cimy_uef_domain));
                    }
                }
                if ($rules['email'] && in_array($type, $rule_email)) {
                    if (!is_email($value)) {
                        $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . ' ' . __('hasn&#8217;t a correct email syntax.', $cimy_uef_domain));
                    }
                }
                if (!$rules['can_be_empty'] && in_array($type, $rule_canbeempty) && empty($value)) {
                    $empty_error = true;
                    // IF   1. it's a file type
                    // AND  2. there is an old one uploaded
                    // AND  3. this old one is not gonna be deleted
                    // THEN   do not throw the empty error.
                    if (in_array($type, $cimy_uef_file_types) && !empty($old_file) && empty($del_old_file)) {
                        $empty_error = false;
                    }
                    if ($empty_error) {
                        $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . ' ' . __('couldn&#8217;t be empty.', $cimy_uef_domain));
                    }
                }
                if (isset($rules['equal_to']) && in_array($type, $apply_equalto_rule)) {
                    $equalTo = $rules['equal_to'];
                    // 	if the type is not allowed to be case sensitive
                    // 	OR if case sensitive is not checked
                    // AND
                    // 	if the type is not allowed to be a regex
                    // 	OR if regex rule is not set
                    // THEN switch to uppercase
                    if ((!in_array($type, $rule_equalto_case_sensitive) || !$rules['equal_to_case_sensitive']) && (!in_array($type, $rule_equalto_regex) || !$rules['equal_to_regex'])) {
                        $value = strtoupper($value);
                        $equalTo = strtoupper($equalTo);
                    }
                    if ($rules['equal_to_regex']) {
                        $equalTo = $rules['equal_to_case_sensitive'] ? $equalTo . 'u' : $equalTo . 'iu';
                        if (!preg_match($equalTo, $value)) {
                            $equalmsg = " " . __("isn&#8217;t correct", $cimy_uef_domain);
                            $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . $equalmsg . '.');
                        }
                    } else {
                        if ($value != $equalTo) {
                            if ($type == "radio" || $type == "checkbox") {
                                $equalTo == "YES" ? $equalTo = __("YES", $cimy_uef_domain) : __("NO", $cimy_uef_domain);
                            }
                            if ($type == "password") {
                                $equalmsg = " " . __("isn&#8217;t correct", $cimy_uef_domain);
                            } else {
                                $equalmsg = ' ' . __("should be", $cimy_uef_domain) . ' ' . esc_html($equalTo);
                            }
                            $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . $equalmsg . '.');
                        }
                    }
                }
                // CHECK IF IT IS A REAL PICTURE
                if (in_array($type, $cimy_uef_file_images_types)) {
                    $allowed_mime_types = get_allowed_mime_types();
                    $validate = wp_check_filetype($value, $allowed_mime_types);
                    $file_type2 = "";
                    if (!empty($validate['type'])) {
                        $file_type2 = $validate['type'];
                    }
                    if ((stristr($file_type1, "image/") === false || stristr($file_type2, "image/") === false) && !empty($value)) {
                        $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . ' ' . __('should be an image.', $cimy_uef_domain));
                    }
                } else {
                    if (in_array($type, $cimy_uef_file_types)) {
                        $allowed_mime_types = get_allowed_mime_types();
                        $validate = wp_check_filetype($value, $allowed_mime_types);
                        $file_type2 = "";
                        if (!empty($validate['type'])) {
                            $file_type2 = $validate['type'];
                        }
                        if (empty($file_type2) && !empty($value)) {
                            $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . ' ' . __('does not accept this file type.', $cimy_uef_domain));
                        }
                    }
                }
                // MIN LEN
                if (isset($rules['min_length'])) {
                    $minlen = intval($rules['min_length']);
                    if (in_array($type, $cimy_uef_file_types)) {
                        if ($file_size < $minlen) {
                            $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . ' ' . __('couldn&#8217;t have size less than', $cimy_uef_domain) . ' ' . $minlen . ' KB.');
                        }
                    } else {
                        if (!in_array($type, $rule_maxlen_is_str)) {
                            if (cimy_strlen($value) < $minlen) {
                                $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . ' ' . __('couldn&#8217;t have length less than', $cimy_uef_domain) . ' ' . $minlen . '.');
                            }
                        }
                    }
                }
                // EXACT LEN
                if (isset($rules['exact_length'])) {
                    $exactlen = intval($rules['exact_length']);
                    if (in_array($type, $cimy_uef_file_types)) {
                        if ($file_size != $exactlen) {
                            $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . ' ' . __('couldn&#8217;t have size different than', $cimy_uef_domain) . ' ' . $exactlen . ' KB.');
                        }
                    } else {
                        if (!in_array($type, $rule_maxlen_is_str)) {
                            if (cimy_strlen($value) != $exactlen) {
                                $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . ' ' . __('couldn&#8217;t have length different than', $cimy_uef_domain) . ' ' . $exactlen . '.');
                            }
                        }
                    }
                }
                // MAX LEN
                if (isset($rules['max_length'])) {
                    $maxlen = intval($rules['max_length']);
                    if (in_array($type, $cimy_uef_file_types)) {
                        if ($file_size > $maxlen) {
                            $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . ' ' . __('couldn&#8217;t have size more than', $cimy_uef_domain) . ' ' . $maxlen . ' KB.');
                        }
                    } else {
                        if (!in_array($type, $rule_maxlen_is_str)) {
                            if (cimy_strlen($value) > $maxlen) {
                                $errors->add($unique_id, '<strong>' . __("ERROR", $cimy_uef_domain) . '</strong>: ' . $label . ' ' . __('couldn&#8217;t have length more than', $cimy_uef_domain) . ' ' . $maxlen . '.');
                            }
                        }
                    }
                }
            }
        }
        $i++;
    }
    if ($options['confirm_form']) {
        // this is executed to test registration for errors, to avoid a real registration we put a fake error
        if (empty($errors->errors) && isset($_POST["register_confirmation"]) && $_POST["register_confirmation"] == 1) {
            $errors->add('register_confirmation', 'true');
        }
    }
    cimy_switch_current_blog();
    return $errors;
}