Subversion Repositories oidplus

Rev

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