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.

127 lines
3.8 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. public static function useragent() {
  11. 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/1.0.0 ('.\Config::$base.')';
  12. }
  13. public function __construct() {
  14. $this->http = new p3k\HTTP(self::useragent());
  15. if(Config::$cache && class_exists('Memcache')) {
  16. $this->mc = new Memcache();
  17. $this->mc->addServer('127.0.0.1');
  18. }
  19. }
  20. public static function debug($msg, $header='X-Parse-Debug') {
  21. syslog(LOG_INFO, $msg);
  22. if(array_key_exists('REMOTE_ADDR', $_SERVER))
  23. header($header . ": " . $msg);
  24. }
  25. private function respond(Response $response, $code, $params, $headers=[]) {
  26. $response->setStatusCode($code);
  27. foreach($headers as $k=>$v) {
  28. $response->headers->set($k, $v);
  29. }
  30. $response->headers->set('Content-Type', 'application/json');
  31. $opts = JSON_UNESCAPED_SLASHES;
  32. if($this->_pretty) $opts += JSON_PRETTY_PRINT;
  33. $response->setContent(json_encode($params, $opts)."\n");
  34. return $response;
  35. }
  36. public function parse(Request $request, Response $response) {
  37. $opts = [];
  38. if($request->get('timeout')) {
  39. // We might make 2 HTTP requests, so each request gets half the desired timeout
  40. $opts['timeout'] = $request->get('timeout') / 2;
  41. }
  42. if($request->get('max_redirects') !== null) {
  43. $opts['max_redirects'] = (int)$request->get('max_redirects');
  44. }
  45. if($request->get('target')) {
  46. $opts['target'] = $request->get('target');
  47. }
  48. if($request->get('pretty')) {
  49. $this->_pretty = true;
  50. }
  51. $url = $request->get('url');
  52. $html = $request->get('html') ?: $request->get('body');
  53. if(!$url && !$html) {
  54. return $this->respond($response, 400, [
  55. 'error' => 'missing_url',
  56. 'error_description' => 'Provide a URL or HTML to fetch',
  57. ]);
  58. }
  59. if($html) {
  60. // If HTML is provided in the request, parse that, and use the URL provided as the base URL for mf2 resolving
  61. $result['body'] = $html;
  62. $result['url'] = $url;
  63. $result['code'] = null;
  64. } else {
  65. $fetcher = new p3k\XRay\Fetcher($this->http);
  66. $fields = [
  67. 'twitter_api_key','twitter_api_secret','twitter_access_token','twitter_access_token_secret',
  68. 'github_access_token',
  69. 'token'
  70. ];
  71. foreach($fields as $f) {
  72. if($v=$request->get($f))
  73. $opts[$f] = $v;
  74. }
  75. $result = $fetcher->fetch($url, $opts);
  76. if(!empty($result['error'])) {
  77. $error_code = isset($result['error_code']) ? $result['error_code'] : 200;
  78. unset($result['error_code']);
  79. return $this->respond($response, $error_code, $result);
  80. }
  81. }
  82. $parser = new p3k\XRay\Parser($this->http);
  83. $parsed = $parser->parse($result['body'], $result['url'], $opts);
  84. // Allow the parser to override the HTTP response code, e.g. a meta-equiv tag
  85. if(isset($parsed['code']))
  86. $result['code'] = $parsed['code'];
  87. if(!empty($parsed['error'])) {
  88. $error_code = isset($parsed['error_code']) ? $parsed['error_code'] : 200;
  89. unset($parsed['error_code']);
  90. return $this->respond($response, $error_code, $parsed);
  91. } else {
  92. $data = [
  93. 'data' => $parsed['data'],
  94. 'url' => $result['url'],
  95. 'code' => $result['code']
  96. ];
  97. if(isset($parsed['info']))
  98. $data['info'] = $parsed['info'];
  99. if($request->get('include_original') && isset($parsed['original']))
  100. $data['original'] = $parsed['original'];
  101. return $this->respond($response, 200, $data);
  102. }
  103. }
  104. }