BxBaseAccountForms.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 = bx_srv('system', 'get_modules_by_type', ['profile']);
  121. if(count($aModulesProfile) == 1)
  122. $sProfileModule = reset($aModulesProfile)['name'];
  123. else if(($sDefaultProfileType = getParam('sys_account_default_profile_type')) !== '')
  124. $sProfileModule = $sDefaultProfileType;
  125. if(!empty($sProfileModule) && getParam('sys_account_auto_profile_creation') == 'on') {
  126. $aProfileFields = BxDolService::call($sProfileModule, 'prepare_fields', [[
  127. 'author' => $iProfileId,
  128. 'name' => BxDolAccount::getInstance($iAccountId)->getDisplayName(),
  129. ]]);
  130. $a = BxDolService::call($sProfileModule, 'entity_add_forcedly', [$iProfileId, $aProfileFields]);
  131. // in case of successful profile add redirect to the page after profile creation
  132. if (0 == $a['code'] && !$bIsApi) {
  133. if($bRelocateCustom)
  134. $this->_redirectAndExit($sRelocateCustom, false);
  135. BxDolService::call($sProfileModule, 'redirect_after_add', array($a['content']));
  136. return;
  137. }
  138. // if creation failed, redirect to create profile form
  139. }
  140. if($bIsApi)
  141. return true;
  142. $sRelocate = !empty($sProfileModule) ? BxDolService::call($sProfileModule, 'profile_create_url', array(false)) : '';
  143. if(empty($sRelocate))
  144. $sRelocate = $bRelocateCustom ? $sRelocateCustom : getParam('sys_redirect_after_account_added');
  145. $this->_redirectAndExit($sRelocate, true, array(
  146. 'account_id' => $iAccountId,
  147. 'profile_id' => $iProfileId,
  148. ));
  149. }
  150. public function createAccount ($aValues)
  151. {
  152. $oForm = $this->getObjectFormAdd ();
  153. if (!$oForm)
  154. return array('code' => 500, 'error' => _t('_sys_txt_error_occured'));
  155. if (empty($aValues['email']))
  156. return array('code' => 500, 'error' => _t('_Incorrect Email'));
  157. if (BxDolAccount::getInstance($aValues['email']))
  158. return array('code' => 500, 'error' => _t('_sys_form_account_input_email_uniq_error_loggedin'));
  159. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  160. $oForm->aParams['csrf']['disable'] = true;
  161. $oForm->initChecker(array(), $aValues);
  162. $iAccountId = $oForm->insert ([]);
  163. if (!$iAccountId)
  164. return array('code' => 500, 'error' => _t('_sys_txt_error_account_creation'));
  165. $iProfileId = $this->onAccountCreated($iAccountId, $oForm->isSetPendingApproval());
  166. return [
  167. 'account_id' => $iAccountId,
  168. 'profile_id' => $iProfileId,
  169. ];
  170. }
  171. public function onAccountCreated ($iAccountId, $isSetPendingApproval, $iAction = BX_PROFILE_ACTION_MANUAL, $bNeedToLogin = true)
  172. {
  173. bx_alert('account', 'add', $iAccountId);
  174. // if email_confirmation procedure is enabled - send email confirmation letter
  175. $oAccount = BxDolAccount::getInstance($iAccountId);
  176. if (BxDolAccount::isNeedConfirmEmail() && $oAccount && !$oAccount->isConfirmedEmail())
  177. $oAccount->sendConfirmationEmail($iAccountId);
  178. // add account and content association
  179. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iAccountId, BX_PROFILE_STATUS_PENDING, 'system');
  180. $oProfile = BxDolProfile::getInstance($iProfileId);
  181. // approve profile if auto-approval is enabled and profile status is 'pending'
  182. $sStatus = $oProfile->getStatus();
  183. $isAutoApprove = !$isSetPendingApproval;
  184. if ($sStatus == BX_PROFILE_STATUS_PENDING && $isAutoApprove)
  185. $oProfile->approve(BX_PROFILE_ACTION_AUTO, $iProfileId, getParam('sys_account_activation_letter') == 'on');
  186. /**
  187. * @hooks
  188. * @hookdef hook-account-added 'account', 'added' - hook on new account created
  189. * - $unit_name - equals `system`
  190. * - $action - equals `added`
  191. * - $object_id - account id
  192. * - $sender_id - not used
  193. * - $extra_params - not used
  194. * @hook @ref hook-account-added
  195. */
  196. bx_alert('account', 'added', $iAccountId);
  197. // login to the created account automatically
  198. if ($bNeedToLogin)
  199. bx_login($iAccountId, bx_is_remember_me());
  200. return $iProfileId;
  201. }
  202. public function editAccountEmailSettingsForm ($iAccountId)
  203. {
  204. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_email');
  205. }
  206. public function editAccountPasswordSettingsForm ($iAccountId)
  207. {
  208. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_pwd');
  209. }
  210. public function editAccountInfoForm ($iAccountId)
  211. {
  212. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_info');
  213. }
  214. public function deleteAccountForm ($iAccountId)
  215. {
  216. $bIsApi = bx_is_api();
  217. $oAccount = BxDolAccount::getInstance($iAccountId);
  218. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  219. if (!$aAccountInfo)
  220. return $bIsApi ? _t('_sys_txt_error_account_is_not_defined') : MsgBox(_t('_sys_txt_error_account_is_not_defined'));
  221. // check access
  222. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedDelete ($this->_iProfileId, $aAccountInfo)))
  223. return $bIsApi ? $sMsg : MsgBox($sMsg);
  224. // check and display form
  225. $oForm = $this->getObjectFormDelete();
  226. if(bx_get('content') !== false) {
  227. $oForm->aInputs['delete_content']['value'] = (int)bx_get('content');
  228. if(!$oForm->aInputs['delete_content']['value'])
  229. $oForm->aInputs['delete_confirm']['caption'] = _t('_sys_form_account_input_delete_confirm_wo_content');
  230. }
  231. if (!$oForm)
  232. return $bIsApi ? _t('_sys_txt_error_occured') : MsgBox(_t('_sys_txt_error_occured'));
  233. if (!$oForm->isSubmitted())
  234. unset($aAccountInfo['password']);
  235. $oForm->initChecker($aAccountInfo);
  236. if (!$oForm->isSubmittedAndValid())
  237. return $bIsApi ? $oForm->getCodeAPI() : $oForm->getCode();
  238. // delete account
  239. if (($oAccount = BxDolAccount::getInstance($aAccountInfo['id'])) !== false && !$oAccount->delete(false === bx_get('delete_content') ? true : (int)$oForm->getCleanValue('delete_content') != 0, true))
  240. return $bIsApi ? _t('_sys_txt_error_account_delete') : MsgBox(_t('_sys_txt_error_account_delete'));
  241. // logout from deleted account
  242. if ($iAccountId == getLoggedId())
  243. bx_logout();
  244. // redirect to homepage
  245. if(!$bIsApi)
  246. $this->_redirectAndExit('', false);
  247. }
  248. protected function _editAccountForm ($iAccountId, $sDisplayName)
  249. {
  250. $bIsApi = bx_is_api();
  251. $oAccount = BxDolAccount::getInstance($iAccountId);
  252. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  253. if (!$aAccountInfo)
  254. return ($sLKey = '_sys_txt_error_account_is_not_defined') && $bIsApi ? _t($sLKey) : MsgBox(_t($sLKey));
  255. // check access
  256. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedEdit ($this->_iProfileId, $aAccountInfo)))
  257. return $bIsApi ? $sMsg : MsgBox($sMsg);
  258. // check and display form
  259. $oForm = BxDolForm::getObjectInstance('sys_account', $sDisplayName);
  260. if (!$oForm)
  261. return $sLKey = '_sys_txt_error_occured' && $bIsApi ? _t($sLKey) : MsgBox(_t($sLKey));
  262. if (!$oForm->isSubmitted())
  263. unset($aAccountInfo['password']);
  264. $oForm->initChecker($aAccountInfo);
  265. if (!$oForm->isSubmittedAndValid())
  266. return $bIsApi ? $oForm->getCodeAPI() : $oForm->getCode();
  267. $aTrackTextFieldsChanges = array (); // track text fields changes, not-null(for example empty array) - means track, null - means don't track
  268. // update email and email setting in DB
  269. if (!$oForm->update ($aAccountInfo['id'], array(), $aTrackTextFieldsChanges)) {
  270. if (!$oForm->isValid())
  271. return $bIsApi ? $oForm->getCodeAPI() : $oForm->getCode();
  272. else
  273. return $sLKey = '_sys_txt_error_account_update' && $bIsApi ? _t($sLKey) : MsgBox(_t($sLKey));
  274. }
  275. if(($sField = 'picture') && isset($oForm->aInputs[$sField]))
  276. $oForm->processFiles($sField, $iAccountId, false);
  277. // check if email was changed
  278. if (!empty($aTrackTextFieldsChanges['changed_fields']) && in_array('email', $aTrackTextFieldsChanges['changed_fields'])){
  279. $oAccount = BxDolAccount::getInstance($iAccountId, true); // refresh account to clear cache
  280. $oAccount->updateEmailConfirmed(false); // mark email as unconfirmed
  281. }
  282. // check if password was changed
  283. if ($oForm->getCleanValue('password')) {
  284. // relogin with new password
  285. bx_alert('account', 'edited', $aAccountInfo['id'], $aAccountInfo['id'], array('action' => 'change_password'));
  286. bx_logout();
  287. bx_login($aAccountInfo['id'], bx_is_remember_me());
  288. }
  289. // check if other text info was changed - if auto-appproval is off
  290. $isAutoApprove = $oForm->isSetPendingApproval() ? false : true;
  291. if (!$isAutoApprove) {
  292. $oProfile = BxDolProfile::getInstanceAccountProfile($aAccountInfo['id']); // get profile associated with account, not current porfile
  293. $aProfileInfo = $oProfile->getInfo();
  294. unset($aTrackTextFieldsChanges['changed_fields']['email']); // email confirmation is automatic and separate, don't need to deactivate whole profile if email is changed
  295. if (BX_PROFILE_STATUS_ACTIVE == $aProfileInfo['status'] && !empty($aTrackTextFieldsChanges['changed_fields']))
  296. $oProfile->disapprove(BX_PROFILE_ACTION_AUTO); // change profile to 'pending' only if some text fields were changed and profile is active
  297. }
  298. bx_alert('account', 'edited', $aAccountInfo['id'], $aAccountInfo['id'], array('display' => $sDisplayName));
  299. // display result message
  300. $sMsg = MsgBox(_t('_' . $sDisplayName . '_successfully_submitted'));
  301. if ($bIsApi)
  302. return ['form' => $oForm->getCodeAPI(), 'msg' => _t('_' . $sDisplayName . '_successfully_submitted')];
  303. return $sMsg . $oForm->getCode();
  304. }
  305. }
  306. /** @} */