<?php
if(isset($_GET['filename']) && $_GET['filename']!= "" && isset($_GET['inFolder']) ) {
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if (preg_match($email_exp, $_GET['filename'])) {
        $_GET['filename'] = filter_var($_GET['filename'], FILTER_SANITIZE_EMAIL);
        $flag = true;
    } else {
        $flag = false;
        $ret->status = "error";
        $ret->msg="Invalid Email Id!!";
    }

    if($flag) {
        $users = glob(APP_DIR.'/CONTENT/ROOT_URI/Admin/users/*');
        $user_arr = array();
        for($i = 0; $i < count($users); $i++) {
            $new_user = explode('/', $users[$i]);
            $new_user = end($new_user);
            $user_arr[$i] = $new_user;
        }
        if (in_array($_GET['filename'], $user_arr)) {
            $ret->status = "error";
            $ret->msg="Agent already exists!!";
        } else {
            $FC = file_put_contents(APP_DIR.'/CONTENT/ROOT_URI/Admin/'.$_GET['inFolder'].'/'.$_GET['filename'] , '');
            if($FC === false) {
                $ret->status = "error";
                $ret->msg="Error in adding new user, check again";
            }
            else {
                $ret->status="success";
                $ret->msg="New agent added successfully!!"; 
            }
        }
    }
} else $ret->msg="Add Email";
echo json_encode($ret);
?>