PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/include/social/facebook/facebook_sdk/src/facebook.php

https://bitbucket.org/allexblacker/suitecrm
PHP | 227 lines | 106 code | 20 blank | 101 comment | 15 complexity | dc3f0f826fbbbd1b337470b3d7919e52 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Copyright 2011 Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License. You may obtain
  7. * a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations
  15. * under the License.
  16. */
  17. require_once "base_facebook.php";
  18. /**
  19. * Extends the BaseFacebook class with the intent of using
  20. * PHP sessions to store user ids and access tokens.
  21. */
  22. class Facebook extends BaseFacebook
  23. {
  24. /**
  25. * Cookie prefix
  26. */
  27. const FBSS_COOKIE_NAME = 'fbss';
  28. /**
  29. * We can set this to a high number because the main session
  30. * expiration will trump this.
  31. */
  32. const FBSS_COOKIE_EXPIRE = 31556926; // 1 year
  33. /**
  34. * Stores the shared session ID if one is set.
  35. *
  36. * @var string
  37. */
  38. protected $sharedSessionID;
  39. /**
  40. * Identical to the parent constructor, except that
  41. * we start a PHP session to store the user ID and
  42. * access token if during the course of execution
  43. * we discover them.
  44. *
  45. * @param array $config the application configuration. Additionally
  46. * accepts "sharedSession" as a boolean to turn on a secondary
  47. * cookie for environments with a shared session (that is, your app
  48. * shares the domain with other apps).
  49. *
  50. * @see BaseFacebook::__construct
  51. */
  52. public function __construct($config) {
  53. if (!session_id()) {
  54. session_start();
  55. }
  56. parent::__construct($config);
  57. if (!empty($config['sharedSession'])) {
  58. $this->initSharedSession();
  59. // re-load the persisted state, since parent
  60. // attempted to read out of non-shared cookie
  61. $state = $this->getPersistentData('state');
  62. if (!empty($state)) {
  63. $this->state = $state;
  64. } else {
  65. $this->state = null;
  66. }
  67. }
  68. }
  69. /**
  70. * Supported keys for persistent data
  71. *
  72. * @var array
  73. */
  74. protected static $kSupportedKeys =
  75. array('state', 'code', 'access_token', 'user_id');
  76. /**
  77. * Initiates Shared Session
  78. */
  79. protected function initSharedSession() {
  80. $cookie_name = $this->getSharedSessionCookieName();
  81. if (isset($_COOKIE[$cookie_name])) {
  82. $data = $this->parseSignedRequest($_COOKIE[$cookie_name]);
  83. if ($data && !empty($data['domain']) &&
  84. self::isAllowedDomain($this->getHttpHost(), $data['domain'])) {
  85. // good case
  86. $this->sharedSessionID = $data['id'];
  87. return;
  88. }
  89. // ignoring potentially unreachable data
  90. }
  91. // evil/corrupt/missing case
  92. $base_domain = $this->getBaseDomain();
  93. $this->sharedSessionID = md5(uniqid(mt_rand(), true));
  94. $cookie_value = $this->makeSignedRequest(
  95. array(
  96. 'domain' => $base_domain,
  97. 'id' => $this->sharedSessionID,
  98. )
  99. );
  100. $_COOKIE[$cookie_name] = $cookie_value;
  101. if (!headers_sent()) {
  102. $expire = time() + self::FBSS_COOKIE_EXPIRE;
  103. SugarApplication::setCookie($cookie_name, $cookie_value, $expire, '/', '.'.$base_domain, false, true);
  104. } else {
  105. // @codeCoverageIgnoreStart
  106. self::errorLog(
  107. 'Shared session ID cookie could not be set! You must ensure you '.
  108. 'create the Facebook instance before headers have been sent. This '.
  109. 'will cause authentication issues after the first request.'
  110. );
  111. // @codeCoverageIgnoreEnd
  112. }
  113. }
  114. /**
  115. * Provides the implementations of the inherited abstract
  116. * methods. The implementation uses PHP sessions to maintain
  117. * a store for authorization codes, user ids, CSRF states, and
  118. * access tokens.
  119. */
  120. /**
  121. * {@inheritdoc}
  122. *
  123. * @see BaseFacebook::setPersistentData()
  124. */
  125. protected function setPersistentData($key, $value) {
  126. if (!in_array($key, self::$kSupportedKeys)) {
  127. self::errorLog('Unsupported key passed to setPersistentData.');
  128. return;
  129. }
  130. $session_var_name = $this->constructSessionVariableName($key);
  131. $_SESSION[$session_var_name] = $value;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. *
  136. * @see BaseFacebook::getPersistentData()
  137. */
  138. protected function getPersistentData($key, $default = false) {
  139. if (!in_array($key, self::$kSupportedKeys)) {
  140. self::errorLog('Unsupported key passed to getPersistentData.');
  141. return $default;
  142. }
  143. $session_var_name = $this->constructSessionVariableName($key);
  144. return isset($_SESSION[$session_var_name]) ?
  145. $_SESSION[$session_var_name] : $default;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. *
  150. * @see BaseFacebook::clearPersistentData()
  151. */
  152. protected function clearPersistentData($key) {
  153. if (!in_array($key, self::$kSupportedKeys)) {
  154. self::errorLog('Unsupported key passed to clearPersistentData.');
  155. return;
  156. }
  157. $session_var_name = $this->constructSessionVariableName($key);
  158. if (isset($_SESSION[$session_var_name])) {
  159. unset($_SESSION[$session_var_name]);
  160. }
  161. }
  162. /**
  163. * {@inheritdoc}
  164. *
  165. * @see BaseFacebook::clearAllPersistentData()
  166. */
  167. protected function clearAllPersistentData() {
  168. foreach (self::$kSupportedKeys as $key) {
  169. $this->clearPersistentData($key);
  170. }
  171. if ($this->sharedSessionID) {
  172. $this->deleteSharedSessionCookie();
  173. }
  174. }
  175. /**
  176. * Deletes Shared session cookie
  177. */
  178. protected function deleteSharedSessionCookie() {
  179. $cookie_name = $this->getSharedSessionCookieName();
  180. unset($_COOKIE[$cookie_name]);
  181. $base_domain = $this->getBaseDomain();
  182. setcookie($cookie_name, '', 1, '/', '.'.$base_domain, false, true);
  183. }
  184. /**
  185. * Returns the Shared session cookie name
  186. *
  187. * @return string The Shared session cookie name
  188. */
  189. protected function getSharedSessionCookieName() {
  190. return self::FBSS_COOKIE_NAME . '_' . $this->getAppId();
  191. }
  192. /**
  193. * Constructs and returns the name of the session key.
  194. *
  195. * @see setPersistentData()
  196. * @param string $key The key for which the session variable name to construct.
  197. *
  198. * @return string The name of the session key.
  199. */
  200. protected function constructSessionVariableName($key) {
  201. $parts = array('fb', $this->getAppId(), $key);
  202. if ($this->sharedSessionID) {
  203. array_unshift($parts, $this->sharedSessionID);
  204. }
  205. return implode('_', $parts);
  206. }
  207. }