fix xmlrpc

This commit is contained in:
joyqi 2021-09-01 00:04:05 +08:00
parent d72d4ea2ab
commit 49eed7b437
5 changed files with 623 additions and 607 deletions

View File

@ -28,10 +28,8 @@ class Error
/**
* 构造函数
*
* @access public
* @param integer $code 错误代码
* @param string|null $message 错误消息
* @return void
*/
public function __construct(int $code, ?string $message)
{
@ -42,10 +40,9 @@ class Error
/**
* 获取xml
*
* @access public
* @return string
*/
public function getXml()
public function getXml(): string
{
return <<<EOD
<methodResponse>

View File

@ -2,10 +2,6 @@
namespace IXR;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* IXR异常类
*

120
var/IXR/Pingback.php Normal file
View File

@ -0,0 +1,120 @@
<?php
namespace IXR;
use Typecho\Common;
use Typecho\Http\Client as HttpClient;
use Typecho\Http\Client\Exception as HttpException;
/**
* fetch pingback
*/
class Pingback
{
/**
* @var string
*/
private $html;
/**
* @var string
*/
private $target;
/**
* @param string $url
* @param string $target
* @throws Exception
*/
public function __construct(string $url, string $target)
{
$client = HttpClient::get();
$this->target = $target;
if (!isset($client)) {
throw new Exception('No available http client');
}
try {
$client->setTimeout(5)
->send($url);
} catch (HttpException $e) {
throw new Exception('Pingback http error');
}
if ($client->getResponseStatus() != 200) {
throw new Exception('Pingback wrong http status');
}
$response = $client->getResponseBody();
$encoding = 'UTF-8';
$contentType = $client->getResponseHeader('Content-Type');
if (!empty($contentType) && preg_match("/charset=([_a-z0-9-]+)/i", $contentType, $matches)) {
$encoding = strtoupper($matches[1]);
} elseif (preg_match("/<meta\s+charset=\"([_a-z0-9-]+)\"/i", $response, $matches)) {
$encoding = strtoupper($matches[1]);
}
$this->html = $encoding == 'UTF-8' ? $response : mb_convert_encoding($response, 'UTF-8', $encoding);
if (
!$client->getResponseHeader('X-Pingback') &&
!preg_match_all("/<link[^>]*rel=[\"']pingback[\"'][^>]+href=[\"']([^\"']*)[\"'][^>]*>/i", $this->html)
) {
throw new Exception("Source server doesn't support pingback");
}
}
/**
* get title
*
* @return string
*/
public function getTitle(): string
{
if (preg_match("/\<title\>([^<]*?)\<\/title\\>/is", $this->html, $matchTitle)) {
return Common::subStr(Common::removeXSS(trim(strip_tags($matchTitle[1]))), 0, 150, '...');
}
return parse_url($this->target, PHP_URL_HOST);
}
/**
* get content
*
* @return string
* @throws Exception
*/
public function getContent(): string
{
/** 干掉html tag只留下<a>*/
$text = Common::stripTags($this->html, '<a href="">');
/** 此处将$target quote,留着后面用*/
$pregLink = preg_quote($this->target);
/** 找出含有target链接的最长的一行作为$finalText*/
$finalText = null;
$lines = explode("\n", $text);
foreach ($lines as $line) {
$line = trim($line);
if (null != $line) {
if (preg_match("|<a[^>]*href=[\"']{$pregLink}[\"'][^>]*>(.*?)</a>|", $line)) {
if (strlen($line) > strlen($finalText)) {
/** <a>也要干掉,*/
$finalText = Common::stripTags($line);
break;
}
}
}
}
if (!isset($finalText)) {
throw new Exception("Source page doesn't have target url");
}
return '[...]' . Common::subStr($finalText, 0, 200, '') . '[...]';
}
}

View File

@ -43,27 +43,40 @@ class Date
*/
public $timeStamp = 0;
/**
* @var string
*/
public $year;
/**
* @var string
*/
public $month;
/**
* @var string
*/
public $day;
/**
* 初始化参数
*
* @access public
*
* @param integer|null $time 时间戳
*/
public function __construct(?int $time = null)
{
$this->timeStamp = (null === $time ? self::time() : $time)
+ (self::$timezoneOffset - self::$serverTimezoneOffset);
$this->year = date('Y', $this->timeStamp);
$this->month = date('m', $this->timeStamp);
$this->day = date('d', $this->timeStamp);
}
/**
* 设置当前期望的时区偏移
*
* @access public
*
* @param integer $offset
*
* @return void
*/
public static function setTimezoneOffset(int $offset)
{
@ -74,10 +87,7 @@ class Date
/**
* 获取格式化时间
*
* @access public
*
* @param string $format 时间格式
*
* @return string
*/
public function format(string $format): string
@ -88,7 +98,6 @@ class Date
/**
* 获取国际化偏移时间
*
* @access public
* @return string
*/
public function word(): string
@ -96,29 +105,6 @@ class Date
return I18n::dateWord($this->timeStamp, self::time() + (self::$timezoneOffset - self::$serverTimezoneOffset));
}
/**
* 获取单项数据
*
* @access public
*
* @param string $name 名称
*
* @return integer
*/
public function __get(string $name)
{
switch ($name) {
case 'year':
return date('Y', $this->timeStamp);
case 'month':
return date('m', $this->timeStamp);
case 'day':
return date('d', $this->timeStamp);
default:
return 0;
}
}
/**
* 获取GMT时间
*

File diff suppressed because it is too large Load Diff