<?php
######################################################################
# ZW3B.Site v7.1.2 : The Web Sites Management System
# --------------------------------------------------------------------
#
# Copyright (c) 2021 by LAB3W : O.Romain Jaillet-ramey - (orj@lab3w.fr)
#
# Date Create : 2020/04/14
# Date Modify : 2020/04/18
#
# Web Site : http://www.lab3w.fr
# Web Site : http://www.lab3w.com
#
# Web Site : http://zw3b.fr
# Web Site : http://zw3b.tv
# Web Site : http://zw3b.site
# Web Site : http://zw3b.net
#
# This module is to manage ---------------------------- :: Descripton
#
# This program is free software. You can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.
######################################################################


class zw3bRdap extends zw3b {
    
    var 
$infoDN = array();
    var 
$infoNS = array();
    var 
$infoAutnum = array();
    var 
$infoIPv4 = array();
    var 
$infoIPv6 = array();

    public 
$rdap;
    
    public function 
__construct()
    {
        return 
$this->rdap;
        
    }
    
    private function 
curl_get_file_contents($URL)
    {
        
$c curl_init();
        
curl_setopt($cCURLOPT_RETURNTRANSFER1);
        
curl_setopt($cCURLOPT_URL$URL);
        
$contents curl_exec($c);
        
curl_close($c);
        
        if (
$contents) return $contents;
        else return 
FALSE;
    }
    
    private function 
uriBootstrapTLD($ext null) {
        
        
$ext = isset($ext) && !empty($ext) ? $ext 'net';
        
        
$bootstrapTLD 'https://data.iana.org/rdap/dns.json';
        
$bootstrapARRAY $this->curl_get_file_contents($bootstrapTLD);
        
$bootstrapARRAY json_decode($bootstrapARRAYtrue);
        
        foreach(
$bootstrapARRAY['services'] as $key => $services):
        
//$servs = $services[$key][0][X];
        //$bootstrap[$ext]['url'] = $services[$key][1][0];
        
foreach($services[0] as $clef => $bootstrapTLD):
        
$bootstrap[$bootstrapTLD]['url']  = $bootstrapARRAY['services'][$key][1][0];
        endforeach;
        endforeach;
        
        
$bootstrapURL'';
        if(
array_key_exists ($ext $bootstrap ))
            
$bootstrapURL $bootstrap[$ext]['url'];
            
            
// Link: https://www.registry.google/rdap/domain/dns.google
            // Link: https://rdap.markmonitor.com/rdap/domain/dns.google
            
            //echo '<pre>'.print_r($bootstrapURL,1).'</pre>';
            //echo '<pre>'.print_r($bootstrap,1).'</pre>';
            
            
return $bootstrapURL;
            
    }
    
    private function 
uriBootstrapIPv4($ipv4 null) {
        
        
$ipv4 = isset($ipv4) && !empty($ipv4) ? $ipv4 '';
        
$ipv4BlOCK strstr($ipv4'.'true);
        
$ipv4BlOCK $ipv4BlOCK.'.0.0.0';
        
        
$bootstrapIPv4 'https://data.iana.org/rdap/ipv4.json';
        
$bootstrapARRAY $this->curl_get_file_contents($bootstrapIPv4);
        
$bootstrapARRAY json_decode($bootstrapARRAYtrue);
        
        foreach(
$bootstrapARRAY['services'] as $key => $services):
        foreach(
$services[0] as $clef => $bootstrapIPv4):
        
$bootstrapIPv4 explode('/'$bootstrapIPv4);
        
$bootstrapIPv4 $bootstrapIPv4[0];
        
$bootstrap[$bootstrapIPv4]['url']  = $bootstrapARRAY['services'][$key][1][0];
        endforeach;
        endforeach;
        
        
$bootstrapURL'';
        if(
array_key_exists ($ipv4BlOCK $bootstrap ))
            
$bootstrapURL $bootstrap[$ipv4BlOCK]['url'];
            
            
//echo '<pre>'.print_r($bootstrapURL,1).'</pre>';
            //echo '<pre>'.print_r($bootstrap,1).'</pre>';
            
            
return $bootstrapURL;
    }
    
    
//---------------------------------------------------------------------------------
    
protected function getInfosEMAIL($request = array()) {
        
        
$request['caption'];
        
$request['value'];
        
        
$request['url'] = 'https://admin.lab3w.com/registry/email/'.$request['value'];
        
        
$request['text'] = 'Ne fait pas partit du service';
        
$request['output'] = '';
        
        return 
$request;
    }
    
