diff --git a/var/Typecho/Request.php b/var/Typecho/Request.php index 310d86e2..8cb1e5ef 100644 --- a/var/Typecho/Request.php +++ b/var/Typecho/Request.php @@ -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; } /** diff --git a/var/Widget/Contents/Page/Edit.php b/var/Widget/Contents/Page/Edit.php index 7418e515..58f27420 100644 --- a/var/Widget/Contents/Page/Edit.php +++ b/var/Widget/Contents/Page/Edit.php @@ -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( diff --git a/var/Widget/Contents/Post/Edit.php b/var/Widget/Contents/Post/Edit.php index 63995afe..0131aae6 100644 --- a/var/Widget/Contents/Post/Edit.php +++ b/var/Widget/Contents/Post/Edit.php @@ -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 ? diff --git a/var/Widget/Options.php b/var/Widget/Options.php index ad55ecde..210ff379 100644 --- a/var/Widget/Options.php +++ b/var/Widget/Options.php @@ -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 { diff --git a/var/Widget/Service.php b/var/Widget/Service.php index abcac02f..e359caec 100644 --- a/var/Widget/Service.php +++ b/var/Widget/Service.php @@ -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 . ' » ' . $post->title, - 'url' => $post->permalink, - 'excerpt' => $post->excerpt + 'blog_name' => $this->options->title . ' » ' . $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; }