BxDolAccount.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. bx_import('BxDolAcl');
  10. class BxDolAccount extends BxDolFactory implements iBxDolSingleton
  11. {
  12. protected $_iAccountID;
  13. protected $_aInfo;
  14. protected $_oQuery;
  15. /**
  16. * Constructor
  17. */
  18. protected function __construct ($iAccountId)
  19. {
  20. $iAccountId = (int)$iAccountId;
  21. $sClass = get_class($this) . '_' . $iAccountId;
  22. if (isset($GLOBALS['bxDolClasses'][$sClass]))
  23. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  24. parent::__construct();
  25. $this->_iAccountID = $iAccountId; // since constructor is protected $iAccountId is always valid
  26. $this->_oQuery = BxDolAccountQuery::getInstance();
  27. }
  28. /**
  29. * Prevent cloning the instance
  30. */
  31. public function __clone()
  32. {
  33. $sClass = get_class($this) . '_' . $this->_iProfileID;
  34. if (isset($GLOBALS['bxDolClasses'][$sClass]))
  35. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  36. }
  37. /**
  38. * Get singleton instance of the class
  39. */
  40. public static function getInstance($mixedAccountId = false, $bClearCache = false)
  41. {
  42. if (!$mixedAccountId)
  43. $mixedAccountId = getLoggedId();
  44. $iAccountId = self::getID($mixedAccountId);
  45. if (!$iAccountId)
  46. return false;
  47. $sClass = __CLASS__ . '_' . $iAccountId;
  48. if ($bClearCache && isset($GLOBALS['bxDolClasses'][$sClass]))
  49. unset($GLOBALS['bxDolClasses'][$sClass]);
  50. if(!isset($GLOBALS['bxDolClasses'][$sClass]))
  51. $GLOBALS['bxDolClasses'][$sClass] = new BxDolAccount($iAccountId);
  52. return $GLOBALS['bxDolClasses'][$sClass];
  53. }
  54. /**
  55. * Get studio operator account singleton instance on the class
  56. */
  57. public static function getInstanceStudioOperator()
  58. {
  59. $oQuery = BxDolAccountQuery::getInstance();
  60. if (!($iId = $oQuery->getStudioOperatorId()))
  61. return false;
  62. return self::getInstance($iId);
  63. }
  64. /**
  65. * Get account id
  66. */
  67. public function id()
  68. {
  69. $a = $this->getInfo($this->_iAccountID);
  70. return isset($a['id']) ? $a['id'] : false;
  71. }
  72. /**
  73. * Check if account is confirmed, it is checked by email confirmation
  74. */
  75. public function isConfirmed($iAccountId = false)
  76. {
  77. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  78. $a = $this->getInfo($iId);
  79. $bResult = false;
  80. $sConfirmationType = getParam('sys_account_confirmation_type');
  81. switch($sConfirmationType) {
  82. case BX_ACCOUNT_CONFIRMATION_NONE:
  83. $bResult = true;
  84. break;
  85. case BX_ACCOUNT_CONFIRMATION_EMAIL:
  86. if($a['email_confirmed'])
  87. $bResult = true;
  88. break;
  89. case BX_ACCOUNT_CONFIRMATION_PHONE:
  90. if($a['phone_confirmed'])
  91. $bResult = true;
  92. break;
  93. case BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE:
  94. if($a['email_confirmed'] && $a['phone_confirmed'])
  95. $bResult = true;
  96. break;
  97. case BX_ACCOUNT_CONFIRMATION_EMAIL_OR_PHONE:
  98. if($a['email_confirmed'] || $a['phone_confirmed'])
  99. $bResult = true;
  100. break;
  101. }
  102. bx_alert('account', 'is_confirmed', $iId, false, array('type' => $sConfirmationType, 'override_result' => &$bResult));
  103. return $bResult;
  104. }
  105. public function getCurrentConfirmationStatusValue($iAccountId = false)
  106. {
  107. $a = $this->getInfo((int)$iAccountId);
  108. $sTmp = $a['email_confirmed'] . $a['phone_confirmed'];
  109. switch ($sTmp) {
  110. case '01':
  111. return BX_ACCOUNT_CONFIRMATION_PHONE;
  112. case '10':
  113. return BX_ACCOUNT_CONFIRMATION_EMAIL;
  114. case '11':
  115. return BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE;
  116. }
  117. return BX_ACCOUNT_CONFIRMATION_NONE;
  118. }
  119. public function isConfirmedEmail($iAccountId = false)
  120. {
  121. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  122. $bResult = false;
  123. if(self::isNeedConfirmEmail()) {
  124. $a = $this->getInfo($iId);
  125. $bResult = $a['email_confirmed'] ? true : false;
  126. }
  127. else
  128. $bResult = true;
  129. bx_alert('account', 'is_confirmed_email', $iId, false, array('override_result' => &$bResult));
  130. return $bResult;
  131. }
  132. public function isConfirmedPhone($iAccountId = false)
  133. {
  134. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  135. $bResult = false;
  136. if(self::isNeedConfirmPhone()) {
  137. $a = $this->getInfo((int)$iAccountId);
  138. $bResult = $a['phone_confirmed'] ? true : false;
  139. }
  140. else
  141. $bResult = true;
  142. bx_alert('account', 'is_confirmed_phone', $iId, false, array('override_result' => &$bResult));
  143. return $bResult;
  144. }
  145. static public function isNeedConfirmEmail()
  146. {
  147. if(in_array(getParam('sys_account_confirmation_type'), array(BX_ACCOUNT_CONFIRMATION_EMAIL, BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE, BX_ACCOUNT_CONFIRMATION_EMAIL_OR_PHONE)))
  148. return true;
  149. return false;
  150. }
  151. static public function isNeedConfirmPhone()
  152. {
  153. if(in_array(getParam('sys_account_confirmation_type'), array(BX_ACCOUNT_CONFIRMATION_PHONE, BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE, BX_ACCOUNT_CONFIRMATION_EMAIL_OR_PHONE)))
  154. return true;
  155. return false;
  156. }
  157. public function isLocked($iAccountId = false)
  158. {
  159. $a = $this->getInfo((int)$iAccountId);
  160. return $a['locked'] ? true : false;
  161. }
  162. //
  163. /**
  164. * Set account email to confirmed or unconfirmed
  165. * @param int $isConfirmed - false: mark email as unconfirmed, true: as confirmed
  166. * @param int $iAccountId - optional account id
  167. * @return true on success or false on error
  168. */
  169. public function updateEmailConfirmed($isConfirmed, $isAutoSendConfrmationEmail = true, $iAccountId = false)
  170. {
  171. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  172. if (!$isConfirmed && $isAutoSendConfrmationEmail && self::isNeedConfirmEmail()) // if email_confirmation procedure is enabled - send email confirmation letter
  173. $this->sendConfirmationEmail($iId);
  174. if ($this->_oQuery->updateEmailConfirmed($isConfirmed, $iId)) {
  175. $this->_aInfo = false;
  176. bx_alert('account', $this->isConfirmed() ? 'confirm' : 'unconfirm', $iId);
  177. return true;
  178. }
  179. return false;
  180. }
  181. /**
  182. * Set account phone to confirmed or unconfirmed
  183. * @param int $isConfirmed - false: mark phone as unconfirmed, true: as confirmed
  184. * @param int $iAccountId - optional account id
  185. * @return true on success or false on error
  186. */
  187. public function updatePhoneConfirmed($isConfirmed, $iAccountId = false)
  188. {
  189. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  190. if ($this->_oQuery->updatePhoneConfirmed($isConfirmed, $iId)) {
  191. $this->_aInfo = false;
  192. bx_alert('account', $this->isConfirmed() ? 'confirm' : 'unconfirm', $iId);
  193. return true;
  194. }
  195. return false;
  196. }
  197. /**
  198. * Change account password
  199. * @param string $sPassword - new password
  200. * @param int $iAccountId - optional account id
  201. * @return true on success or false on error
  202. */
  203. public function updatePassword($sPassword, $iAccountId = false)
  204. {
  205. $sSalt = genRndSalt();
  206. $sPasswordHash = encryptUserPwd($sPassword, $sSalt);
  207. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  208. $oAccountSender = BxDolAccount::getInstance();
  209. if((int)BxDolAccountQuery::getInstance()->updatePassword($sPasswordHash, $sSalt, $iId) > 0) {
  210. bx_alert('account', 'edited', $iId, $oAccountSender ? $oAccountSender->id() : $iId, array('action' => 'reset_password'));
  211. $this->doAudit($iId, '_sys_audit_action_account_reset_password');
  212. return true;
  213. }
  214. return false;
  215. }
  216. /**
  217. * Set account phone
  218. * @param string $sPhone - phone number
  219. * @param int $iAccountId - optional account id
  220. * @return true on success or false on error
  221. */
  222. public function updatePhone($sPhone, $iAccountId = false)
  223. {
  224. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  225. if ($this->_oQuery->updatePhone($sPhone, $iId)) {
  226. bx_alert('account', 'set_phone', $iId);
  227. return true;
  228. }
  229. return false;
  230. }
  231. /**
  232. * Switch context automatically to the first available profile
  233. * @param $iProfileIdFilterOut profile ID to exclude from the list of possible profiles
  234. * @param $iAccountId account ID to use istead of current account
  235. * @return true on success or false on error
  236. */
  237. public function updateProfileContextAuto($iProfileIdFilterOut = false, $iAccountId = false)
  238. {
  239. $oAccount = (!$iAccountId || $iAccountId == $this->_iAccountID ? $this : BxDolAccount::getInstance ($iAccountId));
  240. if (!$oAccount)
  241. return false;
  242. $aAccountInfo = $oAccount->getInfo();
  243. $aProfiles = $oAccount->getProfiles();
  244. $oProfileAccount = BxDolProfile::getInstanceAccountProfile($oAccount->id());
  245. // unset deleted profile and account profile
  246. if ($iProfileIdFilterOut)
  247. unset($aProfiles[$iProfileIdFilterOut]);
  248. unset($aProfiles[$oProfileAccount->id()]);
  249. if ($aProfiles) {
  250. // try to use another profile
  251. reset($aProfiles);
  252. $iProfileId = key($aProfiles);
  253. }
  254. else {
  255. // if no profiles exist, use account profile
  256. $iProfileId = $oProfileAccount->id();
  257. }
  258. return $oAccount->updateProfileContext($iProfileId);
  259. }
  260. public function updateProfileContext($iSwitchToProfileId, $iAccountId = false)
  261. {
  262. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  263. $aInfo = $this->getInfo((int)$iId);
  264. if (!$aInfo)
  265. return false;
  266. $ret = null;
  267. bx_alert('account', 'before_switch_context', $iId, $iSwitchToProfileId, array('profile_id_current' => $aInfo['profile_id'], 'override_result' => &$ret));
  268. if ($ret !== null)
  269. return $ret;
  270. if (!$this->_oQuery->updateCurrentProfile($iId, $iSwitchToProfileId))
  271. return false;
  272. $this->_aInfo = false;
  273. bx_alert('account', 'switch_context', $iId, $iSwitchToProfileId, array('profile_id_old' => $aInfo['profile_id']));
  274. return true;
  275. }
  276. /**
  277. * Send "confirmation" email
  278. */
  279. public function sendConfirmationEmail($iAccountId = false)
  280. {
  281. $iAccountId = (int)$iAccountId;
  282. if(!$iAccountId)
  283. $iAccountId = $this->_iAccountID;
  284. $aAccountInfo = $this->getInfo($iAccountId);
  285. if(empty($aAccountInfo) || !is_array($aAccountInfo))
  286. return false;
  287. $oKey = BxDolKey::getInstance();
  288. $sConfirmationCode = $oKey->getNewKey(array('account_id' => $iAccountId));
  289. $sConfirmationLink = bx_append_url_params(BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email'), array('code' => urlencode($sConfirmationCode)));
  290. $aReplaceVars = array(
  291. 'name' => $this->getDisplayName($iAccountId),
  292. 'email' => $aAccountInfo['email'],
  293. 'conf_code' => $sConfirmationCode,
  294. 'conf_link' => $sConfirmationLink,
  295. 'conf_form_link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email')
  296. );
  297. $bResult = sendMailTemplate('t_Confirmation', $iAccountId, (int)$aAccountInfo['profile_id'], $aReplaceVars, BX_EMAIL_SYSTEM);
  298. if($bResult)
  299. $this->doAudit($iAccountId, '_sys_audit_action_account_resend_confirmation_email');
  300. return $bResult;
  301. }
  302. public function sendResetPasswordEmail($iAccountId = false)
  303. {
  304. $iAccountId = (int)$iAccountId;
  305. if(!$iAccountId)
  306. $iAccountId = $this->_iAccountID;
  307. $aAccountInfo = $this->getInfo($iAccountId);
  308. if(empty($aAccountInfo) || !is_array($aAccountInfo))
  309. return false;
  310. $sKey = bx_get_reset_password_key($aAccountInfo['email']);
  311. $aReplaceVars = array(
  312. 'name' => $this->getDisplayName($iAccountId),
  313. 'email' => $aAccountInfo['email'],
  314. 'key' => $sKey,
  315. 'forgot_password_url' => bx_get_reset_password_link_by_key($sKey)
  316. );
  317. return sendMailTemplate('t_Forgot', $iAccountId, (int)$aAccountInfo['profile_id'], $aReplaceVars, BX_EMAIL_SYSTEM);
  318. }
  319. /**
  320. * Get account info
  321. */
  322. public function getInfo($iAccountId = false)
  323. {
  324. if ($iAccountId && $iAccountId != $this->_iAccountID)
  325. return $this->_oQuery->getInfoById((int)$iAccountId ? (int)$iAccountId : $this->_iAccountID);
  326. if ($this->_aInfo)
  327. return $this->_aInfo;
  328. $this->_aInfo = $this->_oQuery->getInfoById($this->_iAccountID);
  329. return $this->_aInfo;
  330. }
  331. /**
  332. * Get account display name
  333. */
  334. public function getDisplayName($iAccountId = false)
  335. {
  336. $aInfo = $this->getInfo($iAccountId);
  337. $sDisplayName = !empty($aInfo['name']) ? $aInfo['name'] : _t('_sys_txt_user_n', $aInfo['id']);
  338. bx_alert('account', 'account_name', $iAccountId, 0, array('info' => $aInfo, 'display_name' => &$sDisplayName));
  339. return bx_process_output($sDisplayName);
  340. }
  341. /**
  342. * Get account url
  343. */
  344. public function getUrl($iAccountId = false)
  345. {
  346. return 'javascript:void(0);';
  347. }
  348. /**
  349. * Get account url
  350. */
  351. public function getUnit($iAccountId = false, $aParams = array())
  352. {
  353. $sTemplate = 'unit';
  354. $sTemplateSize = false;
  355. $aTemplateVars = array();
  356. if(!empty($aParams['template'])) {
  357. if(is_string($aParams['template']))
  358. $sTemplate = $aParams['template'];
  359. else if(is_array($aParams['template'])) {
  360. if(!empty($aParams['template']['name']))
  361. $sTemplate = $aParams['template']['name'];
  362. if(!empty($aParams['template']['size']))
  363. $sTemplateSize = $aParams['template']['size'];
  364. if(!empty($aParams['template']['vars']))
  365. $aTemplateVars = $aParams['template']['vars'];
  366. }
  367. }
  368. $sTemplate = 'account_' . $sTemplate . '.html';
  369. if(empty($sTemplateSize))
  370. $sTemplateSize = 'thumb';
  371. $sTitle = $this->getDisplayName($iAccountId);
  372. $aTmplVars = array(
  373. 'size' => $sTemplateSize,
  374. 'color' => implode(', ', BxDolTemplate::getColorCode(($iAccountId ? $iAccountId : $this->_iAccountID), 1.0)),
  375. 'letter' => mb_strtoupper(mb_substr($sTitle, 0, 1)),
  376. 'content_url' => $this->getUrl($iAccountId),
  377. 'title' => $sTitle,
  378. 'title_attr' => bx_html_attribute($sTitle),
  379. 'bx_if:show_online' => array(
  380. 'condition' => $this->isOnline($iAccountId),
  381. 'content' => array()
  382. )
  383. );
  384. if(!empty($aTemplateVars) && is_array($aTemplateVars))
  385. $aTmplVars = array_merge ($aTmplVars, $aTemplateVars);
  386. return BxDolTemplate::getInstance()->parseHtmlByName($sTemplate, $aTmplVars);
  387. }
  388. /**
  389. * Get picture url
  390. */
  391. public function getPicture($iAccountId = false)
  392. {
  393. return BxDolTemplate::getInstance()->getImageUrl('account-picture.png');
  394. }
  395. /**
  396. * Get big (2x) avatar url
  397. */
  398. public function getAvatarBig($iAccountId = false)
  399. {
  400. return BxDolTemplate::getInstance()->getImageUrl('account-avatar-big.png');
  401. }
  402. /**
  403. * Get avatar url
  404. */
  405. public function getAvatar($iAccountId = false)
  406. {
  407. return BxDolTemplate::getInstance()->getImageUrl('account-avatar.png');
  408. }
  409. /**
  410. * Get thumb picture url
  411. */
  412. public function getThumb($iAccountId = false)
  413. {
  414. return BxDolTemplate::getInstance()->getImageUrl('account-thumb.png');
  415. }
  416. /**
  417. * Get icon picture url
  418. */
  419. public function getIcon($iAccountId = false)
  420. {
  421. return BxDolTemplate::getInstance()->getImageUrl('account-icon.png');
  422. }
  423. /**
  424. * Get account email
  425. */
  426. public function getEmail($iAccountId = false)
  427. {
  428. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  429. $aAccountInfo = $this->getInfo($iAccountId);
  430. return $aAccountInfo['email'];
  431. }
  432. /**
  433. * Get language ID
  434. */
  435. public function getLanguageId($iAccountId = false)
  436. {
  437. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  438. $aAccountInfo = $this->getInfo($iAccountId);
  439. return $aAccountInfo['lang_id'];
  440. }
  441. /**
  442. * Is account online
  443. */
  444. public function isOnline($iAccountId = false)
  445. {
  446. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  447. return $this->_oQuery->isOnline($iAccountId);
  448. }
  449. /**
  450. * Validate account.
  451. * @param $s - account identifier (id or email)
  452. * @return account id or false if account was not found
  453. */
  454. static public function getID($s)
  455. {
  456. $oQuery = BxDolAccountQuery::getInstance();
  457. bx_import('BxDolForm');
  458. if (BxDolFormCheckerHelper::checkEmail($s)) {
  459. $iId = (int)$oQuery->getIdByEmail($s);
  460. return $iId ? $iId : false;
  461. }
  462. if (preg_match("/^\+[0-9\s]+$/", $s)) {
  463. $iId = (int)$oQuery->getIdByPhone($s);
  464. return $iId ? $iId : false;
  465. }
  466. $iId = $oQuery->getIdById((int)$s);
  467. return $iId ? $iId : false;
  468. }
  469. /**
  470. * Check if profiles limit reached
  471. */
  472. public function isProfilesLimitReached ()
  473. {
  474. $iProfilesLimit = (int)getParam('sys_account_limit_profiles_number');
  475. bx_alert('account', 'get_limit_profiles_number', 0, 0, array('account_id' => $this->_iAccountID, 'number' => &$iProfilesLimit));
  476. if ($iProfilesLimit && ($iProfilesNum = $this->getProfilesNumber()) && $iProfilesNum >= $iProfilesLimit)
  477. return true;
  478. return false;
  479. }
  480. /**
  481. * Get number of profiles associated with the account
  482. */
  483. public function getProfilesNumber ($isFilterNonSwitchableProfiles = true)
  484. {
  485. $a = $this->getProfilesIds($isFilterNonSwitchableProfiles);
  486. return count($a);
  487. }
  488. /**
  489. * Get all profile ids associated with the account
  490. */
  491. public function getProfilesIds ($isFilterNonSwitchableProfiles = true, $isFilterSystemProfiles = true)
  492. {
  493. $a = $this->getProfiles ($isFilterNonSwitchableProfiles, $isFilterSystemProfiles);
  494. return $a ? array_keys($a) : array();
  495. }
  496. /**
  497. * Get all profiles associated with the account
  498. */
  499. public function getProfiles ($isFilterNonSwitchableProfiles = true, $isFilterSystemProfiles = true)
  500. {
  501. $oProfileQuery = BxDolProfileQuery::getInstance();
  502. $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
  503. if ($isFilterNonSwitchableProfiles) {
  504. foreach ($aProfiles as $iProfileId => $aProfile) {
  505. if ('system' == $aProfile['type'])
  506. continue;
  507. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  508. unset($aProfiles[$iProfileId]);
  509. }
  510. }
  511. if ($isFilterSystemProfiles) {
  512. foreach ($aProfiles as $iProfileId => $aProfile) {
  513. if ('system' == $aProfile['type'])
  514. unset($aProfiles[$iProfileId]);
  515. }
  516. }
  517. return $aProfiles;
  518. }
  519. /**
  520. * Delete profile.
  521. * @param $bDeleteWithContent - delete associated profiles with all its contents
  522. */
  523. function delete($bDeleteWithContent = false)
  524. {
  525. $aAccountInfo = $this->_oQuery->getInfoById($this->_iAccountID);
  526. if (!$aAccountInfo)
  527. return false;
  528. // create system event before deletion
  529. $isStopDeletion = false;
  530. bx_alert('account', 'before_delete', $this->_iAccountID, 0, array('delete_with_content' => $bDeleteWithContent, 'stop_deletion' => &$isStopDeletion));
  531. if ($isStopDeletion)
  532. return false;
  533. $oAccountQuery = BxDolAccountQuery::getInstance();
  534. $oProfileQuery = BxDolProfileQuery::getInstance();
  535. $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
  536. foreach ($aProfiles as $iProfileId => $aRow) {
  537. $oProfile = BxDolProfile::getInstance($iProfileId);
  538. if (!$oProfile)
  539. continue;
  540. $oProfile->delete(false, $bDeleteWithContent, true);
  541. }
  542. // delete profile
  543. if (!$oAccountQuery->delete($this->_iAccountID))
  544. return false;
  545. // unset class instance to prevent creating the instance again
  546. $sClass = __CLASS__ . '_' . $this->_iAccountID;
  547. unset($GLOBALS['bxDolClasses'][$sClass]);
  548. // create system event
  549. bx_alert('account', 'delete', $this->_iAccountID, 0, array ('delete_with_content' => $bDeleteWithContent));
  550. $this->doAudit($this->_iAccountID, $bDeleteWithContent ? '_sys_audit_action_account_deleted_with_content' : '_sys_audit_action_account_deleted');
  551. return true;
  552. }
  553. /**
  554. * Add permament messages.
  555. */
  556. public function addInformerPermanentMessages ($oInformer)
  557. {
  558. if (!$this->isConfirmed()) {
  559. if (!$this->isConfirmedEmail()) {
  560. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email') . '&resend=1';
  561. $aAccountInfo = $this->getInfo();
  562. $oInformer->add('sys-account-unconfirmed-email', _t('_sys_txt_account_unconfirmed_email', $sUrl, $aAccountInfo['email']), BX_INFORMER_ALERT);
  563. }
  564. if (!$this->isConfirmedPhone()) {
  565. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-phone') . '';
  566. $aAccountInfo = $this->getInfo();
  567. $oInformer->add('sys-account-unconfirmed-phone', _t('_sys_txt_account_unconfirmed_phone', $sUrl), BX_INFORMER_ALERT);
  568. }
  569. }
  570. }
  571. /**
  572. * Get unsubscribe link for the specified mesage type
  573. */
  574. public function getUnsubscribeLink($iEmailType, $iAccountId = false)
  575. {
  576. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  577. $sUrl = '';
  578. switch ($iEmailType) {
  579. case BX_EMAIL_NOTIFY:
  580. $sUrl = 'page.php?i=unsubscribe-notifications';
  581. break;
  582. case BX_EMAIL_MASS:
  583. $sUrl = 'page.php?i=unsubscribe-news';
  584. break;
  585. default:
  586. return '';
  587. }
  588. return BxDolPermalinks::getInstance()->permalink($sUrl) . '&id=' . $iAccountId . '&code=' . $this->getEmailHash();
  589. }
  590. public function getEmailHash($iAccountId = false)
  591. {
  592. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  593. $a = $this->getInfo();
  594. return md5($a['email'] . $a['salt'] . BX_DOL_SECRET);
  595. }
  596. public function doAudit($iAccountId, $sAction, $aData = array())
  597. {
  598. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  599. bx_audit(
  600. $iAccountId,
  601. 'bx_accounts',
  602. $sAction,
  603. array('content_title' => $this->getEmail(), 'data' => $aData)
  604. );
  605. }
  606. /**
  607. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  608. */
  609. static public function isAllowedCreate ($iProfileId, $isPerformAction = false)
  610. {
  611. $aCheck = checkActionModule($iProfileId, 'create account', 'system', $isPerformAction);
  612. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  613. return MsgBox($aCheck[CHECK_ACTION_MESSAGE]);
  614. return CHECK_ACTION_RESULT_ALLOWED;
  615. }
  616. static public function isAllowedCreateMultiple ($iProfileId, $isPerformAction = false)
  617. {
  618. $bResult = false;
  619. if((int)getParam('sys_account_limit_profiles_number') != 1)
  620. $bResult = true;
  621. $oProfile = BxDolProfile::getInstance($iProfileId);
  622. if($oProfile && $oProfile->getAccountId() != getLoggedId())
  623. $bResult = false;
  624. bx_alert('profile', 'allow_create_another_profile', $iProfileId, 0, array('override_result' => &$bResult));
  625. return $bResult;
  626. }
  627. /**
  628. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  629. */
  630. static public function isAllowedEdit ($iProfileId, $aContentInfo, $isPerformAction = false)
  631. {
  632. $oProfile = BxDolProfile::getInstance($iProfileId);
  633. if (!$oProfile)
  634. return _t('_sys_txt_access_denied');
  635. $aProfileInfo = $oProfile->getInfo();
  636. if (!$aProfileInfo || getLoggedId() != $aProfileInfo['account_id'])
  637. return _t('_sys_txt_access_denied');
  638. return CHECK_ACTION_RESULT_ALLOWED;
  639. }
  640. /**
  641. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  642. */
  643. static public function isAllowedDelete ($iProfileId, $aContentInfo, $isPerformAction = false)
  644. {
  645. $iAccountId = (int)BxDolProfile::getInstance($iProfileId)->getAccountId();
  646. if(isAdmin($iAccountId) && $iAccountId == (int)$aContentInfo['id'])
  647. return _t('_sys_txt_access_denied');
  648. $aCheck = checkActionModule($iProfileId, 'delete account', 'system', $isPerformAction);
  649. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  650. return $aCheck[CHECK_ACTION_MESSAGE];
  651. return CHECK_ACTION_RESULT_ALLOWED;
  652. }
  653. }
  654. /** @} */