@ -28,7 +28,7 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
@@ -28,7 +28,7 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
$parsed = $util->parseHostRecord($record);
if (!$util->isValidAddress ($parsed['host'], $error)) {
if (!$util->isValidDomain ($parsed['host'], $error)) {
$result["error"] = "Error while validating: " . $error;
} else {
if ($util->isPunycodeDomain($parsed['host'])) {
@ -37,13 +37,6 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
@@ -37,13 +37,6 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
$domain = $parsed['host'];
}
/* Check if such domain name already registered */
$STH = $pdo->query("SELECT COUNT(*) FROM `hosts` WHERE `host` = '" . $domain . "' LIMIT 1");
if($STH->fetchColumn() == 1) {
$result["error"] = "Error while validating: That domain is already registered.";
} else {
if (!isset($parsed["commands"]) || !isset($parsed["commands"]["sig"])) {
$result["error"] = "Error while validating: No extended record fields or signature is found.";
@ -51,10 +44,16 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
@@ -51,10 +44,16 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
$result["error"] = "Error while validating: " . $error[0];
} else {
/* Check if such domain name already registered */
$STH = $pdo->query("SELECT COUNT(*) FROM `hosts` WHERE `host` = '" . $domain . "' LIMIT 1");
if (isset($parsed["commands"]["action"])) {
switch ($parsed["commands"]["action"]) {
case 'addsubdomain':
if (!isset($parsed["commands"]["oldname"]) || !isset($parsed["commands"]["olddest"]) || !isset($parsed["commands"]["oldsig"])) {
if ($STH->fetchColumn() == 1) {
$result["error"] = "Error while validating: That subdomain is already registered.";
} 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.";
} else {
@ -82,6 +81,7 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
@@ -82,6 +81,7 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
$result["error"] = "Error happened while inserting record to database. Please try again later.";
} else {
$result["command"] = 'addsubdomain';
$result["host"] = $domain;
$result["base64"] = $parsed["b64"];
$result["base32"] = $base32;
@ -91,7 +91,10 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
@@ -91,7 +91,10 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
break;
case 'adddest':
case 'changedest':
if (!isset($parsed["commands"]["olddest"]) || !isset($parsed["commands"]["oldsig"])) {
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 {
@ -104,6 +107,7 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
@@ -104,6 +107,7 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
$result["error"] = "Error happened while updating record in database. Please try again later.";
} else {
$result["command"] = 'changedest';
$result["host"] = $domain;
$result["base64"] = $parsed["b64"];
$result["base32"] = $base32;
@ -112,19 +116,34 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
@@ -112,19 +116,34 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
}
break;
case 'addname':
if (!isset($parsed["commands"]["olddest"]) || !isset($parsed["commands"]["oldsig"])) {
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) {
$result["error"] = "Error while validating: you can't register subdomain (sub.example.i2p) as domain alias using addname action.";
} else if (!isset($parsed["commands"]["oldname"])) {
$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..";
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...";
} else {
$base32 = $util->b32from64($parsed["b64"]);
if (!$pdo->exec("UPDATE `hosts` SET `base64` = '" . $parsed["b64"] . "', `base32` = '" . $base32 . "' WHERE `host` = '" . $domain . "'")) {
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.";
} else {
$result["command"] = 'addname';
$result["host"] = $domain;
$result["base64"] = $parsed["b64"];
$result["base32"] = $base32;
@ -133,10 +152,14 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
@@ -133,10 +152,14 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
}
break;
default:
$result["error"] = "Error while validating: extended record fields are NOT supported for now .";
$result["error"] = "Error while validating: extended record fields are NOT supported.";
break;
}
} else {
if($STH->fetchColumn() == 1) {
$result["error"] = "Error while validating: That domain is already registered.";
} else {
if (isset($parsed["commands"]["oldname"]) || isset($parsed["commands"]["olddest"]) || isset($parsed["commands"]["oldsig"])) {
$result["error"] = "Error while validating: unexpected fields found.";
@ -152,6 +175,7 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
@@ -152,6 +175,7 @@ if (isset($_POST["record"]) && !empty($_POST["record"])) {
$result["error"] = "Error happened while inserting record to database. Please try again later.";
} else {
$result["command"] = 'added';
$result["host"] = $domain;
$result["base64"] = $parsed["b64"];
$result["base32"] = $base32;