Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
24 / 26
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
sender
92.31% covered (success)
92.31%
24 / 26
50.00% covered (danger)
50.00%
1 / 2
4.01
0.00% covered (danger)
0.00%
0 / 1
 send
92.00% covered (success)
92.00%
23 / 25
0.00% covered (danger)
0.00%
0 / 1
3.00
 subject
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2// This file is part of Moodle - https://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <https://www.gnu.org/licenses/>.
16
17namespace message_webpush\local;
18
19use stdClass;
20
21/**
22 * Sends one encrypted push message to one browser's push service (RFC 8030 section 5).
23 *
24 * @package    message_webpush
25 * @copyright  2026 DevLMS
26 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28class sender {
29    /** @var int Seconds the push service keeps a message for a browser that is offline. */
30    public const TTL = DAYSECS;
31
32    /** @var int RFC 8291 section 4: 4096 bytes of body leave 3993 for the message. */
33    public const MAX_PLAINTEXT = 3993;
34
35    /**
36     * Send one message.
37     *
38     * @param stdClass $sub subscription row
39     * @param string $plaintext the JSON payload, at most MAX_PLAINTEXT bytes
40     * @param array $keys the site's VAPID keys
41     * @return int the push service's HTTP status, or 0 when it could not be reached or the address is not allowed
42     */
43    public static function send(stdClass $sub, string $plaintext, array $keys): int {
44        // Checked again here: the list may have changed since the browser was saved.
45        if (!hosts::allowed($sub->endpoint)) {
46            debugging('webpush: browser ' . $sub->id . ' is on a push service that is no longer allowed', DEBUG_DEVELOPER);
47            return 0;
48        }
49        try {
50            $body = crypto::encrypt($plaintext, crypto::b64u_decode($sub->p256dh), crypto::b64u_decode($sub->auth));
51            $response = \core\di::get(\core\http_client::class)->post($sub->endpoint, [
52                'headers' => [
53                    'Content-Type' => 'application/octet-stream',
54                    'Content-Encoding' => 'aes128gcm',
55                    'TTL' => (string)self::TTL,
56                    'Urgency' => 'normal',
57                    'Authorization' => crypto::vapid($sub->endpoint, $keys['public'], $keys['private'], self::subject(), time()),
58                ],
59                'body' => $body,
60                'timeout' => 5,
61                'connect_timeout' => 3,
62                'http_errors' => false,
63                'allow_redirects' => false,
64            ]);
65        } catch (\Throwable $e) {
66            // Network errors quote the address, which is a secret capability URL: report the browser by id only.
67            debugging(
68                'webpush: could not reach the push service for browser ' . $sub->id . ' (' . get_class($e) . ')',
69                DEBUG_DEVELOPER
70            );
71            return 0;
72        }
73        return $response->getStatusCode();
74    }
75
76    /**
77     * The contact push services see in the VAPID token: the site address (RFC 8292 section 2.1).
78     *
79     * @return string
80     */
81    public static function subject(): string {
82        global $CFG;
83        return $CFG->wwwroot;
84    }
85}