Subversion Repositories oidplus

Rev

Rev 1131 | Rev 1445 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
990 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2022 - 2023 Daniel Marschall, ViaThinkSoft / Till Wehowski, Frdlweb
990 daniel-mar 6
 *
7
 * Licensed under the MIT License.
8
 */
9
 
1050 daniel-mar 10
namespace Frdlweb\OIDplus;
990 daniel-mar 11
 
1131 daniel-mar 12
use ViaThinkSoft\OIDplus\INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_4;
13
use ViaThinkSoft\OIDplus\INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_7;
1050 daniel-mar 14
use ViaThinkSoft\OIDplus\OIDplus;
15
use ViaThinkSoft\OIDplus\OIDplusObject;
16
use ViaThinkSoft\OIDplus\OIDplusPagePluginPublic;
17
 
1086 daniel-mar 18
// phpcs:disable PSR1.Files.SideEffects
19
\defined('INSIDE_OIDPLUS') or die;
20
// phpcs:enable PSR1.Files.SideEffects
21
 
1131 daniel-mar 22
class OIDplusPagePublicAltIds extends OIDplusPagePluginPublic
23
        implements INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_4, /* whois*Attributes */
24
                   INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_7  /* getAlternativesForQuery */
25
{
990 daniel-mar 26
 
1116 daniel-mar 27
        /**
28
         * @param string $actionID
29
         * @param array $params
30
         * @return array
31
         * @throws \ViaThinkSoft\OIDplus\OIDplusException
32
         */
33
        public function action(string $actionID, array $params): array {
34
                return parent::action($actionID, $params);
990 daniel-mar 35
        }
36
 
1116 daniel-mar 37
        /**
38
         * @param string $id
39
         * @param array $out
40
         * @param bool $handled
41
         * @return void
42
         */
43
        public function gui(string $id, array &$out, bool &$handled) {
990 daniel-mar 44
 
45
        }
46
 
1116 daniel-mar 47
        /**
48
         * @param array $out
49
         * @return void
50
         */
51
        public function publicSitemap(array &$out) {
990 daniel-mar 52
 
53
        }
54
 
1116 daniel-mar 55
        /**
56
         * @param array $json
57
         * @param string|null $ra_email
58
         * @param bool $nonjs
59
         * @param string $req_goto
60
         * @return bool
61
         */
62
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
990 daniel-mar 63
                return false;
64
        }
65
 
1116 daniel-mar 66
        /**
67
         * @return string|null
68
         * @throws \ViaThinkSoft\OIDplus\OIDplusException
69
         */
990 daniel-mar 70
        private function cache_id() {
71
                static $cache_id = null;
72
                if (!is_null($cache_id)) return $cache_id;
73
                $cache_id  =  'Create='.OIDplus::db()->getScalar("select max(created) as ts from ###objects where created is not null;");
74
                $cache_id .= '/Update='.OIDplus::db()->getScalar("select max(updated) as ts from ###objects where updated is not null;");
75
                $cache_id .= '/Count='.OIDplus::db()->getScalar("select count(id) as cnt from ###objects;");
76
                $plugin_versions = array();
77
                foreach (OIDplus::getObjectTypePluginsEnabled() as $otp) {
78
                        $plugin_versions[] = '/'.$otp->getManifest()->getOid().'='.$otp->getManifest()->getVersion();
79
                }
80
                sort($plugin_versions);
81
                $cache_id .= implode('',$plugin_versions);
82
                return $cache_id;
83
        }
84
 
1116 daniel-mar 85
        /**
1130 daniel-mar 86
         * @param bool $noCache
1116 daniel-mar 87
         * @return array[]|mixed|null
88
         * @throws \ViaThinkSoft\OIDplus\OIDplusException
89
         */
