Im newbie in php, so i need your help, if you can help me. I have the following code:
if ($system['select_user_group_enabled']) {
$custom_user_group = ($args['custom_user_group'] != '0' && $this->check_user_group($args['custom_user_group'])) ? $args['custom_user_group'] : '0';
} else {
$custom_user_group = ($system['default_custom_user_group'] != '0' && $this->check_user_group($system['default_custom_user_group'])) ? $system['default_custom_user_group'] : '0';
}
This code will show select option in register form, to choose what group you want to join. Everything is ok with code, but i need to be required.
Hope you can understand me. Thank you!
FULL PHP CODE
/* ------------------------------- */
/* User Sign (in|up|out) ✅ */
/* ------------------------------- */
/**
* sign_up
*
* @param array $args
* @param array $device_info
* @return void
*/
public function sign_up($args = [], $device_info = [])
{
global $db, $system, $date;
/* prepare */
$args['from_web'] = (isset($args['from_web'])) ? $args['from_web'] : true;
/* check invitation code */
if ($system['invitation_enabled']) {
if (!$this->check_invitation_code($args['invitation_code'])) {
throw new ValidationException(__("The invitation code is invalid or expired"));
}
}
/* check IP */
$this->_check_ip();
/* validate */
if ($system['show_usernames_enabled']) {
$args['first_name'] = $args['username'];
$args['last_name'] = $args['username'];
} else {
if (!valid_name($args['first_name'])) {
throw new ValidationException(__("Your first name contains invalid characters"));
}
if (strlen($args['first_name']) < $system['name_min_length']) {
throw new ValidationException(__("Your first name must be at least") . " " . $system['name_min_length'] . " " . __("characters long. Please try another"));
}
if (!valid_name($args['last_name'])) {
throw new ValidationException(__("Your last name contains invalid characters"));
}
if (strlen($args['last_name']) < $system['name_min_length']) {
throw new ValidationException(__("Your last name must be at least") . " " . $system['name_min_length'] . " " . __("characters long. Please try another"));
}
}
if (!valid_username($args['username'])) {
throw new ValidationException(__("Please enter a valid username (a-z0-9_.) with minimum 3 characters long"));
}
if ($this->reserved_username($args['username'])) {
throw new ValidationException(__("You can't use") . " " . $args['username'] . " " . __("as username"));
}
if ($this->check_username($args['username'])) {
throw new ValidationException(__("Sorry, it looks like") . " " . $args['username'] . " " . __("belongs to an existing account"));
}
if (!valid_email($args['email'])) {
throw new ValidationException(__("Please enter a valid email address"));
}
if ($this->check_email($args['email'])) {
throw new ValidationException(__("Sorry, it looks like") . " " . $args['email'] . " " . __("belongs to an existing account"));
}
if ($system['activation_enabled'] && $system['activation_type'] == "sms") {
if (is_empty($args['phone'])) {
throw new ValidationException(__("Please enter a valid phone number"));
}
if ($this->check_phone($args['phone'])) {
throw new ValidationException(__("Sorry, it looks like") . " " . $args['phone'] . " " . __("belongs to an existing account"));
}
} else {
$args['phone'] = 'null';
}
if (strlen($args['password']) < 6) {
throw new ValidationException(__("Your password must be at least 6 characters long. Please try another"));
}
if (strlen($args['password']) > 64) {
throw new ValidationException(__("Your password must be less than 64 characters long. Please try another"));
}
$args['gender'] = ($system['genders_disabled']) ? 1 : $args['gender'];
if (!$system['genders_disabled'] && !$this->check_gender($args['gender'])) {
throw new ValidationException(__("Please select a valid gender"));
}
/* check age restriction */
if ($system['age_restriction']) {
if (!in_array($args['birth_month'], range(1, 12))) {
throw new ValidationException(__("Please select a valid birth month (1-12)"));
}
if (!in_array($args['birth_day'], range(1, 31))) {
throw new ValidationException(__("Please select a valid birth day (1-31)"));
}
if (!in_array($args['birth_year'], range(1925, 2025))) {
throw new ValidationException(__("Please select a valid birth year (1925-2025)"));
}
if (date("Y") - $args['birth_year'] < $system['minimum_age']) {
throw new ValidationException(__("Sorry, You must be") . " " . $system['minimum_age'] . " " . __("years old to register"));
}
$args['birth_date'] = $args['birth_year'] . '-' . $args['birth_month'] . '-' . $args['birth_day'];
} else {
$args['birth_date'] = 'null';
}
/* set custom fields */
$custom_fields = $this->set_custom_fields($args);
/* check reCAPTCHA */
if ($system['reCAPTCHA_enabled'] && $args['from_web']) {
$recaptcha = new \ReCaptcha\ReCaptcha($system['reCAPTCHA_secret_key'], new \ReCaptcha\RequestMethod\CurlPost());
$resp = $recaptcha->verify($args['g-recaptcha-response'], get_user_ip());
if (!$resp->isSuccess()) {
throw new ValidationException(__("The security check is incorrect. Please try again"));
}
}
/* check newsletter agreement */
$newsletter_agree = (isset($args['newsletter_agree'])) ? '1' : '0';
/* check privacy agreement */
if (!isset($args['privacy_agree']) && $args['from_web']) {
throw new ValidationException(__("You must read and agree to our terms and privacy policy"));
}
/* generate verification code */
$email_verification_code = ($system['activation_enabled'] && $system['activation_type'] == "email") ? get_hash_key(6, true) : 'null';
$phone_verification_code = ($system['activation_enabled'] && $system['activation_type'] == "sms") ? get_hash_key(6, true) : 'null';
/* set custom user group */
if ($system['select_user_group_enabled']) {
$custom_user_group = ($args['custom_user_group'] != '0' && $this->check_user_group($args['custom_user_group'])) ? $args['custom_user_group'] : '0';
} else {
$custom_user_group = ($system['default_custom_user_group'] != '0' && $this->check_user_group($system['default_custom_user_group'])) ? $system['default_custom_user_group'] : '0';
}
/* check user approved */
$user_approved = ($system['users_approval_enabled']) ? '0' : '1';
/* register user */
$db->query(sprintf("INSERT INTO users (user_group_custom, user_name, user_email, user_phone, user_password, user_firstname, user_lastname, user_gender, user_birthdate, user_registered, user_email_verification_code, user_phone_verification_code, user_newsletter_enabled, user_approved) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", secure($custom_user_group), secure($args['username']), secure($args['email']), secure($args['phone']), secure(_password_hash($args['password'])), secure(ucwords($args['first_name'])), secure(ucwords($args['last_name'])), secure($args['gender']), secure($args['birth_date']), secure($date), secure($email_verification_code), secure($phone_verification_code), secure($newsletter_agree), secure($user_approved)));
/* get user_id */
$user_id = $db->insert_id;
/* set default privacy */
$this->_set_default_privacy($user_id);
/* insert custom fields values */
if ($custom_fields) {
foreach ($custom_fields as $field_id => $value) {
$db->query(sprintf("INSERT INTO custom_fields_values (value, field_id, node_id, node_type) VALUES (%s, %s, %s, 'user')", secure($value), secure($field_id, 'int'), secure($user_id, 'int')));
}
}
/* send activation */
if ($system['activation_enabled']) {
if ($system['activation_type'] == "email") {
/* prepare activation email */
$subject = __("Just one more step to get started on") . " " . html_entity_decode(__($system['system_title']), ENT_QUOTES);
$body = get_email_template("activation_email", $subject, ["first_name" => $args['first_name'], "last_name" => $args['last_name'], "email_verification_code" => $email_verification_code]);
/* send email */
if (!_email($args['email'], $subject, $body['html'], $body['plain'])) {
throw new Exception(__("Activation email could not be sent") . ", " . __("But you can login now"));
}
} else {
/* prepare activation SMS */
$message = __($system['system_title']) . " " . __("Activation Code") . ": " . $phone_verification_code;
/* send SMS */
if (!sms_send($args['phone'], $message)) {
throw new Exception(__("Activation SMS could not be sent") . ", " . __("But you can login now"));
}
}
} else {
/* affiliates system (as activation disabled) */
$this->process_affiliates("registration", $user_id);
}
/* update invitation code */
if ($system['invitation_enabled']) {
$this->update_invitation_code($args['invitation_code'], $user_id);
}
/* auto connect */
$this->auto_friend($user_id);
$this->auto_follow($user_id);
$this->auto_like($user_id);
$this->auto_join($user_id);
/* user approval system */
if ($system['users_approval_enabled']) {
/* send notification to admins */
$this->notify_system_admins("pending_user", true, $user_id);
}
/* set authentication */
if ($args['from_web']) {
$this->_set_authentication_cookies($user_id);
} else {
/* create JWT */
$jwt = $this->_set_authentication_JWT($user_id, $device_info);
/* create new user object */
$user = new User($jwt);
return ['token' => $jwt, 'user' => $user->_data];
}
}
HTML REGISTER FORM
{if $system['select_user_group_enabled'] && $user_groups}
<!-- user group -->
<div class="form-floating">
<select class="form-select" name="custom_user_group">
<option value="0">{__("SELECT PROFILE")}:</option>
{foreach $user_groups as $user_group}
<option value="{$user_group['user_group_id']}">{$user_group['user_group_title']}</option>
{/foreach}
</select>
<label>{__("SELECT PROFILE")}</label>
</div>
<!-- user group -->
{/if}
Those codes are part of simple php, not part of any framework!
For server-side validation you can write (in your /* validate */
area):
if ($args['custom_user_group'] == "0")
{
throw new ValidationException(__("Please enter a custom user group"));
}
I'm assuming that you don't want the SELECT PROFILE option to be considered valid. So the code checks if the submitted value for the custom_user_group
field is 0
- that is the value of the "SELECT PROFILE" <option
in the HTML. If it's set to that 0
value, then it fails validation because the user did not choose one of the other options.