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.

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