PageRenderTime 20ms CodeModel.GetById 11ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/facebook.php

http://github.com/facebook/php-sdk
PHP | 93 lines | 48 code | 11 blank | 34 comment | 4 complexity | 9ca98ec14e2f3926ba89fcb753fbc598 MD5 | raw file
 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
18require_once "base_facebook.php";
19
20/**
21 * Extends the BaseFacebook class with the intent of using
22 * PHP sessions to store user ids and access tokens.
23 */
24class Facebook extends BaseFacebook
25{
26  /**
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
30   * we discover them.
31   *
32   * @param Array $config the application configuration.
33   * @see BaseFacebook::__construct in facebook.php
34   */
35  public function __construct($config) {
36    if (!session_id()) {
37      session_start();
38    }
39    parent::__construct($config);
40  }
41
42  protected static $kSupportedKeys =
43    array('state', 'code', 'access_token', 'user_id');
44
45  /**
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
49   * access tokens.
50   */
51  protected function setPersistentData($key, $value) {
52    if (!in_array($key, self::$kSupportedKeys)) {
53      self::errorLog('Unsupported key passed to setPersistentData.');
54      return;
55    }
56
57    $session_var_name = $this->constructSessionVariableName($key);
58    $_SESSION[$session_var_name] = $value;
59  }
60
61  protected function getPersistentData($key, $default = false) {
62    if (!in_array($key, self::$kSupportedKeys)) {
63      self::errorLog('Unsupported key passed to getPersistentData.');
64      return $default;
65    }
66
67    $session_var_name = $this->constructSessionVariableName($key);
68    return isset($_SESSION[$session_var_name]) ?
69      $_SESSION[$session_var_name] : $default;
70  }
71
72  protected function clearPersistentData($key) {
73    if (!in_array($key, self::$kSupportedKeys)) {
74      self::errorLog('Unsupported key passed to clearPersistentData.');
75      return;
76    }
77
78    $session_var_name = $this->constructSessionVariableName($key);
79    unset($_SESSION[$session_var_name]);
80  }
81
82  protected function clearAllPersistentData() {
83    foreach (self::$kSupportedKeys as $key) {
84      $this->clearPersistentData($key);
85    }
86  }
87
88  protected function constructSessionVariableName($key) {
89    return implode('_', array('fb',
90                              $this->getAppId(),
91                              $key));
92  }
93}