PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/social_connect/facebook/facebook.php

https://github.com/MightyGorgon/icy_phoenix
PHP | 176 lines | 103 code | 20 blank | 53 comment | 13 complexity | c0dff787fc628fc2c3ecdf29ca6a9888 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * Copyright 2011 Facebook, Inc.
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License. You may obtain
  15. * a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  22. * License for the specific language governing permissions and limitations
  23. * under the License.
  24. */
  25. if (!defined('IN_ICYPHOENIX'))
  26. {
  27. die('Hacking attempt');
  28. }
  29. include(IP_ROOT_PATH . "includes/social_connect/facebook/base_facebook." . PHP_EXT);
  30. /**
  31. * Extends the BaseFacebook class with the intent of using
  32. * PHP sessions to store user ids and access tokens.
  33. */
  34. class Facebook extends BaseFacebook
  35. {
  36. const FBSS_COOKIE_NAME = 'fbss';
  37. // We can set this to a high number because the main session
  38. // expiration will trump this.
  39. const FBSS_COOKIE_EXPIRE = 31556926; // 1 year
  40. // Stores the shared session ID if one is set.
  41. protected $sharedSessionID;
  42. /**
  43. * Identical to the parent constructor, except that
  44. * we start a PHP session to store the user ID and
  45. * access token if during the course of execution
  46. * we discover them.
  47. *
  48. * @param Array $config the application configuration. Additionally
  49. * accepts "sharedSession" as a boolean to turn on a secondary
  50. * cookie for environments with a shared session (that is, your app
  51. * shares the domain with other apps).
  52. * @see BaseFacebook::__construct in facebook.php
  53. */
  54. public function __construct($config) {
  55. if (!session_id()) {
  56. session_start();
  57. }
  58. parent::__construct($config);
  59. if (!empty($config['sharedSession'])) {
  60. $this->initSharedSession();
  61. }
  62. }
  63. protected static $kSupportedKeys =
  64. array('state', 'code', 'access_token', 'user_id');
  65. protected function initSharedSession() {
  66. $cookie_name = $this->getSharedSessionCookieName();
  67. if (isset($_COOKIE[$cookie_name])) {
  68. $data = $this->parseSignedRequest($_COOKIE[$cookie_name]);
  69. if ($data && !empty($data['domain']) &&
  70. self::isAllowedDomain($this->getHttpHost(), $data['domain'])) {
  71. // good case
  72. $this->sharedSessionID = $data['id'];
  73. return;
  74. }
  75. // ignoring potentially unreachable data
  76. }
  77. // evil/corrupt/missing case
  78. $base_domain = $this->getBaseDomain();
  79. $this->sharedSessionID = md5(uniqid(mt_rand(), true));
  80. $cookie_value = $this->makeSignedRequest(
  81. array(
  82. 'domain' => $base_domain,
  83. 'id' => $this->sharedSessionID,
  84. )
  85. );
  86. $_COOKIE[$cookie_name] = $cookie_value;
  87. if (!headers_sent()) {
  88. $expire = time() + self::FBSS_COOKIE_EXPIRE;
  89. setcookie($cookie_name, $cookie_value, $expire, '/', '.'.$base_domain);
  90. } else {
  91. // @codeCoverageIgnoreStart
  92. self::errorLog(
  93. 'Shared session ID cookie could not be set! You must ensure you '.
  94. 'create the Facebook instance before headers have been sent. This '.
  95. 'will cause authentication issues after the first request.'
  96. );
  97. // @codeCoverageIgnoreEnd
  98. }
  99. }
  100. /**
  101. * Provides the implementations of the inherited abstract
  102. * methods. The implementation uses PHP sessions to maintain
  103. * a store for authorization codes, user ids, CSRF states, and
  104. * access tokens.
  105. */
  106. protected function setPersistentData($key, $value) {
  107. if (!in_array($key, self::$kSupportedKeys)) {
  108. self::errorLog('Unsupported key passed to setPersistentData.');
  109. return;
  110. }
  111. $session_var_name = $this->constructSessionVariableName($key);
  112. $_SESSION[$session_var_name] = $value;
  113. }
  114. protected function getPersistentData($key, $default = false) {
  115. if (!in_array($key, self::$kSupportedKeys)) {
  116. self::errorLog('Unsupported key passed to getPersistentData.');
  117. return $default;
  118. }
  119. $session_var_name = $this->constructSessionVariableName($key);
  120. return isset($_SESSION[$session_var_name]) ?
  121. $_SESSION[$session_var_name] : $default;
  122. }
  123. protected function clearPersistentData($key) {
  124. if (!in_array($key, self::$kSupportedKeys)) {
  125. self::errorLog('Unsupported key passed to clearPersistentData.');
  126. return;
  127. }
  128. $session_var_name = $this->constructSessionVariableName($key);
  129. unset($_SESSION[$session_var_name]);
  130. }
  131. protected function clearAllPersistentData() {
  132. foreach (self::$kSupportedKeys as $key) {
  133. $this->clearPersistentData($key);
  134. }
  135. if ($this->sharedSessionID) {
  136. $this->deleteSharedSessionCookie();
  137. }
  138. }
  139. protected function deleteSharedSessionCookie() {
  140. $cookie_name = $this->getSharedSessionCookieName();
  141. unset($_COOKIE[$cookie_name]);
  142. $base_domain = $this->getBaseDomain();
  143. setcookie($cookie_name, '', 1, '/', '.'.$base_domain);
  144. }
  145. protected function getSharedSessionCookieName() {
  146. return self::FBSS_COOKIE_NAME . '_' . $this->getAppId();
  147. }
  148. protected function constructSessionVariableName($key) {
  149. $parts = array('fb', $this->getAppId(), $key);
  150. if ($this->sharedSessionID) {
  151. array_unshift($parts, $this->sharedSessionID);
  152. }
  153. return implode('_', $parts);
  154. }
  155. }
  156. ?>