Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.81% covered (warning)
69.81%
37 / 53
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    
25    $html = "<!DOCTYPE html>";
26    $html .= "<html>";
27    $html .= "<head><meta http-equiv='refresh' content='0; URL=".$dsturl."'></head>";
28    $html .= "<body><p>You are being redirected. If not, please follow <a href='".$dsturl."'>this link</a>.</p></body>";
29    $html .= "</html>";
30    
31    return $html;
32}
33
34    /**
35     * Action callable: confirm email change from email link (GET authorization, oldmail, newmail, lang).
36     *
37     * Validates authorization, updates USER email, notifies via awake_on_update,
38     * returns HTML redirect to IonUrl on-mailchng.
39     *
40     * @param string $body Request body (unused).
41     * @param array<string, mixed> $query Query params: authorization, oldmail, newmail, lang.
42     * @param \ConxHelper $conx Connection helper (from LoginService; uses ->global for central DB).
43     * @param \Psr\Log\LoggerInterface $logger Logger (from LoggerFactory).
44     * @param \UppServices\SessionService $sessionService Session service (unused).
45     * @return array{output: string, contentType: string} HTML output and content type.
46     */
47    return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array {
48        $retval = ApplicationError::Success;
49        $userinfo = null;
50
51        $logger->info("Checking authorization key for user activation..");
52        $auth = $query['authorization'] ?? null;
53        if (!$auth) {
54            $logger->error("No authorization key provided on URL. Operation cancelled.");
55            $retval = ApplicationError::Parameters;
56        }
57
58        $oldmail = $query['oldmail'] ?? null;
59        if (success($retval) && !$oldmail) {
60            $logger->error("Mandatory parameter 'oldmail' not provided on URL. Operation cancelled.");
61            $retval = ApplicationError::Parameters;
62        }
63
64        $newmail = $query['newmail'] ?? null;
65        if (success($retval) && !$newmail) {
66            $logger->error("Mandatory parameter 'newmail' not provided on URL. Operation cancelled.");
67            $retval = ApplicationError::Parameters;
68        }
69
70        if (success($retval)) {
71            $item = use_authorization($conx, $auth);
72            if (!$item) {
73                $logger->error("Authorization key '".$auth."' not found or invalid.");
74                $retval = ApplicationError::Unauthorized;
75            } 
76            else {
77                $logger->info("Authorization check test sucessfull.");
78            }
79        }
80
81        if (success($retval)) {
82            $results = [];
83            
84            $res = $conx->global->query('USER', ['email' => $oldmail, 'status' => ['PA', 'AC']], $results);
85            if ($res && count($results) > 0) {
86                $userinfo = $results[0];
87                $iteminfo = ["objid" => $userinfo['objid'], "email" => $newmail];
88                
89                $ok = $conx->global->transactional(function (\transaction $transaction) use ($iteminfo) {
90                    return (bool) $transaction->update('USER', $iteminfo);
91                });
92                
93                if (!$ok) {
94                    $retval = ApplicationError::Database;
95                }
96            } 
97            else {
98                if (!$res) {
99                    $retval = ApplicationError::Database;
100                } else {
101                    $logger->info("No active user found with email address '".$oldmail."'.");
102                    $retval = ApplicationError::NotFound;
103                }
104            }
105        }
106        
107        if (success($retval)) {
108            $retval = awake_on_update($conx, 'USER', [$userinfo], true);
109        }
110
111        $lang = (string) ($query['lang'] ?? 'EN');
112        $output = build_output_html($lang, $retval);
113        
114        return ['output' => $output, 'contentType' => 'text/html; charset=utf-8'];
115    };