Subversion Repositories oidplus

Rev

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