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.

173 lines
5.0 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. echo "Sending result to callback URL: $webmention->callback\n";
  59. return self::$http->post($webmention->callback, $payload);
  60. }
  61. }
  62. public static function send($id, $client=false, $http=false) {
  63. initdb();
  64. $webmention = ORM::for_table('webmentions')->where('id', $id)->find_one();
  65. if(!$webmention) {
  66. echo 'Webmention '.$id.' was not found'."\n";
  67. return;
  68. }
  69. echo "Processing webmention $id from ".$webmention->source."\n";
  70. if(!$client)
  71. $client = new MentionClient();
  72. if(!$http)
  73. $http = new HTTP();
  74. self::$http = $http;
  75. echo "Finding webmention endpoint for $webmention->target\n";
  76. // Discover the webmention or pingback endpoint
  77. $endpoint = $client->discoverWebmentionEndpoint($webmention->target);
  78. if(!$endpoint) {
  79. echo "No webmention endpoint, checking for pingback\n";
  80. // If no webmention endpoint found, try to send a pingback
  81. $pingbackEndpoint = $client->discoverPingbackEndpoint($webmention->target);
  82. // If no pingback endpoint was found, we can't do anything else
  83. if(!$pingbackEndpoint) {
  84. echo "No webmention or pingback endpoint found\n";
  85. return self::updateStatus($webmention, null, 'not_supported');
  86. }
  87. echo "Found pingback endpoint $pingbackEndpoint\n";
  88. $webmention->pingback_endpoint = $pingbackEndpoint;
  89. $webmention->save();
  90. echo "Sending pingback now...\n";
  91. $success = $client->sendPingbackToEndpoint($pingbackEndpoint, $webmention->source, $webmention->target);
  92. echo "Result: ".($success?'accepted':'error')."\n";
  93. return self::updateStatus($webmention, null, ($success ? 'accepted' : 'error'));
  94. }
  95. echo "Found webmention endpoint $endpoint\n";
  96. // There is a webmention endpoint, send the webmention now
  97. $webmention->webmention_endpoint = $endpoint;
  98. $webmention->save();
  99. $params = [];
  100. if($webmention->code) {
  101. $params['code'] = $webmention->code;
  102. if($webmention->realm)
  103. $params['realm'] = $webmention->realm;
  104. }
  105. if($webmention->vouch) {
  106. $params['vouch'] = $webmention->vouch;
  107. }
  108. echo "Sending webmention now...\n";
  109. $response = $client->sendWebmentionToEndpoint($endpoint, $webmention->source, $webmention->target, $params);
  110. echo "Response code: ".$response['code']."\n";
  111. if(in_array($response['code'], [200,201,202])) {
  112. $status = 'accepted';
  113. $webmention->complete = $response['code'] == 200 ? 1 : 0;
  114. // Check if the endpoint returned a status URL
  115. if(array_key_exists('Location', $response['headers'])) {
  116. $webmention->webmention_status_url = \Mf2\resolveUrl($endpoint, $response['headers']['Location']);
  117. echo "Endpoint returned a status URL: ".$webmention->webmention_status_url."\n";
  118. // TODO: queue a job to poll the endpoint for updates and deliver to the callback URL
  119. } else {
  120. // No status URL was returned, so we can't follow up with this later. Mark as complete.
  121. $webmention->complete = 1;
  122. }
  123. } else {
  124. $webmention->complete = 1;
  125. $status = 'error';
  126. }
  127. $webmention->save();
  128. $result = self::updateStatus($webmention, $response['code'], $status, $response['body']);
  129. echo "Done\n";
  130. $pdo = ORM::get_db();
  131. $pdo = null;
  132. return $result;
  133. }
  134. }