PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/demo-apps/public/sportsnews/ringside/api/clients/facebook.php

https://github.com/jkinner/ringside
PHP | 246 lines | 164 code | 29 blank | 53 comment | 35 complexity | 707123c5d182fe9c211ba5ab88fd7999 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. //
  3. // +---------------------------------------------------------------------------+
  4. // | Facebook Platform PHP5 client |
  5. // +---------------------------------------------------------------------------+
  6. // | Copyright (c) 2007 Facebook, Inc. |
  7. // | All rights reserved. |
  8. // | |
  9. // | Redistribution and use in source and binary forms, with or without |
  10. // | modification, are permitted provided that the following conditions |
  11. // | are met: |
  12. // | |
  13. // | 1. Redistributions of source code must retain the above copyright |
  14. // | notice, this list of conditions and the following disclaimer. |
  15. // | 2. Redistributions in binary form must reproduce the above copyright |
  16. // | notice, this list of conditions and the following disclaimer in the |
  17. // | documentation and/or other materials provided with the distribution. |
  18. // | |
  19. // | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
  20. // | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
  21. // | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
  22. // | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
  23. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
  24. // | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  25. // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  26. // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
  27. // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
  28. // | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
  29. // +---------------------------------------------------------------------------+
  30. // | For help with this library, contact developers-help@facebook.com |
  31. // +---------------------------------------------------------------------------+
  32. //
  33. include_once 'ringside/api/clients/facebookapi_php5_restlib.php';
  34. class Facebook {
  35. public $api_client;
  36. public $api_key;
  37. public $secret;
  38. public $fb_params;
  39. public $user;
  40. public function __construct($api_key, $secret, $ringside = null) {
  41. $this->api_key = $api_key;
  42. $this->secret = $secret;
  43. $this->api_client = new FacebookRestClient($api_key, $secret);
  44. $this->validate_fb_params();
  45. if (isset($this->fb_params['friends'])) {
  46. $this->api_client->friends_list = explode(',', $this->fb_params['friends']);
  47. }
  48. if (isset($this->fb_params['added'])) {
  49. $this->api_client->added = $this->fb_params['added'];
  50. }
  51. }
  52. public function validate_fb_params() {
  53. $this->fb_params = $this->get_valid_fb_params($_POST, 48*3600, 'fb_sig');
  54. if (!$this->fb_params) {
  55. $this->fb_params = $this->get_valid_fb_params($_GET, 48*3600, 'fb_sig');
  56. }
  57. if ($this->fb_params) {
  58. // If we got any fb_params passed in at all, then either:
  59. // - they included an fb_user / fb_session_key, which we should assume to be correct
  60. // - they didn't include an fb_user / fb_session_key, which means the user doesn't have a
  61. // valid session and if we want to get one we'll need to use require_login(). (Calling
  62. // set_user with null values for user/session_key will work properly.)
  63. // Note that we should *not* use our cookies in this scenario, since they may be referring to
  64. // the wrong user.
  65. $user = isset($this->fb_params['user']) ? $this->fb_params['user'] : null;
  66. $session_key = isset($this->fb_params['session_key']) ? $this->fb_params['session_key'] : null;
  67. $expires = isset($this->fb_params['expires']) ? $this->fb_params['expires'] : null;
  68. $this->set_user($user, $session_key, $expires);
  69. } else if (!empty($_COOKIE) && $cookies = $this->get_valid_fb_params($_COOKIE, null, $this->api_key)) {
  70. // use $api_key . '_' as a prefix for the cookies in case there are
  71. // multiple facebook clients on the same domain.
  72. $this->set_user($cookies['user'], $cookies['session_key']);
  73. } else if (isset($_GET['auth_token']) && $session = $this->do_get_session($_GET['auth_token'])) {
  74. $this->set_user($session['uid'], $session['session_key'], $session['expires']);
  75. }
  76. return !empty($this->fb_params);
  77. }
  78. public function do_get_session($auth_token) {
  79. try {
  80. return $this->api_client->auth_getSession($auth_token);
  81. } catch (FacebookRestClientException $e) {
  82. // API_EC_PARAM means we don't have a logged in user, otherwise who
  83. // knows what it means, so just throw it.
  84. if ($e->getCode() != FacebookAPIErrorCodes::API_EC_PARAM) {
  85. throw $e;
  86. }
  87. }
  88. }
  89. public function redirect($url) {
  90. if ($this->in_fb_canvas()) {
  91. echo '<fb:redirect url="' . $url . '"/>';
  92. } else if (preg_match('/^https?:\/\/([^\/]*\.)?facebook\.com(:\d+)?/i', $url)) {
  93. // make sure facebook.com url's load in the full frame so that we don't
  94. // get a frame within a frame.
  95. echo "<script type=\"text/javascript\">\ntop.location.href = \"$url\";\n</script>";
  96. } else {
  97. header('Location: ' . $url);
  98. }
  99. exit;
  100. }
  101. public function in_frame() {
  102. return isset($this->fb_params['in_canvas']) || isset($this->fb_params['in_iframe']);
  103. }
  104. public function in_fb_canvas() {
  105. return isset($this->fb_params['in_canvas']);
  106. }
  107. public function get_loggedin_user() {
  108. return $this->user;
  109. }
  110. public static function current_url() {
  111. return 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  112. }
  113. public function require_login() {
  114. if ($user = $this->get_loggedin_user()) {
  115. return $user;
  116. }
  117. $this->redirect($this->get_login_url(self::current_url(), $this->in_frame()));
  118. }
  119. public function require_install() {
  120. // this was renamed, keeping for compatibility's sake
  121. return $this->require_add();
  122. }
  123. public function require_add() {
  124. if ($user = $this->get_loggedin_user()) {
  125. if ($this->fb_params['added']) {
  126. return $user;
  127. }
  128. }
  129. $this->redirect($this->get_add_url(self::current_url()));
  130. }
  131. public function require_frame() {
  132. if (!$this->in_frame()) {
  133. $this->redirect($this->get_login_url(self::current_url(), true));
  134. }
  135. }
  136. public static function get_facebook_url($subdomain='www') {
  137. if (class_exists('RingsideApiClients')) {
  138. $url = RingsideApiClients::get_facebook_url($subdomain);
  139. return $url;
  140. }
  141. return 'http://' . $subdomain . '.facebook.com';
  142. }
  143. public function get_install_url($next=null) {
  144. // this was renamed, keeping for compatibility's sake
  145. return $this->get_add_url($next);
  146. }
  147. public function get_add_url($next=null) {
  148. return self::get_facebook_url().'/add.php?api_key='.$this->api_key .
  149. ($next ? '&next=' . urlencode($next) : '');
  150. }
  151. public function get_login_url($next, $canvas) {
  152. return self::get_facebook_url().'/login.php?v=1.0&api_key=' . $this->api_key .
  153. ($next ? '&next=' . urlencode($next) : '') .
  154. ($canvas ? '&canvas' : '');
  155. }
  156. public static function generate_sig($params_array, $secret) {
  157. $str = '';
  158. ksort($params_array);
  159. // Note: make sure that the signature parameter is not already included in
  160. // $params_array.
  161. foreach ($params_array as $k=>$v) {
  162. $str .= "$k=$v";
  163. }
  164. $str .= $secret;
  165. return md5($str);
  166. }
  167. public function set_user($user, $session_key, $expires=null) {
  168. if (!$this->in_fb_canvas() && (!isset($_COOKIE[$this->api_key . '_user'])
  169. || $_COOKIE[$this->api_key . '_user'] != $user)) {
  170. $cookies = array();
  171. $cookies['user'] = $user;
  172. $cookies['session_key'] = $session_key;
  173. $sig = self::generate_sig($cookies, $this->secret);
  174. foreach ($cookies as $name => $val) {
  175. setcookie($this->api_key . '_' . $name, $val, (int)$expires);
  176. $_COOKIE[$this->api_key . '_' . $name] = $val;
  177. }
  178. setcookie($this->api_key, $sig, (int)$expires);
  179. $_COOKIE[$this->api_key] = $sig;
  180. }
  181. $this->user = $user;
  182. $this->api_client->session_key = $session_key;
  183. }
  184. /**
  185. * Tries to undo the badness of magic quotes as best we can
  186. * @param string $val Should come directly from $_GET, $_POST, etc.
  187. * @return string val without added slashes
  188. */
  189. public static function no_magic_quotes($val) {
  190. if (get_magic_quotes_gpc()) {
  191. return stripslashes($val);
  192. } else {
  193. return $val;
  194. }
  195. }
  196. public function get_valid_fb_params($params, $timeout=null, $namespace='fb_sig') {
  197. $prefix = $namespace . '_';
  198. $prefix_len = strlen($prefix);
  199. $fb_params = array();
  200. foreach ($params as $name => $val) {
  201. if (strpos($name, $prefix) === 0) {
  202. $fb_params[substr($name, $prefix_len)] = self::no_magic_quotes($val);
  203. }
  204. }
  205. if ($timeout && (!isset($fb_params['time']) || time() - $fb_params['time'] > $timeout)) {
  206. return array();
  207. }
  208. if (!isset($params[$namespace]) || !$this->verify_signature($fb_params, $params[$namespace])) {
  209. return array();
  210. }
  211. return $fb_params;
  212. }
  213. public function verify_signature($fb_params, $expected_sig) {
  214. return self::generate_sig($fb_params, $this->secret) == $expected_sig;
  215. }
  216. }
  217. ?>