Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

facebook.php

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 2.91 KB  |  views: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. <?php
  3. /**
  4.  * Copyright 2011 Facebook, Inc.
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the "License"); you may
  7.  * not use this file except in compliance with the License. You may obtain
  8.  * a copy of the License at
  9.  *
  10.  *     http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15.  * License for the specific language governing permissions and limitations
  16.  * under the License.
  17.  */
  18.  
  19. require_once "base_facebook.php";
  20.  
  21. /**
  22.  * Extends the BaseFacebook class with the intent of using
  23.  * PHP sessions to store user ids and access tokens.
  24.  */
  25. class Facebook extends BaseFacebook
  26. {
  27.   /**
  28.    * Identical to the parent constructor, except that
  29.    * we start a PHP session to store the user ID and
  30.    * access token if during the course of execution
  31.    * we discover them.
  32.    *
  33.    * @param Array $config the application configuration.
  34.    * @see BaseFacebook::__construct in facebook.php
  35.    */
  36.   public function __construct($config) {
  37.     if (!session_id()) {
  38.       session_start();
  39.     }
  40.     parent::__construct($config);
  41.   }
  42.  
  43.   protected static $kSupportedKeys =
  44.     array('state', 'code', 'access_token', 'user_id');
  45.  
  46.   /**
  47.    * Provides the implementations of the inherited abstract
  48.    * methods.  The implementation uses PHP sessions to maintain
  49.    * a store for authorization codes, user ids, CSRF states, and
  50.    * access tokens.
  51.    */
  52.   protected function setPersistentData($key, $value) {
  53.     if (!in_array($key, self::$kSupportedKeys)) {
  54.       self::errorLog('Unsupported key passed to setPersistentData.');
  55.       return;
  56.     }
  57.  
  58.     $session_var_name = $this->constructSessionVariableName($key);
  59.     $_SESSION[$session_var_name] = $value;
  60.   }
  61.  
  62.   protected function getPersistentData($key, $default = false) {
  63.     if (!in_array($key, self::$kSupportedKeys)) {
  64.       self::errorLog('Unsupported key passed to getPersistentData.');
  65.       return $default;
  66.     }
  67.  
  68.     $session_var_name = $this->constructSessionVariableName($key);
  69.     return isset($_SESSION[$session_var_name]) ?
  70.       $_SESSION[$session_var_name] : $default;
  71.   }
  72.  
  73.   protected function clearPersistentData($key) {
  74.     if (!in_array($key, self::$kSupportedKeys)) {
  75.       self::errorLog('Unsupported key passed to clearPersistentData.');
  76.       return;
  77.     }
  78.  
  79.     $session_var_name = $this->constructSessionVariableName($key);
  80.     unset($_SESSION[$session_var_name]);
  81.   }
  82.  
  83.   protected function clearAllPersistentData() {
  84.     foreach (self::$kSupportedKeys as $key) {
  85.       $this->clearPersistentData($key);
  86.     }
  87.   }
  88.  
  89.   protected function constructSessionVariableName($key) {
  90.     return implode('_', array('fb',
  91.                               $this->getAppId(),
  92.                               $key));
  93.   }
  94. }