Advertisement
Eddz

Untitled

Sep 12th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.27 KB | None | 0 0
  1. <?php
  2.  
  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.   const FBSS_COOKIE_NAME = 'fbss';
  28.  
  29.   // We can set this to a high number because the main session
  30.   // expiration will trump this.
  31.   const FBSS_COOKIE_EXPIRE = 31556926; // 1 year
  32.  
  33.   // Stores the shared session ID if one is set.
  34.   protected $sharedSessionID;
  35.  
  36.   /**
  37.    * Identical to the parent constructor, except that
  38.    * we start a PHP session to store the user ID and
  39.    * access token if during the course of execution
  40.    * we discover them.
  41.    *
  42.    * @param Array $config the application configuration. Additionally
  43.    * accepts "sharedSession" as a boolean to turn on a secondary
  44.    * cookie for environments with a shared session (that is, your app
  45.    * shares the domain with other apps).
  46.    * @see BaseFacebook::__construct in facebook.php
  47.    */
  48.   public function __construct($config) {
  49.     die('fdff');
  50.     if (!session_id()) {
  51.       session_start();
  52.     }
  53.     parent::__construct($config);
  54.     if (!empty($config['sharedSession'])) {
  55.       $this->initSharedSession();
  56.     }
  57.   }
  58.  
  59.   protected static $kSupportedKeys =
  60.     array('state', 'code', 'access_token', 'user_id');
  61.  
  62.   protected function initSharedSession() {
  63.     $cookie_name = $this->getSharedSessionCookieName();
  64.     if (isset($_COOKIE[$cookie_name])) {
  65.       $data = $this->parseSignedRequest($_COOKIE[$cookie_name]);
  66.       if ($data && !empty($data['domain']) &&
  67.           self::isAllowedDomain($this->getHttpHost(), $data['domain'])) {
  68.         // good case
  69.         $this->sharedSessionID = $data['id'];
  70.         return;
  71.       }
  72.       // ignoring potentially unreachable data
  73.     }
  74.     // evil/corrupt/missing case
  75.     $base_domain = $this->getBaseDomain();
  76.     $this->sharedSessionID = md5(uniqid(mt_rand(), true));
  77.     $cookie_value = $this->makeSignedRequest(
  78.       array(
  79.         'domain' => $base_domain,
  80.         'id' => $this->sharedSessionID,
  81.       )
  82.     );
  83.     $_COOKIE[$cookie_name] = $cookie_value;
  84.     if (!headers_sent()) {
  85.       $expire = time() + self::FBSS_COOKIE_EXPIRE;
  86.       setcookie($cookie_name, $cookie_value, $expire, '/', '.'.$base_domain);
  87.     } else {
  88.       // @codeCoverageIgnoreStart
  89.       self::errorLog(
  90.         'Shared session ID cookie could not be set! You must ensure you '.
  91.         'create the Facebook instance before headers have been sent. This '.
  92.         'will cause authentication issues after the first request.'
  93.       );
  94.       // @codeCoverageIgnoreEnd
  95.     }
  96.   }
  97.  
  98.   /**
  99.    * Provides the implementations of the inherited abstract
  100.    * methods.  The implementation uses PHP sessions to maintain
  101.    * a store for authorization codes, user ids, CSRF states, and
  102.    * access tokens.
  103.    */
  104.   protected function setPersistentData($key, $value) {
  105.     if (!in_array($key, self::$kSupportedKeys)) {
  106.       self::errorLog('Unsupported key passed to setPersistentData.');
  107.       return;
  108.     }
  109.  
  110.     $session_var_name = $this->constructSessionVariableName($key);
  111.     $_SESSION[$session_var_name] = $value;
  112.   }
  113.  
  114.   protected function getPersistentData($key, $default = false) {
  115.     if (!in_array($key, self::$kSupportedKeys)) {
  116.       self::errorLog('Unsupported key passed to getPersistentData.');
  117.       return $default;
  118.     }
  119.  
  120.     $session_var_name = $this->constructSessionVariableName($key);
  121.     return isset($_SESSION[$session_var_name]) ?
  122.       $_SESSION[$session_var_name] : $default;
  123.   }
  124.  
  125.   protected function clearPersistentData($key) {
  126.     if (!in_array($key, self::$kSupportedKeys)) {
  127.       self::errorLog('Unsupported key passed to clearPersistentData.');
  128.       return;
  129.     }
  130.  
  131.     $session_var_name = $this->constructSessionVariableName($key);
  132.     unset($_SESSION[$session_var_name]);
  133.   }
  134.  
  135.   protected function clearAllPersistentData() {
  136.     foreach (self::$kSupportedKeys as $key) {
  137.       $this->clearPersistentData($key);
  138.     }
  139.     if ($this->sharedSessionID) {
  140.       $this->deleteSharedSessionCookie();
  141.     }
  142.   }
  143.  
  144.   protected function deleteSharedSessionCookie() {
  145.     $cookie_name = $this->getSharedSessionCookieName();
  146.     unset($_COOKIE[$cookie_name]);
  147.     $base_domain = $this->getBaseDomain();
  148.     setcookie($cookie_name, '', 1, '/', '.'.$base_domain);
  149.   }
  150.  
  151.   protected function getSharedSessionCookieName() {
  152.     return self::FBSS_COOKIE_NAME . '_' . $this->getAppId();
  153.   }
  154.  
  155.   protected function constructSessionVariableName($key) {
  156.     $parts = array('fb', $this->getAppId(), $key);
  157.     if ($this->sharedSessionID) {
  158.       array_unshift($parts, $this->sharedSessionID);
  159.     }
  160.     return implode('_', $parts);
  161.   }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement