Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
943 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0 RDAP
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
943 daniel-mar 6
 * Authors               Daniel Marschall, ViaThinkSoft
7
 *                       Till Wehowski, Frdlweb
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
 
1050 daniel-mar 22
namespace Frdlweb\OIDplus;
943 daniel-mar 23
 
1050 daniel-mar 24
use ViaThinkSoft\OIDplus\OIDplus;
1189 daniel-mar 25
use ViaThinkSoft\OIDplus\OIDplusBaseClass;
1050 daniel-mar 26
use ViaThinkSoft\OIDplus\OIDplusObject;
27
use ViaThinkSoft\OIDplus\OIDplusOIDIP;
28
use ViaThinkSoft\OIDplus\OIDplusPagePublicObjects;
29
 
1086 daniel-mar 30
// phpcs:disable PSR1.Files.SideEffects
31
\defined('INSIDE_OIDPLUS') or die;
32
// phpcs:enable PSR1.Files.SideEffects
33
 
1189 daniel-mar 34
class OIDplusRDAP extends OIDplusBaseClass {
943 daniel-mar 35
 
1130 daniel-mar 36
        /**
37
         * @var string
38
         */
943 daniel-mar 39
        protected $rdapBaseUri;
1130 daniel-mar 40
 
41
        /**
42
         * @var bool
43
         */
943 daniel-mar 44
        protected $useCache;
1130 daniel-mar 45
 
46
        /**
47
         * @var string
48
         */
943 daniel-mar 49
        protected $rdapCacheDir;
1130 daniel-mar 50
 
51
        /**
52
         * @var int
53
         */
943 daniel-mar 54
        protected $rdapCacheExpires;
55
 
1116 daniel-mar 56
        /**
57
         * @throws \ViaThinkSoft\OIDplus\OIDplusException
58
         */
943 daniel-mar 59
        public function __construct() {
60
                $this->rdapBaseUri = OIDplus::baseConfig()->getValue('RDAP_BASE_URI', OIDplus::webpath() );
61
                $this->useCache = OIDplus::baseConfig()->getValue('RDAP_CACHE_ENABLED', false );
950 daniel-mar 62
                $this->rdapCacheDir = OIDplus::baseConfig()->getValue('RDAP_CACHE_DIRECTORY', OIDplus::localpath().'userdata/cache/' );
63
                $this->rdapCacheExpires = OIDplus::baseConfig()->getValue('RDAP_CACHE_EXPIRES', 60 * 3 );
943 daniel-mar 64
        }
65
 
1116 daniel-mar 66
        /**
1125 daniel-mar 67
         * @param string $query
1116 daniel-mar 68
         * @return array
69
         * @throws \ViaThinkSoft\OIDplus\OIDplusException
70
         */
1130 daniel-mar 71
        public function rdapQuery(string $query): array {
943 daniel-mar 72
                $query = str_replace('oid:.', 'oid:', $query);
73
                $n = explode(':', $query);
74
                if(2>count($n)){
75
                 array_unshift($n, 'oid');
76
                 $query = 'oid:'.$query;
77
                }
78
                $ns = $n[0];
79
 
80
                if(true === $this->useCache){
950 daniel-mar 81
                        $cacheFile = $this->rdapCacheDir. 'rdap_'
943 daniel-mar 82
                        .sha1(\get_current_user()
83
                                  . $this->rdapBaseUri.__FILE__.$query
1282 daniel-mar 84
                                  .OIDplus::authUtils()->makeSecret('cee75760-f4f8-11ed-b67e-3c4a92df8582')
943 daniel-mar 85
                                 )
86
                        .'.'
87
                        .strlen( $this->rdapBaseUri.$query )
950 daniel-mar 88
                        .'.ser'
943 daniel-mar 89
                        ;
90
 
944 daniel-mar 91
                        $tmp = $this->rdap_read_cache($cacheFile, $this->rdapCacheExpires);
92
                        if ($tmp) return $tmp;
943 daniel-mar 93
                }else{
94
                        $cacheFile = false;
95
                }
96
 
969 daniel-mar 97
                $out = [];
98
 
99
                $obj = OIDplusObject::findFitting($query);
100
 
101
                if(!$obj){
972 daniel-mar 102
                        // If object was not found, try if it is an alternative identifier of another object
969 daniel-mar 103
                        $alts = OIDplusPagePublicObjects::getAlternativesForQuery($query);
104
                        foreach ($alts as $alt) {
105
                                if ($obj = OIDplusObject::findFitting($alt)) {
106
                                        $query = $obj->nodeId();
107
                                        break;
943 daniel-mar 108
                                }
109
                        }
110
 
972 daniel-mar 111
                        // Still nothing found?
112
                        if(!$obj){
113
                                $out['error'] = 'Not found';
114
                                if(true === $this->useCache){
115
                                        $this->rdap_write_cache($out, $cacheFile);
116
                                }
117
                                return $this->rdap_out($out);
943 daniel-mar 118
                        }
972 daniel-mar 119
                } else {
120
                        $query = $obj->nodeId();
943 daniel-mar 121
                }
122
 
123
                $whois_server = '';
124
                if (OIDplus::config()->getValue('individual_whois_server', '') != '') {
125
                        $whois_server = OIDplus::config()->getValue('individual_whois_server', '');
126
                }
127
                else if (OIDplus::config()->getValue('vts_whois', '') != '') {
128
                        $whois_server = OIDplus::config()->getValue('vts_whois', '');
129
                }
130
                if (!empty($whois_server)) {
131
                        list($whois_host, $whois_port) = explode(':',"$whois_server:43");
132
                        if ($whois_port === '43') $out['port43'] = $whois_host;
133
                }
134
 
135
                $parentHandle=$obj->one_up();
136
 
137
                $out['name'] = $obj->nodeId(true);
138
                $out['objectClassName'] = $ns;
139
                $out['handle'] = $ns.':'.$n[1];
140
                $out['parentHandle'] =   (null !== $parentHandle && is_callable([$parentHandle, 'nodeId']) )
1116 daniel-mar 141
                                         ? $parentHandle->nodeId(true)
943 daniel-mar 142
                                         : null;
143
 
144
                $out['rdapConformance'] = [
145
                        "rdap_level_0", //https://datatracker.ietf.org/doc/html/rfc9083
146
                ];
147
                $out['links'] = [
148
                        [
149
                                "href"=> 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'],
150
                                "type"=> "application/rdap+json",
151
                                "title"=> sprintf("Information about the %s %s", $ns, $n[1]),
152
                                "value"=> $this->rdapBaseUri.$ns.'/'.$n[1],
153
                                "rel"=> "self"
154
                        ],
155
                        [
156
                                "href"=> OIDplus::webpath()."?goto=".urlencode($query),
157
                                "type"=> "text/html",
158
                                "title"=> sprintf("Information about the %s %s in the online repository", $ns, $n[1]),
159
                                "value"=> OIDplus::webpath()."?goto=".urlencode($query),
160
                                "rel"=> "alternate"
161
                        ]
162
                ];
163
                $out['remarks'] = [
164
                        [
165
                                "title"=>"Availability",
166
                                "description"=> [
167
                                        sprintf("The %s %s is known.", strtoupper($ns), $n[1]),
168
                                ],
169
                                "links"=> []
170
                        ],
171
                        [
172
                                "title"=>"Description",
173
                                "description"=> [
977 daniel-mar 174
                                        ($obj->isConfidential()) ? 'REDACTED FOR PRIVACY' : $obj->getDescription(),
943 daniel-mar 175
                                ],
176
                                "links"=> [
177
                                        [
178
                                                "href"=> OIDplus::webpath()."?goto=".urlencode($query),
179
                                                "type"=> "text/html",
180
                                                "title"=> sprintf("Information about the %s %s in the online repository", $ns, $n[1]),
181
                                                "value"=> OIDplus::webpath()."?goto=".urlencode($query),
182
                                                "rel"=> "alternate"
183
                                        ]
184
                                ]
185
                        ],
186
 
187
                ];
188
 
189
                if (!is_null(OIDplus::getPluginByOid("1.3.6.1.4.1.37476.2.5.2.4.1.100"))) { // OIDplusPagePublicWhois
190
                        $oidIPUrl = OIDplus::webpath().'plugins/viathinksoft/publicPages/100_whois/whois/webwhois.php?query='.urlencode($query);
191
 
192
                        $oidip_generator = new OIDplusOIDIP();
193
 
194
                        list($oidIP, $dummy_content_type) = $oidip_generator->oidipQuery($query);
195
 
196
                        $out['remarks'][] = [
197
                                "title" => "OID-IP Result",
198
                                "description" => $oidIP,
199
                                "links" => [
200
                                                [
201
                                                        "href"=> $oidIPUrl,
202
                                                        "type"=> "text/plain",
203
                                                        "title"=> sprintf("OIDIP Result for the %s %s (Plaintext)", $ns, $n[1]),
204
                                                        "value"=> $oidIPUrl,
205
                                                        "rel"=> "alternate"
206
                                                ],
207
                                                [
208
                                                        "href"=> "$oidIPUrl\$format=json",
209
                                                        "type"=> "application/json",
210
                                                        "title"=> sprintf("OIDIP Result for the %s %s (JSON)", $ns, $n[1]),
211
                                                        "value"=> "$oidIPUrl\$format=json",
212
                                                        "rel"=> "alternate"
213
                                                ],
214
                                                [
215
                                                        "href"=> "$oidIPUrl\$format=xml",
216
                                                        "type"=> "application/xml",
217
                                                        "title"=> sprintf("OIDIP Result for the %s %s (XML)", $ns, $n[1]),
218
                                                        "value"=> "$oidIPUrl\$format=xml",
219
                                                        "rel"=> "alternate"
220
                                                ]
221
                                        ]
222
                                ];
223
 
224
                        list($oidIPJSON, $dummy_content_type) = $oidip_generator->oidipQuery("$query\$format=json");
225
                        $out['oidplus_oidip'] = json_decode($oidIPJSON);
226
                }
227
 
228
                $out['notices']=[
229
                         [
230
                                "title" => "Authentication Policy",
231
                                "description" =>
232
                                [
233
                                        "Access to sensitive data for users with proper credentials."
234
                                ],
235
                                "links" =>
236
                                [
237
                                        [
238
                                                "value" => $this->rdapBaseUri."help",
239
                                                "rel" => "alternate",
240
                                                "type" => "text/html",
241
                                                "href" => OIDplus::webpath()."?goto=oidplus%3Aresources%24OIDplus%2Fprivacy_documentation.html"
242
                                        ]
243
                                ]
244
                        ]
245
                ];
246
 
247
                if($obj->isConfidential()){
944 daniel-mar 248
                        $out['remarks'][1]['type'] = "result set truncated due to authorization";
943 daniel-mar 249
                }
250
 
251
                $out['statuses']=[
252
                        'active',
253
                ];
254
 
255
 
256
                if(true === $this->useCache){
257
                        $this->rdap_write_cache($out, $cacheFile);
258
                }
944 daniel-mar 259
                return $this->rdap_out($out);
943 daniel-mar 260
        }
261
 
1116 daniel-mar 262
        /**
1130 daniel-mar 263
         * @param array $out
264
         * @param string $cacheFile
1116 daniel-mar 265
         * @return void
266
         */
1130 daniel-mar 267
        protected function rdap_write_cache(array $out, string $cacheFile){
950 daniel-mar 268
                @file_put_contents($cacheFile, serialize($out));
943 daniel-mar 269
        }
270
 
1116 daniel-mar 271
        /**
1130 daniel-mar 272
         * @param string $cacheFile
273
         * @param int $rdapCacheExpires
1116 daniel-mar 274
         * @return array|null
275
         */
1130 daniel-mar 276
        protected function rdap_read_cache(string $cacheFile, int $rdapCacheExpires){
277
                if (file_exists($cacheFile) && filemtime($cacheFile) >= time() - $rdapCacheExpires) {
950 daniel-mar 278
                        $out = unserialize(file_get_contents($cacheFile));
943 daniel-mar 279
                        if(is_array($out) || is_object($out)){
944 daniel-mar 280
                                return $this->rdap_out($out);
943 daniel-mar 281
                        }
282
                }
944 daniel-mar 283
                return null;
943 daniel-mar 284
        }
285
 
1116 daniel-mar 286
        /**
1130 daniel-mar 287
         * @param array $out
1116 daniel-mar 288
         * @return array
289
         */
1130 daniel-mar 290
        protected function rdap_out(array $out): array {
944 daniel-mar 291
                $out_content = json_encode($out);
292
                $out_type = 'application/rdap+json';
293
                return array($out_content, $out_type);
943 daniel-mar 294
        }
295
 
296
}