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.

129 lines
3.9 KiB

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