facebook.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * Identical to the parent constructor, except that
  26. * we start a PHP session to store the user ID and
  27. * access token if during the course of execution
  28. * we discover them.
  29. *
  30. * @param Array $config the application configuration.
  31. * @see BaseFacebook::__construct in facebook.php
  32. */
  33. public function __construct($config) {
  34. if (!session_id()) {
  35. session_start();
  36. }
  37. parent::__construct($config);
  38. }
  39. protected static $kSupportedKeys =
  40. array('state', 'code', 'access_token', 'user_id');
  41. /**
  42. * Provides the implementations of the inherited abstract
  43. * methods. The implementation uses PHP sessions to maintain
  44. * a store for authorization codes, user ids, CSRF states, and
  45. * access tokens.
  46. */
  47. protected function setPersistentData($key, $value) {
  48. if (!in_array($key, self::$kSupportedKeys)) {
  49. self::errorLog('Unsupported key passed to setPersistentData.');
  50. return;
  51. }
  52. $session_var_name = $this->constructSessionVariableName($key);
  53. $_SESSION[$session_var_name] = $value;
  54. }
  55. protected function getPersistentData($key, $default = false) {
  56. if (!in_array($key, self::$kSupportedKeys)) {
  57. self::errorLog('Unsupported key passed to getPersistentData.');
  58. return $default;
  59. }
  60. $session_var_name = $this->constructSessionVariableName($key);
  61. return isset($_SESSION[$session_var_name]) ?
  62. $_SESSION[$session_var_name] : $default;
  63. }
  64. protected function clearPersistentData($key) {
  65. if (!in_array($key, self::$kSupportedKeys)) {
  66. self::errorLog('Unsupported key passed to clearPersistentData.');
  67. return;
  68. }
  69. $session_var_name = $this->constructSessionVariableName($key);
  70. unset($_SESSION[$session_var_name]);
  71. }
  72. protected function clearAllPersistentData() {
  73. foreach (self::$kSupportedKeys as $key) {
  74. $this->clearPersistentData($key);
  75. }
  76. }
  77. protected function constructSessionVariableName($key) {
  78. return implode('_', array('fb',
  79. $this->getAppId(),
  80. $key));
  81. }
  82. }