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.

150 lines
4.6 KiB

6 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. if($request->get('allow-iframe-video')) {
  60. $opts['allowIframeVideo'] = $request->get('allow-iframe-video') == 'true';
  61. }
  62. if($request->get('ignore-as2')) {
  63. $opts['ignore-as2'] = $request->get('ignore-as2') == 'true';
  64. }
  65. $url = $request->get('url');
  66. $html = $request->get('html') ?: $request->get('body');
  67. if(!$url && !$html) {
  68. return $this->respond($response, 400, [
  69. 'error' => 'missing_url',
  70. 'error_description' => 'Provide a URL or HTML to fetch',
  71. ]);
  72. }
  73. if($html) {
  74. // If HTML is provided in the request, parse that, and use the URL provided as the base URL for mf2 resolving
  75. $result['body'] = $html;
  76. $result['url'] = $url;
  77. $result['code'] = null;
  78. } else {
  79. $fetcher = new p3k\XRay\Fetcher($this->http);
  80. $fields = [
  81. 'twitter_api_key','twitter_api_secret','twitter_access_token','twitter_access_token_secret',
  82. 'github_access_token',
  83. 'token',
  84. 'accept',
  85. ];
  86. foreach($fields as $f) {
  87. if($v=$request->get($f))
  88. $opts[$f] = $v;
  89. }
  90. $result = $fetcher->fetch($url, $opts);
  91. if(!empty($result['error'])) {
  92. $error_code = isset($result['error_code']) ? $result['error_code'] : 200;
  93. unset($result['error_code']);
  94. return $this->respond($response, $error_code, $result);
  95. }
  96. }
  97. $parser = new p3k\XRay\Parser($this->http);
  98. $parsed = $parser->parse($result, $opts);
  99. // Allow the parser to override the HTTP response code, e.g. a meta-equiv tag
  100. if(isset($parsed['code']))
  101. $result['code'] = $parsed['code'];
  102. if(!empty($parsed['error'])) {
  103. $error_code = isset($parsed['error_code']) ? $parsed['error_code'] : 200;
  104. unset($parsed['error_code']);
  105. return $this->respond($response, $error_code, $parsed);
  106. } else {
  107. $data = [
  108. 'data' => $parsed['data'],
  109. 'url' => $result['url'],
  110. 'code' => $result['code'],
  111. ];
  112. if(isset($parsed['info']))
  113. $data['info'] = $parsed['info'];
  114. if($request->get('include_original') && isset($parsed['original']))
  115. $data['original'] = $parsed['original'];
  116. if(isset($parsed['source-format']))
  117. $data['source-format'] = $parsed['source-format'];
  118. if(isset($parsed['url']) && $parsed['url'] != $result['url'])
  119. $data['parsed-url'] = $parsed['url'];
  120. return $this->respond($response, 200, $data);
  121. }
  122. }
  123. }