PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/libs/facebook/facebook.php

https://bitbucket.org/VolCh/2moons
PHP | 160 lines | 98 code | 17 blank | 45 comment | 12 complexity | dcb23268876023d6fa7e1d3349d13819 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-3.0, GPL-2.0, Apache-2.0, AGPL-3.0
  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. const FBSS_COOKIE_NAME = 'fbss';
  25. // We can set this to a high number because the main session
  26. // expiration will trump this.
  27. const FBSS_COOKIE_EXPIRE = 31556926; // 1 year
  28. // Stores the shared session ID if one is set.
  29. protected $sharedSessionID;
  30. /**
  31. * Identical to the parent constructor, except that
  32. * we start a PHP session to store the user ID and
  33. * access token if during the course of execution
  34. * we discover them.
  35. *
  36. * @param Array $config the application configuration. Additionally
  37. * accepts "sharedSession" as a boolean to turn on a secondary
  38. * cookie for environments with a shared session (that is, your app
  39. * shares the domain with other apps).
  40. * @see BaseFacebook::__construct in facebook.php
  41. */
  42. public function __construct($config) {
  43. if (!session_id()) {
  44. session_start();
  45. }
  46. parent::__construct($config);
  47. if (!empty($config['sharedSession'])) {
  48. $this->initSharedSession();
  49. }
  50. }
  51. protected static $kSupportedKeys =
  52. array('state', 'code', 'access_token', 'user_id');
  53. protected function initSharedSession() {
  54. $cookie_name = $this->getSharedSessionCookieName();
  55. if (isset($_COOKIE[$cookie_name])) {
  56. $data = $this->parseSignedRequest($_COOKIE[$cookie_name]);
  57. if ($data && !empty($data['domain']) &&
  58. self::isAllowedDomain($this->getHttpHost(), $data['domain'])) {
  59. // good case
  60. $this->sharedSessionID = $data['id'];
  61. return;
  62. }
  63. // ignoring potentially unreachable data
  64. }
  65. // evil/corrupt/missing case
  66. $base_domain = $this->getBaseDomain();
  67. $this->sharedSessionID = md5(uniqid(mt_rand(), true));
  68. $cookie_value = $this->makeSignedRequest(
  69. array(
  70. 'domain' => $base_domain,
  71. 'id' => $this->sharedSessionID,
  72. )
  73. );
  74. $_COOKIE[$cookie_name] = $cookie_value;
  75. if (!headers_sent()) {
  76. $expire = time() + self::FBSS_COOKIE_EXPIRE;
  77. setcookie($cookie_name, $cookie_value, $expire, '/', '.'.$base_domain);
  78. } else {
  79. // @codeCoverageIgnoreStart
  80. self::errorLog(
  81. 'Shared session ID cookie could not be set! You must ensure you '.
  82. 'create the Facebook instance before headers have been sent. This '.
  83. 'will cause authentication issues after the first request.'
  84. );
  85. // @codeCoverageIgnoreEnd
  86. }
  87. }
  88. /**
  89. * Provides the implementations of the inherited abstract
  90. * methods. The implementation uses PHP sessions to maintain
  91. * a store for authorization codes, user ids, CSRF states, and
  92. * access tokens.
  93. */
  94. protected function setPersistentData($key, $value) {
  95. if (!in_array($key, self::$kSupportedKeys)) {
  96. self::errorLog('Unsupported key passed to setPersistentData.');
  97. return;
  98. }
  99. $session_var_name = $this->constructSessionVariableName($key);
  100. $_SESSION[$session_var_name] = $value;
  101. }
  102. protected function getPersistentData($key, $default = false) {
  103. if (!in_array($key, self::$kSupportedKeys)) {
  104. self::errorLog('Unsupported key passed to getPersistentData.');
  105. return $default;
  106. }
  107. $session_var_name = $this->constructSessionVariableName($key);
  108. return isset($_SESSION[$session_var_name]) ?
  109. $_SESSION[$session_var_name] : $default;
  110. }
  111. protected function clearPersistentData($key) {
  112. if (!in_array($key, self::$kSupportedKeys)) {
  113. self::errorLog('Unsupported key passed to clearPersistentData.');
  114. return;
  115. }
  116. $session_var_name = $this->constructSessionVariableName($key);
  117. unset($_SESSION[$session_var_name]);
  118. }
  119. protected function clearAllPersistentData() {
  120. foreach (self::$kSupportedKeys as $key) {
  121. $this->clearPersistentData($key);
  122. }
  123. if ($this->sharedSessionID) {
  124. $this->deleteSharedSessionCookie();
  125. }
  126. }
  127. protected function deleteSharedSessionCookie() {
  128. $cookie_name = $this->getSharedSessionCookieName();
  129. unset($_COOKIE[$cookie_name]);
  130. $base_domain = $this->getBaseDomain();
  131. setcookie($cookie_name, '', 1, '/', '.'.$base_domain);
  132. }
  133. protected function getSharedSessionCookieName() {
  134. return self::FBSS_COOKIE_NAME . '_' . $this->getAppId();
  135. }
  136. protected function constructSessionVariableName($key) {
  137. $parts = array('fb', $this->getAppId(), $key);
  138. if ($this->sharedSessionID) {
  139. array_unshift($parts, $this->sharedSessionID);
  140. }
  141. return implode('_', $parts);
  142. }
  143. }