您可以编写使用 XML-RPC 的 php 脚本来测试登录密码是否正确。所有 wordpress 方法都有用户名和密码,因此您可以选择wp.getOptions之类的任何方法(这是安全的,因为它不会更改任何内容,但您需要进行身份验证),如果用户/密码输入错误,它将返回错误 403是对的这是php代码:
<?php
function post($url, $data) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($ch);
}
function authenticate($user, $password) {
$request = '<?xml version="1.0"?>
<methodCall>
<methodName>wp.getOptions</methodName>
<params>
<param>
<value><int>0</int></value>
</param>
<param>
<value><string>' . $user . '</string></value>
</param>
<param>
<value><string>' . $password . '</string></value>
</param>
</params>
</methodCall>';
$url = 'http://www.domektkaczki.pl/xmlrpc.php';
$regex = '/<name>faultCode<\/name>.*<value><int>403<\/int><\/value>/s';
return !preg_match($regex, post($url, $request));
}
header('Content-Type: text/plain');
if (isset($_GET['user']) && isset($_GET['passwd'])) {
echo authenticate($_GET['user'], $_GET['passwd']) ? 'true' : 'false';
} else {
echo 'false';
}
然后在javascript中,您只需在终端中调用该脚本
$('body').terminal($.noop, {
login: function(user, passwd, callback) {
$.getJSON('auth.php', {user:user, passwd:passwd}, function(response) {
if (response) {
// the page redirected_page.php need to be password protected
// because users will be able to see the code and open that page directly
// you can use sessions to save value if user put right credentials
// in auth.php and in redirected_page.php you can check
// if that value is set
window.location = 'redirected_page.php';
} else {
callback(null);
}
});
}
});
如果您想实现更多的 wordpress 命令,您可以使用xmlrpc 库,这里是如何创建客户端的代码。如果您想在 javascript 中使用命令,您的登录应该返回一个可以在 javascript 中使用的令牌。
编辑:如果您想登录管理界面并(从终端)登录,那么您可以检查 wp-login,打开开发人员工具,检查网络选项卡中的保留日志并查看发送到服务器的请求。然后您可以在登录功能中使用 AJAX 而不是我编写的 php 脚本来执行相同的请求。您需要检查当您输入错误密码时发送什么响应,并在正确时需要,并使用与 JS 代码中相同的位置和回调(空)。