HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux ip-172-31-18-78 6.17.0-1017-aws #17~24.04.1-Ubuntu SMP Tue May 26 21:09:53 UTC 2026 aarch64
User: ubuntu (1000)
PHP: 8.2.31
Disabled: NONE
Upload Files
File: /var/www/selfanalyse/wp-content/plugins/psychometrics/api/psy_wht_user_register.php
<?php

/***
 * @purpose : Register user for SP whitelabel
 * @author Rohit Kashyap
 * @since 31/07/2020 >> dd/mm/yyyy
 * @version 0.1
 * @copyright Copyright (C) 2020 Graposs Edutech Ltd. All rights reserved.
 * @license Graposs Edutech Ltd.
 */

/***
 * define header and include config
 */
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
include("../../../../wp-config.php");

global $wpdb;
$PSY_USERS = $wpdb->prefix . PSY_USERS;
$PSY_USERS_DETAILS = $wpdb->prefix . PSY_USERS_DETAILS;
$PSY_SUBSCRIPTION = $wpdb->prefix . PSY_SUBSCRIPTION;
$PSY_PLANS = $wpdb->prefix . PSY_PLANS;
$PSY_STATES = $wpdb->prefix . PSY_STATES;

$date = date('Y-m-d H:i:s');
include("/var/www/selfanalyse/wp-content/plugins/psychometrics/api/psy_mail_sms_functions.php");

$type = "";
$ref_id = $_REQUEST['id'];
$whitelabel_id = $_REQUEST['whitelabel_id'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$mobile = $_REQUEST['mobile'];
$class_id = $_REQUEST['class_id'];
$plan_id = $_REQUEST['plan_id'];
$state_id = $_REQUEST['state_id'];
$city_id = $_REQUEST['city_id'];
$password = str_shuffle(bin2hex(openssl_random_pseudo_bytes(4)));
$center_lab_id = $_REQUEST['center_lab_id'];

$userExist = $wpdb->get_var("SELECT count(ID) FROM $PSY_USERS WHERE user_login ='$mobile' OR user_email ='$email'");
if ($userExist > 0) {
	$array = array("status" => 0, "message" => 'You are already register with this mobile number.', "type" =>$type, "ref_id" => $ref_id );
} else {
    $type = 'register-user';
}

if ($type == 'register-user') {

    if ($name != '' && $email != '' && $mobile != '' && $plan_id != '' && $plan_id != 0) {

        if (username_exists($mobile)) {
            $array = array("status" => 0, "message" => "This number is already registered", "type" =>$type, "ref_id" => $ref_id );
        } else if (email_exists($email)) {
            $array = array("status" => 0, "message" => "This email id is already registered", "type" =>$type, "ref_id" => $ref_id );
        } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $array = array("status" => 0, "message" => "Please enter valid email address", "type" =>$type, "ref_id" => $ref_id );
        } else {

            $user_id = wp_create_user($mobile, $password, $email);
            $username = $email;
            if (is_wp_error($user_id)) {
                $error = $user->get_error_message();
                $array = array("status" => 0, "message" => $error, "type" =>$type, "ref_id" => $ref_id  , "upID" => $plan_id, "ucID" => $class_id);
            } else {

                /**check ref */
                $checkRef = $wpdb->get_var("SELECT count(subscription_id) FROM $PSY_SUBSCRIPTION WHERE user_id='$user_id' and sp_ref_id='$ref_id'");

                $user_state = $wpdb->get_var("SELECT state_name FROM $PSY_STATES where state_id='$state_id'");

                $insertUserDetails = $wpdb->query("INSERT INTO $PSY_USERS_DETAILS SET user_id='$user_id', first_name='$name', mobile_number='$mobile', email_address='$email', gender_id='1',state_id='$state_id', city_id='$city_id', current_education_level='$class_id', source='Whitelabel', source_id='$whitelabel_id', center_id='$center_lab_id' ");

                //insert plan
                $get_exams = $wpdb->get_row("SELECT reference_id,plan_price FROM $PSY_PLANS WHERE plan_id='$plan_id'");

                $exam_id = explode(",", $get_exams->reference_id);
                $amount = $get_exams->plan_price;
                $planSucess = 0;
                if ($checkRef == 0) {
                    foreach ($exam_id as $e_id) {
                        if ($e_id == '48282') {
                            $test_type = 'APT';
                        } else if ($e_id == '48305') {
                            $test_type = 'PER';
                        } else if ($e_id == '48328') {
                            $test_type = 'INT';
                        }
                        $insertSubscription = "INSERT INTO $PSY_SUBSCRIPTION SET user_id='$user_id', plan_id='$plan_id', exam_id='$e_id', test_type='$test_type', purchase_date='$date', status='1', active_status='1', active_date='$date', source='Whitelabel', source_id='$whitelabel_id',sp_ref_id='$ref_id'";
                        if ($wpdb->query($insertSubscription)) {
                            $planSucess += 1;
                        }
                    }
                }

                if ($planSucess == count($exam_id)) {
					
                    /********create invoice data */
                    include_once '/var/www/selfanalyse/wp-content/plugins/psychometrics/api/psy_functions.php';
                    invoice('Whitelabel Plan Purchase','WP',$plan_id,$ref_id,$amount,'',$user_id,$name,$email,'',$user_state,$date);
					sendTestPurchaseSMS($mobile, $name);
                    sendRegEmail($username,$email,$name,$password);
                    sendTestPurchaseEmail($email, $name);
                    sendCredentials($mobile,$password);
                    $array = array("status" => 1, "message" => 'Success', "type" =>$type, "ref_id" => $ref_id, "upID" => $plan_id, "ucID" => $class_id );
                    
                } else {
                    $array = array("status" => 0, "message" => 'Failed', "type" =>$type, "ref_id" => $ref_id, "upID" => $plan_id, "ucID" => $class_id );
                }
            }
        }
    } else {
        $array = array("status" => 0, "message" => 'Name,Email,Mobile is required', "type" =>$type, "ref_id" => $ref_id , "ref_id" => $ref_id, "upID" => $plan_id, "ucID" => $class_id );
    }
}

