class.facebooksdk4.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. <?php
  2. if (!defined('IN_WEB')) {
  3. exit('No direct script access allowed');
  4. }
  5. if (!isset($_SESSION)) {
  6. session_start();
  7. }
  8. /**
  9. * Name: Facebook Login Library
  10. *
  11. * Author: dulu
  12. */
  13. use Facebook\FacebookCanvasLoginHelper;
  14. use Facebook\FacebookRedirectLoginHelper;
  15. use Facebook\FacebookSession;
  16. use Facebook\FacebookRequest;
  17. class facebooksdk4
  18. {
  19. //记录重定向登录请求信息
  20. private $helper;
  21. //记录session
  22. private $session;
  23. //游戏默认授权项
  24. private $facebook_default_scope = array("user_friends","email","public_profile");
  25. /**
  26. * 初始化游戏平台相关信息
  27. *
  28. * @author dulu
  29. *
  30. * @return object Facebookv4_NewApi
  31. */
  32. public function __construct()
  33. {
  34. FacebookSession::setDefaultApplication(oo::$config['facebookAppid'], oo::$config['facebookAppsecret']);
  35. }
  36. /**
  37. * [checkAccessToken 检查AccessToken,设置会话信息]
  38. * @return [type] [description]
  39. */
  40. private function checkAccessToken()
  41. {
  42. if(isset($_SESSION) && isset($_SESSION['w7poker_fb_token']))
  43. {
  44. $this->session = new FacebookSession($_SESSION['w7poker_fb_token']);
  45. }
  46. }
  47. /**
  48. * [setAccessToken 设置token]
  49. * @param [type] $access_token [token]
  50. */
  51. public function setAccessToken($access_token)
  52. {
  53. $_SESSION['w7poker_fb_token'] = $access_token;
  54. $this->session = new FacebookSession($_SESSION['w7poker_fb_token']);
  55. }
  56. /**
  57. * [getAccessToken 获取token]
  58. * @return [type] [description]
  59. */
  60. public function getAccessToken()
  61. {
  62. return isset($_SESSION['w7poker_fb_token']) ? $_SESSION['w7poker_fb_token'] : null;
  63. }
  64. /**
  65. * 获取用户ID
  66. *
  67. * @author dulu
  68. *
  69. * @return [string_type] 平台ID
  70. */
  71. public function getUid()
  72. {
  73. //获取相关FB登录信息
  74. $signedHelper = new FacebookCanvasLoginHelper();
  75. $this->session = $signedHelper->getSession();
  76. //用户会话不存在,重新连接
  77. if (!isset($this->session) || $this->session === null)
  78. {
  79. $this->resetLogin();
  80. }
  81. else
  82. {
  83. $sitemid = $this->session->getUserId();
  84. if(empty($sitemid))
  85. {
  86. $this->resetLogin();
  87. }
  88. else
  89. {
  90. $_SESSION['w7poker_fb_token'] = $this->session->getToken();
  91. return $sitemid;
  92. }
  93. }
  94. }
  95. /**
  96. * [resetLogin 令牌过期,重新登录,生成令牌]
  97. *
  98. * @author dulu
  99. *
  100. * @return [void] 获得access_token信息
  101. */
  102. private function resetLogin()
  103. {
  104. // no session exists
  105. try {
  106. $this->helper = new FacebookRedirectLoginHelper(oo::$config['facebookUrl'] . '?' . http_build_query($_REQUEST));
  107. $this->helper->disableSessionStatusCheck();
  108. $this->session = $this->helper->getSessionFromRedirect();
  109. //重定向刷新获取不到信息,说明用户移除了应用
  110. if(!isset($this->session) || $this->session === null) {
  111. $this->toAuth($this->facebook_default_scope, 1);
  112. } else {
  113. $_SESSION['w7poker_fb_token'] = $this->session->getToken();
  114. }
  115. } catch(FacebookRequestException $ex) {
  116. // When Facebook returns an error
  117. // handle this better in production code
  118. } catch(Exception $ex) {
  119. // When validation fails or other local issues
  120. // handle this better in production code
  121. }
  122. }
  123. /**
  124. * [toAuth 显示授权页面授权]
  125. *
  126. * @author dulu
  127. *
  128. * @param [array_type] [授权列表]
  129. * @param [int_type] [是否注册游戏授权, 默认不是,默认为游戏内部授权]
  130. *
  131. * @return [array_type] [显示授权页面授权]
  132. */
  133. private function toAuth($perms, $type=0)
  134. {
  135. //用户游戏中授权
  136. if(!$type) {
  137. $this->helper = new FacebookRedirectLoginHelper(oo::$config['facebookUrl'] . '?' . http_build_query($_REQUEST));
  138. }
  139. $loginUrl = $this->helper->getLoginUrl($perms);
  140. echo "<script type=\"text/javascript\">top.location.href=\"{$loginUrl}\"</script>";
  141. }
  142. /**
  143. * [getMe 获取用户信息]
  144. *
  145. * @author dulu
  146. *
  147. * @return [array] [用户平台信息]
  148. */
  149. public function getMe($isMobile=0)
  150. {
  151. $this->checkAccessToken();
  152. // if($isMobile == 0)
  153. // {
  154. // $perms = $this->getUserPerms();
  155. // if(!in_array("public_profile", $perms))
  156. // {
  157. // //授权
  158. // $this->toAuth(array("public_profile"));
  159. // }
  160. // }
  161. $graphObject = array();
  162. if(isset($this->session))
  163. {
  164. try {
  165. $path = '/me?fields=id,name,email,first_name,gender,locale,location,birthday,hometown,token_for_business,picture.height(200).width(200)';
  166. // if ( in_array(SGSID,array(1,2)) ){
  167. $path = str_replace(',token_for_business', '', $path);
  168. // }
  169. $request = new FacebookRequest($this->session, 'GET', $path);
  170. $response = $request->execute();
  171. // get response
  172. $graphObject = $response->getGraphObject()->asArray();
  173. } catch(FacebookRequestException $e) {
  174. oo::logs()->debug3(array($e->getMessage(), 'line' => __LINE__), 'fblogin.log');
  175. return array();
  176. } catch(Exception $e) {
  177. oo::logs()->debug3(array($e->getMessage(), 'line' => __LINE__), 'fblogin.log');
  178. return array();
  179. }
  180. }
  181. else
  182. {
  183. $this->resetLogin();
  184. }
  185. if(!empty($graphObject) && isset($graphObject['id']))
  186. {
  187. return $graphObject;
  188. }
  189. return array();
  190. }
  191. /**
  192. * [获取该用户所有平台FBid]
  193. *
  194. * @author dulu
  195. *
  196. * @return [array] [用户各个平台应用信息]
  197. */
  198. public function getIdsForBusiness($type=0)
  199. {
  200. $this->checkAccessToken();
  201. $userInfo = array();
  202. $graphObject = array();
  203. if(isset($this->session))
  204. {
  205. try{
  206. $request = new FacebookRequest($this->session, 'GET', '/me/ids_for_business');
  207. $response = $request->execute();
  208. // get response
  209. $graphObject = $response->getGraphObject()->asArray();
  210. }catch(FacebookRequestException $e){
  211. //oo::logs()->logsUdp("debug",$e->getMessage(), 'fblog');
  212. return array();
  213. }catch(Exception $e){
  214. // oo::logs()->logsUdp("debug",$e->getMessage(), 'fblog');
  215. return array();
  216. }
  217. }
  218. else
  219. {
  220. $this->resetLogin();
  221. }
  222. if(!empty($graphObject) && isset($graphObject['data']))
  223. {
  224. $i = 0;
  225. if ( $type == 1) {
  226. return (array)$graphObject['data'];
  227. } else {
  228. foreach ((array)$graphObject['data'] as $key => $value)
  229. {
  230. if ( $value->app->id == '1468603136785474') {
  231. return $value->id;
  232. }
  233. }
  234. }
  235. }
  236. return '';
  237. }
  238. /**
  239. * 获取非游戏好友列表
  240. *
  241. * @author dulu
  242. *
  243. * @return array
  244. */
  245. public function getNoAppFriendList()
  246. {
  247. $this->checkAccessToken();
  248. $perms = $this->getUserPerms();
  249. if(!in_array("user_friends", $perms))
  250. {
  251. //授权
  252. $this->toAuth(array("user_friends"));
  253. }
  254. $friends = array();
  255. $friendInfo = array();
  256. $i = 0;
  257. if(isset($this->session))
  258. {
  259. //$request = new FacebookRequest($this->session, 'GET', "/me/invitable_friends");
  260. $request = new FacebookRequest($this->session, 'GET', "/me/invitable_friends?field=name,picture");
  261. $response = $request->execute();
  262. // get response
  263. $friends = $response->getGraphObject()->asArray();
  264. }
  265. else
  266. {
  267. $this->resetLogin();
  268. }
  269. if(!empty($friends) && isset($friends['data']) && is_array($friends['data']))
  270. {
  271. foreach ((array)$friends['data'] as $key => $value)
  272. {
  273. $friendInfo[$i] = array($value->id, $value->name, $value->picture);
  274. // $friendInfo[$i]['id'] = $value->id;
  275. // $friendInfo[$i]['name'] = $value->name;
  276. // $friendInfo[$i]['url'] = $value->picture->data->url;
  277. $i++;
  278. }
  279. }
  280. else
  281. {
  282. $friendInfo = array();
  283. }
  284. return $friendInfo;
  285. }
  286. /**
  287. * 获取游戏好友列表
  288. *
  289. * @author dulu
  290. *
  291. * @return array
  292. */
  293. public function getAppFriendList()
  294. {
  295. $this->checkAccessToken();
  296. $perms = $this->getUserPerms();
  297. if(!in_array("user_friends", $perms))
  298. {
  299. //授权
  300. $this->toAuth(array("user_friends"));
  301. }
  302. $friends = array();
  303. $friendInfo = array();
  304. $i = 0;
  305. if(isset($this->session))
  306. {
  307. $request = new FacebookRequest($this->session, 'GET', "/me/friends");
  308. $response = $request->execute();
  309. // get response
  310. $friends = $response->getGraphObject()->asArray();
  311. //var_dump($friends);
  312. }
  313. else
  314. {
  315. $this->resetLogin();
  316. }
  317. if(!empty($friends) && isset($friends['data']) && is_array($friends['data']))
  318. {
  319. foreach ((array)$friends['data'] as $key => $value)
  320. {
  321. $friendInfo[$i] = $value->id;
  322. /*$friendInfo[$i]['id'] = $value->id;
  323. $friendInfo[$i]['name'] = $value->name;*/
  324. $i++;
  325. }
  326. }
  327. else
  328. {
  329. $friendInfo = array();
  330. }
  331. return $friendInfo;
  332. }
  333. /**
  334. * 获取所有好友列表
  335. *
  336. * @author dulu
  337. *
  338. * @return array
  339. */
  340. public function getAllFriendList()
  341. {
  342. $this->checkAccessToken();
  343. $perms = $this->getUserPerms();
  344. if(!in_array("user_friends", $perms))
  345. {
  346. //授权
  347. $this->toAuth(array("user_friends"));
  348. }
  349. $friends = array();
  350. $friendInfo = array();
  351. $i = 0;
  352. if(isset($this->session))
  353. {
  354. $request = new FacebookRequest($this->session, 'GET', "/me/taggable_friends");
  355. $response = $request->execute();
  356. // get response
  357. $friends = $response->getGraphObject()->asArray();
  358. }
  359. else
  360. {
  361. $this->resetLogin();
  362. }
  363. if(!empty($friends) && isset($friends['data']) && is_array($friends['data']))
  364. {
  365. foreach ((array)$friends['data'] as $key => $value)
  366. {
  367. $friendInfo[$i] = $value->id;
  368. // $friendInfo[$i]['name'] = $value->name;
  369. // $friendInfo[$i]['url'] = $value->picture->data->url;
  370. $i++;
  371. }
  372. }
  373. else
  374. {
  375. $friendInfo = array();
  376. }
  377. return $friendInfo;
  378. }
  379. /**
  380. * [getUserPerms 获取用户授权信息]
  381. *
  382. * @author dulu
  383. *
  384. * @return [type] [description]
  385. */
  386. public function getUserPerms()
  387. {
  388. $this->checkAccessToken();
  389. $permissions = array();
  390. $perms = array();
  391. if(isset($this->session))
  392. {
  393. try {
  394. $request = new FacebookRequest($this->session, 'GET', "/me/permissions");
  395. $response = $request->execute();
  396. // get response
  397. $permissions = $response->getGraphObject()->asArray();
  398. } catch(FacebookRequestException $e) {
  399. oo::logs()->logsUdp("debug",$e->getMessage(), 'fblog');
  400. return array();
  401. } catch(Exception $e) {
  402. oo::logs()->logsUdp("debug",$e->getMessage(), 'fblog');
  403. return array();
  404. }
  405. }
  406. else
  407. {
  408. $this->resetLogin();
  409. }
  410. if(!empty($permissions) && is_array($permissions))
  411. {
  412. foreach ($permissions as $key => $value)
  413. {
  414. if($value->status == "granted")
  415. {
  416. array_push($perms, $value->permission);
  417. }
  418. }
  419. }
  420. return $perms;
  421. }
  422. /**
  423. * 发送通知小地球(用这个)
  424. * @from https://developers.facebook.com/docs/games/notifications/ 官方文档
  425. * https://developers.facebook.com/docs/graph-api/reference/v2.2/user/notifications?locale=zh_CN
  426. *
  427. * @author dulu
  428. *
  429. * @param array $uid FB用户ID集合
  430. * @param string $content 内容
  431. * @param string $href 链接地址 &用%26来代替
  432. * @return array
  433. */
  434. public function sendNotifications($uid, $content, $href='?faction=notifx')
  435. {
  436. if(empty($uid))
  437. {
  438. return array();
  439. }
  440. if(!is_array($uid))
  441. {
  442. $uid = array($uid);
  443. }
  444. $appid = oo::$config['facebookAppid'];
  445. $secret = oo::$config['facebookAppsecret'];
  446. $access_token = $appid."|".$secret;
  447. $batchs = array();
  448. $sitemid = $uid[0];
  449. foreach ($uid as $u)
  450. {
  451. $msg = str_replace('{sitemid}', "@[{$u}]", $content);
  452. $batch = array();
  453. $batch['method'] = 'POST';
  454. $batch['relative_url'] = "{$u}/notifications";
  455. $batch['body'] = 'template='.urlencode($msg).'&href='.$href.'%26sent='.date('Ymd');
  456. $batchs[] = $batch;
  457. $sitemid = $u;
  458. }
  459. if(empty($batchs))
  460. {
  461. return array('error'=>'param error.');
  462. }
  463. else
  464. {
  465. $batchs = json_encode($batchs);
  466. }
  467. $ret = array('error'=>'send query exception.');
  468. try
  469. {
  470. $session = new FacebookSession($access_token);
  471. $request = new FacebookRequest($session, 'POST', "/{$sitemid}/notifications", array(
  472. 'access_token' => $access_token,
  473. 'batch' => $batchs,
  474. ));
  475. $response = $request->execute();
  476. // get response
  477. $ret = $response->getGraphObject()->asArray();
  478. }
  479. catch(Exception $e)
  480. {
  481. $ret = array('error'=>$e);
  482. }
  483. if(isset($ret['error']))
  484. {
  485. return $ret;
  486. }
  487. else
  488. {
  489. //统计成功发送的个数
  490. $successNum = 0;
  491. $loseNum = 0;
  492. foreach ($ret as $key => $value)
  493. {
  494. //获取返回值
  495. $info = json_decode($value->body, true);
  496. if(isset($info['success']) && $info['success'] == 'true')
  497. {
  498. //成功发送
  499. $successNum++;
  500. }
  501. else
  502. {
  503. //发送失败
  504. $loseNum = 0;
  505. }
  506. }
  507. return array("success"=>"true", "successNum" => $successNum);
  508. }
  509. }
  510. /**
  511. * 判断用户是否某个页面的like
  512. *
  513. * @author dulu
  514. *
  515. * @param string_type $pageId 页面ID
  516. * @param string_type $uid 平台ID
  517. */
  518. public function isFans($pageId, $uid)
  519. {
  520. $this->checkAccessToken();
  521. $graphObject = array();
  522. if(isset($this->session))
  523. {
  524. $request = new FacebookRequest(
  525. $this->session,
  526. 'GET',
  527. "/{$uid}/likes/{$pageId}"
  528. );
  529. $response = $request->execute();
  530. $graphObject = $response->getGraphObject()->asArray();
  531. }
  532. else
  533. {
  534. $this->resetLogin();
  535. }
  536. //不存在就是用户还没有对此页点缀
  537. if(empty($graphObject))
  538. {
  539. return 0;
  540. }
  541. else
  542. {
  543. return 1;
  544. }
  545. }
  546. /**
  547. * 获得用户本地化货币设置币种
  548. *
  549. * @author dulu
  550. *
  551. * @return string_type $local 本地币种
  552. **/
  553. public function getUserLocalCurrency()
  554. {
  555. $this->checkAccessToken();
  556. $local = "USD"; //默认值为USD取不到的时候
  557. $graphObject = array();
  558. if(isset($this->session))
  559. {
  560. $request = new FacebookRequest(
  561. $this->session,
  562. 'GET',
  563. "/me?fields=currency"
  564. );
  565. $response = $request->execute();
  566. $graphObject = $response->getGraphObject()->asArray();
  567. }
  568. else
  569. {
  570. $this->resetLogin();
  571. }
  572. if(!empty($graphObject))
  573. {
  574. $local = $graphObject['currency']->user_currency;
  575. }
  576. return $local;
  577. }
  578. /**
  579. * 上传一张图片(URL)
  580. *
  581. * @author dulu
  582. *
  583. * @param string $file 图片在服务器的文件绝对地址
  584. * @param string $picMessage 图片的说明
  585. */
  586. public function uploadPictureByUrl($file, $picMessage='good')
  587. {
  588. $this->checkAccessToken();
  589. //创建相簿
  590. $graphObject = array();
  591. if(isset($this->session))
  592. {
  593. $request = new FacebookRequest($this->session, 'POST', '/me/photos', array(
  594. 'url' => $file,
  595. 'message' => $picMessage,
  596. 'value' => 'EVERYONE'
  597. ));
  598. $response = $request->execute();
  599. $graphObject = $response->getGraphObject()->asArray();
  600. }
  601. else
  602. {
  603. $this->resetLogin();
  604. }
  605. return $graphObject;
  606. }
  607. /**
  608. * 上传一张图片(Source)
  609. *
  610. * @author dulu
  611. *
  612. * @param string $file 获取通过文件上传照片multipart/form-data数据然后使用源参数
  613. * @param string $message 照片的说明
  614. */
  615. public function uploadPictureBySource($file, $picMessage='good')
  616. {
  617. $this->checkAccessToken();
  618. //创建相簿
  619. $graphObject = array();
  620. if(isset($this->session))
  621. {
  622. $request = new FacebookRequest($this->session, 'POST', '/me/photos', array(
  623. 'url' => $file,
  624. 'message' => $picMessage,
  625. 'value' => 'EVERYONE'
  626. ));
  627. $response = $request->execute();
  628. $graphObject = $response->getGraphObject()->asArray();
  629. }
  630. else
  631. {
  632. $this->resetLogin();
  633. }
  634. return $graphObject;
  635. }
  636. /**
  637. * 创建一个指定名称的相册, 如果存在就直接返回相簿ID
  638. *
  639. * @author dulu
  640. *
  641. * @param string $name 相册名称
  642. * @param string $message 相册说明
  643. *
  644. * @return string 相簿ID
  645. */
  646. public function createAlbum($name, $message='')
  647. {
  648. $this->checkAccessToken();
  649. //检查是否已经存在此相簿,避免重复创建同名称相簿
  650. $albumInfo = $this->getAlbum();
  651. foreach ($albumInfo as $key => $value)
  652. {
  653. if($name == $value['name'])
  654. {
  655. return $value['id'];
  656. }
  657. }
  658. //创建相簿
  659. $graphObject = array();
  660. if(isset($this->session))
  661. {
  662. $request = new FacebookRequest($this->session, 'POST', '/me/albums', array(
  663. 'name' => $name,
  664. 'message' => $message,
  665. 'value' => 'EVERYONE'
  666. ));
  667. $response = $request->execute();
  668. $graphObject = $response->getGraphObject()->asArray();
  669. }
  670. else
  671. {
  672. $this->resetLogin();
  673. }
  674. //是否创建成功,成功返回相簿ID
  675. if(!empty($graphObject) && is_array($graphObject) && isset($graphObject['id']))
  676. {
  677. return $graphObject['id'];
  678. }
  679. else
  680. {
  681. return 0;
  682. }
  683. }
  684. /**
  685. * [getAlbum 获得用户所有相簿信息]
  686. *
  687. * @author dulu
  688. *
  689. * @return [array] [相簿信息(名称,ID)]
  690. */
  691. public function getAlbum()
  692. {
  693. $this->checkAccessToken();
  694. $graphObject = array();
  695. $photosInfo = array();
  696. if(isset($this->session))
  697. {
  698. $request = new FacebookRequest($this->session, 'GET', '/me/albums');
  699. $response = $request->execute();
  700. $graphObject = $response->getGraphObject()->asArray();
  701. }
  702. else
  703. {
  704. $this->resetLogin();
  705. }
  706. if(!empty($graphObject) && is_array($graphObject['data']) && isset($graphObject['data']))
  707. {
  708. foreach ($graphObject['data'] as $key => $value)
  709. {
  710. $photosInfo[$key]['name'] = $value->name;
  711. $photosInfo[$key]['id'] = $value->id;
  712. }
  713. }
  714. return $photosInfo;
  715. }
  716. }