Advertisement
Eddz

Untitled

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