if ($type == 'upgrade-user') {

    if (username_exists($mobile)) {

        $user = get_user_by('login', $mobile);
        $user_id = $user->ID;

        /**check ref */
        $checkRef = $wpdb->get_var("SELECT count(subscription_id) FROM $PSY_SUBSCRIPTION where user_id='$user_id' and sp_ref_id='$ref_id'");
        //insert plan
        $get_exams = $wpdb->get_row("SELECT reference_id FROM $PSY_PLANS where plan_id='$plan_id'");
        $userData = $wpdb->get_row("SELECT first_name,email_address,mobile_number FROM $PSY_USERS_DETAILS where user_id='$user_id'");

        $name = $userData->first_name;
        $email = $userData->email_address;
        $mobile = $userData->mobile_number;

        $exam_id = explode(",", $get_exams->reference_id);
        $planSucess = 0;
        if ($checkRef == 0) {
            foreach ($exam_id as $e_id) {
                if ($e_id == '48282') {
                    $test_type = 'APT';
                } else if ($e_id == '48305') {
                    $test_type = 'PER';
                } else if ($e_id == '48328') {
                    $test_type = 'INT';
                }
                $insertSubscription = "INSERT into $PSY_SUBSCRIPTION set user_id='$user_id', plan_id='$plan_id', exam_id='$e_id', test_type='$test_type', purchase_date='$date', status='1', active_status='1', active_date='$date', source='Whitelabel', source_id='$whitelabel_id',sp_ref_id='$ref_id'";
                if ($wpdb->query($insertSubscription)) {
                    $planSucess += 1;
                }
            }
        }


        if ($planSucess == count($exam_id)) {
            $array = array("status" => 1, "message" => 'Success', "type" =>$type, "ref_id" => $ref_id );
            sendTestPurchaseSMS($mobile, $name);
			sendTestPurchaseEmail($email, $name);
        } else {
            $array = array("status" => 0, "message" => 'Failed', "type" =>$type, "ref_id" => $ref_id );
        }
    } else {

        $array = array("status" => 0, "message" => 'No user found', "type" =>$type, "ref_id" => $ref_id );
    }
}

echo json_encode($array);