2021-02-21 16:37:27 +00:00
< ? php
require_once __DIR__ . '/../vendor/autoload.php' ;
require_once __DIR__ . '/../config.php' ;
/* Initialize Twig engine */
$loader = new \Twig\Loader\FilesystemLoader ( __DIR__ . '/../templates' );
$twig = new \Twig\Environment ( $loader , [
'cache' => __DIR__ . '/../cache' ,
'auto_reload' => true ,
]);
2021-09-13 17:21:23 +00:00
$all = false ;
2021-02-21 16:37:27 +00:00
$record = " " ;
$desc = " " ;
$error = " " ;
$result = [];
2021-09-13 17:21:23 +00:00
if ( isset ( $_POST [ " all " ]) || isset ( $_GET [ " all " ]))
$all = true ;
2021-02-21 16:37:27 +00:00
if ( isset ( $_POST [ " record " ]) && ! empty ( $_POST [ " record " ])) {
$record = ( string ) $_POST [ " record " ];
if ( isset ( $_POST [ " desc " ]) && ! empty ( $_POST [ " desc " ])) {
$desc = ( string ) $_POST [ " desc " ];
}
$pdo = ( new App\DB ( $options )) -> pdo ;
$util = new App\Utils ;
$parsed = $util -> parseHostRecord ( $record );
2022-06-06 17:57:49 +00:00
if ( ! isset ( $parsed [ 'host' ])) {
$result [ " error " ] = " Error while validating: Incorrect Auth string " ;
} else if ( ! $util -> isValidDomain ( $parsed [ 'host' ], $error )) {
2021-02-21 16:37:27 +00:00
$result [ " error " ] = " Error while validating: " . $error ;
2022-06-06 17:57:49 +00:00
2021-02-21 16:37:27 +00:00
} else {
if ( $util -> isPunycodeDomain ( $parsed [ 'host' ])) {
2021-06-13 04:55:08 +00:00
$domain = idn_to_utf8 ( $parsed [ 'host' ], 0 , INTL_IDNA_VARIANT_UTS46 );
2022-06-06 17:57:49 +00:00
2021-02-21 16:37:27 +00:00
} else {
$domain = $parsed [ 'host' ];
}
2021-06-27 16:05:27 +00:00
if ( ! isset ( $parsed [ " commands " ]) || ! isset ( $parsed [ " commands " ][ " sig " ])) {
$result [ " error " ] = " Error while validating: No extended record fields or signature is found. " ;
2021-02-21 16:37:27 +00:00
2021-06-27 16:05:27 +00:00
} else if ( ! $util -> verifyHostRecord ( $record , $error )) {
$result [ " error " ] = " Error while validating: " . $error [ 0 ];
2021-02-21 16:37:27 +00:00
} else {
2021-06-27 16:05:27 +00:00
if ( isset ( $parsed [ " commands " ][ " action " ])) {
2022-06-06 17:57:49 +00:00
/* Check if such domain name already registered */
$STH = $pdo -> query ( " SELECT COUNT(*) FROM `hosts` WHERE `host` = ' " . $domain . " ' LIMIT 1 " );
2021-06-27 16:05:27 +00:00
switch ( $parsed [ " commands " ][ " action " ]) {
case 'addsubdomain' :
if ( $STH -> fetchColumn () == 1 ) {
$result [ " error " ] = " Error while validating: That subdomain is already registered. " ;
2021-02-21 16:37:27 +00:00
2021-06-27 16:05:27 +00:00
} else if ( ! isset ( $parsed [ " commands " ][ " oldname " ]) || ! isset ( $parsed [ " commands " ][ " olddest " ]) || ! isset ( $parsed [ " commands " ][ " oldsig " ])) {
$result [ " error " ] = " Error while validating: required fields not found. Re-check your registration string. " ;
2021-02-21 16:37:27 +00:00
2021-06-27 16:05:27 +00:00
} else {
/* Getting domain at higher level (2LD for registering 3LD and etc.) and validating that domain is lower than 2LD. */
$darr = explode ( " . " , $domain );
$dtop = " " ;
2021-02-21 16:37:27 +00:00
2021-06-27 16:05:27 +00:00
for ( $i = 1 ; $i < sizeof ( $darr ); $i ++ ) {
$dtop .= $darr [ $i ];
if (( sizeof ( $darr ) - 1 ) != $i ) $dtop .= " . " ;
}
if ( sizeof ( $darr ) < 3 ) {
$result [ " error " ] = " Error while validating: you can't register second level domain (example.i2p) using addsubdomain action. " ;
2021-02-21 16:37:27 +00:00
2021-06-27 16:05:27 +00:00
} else if ( $dtop != $parsed [ " commands " ][ " oldname " ]) {
$result [ " error " ] = " Error while validating: oldname value is not same as your higher level domain. " ;
2021-02-21 16:37:27 +00:00
2021-06-27 16:05:27 +00:00
} else if ( ! $pdo -> query ( " SELECT COUNT(*) FROM `hosts` WHERE `host` = ' " . $parsed [ " commands " ][ " oldname " ] . " ' AND `base64` = ' " . $parsed [ " commands " ][ " olddest " ] . " ' LIMIT 1 " ) -> fetchColumn ()) {
$result [ " error " ] = " Error while validating: can't find higher level domain with values from oldname and olddest. " ;
2021-02-21 16:37:27 +00:00
2021-06-27 16:05:27 +00:00
} else {
$base32 = $util -> b32from64 ( $parsed [ " b64 " ]);
if ( ! $pdo -> exec ( " INSERT INTO `hosts` (`host`, `base64`, `base32`) VALUES (' " . $domain . " ', ' " . $parsed [ " b64 " ] . " ', ' " . $base32 . " ') " )) {
$result [ " error " ] = " Error happened while inserting record to database. Please try again later. " ;
2021-02-21 16:37:27 +00:00
} else {
2021-06-27 16:05:27 +00:00
$result [ " command " ] = 'addsubdomain' ;
$result [ " host " ] = $domain ;
$result [ " base64 " ] = $parsed [ " b64 " ];
$result [ " base32 " ] = $base32 ;
2021-02-21 16:37:27 +00:00
}
}
2021-06-27 16:05:27 +00:00
}
break ;
case 'adddest' :
case 'changedest' :
if ( $STH -> fetchColumn () == 0 ) {
$result [ " error " ] = " Error while validating: That domain is not registered. " ;
} else if ( ! isset ( $parsed [ " commands " ][ " olddest " ]) || ! isset ( $parsed [ " commands " ][ " oldsig " ])) {
$result [ " error " ] = " Error while validating: required fields not found. Re-check your registration string. " ;
} else {
if ( ! $pdo -> query ( " SELECT COUNT(*) FROM `hosts` WHERE `host` = ' " . $domain . " ' AND `base64` = ' " . $parsed [ " commands " ][ " olddest " ] . " ' LIMIT 1 " ) -> fetchColumn ()) {
$result [ " error " ] = " Error while validating: old base64 and value in olddest field does not match.. " ;
2021-02-21 16:37:27 +00:00
} else {
2021-06-27 16:05:27 +00:00
$base32 = $util -> b32from64 ( $parsed [ " b64 " ]);
if ( ! $pdo -> exec ( " UPDATE `hosts` SET `base64` = ' " . $parsed [ " b64 " ] . " ', `base32` = ' " . $base32 . " ' WHERE `host` = ' " . $domain . " ' " )) {
$result [ " error " ] = " Error happened while updating record in database. Please try again later. " ;
2021-02-21 16:37:27 +00:00
} else {
2021-06-27 16:05:27 +00:00
$result [ " command " ] = 'changedest' ;
$result [ " host " ] = $domain ;
$result [ " base64 " ] = $parsed [ " b64 " ];
$result [ " base32 " ] = $base32 ;
2021-02-21 16:37:27 +00:00
}
}
2021-06-27 16:05:27 +00:00
}
break ;
case 'addname' :
if ( $STH -> fetchColumn () == 1 ) {
$result [ " error " ] = " Error while validating: That domain is already registered. " ;
/* Getting domain level (2LD, 3LD and etc.) and validating that domain at higher level is registered (2LD if registering 3LD). */
} else if ( sizeof ( explode ( " . " , $domain )) > 2 ) {
2022-06-06 17:57:49 +00:00
$result [ " error " ] = " Error while validating: you can't register subdomain (sub.example.i2p) as domain alias using addname action. " ;
2021-06-27 16:05:27 +00:00
} else if ( ! isset ( $parsed [ " commands " ][ " oldname " ])) {
$result [ " error " ] = " Error while validating: required fields not found. Re-check your registration string. " ;
} else {
if ( $util -> isPunycodeDomain ( $parsed [ " commands " ][ " oldname " ])) {
$olddomain = idn_to_utf8 ( $parsed [ " commands " ][ " oldname " ], 0 , INTL_IDNA_VARIANT_UTS46 );
} else {
$olddomain = $parsed [ " commands " ][ " oldname " ];
}
if ( ! $pdo -> query ( " SELECT COUNT(*) FROM `hosts` WHERE `host` = ' " . $olddomain . " ' AND `base64` = ' " . parsed [ " b64 " ] . " ' LIMIT 1 " ) -> fetchColumn ()) {
$result [ " error " ] = " Error while validating: base64 does not match for domain in oldname field... " ;
2021-02-21 16:37:27 +00:00
} else {
2021-06-27 16:05:27 +00:00
$base32 = $util -> b32from64 ( $parsed [ " b64 " ]);
if ( ! $pdo -> exec ( " INSERT INTO `hosts` (`host`, `base64`, `base32`) VALUES (' " . $domain . " ', ' " . $parsed [ " b64 " ] . " ', ' " . $base32 . " ') " )) {
$result [ " error " ] = " Error happened while updating record in database. Please try again later. " ;
2021-02-21 16:37:27 +00:00
} else {
2021-06-27 16:05:27 +00:00
$result [ " command " ] = 'addname' ;
$result [ " host " ] = $domain ;
$result [ " base64 " ] = $parsed [ " b64 " ];
$result [ " base32 " ] = $base32 ;
2021-02-21 16:37:27 +00:00
}
}
2021-06-27 16:05:27 +00:00
}
break ;
default :
$result [ " error " ] = " Error while validating: extended record fields are NOT supported. " ;
break ;
}
} else {
2022-06-06 17:57:49 +00:00
/* Check if such domain name already registered */
$STH = $pdo -> query ( " SELECT `host`, `base32`, `base64`, `initial`, `disabled` FROM `hosts` WHERE `host` = ' " . $domain . " ' LIMIT 1 " );
$row = $STH -> fetch ( PDO :: FETCH_ASSOC );
2022-06-29 18:39:49 +00:00
if ( $row && ! $row [ 'disabled' ]) {
2021-06-27 16:05:27 +00:00
$result [ " error " ] = " Error while validating: That domain is already registered. " ;
2021-02-21 16:37:27 +00:00
2022-06-29 18:39:49 +00:00
} else if ( $row && $row [ 'disabled' ] && $row [ 'initial' ]) {
2022-06-06 17:57:49 +00:00
$result [ " error " ] = " Error while validating: That domain is reserved and can't be re-registered automatically. Please contact service support team. " ;
2021-02-21 16:37:27 +00:00
} else {
if ( isset ( $parsed [ " commands " ][ " oldname " ]) || isset ( $parsed [ " commands " ][ " olddest " ]) || isset ( $parsed [ " commands " ][ " oldsig " ])) {
$result [ " error " ] = " Error while validating: unexpected fields found. " ;
} else if ( sizeof ( explode ( " . " , $domain )) > 2 ) {
$result [ " error " ] = " Error while validating: you can't register subdomain without specific action field. " ;
} else {
2022-06-06 17:57:49 +00:00
$result [ " reregister " ] = false ;
2022-06-29 18:39:49 +00:00
if ( $row && $row [ 'disabled' ]) { /* processing disabled domain */
2022-06-06 17:57:49 +00:00
$log = " [ " . date ( " d-M-Y H:i:s e " ) . " ] Re-registering attempt for " . $row [ 'host' ] . " ! Next records will be deleted: " . PHP_EOL ;
/* print all records, which will be deleted*/
$STH = $pdo -> query ( " SELECT `host`, `base32`, `base64` FROM `hosts` WHERE `host` = ' " . $domain . " ' OR `host` LIKE '%. " . $domain . " ' " );
$hosts = $STH -> fetchAll ( PDO :: FETCH_ASSOC );
foreach ( $hosts as $host ) {
$log .= " Host: " . $host [ 'host' ] . PHP_EOL . " Base32: " . $host [ 'base32' ] . PHP_EOL . " Base64: " . $host [ 'base64' ] . PHP_EOL ;
}
file_put_contents ( __DIR__ . '/../logs/reg.log' , $log , FILE_APPEND );
/* remove domain and subdomains if any found */
$pdo -> exec ( " DELETE FROM `hosts` WHERE `host` = ' " . $domain . " ' OR `host` LIKE '%. " . $domain . " ' " );
$result [ " reregister " ] = true ;
}
2021-02-21 16:37:27 +00:00
$base32 = $util -> b32from64 ( $parsed [ " b64 " ]);
/* Adding to database 2LD domain */
if ( ! $pdo -> exec ( " INSERT INTO `hosts` (`host`, `base64`, `base32`) VALUES (' " . $domain . " ', ' " . $parsed [ " b64 " ] . " ', ' " . $base32 . " ') " )) {
$result [ " error " ] = " Error happened while inserting record to database. Please try again later. " ;
} else {
2021-06-27 16:05:27 +00:00
$result [ " command " ] = 'added' ;
$result [ " host " ] = $domain ;
$result [ " base64 " ] = $parsed [ " b64 " ];
$result [ " base32 " ] = $base32 ;
2021-02-21 16:37:27 +00:00
}
}
}
}
}
}
} else if ( isset ( $_POST [ " desc " ]) && ! empty ( $_POST [ " desc " ])) {
$result [ " error " ] = " Authentication string is required for registering record. " ;
}
if ( ! empty ( $result )) {
$record = $desc = " " ; // clear them if any result present
}
$pdo = null ;
$template = $twig -> load ( 'add.twig' );
2021-09-13 17:21:23 +00:00
echo $template -> render ([ 'record' => $record , 'desc' => $desc , 'result' => $result , 'all' => $all ]);