<?php
require_once __DIR__ . '/db_config.php';
$response = array();


if (isset($_POST["emailid"]) ) {
    $emailid = $_POST['emailid'];
 
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());
    $query ="SELECT * FROM signupdetails WHERE emailid = '$emailid' ";
    $result=mysqli_query($con,$query);

  
    if (mysqli_num_rows($result) > 0) {
	
		while ($row = mysqli_fetch_array($result)) {
		    
            $response["s"] = 1; 
            $mcrypt = new MCrypt();
            $decrypted = $mcrypt->decrypt($row["password"]);

            // The message
            $message = "Hi,\nUser Name : ".$emailid."\nPassword : ".$decrypted."\nTo know more about FieldCloudDesk, visit http://www.fieldcloud.in/ \nBest Regards \nTeam FieldcloudDesk";
            // In case any of our lines are larger than 70 characters, we should use wordwrap()
            $message = wordwrap($message, 70);

            // Send
            mail($emailid, 'Forgot Password?', $message);
		}
    } else {
        $response["s"] = 0; 
    }
    mysqli_close($con);
}else {
    $response["s"] = 2; 
}
    // echo no users JSON
    echo json_encode($response);
?>

<?php
class MCrypt{
   
	private $iv = 'B2LMOBITECHPVTLD';
	private $key = 'B2LMOBITECHPVTLD';
	 
	 
    function __construct()
    {
    }

    /**
     * @param string $str
     * @param bool $isBinary whether to encrypt as binary or not. Default is: false
     * @return string Encrypted data
     */
    function encrypt($str, $isBinary = false)
    {
        $iv = $this->iv;
        $str = $isBinary ? $str : utf8_decode($str);

        $td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);
        mcrypt_generic_init($td, $this->key, $iv);
        $encrypted = mcrypt_generic($td, $str);

        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);

        return $isBinary ? $encrypted : bin2hex($encrypted);
    }

   
    function decrypt($code, $isBinary = false)
    {
        $code = $isBinary ? $code : $this->hex2bin($code);
        $iv = $this->iv;

        $td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);

        mcrypt_generic_init($td, $this->key, $iv);
        $decrypted = mdecrypt_generic($td, $code);

        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);

        return $isBinary ? trim($decrypted) : utf8_encode(trim($decrypted));
    }

    protected function hex2bin($hexdata)
    {
        $bindata = '';

        for ($i = 0; $i < strlen($hexdata); $i += 2) {
            $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
        }

        return $bindata;
    }

}
?>