PageRenderTime 128ms CodeModel.GetById 27ms RepoModel.GetById 4ms app.codeStats 1ms

/Application/Library/facebook_full/src/Facebook/FacebookSession.php

https://gitlab.com/hoanghung.dev/aloads
PHP | 367 lines | 130 code | 30 blank | 207 comment | 6 complexity | a41daaa68e1e6e8b6d464d28fc7671c4 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2014 Facebook, Inc.
  4. *
  5. * You are hereby granted a non-exclusive, worldwide, royalty-free license to
  6. * use, copy, modify, and distribute this software in source code or binary
  7. * form for use in connection with the web services and APIs provided by
  8. * Facebook.
  9. *
  10. * As with any software that integrates with the Facebook platform, your use
  11. * of this software is subject to the Facebook Developer Principles and
  12. * Policies [http://developers.facebook.com/policy/]. This copyright notice
  13. * shall be included in all copies or substantial portions of the software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. namespace Facebook;
  25. use Facebook\Entities\AccessToken;
  26. use Facebook\Entities\SignedRequest;
  27. /**
  28. * Class FacebookSession
  29. * @package Facebook
  30. * @author Fosco Marotto <fjm@fb.com>
  31. * @author David Poll <depoll@fb.com>
  32. */
  33. class FacebookSession
  34. {
  35. /**
  36. * @var string
  37. */
  38. private static $defaultAppId;
  39. /**
  40. * @var string
  41. */
  42. private static $defaultAppSecret;
  43. /**
  44. * @var AccessToken The AccessToken entity for this connection.
  45. */
  46. private $accessToken;
  47. /**
  48. * @var SignedRequest
  49. */
  50. private $signedRequest;
  51. /**
  52. * @var bool
  53. */
  54. protected static $useAppSecretProof = true;
  55. /**
  56. * When creating a Session from an access_token, use:
  57. * var $session = new FacebookSession($accessToken);
  58. * This will validate the token and provide a Session object ready for use.
  59. * It will throw a SessionException in case of error.
  60. *
  61. * @param AccessToken|string $accessToken
  62. * @param SignedRequest $signedRequest The SignedRequest entity
  63. */
  64. public function __construct($accessToken, SignedRequest $signedRequest = null)
  65. {
  66. $this->accessToken = $accessToken instanceof AccessToken ? $accessToken : new AccessToken($accessToken);
  67. $this->signedRequest = $signedRequest;
  68. }
  69. /**
  70. * Returns the access token.
  71. *
  72. * @return string
  73. */
  74. public function getToken()
  75. {
  76. return (string) $this->accessToken;
  77. }
  78. /**
  79. * Returns the access token entity.
  80. *
  81. * @return AccessToken
  82. */
  83. public function getAccessToken()
  84. {
  85. return $this->accessToken;
  86. }
  87. /**
  88. * Returns the SignedRequest entity.
  89. *
  90. * @return SignedRequest
  91. */
  92. public function getSignedRequest()
  93. {
  94. return $this->signedRequest;
  95. }
  96. /**
  97. * Returns the signed request payload.
  98. *
  99. * @return null|array
  100. */
  101. public function getSignedRequestData()
  102. {
  103. return $this->signedRequest ? $this->signedRequest->getPayload() : null;
  104. }
  105. /**
  106. * Returns a property from the signed request data if available.
  107. *
  108. * @param string $key
  109. *
  110. * @return null|mixed
  111. */
  112. public function getSignedRequestProperty($key)
  113. {
  114. return $this->signedRequest ? $this->signedRequest->get($key) : null;
  115. }
  116. /**
  117. * Returns user_id from signed request data if available.
  118. *
  119. * @return null|string
  120. */
  121. public function getUserId()
  122. {
  123. return $this->signedRequest ? $this->signedRequest->getUserId() : null;
  124. }
  125. // @TODO Remove getSessionInfo() in 4.1: can be accessed from AccessToken directly
  126. /**
  127. * getSessionInfo - Makes a request to /debug_token with the appropriate
  128. * arguments to get debug information about the sessions token.
  129. *
  130. * @param string|null $appId
  131. * @param string|null $appSecret
  132. *
  133. * @return GraphSessionInfo
  134. */
  135. public function getSessionInfo($appId = null, $appSecret = null)
  136. {
  137. return $this->accessToken->getInfo($appId, $appSecret);
  138. }
  139. // @TODO Remove getLongLivedSession() in 4.1: can be accessed from AccessToken directly
  140. /**
  141. * getLongLivedSession - Returns a new Facebook session resulting from
  142. * extending a short-lived access token. If this session is not
  143. * short-lived, returns $this.
  144. *
  145. * @param string|null $appId
  146. * @param string|null $appSecret
  147. *
  148. * @return FacebookSession
  149. */
  150. public function getLongLivedSession($appId = null, $appSecret = null)
  151. {
  152. $longLivedAccessToken = $this->accessToken->extend($appId, $appSecret);
  153. return new static($longLivedAccessToken, $this->signedRequest);
  154. }
  155. // @TODO Remove getExchangeToken() in 4.1: can be accessed from AccessToken directly
  156. /**
  157. * getExchangeToken - Returns an exchange token string which can be sent
  158. * back to clients and exchanged for a device-linked access token.
  159. *
  160. * @param string|null $appId
  161. * @param string|null $appSecret
  162. *
  163. * @return string
  164. */
  165. public function getExchangeToken($appId = null, $appSecret = null)
  166. {
  167. return AccessToken::getCodeFromAccessToken($this->accessToken, $appId, $appSecret);
  168. }
  169. // @TODO Remove validate() in 4.1: can be accessed from AccessToken directly
  170. /**
  171. * validate - Ensures the current session is valid, throwing an exception if
  172. * not. Fetches token info from Facebook.
  173. *
  174. * @param string|null $appId Application ID to use
  175. * @param string|null $appSecret App secret value to use
  176. * @param string|null $machineId
  177. *
  178. * @return boolean
  179. *
  180. * @throws FacebookSDKException
  181. */
  182. public function validate($appId = null, $appSecret = null, $machineId = null)
  183. {
  184. if ($this->accessToken->isValid($appId, $appSecret, $machineId)) {
  185. return true;
  186. }
  187. // @TODO For v4.1 this should not throw an exception, but just return false.
  188. throw new FacebookSDKException(
  189. 'Session has expired, or is not valid for this app.', 601
  190. );
  191. }
  192. // @TODO Remove validateSessionInfo() in 4.1: can be accessed from AccessToken directly
  193. /**
  194. * validateTokenInfo - Ensures the provided GraphSessionInfo object is valid,
  195. * throwing an exception if not. Ensures the appId matches,
  196. * that the token is valid and has not expired.
  197. *
  198. * @param GraphSessionInfo $tokenInfo
  199. * @param string|null $appId Application ID to use
  200. * @param string|null $machineId
  201. *
  202. * @return boolean
  203. *
  204. * @throws FacebookSDKException
  205. */
  206. public static function validateSessionInfo(GraphSessionInfo $tokenInfo,
  207. $appId = null,
  208. $machineId = null)
  209. {
  210. if (AccessToken::validateAccessToken($tokenInfo, $appId, $machineId)) {
  211. return true;
  212. }
  213. // @TODO For v4.1 this should not throw an exception, but just return false.
  214. throw new FacebookSDKException(
  215. 'Session has expired, or is not valid for this app.', 601
  216. );
  217. }
  218. /**
  219. * newSessionFromSignedRequest - Returns a FacebookSession for a
  220. * given signed request.
  221. *
  222. * @param SignedRequest $signedRequest
  223. *
  224. * @return FacebookSession
  225. */
  226. public static function newSessionFromSignedRequest(SignedRequest $signedRequest)
  227. {
  228. if ($signedRequest->get('code')
  229. && !$signedRequest->get('oauth_token')) {
  230. return self::newSessionAfterValidation($signedRequest);
  231. }
  232. $accessToken = $signedRequest->get('oauth_token');
  233. $expiresAt = $signedRequest->get('expires', 0);
  234. $accessToken = new AccessToken($accessToken, $expiresAt);
  235. return new static($accessToken, $signedRequest);
  236. }
  237. /**
  238. * newSessionAfterValidation - Returns a FacebookSession for a
  239. * validated & parsed signed request.
  240. *
  241. * @param SignedRequest $signedRequest
  242. *
  243. * @return FacebookSession
  244. */
  245. protected static function newSessionAfterValidation(SignedRequest $signedRequest)
  246. {
  247. $code = $signedRequest->get('code');
  248. $accessToken = AccessToken::getAccessTokenFromCode($code);
  249. return new static($accessToken, $signedRequest);
  250. }
  251. /**
  252. * newAppSession - Returns a FacebookSession configured with a token for the
  253. * application which can be used for publishing and requesting app-level
  254. * information.
  255. *
  256. * @param string|null $appId Application ID to use
  257. * @param string|null $appSecret App secret value to use
  258. *
  259. * @return FacebookSession
  260. */
  261. public static function newAppSession($appId = null, $appSecret = null)
  262. {
  263. $targetAppId = static::_getTargetAppId($appId);
  264. $targetAppSecret = static::_getTargetAppSecret($appSecret);
  265. return new FacebookSession(
  266. $targetAppId . '|' . $targetAppSecret
  267. );
  268. }
  269. /**
  270. * setDefaultApplication - Will set the static default appId and appSecret
  271. * to be used for API requests.
  272. *
  273. * @param string $appId Application ID to use by default
  274. * @param string $appSecret App secret value to use by default
  275. */
  276. public static function setDefaultApplication($appId, $appSecret)
  277. {
  278. self::$defaultAppId = $appId;
  279. self::$defaultAppSecret = $appSecret;
  280. }
  281. /**
  282. * _getTargetAppId - Will return either the provided app Id or the default,
  283. * throwing if neither are populated.
  284. *
  285. * @param string $appId
  286. *
  287. * @return string
  288. *
  289. * @throws FacebookSDKException
  290. */
  291. public static function _getTargetAppId($appId = null) {
  292. $target = ($appId ?: self::$defaultAppId);
  293. if (!$target) {
  294. throw new FacebookSDKException(
  295. 'You must provide or set a default application id.', 700
  296. );
  297. }
  298. return $target;
  299. }
  300. /**
  301. * _getTargetAppSecret - Will return either the provided app secret or the
  302. * default, throwing if neither are populated.
  303. *
  304. * @param string $appSecret
  305. *
  306. * @return string
  307. *
  308. * @throws FacebookSDKException
  309. */
  310. public static function _getTargetAppSecret($appSecret = null) {
  311. $target = ($appSecret ?: self::$defaultAppSecret);
  312. if (!$target) {
  313. throw new FacebookSDKException(
  314. 'You must provide or set a default application secret.', 701
  315. );
  316. }
  317. return $target;
  318. }
  319. /**
  320. * Enable or disable sending the appsecret_proof with requests.
  321. *
  322. * @param bool $on
  323. */
  324. public static function enableAppSecretProof($on = true)
  325. {
  326. static::$useAppSecretProof = ($on ? true : false);
  327. }
  328. /**
  329. * Get whether or not appsecret_proof should be sent with requests.
  330. *
  331. * @return bool
  332. */
  333. public static function useAppSecretProof()
  334. {
  335. return static::$useAppSecretProof;
  336. }
  337. }