xref: /webtrees/app/Elements/PafUid.php (revision c2ed51d13a57743094c11c8fe84befd9d4f158cd)
1*c2ed51d1SGreg Roach<?php
2*c2ed51d1SGreg Roach
3*c2ed51d1SGreg Roach/**
4*c2ed51d1SGreg Roach * webtrees: online genealogy
5*c2ed51d1SGreg Roach * Copyright (C) 2021 webtrees development team
6*c2ed51d1SGreg Roach * This program is free software: you can redistribute it and/or modify
7*c2ed51d1SGreg Roach * it under the terms of the GNU General Public License as published by
8*c2ed51d1SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*c2ed51d1SGreg Roach * (at your option) any later version.
10*c2ed51d1SGreg Roach * This program is distributed in the hope that it will be useful,
11*c2ed51d1SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*c2ed51d1SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*c2ed51d1SGreg Roach * GNU General Public License for more details.
14*c2ed51d1SGreg Roach * You should have received a copy of the GNU General Public License
15*c2ed51d1SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16*c2ed51d1SGreg Roach */
17*c2ed51d1SGreg Roach
18*c2ed51d1SGreg Roachdeclare(strict_types=1);
19*c2ed51d1SGreg Roach
20*c2ed51d1SGreg Roachnamespace Fisharebest\Webtrees\Elements;
21*c2ed51d1SGreg Roach
22*c2ed51d1SGreg Roachuse Exception;
23*c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Tree;
24*c2ed51d1SGreg Roachuse Ramsey\Uuid\Uuid;
25*c2ed51d1SGreg Roach
26*c2ed51d1SGreg Roachuse function dechex;
27*c2ed51d1SGreg Roachuse function hexdec;
28*c2ed51d1SGreg Roachuse function strtoupper;
29*c2ed51d1SGreg Roachuse function strtr;
30*c2ed51d1SGreg Roachuse function substr;
31*c2ed51d1SGreg Roach
32*c2ed51d1SGreg Roach/**
33*c2ed51d1SGreg Roach * _UID fields, as created by PAF and other applications
34*c2ed51d1SGreg Roach */
35*c2ed51d1SGreg Roachclass PafUid extends AbstractElement
36*c2ed51d1SGreg Roach{
37*c2ed51d1SGreg Roach    protected const MAX_LENGTH = 34;
38*c2ed51d1SGreg Roach
39*c2ed51d1SGreg Roach    /**
40*c2ed51d1SGreg Roach     * Create a default value for this element.
41*c2ed51d1SGreg Roach     *
42*c2ed51d1SGreg Roach     * @param Tree $tree
43*c2ed51d1SGreg Roach     *
44*c2ed51d1SGreg Roach     * @return string
45*c2ed51d1SGreg Roach     */
46*c2ed51d1SGreg Roach    public function default(Tree $tree): string
47*c2ed51d1SGreg Roach    {
48*c2ed51d1SGreg Roach        try {
49*c2ed51d1SGreg Roach            $uid = strtr(Uuid::uuid4()->toString(), ['-' => '']);
50*c2ed51d1SGreg Roach        } catch (Exception $ex) {
51*c2ed51d1SGreg Roach            // uuid4() can fail if there is insufficient entropy in the system.
52*c2ed51d1SGreg Roach            return '';
53*c2ed51d1SGreg Roach        }
54*c2ed51d1SGreg Roach
55*c2ed51d1SGreg Roach        $checksum_a = 0; // a sum of the bytes
56*c2ed51d1SGreg Roach        $checksum_b = 0; // a sum of the incremental values of $checksum_a
57*c2ed51d1SGreg Roach
58*c2ed51d1SGreg Roach        // Compute checksums
59*c2ed51d1SGreg Roach        for ($i = 0; $i < 32; $i += 2) {
60*c2ed51d1SGreg Roach            $checksum_a += hexdec(substr($uid, $i, 2));
61*c2ed51d1SGreg Roach            $checksum_b += $checksum_a & 0xff;
62*c2ed51d1SGreg Roach        }
63*c2ed51d1SGreg Roach
64*c2ed51d1SGreg Roach        return strtoupper($uid . substr(dechex($checksum_a), -2) . substr(dechex($checksum_b), -2));
65*c2ed51d1SGreg Roach    }
66*c2ed51d1SGreg Roach}
67