/db/library/facebook_stream/Services/Facebook.php

https://github.com/Br3nda/openmicroblogger · PHP · 230 lines · 88 code · 24 blank · 118 comment · 11 complexity · 07b6c722fb6b36a117f06e7a6c00bed2 MD5 · raw file

  1. <?php
  2. /**
  3. * PHP5 interface for Facebook's REST API
  4. *
  5. * PHP version 5.1.0+
  6. *
  7. * LICENSE: This source file is subject to the New BSD license that is
  8. * available through the world-wide-web at the following URI:
  9. * http://www.opensource.org/licenses/bsd-license.php. If you did not receive
  10. * a copy of the New BSD License and are unable to obtain it through the web,
  11. * please send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category Services
  14. * @package Services_Facebook
  15. * @author Joe Stump <joe@joestump.net>
  16. * @copyright 2007-2008 Joe Stump <joe@joestump.net>
  17. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  18. * @version Release: @package_version@
  19. * @link http://pear.php.net/package/Services_Facebook
  20. */
  21. require_once 'Services/Facebook/Common.php';
  22. require_once 'Services/Facebook/Exception.php';
  23. /**
  24. * Services_Facebook
  25. *
  26. * @category Services
  27. * @package Services_Facebook
  28. * @author Joe Stump <joe@joestump.net>
  29. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  30. * @version Release: @package_version@
  31. * @link http://wiki.developers.facebook.com
  32. */
  33. class Services_Facebook
  34. {
  35. /**
  36. * Facebook API URL
  37. *
  38. * @var string $apiURL URL that that API calls will be sent to
  39. * @static
  40. */
  41. static public $apiURL = 'http://api.new.facebook.com/restserver.php';
  42. /**
  43. * Facebok application API key
  44. *
  45. * @var string $apiKey 32 character api_key from Facebook
  46. * @static
  47. */
  48. static public $apiKey = '';
  49. /**
  50. * Facebok application secret
  51. *
  52. * The Facebook secret token is used to both sign requests sent to
  53. * Facebook and to verify requests sent from Facebook to your application.
  54. *
  55. * @var string $secret 32 character secret from Facebook
  56. * @static
  57. */
  58. static public $secret = '';
  59. /**
  60. * Timeout
  61. *
  62. * The amount in seconds for the curl http request timeout
  63. *
  64. * @var string $timeout Time in seconds for a request to timeout
  65. * @static
  66. */
  67. static public $timeout = 30;
  68. /**
  69. * Currently logged in user
  70. *
  71. * @var string $sessionKey
  72. */
  73. public $sessionKey = '';
  74. /**
  75. * Instances of various drivers
  76. *
  77. * @var array $instances
  78. */
  79. static protected $instances = array();
  80. /**
  81. * Instance of Services_Facebook
  82. *
  83. * @var Services_Facebook $instance
  84. * @see Services_Facebook::singleton();
  85. */
  86. static protected $instance;
  87. /**
  88. * Available drivers
  89. *
  90. * @var array $drivers
  91. */
  92. static protected $drivers = array(
  93. 'admin' => 'Admin',
  94. 'application' => 'Application',
  95. 'auth' => 'Auth',
  96. 'connect' => 'Connect',
  97. 'data' => 'Data',
  98. 'events' => 'Events',
  99. 'fbml' => 'FBML',
  100. 'fql' => 'FQL',
  101. 'feed' => 'Feed',
  102. 'friends' => 'Friends',
  103. 'groups' => 'Friends',
  104. 'marketplace' => 'MarketPlace',
  105. 'notifications' => 'Notifications',
  106. 'pages' => 'Pages',
  107. 'photos' => 'Photos',
  108. 'profile' => 'Profile',
  109. 'share' => 'Share',
  110. 'users' => 'Users'
  111. );
  112. /**
  113. * Create a facebook service
  114. *
  115. * @param string $endPoint Services to create
  116. *
  117. * @return object Instance of Facebook endpoint
  118. * @throws Services_Facebook_Exception
  119. */
  120. static protected function factory($endPoint)
  121. {
  122. $file = 'Services/Facebook/' . $endPoint . '.php';
  123. include_once $file;
  124. $class = 'Services_Facebook_' . $endPoint;
  125. if (!class_exists($class)) {
  126. throw new Services_Facebook_Exception('Class not found ' . $class);
  127. }
  128. $instance = new $class();
  129. return $instance;
  130. }
  131. /**
  132. * Singleton
  133. *
  134. * @return Services_Facebook
  135. */
  136. static public function singleton()
  137. {
  138. if (self::$instance !== null) {
  139. return self::$instance;
  140. }
  141. $instance = new Services_Facebook();
  142. self::$instance = $instance;
  143. return $instance;
  144. }
  145. /**
  146. * Lazy loader for Facebook drivers
  147. *
  148. * @param string $driver The Facebook driver/endpoint to load
  149. *
  150. * @throws Services_Facebook_Exception
  151. * @return object
  152. */
  153. public function __get($driver)
  154. {
  155. $driver = strtolower($driver);
  156. if (!isset(self::$drivers[$driver])) {
  157. throw new Services_Facebook_Exception(
  158. 'The driver requested, ' . $driver . ', is not supported'
  159. );
  160. } else {
  161. $driver = self::$drivers[$driver];
  162. }
  163. if (isset(self::$instances[$driver])) {
  164. return self::$instances[$driver];
  165. }
  166. self::$instances[$driver] = self::factory($driver);
  167. self::$instances[$driver]->sessionKey = $this->sessionKey;
  168. return self::$instances[$driver];
  169. }
  170. /**
  171. * Validates requests from Facebook
  172. *
  173. * Facebook sends a series of $_POST variables when it requests a canvas
  174. * page or sends a user to the post removal URL. This function validates
  175. * that request came from Facebook. This function returns true if the
  176. * request came from Facebook.
  177. *
  178. * Both the signature of the request and the api_key is verified. If the
  179. * api_key given doesn't match up to the current Services_Facebook::$apiKey
  180. * then it will return false.
  181. *
  182. * @param array $args Normally the $_POST array
  183. *
  184. * @return boolean True if the request signature is valid
  185. */
  186. static public function isValidRequest($args)
  187. {
  188. if ($args['fb_sig_api_key'] != Services_Facebook::$apiKey) {
  189. return false;
  190. }
  191. ksort($args);
  192. $sig = '';
  193. foreach ($args as $k => $v) {
  194. if ($k == 'fb_sig') {
  195. continue;
  196. }
  197. // The signature is based on fb_sig_* fields only. Extra POST
  198. // args are passed along, but don't alter the signature.
  199. if (preg_match('/^fb_sig_/', $k)) {
  200. $sig .= substr($k, 7) . '=' . $v;
  201. }
  202. }
  203. return (md5($sig . Services_Facebook::$secret) == $args['fb_sig']);
  204. }
  205. }
  206. ?>