    protected function 
getInfosAS($request = array()) {
        
        
$request['caption'];
        
$request['value'];
        
        
$request['url'] = 'https://rdap.arin.net/registry/autnum/'.$request['value'];
        
        return 
$this->getInfosBootstrap($request);
    }
    
    protected function 
getInfosDN($request = array()) {
        
        
$request['caption'];
        
$request['value'];
        
$request['ext'];
        
        
$request['url'] = $this->uriBootstrapTLD($request['ext']).'domain/'.$request['value'];
        
        unset(
$request['ext']);
        
        return 
$this->getInfosBootstrap($request);
    }
    
    protected function 
getInfosNS($request = array()) {
        
        
$request['caption'];
        
$request['value'];
        
$request['ext'];
        
        
$request['url'] = $this->uriBootstrapTLD($request['ext']).'nameserver/'.$request['value'];
        
        unset(
$request['ext']);
        
        return 
$this->getInfosBootstrap($request);
    }
    
    protected function 
getInfosIPv4($request = array()) {
        
        
$request['caption'];
        
$request['value'];
        
        
$request['url'] = $this->uriBootstrapIPv4($request['value']).'ip/'.$request['value'];
        
        return 
$this->getInfosBootstrap($request);
    }
    
    protected function 
getInfosIPv6($request = array()) {
        
        
$info $request;
        
        
$ipv6 = isset($request['value']) && !empty($request['value']) ? $request['value'] : ''// == 2607:5300:0060:9389::
        
        
$bootstrapIPv6 'https://data.iana.org/rdap/ipv6.json';
        
$bootstrapARRAY $this->curl_get_file_contents($bootstrapIPv6);
        
$bootstrapARRAY json_decode($bootstrapARRAYtrue);
        
        foreach(
$bootstrapARRAY['services'] as $key => $services)
            
$urls[] = $bootstrapARRAY['services'][$key][1][0];
            
            foreach(
$urls as $key => $uri):
            
//if($key == 2):
            
$bootstrapURL $uri.'ip/'.$ipv6;
            
            
$output $this->curl_get_file_contents($bootstrapURL);
            
$output json_decode($output);
            
            if(isset(
$output->handle) && !empty($output->handle))
                break;
                
//endif;
                
                
endforeach;
                
                
$info['url'] = $bootstrapURL;
                
$info['output'] = $output;
                
                
//echo '<pre>'.print_r($bootstrapURL,1).'</pre>';
                //echo '<pre>'.print_r($bootstrap,1).'</pre>';
                
                
return $info;
    }
    
    
    private function 
getInfosBootstrap($request = array()) {
        
        
$info $request;
        
        if(isset(
$request['url']) && !empty($request['url'])) {
            
//$output = shell_exec('curl '.$url);
            
$info['output'] = $this->curl_get_file_contents($request['url']);
            
$info['output'] = json_decode($info['output']);
            
        }
        
        if(empty(
$info['output']) && !empty($request['url'])) $info['text'] = ' ==> R&eacute;ponse vide ('.$request['url'].') : soit le num&eacute;ro d&apos;<acronym  title="Autonomous System" lang="EN">AS</acronym>, soit l&apos;adresse <acronym  title="Internet Protocol" lang="EN">IP</acronym>, soit le Serveur de Nom (<acronym  title="Name Server" lang="EN">NS</acronym>), soit le Nom de Domaine (<acronym  title="Domain Name" lang="EN">DN</acronym>) est inconnu ou prot&eacute;g&eacute; (extension de noms de domaine de pays).';
        
        return 
$info;
    }
    
    
    public function 
getInfos($request) {
        
        
$request trim($request);
        
        
//$info['text'] = 'Req ==> ';
        
        
if( preg_match(
            
'#\A'
            
.'[0-9]{1,3}\.+' // 192
            
.'[0-9]{1,3}\.+' // 168
            
.'[0-9]{1,3}\.+' // 10
            
.'[0-9]{1,3}'// 1
            
.'\z#'
            
$request)) {
                
                
$infos['caption'] = "IPv4";
                
$infos['value'] = $request;
                
                
// return
                
$infos $this->getInfosIPv4($infos);
                
            }
            else if( 
preg_match(
                
'#\A'
                
.'[a-zA-Z0-9-]+'
                
.'\.'
                
.'(?:(asso\.)|(gouv\.))?'
                
.'[a-zA-Z]{2,}'
                
.'\z#'
                
$request)
                ) {
                    
                    
$infos['caption'] = 'DN';
                    
$infos['value'] = $request;
                    
                    
$ext strrchr($request'.');
                    
$ext substr($ext1strlen($ext));
                    
$ext strToLower($ext);
                    
                    
$infos['ext'] = $ext;
                    
                    
// return
                    
$infos $this->getInfosDN($infos);
                    
                }
                else if(
substr_count($request'.') >= '2' &&
                    
//(preg_match('#\A[a-zA-Z0-9-.]{8,}\z#', $request)
                    
preg_match('#\A[a-zA-Z0-9-.]{8,}?\.?\z#'$request)
                    ) {
                        
                        
$infos['caption'] = 'DNS';
                        
                        
$request preg_match('#\A[a-zA-Z0-9-.]{8,}\.\z#'$request) ? substr($request0strlen($request)-1)  : $request;
                        
                        
$infos['value'] = $request;
                        
                        
$ext strrchr($request'.');
                        
$ext substr($ext1strlen($ext));
                        
$ext strToLower($ext);
                        
                        
$infos['ext'] = $ext;
                        
                        
// return
                        
$infos $this->getInfosNS($infos);
                        
                    }
                    
//else if(strlen($request) >= '3' && (substr($request, 0, 3) == 'ASN') ) {
                    
else if(strlen($request) >= '3' && preg_match('#\A(?:(?i)(AS)|ASN)+[0-9]{1,}\z#'$request) ) {
                        
                        
$infos['caption'] = 'ASN';
                        
                        if(
substr($request03) == 'ASN')
                            
$request substr($request3strlen($request));
                            elseif(
substr($request02) == 'AS')
                            
$request substr($request2strlen($request));
                            else
                                
$request null;
                                
                                
$infos['value'] = $request;
                                
                                
// return
                                
$infos $this->getInfosAS($infos);
                                
                    }
                    
// Pour faire de l'ereg sous-classes
                    
elseif( preg_match(
                        
'#\A'
                        
.'[a-zA-Z0-9-_.]+'
                        
.'@'
                        
.'[a-zA-Z0-9-]{2,}+'
                        
.'\.'
                        
.'(?:(asso\.)|(gouv\.))?'
                        
.'[a-zA-Z]{2,}'
                        
.'\z#'
                        
$request)) {
                            
                            
$infos['caption'] = 'EMAIL';
                            
                            
$infos['value'] = $request;
                            
                            
//$info['text'].= ''.$REQTYPE.' (Ne fait pas partit du service de <a href="https://en.wikipedia.org/wiki/Registration_Data_Access_Protocol" title="WikipediA : Registration Data Access Protocol">Registration Data Access Protocol</a>)';
                            
                            // return
                            
$infos $this->getInfosEMAIL($infos);
                    }
                    else if(
                        
preg_match('#[a-fA-F0-9:]#'$request) && strstr($request':') && strlen($request) >= 10 && strlen($request) <= 39
                        
&& (
                            
//(substr_count($request, ':') == '2' && strlen($request) >= 6  && strlen($request) <= 10)
                            
(substr_count($request':') == '3' && strlen($request) >= 10 && strlen($request) <= 15// 2001:0bc8::0001
                            
|| (substr_count($request':') == '4' && strlen($request) >= 11  && strlen($request) <= 26// 2001:bc8:25bb::
                            
|| (substr_count($request':') == '5' && strlen($request) >= 11  && strlen($request) <= 31// 2001:0bc8:1:ff00::1234
                            
|| (substr_count($request':') == '6' && strlen($request) >= 11  && strlen($request) <= 36// 2001:bc8:1:0:1::
                            
|| (substr_count($request':') == '7' && strlen($request) >= 13  && strlen($request) <= 39// 2001:bc8:1:0:1:2::
                            
|| (substr_count($request':') == '8' && strlen($request) >= 13  && strlen($request) <= 35 && preg_match('#:\z#'$request)) // 2001:bc8:1:0:1:2::
                            
)
                        ) {
                            
                            
$infos['caption'] = 'IPv6';
                            
                            
$infos['value'] = $request;
                            
                            
// return
                            
$infos $this->getInfosIPv6($infos);
                            
                        }
                        else 
$infos['text'] = 'Error - ';
                        
                        
                        return 
$infos;
    }
    
    
}
?>