You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.3 KiB

  1. <?php
  2. namespace Telegraph;
  3. use ORM, Exception;
  4. use IndieWeb\MentionClient;
  5. class Webmention {
  6. private static $http = false;
  7. // Returns false if the target URL is known to not accept webmentions
  8. public static function isProbablySupported($targetURL) {
  9. // Reject links that are known to not accept webmentions
  10. $host = str_replace('www.','',parse_url($targetURL, PHP_URL_HOST));
  11. if(!$host) return false;
  12. $unsupported = [
  13. 'twitter.com',
  14. 'instagram.com',
  15. 'facebook.com',
  16. 'meetup.com',
  17. 'eventbrite.com',
  18. 'eventbrite.co.uk',
  19. 'github.com',
  20. 'blog.github.com',
  21. 'gitlab.com',
  22. 't.co',
  23. ];
  24. if(in_array($host, $unsupported))
  25. return false;
  26. if(preg_match('/.+\.amazonaws\.com/', $host))
  27. return false;
  28. return true;
  29. }
  30. private static function updateStatus($webmention, $http_code, $code, $raw=null) {
  31. $status = ORM::for_table('webmention_status')->create();
  32. $status->webmention_id = $webmention->id;
  33. $status->created_at = date('Y-m-d H:i:s');
  34. if($http_code)
  35. $status->http_code = $http_code;
  36. $status->status = $code;
  37. if($raw)
  38. $status->raw_response = $raw;
  39. $status->save();
  40. // Post to the callback URL if one is set
  41. if($webmention->callback) {
  42. $payload = [
  43. 'source' => $webmention->source,
  44. 'target' => $webmention->target,
  45. 'status' => $code,
  46. ];
  47. if($webmention->webmention_endpoint) {
  48. $payload['type'] = 'webmention';
  49. } elseif($webmention->pingback_endpoint) {
  50. $payload['type'] = 'pingback';
  51. }
  52. if($status->http_code) {
  53. $payload['http_code'] = $status->http_code;
  54. }
  55. if($raw) {
  56. $payload['http_body'] = $raw;
  57. }
  58. return self::$http->post($webmention->callback, $payload);
  59. }
  60. }
  61. public static function send($id, $client=false, $http=false) {
  62. initdb();
  63. $webmention = ORM::for_table('webmentions')->where('id', $id)->find_one();
  64. if(!$webmention) {
  65. echo 'Webmention '.$id.' was not found'."\n";
  66. return;
  67. }
  68. if(!$client)
  69. $client = new MentionClient();
  70. if(!$http)
  71. $http = new HTTP();
  72. self::$http = $http;
  73. // Discover the webmention or pingback endpoint
  74. $endpoint = $client->discoverWebmentionEndpoint($webmention->target);
  75. if(!$endpoint) {
  76. // If no webmention endpoint found, try to send a pingback
  77. $pingbackEndpoint = $client->discoverPingbackEndpoint($webmention->target);
  78. // If no pingback endpoint was found, we can't do anything else
  79. if(!$pingbackEndpoint) {
  80. return self::updateStatus($webmention, null, 'not_supported');
  81. }
  82. $webmention->pingback_endpoint = $pingbackEndpoint;
  83. $webmention->save();
  84. $success = $client->sendPingbackToEndpoint($pingbackEndpoint, $webmention->source, $webmention->target);
  85. return self::updateStatus($webmention, null, ($success ? 'accepted' : 'error'));
  86. }
  87. // There is a webmention endpoint, send the webmention now
  88. $webmention->webmention_endpoint = $endpoint;
  89. $webmention->save();
  90. $params = [];
  91. if($webmention->code) {
  92. $params['code'] = $webmention->code;
  93. if($webmention->realm)
  94. $params['realm'] = $webmention->realm;
  95. }
  96. if($webmention->vouch) {
  97. $params['vouch'] = $webmention->vouch;
  98. }
  99. $response = $client->sendWebmentionToEndpoint($endpoint, $webmention->source, $webmention->target, $params);
  100. if(in_array($response['code'], [200,201,202])) {
  101. $status = 'accepted';
  102. $webmention->complete = $response['code'] == 200 ? 1 : 0;
  103. // Check if the endpoint returned a status URL
  104. if(array_key_exists('Location', $response['headers'])) {
  105. $webmention->webmention_status_url = \Mf2\resolveUrl($endpoint, $response['headers']['Location']);
  106. // TODO: queue a job to poll the endpoint for updates and deliver to the callback URL
  107. } else {
  108. // No status URL was returned, so we can't follow up with this later. Mark as complete.
  109. $webmention->complete = 1;
  110. }
  111. } else {
  112. $webmention->complete = 1;
  113. $status = 'error';
  114. }
  115. $webmention->save();
  116. $result = self::updateStatus($webmention, $response['code'], $status, $response['body']);
  117. $pdo = ORM::get_db();
  118. $pdo = null;
  119. return $result;
  120. }
  121. }