PageRenderTime 27ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/tenants/apps/samlservice/deploy-samlservice/samlservice/modules/authfacebook/extlibinc/facebook.php

https://github.com/hpgihan/cronus
PHP | 348 lines | 249 code | 39 blank | 60 comment | 47 complexity | 643d5832a1113c8ef4d760afdd4ab6ed MD5 | raw file
  1. <?php
  2. // Copyright 2004-2008 Facebook. All Rights Reserved.
  3. //
  4. // +---------------------------------------------------------------------------+
  5. // | Facebook Platform PHP5 client |
  6. // +---------------------------------------------------------------------------+
  7. // | Copyright (c) 2007 Facebook, Inc. |
  8. // | All rights reserved. |
  9. // | |
  10. // | Redistribution and use in source and binary forms, with or without |
  11. // | modification, are permitted provided that the following conditions |
  12. // | are met: |
  13. // | |
  14. // | 1. Redistributions of source code must retain the above copyright |
  15. // | notice, this list of conditions and the following disclaimer. |
  16. // | 2. Redistributions in binary form must reproduce the above copyright |
  17. // | notice, this list of conditions and the following disclaimer in the |
  18. // | documentation and/or other materials provided with the distribution. |
  19. // | |
  20. // | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
  21. // | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
  22. // | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
  23. // | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
  24. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
  25. // | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  26. // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  27. // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
  28. // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
  29. // | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
  30. // +---------------------------------------------------------------------------+
  31. // | For help with this library, contact developers-help@facebook.com |
  32. // +---------------------------------------------------------------------------+
  33. //
  34. include_once 'facebookapi_php5_restlib.php';
  35. define('FACEBOOK_API_VALIDATION_ERROR', 1);
  36. class Facebook {
  37. public $api_client;
  38. public $api_key;
  39. public $secret;
  40. public $generate_session_secret;
  41. public $session_expires;
  42. public $fb_params;
  43. public $user;
  44. public function __construct($api_key, $secret, $generate_session_secret=false) {
  45. $this->api_key = $api_key;
  46. $this->secret = $secret;
  47. $this->generate_session_secret = $generate_session_secret;
  48. $this->api_client = new FacebookRestClient($api_key, $secret);
  49. $this->validate_fb_params();
  50. if (isset($this->fb_params['friends'])) {
  51. $this->api_client->friends_list = explode(',', $this->fb_params['friends']);
  52. }
  53. if (isset($this->fb_params['added'])) {
  54. $this->api_client->added = $this->fb_params['added'];
  55. }
  56. }
  57. public function validate_fb_params($resolve_auth_token=true) {
  58. $this->fb_params = $this->get_valid_fb_params($_POST, 48*3600, 'fb_sig');
  59. if (!$this->fb_params) {
  60. $this->fb_params = $this->get_valid_fb_params($_GET, 48*3600, 'fb_sig');
  61. }
  62. if ($this->fb_params) {
  63. // If we got any fb_params passed in at all, then either:
  64. // - they included an fb_user / fb_session_key, which we should assume to be correct
  65. // - they didn't include an fb_user / fb_session_key, which means the user doesn't have a
  66. // valid session and if we want to get one we'll need to use require_login(). (Calling
  67. // set_user with null values for user/session_key will work properly.)
  68. // Note that we should *not* use our cookies in this scenario, since they may be referring to
  69. // the wrong user.
  70. $user = isset($this->fb_params['user']) ? $this->fb_params['user'] : null;
  71. $session_key = isset($this->fb_params['session_key']) ? $this->fb_params['session_key'] : null;
  72. $expires = isset($this->fb_params['expires']) ? $this->fb_params['expires'] : null;
  73. $this->set_user($user, $session_key, $expires);
  74. } else if (!empty($_COOKIE) && $cookies = $this->get_valid_fb_params($_COOKIE, null, $this->api_key)) {
  75. // use $api_key . '_' as a prefix for the cookies in case there are
  76. // multiple facebook clients on the same domain.
  77. $expires = isset($cookies['expires']) ? $cookies['expires'] : null;
  78. $this->set_user($cookies['user'], $cookies['session_key'], $expires);
  79. } else if (isset($_GET['auth_token']) && $resolve_auth_token &&
  80. $session = $this->do_get_session($_GET['auth_token'])) {
  81. $session_secret = ($this->generate_session_secret && !empty($session['secret'])) ? $session['secret'] : null;
  82. $this->set_user($session['uid'], $session['session_key'], $session['expires'], $session_secret);
  83. }
  84. return !empty($this->fb_params);
  85. }
  86. // Store a temporary session secret for the current session
  87. // for use with the JS client library
  88. public function promote_session() {
  89. try {
  90. $session_secret = $this->api_client->auth_promoteSession();
  91. if (!$this->in_fb_canvas()) {
  92. $this->set_cookies($this->user, $this->api_client->session_key, $this->session_expires, $session_secret);
  93. }
  94. return $session_secret;
  95. } catch (FacebookRestClientException $e) {
  96. // API_EC_PARAM means we don't have a logged in user, otherwise who
  97. // knows what it means, so just throw it.
  98. if ($e->getCode() != FacebookAPIErrorCodes::API_EC_PARAM) {
  99. throw $e;
  100. }
  101. }
  102. }
  103. public function do_get_session($auth_token) {
  104. try {
  105. return $this->api_client->auth_getSession($auth_token, $this->generate_session_secret);
  106. } catch (FacebookRestClientException $e) {
  107. // API_EC_PARAM means we don't have a logged in user, otherwise who
  108. // knows what it means, so just throw it.
  109. if ($e->getCode() != FacebookAPIErrorCodes::API_EC_PARAM) {
  110. throw $e;
  111. }
  112. }
  113. }
  114. // Invalidate the session currently being used, and clear any state associated with it
  115. public function expire_session() {
  116. if ($this->api_client->auth_expireSession()) {
  117. if (!$this->in_fb_canvas() && isset($_COOKIE[$this->api_key . '_user'])) {
  118. $cookies = array('user', 'session_key', 'expires', 'ss');
  119. foreach ($cookies as $name) {
  120. setcookie($this->api_key . '_' . $name, false, time() - 3600);
  121. unset($_COOKIE[$this->api_key . '_' . $name]);
  122. }
  123. setcookie($this->api_key, false, time() - 3600);
  124. unset($_COOKIE[$this->api_key]);
  125. }
  126. // now, clear the rest of the stored state
  127. $this->user = 0;
  128. $this->api_client->session_key = 0;
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. }
  134. public function redirect($url) {
  135. if ($this->in_fb_canvas()) {
  136. echo '<fb:redirect url="' . $url . '"/>';
  137. } else if (preg_match('/^https?:\/\/([^\/]*\.)?facebook\.com(:\d+)?/i', $url)) {
  138. // make sure facebook.com url's load in the full frame so that we don't
  139. // get a frame within a frame.
  140. echo "<script type=\"text/javascript\">\ntop.location.href = \"$url\";\n</script>";
  141. } else {
  142. header('Location: ' . $url);
  143. }
  144. exit;
  145. }
  146. public function in_frame() {
  147. return isset($this->fb_params['in_canvas']) || isset($this->fb_params['in_iframe']);
  148. }
  149. public function in_fb_canvas() {
  150. return isset($this->fb_params['in_canvas']);
  151. }
  152. public function get_loggedin_user() {
  153. return $this->user;
  154. }
  155. public static function current_url() {
  156. return 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '?poot=bar';
  157. }
  158. public function require_login($next = NULL) {
  159. if ($next === NULL) $next = self::current_url();
  160. if ($user = $this->get_loggedin_user()) {
  161. return $user;
  162. }
  163. $this->redirect($this->get_login_url($next, $this->in_frame()));
  164. }
  165. public function require_install() {
  166. // this was renamed, keeping for compatibility's sake
  167. return $this->require_add();
  168. }
  169. public function require_add() {
  170. if ($user = $this->get_loggedin_user()) {
  171. if ($this->fb_params['added']) {
  172. return $user;
  173. }
  174. }
  175. $this->redirect($this->get_add_url(self::current_url()));
  176. }
  177. public function require_frame() {
  178. if (!$this->in_frame()) {
  179. $this->redirect($this->get_login_url(self::current_url(), true));
  180. }
  181. }
  182. public static function get_facebook_url($subdomain='www') {
  183. return 'http://' . $subdomain . '.facebook.com';
  184. }
  185. public function get_install_url($next=null) {
  186. // this was renamed, keeping for compatibility's sake
  187. return $this->get_add_url($next);
  188. }
  189. public function get_add_url($next=null) {
  190. return self::get_facebook_url().'/add.php?api_key='.$this->api_key .
  191. ($next ? '&next=' . urlencode($next) : '');
  192. }
  193. public function get_login_url($next, $canvas) {
  194. return self::get_facebook_url().'/login.php?v=1.0&api_key=' . $this->api_key .
  195. ($next ? '&next=' . urlencode($next) : '') .
  196. '&req_perms=email' .
  197. ($canvas ? '&canvas' : '');
  198. }
  199. public static function generate_sig($params_array, $secret) {
  200. $str = '';
  201. ksort($params_array);
  202. // Note: make sure that the signature parameter is not already included in
  203. // $params_array.
  204. foreach ($params_array as $k=>$v) {
  205. $str .= "$k=$v";
  206. }
  207. $str .= $secret;
  208. return md5($str);
  209. }
  210. public function set_user($user, $session_key, $expires=null, $session_secret=null) {
  211. if (!$this->in_fb_canvas() && (!isset($_COOKIE[$this->api_key . '_user'])
  212. || $_COOKIE[$this->api_key . '_user'] != $user)) {
  213. $this->set_cookies($user, $session_key, $expires, $session_secret);
  214. }
  215. $this->user = $user;
  216. $this->api_client->session_key = $session_key;
  217. $this->session_expires = $expires;
  218. }
  219. public function set_cookies($user, $session_key, $expires=null, $session_secret=null) {
  220. $cookies = array();
  221. $cookies['user'] = $user;
  222. $cookies['session_key'] = $session_key;
  223. if ($expires != null) {
  224. $cookies['expires'] = $expires;
  225. }
  226. if ($session_secret != null) {
  227. $cookies['ss'] = $session_secret;
  228. }
  229. foreach ($cookies as $name => $val) {
  230. setcookie($this->api_key . '_' . $name, $val, (int)$expires);
  231. $_COOKIE[$this->api_key . '_' . $name] = $val;
  232. }
  233. $sig = self::generate_sig($cookies, $this->secret);
  234. setcookie($this->api_key, $sig, (int)$expires);
  235. $_COOKIE[$this->api_key] = $sig;
  236. }
  237. /**
  238. * Tries to undo the badness of magic quotes as best we can
  239. * @param string $val Should come directly from $_GET, $_POST, etc.
  240. * @return string val without added slashes
  241. */
  242. public static function no_magic_quotes($val) {
  243. if (get_magic_quotes_gpc()) {
  244. return stripslashes($val);
  245. } else {
  246. return $val;
  247. }
  248. }
  249. public function get_valid_fb_params($params, $timeout=null, $namespace='fb_sig') {
  250. $prefix = $namespace . '_';
  251. $prefix_len = strlen($prefix);
  252. $fb_params = array();
  253. foreach ($params as $name => $val) {
  254. if (strpos($name, $prefix) === 0) {
  255. $fb_params[substr($name, $prefix_len)] = self::no_magic_quotes($val);
  256. }
  257. }
  258. if ($timeout && (!isset($fb_params['time']) || time() - $fb_params['time'] > $timeout)) {
  259. return array();
  260. }
  261. if (!isset($params[$namespace]) || (!$this->verify_signature($fb_params, $params[$namespace]))) {
  262. return array();
  263. }
  264. return $fb_params;
  265. }
  266. public function verify_signature($fb_params, $expected_sig) {
  267. return self::generate_sig($fb_params, $this->secret) == $expected_sig;
  268. }
  269. public function encode_validationError($summary, $message) {
  270. return json_encode(
  271. array('errorCode' => FACEBOOK_API_VALIDATION_ERROR,
  272. 'errorTitle' => $summary,
  273. 'errorMessage' => $message));
  274. }
  275. public function encode_multiFeedStory($feed, $next) {
  276. return json_encode(
  277. array('method' => 'multiFeedStory',
  278. 'content' =>
  279. array('next' => $next,
  280. 'feed' => $feed)));
  281. }
  282. public function encode_feedStory($feed, $next) {
  283. return json_encode(
  284. array('method' => 'feedStory',
  285. 'content' =>
  286. array('next' => $next,
  287. 'feed' => $feed)));
  288. }
  289. public function create_templatizedFeedStory($title_template, $title_data=array(),
  290. $body_template='', $body_data = array(), $body_general=null,
  291. $image_1=null, $image_1_link=null,
  292. $image_2=null, $image_2_link=null,
  293. $image_3=null, $image_3_link=null,
  294. $image_4=null, $image_4_link=null) {
  295. return array('title_template'=> $title_template,
  296. 'title_data' => $title_data,
  297. 'body_template'=> $body_template,
  298. 'body_data' => $body_data,
  299. 'body_general' => $body_general,
  300. 'image_1' => $image_1,
  301. 'image_1_link' => $image_1_link,
  302. 'image_2' => $image_2,
  303. 'image_2_link' => $image_2_link,
  304. 'image_3' => $image_3,
  305. 'image_3_link' => $image_3_link,
  306. 'image_4' => $image_4,
  307. 'image_4_link' => $image_4_link);
  308. }
  309. }