<?php
if (!isset($_SESSION['FirstName'])) {
    session_start();
}
if ($_SESSION['FirstName'] == "") {
    echo 'You are not authorized to view this page. <br><br>You may <a href="index.php"><font color="blue">log in here.</font></a>';
    exit;
}

include ('includes/db1config.php'); //db connection config file
include ('../includes/mysql_connect.php'); //connect using selected db config file

$action = isset($_POST['action']) ? $_POST['action'] : '';

switch ($action) {
    case 'get_email':
        $id = isset($_POST['id']) ? $_POST['id'] : '';

        $qry = "SELECT *
                FROM email_storage
                WHERE email_storage.recordkey = '" . mysql_real_escape_string($id) . "'";

        $result = mysql_query($qry) or die(mysql_error());

        if (mysql_num_rows($result) < 1) {
            json_response(0);
        }

        $row = mysql_fetch_assoc($result);

        json_response(1, ['email' => $row]);

        break;
    default:
        json_response(0);
}

function json_response($result, $data = []) {
    $response = [
        'result' => $result
    ];

    $response = array_merge($response, $data);

    echo json_encode($response);
    die();
}