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.

142 lines
4.4 KiB

3 years ago
  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use p3k\XRay\Formats;
  5. class Parse {
  6. public $http;
  7. public $mc;
  8. private $_cacheTime = 120;
  9. private $_pretty = false;
  10. private static $_version = '1.4.25';
  11. public static function useragent() {
  12. return 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 XRay/'.self::$_version.' ('.\Config::$base.')';
  13. }
  14. public function __construct() {
  15. $this->http = new p3k\HTTP(self::useragent());
  16. if(Config::$cache && class_exists('Memcache')) {
  17. $this->mc = new Memcache();
  18. $this->mc->addServer('127.0.0.1');
  19. }
  20. }
  21. public static function debug($msg, $header='X-Parse-Debug') {
  22. syslog(LOG_INFO, $msg);
  23. if(array_key_exists('REMOTE_ADDR', $_SERVER))
  24. header($header . ": " . $msg);
  25. }
  26. private function respond(Response $response, $code, $params, $headers=[]) {
  27. $response->setStatusCode($code);
  28. foreach($headers as $k=>$v) {
  29. $response->headers->set($k, $v);
  30. }
  31. $response->headers->set('Content-Type', 'application/json');
  32. $response->headers->set('Version', 'XRay/'.self::$_version.' php-mf2/'.p3k\XRay\phpmf2_version());
  33. $opts = JSON_UNESCAPED_SLASHES;
  34. if($this->_pretty) $opts += JSON_PRETTY_PRINT;
  35. $response->setContent(json_encode($params, $opts)."\n");
  36. return $response;
  37. }
  38. public function parse(Request $request, Response $response) {
  39. $opts = [];
  40. if($request->get('timeout')) {
  41. // We might make 2 HTTP requests, so each request gets half the desired timeout
  42. $opts['timeout'] = $request->get('timeout') / 2;
  43. }
  44. if($request->get('max_redirects') !== null) {
  45. $opts['max_redirects'] = (int)$request->get('max_redirects');
  46. }
  47. if($request->get('target')) {
  48. $opts['target'] = $request->get('target');
  49. }
  50. if($request->get('expect')) {
  51. $opts['expect'] = $request->get('expect');
  52. }
  53. if($request->get('pretty')) {
  54. $this->_pretty = true;
  55. }
  56. if($request->get('include-mf1')) {
  57. $opts['include-mf1'] = $request->get('include-mf1') == 'false' ? false : true;
  58. }
  59. $url = $request->get('url');
  60. $html = $request->get('html') ?: $request->get('body');
  61. if(!$url && !$html) {
  62. return $this->respond($response, 400, [
  63. 'error' => 'missing_url',
  64. 'error_description' => 'Provide a URL or HTML to fetch',
  65. ]);
  66. }
  67. if($html) {
  68. // If HTML is provided in the request, parse that, and use the URL provided as the base URL for mf2 resolving
  69. $result['body'] = $html;
  70. $result['url'] = $url;
  71. $result['code'] = null;
  72. } else {
  73. $fetcher = new p3k\XRay\Fetcher($this->http);
  74. $fields = [
  75. 'twitter_api_key','twitter_api_secret','twitter_access_token','twitter_access_token_secret',
  76. 'facebook_app_id', 'facebook_app_secret',
  77. 'github_access_token',
  78. 'token'
  79. ];
  80. foreach($fields as $f) {
  81. if($v=$request->get($f))
  82. $opts[$f] = $v;
  83. }
  84. $result = $fetcher->fetch($url, $opts);
  85. if(!empty($result['error'])) {
  86. $error_code = isset($result['error_code']) ? $result['error_code'] : 200;
  87. unset($result['error_code']);
  88. return $this->respond($response, $error_code, $result);
  89. }
  90. }
  91. $parser = new p3k\XRay\Parser($this->http);
  92. $parsed = $parser->parse($result, $opts);
  93. // Allow the parser to override the HTTP response code, e.g. a meta-equiv tag
  94. if(isset($parsed['code']))
  95. $result['code'] = $parsed['code'];
  96. if(!empty($parsed['error'])) {
  97. $error_code = isset($parsed['error_code']) ? $parsed['error_code'] : 200;
  98. unset($parsed['error_code']);
  99. return $this->respond($response, $error_code, $parsed);
  100. } else {
  101. $data = [
  102. 'data' => $parsed['data'],
  103. 'url' => $result['url'],
  104. 'code' => $result['code'],
  105. ];
  106. if(isset($parsed['info']))
  107. $data['info'] = $parsed['info'];
  108. if($request->get('include_original') && isset($parsed['original']))
  109. $data['original'] = $parsed['original'];
  110. if(isset($parsed['source-format']))
  111. $data['source-format'] = $parsed['source-format'];
  112. if(isset($parsed['url']) && $parsed['url'] != $result['url'])
  113. $data['parsed-url'] = $parsed['url'];
  114. return $this->respond($response, 200, $data);
  115. }
  116. }
  117. }