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.

146 lines
4.0 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. private $_pretty = false;
  8. public function __construct() {
  9. $this->http = new p3k\HTTP();
  10. }
  11. private function respond(Response $response, $code, $params, $headers=[]) {
  12. $response->setStatusCode($code);
  13. foreach($headers as $k=>$v) {
  14. $response->headers->set($k, $v);
  15. }
  16. $response->headers->set('Content-Type', 'application/json');
  17. $opts = JSON_UNESCAPED_SLASHES;
  18. if($this->_pretty) $opts += JSON_PRETTY_PRINT;
  19. $response->setContent(json_encode($params, $opts)."\n");
  20. return $response;
  21. }
  22. private static function toHtmlEntities($input) {
  23. return mb_convert_encoding($input, 'HTML-ENTITIES', mb_detect_encoding($input));
  24. }
  25. public function parse(Request $request, Response $response) {
  26. if($request->get('timeout')) {
  27. // We might make 2 HTTP requests, so each request gets half the desired timeout
  28. $this->http->timeout = $request->get('timeout') / 2;
  29. }
  30. if($request->get('max_redirects')) {
  31. $this->http->max_redirects = (int)$request->get('max_redirects');
  32. }
  33. if($request->get('pretty')) {
  34. $this->_pretty = true;
  35. }
  36. $url = $request->get('url');
  37. $html = $request->get('html');
  38. if(!$url && !$html) {
  39. return $this->respond($response, 400, [
  40. 'error' => 'missing_url',
  41. 'error_description' => 'Provide a URL or HTML to fetch'
  42. ]);
  43. }
  44. if($html) {
  45. // If HTML is provided in the request, parse that, and use the URL provided as the base URL for mf2 resolving
  46. $result['body'] = $html;
  47. } else {
  48. // Attempt some basic URL validation
  49. $scheme = parse_url($url, PHP_URL_SCHEME);
  50. if(!in_array($scheme, ['http','https'])) {
  51. return $this->respond($response, 400, [
  52. 'error' => 'invalid_url',
  53. 'error_description' => 'Only http and https URLs are supported'
  54. ]);
  55. }
  56. $host = parse_url($url, PHP_URL_HOST);
  57. if(!$host) {
  58. return $this->respond($response, 400, [
  59. 'error' => 'invalid_url',
  60. 'error_description' => 'The URL provided was not valid'
  61. ]);
  62. }
  63. $url = \normalize_url($url);
  64. // Now fetch the URL and check for any curl errors
  65. $result = $this->http->get($url);
  66. if($result['error']) {
  67. return $this->respond($response, 400, [
  68. 'error' => $result['error'],
  69. 'error_description' => $result['error_description']
  70. ]);
  71. }
  72. }
  73. // attempt to parse the page as HTML
  74. $doc = new DOMDocument();
  75. @$doc->loadHTML(self::toHtmlEntities($result['body']));
  76. if(!$doc) {
  77. return $this->respond($response, 400, [
  78. 'error' => 'invalid_content',
  79. 'error_description' => 'The document could not be parsed as HTML'
  80. ]);
  81. }
  82. // If a target parameter was provided, make sure a link to it exists on the page
  83. if($target=$request->get('target')) {
  84. $xpath = new DOMXPath($doc);
  85. $found = [];
  86. foreach($xpath->query('//a[@href]') as $href) {
  87. $u = $href->getAttribute('href');
  88. if($target) {
  89. # target parameter was provided
  90. if($u == $target) {
  91. $found[$u] = null;
  92. }
  93. }
  94. }
  95. if(!$found) {
  96. return $this->respond($response, 400, [
  97. 'error' => 'no_link_found',
  98. 'error_description' => 'The source document does not have a link to the target URL'
  99. ]);
  100. }
  101. }
  102. // Now start pulling in the data from the page. Start by looking for microformats2
  103. $mf2 = mf2\Parse($result['body'], $url);
  104. if($mf2 && count($mf2['items']) > 0) {
  105. $data = Formats\Mf2::parse($mf2, $url, $this->http);
  106. if($data) {
  107. return $this->respond($response, 200, $data);
  108. }
  109. }
  110. // TODO: look for other content like OEmbed or other known services later
  111. return $this->respond($response, 200, [
  112. 'data' => [
  113. 'type' => 'unknown',
  114. ]
  115. ]);
  116. }
  117. }