Skip to content

Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.

2.7 KB
Raw Blame
<?php
require_once '../../includes/connect_endpoint.php';
require_once '../../includes/validate_endpoint.php';
require_once '../../includes/ssrf_helper.php';

$postData = file_get_contents("php://input");
$data = json_decode($postData, true);

if (
    !isset($data["gotify_url"]) || $data["gotify_url"] == "" ||
    !isset($data["token"]) || $data["token"] == ""
) {
    $response = [
        "success" => false,
        "message" => translate('fill_mandatory_fields', $i18n)
    ];
    die(json_encode($response));
} else {
    // Set the message parameters
    $title = translate('wallos_notification', $i18n);
    $message = translate('test_notification', $i18n);
    $priority = 5;

    $url = $data["gotify_url"];
    $token = $data["token"];
    $ignore_ssl = $data["ignore_ssl"];

    // Validate URL scheme
    $parsedUrl = parse_url($url);
    if (
        !isset($parsedUrl['scheme']) ||
        !in_array(strtolower($parsedUrl['scheme']), ['http', 'https']) ||
        !filter_var($url, FILTER_VALIDATE_URL)
    ) {
        die(json_encode([
            "success" => false,
            "message" => translate("error", $i18n)
        ]));
    }

    $ssrf = validate_webhook_url_for_ssrf($url, $db, $i18n, $userId);

    $ch = curl_init();

    // Set the URL and other options
    curl_setopt($ch, CURLOPT_URL, rtrim($url, '/') . "/message?token=" . $token);
    
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
    curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
        'title' => $title,
        'message' => $message,
        'priority' => $priority,
    ]));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);

    if ($ignore_ssl) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }

    // Execute the request
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    // Close the cURL session
    unset($ch);

    // Check if the message was sent successfully
    if ($response === false || $httpCode < 200 || $httpCode >= 300) {
        die(json_encode([
            "success" => false,
            "message" => translate('notification_failed', $i18n),
            "response" => $response,
            "http_code" => $httpCode
        ]));
    } else {
        die(json_encode([
            "success" => true,
            "message" => translate('notification_sent_successfuly', $i18n),
            "response" => $response
        ]));
    }
}