<?php

/* [subscribe.php]
 *
 * Subscribe an email address to a Mailman3 mailing list. The email address is taken from the request, and
 * the mailing list and API credentials are set below.
 *
 * Mailman3 API subscription documentation:
 * 
 * [https://docs.mailman3.org/projects/mailman/en/latest/src/mailman/rest/docs/membership.html#joining-a-mailing-list]
 */

const MAILING_LIST_ID = "scrapeboard-list.nugget.fun";
const API_USER = "restadmin";
const API_TOKEN = "GgzPdbc0rvvsWkHIhmGtjtbv0rmDlz0Y4zBjQv+kEjl6HvSS";
const API_HOST = "http://localhost:8001";
const API_VERSION = "3.1";

/* Submit an API request to mailman using cURL with the above settings
 *
 * @param request     request part of the URL to submit to mailman, see Mailman3 for possible requests
 * @param method      either "GET" or "POST"
 * @param post_data   array of post parameters to pass with the request URL
 * @return            array of reponse parameters decoded from JSON, along with response["success"] and response["message"]
 */
function submit_request_to_mailman($request, $method = "GET", $post_data = array())
{
    $ch = curl_init();
    curl_setopt_array($ch, array(
                          CURLOPT_URL => API_HOST. "/" . API_VERSION . "/" . $request,
                          CURLOPT_HTTPAUTH => CURLAUTH_ANY,
                          CURLOPT_USERPWD => API_USER . ":" . API_TOKEN,
                          
                          /* Return the response from curl_exec as a string instead or outputting it directly */
                          CURLOPT_RETURNTRANSFER => true
                          ));

    /* Use HTTP POST method if requested, application/x-www-form-urlencoded */
    if ($method == "POST")
    {
        curl_setopt($ch, CURLOPT_POST, true);

        /* Include any post data in the request, using http_build_query to post as application/x-www-form-urlencoded */
        if ($post_data)
        {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
        }
    }

    /* Define a response array that contains the success state and a message that will be filled in the case of an error */
    $response = array("success" => false, "message" => "");
    $result = curl_exec($ch);
    if ($result !== false)
    {
        /* Make sure the appropriate response for GET or POST was received */
        $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($method == "POST" && $http_status != 201 || $method == "GET" && $http_status != 200)
        {
            $response["message"] = "[" . curl_getinfo($ch, CURLINFO_HTTP_CODE) . "]";
            if ($result != "")
            {
                $result = json_decode($result, true);
                if (array_key_exists("title", $result))
                {
                    $response["message"] .= " " . $result["title"];
                }
                if (array_key_exists("description", $result))
                {
                    $response["message"] .= " " . $result["description"];
                }
            }
        }
        else
        {
            /* Merge the response from Mailman3 into the existing response array */
            $response["success"] = true;
            if ($result != "")
            {
                $response = array_merge($response, json_decode($result, true));
            }
        }
    }
    else
    {
        $response["message"] = "cURL error: " . curl_error($ch);
    }

    curl_close($ch);
    return $response;
}

/* Start a response array that will be output as JSON and get the log directory */

// Commented out while transitioning away from Mailman
// $response = submit_request_to_mailman("system/configuration/paths.debian");

// if ($response["success"])
// {
    /* There is a log_dir in the response for getting the system level log directory ($response["log_dir"]), but use
     * /home/frank/ instead for now and log mailing list subscription request to a single site-wide file. */
    $log_path = "/home/frank/signups.txt";
    $response = array("success" => false, "message" => "");
    file_put_contents(
        $log_path,
        "[" . date(DATE_RFC2822) . "] Subscription request to " . MAILING_LIST_ID . " with input " . json_encode($_POST) . "\n",
        FILE_APPEND);

    if (array_key_exists("email", $_POST))
    {
        /* Use Data Filtering to validate that a valid email address was submitted
         * [https://www.php.net/manual/en/filter.examples.sanitization.php]
         *
         * If an invalid email was submitted, print an error message and exit
         */
        $sanitized_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
        $response["email"] = $sanitized_email;
        if (!filter_var($sanitized_email, FILTER_VALIDATE_EMAIL))
        {
            $response["success"] = false;
            $response["message"] = $_POST["email"] . " (sanitized to $sanitized_email) is not a valid email address";
        }
        else
        {
            /* Skip the verification, confirmation, and approval emails, and subscribe the submitted address */
            // $subscription_response = submit_request_to_mailman(
            //     "members", "POST", array(
            //         'list_id' => MAILING_LIST_ID,
            //         'subscriber' => $sanitized_email,
            //         'pre_verified' => true,
            //         'pre_confirmed' => true,
            //         'pre_approved' => true));
            
            // if ($subscription_response["success"])
            // {
                $response["success"] = true;
                $response["message"] = "Added $sanitized_email to " . MAILING_LIST_ID;
            // }
            // else
            // {
            //     /* Merge in the API request response, which will contain the error information */
            //     $response = array_merge($response, $subscription_response);
            // }
        }
    }
    else
    {
        $response["success"] = false;
        $response["message"] = "No email address to subscribe was provided";
    }
// }

/* Log the response internally, set the JSON output header, and output JSON for the client */
file_put_contents($log_path, "[" . date(DATE_RFC2822) . "] Final response is " . json_encode($response) . "\n", FILE_APPEND);
header("Content-Type: application/json");
echo json_encode($response);

?>