BxFaceBookConnectModule.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. /***************************************************************************
  3. * Dolphin Smart Community Builder
  4. * -------------------
  5. * begin : Mon Mar 23 2006
  6. * copyright : (C) 2007 BoonEx Group
  7. * website : http://www.boonex.com
  8. * This file is part of Dolphin - Smart Community Builder
  9. *
  10. * Dolphin is free software; you can redistribute it and/or modify it under
  11. * the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the
  13. * License, or any later version.
  14. *
  15. * Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  16. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. * See the GNU General Public License for more details.
  18. * You should have received a copy of the GNU General Public License along with Dolphin,
  19. * see license.txt file; if not, write to marketing@boonex.com
  20. ***************************************************************************/
  21. require_once( BX_DIRECTORY_PATH_INC . 'profiles.inc.php' );
  22. bx_import('BxDolModuleDb');
  23. bx_import('BxDolModule');
  24. bx_import('BxDolInstallerUtils');
  25. bx_import('BxDolProfilesController');
  26. bx_import('BxDolAlerts');
  27. class BxFaceBookConnectModule extends BxDolModule
  28. {
  29. // contain some module information ;
  30. var $aModuleInfo;
  31. // contain path for current module;
  32. var $sPathToModule;
  33. var $sHomeUrl;
  34. var $oFacebook;
  35. /**
  36. * Class constructor ;
  37. *
  38. * @param : $aModule (array) - contain some information about this module;
  39. * [ id ] - (integer) module's id ;
  40. * [ title ] - (string) module's title ;
  41. * [ vendor ] - (string) module's vendor ;
  42. * [ path ] - (string) path to this module ;
  43. * [ uri ] - (string) this module's URI ;
  44. * [ class_prefix ] - (string) this module's php classes file prefix ;
  45. * [ db_prefix ] - (string) this module's Db tables prefix ;
  46. * [ date ] - (string) this module's date installation ;
  47. */
  48. function BxFaceBookConnectModule(&$aModule)
  49. {
  50. parent::BxDolModule($aModule);
  51. require_once(BX_DIRECTORY_PATH_MODULES . $aModule['path'] . '/inc/facebook.php');
  52. // prepare the location link ;
  53. $this -> sPathToModule = BX_DOL_URL_ROOT . $this -> _oConfig -> getBaseUri();
  54. $this -> aModuleInfo = $aModule;
  55. $this -> sHomeUrl = $this ->_oConfig -> _sHomeUrl;
  56. // Create our Application instance.
  57. $this -> oFacebook = new Facebook(array(
  58. 'appId' => $this -> _oConfig -> mApiID,
  59. 'secret' => $this -> _oConfig -> mApiSecret,
  60. 'cookie' => true,
  61. ));
  62. }
  63. /**
  64. * Function will generate facebook's admin page;
  65. *
  66. * @return : (text) - html presentation data;
  67. */
  68. function actionAdministration()
  69. {
  70. $GLOBALS['iAdminPage'] = 1;
  71. if( !isAdmin() ) {
  72. header('location: ' . BX_DOL_URL_ROOT);
  73. }
  74. // get sys_option's category id;
  75. $iCatId = $this-> _oDb -> getSettingsCategoryId('bx_facebook_connect_api_key');
  76. if(!$iCatId) {
  77. $sOptions = MsgBox( _t('_Empty') );
  78. }
  79. else {
  80. bx_import('BxDolAdminSettings');
  81. $oSettings = new BxDolAdminSettings($iCatId);
  82. $mixedResult = '';
  83. if(isset($_POST['save']) && isset($_POST['cat'])) {
  84. $mixedResult = $oSettings -> saveChanges($_POST);
  85. }
  86. // get option's form;
  87. $sOptions = $oSettings -> getForm();
  88. if($mixedResult !== true && !empty($mixedResult)) {
  89. $sOptions = $mixedResult . $sOptions;
  90. }
  91. }
  92. $sCssStyles = $this -> _oTemplate -> addCss('forms_adv.css', true);
  93. $this -> _oTemplate-> pageCodeAdminStart();
  94. echo DesignBoxAdmin( _t('_bx_facebook_information')
  95. , $GLOBALS['oSysTemplate'] -> parseHtmlByName('default_padding.html', array('content' => _t('_bx_facebook_information_block', BX_DOL_URL_ROOT))) );
  96. echo DesignBoxAdmin( _t('_Settings')
  97. , $GLOBALS['oSysTemplate'] -> parseHtmlByName('default_padding.html', array('content' => $sCssStyles . $sOptions) ));
  98. $this -> _oTemplate->pageCodeAdmin( _t('_bx_facebook_settings') );
  99. }
  100. /**
  101. * Generare facebook login form;
  102. *
  103. * @return (text) - html presentation data;
  104. */
  105. function actionLoginForm()
  106. {
  107. $sCode = '';
  108. if( isLogged() ) {
  109. header ('Location:' . $this -> _oConfig -> sDefaultRedirectUrl);
  110. exit;
  111. }
  112. if(!$this -> _oConfig -> mApiID || !$this -> _oConfig -> mApiSecret) {
  113. $sCode = MsgBox( _t('_bx_facebook_profile_error_api_keys') );
  114. }
  115. else {
  116. $sSession = $this -> oFacebook -> getSession();
  117. //redirect to facebook login form
  118. if(!$sSession) {
  119. //step one
  120. header('location: ' . $this -> oFacebook -> getLoginUrl($this
  121. -> _oConfig -> aFaceBookReqParams));
  122. exit;
  123. }
  124. else {
  125. //we already logged in facebook
  126. try {
  127. $aFacebookProfileInfo = $this -> oFacebook -> api('/me');
  128. } catch (FacebookApiException $e) {
  129. $sCode = MsgBox($e);
  130. }
  131. //process profile info
  132. if($aFacebookProfileInfo) {
  133. //-- nedded for old auth method (will need remove it in a feature version) --//
  134. $sOldFacebookUid = md5($aFacebookProfileInfo['proxied_email']
  135. . $aFacebookProfileInfo['first_name']);
  136. //--
  137. // try define user id
  138. $iProfileId = $this -> _oDb
  139. -> getProfileId($aFacebookProfileInfo['id'], $sOldFacebookUid);
  140. if($iProfileId) {
  141. // get profile info
  142. $aDolphinProfileInfo = getProfileInfo($iProfileId);
  143. $this -> setLogged($iProfileId, $aDolphinProfileInfo['Password']);
  144. }
  145. else {
  146. $sAlternativeNickName = '';
  147. //process profile's nickname
  148. $aFacebookProfileInfo['nick_name'] = $this
  149. -> _proccesNickName($aFacebookProfileInfo['first_name']);
  150. //-- profile nickname already used by other person --//
  151. if( getID($aFacebookProfileInfo['nick_name']) ) {
  152. $sAlternativeNickName = $this
  153. -> getAlternativeName($aFacebookProfileInfo['nick_name']);
  154. }
  155. //--
  156. //try to get profile's image
  157. if( NULL != ($aFacebookProfileImage = $this
  158. -> oFacebook -> api('/me?fields=picture&type=large')) ) {
  159. $aFacebookProfileInfo['picture'] = isset($aFacebookProfileImage['picture'])
  160. ? $aFacebookProfileImage['picture']
  161. : '';
  162. }
  163. //create new profile
  164. $this -> _createProfile($aFacebookProfileInfo, $sAlternativeNickName);
  165. }
  166. }
  167. else {
  168. // FB profile info is not defined;
  169. $sCode = MsgBox( _t('_bx_facebook_profile_error_info') );
  170. }
  171. }
  172. }
  173. $this -> _oTemplate -> getPage( _t('_bx_facebook'), $sCode );
  174. }
  175. /**
  176. * Logged profile
  177. *
  178. * @param $iProfileId integer
  179. * @param $sPassword string
  180. * @param $sCallbackUrl
  181. * @param $bRedirect boolean
  182. * @return void
  183. */
  184. function setLogged($iProfileId, $sPassword, $sCallbackUrl = '', $bRedirect = true)
  185. {
  186. bx_login($iProfileId);
  187. $GLOBALS['logged']['member'] = true;
  188. if($bRedirect) {
  189. $sCallbackUrl = $sCallbackUrl
  190. ? $sCallbackUrl
  191. : $this -> _oConfig -> sDefaultRedirectUrl;
  192. header('location: ' . $sCallbackUrl);
  193. }
  194. }
  195. /**
  196. * get profile's alternative nickname
  197. *
  198. * @param $sNickName string
  199. * @return string
  200. */
  201. function getAlternativeName($sNickName)
  202. {
  203. $sRetNickName = '';
  204. $iIndex = 0;
  205. //-- get new allternative nickname --//
  206. do
  207. {
  208. $sPostfix = $iIndex
  209. ? $this -> _oConfig -> sFaceBookAlternativePostfix . $iIndex
  210. : $this -> _oConfig -> sFaceBookAlternativePostfix;
  211. if( !getID($sNickName . $sPostfix) ) {
  212. $sRetNickName = $sPostfix;
  213. }
  214. $iIndex++;
  215. } while ($sRetNickName == '');
  216. //--
  217. return $sRetNickName;
  218. }
  219. /**
  220. * Assign avatar to user
  221. *
  222. * @param $sAvatarUrl string
  223. * @return void
  224. */
  225. function _assignAvatar($sAvatarUrl)
  226. {
  227. if( BxDolInstallerUtils::isModuleInstalled('avatar') ) {
  228. BxDolService::call ('avatar', 'make_avatar_from_image_url', array($sAvatarUrl));
  229. }
  230. }
  231. /**
  232. * Make friends
  233. *
  234. * @param $iProfileId integer
  235. * @return void
  236. */
  237. function _makeFriends($iProfileId)
  238. {
  239. if(!$this -> _oConfig -> bAutoFriends) {
  240. return;
  241. }
  242. try {
  243. //get friends from facebook
  244. $aFacebookFriends = $this -> oFacebook -> api('/me/friends/');
  245. } catch (FacebookApiException $e) {
  246. return;
  247. }
  248. //process friends
  249. if( !empty($aFacebookFriends) && is_array($aFacebookFriends) ) {
  250. $aFacebookFriends = array_shift($aFacebookFriends);
  251. foreach($aFacebookFriends as $iKey => $aFriend)
  252. {
  253. $iFriendId = $this -> _oDb -> getProfileId($aFriend['id']);
  254. if($iFriendId && !is_friends($iProfileId, $iFriendId) ) {
  255. //add to friends list
  256. $this -> _oDb -> makeFriend($iProfileId, $iFriendId);
  257. //create system alert
  258. $oZ = new BxDolAlerts('friend', 'accept', $iProfileId, $iFriendId);
  259. $oZ -> alert();
  260. }
  261. }
  262. }
  263. }
  264. /**
  265. * Create new profile;
  266. *
  267. * @param : $aProfileInfo (array) - some profile's information;
  268. * @see : $this -> aFacebookProfileFields;
  269. *
  270. * @param : $sAlternativeName (string) - profiles alternative nickname;
  271. * @return : (integer) profile's id;
  272. */
  273. function _createProfile($aProfileInfo, $sAlternativeName = '')
  274. {
  275. $sCountry = '';
  276. $sCity = '';
  277. $bAvatarRedirect = false;
  278. $sMemberAvatar = !empty($aProfileInfo['picture'])
  279. ? $aProfileInfo['picture']
  280. : '';
  281. //-- join by invite only --//
  282. if( getParam('reg_by_inv_only') == 'on'
  283. && (!isset($_COOKIE['idFriend']) || getID($_COOKIE['idFriend']) == 0) ){
  284. $this -> _oTemplate -> getPage( _t('_bx_facebook')
  285. , MsgBox(_t('_registration by invitation only')));
  286. exit;
  287. }
  288. //--
  289. // process the date of birth;
  290. if( isset($aProfileInfo['birthday']) ) {
  291. $aProfileInfo['birthday'] = isset($aProfileInfo['birthday'])
  292. ? date('Y-m-d', strtotime($aProfileInfo['birthday']) )
  293. : '';
  294. }
  295. // generate new password for profile;
  296. $sNewPassword = genRndPwd();
  297. $sPasswordSalt = genRndSalt();
  298. $aProfileInfo['password'] = encryptUserPwd($sNewPassword,$sPasswordSalt);
  299. //-- define user's country and city --//
  300. $aLocation = array();
  301. if( isset($aProfileInfo['location']['name']) ) {
  302. $aLocation = $aProfileInfo['location']['name'];
  303. }
  304. else if( isset($aProfileInfo['hometown']['name']) ) {
  305. $aLocation = $aProfileInfo['hometown']['name'];
  306. }
  307. if($aLocation) {
  308. $aCountryInfo = explode(',', $aLocation);
  309. $sCountry = $this -> _oDb -> getCountryCode( trim($aCountryInfo[1]) );
  310. $sCity = trim($aCountryInfo[0]);
  311. //set default country name, especially for American brothers
  312. if($sCity && !$sCountry) {
  313. $sCountry = $this -> _oConfig -> sDefaultCountryCode;
  314. }
  315. }
  316. //--
  317. //try define the user's email
  318. $sEmail = !empty($aProfileInfo['email'])
  319. ? $aProfileInfo['email']
  320. : $aProfileInfo['proxied_email'];
  321. //check email
  322. if( $this -> _oDb -> isEmailExisting($sEmail) ) {
  323. $this -> _oTemplate -> getPage( _t('_bx_facebook'), MsgBox(_t('_bx_facebook_error_email')));
  324. exit;
  325. }
  326. //-- fill array with all needed values --//
  327. $aProfileFields = array(
  328. 'NickName' => $aProfileInfo['nick_name'] . $sAlternativeName,
  329. 'Email' => $sEmail,
  330. 'Sex' => isset($aProfileInfo['gender']) ? $aProfileInfo['gender'] : '',
  331. 'DateOfBirth' => $aProfileInfo['birthday'],
  332. 'Password' => $aProfileInfo['password'],
  333. 'FirstName' => isset($aProfileInfo['first_name']) ? $aProfileInfo['first_name'] : '',
  334. 'LastName' => isset($aProfileInfo['last_name']) ? $aProfileInfo['last_name'] : '',
  335. 'DescriptionMe' => isset($aProfileInfo['bio']) ? $aProfileInfo['bio'] : '',
  336. 'Interests' => isset($aProfileInfo['interests']) ? $aProfileInfo['interests'] : '',
  337. 'Religion' => isset($aProfileInfo['religion']) ? $aProfileInfo['religion'] : '',
  338. 'Country' => $sCountry,
  339. 'City' => $sCity,
  340. );
  341. //--
  342. // check fields existence;
  343. foreach($aProfileFields as $sKey => $mValue) {
  344. if( !$this -> _oDb -> isFieldExist($sKey) ) {
  345. // (field not existence) remove from array;
  346. unset($aProfileFields[$sKey]);
  347. }
  348. }
  349. //-- add some system values --//
  350. $aProfileFields['Role'] = BX_DOL_ROLE_MEMBER;
  351. $aProfileFields['DateReg'] = date( 'Y-m-d H:i:s' ); // set current date;
  352. $aProfileFields['Salt'] = $sPasswordSalt;
  353. //--
  354. //check redirect page
  355. switch($this -> _oConfig -> sRedirectPage) {
  356. case 'join' :
  357. return $this -> _getJoinPage($aProfileFields, $aProfileInfo['id']);
  358. case 'pedit' :
  359. $sRedirectUrl = BX_DOL_URL_ROOT . 'pedit.php';
  360. break;
  361. case 'avatar' :
  362. $bAvatarRedirect = true;
  363. break;
  364. case 'index' :
  365. $sRedirectUrl = BX_DOL_URL_ROOT;
  366. break;
  367. case 'member' :
  368. default :
  369. $sRedirectUrl = BX_DOL_URL_ROOT . 'member.php';
  370. break;
  371. }
  372. // create new profile;
  373. $iProfileId = $this -> _oDb -> createProfile($aProfileFields);
  374. $oProfileFields = new BxDolProfilesController();
  375. //remember FB uid for created member
  376. $this -> _oDb -> saveFbUid($iProfileId, $aProfileInfo['id']);
  377. // check profile status;
  378. if ( getParam('autoApproval_ifNoConfEmail') == 'on' ) {
  379. if ( getParam('autoApproval_ifJoin') == 'on' ) {
  380. $sProfileStatus = 'Active';
  381. if( !empty($aProfileInfo['email']) ) {
  382. $oProfileFields -> sendActivationMail($iProfileId);
  383. }
  384. }
  385. else {
  386. $sProfileStatus = 'Approval';
  387. if( !empty($aProfileInfo['email']) ) {
  388. $oProfileFields -> sendApprovalMail($iProfileId);
  389. }
  390. }
  391. }
  392. else {
  393. if( !empty($aProfileInfo['email']) ) {
  394. $oProfileFields -> sendConfMail($iProfileId);
  395. $sProfileStatus = 'Unconfirmed';
  396. }
  397. else {
  398. if ( getParam('autoApproval_ifJoin') == 'on' ) {
  399. $sProfileStatus = 'Active';
  400. }
  401. else {
  402. $sProfileStatus = 'Approval';
  403. }
  404. }
  405. }
  406. // update profile's status;
  407. $this -> _oDb -> updateProfileStatus($iProfileId, $sProfileStatus);
  408. $oProfileFields -> createProfileCache($iProfileId);
  409. if( !empty($aProfileInfo['email']) ) {
  410. //-- send email notification --//
  411. $oEmailTemplate = new BxDolEmailTemplates();
  412. $aTemplate = $oEmailTemplate -> getTemplate('t_fb_connect_password_generated') ;
  413. $aNewProfileInfo = getProfileInfo($iProfileId);
  414. $aPlus = array(
  415. 'NickName' => $aNewProfileInfo['NickName'],
  416. 'NewPassword' => $sNewPassword,
  417. );
  418. sendMail( $aNewProfileInfo['Email'], $aTemplate['Subject']
  419. , $aTemplate['Body'], '', $aPlus );
  420. }
  421. //--
  422. // create system event
  423. $oZ = new BxDolAlerts('profile', 'join', $iProfileId);
  424. $oZ -> alert();
  425. //Auto-friend members if they are already friends on Facebook
  426. $this -> _makeFriends($iProfileId);
  427. // set logged
  428. $aProfileInfo = getProfileInfo($iProfileId);
  429. $this -> setLogged($iProfileId, $aProfileInfo['Password'], '', false);
  430. //redirect to avatar page
  431. if($bAvatarRedirect) {
  432. if( BxDolInstallerUtils::isModuleInstalled('avatar') ) {
  433. // check profile's logo;
  434. if($sMemberAvatar) {
  435. BxDolService::call('avatar', 'set_image_for_cropping', array ($iProfileId, $sMemberAvatar));
  436. }
  437. if (BxDolService::call('avatar', 'join', array ($iProfileId, '_Join complete'))) {
  438. exit;
  439. }
  440. }
  441. else {
  442. header('location:' . $this -> _oConfig -> sDefaultRedirectUrl);
  443. exit;
  444. }
  445. }
  446. else {
  447. //assign avatar
  448. if($sMemberAvatar) {
  449. $this -> _assignAvatar($sMemberAvatar);
  450. }
  451. //redirect to other page
  452. header('location:' . $sRedirectUrl);
  453. exit;
  454. }
  455. }
  456. /**
  457. * get join page
  458. *
  459. * @param $aProfileFields array
  460. * @param $iFacebookUserId integer
  461. * @return void
  462. */
  463. function _getJoinPage($aProfileFields, $iFacebookUserId)
  464. {
  465. bx_import('BxDolSession');
  466. $oSession = BxDolSession::getInstance();
  467. $oSession -> setValue($this -> _oConfig -> sFacebookSessionUid, $iFacebookUserId);
  468. bx_import("BxDolJoinProcessor");
  469. $GLOBALS['oSysTemplate']->addJsTranslation('_Errors in join form');
  470. $GLOBALS['oSysTemplate']->addJs(array('join.js', 'jquery.form.js'));
  471. $oJoin = new BxDolJoinProcessor();
  472. //process recived fields
  473. foreach($aProfileFields as $sFieldName => $sValue) {
  474. $oJoin -> aValues[0][$sFieldName] = $sValue;
  475. }
  476. $this -> _oTemplate -> getPage( _t( '_JOIN_H' ), $oJoin->process());
  477. exit;
  478. }
  479. /**
  480. * Function will clear all unnecessary sybmols from profile's nickname;
  481. *
  482. * @param : $sProfileName (string) - profile's nickname;
  483. * @return : (string) - cleared nickname;
  484. */
  485. function _proccesNickName($sProfileName)
  486. {
  487. $sProfileName = preg_replace("/^http:\/\/|^https:\/\/|\/$/", '', $sProfileName);
  488. $sProfileName = str_replace('/', '_', $sProfileName);
  489. $sProfileName = str_replace('.', '-', $sProfileName);
  490. return $sProfileName;
  491. }
  492. }