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.

134 lines
3.6 KiB

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