Subversion Repositories oidplus

Rev

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