1 | <?php |
2 | |
3 | require_once __DIR__ . "/../vendor/autoload.php"; |
4 | require_once __DIR__ . "/../lib/php/fetchAll.php"; |
5 | require_once __DIR__ . "/../lib/php/ProblemDetails.php"; |
6 | require_once __DIR__ . "/../lib/php/devuelveJson.php"; |
7 | require_once __DIR__ . "/../lib/php/devuelveProblemDetails.php"; |
8 | require_once __DIR__ . "/../lib/php/devuelveErrorInterno.php"; |
9 | require_once __DIR__ . "/Bd.php"; |
10 | require_once __DIR__ . "/Suscripcion.php"; |
11 | require_once __DIR__ . "/suscripcionElimina.php"; |
12 | |
13 | use Minishlink\WebPush\WebPush; |
14 | |
15 | const AUTH = [ |
16 | "VAPID" => [ |
17 | "subject" => "https://notificacionesphp.gilbertopachec2.repl.co/", |
18 | "publicKey" => "BMBlr6YznhYMX3NgcWIDRxZXs0sh7tCv7_YCsWcww0ZCv9WGg-tRCXfMEHTiBPCksSqeve1twlbmVAZFv7GSuj0", |
19 | "privateKey" => "vplfkITvu0cwHqzK9Kj-DYStbCH_9AhGx9LqMyaeI6w" |
20 | ] |
21 | ]; |
22 | |
23 | try { |
24 | |
25 | $webPush = new WebPush(AUTH); |
26 | $mensaje = "Hola! 👋"; |
27 | |
28 | |
29 | |
30 | $conexion = Bd::getConexion(); |
31 | |
32 | $suscripciones = fetchAll( |
33 | $conexion->query( |
34 | "SELECT |
35 | SUS_ENDPOINT as endpoint, |
36 | SUS_PUB_KEY as publicKey, |
37 | SUS_AUT_TOK as authToken, |
38 | SUS_CONT_ENCOD as contentEncoding |
39 | FROM SUSCRIPCION" |
40 | ), |
41 | [], |
42 | PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, |
43 | Suscripcion::class |
44 | ); |
45 | |
46 | foreach ($suscripciones as $suscripcion) { |
47 | $webPush->queueNotification($suscripcion, $mensaje); |
48 | } |
49 | $reportes = $webPush->flush(); |
50 | |
51 | |
52 | $reporteDeEnvios = ""; |
53 | foreach ($reportes as $reporte) { |
54 | $endpoint = htmlentities($reporte->getRequest()->getUri()); |
55 | if ($reporte->isSuccess()) { |
56 | |
57 | $reporteDeEnvios .= "<dt>$endpoint</dt><dd>Éxito</dd>"; |
58 | } else { |
59 | if ($reporte->isSubscriptionExpired()) { |
60 | suscripcionElimina($endpoint); |
61 | } |
62 | |
63 | $explicacion = htmlentities($reporte->getReason()); |
64 | $reporteDeEnvios .= "<dt>$endpoint</dt><dd>Fallo: $explicacion</dd>"; |
65 | } |
66 | } |
67 | |
68 | devuelveJson(["reporte" => ["innerHTML" => $reporteDeEnvios]]); |
69 | } catch (ProblemDetails $details) { |
70 | |
71 | devuelveProblemDetails($details); |
72 | } catch (Throwable $error) { |
73 | |
74 | devuelveErrorInterno($error); |
75 | } |
76 | |