PageRenderTime 112ms CodeModel.GetById 6ms RepoModel.GetById 5ms app.codeStats 0ms

/wp-content/plugins/premium-seo-pack/lib/scripts/facebook-v4-5.0.0/FacebookRequest.php

https://gitlab.com/iamgraeme/royalmile
PHP | 536 lines | 226 code | 65 blank | 245 comment | 14 complexity | df1404d1995faa2e806f51684301b401 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\Authentication\AccessToken;
  26. use Facebook\Url\FacebookUrlManipulator;
  27. use Facebook\FileUpload\FacebookFile;
  28. use Facebook\FileUpload\FacebookVideo;
  29. use Facebook\Http\RequestBodyMultipart;
  30. use Facebook\Http\RequestBodyUrlEncoded;
  31. use Facebook\Exceptions\FacebookSDKException;
  32. /**
  33. * Class Request
  34. *
  35. * @package Facebook
  36. */
  37. class FacebookRequest
  38. {
  39. /**
  40. * @var FacebookApp The Facebook app entity.
  41. */
  42. protected $app;
  43. /**
  44. * @var string|null The access token to use for this request.
  45. */
  46. protected $accessToken;
  47. /**
  48. * @var string The HTTP method for this request.
  49. */
  50. protected $method;
  51. /**
  52. * @var string The Graph endpoint for this request.
  53. */
  54. protected $endpoint;
  55. /**
  56. * @var array The headers to send with this request.
  57. */
  58. protected $headers = [];
  59. /**
  60. * @var array The parameters to send with this request.
  61. */
  62. protected $params = [];
  63. /**
  64. * @var array The files to send with this request.
  65. */
  66. protected $files = [];
  67. /**
  68. * @var string ETag to send with this request.
  69. */
  70. protected $eTag;
  71. /**
  72. * @var string Graph version to use for this request.
  73. */
  74. protected $graphVersion;
  75. /**
  76. * Creates a new Request entity.
  77. *
  78. * @param FacebookApp|null $app
  79. * @param AccessToken|string|null $accessToken
  80. * @param string|null $method
  81. * @param string|null $endpoint
  82. * @param array|null $params
  83. * @param string|null $eTag
  84. * @param string|null $graphVersion
  85. */
  86. public function __construct(FacebookApp $app = null, $accessToken = null, $method = null, $endpoint = null, array $params = [], $eTag = null, $graphVersion = null)
  87. {
  88. $this->setApp($app);
  89. $this->setAccessToken($accessToken);
  90. $this->setMethod($method);
  91. $this->setEndpoint($endpoint);
  92. $this->setParams($params);
  93. $this->setETag($eTag);
  94. $this->graphVersion = $graphVersion ?: Facebook::DEFAULT_GRAPH_VERSION;
  95. }
  96. /**
  97. * Set the access token for this request.
  98. *
  99. * @param AccessToken|string
  100. *
  101. * @return FacebookRequest
  102. */
  103. public function setAccessToken($accessToken)
  104. {
  105. $this->accessToken = $accessToken;
  106. if ($accessToken instanceof AccessToken) {
  107. $this->accessToken = $accessToken->getValue();
  108. }
  109. return $this;
  110. }
  111. /**
  112. * Sets the access token with one harvested from a URL or POST params.
  113. *
  114. * @param string $accessToken The access token.
  115. *
  116. * @return FacebookRequest
  117. *
  118. * @throws FacebookSDKException
  119. */
  120. public function setAccessTokenFromParams($accessToken)
  121. {
  122. $existingAccessToken = $this->getAccessToken();
  123. if (!$existingAccessToken) {
  124. $this->setAccessToken($accessToken);
  125. } elseif ($accessToken !== $existingAccessToken) {
  126. throw new FacebookSDKException('Access token mismatch. The access token provided in the FacebookRequest and the one provided in the URL or POST params do not match.');
  127. }
  128. return $this;
  129. }
  130. /**
  131. * Return the access token for this request.
  132. *
  133. * @return string|null
  134. */
  135. public function getAccessToken()
  136. {
  137. return $this->accessToken;
  138. }
  139. /**
  140. * Return the access token for this request an an AccessToken entity.
  141. *
  142. * @return AccessToken|null
  143. */
  144. public function getAccessTokenEntity()
  145. {
  146. return $this->accessToken ? new AccessToken($this->accessToken) : null;
  147. }
  148. /**
  149. * Set the FacebookApp entity used for this request.
  150. *
  151. * @param FacebookApp|null $app
  152. */
  153. public function setApp(FacebookApp $app = null)
  154. {
  155. $this->app = $app;
  156. }
  157. /**
  158. * Return the FacebookApp entity used for this request.
  159. *
  160. * @return FacebookApp
  161. */
  162. public function getApp()
  163. {
  164. return $this->app;
  165. }
  166. /**
  167. * Generate an app secret proof to sign this request.
  168. *
  169. * @return string|null
  170. */
  171. public function getAppSecretProof()
  172. {
  173. if (!$accessTokenEntity = $this->getAccessTokenEntity()) {
  174. return null;
  175. }
  176. return $accessTokenEntity->getAppSecretProof($this->app->getSecret());
  177. }
  178. /**
  179. * Validate that an access token exists for this request.
  180. *
  181. * @throws FacebookSDKException
  182. */
  183. public function validateAccessToken()
  184. {
  185. $accessToken = $this->getAccessToken();
  186. if (!$accessToken) {
  187. throw new FacebookSDKException('You must provide an access token.');
  188. }
  189. }
  190. /**
  191. * Set the HTTP method for this request.
  192. *
  193. * @param string
  194. *
  195. * @return FacebookRequest
  196. */
  197. public function setMethod($method)
  198. {
  199. $this->method = strtoupper($method);
  200. }
  201. /**
  202. * Return the HTTP method for this request.
  203. *
  204. * @return string
  205. */
  206. public function getMethod()
  207. {
  208. return $this->method;
  209. }
  210. /**
  211. * Validate that the HTTP method is set.
  212. *
  213. * @throws FacebookSDKException
  214. */
  215. public function validateMethod()
  216. {
  217. if (!$this->method) {
  218. throw new FacebookSDKException('HTTP method not specified.');
  219. }
  220. if (!in_array($this->method, ['GET', 'POST', 'DELETE'])) {
  221. throw new FacebookSDKException('Invalid HTTP method specified.');
  222. }
  223. }
  224. /**
  225. * Set the endpoint for this request.
  226. *
  227. * @param string
  228. *
  229. * @return FacebookRequest
  230. *
  231. * @throws FacebookSDKException
  232. */
  233. public function setEndpoint($endpoint)
  234. {
  235. // Harvest the access token from the endpoint to keep things in sync
  236. $params = FacebookUrlManipulator::getParamsAsArray($endpoint);
  237. if (isset($params['access_token'])) {
  238. $this->setAccessTokenFromParams($params['access_token']);
  239. }
  240. // Clean the token & app secret proof from the endpoint.
  241. $filterParams = ['access_token', 'appsecret_proof'];
  242. $this->endpoint = FacebookUrlManipulator::removeParamsFromUrl($endpoint, $filterParams);
  243. return $this;
  244. }
  245. /**
  246. * Return the HTTP method for this request.
  247. *
  248. * @return string
  249. */
  250. public function getEndpoint()
  251. {
  252. // For batch requests, this will be empty
  253. return $this->endpoint;
  254. }
  255. /**
  256. * Generate and return the headers for this request.
  257. *
  258. * @return array
  259. */
  260. public function getHeaders()
  261. {
  262. $headers = static::getDefaultHeaders();
  263. if ($this->eTag) {
  264. $headers['If-None-Match'] = $this->eTag;
  265. }
  266. return array_merge($this->headers, $headers);
  267. }
  268. /**
  269. * Set the headers for this request.
  270. *
  271. * @param array $headers
  272. */
  273. public function setHeaders(array $headers)
  274. {
  275. $this->headers = array_merge($this->headers, $headers);
  276. }
  277. /**
  278. * Sets the eTag value.
  279. *
  280. * @param string $eTag
  281. */
  282. public function setETag($eTag)
  283. {
  284. $this->eTag = $eTag;
  285. }
  286. /**
  287. * Set the params for this request.
  288. *
  289. * @param array $params
  290. *
  291. * @return FacebookRequest
  292. *
  293. * @throws FacebookSDKException
  294. */
  295. public function setParams(array $params = [])
  296. {
  297. if (isset($params['access_token'])) {
  298. $this->setAccessTokenFromParams($params['access_token']);
  299. }
  300. // Don't let these buggers slip in.
  301. unset($params['access_token'], $params['appsecret_proof']);
  302. // @TODO Refactor code above with this
  303. //$params = $this->sanitizeAuthenticationParams($params);
  304. $params = $this->sanitizeFileParams($params);
  305. $this->dangerouslySetParams($params);
  306. return $this;
  307. }
  308. /**
  309. * Set the params for this request without filtering them first.
  310. *
  311. * @param array $params
  312. *
  313. * @return FacebookRequest
  314. */
  315. public function dangerouslySetParams(array $params = [])
  316. {
  317. $this->params = array_merge($this->params, $params);
  318. return $this;
  319. }
  320. /**
  321. * Iterate over the params and pull out the file uploads.
  322. *
  323. * @param array $params
  324. *
  325. * @return array
  326. */
  327. public function sanitizeFileParams(array $params)
  328. {
  329. foreach ($params as $key => $value) {
  330. if ($value instanceof FacebookFile) {
  331. $this->addFile($key, $value);
  332. unset($params[$key]);
  333. }
  334. }
  335. return $params;
  336. }
  337. /**
  338. * Add a file to be uploaded.
  339. *
  340. * @param string $key
  341. * @param FacebookFile $file
  342. */
  343. public function addFile($key, FacebookFile $file)
  344. {
  345. $this->files[$key] = $file;
  346. }
  347. /**
  348. * Removes all the files from the upload queue.
  349. */
  350. public function resetFiles()
  351. {
  352. $this->files = [];
  353. }
  354. /**
  355. * Get the list of files to be uploaded.
  356. *
  357. * @return array
  358. */
  359. public function getFiles()
  360. {
  361. return $this->files;
  362. }
  363. /**
  364. * Let's us know if there is a file upload with this request.
  365. *
  366. * @return boolean
  367. */
  368. public function containsFileUploads()
  369. {
  370. return !empty($this->files);
  371. }
  372. /**
  373. * Let's us know if there is a video upload with this request.
  374. *
  375. * @return boolean
  376. */
  377. public function containsVideoUploads()
  378. {
  379. foreach ($this->files as $file) {
  380. if ($file instanceof FacebookVideo) {
  381. return true;
  382. }
  383. }
  384. return false;
  385. }
  386. /**
  387. * Returns the body of the request as multipart/form-data.
  388. *
  389. * @return RequestBodyMultipart
  390. */
  391. public function getMultipartBody()
  392. {
  393. $params = $this->getPostParams();
  394. return new RequestBodyMultipart($params, $this->files);
  395. }
  396. /**
  397. * Returns the body of the request as URL-encoded.
  398. *
  399. * @return RequestBodyUrlEncoded
  400. */
  401. public function getUrlEncodedBody()
  402. {
  403. $params = $this->getPostParams();
  404. return new RequestBodyUrlEncoded($params);
  405. }
  406. /**
  407. * Generate and return the params for this request.
  408. *
  409. * @return array
  410. */
  411. public function getParams()
  412. {
  413. $params = $this->params;
  414. $accessToken = $this->getAccessToken();
  415. if ($accessToken) {
  416. $params['access_token'] = $accessToken;
  417. $params['appsecret_proof'] = $this->getAppSecretProof();
  418. }
  419. return $params;
  420. }
  421. /**
  422. * Only return params on POST requests.
  423. *
  424. * @return array
  425. */
  426. public function getPostParams()
  427. {
  428. if ($this->getMethod() === 'POST') {
  429. return $this->getParams();
  430. }
  431. return [];
  432. }
  433. /**
  434. * The graph version used for this request.
  435. *
  436. * @return string
  437. */
  438. public function getGraphVersion()
  439. {
  440. return $this->graphVersion;
  441. }
  442. /**
  443. * Generate and return the URL for this request.
  444. *
  445. * @return string
  446. */
  447. public function getUrl()
  448. {
  449. $this->validateMethod();
  450. $graphVersion = FacebookUrlManipulator::forceSlashPrefix($this->graphVersion);
  451. $endpoint = FacebookUrlManipulator::forceSlashPrefix($this->getEndpoint());
  452. $url = $graphVersion . $endpoint;
  453. if ($this->getMethod() !== 'POST') {
  454. $params = $this->getParams();
  455. $url = FacebookUrlManipulator::appendParamsToUrl($url, $params);
  456. }
  457. return $url;
  458. }
  459. /**
  460. * Return the default headers that every request should use.
  461. *
  462. * @return array
  463. */
  464. public static function getDefaultHeaders()
  465. {
  466. return [
  467. 'User-Agent' => 'fb-php-' . Facebook::VERSION,
  468. 'Accept-Encoding' => '*',
  469. ];
  470. }
  471. }