Advertisement
Eddz

Untitled

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