BxBaseAccountForms.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * System profile(account) forms functions
  11. * @see BxDolProfileForms
  12. */
  13. class BxBaseAccountForms extends BxDolProfileForms
  14. {
  15. protected $_iProfileId;
  16. static public $PROFILE_FIELDS = array();
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->_iProfileId = bx_get_logged_profile_id();
  21. }
  22. public function getObjectFormAdd ()
  23. {
  24. $oForm = BxDolForm::getObjectInstance('sys_account', 'sys_account_create');
  25. /**
  26. * @hooks
  27. * @hookdef hook-account-add_form_get 'account', 'add_form_get' - hook in get some account form
  28. * - $unit_name - equals `account`
  29. * - $action - equals `add_form_get`
  30. * - $object_id - not used
  31. * - $sender_id - not used
  32. * - $extra_params - array of additional params with the following array keys:
  33. * - `form_object` - [object] by ref, form object, can be overridden in hook processing
  34. * @hook @ref hook-account-add_form_get
  35. */
  36. bx_alert('account', 'add_form_get', 0, 0, [
  37. 'form_object' => &$oForm
  38. ]);
  39. return $oForm;
  40. }
  41. public function getObjectFormEdit ()
  42. {
  43. return BxDolForm::getObjectInstance('sys_account', 'sys_account_settings_info');
  44. }
  45. public function getObjectFormDelete ()
  46. {
  47. return BxDolForm::getObjectInstance('sys_account', 'sys_account_settings_del_account');
  48. }
  49. public function createAccountForm ($aParams = array())
  50. {
  51. $bIsApi = bx_is_api();
  52. // check access
  53. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedCreate (0)))
  54. return MsgBox($sMsg);
  55. // check and display form
  56. $oForm = $this->getObjectFormAdd ();
  57. if (!$oForm)
  58. return MsgBox(_t('_sys_txt_error_occured'));
  59. $oForm->aFormAttrs['action'] = !empty($aParams['action']) ? $aParams['action'] : bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=create-account'));
  60. $oForm->initChecker(self::$PROFILE_FIELDS);
  61. /**
  62. * @hooks
  63. * @hookdef hook-account-add_form_check 'account', 'add_form_check' - hook in some account form after check
  64. * - $unit_name - equals `account`
  65. * - $action - equals `add_form_check`
  66. * - $object_id - not used
  67. * - $sender_id - not used
  68. * - $extra_params - array of additional params with the following array keys:
  69. * - `form_object` - [object] by ref, form object, can be overridden in hook processing
  70. * @hook @ref hook-account-add_form_check
  71. */
  72. bx_alert('account', 'add_form_check', 0, 0, array(
  73. 'form_object' => &$oForm
  74. ));
  75. if (!$oForm->isSubmittedAndValid()) {
  76. $sCode = $oForm->getCode();
  77. /**
  78. * @hooks
  79. * @hookdef hook-account-add_form 'account', 'add_form' - hook in some account form after check
  80. * - $unit_name - equals `account`
  81. * - $action - equals `add_form`
  82. * - $object_id - not used
  83. * - $sender_id - not used
  84. * - $extra_params - array of additional params with the following array keys:
  85. * - `form_object` - [object] by ref, form object, can be overridden in hook processing
  86. * - `form_code` - [string] by ref, html for form, can be overridden in hook processing
  87. * @hook @ref hook-account-add_form
  88. */
  89. bx_alert('account', 'add_form', 0, 0, array(
  90. 'form_object' => &$oForm,
  91. 'form_code' => &$sCode
  92. ));
  93. if($bIsApi){
  94. if (!$oForm)
  95. return false;
  96. return $oForm->getCodeAPI();
  97. }
  98. return $sCode;
  99. }
  100. // insert data into database
  101. $aValsToAdd = array (
  102. 'email_confirmed' => 0,
  103. );
  104. $iAccountId = $oForm->insert ($aValsToAdd);
  105. if (!$iAccountId) {
  106. if (!$oForm->isValid())
  107. return $bIsApi ? $oForm->getCodeAPI() : $oForm->getCode();
  108. else
  109. return MsgBox(_t('_sys_txt_error_account_creation'));
  110. }
  111. $iProfileId = $this->onAccountCreated($iAccountId, $oForm->isSetPendingApproval());
  112. if(($sField = 'picture') && isset($oForm->aInputs[$sField]))
  113. $oForm->processFiles($sField, $iAccountId, true);
  114. // perform action
  115. BxDolAccount::isAllowedCreate ($iProfileId, true);
  116. $this->_iProfileId = bx_get_logged_profile_id();
  117. $sRelocateCustom = $oForm->getCleanValue('relocate');
  118. $bRelocateCustom = !empty($sRelocateCustom);
  119. // check and redirect
  120. $aModulesProfile = array();
  121. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  122. foreach($aModules as $aModule) {
  123. $oModule = BxDolModule::getInstance($aModule['name']);
  124. if($oModule instanceof iBxDolProfileService && BxDolService::call($aModule['name'], 'act_as_profile') === true)
  125. $aModulesProfile[] = $aModule;
  126. }
  127. $sDefaultProfileType = getParam('sys_account_default_profile_type');
  128. if(count($aModulesProfile) == 1)
  129. $sProfileModule = $aModulesProfile[0]['name'];
  130. else if(!empty($sDefaultProfileType))
  131. $sProfileModule = $sDefaultProfileType;
  132. if (getParam('sys_account_auto_profile_creation') && !empty($sProfileModule)) {
  133. $oAccount = BxDolAccount::getInstance($iAccountId);
  134. $aProfileInfo = BxDolService::call($sProfileModule, 'prepare_fields', array(array(
  135. 'author' => $iProfileId,
  136. 'name' => $oAccount->getDisplayName(),
  137. )));
  138. $a = BxDolService::call($sProfileModule, 'entity_add', array($iProfileId, $aProfileInfo));
  139. // in case of successful profile add redirect to the page after profile creation
  140. if (0 == $a['code'] && !$bIsApi) {
  141. if($bRelocateCustom)
  142. $this->_redirectAndExit($sRelocateCustom, false);
  143. BxDolService::call($sProfileModule, 'redirect_after_add', array($a['content']));
  144. return;
  145. }
  146. // if creation failed, redirect to create profile form
  147. }
  148. if($bIsApi)
  149. return true;
  150. $sRelocate = !empty($sProfileModule) ? BxDolService::call($sProfileModule, 'profile_create_url', array(false)) : '';
  151. if(empty($sRelocate))
  152. $sRelocate = $bRelocateCustom ? $sRelocateCustom : getParam('sys_redirect_after_account_added');
  153. $this->_redirectAndExit($sRelocate, true, array(
  154. 'account_id' => $iAccountId,
  155. 'profile_id' => $iProfileId,
  156. ));
  157. }
  158. public function createAccount ($aValues)
  159. {
  160. $oForm = $this->getObjectFormAdd ();
  161. if (!$oForm)
  162. return array('code' => 500, 'error' => _t('_sys_txt_error_occured'));
  163. if (empty($aValues['email']))
  164. return array('code' => 500, 'error' => _t('_Incorrect Email'));
  165. if (BxDolAccount::getInstance($aValues['email']))
  166. return array('code' => 500, 'error' => _t('_sys_form_account_input_email_uniq_error_loggedin'));
  167. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  168. $oForm->aParams['csrf']['disable'] = true;
  169. $oForm->initChecker(array(), $aValues);
  170. $iAccountId = $oForm->insert ([]);
  171. if (!$iAccountId)
  172. return array('code' => 500, 'error' => _t('_sys_txt_error_account_creation'));
  173. $iProfileId = $this->onAccountCreated($iAccountId, $oForm->isSetPendingApproval());
  174. return [
  175. 'account_id' => $iAccountId,
  176. 'profile_id' => $iProfileId,
  177. ];
  178. }
  179. public function onAccountCreated ($iAccountId, $isSetPendingApproval, $iAction = BX_PROFILE_ACTION_MANUAL, $bNeedToLogin = true)
  180. {
  181. bx_alert('account', 'add', $iAccountId);
  182. // if email_confirmation procedure is enabled - send email confirmation letter
  183. $oAccount = BxDolAccount::getInstance($iAccountId);
  184. if (BxDolAccount::isNeedConfirmEmail() && $oAccount && !$oAccount->isConfirmedEmail())
  185. $oAccount->sendConfirmationEmail($iAccountId);
  186. // add account and content association
  187. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iAccountId, BX_PROFILE_STATUS_PENDING, 'system');
  188. $oProfile = BxDolProfile::getInstance($iProfileId);
  189. // approve profile if auto-approval is enabled and profile status is 'pending'
  190. $sStatus = $oProfile->getStatus();
  191. $isAutoApprove = !$isSetPendingApproval;
  192. if ($sStatus == BX_PROFILE_STATUS_PENDING && $isAutoApprove)
  193. $oProfile->approve(BX_PROFILE_ACTION_AUTO, $iProfileId, getParam('sys_account_activation_letter') == 'on');
  194. /**
  195. * @hooks
  196. * @hookdef hook-account-added 'account', 'added' - hook on new account created
  197. * - $unit_name - equals `system`
  198. * - $action - equals `added`
  199. * - $object_id - account id
  200. * - $sender_id - not used
  201. * - $extra_params - not used
  202. * @hook @ref hook-account-added
  203. */
  204. bx_alert('account', 'added', $iAccountId);
  205. // login to the created account automatically
  206. if ($bNeedToLogin)
  207. bx_login($iAccountId, bx_is_remember_me());
  208. return $iProfileId;
  209. }
  210. public function editAccountEmailSettingsForm ($iAccountId)
  211. {
  212. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_email');
  213. }
  214. public function editAccountPasswordSettingsForm ($iAccountId)
  215. {
  216. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_pwd');
  217. }
  218. public function editAccountInfoForm ($iAccountId)
  219. {
  220. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_info');
  221. }
  222. public function deleteAccountForm ($iAccountId)
  223. {
  224. $bIsApi = bx_is_api();
  225. $oAccount = BxDolAccount::getInstance($iAccountId);
  226. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  227. if (!$aAccountInfo)
  228. return $bIsApi ? _t('_sys_txt_error_account_is_not_defined') : MsgBox(_t('_sys_txt_error_account_is_not_defined'));
  229. // check access
  230. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedDelete ($this->_iProfileId, $aAccountInfo)))
  231. return $bIsApi ? $sMsg : MsgBox($sMsg);
  232. // check and display form
  233. $oForm = $this->getObjectFormDelete();
  234. if(bx_get('content') !== false) {
  235. $oForm->aInputs['delete_content']['value'] = (int)bx_get('content');
  236. if(!$oForm->aInputs['delete_content']['value'])
  237. $oForm->aInputs['delete_confirm']['caption'] = _t('_sys_form_account_input_delete_confirm_wo_content');
  238. }
  239. if (!$oForm)
  240. return $bIsApi ? _t('_sys_txt_error_occured') : MsgBox(_t('_sys_txt_error_occured'));
  241. if (!$oForm->isSubmitted())
  242. unset($aAccountInfo['password']);
  243. $oForm->initChecker($aAccountInfo);
  244. if (!$oForm->isSubmittedAndValid())
  245. return $bIsApi ? $oForm->getCodeAPI() : $oForm->getCode();
  246. // delete account
  247. if (($oAccount = BxDolAccount::getInstance($aAccountInfo['id'])) !== false && !$oAccount->delete(false === bx_get('delete_content') ? true : (int)$oForm->getCleanValue('delete_content') != 0, true))
  248. return $bIsApi ? _t('_sys_txt_error_account_delete') : MsgBox(_t('_sys_txt_error_account_delete'));
  249. // logout from deleted account
  250. if ($iAccountId == getLoggedId())
  251. bx_logout();
  252. // redirect to homepage
  253. if(!$bIsApi)
  254. $this->_redirectAndExit('', false);
  255. }
  256. protected function _editAccountForm ($iAccountId, $sDisplayName)
  257. {
  258. $bIsApi = bx_is_api();
  259. $oAccount = BxDolAccount::getInstance($iAccountId);
  260. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  261. if (!$aAccountInfo)
  262. return ($sLKey = '_sys_txt_error_account_is_not_defined') && $bIsApi ? _t($sLKey) : MsgBox(_t($sLKey));
  263. // check access
  264. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedEdit ($this->_iProfileId, $aAccountInfo)))
  265. return $bIsApi ? $sMsg : MsgBox($sMsg);
  266. // check and display form
  267. $oForm = BxDolForm::getObjectInstance('sys_account', $sDisplayName);
  268. if (!$oForm)
  269. return $sLKey = '_sys_txt_error_occured' && $bIsApi ? _t($sLKey) : MsgBox(_t($sLKey));
  270. if (!$oForm->isSubmitted())
  271. unset($aAccountInfo['password']);
  272. $oForm->initChecker($aAccountInfo);
  273. if (!$oForm->isSubmittedAndValid())
  274. return $bIsApi ? $oForm->getCodeAPI() : $oForm->getCode();
  275. $aTrackTextFieldsChanges = array (); // track text fields changes, not-null(for example empty array) - means track, null - means don't track
  276. // update email and email setting in DB
  277. if (!$oForm->update ($aAccountInfo['id'], array(), $aTrackTextFieldsChanges)) {
  278. if (!$oForm->isValid())
  279. return $bIsApi ? $oForm->getCodeAPI() : $oForm->getCode();
  280. else
  281. return $sLKey = '_sys_txt_error_account_update' && $bIsApi ? _t($sLKey) : MsgBox(_t($sLKey));
  282. }
  283. if(($sField = 'picture') && isset($oForm->aInputs[$sField]))
  284. $oForm->processFiles($sField, $iAccountId, false);
  285. // check if email was changed
  286. if (!empty($aTrackTextFieldsChanges['changed_fields']) && in_array('email', $aTrackTextFieldsChanges['changed_fields'])){
  287. $oAccount = BxDolAccount::getInstance($iAccountId, true); // refresh account to clear cache
  288. $oAccount->updateEmailConfirmed(false); // mark email as unconfirmed
  289. }
  290. // check if password was changed
  291. if ($oForm->getCleanValue('password')) {
  292. // relogin with new password
  293. bx_alert('account', 'edited', $aAccountInfo['id'], $aAccountInfo['id'], array('action' => 'change_password'));
  294. bx_logout();
  295. bx_login($aAccountInfo['id'], bx_is_remember_me());
  296. }
  297. // check if other text info was changed - if auto-appproval is off
  298. $isAutoApprove = $oForm->isSetPendingApproval() ? false : true;
  299. if (!$isAutoApprove) {
  300. $oProfile = BxDolProfile::getInstanceAccountProfile($aAccountInfo['id']); // get profile associated with account, not current porfile
  301. $aProfileInfo = $oProfile->getInfo();
  302. unset($aTrackTextFieldsChanges['changed_fields']['email']); // email confirmation is automatic and separate, don't need to deactivate whole profile if email is changed
  303. if (BX_PROFILE_STATUS_ACTIVE == $aProfileInfo['status'] && !empty($aTrackTextFieldsChanges['changed_fields']))
  304. $oProfile->disapprove(BX_PROFILE_ACTION_AUTO); // change profile to 'pending' only if some text fields were changed and profile is active
  305. }
  306. bx_alert('account', 'edited', $aAccountInfo['id'], $aAccountInfo['id'], array('display' => $sDisplayName));
  307. // display result message
  308. $sMsg = MsgBox(_t('_' . $sDisplayName . '_successfully_submitted'));
  309. if ($bIsApi)
  310. return ['form' => $oForm->getCodeAPI(), 'msg' => _t('_' . $sDisplayName . '_successfully_submitted')];
  311. return $sMsg . $oForm->getCode();
  312. }
  313. }
  314. /** @} */