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

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