File: /home/k/kulikosl/kulikosl.beget.tech/public_html/back/zootheme 2612/functions/auth.php
<?php
add_action('init', 'start_session', 1);
function start_session() {
if(!session_id()) {
session_start();
}
}
add_action('wp_logout', 'end_session');
add_action('wp_login', 'end_session');
add_action('end_session_action', 'end_session');
function end_session() {
session_destroy ();
}
add_action( 'init', 'add_my_endpoint' );
function add_my_endpoint() {
add_rewrite_endpoint( 'register', EP_ALL);
add_rewrite_endpoint( 'login', EP_ALL);
add_rewrite_endpoint( 'offer-seats', EP_ALL);
add_rewrite_endpoint( 'googleauth', EP_ALL);
add_rewrite_endpoint( 'yandexauth', EP_ALL);
add_rewrite_endpoint( 'vkauth', EP_ALL);
add_rewrite_endpoint( 'cabinet', EP_ALL);
add_rewrite_endpoint( 'rides', EP_ALL);
add_rewrite_endpoint( 'search-car-sharing', EP_ALL);
add_rewrite_endpoint( 'search-results', EP_ALL);
add_rewrite_endpoint( 'booking', EP_ALL);
add_rewrite_endpoint( 'add-parcel', EP_ALL);
add_rewrite_endpoint( 'my-parcels', EP_ALL);
add_rewrite_endpoint( 'recover', EP_ALL);
}
add_action('init', function()
{
$url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
if ( $url_path === 'register' ) {
$load = locate_template('/templates/register.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'recover' ) {
$load = locate_template('/templates/recover.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'login' ) {
$load = locate_template('/templates/login.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'offer-seats' ) {
$load = locate_template('/templates/offer-seats.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'googleauth' ) {
$load = locate_template('/templates/googleauth.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'yandexauth' ) {
$load = locate_template('/templates/yandexauth.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'vkauth' ) {
$load = locate_template('/templates/vkauth.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'cabinet' ) {
$load = locate_template('/templates/cabinet.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'rides' ) {
$load = locate_template('/templates/rides.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'search-car-sharing' ) {
$load = locate_template('/templates/search-car-sharing.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'search-results' ) {
$load = locate_template('/templates/search-results.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'booking' ) {
$load = locate_template('/templates/booking.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'add-parcel' ) {
$load = locate_template('/templates/add-parcel.php', true);
if ($load) {
exit();
}
}
if ( $url_path === 'my-parcels' ) {
$load = locate_template('/templates/my-parcels.php', true);
if ($load) {
exit();
}
}
});
function myenter(){
if (wp_verify_nonce( $_POST['veteran'], 'veteran') ){
$_SESSION['auth']=array();
$email=$_POST['email'];
global $wpdb;
$s="SELECT
a.ID, b.post_id
FROM $wpdb->posts a
LEFT JOIN
$wpdb->postmeta b
ON a.ID = b.post_id
WHERE b.meta_key='email'
AND b.meta_value='$email'
AND a.post_type='users'
";
$sel=$wpdb->get_results($s,ARRAY_A);
$code=rand(1111,9999);
if($sel){
//пользователь есть - авторизуем
$_SESSION['auth']['exists']='yes';
$_SESSION['auth']['email']=$email;
$_SESSION['auth']['action']='login';
$_SESSION['auth']['id']=$sel[0]['ID'];
$_SESSION['auth']['code']=$code=rand(1111,9999);
$_SESSION['auth']['code_time']=current_time('timestamp');
$_SESSION['auth']['step']=5;
$_SESSION['auth']['role']=get_post_meta($sel[0]['ID'],'status',1);
$_SESSION['auth']['allow']='no';
write_log($sel[0]['ID'],'Авторизация','Пользователь авторизовался');
}
else{
$_SESSION['auth']['exists']='no';
$_SESSION['auth']['email']=$email;
$_SESSION['auth']['action']='register';
$_SESSION['auth']['id']=$sel[0]['ID'];
$_SESSION['auth']['code']=$code=rand(1111,9999);
$_SESSION['auth']['code_time']=current_time('timestamp');
$_SESSION['auth']['step']=1;
$_SESSION['auth']['allow']='no';
}
}
$title='Авторизация на сайте';
$message='Код для входа: <div style="font-size:24px; color:#2C7BE5; margin-top:10px">'.$code.'</div>';
write_log($sel[0]['ID'],'Авторизация','Выслан код');
send_my_mail($email,'admin',$title,$message);
//send_my_mail('sai@solorum.ru','admin',$title,$message);
//send_my_mail('dontbs2004@yandex.ru','admin',$title,$message);
//send_my_mail('petrova@vsezapobedu.com','admin',$title,$message);
//send_my_mail('art@solorum.ru','admin',$title,$message);
wp_die();
}
add_action('wp_ajax_enter', 'myenter');
add_action('wp_ajax_nopriv_enter', 'myenter');
function mycheckcode(){
$code=$_POST['code'];
if(isset($_SESSION['auth']['code'])){
if($code==$_SESSION['auth']['code']){
if($_SESSION['auth']['action']=='register'){
$_SESSION['auth']['step']=2;
echo '/register/';
}
else{
$_SESSION['auth']['allow']='yes';
echo '/veterans/';
}
}
else{
echo 'no';
}
}
else{
echo 'no';
}
wp_die();
}
add_action('wp_ajax_checkcode', 'mycheckcode');
add_action('wp_ajax_nopriv_checkcode', 'mycheckcode');
function mygetcode(){
if(isset($_SESSION['auth'])){
$code=rand(1111,9999);
$email=$_SESSION['auth']['email'];
$_SESSION['auth']['code']=$code;
$title='Авторизация на сайте';
$message='Код для входа: <div style="font-size:24px; color:#2C7BE5; margin-top:10px">'.$code.'</div>';
send_my_mail($email,'admin',$title,$message);
//send_my_mail('sai@solorum.ru','admin',$title,$message);
//send_my_mail('dontbs2004@yandex.ru','admin',$title,$message);
//send_my_mail('petrova@vsezapobedu.com','admin',$title,$message);
//send_my_mail('art@solorum.ru','admin',$title,$message);
}
wp_die();
}
add_action('wp_ajax_getcode', 'mygetcode');
add_action('wp_ajax_nopriv_getcode', 'mygetcode');
function myreg1(){
//$_SESSION['auth']=array();
if(isset($_SESSION['auth'])){
$_SESSION['auth']['step']=3;
foreach($_POST['extra'] as $key=>$val){
$_SESSION['auth'][$key]=wp_strip_all_tags($val);
}
$_SESSION['auth']['role']='moderation';
}
wp_die();
}
add_action('wp_ajax_reg1', 'myreg1');
add_action('wp_ajax_nopriv_reg1', 'myreg1');
function myreg2(){
global $wpdb;
if(isset($_SESSION['auth'])){
$_SESSION['auth']['step']=4;
foreach($_POST['extra'] as $key=>$val){
$_SESSION['auth'][$key]=wp_strip_all_tags($val);
}
$last_name=$_SESSION['auth']['last_name'];
$first_name=$_SESSION['auth']['first_name'];
$patronymic=$_SESSION['auth']['patronymic'];
$email=$_SESSION['auth']['email'];
$phone=$_SESSION['auth']['phone'];
$country=$_SESSION['auth']['country'];
$region=$_SESSION['auth']['region'];
$city=$_SESSION['auth']['city'];
$sel=$wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key='email' AND meta_value='$email'",ARRAY_A);
if($sel){
if(get_post_type($sel[0]['post_id'])=='users')die();
}
$post_data = array(
'post_title' => $last_name.' '.$first_name.' '.$patronymic,
'post_status' => 'publish',
'post_type' => 'users',
);
echo $post_id = wp_insert_post( wp_slash($post_data) );
if($post_id)
{
global $wpdb;
$wpdb->query("UPDATE $wpdb->posts SET post_name='$post_id' WHERE ID='$post_id'");
update_post_meta($post_id,'last_name',$last_name);
update_post_meta($post_id,'first_name',$first_name);
update_post_meta($post_id,'patronymic',$patronymic);
update_post_meta($post_id,'email',$email);
update_post_meta($post_id,'phone',$phone);
update_post_meta($post_id,'country',$country);
update_post_meta($post_id,'region',$region);
update_post_meta($post_id,'city',$city);
update_post_meta($post_id,'region_name',get_region(get_post_meta($post_id,'region',1)));
update_post_meta($post_id,'status','moderation');
update_post_meta($post_id,'status_title','На модерации');
$_SESSION['auth']['id']=$post_id;
write_log($post_id,'Регистрация','Новый пользователь');
}
}
wp_die();
}
add_action('wp_ajax_reg2', 'myreg2');
add_action('wp_ajax_nopriv_reg2', 'myreg2');