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.

113 lines
2.9 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class Parse {
  5. public $http;
  6. public function __construct() {
  7. $this->http = new p3k\HTTP();
  8. }
  9. private function respond(Response $response, $code, $params, $headers=[]) {
  10. $response->setStatusCode($code);
  11. foreach($headers as $k=>$v) {
  12. $response->headers->set($k, $v);
  13. }
  14. $response->headers->set('Content-Type', 'application/json');
  15. $response->setContent(json_encode($params));
  16. return $response;
  17. }
  18. private static function toHtmlEntities($input) {
  19. return mb_convert_encoding($input, 'HTML-ENTITIES', mb_detect_encoding($input));
  20. }
  21. public function parse(Request $request, Response $response) {
  22. $url = $request->get('url');
  23. if(!$url) {
  24. return $this->respond($response, 400, [
  25. 'type' => 'error',
  26. 'error' => 'missing_url',
  27. 'error_description' => 'Provide a URL to fetch'
  28. ]);
  29. }
  30. // Attempt some basic URL validation
  31. $scheme = parse_url($url, PHP_URL_SCHEME);
  32. if(!in_array($scheme, ['http','https'])) {
  33. return $this->respond($response, 400, [
  34. 'type' => 'error',
  35. 'error' => 'invalid_url',
  36. 'error_description' => 'Only http and https URLs are supported'
  37. ]);
  38. }
  39. $host = parse_url($url, PHP_URL_HOST);
  40. if(!$host) {
  41. return $this->respond($response, 400, [
  42. 'type' => 'error',
  43. 'error' => 'invalid_url',
  44. 'error_description' => 'The URL provided was not valid'
  45. ]);
  46. }
  47. // Now fetch the URL and check for any curl errors
  48. $result = $this->http->get($url);
  49. if($result['error']) {
  50. return $this->respond($response, 400, [
  51. 'type' => 'error',
  52. 'error' => $result['error'],
  53. 'error_description' => $result['error_description']
  54. ]);
  55. }
  56. // attempt to parse the page as HTML
  57. $doc = new DOMDocument();
  58. @$doc->loadHTML(self::toHtmlEntities($result['body']));
  59. if(!$doc) {
  60. return $this->respond($response, 400, [
  61. 'type' => 'error',
  62. 'error' => 'invalid_content',
  63. 'error_description' => 'The document could not be parsed as HTML'
  64. ]);
  65. }
  66. // If a target parameter was provided, make sure a link to it exists on the page
  67. if($target=$request->get('target')) {
  68. $xpath = new DOMXPath($doc);
  69. $found = [];
  70. foreach($xpath->query('//a[@href]') as $href) {
  71. $url = $href->getAttribute('href');
  72. if($target) {
  73. # target parameter was provided
  74. if($url == $target) {
  75. $found[$url] = null;
  76. }
  77. }
  78. }
  79. if(!$found) {
  80. return $this->respond($response, 400, [
  81. 'type' => 'error',
  82. 'error' => 'no_link_found',
  83. 'error_description' => 'The source document does not have a link to the target URL'
  84. ]);
  85. }
  86. }
  87. return $this->respond($response, 200, [
  88. 'url' => $url,
  89. ]);
  90. }
  91. }