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.

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