Skip to content

Instantly share code, notes, and snippets.

@ahgsql
Forked from lazenes/auto_plesk_trial.php
Created May 25, 2024 23:26
Show Gist options
  • Save ahgsql/e639dfab7be5d650e66e03ac272b8a12 to your computer and use it in GitHub Desktop.
Save ahgsql/e639dfab7be5d650e66e03ac272b8a12 to your computer and use it in GitHub Desktop.
Auto Plesk Trial Renewal (using Anti-Captcha to solve ReCaptcha)
<?php
/*
Make sure to enter $anticaptcha_key before upload to server.
Upload "auto_plesk_trial.php" file to "/home/centos/auto_plesk_trial.php"
Then create Schedule Task in Root account of Plesk.
Task Type: Run a PHP script
Script path: /home/centos/auto_plesk_trial.php
Use PHP version: 7.x.x / 8.x.x
Run: Cron style * * * * *
Notify: Errors only
*/
$update_frequency = 60 * 60 * 24 * 14; // Update Every 14 Days
$anticaptcha_key = ""; // Anti-Recaptcha Key
set_time_limit(600);
$filename = dirname($_SERVER["PHP_SELF"])."/".pathinfo($_SERVER["PHP_SELF"], PATHINFO_FILENAME).".json";
function update_log ($text) {
$fh = fopen(dirname($_SERVER["PHP_SELF"])."/".pathinfo($_SERVER["PHP_SELF"], PATHINFO_FILENAME).".log", "a");
fwrite($fh, date("[Y-m-d H:i:s] ").$text."\n");
fclose($fh);
echo date("[Y-m-d H:i:s] ").$text."\n";
}
$info = json_decode(@file_get_contents($filename), true);
if (!is_array($info)) $info = array();
if (!isset($info["last_updated"])) $info["last_updated"] = 0;
if ($info["last_updated"] + $update_frequency > time()) {
exit();
}
$curl_options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => "AutoPleskTrial/2.0",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSLVERSION => defined("CURL_SSLVERSION_TLSv1_3") ? CURL_SSLVERSION_TLSv1_3 : CURL_SSLVERSION_TLSv1_2
);
$ch = curl_init();
if (!isset($info["email_address"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_URL => "https://web2.temp-mail.org/mailbox"
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200) {
$data = json_decode($response, true);
if (isset($data["mailbox"]) && isset($data["token"])) {
$info["email_address"] = $data["mailbox"];
$info["email_token"] = $data["token"];
file_put_contents($filename, json_encode($info));
update_log("Update: email_address = ".$info["email_address"]);
} else {
update_log("Error: email_address = null");
exit();
}
} else {
update_log("Error: email_address = null");
exit();
}
}
if (!isset($info["task_id"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_URL => "https://api.anti-captcha.com/createTask",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
CURLOPT_POSTFIELDS => json_encode(array(
"clientKey" => $anticaptcha_key,
"task" => array(
"type" => "RecaptchaV2TaskProxyless",
"websiteURL" => "https://www.plesk.com/plesk-free-download/",
"websiteKey" => "6LfC_lQaAAAAAES5qQ5CZbVfE1_RoA_azMlBZQkN"
)
))
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 200 && $result = json_decode($response, true)) {
if (isset($result["errorId"]) && $result["errorId"] == 0 && isset($result["taskId"])) {
$info["task_id"] = $result["taskId"];
file_put_contents($filename, json_encode($info));
update_log("Update: task_id = ".$info["task_id"]);
exit();
} else {
update_log("Error: task_id = null");
exit();
}
} else {
update_log("Error: task_id = null");
exit();
}
}
if (!isset($info["g_recaptcha_response"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_URL => "https://api.anti-captcha.com/getTaskResult",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
CURLOPT_POSTFIELDS => json_encode(array(
"clientKey" => $anticaptcha_key,
"taskId" => $info["task_id"]
))
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 200 && $result = json_decode($response, true)) {
if (isset($result["errorId"]) && $result["errorId"] == 0 && isset($result["status"])) {
if ($result["status"] == "ready" && isset($result["solution"]["gRecaptchaResponse"])) {
$info["g_recaptcha_response"] = $result["solution"]["gRecaptchaResponse"];
file_put_contents($filename, json_encode($info));
update_log("Update: g_recaptcha_response = ".$info["g_recaptcha_response"]);
} elseif ($result["status"] !== "processing") {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: g_recaptcha_response = null");
exit();
} else {
exit();
}
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: g_recaptcha_response = null");
exit();
}
} else {
update_log("Error: task_id = null");
exit();
}
}
if (!isset($info["confirm_requested"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_URL => "https://www.plesk.com/plesk-free-download/",
CURLOPT_POSTFIELDS => http_build_query(array(
"input_1.3" => "John",
"input_1.6" => "Scott",
"input_2" => $info["email_address"],
"input_15" => "Developer",
"input_16" => "Junior",
"input_17" => "Canada",
"input_21.1" => "true",
"g-recaptcha-response" => $info["g_recaptcha_response"],
"is_submit_55" => "1",
"gform_submit" => "55",
"gf_zero_spam_key" => "eN4*@MnEel3KQ!o!zyCZK5j*Om\$wQ82kI10BI$*AW7\$IKutxZerPTGUoGTRKxor@"
)),
CURLOPT_HEADER => true
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 302 && preg_match("/[Ll]ocation: https:\/\/www\.plesk\.com\/free-trial-for-web-professionals-thank-you-page\//", $response)) {
$info["confirm_requested"] = true;
file_put_contents($filename, json_encode($info));
update_log("Update: confirm_requested = true");
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: confirm_requested = false");
exit();
}
}
if (!isset($info["trial_requested"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => "https://www.plesk.com/free-trial-for-web-professionals-thank-you-page/",
CURLOPT_HTTPHEADER => array(
"Cookie: Plesk_TrialFormData=".urlencode(base64_encode(json_encode(array(
"name" => "John",
"lastname" => "Scott",
"email" => $info["email_address"]
))))
),
CURLOPT_HEADER => true
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 200 && preg_match("/[Ss]et-[Cc]ookie: Plesk_TrialForm_Status=1;/", $response)) {
$info["trial_requested"] = true;
file_put_contents($filename, json_encode($info));
update_log("Update: trial_requested = true");
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: trial_requested = false");
exit();
}
}
if (!isset($info["email_id"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => "https://web2.temp-mail.org/messages",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$info["email_token"]
)
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200) {
$data = json_decode($response, true);
if (isset($data["messages"]) && is_array($data["messages"])) {
foreach ($data["messages"] as $mail) {
if ($mail["subject"] === "Please confirm your email address") {
$info["email_id"] = $mail["_id"];
file_put_contents($filename, json_encode($info));
update_log("Update: email_id = ".$info["email_id"]);
break;
}
}
if (!isset($info["email_id"])) {
exit();
}
} else {
exit();
}
} else {
update_log("Error: email_id = null");
exit();
}
}
if (!isset($info["confirm_link"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => "https://web2.temp-mail.org/messages/".$info["email_id"],
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$info["email_token"]
)
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200 && $data = json_decode($response, true)) {
if (isset($data["bodyHtml"]) && preg_match("/please confirm your email address by clicking on the link below:(?:.*?)<a(?:.*?)href=\"(.*?)\"(?:.*?)>(?:.*?)CONFIRM MY EMAIL(?:.*?)<\/a>/s", $data["bodyHtml"], $matches)) {
$info["confirm_link"] = $matches[1];
file_put_contents($filename, json_encode($info));
update_log("Update: confirm_link = ".$info["confirm_link"]);
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: confirm_link = null");
exit();
}
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: confirm_link = null");
exit();
}
}
if (!isset($info["confirm_link2"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => $info["confirm_link"]
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200 && preg_match("/window\.location\.replace\(\"(.*?)\"\);/", $response, $matches)) {
$info["confirm_link2"] = $matches[1];
file_put_contents($filename, json_encode($info));
update_log("Update: confirm_link2 = ".$info["confirm_link2"]);
} else {
update_log("Error: confirm_link2 = null");
exit();
}
}
if (!isset($info["confirm_link3"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => $info["confirm_link2"],
CURLOPT_HEADER => true
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 307 && preg_match("/[Ll]ocation: (https:\/\/api(.*)\.hubapi\.com\/email\/v1\/optin\/confirm\/doi\?(?:[^\r\n]*))/", $response, $matches)) {
$info["confirm_link3"] = $matches[1];
file_put_contents($filename, json_encode($info));
update_log("Update: confirm_link3 = ".$info["confirm_link3"]);
} else {
update_log("Error: confirm_link3 = null");
exit();
}
}
if (!isset($info["email_confirmed"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => $info["confirm_link3"],
CURLOPT_HEADER => true
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 307 && preg_match("/[Ll]ocation: https:\/\/page\.plesk\.com\/confirmsubscribe/", $response)) {
$info["email_confirmed"] = true;
file_put_contents($filename, json_encode($info));
update_log("Update: email_confirmed = true");
} else {
update_log("Error: email_confirmed = false");
exit();
}
}
if (!isset($info["email_id2"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => "https://web2.temp-mail.org/messages",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$info["email_token"]
)
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200) {
$data = json_decode($response, true);
if (isset($data["messages"]) && is_array($data["messages"])) {
foreach ($data["messages"] as $mail) {
if ($mail["subject"] === "Your Plesk Trial Activation Code") {
$info["email_id2"] = $mail["_id"];
file_put_contents($filename, json_encode($info));
update_log("Update: email_id2 = ".$info["email_id2"]);
break;
}
}
if (!isset($info["email_id2"])) {
exit();
}
} else {
exit();
}
} else {
update_log("Error: email_id2 = null");
exit();
}
}
if (!isset($info["activation_code"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => "https://web2.temp-mail.org/messages/".$info["email_id2"],
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$info["email_token"]
)
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200 && $data = json_decode($response, true)) {
if (isset($data["bodyHtml"]) && preg_match("/Now here's your unique activation code:(?:.*?)>([A-Z0-9]{6}-[A-Z0-9]{6}-[A-Z0-9]{6}-[A-Z0-9]{6}-[A-Z0-9]{6})</s", $data["bodyHtml"], $matches)) {
$info["activation_code"] = $matches[1];
file_put_contents($filename, json_encode($info));
update_log("Update: activation_code = ".$info["activation_code"]);
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: activation_code = null");
exit();
}
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: activation_code = null");
exit();
}
}
$status = trim(shell_exec("/usr/sbin/plesk bin license --install ".$info["activation_code"]));
if ($status === "The license key was successfully installed.") {
$info = array(
"last_updated" => time()
);
file_put_contents($filename, json_encode($info));
update_log("Update: status = ".$status);
update_log("Update: last_updated = ".$info["last_updated"]);
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: status = ".$status);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment