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.

199 lines
5.7 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class API {
  5. public $http;
  6. public function __construct() {
  7. $this->http = new Telegraph\HTTP();
  8. }
  9. private function respond(Response $response, $code, $params, $headers=[]) {
  10. $response->setStatusCode($code);
  11. foreach($headers as $k=>$v) {
  12. $response->headers->set($k, $v);
  13. }
  14. $response->setContent(json_encode($params));
  15. return $response;
  16. }
  17. private static function toHtmlEntities($input) {
  18. return mb_convert_encoding($input, 'HTML-ENTITIES', mb_detect_encoding($input));
  19. }
  20. private static function generateStatusToken() {
  21. $str = dechex(date('y'));
  22. $chs = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  23. $len = strlen($chs);
  24. for($i = 0; $i < 16; $i++) {
  25. $str .= $chs[mt_rand(0, $len - 1)];
  26. }
  27. return $str;
  28. }
  29. public function webmention(Request $request, Response $response) {
  30. # Require the token parameter
  31. if(!$token=$request->get('token')) {
  32. return $this->respond($response, 401, [
  33. 'error' => 'authentication_required',
  34. 'error_description' => 'A token is required to use the API'
  35. ]);
  36. }
  37. # Require source and target parameters
  38. if((!$source=$request->get('source')) || (!$target=$request->get('target'))) {
  39. return $this->respond($response, 400, [
  40. 'error' => 'missing_parameters',
  41. 'error_description' => 'The source or target parameters were missing'
  42. ]);
  43. }
  44. $urlregex = '/^https?:\/\/[^ ]+\.[^ ]+$/';
  45. # Verify source and target are URLs
  46. if(!preg_match($urlregex, $source) || !preg_match($urlregex, $target)) {
  47. return $this->respond($response, 400, [
  48. 'error' => 'invalid_parameter',
  49. 'error_description' => 'The source or target parameters were invalid'
  50. ]);
  51. }
  52. # If a callback was provided, verify it is a URL
  53. if($callback=$request->get('callback')) {
  54. if(!preg_match($urlregex, $source) || !preg_match($urlregex, $target)) {
  55. return $this->respond($response, 400, [
  56. 'error' => 'invalid_parameter',
  57. 'error_description' => 'The callback parameter was invalid'
  58. ]);
  59. }
  60. }
  61. # Verify the token is valid
  62. $role = ORM::for_table('roles')->where('token', $token)->find_one();
  63. if(!$role) {
  64. return $this->respond($response, 401, [
  65. 'error' => 'invalid_token',
  66. 'error_description' => 'The token provided is not valid'
  67. ]);
  68. }
  69. # Synchronously check the source URL and verify that it actually contains
  70. # a link to the target. This way we prevent this API from sending known invalid mentions.
  71. $sourceData = $this->http->get($source);
  72. $doc = new DOMDocument();
  73. @$doc->loadHTML(self::toHtmlEntities($sourceData['body']));
  74. if(!$doc) {
  75. return $this->respond($response, 400, [
  76. 'error' => 'source_not_html',
  77. 'error_description' => 'The source document could not be parsed as HTML'
  78. ]);
  79. }
  80. $xpath = new DOMXPath($doc);
  81. $found = false;
  82. foreach($xpath->query('//a[@href]') as $href) {
  83. if($href->getAttribute('href') == $target) {
  84. $found = true;
  85. continue;
  86. }
  87. }
  88. if(!$found) {
  89. return $this->respond($response, 400, [
  90. 'error' => 'no_link_found',
  91. 'error_description' => 'The source document does not have a link to the target URL'
  92. ]);
  93. }
  94. # Everything checked out, so write the webmention to the log and queue a job to start sending
  95. $w = ORM::for_table('webmentions')->create();
  96. $w->site_id = $role->site_id;
  97. $w->created_by = $role->user_id;
  98. $w->created_at = date('Y-m-d H:i:s');
  99. $w->token = self::generateStatusToken();
  100. $w->source = $source;
  101. $w->target = $target;
  102. $w->vouch = $request->get('vouch');
  103. $w->callback = $callback;
  104. $w->save();
  105. q()->queue('Telegraph\Webmention', 'send', [$w->id]);
  106. $statusURL = Config::$base . 'webmention/' . $w->token;
  107. return $this->respond($response, 201, [
  108. 'status' => 'queued',
  109. 'location' => $statusURL
  110. ], [
  111. 'Location' => $statusURL
  112. ]);
  113. }
  114. public function webmention_status(Request $request, Response $response, $args) {
  115. $webmention = ORM::for_table('webmentions')->where('token', $args['code'])->find_one();
  116. if(!$webmention) {
  117. return $this->respond($response, 404, [
  118. 'status' => 'not_found',
  119. ]);
  120. }
  121. $status = ORM::for_table('webmention_status')->where('webmention_id', $webmention->id)->order_by_desc('created_at')->find_one();
  122. $statusURL = Config::$base . 'webmention/' . $webmention->token;
  123. if(!$status) {
  124. $code = 'queued';
  125. } else {
  126. $code = $status->status;
  127. }
  128. $data = [
  129. 'status' => $code,
  130. ];
  131. if($webmention->webmention_endpoint) {
  132. $data['type'] = 'webmention';
  133. $data['endpoint'] = $webmention->webmention_endpoint;
  134. }
  135. if($webmention->pingback_endpoint) {
  136. $data['type'] = 'pingback';
  137. $data['endpoint'] = $webmention->pingback_endpoint;
  138. }
  139. switch($code) {
  140. case 'queued':
  141. $summary = 'The webmention is still in the processing queue';
  142. break;
  143. case 'not_supported':
  144. $summary = 'No webmention or pingback endpoint were found at the target';
  145. break;
  146. case 'accepted':
  147. $summary = 'The '.$data['type'].' request was accepted';
  148. break;
  149. default:
  150. $summary = false;
  151. }
  152. if($status && $status->http_code)
  153. $data['http_code'] = (int)$status->http_code;
  154. if($summary)
  155. $data['summary'] = $summary;
  156. if($webmention->complete == 0)
  157. $data['location'] = $statusURL;
  158. return $this->respond($response, 200, $data);
  159. }
  160. }