File: /var/www/selfanalyse/wp-content/plugins/psychometrics/api/psy_school_api.php
<?php
// ini_set('display_errors',1);
// ini_set('display_startup_errors',1);
// error_reporting(E_ALL);
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
include("../../../../wp-config.php");
global $wpdb;
$PSY_SCHOOL = $wpdb->prefix . PSY_SCHOOL;
$PSY_SCHOOL_STUDENT = $wpdb->prefix . PSY_SCHOOL_STUDENT;
$PSY_PLANS = $wpdb->prefix . PSY_PLANS;
$PSY_SUBSCRIPTION = $wpdb->prefix . PSY_SUBSCRIPTION;
$PSY_USERS_DETAILS = $wpdb->prefix . PSY_USERS_DETAILS;
$json = file_get_contents('php://input');
$data = json_decode($json);
$type = $data->type;
$date = date('Y-m-d H:i:s');
if (is_user_logged_in()) {
global $current_user;
$logg_user_id = $current_user->ID;
} else {
$logg_user_id = 0;
}
//encoded parameters
function random_strings($length_of_string)
{
$str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZab!cdefghijklmnopqrstuvwxyz';
return substr(str_shuffle($str_result), 0, $length_of_string);
}
//decode function
function decodeReq($value)
{
$str = substr($value, 7);
$str1 = substr($str, 0, -6);
$decode = base64_decode($str1);
return $decode;
}
if ($type == 'school-check') {
$email = $data->p_ema;
$school = $data->p_sch;
$getSchoolData = $wpdb->get_row("SELECT id,name,email,class,section,status,reg_status FROM $PSY_SCHOOL_STUDENT where email='$email' and school_id='$school' and status='1'");
$id = $getSchoolData->id;
$name = $getSchoolData->name;
$email = $getSchoolData->email;
$class = $getSchoolData->class;
$section = $getSchoolData->section;
$status = $getSchoolData->status;
$reg_status = $getSchoolData->reg_status;
//ref_id
$encodedRefId = rtrim(strtr(base64_encode($id), '+/', '-_'), '=');
$shuffle = random_strings(7);
$backShuffle = random_strings(6);
$ref_id = $shuffle . $encodedRefId . $backShuffle;
if ($id != '') {
if ($reg_status == 0) {
if ($logg_user_id == 0) {
if ($status == 1) {
$dataArray = array("status" => 1, "p_i" => $ref_id, "p_n" => $name, "p_e" => $email, "p_c" => $class, "p_s" => $section);
} else {
$dataArray = array("status" => 0, "message" => "Not a valid email address");
}
} else {
$checkIfExist = $wpdb->get_var("SELECT count(subscription_id) FROM $PSY_SUBSCRIPTION where user_id='$logg_user_id'");
if ($checkIfExist != 0) {
$dataArray = array("status" => 0, "message" => "Not a valid email address");
} else {
$dataArray = array("status" => 1, "p_i" => $ref_id, "p_n" => $name, "p_e" => $email, "p_c" => $class, "p_s" => $section);
}
}
} else {
$dataArray = array("status" => 0, "message" => "You are already registered before.");
}
} else {
$dataArray = array("status" => 0, "message" => "Not a valid email address");
}
} else if ($type == 'submit') {
$p_si = $data->p_si;
$e_mob = $data->e_mob;
$e_dob = $data->e_dob;
$e_gen = $data->e_gen;
$state_id = $data->state_id;
$city_id = $data->city_id;
$mainId = decodeReq($p_si);
$getStd = $wpdb->get_row("SELECT id,name,email,class,section FROM $PSY_SCHOOL_STUDENT where id='$mainId'");
$s_id = $getStd->id;
$s_name = $getStd->name;
$s_email = $getStd->email;
$s_class = $getStd->class;
$s_section = $getStd->section;
$s_plan = 3;
if ($s_id != '') {
if ($logg_user_id == 0) {
/**check user */
if (username_exists($e_mob)) {
$dataArray = array("status" => 0, "message" => "This number is already registered");
} else {
$password = str_shuffle(bin2hex(openssl_random_pseudo_bytes(4)));
/**register user */
$user_id = wp_create_user($e_mob, $password, $s_email);
if (is_wp_error($user_id)) {
$error = $user_id->get_error_message();
$dataArray = array("status" => 0, "message" => $error);
} else {
$inserTDetails = $wpdb->query("INSERT into $PSY_USERS_DETAILS set user_id='$user_id', first_name='$s_name', mobile_number='$e_mob', email_address='$s_email', gender_id='$e_gen', date_of_birth='$e_dob', state_id='$state_id',city_id='$city_id',current_education_level='$s_class', source='SP', source_id='0'");
/****subscribe for plans */
$get_exams = $wpdb->get_row("SELECT reference_id FROM $PSY_PLANS where plan_id='$s_plan'");
$exam_id = explode(",", $get_exams->reference_id);
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='$s_plan', exam_id='$e_id', test_type='$test_type', purchase_date='$date', status='1', active_status='1', active_date='$date', source='SM', source_id='0'";
if ($wpdb->query($insertSubscription)) {
$last_id = $wpdb->insert_id;
$order_id = $last_id;
}
}
if ($order_id != '') {
$updateStudent = $wpdb->query("UPDATE $PSY_SCHOOL_STUDENT set user_id='$user_id', ps_id='$s_plan', reg_status='1',reg_date='$date' where id='$mainId'");
}
$dataArray = array("status" => 1, "message" => "You are successfully enrolled!");
/*********send welcome register message***** */
include_once 'psy_mail_sms_functions.php';
sendRegEmail($e_mob, $s_email, $s_name, $password);
sendCredentials($e_mob, $password);
}
}
} else {
/****subscribe for plans */
$get_exams = $wpdb->get_row("SELECT reference_id FROM $PSY_PLANS where plan_id='$s_plan'");
$exam_id = explode(",", $get_exams->reference_id);
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='$logg_user_id', plan_id='$s_plan', exam_id='$e_id', test_type='$test_type', purchase_date='$date', status='1', active_status='1', active_date='$date', source='SM', source_id='0'";
if ($wpdb->query($insertSubscription)) {
$last_id = $wpdb->insert_id;
$order_id = $last_id;
}
}
if ($order_id != '') {
$updateStudent = $wpdb->query("UPDATE $PSY_SCHOOL_STUDENT set user_id='$logg_user_id', ps_id='$s_plan', reg_status='1',reg_date='$date' where id='$mainId'");
}
$dataArray = array("status" => 1, "message" => "You are successfully enrolled!");
}
}
}
echo json_encode($dataArray);