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.

105 lines
2.8 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. 'summary' => $fbObject['description'],
  24. 'location' => [
  25. $fbObject['place']['name']
  26. ]
  27. );
  28. return [
  29. 'data' => $event,
  30. 'original' => $fbObject
  31. ];
  32. }
  33. }
  34. public static function fetch($url, $creds) {
  35. $parts = self::extract_url_parts($url);
  36. if(!$parts or $parts['api_uri'] == false) {
  37. return [
  38. 'error' => 'unsupported_url',
  39. 'error_description' => 'This Facebook URL is not supported',
  40. 'error_code' => 400,
  41. ];
  42. }
  43. $fb = new \Facebook\Facebook([
  44. 'app_id' => $creds['facebook_app_id'],
  45. 'app_secret' => $creds['facebook_app_secret'],
  46. 'default_graph_version' => 'v2.9',
  47. ]);
  48. $fbApp = new \Facebook\FacebookApp($creds['facebook_app_id'], $creds['facebook_app_secret']);
  49. $token = $fbApp->getAccessToken();
  50. $request = new \Facebook\FacebookRequest($fbApp, $token, 'GET', $parts['api_uri']);
  51. try {
  52. $response = $fb->getClient()->sendRequest($request);
  53. } catch(\Facebook\Exceptions\FacebookResponseException $e) {
  54. return [
  55. 'error' => 'facebook_graph_error',
  56. 'error_description' => 'Graph returned an error: ' . $e->getMessage(),
  57. 'error_code' => 400,
  58. ];
  59. } catch(\Facebook\Exceptions\FacebookSDKException $e) {
  60. return [
  61. 'error' => 'facebook_sdk_error',
  62. 'error_description' => 'Facebook SDK returned an error: ' . $e->getMessage(),
  63. 'error_code' => 400,
  64. ];
  65. }
  66. return [
  67. 'code' => 200,
  68. 'body' => $response->getDecodedBody(),
  69. 'url' => $url
  70. ];
  71. }
  72. private static function extract_url_parts($url) {
  73. $response = false;
  74. if(preg_match('~https://(.*?).?facebook.com/([^/]+)/posts/(\d+)/?$~', $url, $match)) {
  75. // TODO: how do we get these?
  76. // $response['type'] = 'entry';
  77. // $response['api_uri'] = false;
  78. } elseif(preg_match('~https://(.*?).?facebook.com/events/(\d+)/?$~', $url, $match)) {
  79. $response['type'] = 'event';
  80. $response['api_uri'] = '/'.$match[2];
  81. }
  82. return $response;
  83. }
  84. }