3 * Copyright 2011 Facebook, Inc.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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
18 require_once "base_facebook.php";
21 * Extends the BaseFacebook class with the intent of using
22 * PHP sessions to store user ids and access tokens.
24 class Facebook extends BaseFacebook
27 * Identical to the parent constructor, except that
28 * we start a PHP session to store the user ID and
29 * access token if during the course of execution
32 * @param Array $config the application configuration.
33 * @see BaseFacebook::__construct in facebook.php
35 public function __construct($config) {
39 parent::__construct($config);
42 protected static $kSupportedKeys =
43 array('state', 'code', 'access_token', 'user_id');
46 * Provides the implementations of the inherited abstract
47 * methods. The implementation uses PHP sessions to maintain
48 * a store for authorization codes, user ids, CSRF states, and
51 protected function setPersistentData($key, $value) {
52 if (!in_array($key, self::$kSupportedKeys)) {
53 self::errorLog('Unsupported key passed to setPersistentData.');
57 $session_var_name = $this->constructSessionVariableName($key);
58 $_SESSION[$session_var_name] = $value;
61 protected function getPersistentData($key, $default = false) {
62 if (!in_array($key, self::$kSupportedKeys)) {
63 self::errorLog('Unsupported key passed to getPersistentData.');
67 $session_var_name = $this->constructSessionVariableName($key);
68 return isset($_SESSION[$session_var_name]) ?
69 $_SESSION[$session_var_name] : $default;
72 protected function clearPersistentData($key) {
73 if (!in_array($key, self::$kSupportedKeys)) {
74 self::errorLog('Unsupported key passed to clearPersistentData.');
78 $session_var_name = $this->constructSessionVariableName($key);
79 unset($_SESSION[$session_var_name]);
82 protected function clearAllPersistentData() {
83 foreach (self::$kSupportedKeys as $key) {
84 $this->clearPersistentData($key);
88 protected function constructSessionVariableName($key) {
89 return implode('_', array('fb',