| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |  * Copyright 2011 Facebook, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); you may | 
					
						
							|  |  |  |  * not use this file except in compliance with the License. You may obtain | 
					
						
							|  |  |  |  * a copy of the License at | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
					
						
							|  |  |  |  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | 
					
						
							|  |  |  |  * License for the specific language governing permissions and limitations | 
					
						
							|  |  |  |  * under the License. | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  | require_once "base_facebook.php"; | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |  * Extends the BaseFacebook class with the intent of using | 
					
						
							|  |  |  |  * PHP sessions to store user ids and access tokens. | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  | class Facebook extends BaseFacebook | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |    * Identical to the parent constructor, except that | 
					
						
							|  |  |  |    * we start a PHP session to store the user ID and | 
					
						
							|  |  |  |    * access token if during the course of execution | 
					
						
							|  |  |  |    * we discover them. | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |    * | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |    * @param Array $config the application configuration. | 
					
						
							|  |  |  |    * @see BaseFacebook::__construct in facebook.php | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |    */ | 
					
						
							|  |  |  |   public function __construct($config) { | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |     if (!session_id()) { | 
					
						
							|  |  |  |       session_start(); | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |     parent::__construct($config); | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |   protected static $kSupportedKeys = | 
					
						
							|  |  |  |     array('state', 'code', 'access_token', 'user_id'); | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |    * Provides the implementations of the inherited abstract | 
					
						
							|  |  |  |    * methods.  The implementation uses PHP sessions to maintain | 
					
						
							|  |  |  |    * a store for authorization codes, user ids, CSRF states, and | 
					
						
							|  |  |  |    * access tokens. | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |   protected function setPersistentData($key, $value) { | 
					
						
							|  |  |  |     if (!in_array($key, self::$kSupportedKeys)) { | 
					
						
							|  |  |  |       self::errorLog('Unsupported key passed to setPersistentData.'); | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |     $session_var_name = $this->constructSessionVariableName($key); | 
					
						
							|  |  |  |     $_SESSION[$session_var_name] = $value; | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |   protected function getPersistentData($key, $default = false) { | 
					
						
							|  |  |  |     if (!in_array($key, self::$kSupportedKeys)) { | 
					
						
							|  |  |  |       self::errorLog('Unsupported key passed to getPersistentData.'); | 
					
						
							|  |  |  |       return $default; | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |     $session_var_name = $this->constructSessionVariableName($key); | 
					
						
							|  |  |  |     return isset($_SESSION[$session_var_name]) ? | 
					
						
							|  |  |  |       $_SESSION[$session_var_name] : $default; | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |   protected function clearPersistentData($key) { | 
					
						
							|  |  |  |     if (!in_array($key, self::$kSupportedKeys)) { | 
					
						
							|  |  |  |       self::errorLog('Unsupported key passed to clearPersistentData.'); | 
					
						
							|  |  |  |       return; | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |     $session_var_name = $this->constructSessionVariableName($key); | 
					
						
							|  |  |  |     unset($_SESSION[$session_var_name]); | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |   protected function clearAllPersistentData() { | 
					
						
							|  |  |  |     foreach (self::$kSupportedKeys as $key) { | 
					
						
							|  |  |  |       $this->clearPersistentData($key); | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-22 15:47:55 -07:00
										 |  |  |   protected function constructSessionVariableName($key) { | 
					
						
							|  |  |  |     return implode('_', array('fb', | 
					
						
							|  |  |  |                               $this->getAppId(), | 
					
						
							|  |  |  |                               $key)); | 
					
						
							| 
									
										
										
										
											2010-10-30 00:44:16 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } |