fix pingback & request

This commit is contained in:
joyqi 2021-09-15 01:25:36 +08:00
parent 3c4a0022c3
commit 7ef5fcabd5
5 changed files with 39 additions and 27 deletions

View File

@ -136,6 +136,7 @@ class Request
public function get(string $key, $default = null, ?bool &$exists = true)
{
$exists = true;
$value = null;
switch (true) {
case isset($this->params) && isset($this->params[$key]):
@ -145,7 +146,6 @@ class Request
if (isset($this->sandbox[$key])) {
$value = $this->sandbox[$key];
} else {
$value = $default;
$exists = false;
}
break;
@ -156,7 +156,6 @@ class Request
$value = $_POST[$key];
break;
default:
$value = $default;
$exists = false;
break;
}
@ -166,7 +165,7 @@ class Request
$this->params = null;
}
return ((!is_array($value) && strlen($value) > 0) || is_array($default)) ? $value : $default;
return $value ?? $default;
}
/**

View File

@ -100,7 +100,7 @@ class Edit extends PostEdit implements ActionInterface
self::pluginHandle()->finishPublish($contents, $this);
/** 发送ping */
Service::alloc()->sendPing($this->cid);
Service::alloc()->sendPing($this);
/** 设置提示信息 */
Notice::alloc()->set(

View File

@ -289,7 +289,7 @@ class Edit extends Contents implements ActionInterface
/** 发送ping */
$trackback = array_unique(preg_split("/(\r|\n|\r\n)/", trim($this->request->trackback)));
Service::alloc()->sendPing($this->cid, $trackback);
Service::alloc()->sendPing($this, $trackback);
/** 设置提示信息 */
Notice::alloc()->set('post' == $this->type ?

View File

@ -94,6 +94,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
* @property int $defaultCategory
* @property bool $frontArchive
* @property array $plugins
* @property string $secret
*/
class Options extends Base
{

View File

@ -6,6 +6,7 @@ use Typecho\Common;
use Typecho\Http\Client;
use Typecho\Response;
use Typecho\Widget\Exception;
use Widget\Base\Contents;
use Widget\Base\Options as BaseOptions;
if (!defined('__TYPECHO_ROOT_DIR__')) {
@ -36,10 +37,14 @@ class Service extends BaseOptions implements ActionInterface
public function sendPingHandle()
{
/** 验证权限 */
$token = $this->request->token;
$response = ['trackbacks' => [], 'pingbacks' => []];
$token = $this->request->get('token');
$permalink = $this->request->get('permalink');
$title = $this->request->get('title');
$excerpt = $this->request->get('excerpt');
if (!Common::timeTokenValidate($token, $this->options->secret, 3)) {
$response = ['trackback' => [], 'pingback' => []];
if (!Common::timeTokenValidate($token, $this->options->secret, 3) || empty($permalink)) {
throw new Exception(_t('禁止访问'), 403);
}
@ -52,12 +57,9 @@ class Service extends BaseOptions implements ActionInterface
set_time_limit(30);
}
/** 获取post */
$post = Archive::alloc('type=post', "cid={$this->request->cid}");
if ($post->have() && preg_match_all("|<a[^>]*href=[\"'](.*?)[\"'][^>]*>(.*?)</a>|", $post->content, $matches)) {
$links = array_unique($matches[1]);
$permalinkPart = parse_url($post->permalink);
if (!empty($this->request->pingback)) {
$links = $this->request->getArray('pingback');
$permalinkPart = parse_url($permalink);
/** 发送pingback */
foreach ($links as $url) {
@ -95,11 +97,11 @@ class Service extends BaseOptions implements ActionInterface
}
if (!empty($xmlrpcUrl)) {
$response['pingbacks'][] = $url;
$response['pingback'][] = $url;
try {
$xmlrpc = new \IXR\Client($xmlrpcUrl);
$xmlrpc->pingback->ping($post->permalink, $url);
$xmlrpc->pingback->ping($permalink, $url);
unset($xmlrpc);
} catch (\IXR\Exception $e) {
continue;
@ -112,20 +114,20 @@ class Service extends BaseOptions implements ActionInterface
}
/** 发送trackback */
if ($post->have() && !empty($this->request->trackback)) {
$links = array_filter(array_map('trim', explode("\n", $this->request->trackback)));
if (!empty($this->request->trackback)) {
$links = $this->request->getArray('trackback');
foreach ($links as $url) {
$client = Client::get();
$response['trackbacks'][] = $url;
$response['trackback'][] = $url;
if ($client) {
try {
$client->setTimeout(5)
->setData([
'blog_name' => $this->options->title . ' &raquo ' . $post->title,
'url' => $post->permalink,
'excerpt' => $post->excerpt
'blog_name' => $this->options->title . ' &raquo ' . $title,
'url' => $permalink,
'excerpt' => $excerpt
])
->send($url);
@ -143,13 +145,13 @@ class Service extends BaseOptions implements ActionInterface
/**
* 发送pingback
* <code>
* $this->sendPingbacks(365);
* $this->sendPing($post);
* </code>
*
* @param integer $cid 内容id
* @param array|null $trackback trackback的url
* @param Contents $content 内容url
* @param array|null $trackback
*/
public function sendPing($cid, ?array $trackback = null)
public function sendPing(Contents $content, ?array $trackback = null)
{
$this->user->pass('contributor');
@ -157,10 +159,20 @@ class Service extends BaseOptions implements ActionInterface
try {
$input = [
'do' => 'ping',
'cid' => $cid,
'permalink' => $content->permalink,
'excerpt' => $content->excerpt,
'title' => $content->title,
'token' => Common::timeToken($this->options->secret)
];
if (preg_match_all("|<a[^>]*href=[\"'](.*?)[\"'][^>]*>(.*?)</a>|", $content->content, $matches)) {
$pingback = array_unique($matches[1]);
if (!empty($pingback)) {
$input['pingback'] = $pingback;
}
}
if (!empty($trackback)) {
$input['trackback'] = $trackback;
}