1130 daniel-mar 90
        public function readAll(bool $noCache = false) {
990 daniel-mar 91
                static $local_cache = null;
92
 
93
                $cache_file = OIDplus::localpath().'/userdata/cache/frdl_alt_id.ser';
94
                if ($noCache === false) {
95
                        // Local cache (to save time for multiple calls during the same HTTP request)
96
                        if (!is_null($local_cache)) return $local_cache;
97
 
98
                        // File cache (to save time between HTTP requests)
99
                        if (file_exists($cache_file)) {
100
                                $cache_data = unserialize(file_get_contents($cache_file));
101
                                $cache_id = $cache_data[0];
102
                                if ($cache_id == $this->cache_id()) {
103
                                        return $cache_data[1];
104
                                }
105
                        }
106
                }
107
 
108
                $alt_ids = array();
109
                $rev_lookup = array();
110
 
111
                $res = OIDplus::db()->query("select id from ###objects");
112
                while ($row = $res->fetch_array()) {
113
                        $obj = OIDplusObject::parse($row['id']);
114
                        if (!$obj) continue; // e.g. if plugin is disabled
115
                        $ary = $obj->getAltIds();
116
                        foreach ($ary as $a) {
117
                                $origin = $obj->nodeId(true);
118
                                $alternative = $a->getNamespace() . ':' . $a->getId();
119
 
120
                                if (!isset($alt_ids[$origin])) $alt_ids[$origin] = array();
121
                                $alt_ids[$origin][] = $alternative;
122
 
123
                                if (!isset($rev_lookup[$alternative])) $rev_lookup[$alternative] = array();
124
                                $rev_lookup[$alternative][] = $origin;
125
                        }
126
                }
127
 
128
                $data = array($alt_ids, $rev_lookup);
129
 
130
                // File cache (to save time between HTTP requests)
131
                $cache_data = array($this->cache_id(), $data);
132
                @file_put_contents($cache_file, serialize($cache_data));
133
 
134
                // Local cache (to save time for multiple calls during the same HTTP request)
135
                $local_cache = $data;
136
 
137
                return $data;
138
        }
139
 
1116 daniel-mar 140
        /**
1130 daniel-mar 141
         * @param string $id
1131 daniel-mar 142
         * @return string[]
1116 daniel-mar 143
         * @throws \ViaThinkSoft\OIDplus\OIDplusException
144
         */
1131 daniel-mar 145
        public function getAlternativesForQuery(string $id/* INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_7 signature takes just 1 param!? , $noCache = false*/): array {
990 daniel-mar 146
 
147
                static $caches = array();
148
 
149
                if(/*$noCache === false && */isset($caches[$id]) ){
150
                        return $caches[$id];
151
                }
152
 
1008 daniel-mar 153
                if (strpos($id,':') !== false) {
154
                        list($ns, $altIdRaw) = explode(':', $id, 2);
155
                        if($ns === 'weid'){
1050 daniel-mar 156
                                $id='oid:'.\Frdl\Weid\WeidOidConverter::weid2oid($id);
1008 daniel-mar 157
                        }
990 daniel-mar 158
                }
159
 
160
                list($alt_ids, $rev_lookup) = $this->readAll(false);
161
 
162
                $res = [
163
                        $id,
164
                ];
165
                if(isset($rev_lookup[$id])){
166
                        $res = array_merge($res, $rev_lookup[$id]);
167
                }
168
                foreach($alt_ids as $original => $altIds){
169
                        if($id === $original || in_array($id, $altIds) ){
170
                                 $res = array_merge($res, $altIds);
171
                                 $res = array_merge($res, [$original]);
172
                        }
173
                }
174
 
175
                $weid = false;
176
                foreach($res as $alt){
1008 daniel-mar 177
                        if (strpos($alt,':') !== false) {
178
                                list($ns, $altIdRaw) = explode(':', $alt, 2);
179
                                if($ns === 'oid'){
1050 daniel-mar 180
                                        $weid=\Frdl\Weid\WeidOidConverter::oid2weid($altIdRaw);
1008 daniel-mar 181
                                        break;
182
                                }
990 daniel-mar 183
                        }
184
                }
185
 
186
                if ($weid !== false) {
187
                        $res[]=$weid;
188
                }
189
                $res = array_unique($res);
190
 
191
                $caches[$id] = $res;
192
 
193
                return $res;
194
        }
195
 
1116 daniel-mar 196
        /**
197
         * @param string $request
198
         * @return array|false
199
         */
200
        public function tree_search(string $request) {
990 daniel-mar 201
                return false;
202
        }
203
 
1116 daniel-mar 204
        /**
1130 daniel-mar 205
         * @param string $id
1116 daniel-mar 206
         * @return false|mixed|string
207
         * @throws \ViaThinkSoft\OIDplus\OIDplusException
208
         */
