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