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.

123 lines
3.3 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. 'error' => 'missing_url',
  27. 'error_description' => 'Provide a URL to fetch'
  28. ]);
  29. }
  30. // Attempt some basic URL validation
  31. $scheme = parse_url($url, PHP_URL_SCHEME);
  32. if(!in_array($scheme, ['http','https'])) {
  33. return $this->respond($response, 400, [
  34. 'error' => 'invalid_url',
  35. 'error_description' => 'Only http and https URLs are supported'
  36. ]);
  37. }
  38. $host = parse_url($url, PHP_URL_HOST);
  39. if(!$host) {
  40. return $this->respond($response, 400, [
  41. 'error' => 'invalid_url',
  42. 'error_description' => 'The URL provided was not valid'
  43. ]);
  44. }
  45. // Now fetch the URL and check for any curl errors
  46. $result = $this->http->get($url);
  47. if($result['error']) {
  48. return $this->respond($response, 400, [
  49. 'error' => $result['error'],
  50. 'error_description' => $result['error_description']
  51. ]);
  52. }
  53. // attempt to parse the page as HTML
  54. $doc = new DOMDocument();
  55. @$doc->loadHTML(self::toHtmlEntities($result['body']));
  56. if(!$doc) {
  57. return $this->respond($response, 400, [
  58. 'error' => 'invalid_content',
  59. 'error_description' => 'The document could not be parsed as HTML'
  60. ]);
  61. }
  62. // If a target parameter was provided, make sure a link to it exists on the page
  63. if($target=$request->get('target')) {
  64. $xpath = new DOMXPath($doc);
  65. $found = [];
  66. foreach($xpath->query('//a[@href]') as $href) {
  67. $url = $href->getAttribute('href');
  68. if($target) {
  69. # target parameter was provided
  70. if($url == $target) {
  71. $found[$url] = null;
  72. }
  73. }
  74. }
  75. if(!$found) {
  76. return $this->respond($response, 400, [
  77. 'error' => 'no_link_found',
  78. 'error_description' => 'The source document does not have a link to the target URL'
  79. ]);
  80. }
  81. }
  82. // Now start pulling in the data from the page. Start by looking for microformats2
  83. $mf2 = mf2\Parse($result['body']);
  84. if($mf2 && count($mf2['items']) > 0) {
  85. $data = Formats\Mf2::parse($mf2);
  86. if($data) {
  87. return $this->respond($response, 200, [
  88. 'data' => $data,
  89. 'mf2' => $mf2
  90. ]);
  91. }
  92. }
  93. // TODO: look for other content like OEmbed or known services later
  94. return $this->respond($response, 400, [
  95. 'error' => 'no_content',
  96. 'error_description' => 'No usable content could be found at the given URL'
  97. ]);
  98. }
  99. }