Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.27% covered (warning)
67.27%
37 / 55
100.00% covered (success)
100.00%
1 / 1
CRAP
n/a
0 / 0
build_output_html
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * @file
5 * Login action: confirm email change from email link.
6 * Logic inlined from legacy/login/mailchange.php; no longer requires that file.
7 */
8
9declare(strict_types=1);
10
11require_once __DIR__.'/../../Legacy/common/authorize.php';
12require_once __DIR__.'/../../Legacy/common/awake.php';
13
14/**
15 * Builds HTML redirect page to IonUrl on-mailchng.
16 *
17 * @param string $language Lang code.
18 * @param int $errorcode ApplicationError code.
19 * @return string HTML document.
20 */
21function build_output_html(string $language, int $errorcode): string {
22    $result = ($errorcode === ApplicationError::Success) ? 'SUCCESS' : 'FAILURE';
23    $dsturl = AppConstants::IonUrl().'on-mailchng?lang='.$language.'&result='.$result;
24    $html = "<!DOCTYPE html>";
25    $html .= "<html>";
26    $html .= "<head><meta http-equiv='refresh' content='0; URL=".$dsturl."'></head>";
27    $html .= "<body><p>You are being redirected. If not, please follow <a href='".$dsturl."'>this link</a>.</p></body>";
28    $html .= "</html>";
29    return $html;
30}
31
32    /**
33     * Action callable: confirm email change from email link (GET authorization, oldmail, newmail, lang).
34     *
35     * Validates authorization, updates USER email, notifies via awake_on_update,
36     * returns HTML redirect to IonUrl on-mailchng.
37     *
38     * @param string $body Request body (unused).
39     * @param array<string, mixed> $query Query params: authorization, oldmail, newmail, lang.
40     * @param \ConxHelper $conx Connection helper (from LoginService; uses ->global for central DB).
41     * @param \Psr\Log\LoggerInterface $logger Logger (from LoggerFactory).
42     * @param \UppServices\SessionService $sessionService Session service (unused).
43     * @return array{output: string, contentType: string} HTML output and content type.
44     */
45    return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array {
46        $retval = ApplicationError::Success;
47        $userinfo = null;
48
49        $logger->info("Checking authorization key for user activation..");
50        $auth = $query['authorization'] ?? null;
51        if (!$auth) {
52            $logger->error("No authorization key provided on URL. Operation cancelled.");
53            $retval = ApplicationError::Parameters;
54        }
55
56        $oldmail = $query['oldmail'] ?? null;
57        if (success($retval) && !$oldmail) {
58            $logger->error("Mandatory parameter 'oldmail' not provided on URL. Operation cancelled.");
59            $retval = ApplicationError::Parameters;
60        }
61
62        $newmail = $query['newmail'] ?? null;
63        if (success($retval) && !$newmail) {
64            $logger->error("Mandatory parameter 'newmail' not provided on URL. Operation cancelled.");
65            $retval = ApplicationError::Parameters;
66        }
67
68        if (success($retval)) {
69            $item = use_authorization($conx, $auth);
70            if (!$item) {
71                $logger->error("Authorization key '".$auth."' not found or invalid.");
72                $retval = ApplicationError::Unauthorized;
73            } else {
74                $logger->info("Authorization check test sucessfull.");
75            }
76        }
77
78        if (success($retval)) {
79            $results = [];
80            $res = $conx->global->query('USER', ['email' => $oldmail, 'status' => ['PA', 'AC']], $results);
81            if ($res && count($results) > 0) {
82                $userinfo = $results[0];
83                $iteminfo = ["objid" => $userinfo['objid'], "email" => $newmail];
84                $transaction = $conx->global->begin();
85                if ($transaction) {
86                    $res = $transaction->update('USER', $iteminfo);
87                    if (!$res) {
88                        $retval = ApplicationError::Database;
89                    }
90                    $transaction->flush($res);
91                } else {
92                    $retval = ApplicationError::Database;
93                }
94            } else {
95                if (!$res) {
96                    $retval = ApplicationError::Database;
97                } else {
98                    $logger->info("No active user found with email address '".$oldmail."'.");
99                    $retval = ApplicationError::NotFound;
100                }
101            }
102        }
103        
104        if (success($retval)) {
105            $retval = awake_on_update($conx, 'USER', [$userinfo], true);
106        }
107
108        $lang = (string) ($query['lang'] ?? 'EN');
109        $output = build_output_html($lang, $retval);
110        return ['output' => $output, 'contentType' => 'text/html; charset=utf-8'];
111    };