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.

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