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.

125 lines
3.4 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Percolator\Formats;
  5. class Parse {
  6. public $http;
  7. public function __construct() {
  8. $this->http = new p3k\HTTP();
  9. }
  10. private function respond(Response $response, $code, $params, $headers=[]) {
  11. $response->setStatusCode($code);
  12. foreach($headers as $k=>$v) {
  13. $response->headers->set($k, $v);
  14. }
  15. $response->headers->set('Content-Type', 'application/json');
  16. $response->setContent(json_encode($params));
  17. return $response;
  18. }
  19. private static function toHtmlEntities($input) {
  20. return mb_convert_encoding($input, 'HTML-ENTITIES', mb_detect_encoding($input));
  21. }
  22. public function parse(Request $request, Response $response) {
  23. $url = $request->get('url');
  24. if(!$url) {
  25. return $this->respond($response, 400, [
  26. 'type' => 'error',
  27. 'error' => 'missing_url',
  28. 'error_description' => 'Provide a URL to fetch'
  29. ]);
  30. }
  31. // Attempt some basic URL validation
  32. $scheme = parse_url($url, PHP_URL_SCHEME);
  33. if(!in_array($scheme, ['http','https'])) {
  34. return $this->respond($response, 400, [
  35. 'type' => 'error',
  36. 'error' => 'invalid_url',
  37. 'error_description' => 'Only http and https URLs are supported'
  38. ]);
  39. }
  40. $host = parse_url($url, PHP_URL_HOST);
  41. if(!$host) {
  42. return $this->respond($response, 400, [
  43. 'type' => 'error',
  44. 'error' => 'invalid_url',
  45. 'error_description' => 'The URL provided was not valid'
  46. ]);
  47. }
  48. // Now fetch the URL and check for any curl errors
  49. $result = $this->http->get($url);
  50. if($result['error']) {
  51. return $this->respond($response, 400, [
  52. 'type' => 'error',
  53. 'error' => $result['error'],
  54. 'error_description' => $result['error_description']
  55. ]);
  56. }
  57. // attempt to parse the page as HTML
  58. $doc = new DOMDocument();
  59. @$doc->loadHTML(self::toHtmlEntities($result['body']));
  60. if(!$doc) {
  61. return $this->respond($response, 400, [
  62. 'type' => 'error',
  63. 'error' => 'invalid_content',
  64. 'error_description' => 'The document could not be parsed as HTML'
  65. ]);
  66. }
  67. // If a target parameter was provided, make sure a link to it exists on the page
  68. if($target=$request->get('target')) {
  69. $xpath = new DOMXPath($doc);
  70. $found = [];
  71. foreach($xpath->query('//a[@href]') as $href) {
  72. $url = $href->getAttribute('href');
  73. if($target) {
  74. # target parameter was provided
  75. if($url == $target) {
  76. $found[$url] = null;
  77. }
  78. }
  79. }
  80. if(!$found) {
  81. return $this->respond($response, 400, [
  82. 'type' => 'error',
  83. 'error' => 'no_link_found',
  84. 'error_description' => 'The source document does not have a link to the target URL'
  85. ]);
  86. }
  87. }
  88. // Now start pulling in the data from the page. Start by looking for microformats2
  89. $mf2 = mf2\Parse($result['body']);
  90. if($mf2 && count($mf2['items']) > 0) {
  91. $data = Formats\Mf2::parse($mf2);
  92. return $this->respond($response, 200, $data);
  93. }
  94. // TODO: look for other content like OEmbed or known services later
  95. return $this->respond($response, 400, [
  96. 'type' => 'error',
  97. 'error' => 'no_content',
  98. 'error_description' => 'No usable content could be found at the given URL'
  99. ]);
  100. }
  101. }