-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
112 lines (95 loc) · 3.66 KB
/
plugin.php
File metadata and controls
112 lines (95 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
if (!defined("IN_ESO")) exit;
/**
* Turnstile plugin: uses the Cloudflare Turnstile API to provide a captcha
* that prevents bots from joining the forum.
*/
class Turnstile extends Plugin {
var $id = "Turnstile";
var $name = "Turnstile";
var $version = "1.0";
var $description = "Provides verification using the Cloudflare Turnstile captcha";
var $author = "grntbg";
var $defaultConfig = array(
"captchaSecret" => NULL,
"captchaKey" => NULL
);
function init()
{
parent::init();
// Add language definitions and messages.
$this->eso->addLanguage("Are you human", "Are you human?");
$this->eso->addMessage("tryAgain", "warning", "Please fill out the captcha.");
// Add the Cloudflare script and hook to the join controller so we can add Turnstile to the form.
if ($this->eso->action == "join") {
$this->eso->addScript("https://challenges.cloudflare.com/turnstile/v0/api.js", -1);
$this->eso->controller->addHook("init", array($this, "initCaptchaForm"));
}
}//init
// Add the captcha fieldset and input to the join form.
function initCaptchaForm(&$join)
{
global $config, $language;
$join->addFieldset("verifyTurnstile", $language["Are you human"], 900);
$join->addToForm("verifyTurnstile", array(
"id" => "turnstile",
"html" => "<div class='cf-turnstile' data-sitekey='{$config["Turnstile"]["captchaKey"]}'></div>",
"validate" => array($this, "validateCaptcha"),
"required" => true
));
}//initCaptchaForm
// Validate the captcha input.
function validateCaptcha($input)
{
global $config;
if (!$_POST["cf-turnstile-response"]) return "tryAgain";
if (isset($_POST["cf-turnstile-response"]) && !empty($_POST["cf-turnstile-response"])) {
$path = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
$data = array(
"secret" => $config["Turnstile"]["captchaSecret"],
"response" => $_POST['cf-turnstile-response'],
"remoteip" => $_SERVER["REMOTE_ADDR"]
);
$options = array(
"http" => array(
"method" => "POST",
"content" => http_build_query($data)
)
);
$result = file_get_contents($path, false, stream_context_create($options));
$responseKeys = json_decode($result, true);
if (!$responseKeys["success"]) return "tryAgain";
} else {
return "tryAgain";
}
}//validateCaptcha
// Plugin settings: captcha preview and how many characters to use.
function settings()
{
global $config, $language;
// Add language definitions.
$this->eso->addMessage("cfCaptchaEmpty", "warning", "Please enter a valid Turnstile CAPTCHA key.");
$this->eso->addLanguage("cfCaptchaKey", "CAPTCHA site key");
$this->eso->addLanguage("cfCaptchaSecret", "CAPTCHA secret key");
// Generate settings panel HTML.
$settingsHTML = "<ul class='form'>
<li><label>{$language["cfCaptchaKey"]}</label> <input name='Turnstile[captchaKey]' type='text' class='text' value='{$config["Turnstile"]["captchaKey"]}'/></li>
<li><label>{$language["cfCaptchaSecret"]}</label> <input name='Turnstile[captchaSecret]' type='text' class='text' value='{$config["Turnstile"]["captchaSecret"]}'/></li>
<li><label></label> " . $this->eso->skin->button(array("value" => $language["Save changes"], "name" => "saveSettings")) . "</li>
</ul>";
return $settingsHTML;
}//setings
// Save the plugin settings.
function saveSettings()
{
global $config;
if (empty($_POST["Turnstile"]["captchaKey"]) or empty($_POST["Turnstile"]["captchaSecret"])) $this->eso->message("cfCaptchaEmpty");
else {
$config["Turnstile"]["captchaKey"] = $_POST["Turnstile"]["captchaKey"];
$config["Turnstile"]["captchaSecret"] = $_POST["Turnstile"]["captchaSecret"];
writeConfigFile("config/Turnstile.php", '$config["Turnstile"]', $config["Turnstile"]);
$this->eso->message("changesSaved");
}
}//saveSettings
}//reCAPTCHA
?>