BxBaseModConnectModule.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup BaseConnect Base classes for OAuth connect modules
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModConnectModule extends BxDolModule
  12. {
  13. function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. public function serviceGetProfilesModules ()
  18. {
  19. $aModules = array();
  20. $a = BxDolService::call('system', 'get_profiles_modules', array(), 'TemplServiceProfiles');
  21. foreach ($a as &$aModule)
  22. $aModules[$aModule['name']] = $aModule['title'];
  23. return $aModules;
  24. }
  25. public function serviceGetPrivacyGroups ()
  26. {
  27. $oPrivacyQuery = new BxDolPrivacyQuery();
  28. $a = $oPrivacyQuery->getGroupsBy(array('type' => 'active'));
  29. $aGroups = array();
  30. foreach ($a as $r) {
  31. if (!(int)$r['active'])
  32. continue;
  33. $aGroups[$r['id']] = _t($r['title']);
  34. }
  35. return $aGroups;
  36. }
  37. /**
  38. * Logged profile
  39. *
  40. * @param $iProfileId integer
  41. * @param $sPassword string
  42. * @param $sCallbackUrl
  43. * @param $bRedirect boolean
  44. * @return void
  45. */
  46. function setLogged($iProfileId, $sCallbackUrl = '', $bRedirect = true)
  47. {
  48. $oProfile = BxDolProfile::getInstance($iProfileId);
  49. bx_login($oProfile->getAccountId());
  50. if ($bRedirect) {
  51. $sCallbackUrl = $sCallbackUrl
  52. ? $sCallbackUrl
  53. : $this -> _oConfig -> sDefaultRedirectUrl;
  54. header('Location: ' . $sCallbackUrl);
  55. }
  56. }
  57. /**
  58. * Create new profile;
  59. *
  60. * @param : $aProfileInfo (array) - remote profile's information;
  61. *
  62. * @param : $sAlternativeName (string) - profiles alternative nickname;
  63. */
  64. function _createProfile($aProfileInfo, $sAlternativeName = '')
  65. {
  66. $mixed = $this->_createProfileRaw($aProfileInfo, $sAlternativeName);
  67. // display error
  68. if (is_string($mixed)) {
  69. $this->_oTemplate->getPage(_t($this->_oConfig->sDefaultTitleLangKey), DesignBoxContent(_t($this->_oConfig->sDefaultTitleLangKey), MsgBox($mixed)));
  70. exit;
  71. }
  72. // display join page
  73. if (is_array($mixed) && isset($mixed['join_page_redirect'])) {
  74. $this->_getJoinPage($mixed['profile_fields'], $mixed['remote_profile_info']['id']);
  75. exit;
  76. }
  77. // continue profile creation
  78. if (is_array($mixed) && isset($mixed['profile_id'])) {
  79. $iProfileId = (int)$mixed['profile_id'];
  80. //redirect to other page
  81. header('location:' . $this->_getRedirectUrl($iProfileId, $mixed['existing_profile']));
  82. exit;
  83. }
  84. $this->_oTemplate->getPage( _t($this->_oConfig->sDefaultTitleLangKey), MsgBox(_t('_Error Occured')) );
  85. exit;
  86. }
  87. /**
  88. * @param $aProfileInfo - remote profile info
  89. * @param $sAlternativeName - suffix to add to NickName to make it unique
  90. * @return profile array info, ready for the local database
  91. */
  92. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  93. {
  94. }
  95. /**
  96. * Create new profile;
  97. *
  98. * @param : $aProfileInfo (array) - remote profile's information;
  99. *
  100. * @param : $sAlternativeName (string) - profiles alternative nickname;
  101. * @return : error string or error or request invite form or profile info array on success
  102. */
  103. function _createProfileRaw($aProfileInfo, $sAlternativeName = '', $isAutoFriends = true, $isSetLoggedIn = true)
  104. {
  105. // join by invite only
  106. if (BxDolRequest::serviceExists('bx_invites', 'account_add_form_check') && $sCode = BxDolService::call('bx_invites', 'account_add_form_check'))
  107. return $sCode;
  108. // convert fields to unique format
  109. $aFieldsProfile = $aFieldsAccount = $this->_convertRemoteFields($aProfileInfo, $sAlternativeName);
  110. if (empty($aFieldsProfile['email']))
  111. return _t('_Incorrect Email');
  112. // prepare fields for particular module
  113. $aFieldsAccount = BxDolService::call('system', 'prepare_fields', array($aFieldsAccount));
  114. $aFieldsProfile = BxDolService::call($this->_oConfig->sProfilesModule, 'prepare_fields', array($aFieldsProfile));
  115. // check fields existence in Account
  116. $oFormHelperAccount = BxDolService::call('system', 'forms_helper');
  117. $oFormAccount = $oFormHelperAccount->getObjectFormAdd();
  118. foreach ($aFieldsAccount as $sKey => $mValue) {
  119. if (!$oFormAccount->isFieldExist($sKey))
  120. unset($aFieldsAccount[$sKey]);
  121. }
  122. // check fields existence in Profile
  123. if ('system' != $this->_oConfig->sProfilesModule && $oFormHelperProfile = BxDolService::call($this->_oConfig->sProfilesModule, 'forms_helper')) {
  124. $oFormProfile = $oFormHelperProfile->getObjectFormAdd();
  125. foreach ($aFieldsProfile as $sKey => $mValue) {
  126. if (!$oFormProfile->isFieldExist($sKey))
  127. unset($aFieldsProfile[$sKey]);
  128. }
  129. }
  130. // antispam check
  131. $sErrorMsg = '';
  132. $bSetPendingApproval = false;
  133. bx_alert('account', 'check_join', 0, false, array('error_msg' => &$sErrorMsg, 'email' => $aFieldsAccount['email'], 'approve' => &$bSetPendingApproval));
  134. if ($sErrorMsg)
  135. return $sErrorMsg;
  136. // check if user with the same email already exists
  137. $oExistingAccount = BxDolAccount::getInstance($aFieldsAccount['email']);
  138. // check redirect page
  139. if ('join' == $this->_oConfig->sRedirectPage && !$oExistingAccount)
  140. return array('remote_profile_info' => $aProfileInfo, 'profile_fields' => $aFieldsAccount, 'join_page_redirect' => true);
  141. // create new profile
  142. if ($oExistingAccount) {
  143. if (!($oExistingProfile = BxDolProfile::getInstanceByAccount($oExistingAccount->id())))
  144. return _t('_sys_txt_error_account_creation');
  145. $iProfileId = $oExistingProfile->id();
  146. $this->setLogged($iProfileId);
  147. }
  148. else {
  149. // create account
  150. $aFieldsAccount['password'] = genRndPwd();
  151. $aFieldsAccount['email_confirmed'] = $this->_oConfig->isAlwaysConfirmEmail;
  152. if (!($iAccountId = $oFormAccount->insert($aFieldsAccount)))
  153. return _t('_sys_txt_error_account_creation');
  154. $isSetPendingApproval = $this->_oConfig->isAlwaysAutoApprove ? false : !(bool)getParam('sys_account_autoapproval');
  155. $iAccountProfileId = $oFormHelperAccount->onAccountCreated ($iAccountId, $isSetPendingApproval, BX_PROFILE_ACTION_EXTERNAL);
  156. // create profile
  157. if (isset($oFormProfile) && $oFormProfile) {
  158. $aFieldsProfile['picture'] = $this->_processImage($aFieldsProfile, $iAccountProfileId, $oFormHelperProfile);
  159. $_POST['picture'] = $aFieldsProfile['picture']; // set POST variable to correctly process images in processFiles method in form object
  160. if (!($iContentId = $oFormProfile->insert($aFieldsProfile)))
  161. return _t('_sys_txt_error_account_creation');
  162. $oFormHelperProfile->setAutoApproval($oFormHelperProfile->isAutoApproval() ? true : $this->_oConfig->isAlwaysAutoApprove);
  163. if ($sErrorMsg = $oFormHelperProfile->onDataAddAfter ($iAccountId, $iContentId))
  164. return $sErrorMsg;
  165. $oProfile = BxDolProfile::getInstanceByAccount($iAccountId);
  166. $iProfileId = $oProfile->id();
  167. }
  168. else {
  169. $iProfileId = $iAccountProfileId;
  170. }
  171. // send email with password
  172. sendMailTemplate($this->_oConfig->sEmailTemplatePasswordGenerated, $iAccountId, $iProfileId, array('password' => $aFieldsAccount['password']), BX_EMAIL_SYSTEM);
  173. }
  174. // remember remote profile id for created member
  175. $this ->_oDb->saveRemoteId($iProfileId, $aProfileInfo['id']);
  176. // auto-friend members if they are already friends on remote site
  177. if ($isAutoFriends && method_exists($this, '_makeFriends'))
  178. $this->_makeFriends($iProfileId);
  179. return array('remote_profile_info' => $aProfileInfo, 'profile_id' => $iProfileId, 'existing_profile' => $oExistingAccount ? true : false);
  180. }
  181. protected function _processImage($aFieldsProfile, $iAccountProfileId, $oFormHelperProfile)
  182. {
  183. if (!isset($aFieldsProfile['picture']) || !$aFieldsProfile['picture'])
  184. return 0;
  185. if (!($oStorage = $oFormHelperProfile->getObjectStorage()))
  186. return 0;
  187. if (!($iFileId = $oStorage->storeFileFromUrl($aFieldsProfile['picture'], false, $iAccountProfileId)))
  188. return 0;
  189. return $iFileId;
  190. }
  191. /**
  192. * Get join page
  193. *
  194. * @param $aProfileFields array
  195. * @param $iRemoteProfileId remote profile id
  196. * @return void
  197. */
  198. function _getJoinPage($aProfileFields, $iRemoteProfileId)
  199. {
  200. bx_import('BxDolSession');
  201. $oSession = BxDolSession::getInstance();
  202. $oSession->setValue($this->_oConfig->sSessionUid, $iRemoteProfileId);
  203. $oPage = BxDolPage::getObjectInstanceByURI('create-account');
  204. BxBaseAccountForms::$PROFILE_FIELDS = $aProfileFields;
  205. $this->_oTemplate->getPage(false, $oPage->getCode());
  206. }
  207. /**
  208. * get redirect URL
  209. *
  210. * @param $iProfileId integer - profile ID
  211. * @return string redirect URL
  212. */
  213. function _getRedirectUrl($iProfileId, $isExistingProfile = false)
  214. {
  215. $sRedirectUrl = $this->_oConfig->sDefaultRedirectUrl;
  216. switch($this->_oConfig->sRedirectPage) {
  217. case 'index':
  218. $sRedirectUrl = BX_DOL_URL_ROOT;
  219. break;
  220. case 'settings':
  221. if (!$isExistingProfile) {
  222. $sRedirectUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=account-settings-email');
  223. break;
  224. }
  225. case 'dashboard':
  226. default:
  227. $sRedirectUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=dashboard');
  228. break;
  229. }
  230. return $sRedirectUrl;
  231. }
  232. protected function _redirect($sUrl, $iStatus = 302)
  233. {
  234. header("Location:{$sUrl}", true, $iStatus);
  235. exit;
  236. }
  237. }
  238. /** @} */