PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/pointfinder/admin/core/Facebook/facebook.php

https://bitbucket.org/encapsulator/link-directory
PHP | 226 lines | 106 code | 19 blank | 101 comment | 17 complexity | 5f6e015aebeb62fca1c96e6bfa77ce1f MD5 | raw file
  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. /**
  18. * Extends the BaseFacebook class with the intent of using
  19. * PHP sessions to store user ids and access tokens.
  20. */
  21. class Facebook extends BaseFacebook
  22. {
  23. /**
  24. * Cookie prefix
  25. */
  26. const FBSS_COOKIE_NAME = 'fbss';
  27. /**
  28. * We can set this to a high number because the main session
  29. * expiration will trump this.
  30. */
  31. const FBSS_COOKIE_EXPIRE = 31556926; // 1 year
  32. /**
  33. * Stores the shared session ID if one is set.
  34. *
  35. * @var string
  36. */
  37. protected $sharedSessionID;
  38. /**
  39. * Identical to the parent constructor, except that
  40. * we start a PHP session to store the user ID and
  41. * access token if during the course of execution
  42. * we discover them.
  43. *
  44. * @param array $config the application configuration. Additionally
  45. * accepts "sharedSession" as a boolean to turn on a secondary
  46. * cookie for environments with a shared session (that is, your app
  47. * shares the domain with other apps).
  48. *
  49. * @see BaseFacebook::__construct
  50. */
  51. public function __construct($config) {
  52. if ((function_exists('session_status')
  53. && session_status() !== PHP_SESSION_ACTIVE) || !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. setcookie($cookie_name, $cookie_value, $expire, '/', '.'.$base_domain);
  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);
  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. }