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.

135 lines
4.1 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. return [
  50. 'data' => $event,
  51. 'original' => $fbObject
  52. ];
  53. }
  54. }
  55. public static function fetch($url, $creds) {
  56. $parts = self::extract_url_parts($url);
  57. if(!$parts or $parts['api_uri'] == false) {
  58. return [
  59. 'error' => 'unsupported_url',
  60. 'error_description' => 'This Facebook URL is not supported',
  61. 'error_code' => 400,
  62. ];
  63. }
  64. $fb = new \Facebook\Facebook(array(
  65. 'app_id' => $creds['facebook_app_id'],
  66. 'app_secret' => $creds['facebook_app_secret'],
  67. 'default_graph_version' => 'v2.9',
  68. ));
  69. $fbApp = new \Facebook\FacebookApp($creds['facebook_app_id'], $creds['facebook_app_secret']);
  70. $token = $fbApp->getAccessToken();
  71. $request = new \Facebook\FacebookRequest($fbApp, $token, 'GET', $parts['api_uri']);
  72. try {
  73. $response = $fb->getClient()->sendRequest($request);
  74. } catch(\Facebook\Exceptions\FacebookResponseException $e) {
  75. return [
  76. 'error' => 'facebook_graph_error',
  77. 'error_description' => 'Graph returned an error: ' . $e->getMessage(),
  78. 'error_code' => 400,
  79. ];
  80. } catch(\Facebook\Exceptions\FacebookSDKException $e) {
  81. return [
  82. 'error' => 'facebook_sdk_error',
  83. 'error_description' => 'Facebook SDK returned an error: ' . $e->getMessage(),
  84. 'error_code' => 400,
  85. ];
  86. }
  87. return [
  88. 'code' => 200,
  89. 'body' => $response->getDecodedBody(),
  90. 'url' => $url
  91. ];
  92. }
  93. private static function extract_url_parts($url) {
  94. $response = false;
  95. if(preg_match('~https://(.*?).?facebook.com/([^/]+)/posts/(\d+)/?$~', $url, $match)) {
  96. // TODO: how do we get these?
  97. // $response['type'] = 'entry';
  98. // $response['api_uri'] = false;
  99. } elseif(preg_match('~https://(.*?).?facebook.com/events/(\d+)/?$~', $url, $match)) {
  100. $response['type'] = 'event';
  101. $response['api_uri'] = '/'.$match[2];
  102. }
  103. return $response;
  104. }
  105. }