Pingback.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace IXR;
  3. use Typecho\Common;
  4. use Typecho\Http\Client as HttpClient;
  5. use Typecho\Http\Client\Exception as HttpException;
  6. /**
  7. * fetch pingback
  8. */
  9. class Pingback
  10. {
  11. /**
  12. * @var string
  13. */
  14. private $html;
  15. /**
  16. * @var string
  17. */
  18. private $target;
  19. /**
  20. * @param string $url
  21. * @param string $target
  22. * @throws Exception
  23. */
  24. public function __construct(string $url, string $target)
  25. {
  26. $client = HttpClient::get();
  27. $this->target = $target;
  28. if (!isset($client)) {
  29. throw new Exception('No available http client', 50);
  30. }
  31. try {
  32. $client->setTimeout(5)
  33. ->send($url);
  34. } catch (HttpException $e) {
  35. throw new Exception('Pingback http error', 50);
  36. }
  37. if ($client->getResponseStatus() != 200) {
  38. throw new Exception('Pingback wrong http status', 50);
  39. }
  40. $response = $client->getResponseBody();
  41. $encoding = 'UTF-8';
  42. $contentType = $client->getResponseHeader('Content-Type');
  43. if (!empty($contentType) && preg_match("/charset=([_a-z0-9-]+)/i", $contentType, $matches)) {
  44. $encoding = strtoupper($matches[1]);
  45. } elseif (preg_match("/<meta\s+charset=\"([_a-z0-9-]+)\"/i", $response, $matches)) {
  46. $encoding = strtoupper($matches[1]);
  47. }
  48. $this->html = $encoding == 'UTF-8' ? $response : mb_convert_encoding($response, 'UTF-8', $encoding);
  49. if (
  50. !$client->getResponseHeader('X-Pingback') &&
  51. !preg_match_all("/<link[^>]*rel=[\"']pingback[\"'][^>]+href=[\"']([^\"']*)[\"'][^>]*>/i", $this->html)
  52. ) {
  53. throw new Exception("Source server doesn't support pingback", 50);
  54. }
  55. }
  56. /**
  57. * get title
  58. *
  59. * @return string
  60. */
  61. public function getTitle(): string
  62. {
  63. if (preg_match("/\<title\>([^<]*?)\<\/title\\>/is", $this->html, $matchTitle)) {
  64. return Common::subStr(Common::removeXSS(trim(strip_tags($matchTitle[1]))), 0, 150, '...');
  65. }
  66. return parse_url($this->target, PHP_URL_HOST);
  67. }
  68. /**
  69. * get content
  70. *
  71. * @return string
  72. * @throws Exception
  73. */
  74. public function getContent(): string
  75. {
  76. /** 干掉html tag,只留下<a>*/
  77. $text = Common::stripTags($this->html, '<a href="">');
  78. /** 此处将$target quote,留着后面用*/
  79. $pregLink = preg_quote($this->target);
  80. /** 找出含有target链接的最长的一行作为$finalText*/
  81. $finalText = null;
  82. $lines = explode("\n", $text);
  83. foreach ($lines as $line) {
  84. $line = trim($line);
  85. if (null != $line) {
  86. if (preg_match("|<a[^>]*href=[\"']{$pregLink}[\"'][^>]*>(.*?)</a>|", $line)) {
  87. if (strlen($line) > strlen($finalText)) {
  88. /** <a>也要干掉,*/
  89. $finalText = Common::stripTags($line);
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. if (!isset($finalText)) {
  96. throw new Exception("Source page doesn't have target url", 50);
  97. }
  98. return '[...]' . Common::subStr($finalText, 0, 200, '') . '[...]';
  99. }
  100. }