1130 daniel-mar 209
        public function getCanonical(string $id){
990 daniel-mar 210
                foreach($this->getAlternativesForQuery($id) as $alt){
1008 daniel-mar 211
                        if (strpos($alt,':') !== false) {
212
                                list($ns, $altIdRaw) = explode(':', $alt, 2);
213
                                if($ns === 'oid'){
214
                                        return $alt;
215
                                }
990 daniel-mar 216
                        }
217
                }
218
 
219
                return false;
220
        }
221
 
1116 daniel-mar 222
        /**
1131 daniel-mar 223
         * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_4
1130 daniel-mar 224
         * @param string $id
225
         * @param array $out
1116 daniel-mar 226
         * @return void
227
         * @throws \ViaThinkSoft\OIDplus\OIDplusException
228
         */
1130 daniel-mar 229
        public function whoisObjectAttributes(string $id, array &$out) {
990 daniel-mar 230
                $xmlns = 'oidplus-frdlweb-altids-plugin';
231
                $xmlschema = 'urn:oid:1.3.6.1.4.1.37553.8.1.8.8.53354196964.641310544.1714020422';
232
                $xmlschemauri = OIDplus::webpath(__DIR__.'/altids.xsd',OIDplus::PATH_ABSOLUTE);
233
 
234
                $handleShown = false;
235
                $canonicalShown = false;
236
 
1135 daniel-mar 237
                $out1 = array();
238
                $out2 = array();
990 daniel-mar 239
 
1135 daniel-mar 240
                $tmp = $this->getAlternativesForQuery($id);
241
                sort($tmp); // DM 26.03.2023 : Added sorting (intended to sort "alternate-identifier")
242
                foreach($tmp as $alt) {
1040 daniel-mar 243
                        if (strpos($alt,':') === false) continue;
1008 daniel-mar 244
 
990 daniel-mar 245
                        list($ns, $altIdRaw) = explode(':', $alt, 2);
246
 
247
                        if (($canonicalShown === false) && ($ns === 'oid')) {
248
                                $canonicalShown=true;
249
 
1135 daniel-mar 250
                                $out1[] = [
990 daniel-mar 251
                                        'xmlns' => $xmlns,
252
                                        'xmlschema' => $xmlschema,
253
                                        'xmlschemauri' => $xmlschemauri,
254
                                        'name' => 'canonical-identifier',
255
                                        'value' => $ns.':'.$altIdRaw,
256
                                ];
257
 
258
                        }
259
 
260
                        if (($handleShown === false) && ($alt === $id)) {
261
                                $handleShown=true;
262
 
1135 daniel-mar 263
                                $out1[] = [
990 daniel-mar 264
                                        'xmlns' => $xmlns,
265
                                        'xmlschema' => $xmlschema,
266
                                        'xmlschemauri' => $xmlschemauri,
267
                                        'name' => 'handle-identifier',
268
                                        'value' => $alt,
269
                                ];
270
 
271
                        }
272
 
1135 daniel-mar 273
                        if ($alt !== $id) { // DM 26.03.2023 : Added condition that alternate must not be the id itself
274
                                $out2[] = [
275
                                        'xmlns' => $xmlns,
276
                                        'xmlschema' => $xmlschema,
277
                                        'xmlschemauri' => $xmlschemauri,
278
                                        'name' => 'alternate-identifier',
279
                                        'value' => $ns.':'.$altIdRaw,
280
                                ];
281
                        }
990 daniel-mar 282
 
283
                }
1135 daniel-mar 284
 
285
                // DM 26.03.2023 : Added this
286
                $out = array_merge($out, $out1); // handle-identifier and canonical-identifier
287
                $out = array_merge($out, $out2); // alternate-identifier
288
 
990 daniel-mar 289
        }
290
 
1116 daniel-mar 291
        /**
1131 daniel-mar 292
         * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_4
1130 daniel-mar 293
         * @param string $email
294
         * @param array $out
1116 daniel-mar 295
         * @return void
296
         */
1130 daniel-mar 297
        public function whoisRaAttributes(string $email, array &$out) {
1116 daniel-mar 298
 
990 daniel-mar 299
        }
300
 
301
 }