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.

138 lines
4.2 KiB

  1. <?php
  2. namespace p3k\XRay\Formats;
  3. use DOMDocument, DOMXPath;
  4. use DateTime, DateTimeZone;
  5. class Facebook extends Format {
  6. public static function matches_host($url) {
  7. $host = parse_url($url, PHP_URL_HOST);
  8. // TODO: match hosts like 'm.facebook.com' and 'mbasic.facebook.com'
  9. return in_array($host, ['www.facebook.com','facebook.com']);
  10. }
  11. public static function matches($url) {
  12. return self::matches_host($url);
  13. }
  14. public static function parse($fbObject, $url) {
  15. if(is_string($fbObject)) $fbObject = json_decode($fbObject, true);
  16. $parts = self::extract_url_parts($url);
  17. if($parts['type'] == 'event') {
  18. $event = array(
  19. 'type' => 'event',
  20. 'url' => $url,
  21. 'name' => $fbObject['name'],
  22. 'start' => $fbObject['start_time']
  23. );
  24. if(isset($fbObject['end_time'])) $event['end'] = $fbObject['end_time'];
  25. if(isset($fbObject['description'])) $event['summary'] = $fbObject['description'];
  26. // Is the event linked to a Page?
  27. if(isset($fbObject['place']['id'])) {
  28. $card = array(
  29. 'type' => 'card',
  30. 'url' => 'https://facebook.com/'.$fbObject['place']['id'],
  31. 'name' => $fbObject['place']['name']
  32. );
  33. if(isset($fbObject['place']['location'])) {
  34. $location = $fbObject['place']['location'];
  35. if(isset($location['zip'])) $card['postal-code'] = $location['zip'];
  36. if(isset($location['city'])) $card['locality'] = $location['city'];
  37. if(isset($location['state'])) $card['region'] = $location['state'];
  38. if(isset($location['street'])) $card['street-address'] = $location['street'];
  39. if(isset($location['country'])) $card['country'] = $location['country'];
  40. if(isset($location['latitude'])) $card['latitude'] = (string)$location['latitude'];
  41. if(isset($location['longitude'])) $card['longitude'] = (string)$location['longitude'];
  42. }
  43. $event['location'] = $card['url'];
  44. $event['refs'] = array($card);
  45. // If we only have a name, use that
  46. } elseif(isset($fbObject['place']['name'])) {
  47. $event['location'] = $fbObject['place']['name'];
  48. }
  49. $event['post-type'] = \p3k\XRay\PostType::discover($event);
  50. return [
  51. 'data' => $event,
  52. 'original' => $fbObject,
  53. 'source-format' => 'facebook',
  54. ];
  55. }
  56. }
  57. public static function fetch($url, $creds) {
  58. $parts = self::extract_url_parts($url);
  59. if(!$parts or $parts['api_uri'] == false) {
  60. return [
  61. 'error' => 'unsupported_url',
  62. 'error_description' => 'This Facebook URL is not supported',
  63. 'error_code' => 400,
  64. ];
  65. }
  66. $fb = new \Facebook\Facebook(array(
  67. 'app_id' => $creds['facebook_app_id'],
  68. 'app_secret' => $creds['facebook_app_secret'],
  69. 'default_graph_version' => 'v2.9',
  70. ));
  71. $fbApp = new \Facebook\FacebookApp($creds['facebook_app_id'], $creds['facebook_app_secret']);
  72. $token = $fbApp->getAccessToken();
  73. $request = new \Facebook\FacebookRequest($fbApp, $token, 'GET', $parts['api_uri']);
  74. try {
  75. $response = $fb->getClient()->sendRequest($request);
  76. } catch(\Facebook\Exceptions\FacebookResponseException $e) {
  77. return [
  78. 'error' => 'facebook_graph_error',
  79. 'error_description' => 'Graph returned an error: ' . $e->getMessage(),
  80. 'error_code' => 400,
  81. ];
  82. } catch(\Facebook\Exceptions\FacebookSDKException $e) {
  83. return [
  84. 'error' => 'facebook_sdk_error',
  85. 'error_description' => 'Facebook SDK returned an error: ' . $e->getMessage(),
  86. 'error_code' => 400,
  87. ];
  88. }
  89. return [
  90. 'code' => 200,
  91. 'body' => $response->getDecodedBody(),
  92. 'url' => $url
  93. ];
  94. }
  95. private static function extract_url_parts($url) {
  96. $response = false;
  97. if(preg_match('~https://(.*?).?facebook.com/([^/]+)/posts/(\d+)/?$~', $url, $match)) {
  98. // TODO: how do we get these?
  99. // $response['type'] = 'entry';
  100. // $response['api_uri'] = false;
  101. } elseif(preg_match('~https://(.*?).?facebook.com/events/(\d+)/?$~', $url, $match)) {
  102. $response['type'] = 'event';
  103. $response['api_uri'] = '/'.$match[2];
  104. }
  105. return $response;
  106. }
  107. }