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.

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