PageRenderTime 199ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/premium-seo-pack/lib/scripts/facebook/facebook.php

https://gitlab.com/iamgraeme/royalmile
PHP | 173 lines | 106 code | 20 blank | 47 comment | 15 complexity | 62c5052211fabbbfe629713bb4c27724 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2011 psp_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 Basepsp_Facebook class with the intent of using
  20. * PHP sessions to store user ids and access tokens.
  21. */
  22. class psp_Facebook extends Basepsp_Facebook
  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 Basepsp_Facebook::__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. // re-load the persisted state, since parent
  50. // attempted to read out of non-shared cookie
  51. $state = $this->getPersistentData('state');
  52. if (!empty($state)) {
  53. $this->state = $state;
  54. } else {
  55. $this->state = null;
  56. }
  57. }
  58. }
  59. protected static $kSupportedKeys =
  60. array('state', 'code', 'access_token', 'user_id');
  61. protected function initSharedSession() {
  62. $cookie_name = $this->getSharedSessionCookieName();
  63. if (isset($_COOKIE[$cookie_name])) {
  64. $data = $this->parseSignedRequest($_COOKIE[$cookie_name]);
  65. if ($data && !empty($data['domain']) &&
  66. self::isAllowedDomain($this->getHttpHost(), $data['domain'])) {
  67. // good case
  68. $this->sharedSessionID = $data['id'];
  69. return;
  70. }
  71. // ignoring potentially unreachable data
  72. }
  73. // evil/corrupt/missing case
  74. $base_domain = $this->getBaseDomain();
  75. $this->sharedSessionID = md5(uniqid(mt_rand(), true));
  76. $cookie_value = $this->makeSignedRequest(
  77. array(
  78. 'domain' => $base_domain,
  79. 'id' => $this->sharedSessionID,
  80. )
  81. );
  82. $_COOKIE[$cookie_name] = $cookie_value;
  83. if (!headers_sent()) {
  84. $expire = time() + self::FBSS_COOKIE_EXPIRE;
  85. setcookie($cookie_name, $cookie_value, $expire, '/', '.'.$base_domain);
  86. } else {
  87. // @codeCoverageIgnoreStart
  88. self::errorLog(
  89. 'Shared session ID cookie could not be set! You must ensure you '.
  90. 'create the psp_Facebook instance before headers have been sent. This '.
  91. 'will cause authentication issues after the first request.'
  92. );
  93. // @codeCoverageIgnoreEnd
  94. }
  95. }
  96. /**
  97. * Provides the implementations of the inherited abstract
  98. * methods. The implementation uses PHP sessions to maintain
  99. * a store for authorization codes, user ids, CSRF states, and
  100. * access tokens.
  101. */
  102. protected function setPersistentData($key, $value) {
  103. if (!in_array($key, self::$kSupportedKeys)) {
  104. self::errorLog('Unsupported key passed to setPersistentData.');
  105. return;
  106. }
  107. $session_var_name = $this->constructSessionVariableName($key);
  108. $_SESSION[$session_var_name] = $value;
  109. }
  110. protected function getPersistentData($key, $default = false) {
  111. if (!in_array($key, self::$kSupportedKeys)) {
  112. self::errorLog('Unsupported key passed to getPersistentData.');
  113. return $default;
  114. }
  115. $session_var_name = $this->constructSessionVariableName($key);
  116. return isset($_SESSION[$session_var_name]) ?
  117. $_SESSION[$session_var_name] : $default;
  118. }
  119. protected function clearPersistentData($key) {
  120. if (!in_array($key, self::$kSupportedKeys)) {
  121. self::errorLog('Unsupported key passed to clearPersistentData.');
  122. return;
  123. }
  124. $session_var_name = $this->constructSessionVariableName($key);
  125. if (isset($_SESSION[$session_var_name])) {
  126. unset($_SESSION[$session_var_name]);
  127. }
  128. }
  129. protected function clearAllPersistentData() {
  130. foreach (self::$kSupportedKeys as $key) {
  131. $this->clearPersistentData($key);
  132. }
  133. if ($this->sharedSessionID) {
  134. $this->deleteSharedSessionCookie();
  135. }
  136. }
  137. protected function deleteSharedSessionCookie() {
  138. $cookie_name = $this->getSharedSessionCookieName();
  139. unset($_COOKIE[$cookie_name]);
  140. $base_domain = $this->getBaseDomain();
  141. setcookie($cookie_name, '', 1, '/', '.'.$base_domain);
  142. }
  143. protected function getSharedSessionCookieName() {
  144. return self::FBSS_COOKIE_NAME . '_' . $this->getAppId();
  145. }
  146. protected function constructSessionVariableName($key) {
  147. $parts = array('fb', $this->getAppId(), $key);
  148. if ($this->sharedSessionID) {
  149. array_unshift($parts, $this->sharedSessionID);
  150. }
  151. return implode('_', $parts);
  152. }
  153. }