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.

52 lines
1.4 KiB

  1. <?php
  2. namespace Telegraph;
  3. use ORM, Exception;
  4. use IndieWeb\MentionClient;
  5. class Webmention {
  6. private static function saveStatus($webmentionID, $code, $raw=null) {
  7. $status = ORM::for_table('webmention_status')->create();
  8. $status->webmention_id = $webmentionID;
  9. $status->created_at = date('Y-m-d H:i:s');
  10. $status->status = $code;
  11. $status->save();
  12. }
  13. public static function send($id, $client=false) {
  14. $webmention = ORM::for_table('webmentions')->where('id', $id)->find_one();
  15. if(!$webmention) {
  16. echo 'Webmention '.$id.' was not found'."\n";
  17. return;
  18. }
  19. if(!$client)
  20. $client = new MentionClient();
  21. // Discover the webmention or pingback endpoint
  22. $endpoint = $client->discoverWebmentionEndpoint($webmention->target);
  23. if(!$endpoint) {
  24. // If no webmention endpoint found, try to send a pingback
  25. $pingbackEndpoint = $client->discoverPingbackEndpoint($webmention->target);
  26. // If no pingback endpoint was found, we can't do anything else
  27. if(!$pingbackEndpoint) {
  28. self::saveStatus($id, 'not_supported');
  29. return;
  30. }
  31. $webmention->pingback_endpoint = $pingbackEndpoint;
  32. $webmention->save();
  33. $success = $client->sendPingbackToEndpoint($pingbackEndpoint, $webmention->source, $webmention->target);
  34. self::saveStatus($id, $success ? 'pingback_success' : 'pingback_error');
  35. return;
  36. }
  37. }
  38. }