Subversion Repositories oidplus

Rev

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