BxDolAccount.php 25 KB

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