BxDolAccount.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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. $this->_oQuery->logPassword($iId);
  210. $iPasswordExpired = $this->getPasswordExpiredDateByAccount($iAccountId);
  211. if((int)$this->_oQuery->updatePassword($sPasswordHash, $sSalt, $iId, $iPasswordExpired) > 0) {
  212. bx_alert('account', 'edited', $iId, $oAccountSender ? $oAccountSender->id() : $iId, array('action' => 'reset_password'));
  213. $this->doAudit($iId, '_sys_audit_action_account_reset_password');
  214. return true;
  215. }
  216. return false;
  217. }
  218. /**
  219. * Set account phone
  220. * @param string $sPhone - phone number
  221. * @param int $iAccountId - optional account id
  222. * @return true on success or false on error
  223. */
  224. public function updatePhone($sPhone, $iAccountId = false)
  225. {
  226. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  227. if ($this->_oQuery->updatePhone($sPhone, $iId)) {
  228. bx_alert('account', 'set_phone', $iId);
  229. return true;
  230. }
  231. return false;
  232. }
  233. /**
  234. * Switch context automatically to the first available profile
  235. * @param $iProfileIdFilterOut profile ID to exclude from the list of possible profiles
  236. * @param $iAccountId account ID to use istead of current account
  237. * @return true on success or false on error
  238. */
  239. public function updateProfileContextAuto($iProfileIdFilterOut = false, $iAccountId = false)
  240. {
  241. $oAccount = (!$iAccountId || $iAccountId == $this->_iAccountID ? $this : BxDolAccount::getInstance ($iAccountId));
  242. if (!$oAccount)
  243. return false;
  244. $aAccountInfo = $oAccount->getInfo();
  245. $aProfiles = $oAccount->getProfiles();
  246. $oProfileAccount = BxDolProfile::getInstanceAccountProfile($oAccount->id());
  247. // unset deleted profile and account profile
  248. if ($iProfileIdFilterOut)
  249. unset($aProfiles[$iProfileIdFilterOut]);
  250. unset($aProfiles[$oProfileAccount->id()]);
  251. if ($aProfiles) {
  252. // try to use another profile
  253. reset($aProfiles);
  254. $iProfileId = key($aProfiles);
  255. }
  256. else {
  257. // if no profiles exist, use account profile
  258. $iProfileId = $oProfileAccount->id();
  259. }
  260. return $oAccount->updateProfileContext($iProfileId);
  261. }
  262. public function updateProfileContext($iSwitchToProfileId, $iAccountId = false)
  263. {
  264. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  265. $aInfo = $this->getInfo((int)$iId);
  266. if (!$aInfo)
  267. return false;
  268. $ret = null;
  269. bx_alert('account', 'before_switch_context', $iId, $iSwitchToProfileId, array('profile_id_current' => $aInfo['profile_id'], 'override_result' => &$ret));
  270. if ($ret !== null)
  271. return $ret;
  272. if (!$this->_oQuery->updateCurrentProfile($iId, $iSwitchToProfileId))
  273. return false;
  274. $this->_aInfo = false;
  275. bx_alert('account', 'switch_context', $iId, $iSwitchToProfileId, array('profile_id_old' => $aInfo['profile_id']));
  276. return true;
  277. }
  278. /**
  279. * Send "confirmation" email
  280. */
  281. public function sendConfirmationEmail($iAccountId = false)
  282. {
  283. $iAccountId = (int)$iAccountId;
  284. if(!$iAccountId)
  285. $iAccountId = $this->_iAccountID;
  286. $aAccountInfo = $this->getInfo($iAccountId);
  287. if(empty($aAccountInfo) || !is_array($aAccountInfo))
  288. return false;
  289. $oKey = BxDolKey::getInstance();
  290. $sConfirmationCode = $oKey->getNewKey(array('account_id' => $iAccountId));
  291. $sConfirmationLink = bx_append_url_params(BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email'), array('code' => $sConfirmationCode));
  292. $aReplaceVars = array(
  293. 'name' => $this->getDisplayName($iAccountId),
  294. 'email' => $aAccountInfo['email'],
  295. 'conf_code' => $sConfirmationCode,
  296. 'conf_link' => $sConfirmationLink,
  297. 'conf_form_link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email')
  298. );
  299. $bResult = sendMailTemplate('t_Confirmation', $iAccountId, (int)$aAccountInfo['profile_id'], $aReplaceVars, BX_EMAIL_SYSTEM);
  300. if($bResult)
  301. $this->doAudit($iAccountId, '_sys_audit_action_account_resend_confirmation_email');
  302. return $bResult;
  303. }
  304. public function sendResetPasswordEmail($iAccountId = false)
  305. {
  306. $iAccountId = (int)$iAccountId;
  307. if(!$iAccountId)
  308. $iAccountId = $this->_iAccountID;
  309. $aAccountInfo = $this->getInfo($iAccountId);
  310. if(empty($aAccountInfo) || !is_array($aAccountInfo))
  311. return false;
  312. $sKey = bx_get_reset_password_key($aAccountInfo['email']);
  313. $aReplaceVars = array(
  314. 'name' => $this->getDisplayName($iAccountId),
  315. 'email' => $aAccountInfo['email'],
  316. 'key' => $sKey,
  317. 'forgot_password_url' => bx_get_reset_password_link_by_key($sKey)
  318. );
  319. return sendMailTemplate('t_Forgot', $iAccountId, (int)$aAccountInfo['profile_id'], $aReplaceVars, BX_EMAIL_SYSTEM);
  320. }
  321. /**
  322. * Get account info
  323. */
  324. public function getInfo($iAccountId = false)
  325. {
  326. if ($iAccountId && $iAccountId != $this->_iAccountID)
  327. return $this->_oQuery->getInfoById((int)$iAccountId ? (int)$iAccountId : $this->_iAccountID);
  328. if ($this->_aInfo)
  329. return $this->_aInfo;
  330. $this->_aInfo = $this->_oQuery->getInfoById($this->_iAccountID);
  331. return $this->_aInfo;
  332. }
  333. /**
  334. * Get account display name
  335. */
  336. public function getDisplayName($iAccountId = false)
  337. {
  338. $aInfo = $this->getInfo($iAccountId);
  339. $sDisplayName = !empty($aInfo['name']) ? $aInfo['name'] : _t('_sys_txt_user_n', $aInfo['id']);
  340. bx_alert('account', 'account_name', $iAccountId, 0, array('info' => $aInfo, 'display_name' => &$sDisplayName));
  341. return bx_process_output($sDisplayName);
  342. }
  343. /**
  344. * Get account url
  345. */
  346. public function getUrl($iAccountId = false)
  347. {
  348. return 'javascript:void(0);';
  349. }
  350. /**
  351. * Get account url
  352. */
  353. public function getUnit($iAccountId = false, $aParams = array())
  354. {
  355. $sTemplate = 'unit';
  356. $sTemplateSize = false;
  357. $aTemplateVars = array();
  358. if(!empty($aParams['template'])) {
  359. if(is_string($aParams['template']))
  360. $sTemplate = $aParams['template'];
  361. else if(is_array($aParams['template'])) {
  362. if(!empty($aParams['template']['name']))
  363. $sTemplate = $aParams['template']['name'];
  364. if(!empty($aParams['template']['size']))
  365. $sTemplateSize = $aParams['template']['size'];
  366. if(!empty($aParams['template']['vars']))
  367. $aTemplateVars = $aParams['template']['vars'];
  368. }
  369. }
  370. $sTemplate = 'account_' . $sTemplate . '.html';
  371. if(empty($sTemplateSize))
  372. $sTemplateSize = 'thumb';
  373. $sTitle = $this->getDisplayName($iAccountId);
  374. $aTmplVars = array(
  375. 'size' => $sTemplateSize,
  376. 'color' => implode(', ', BxDolTemplate::getColorCode(($iAccountId ? $iAccountId : $this->_iAccountID), 1.0)),
  377. 'letter' => mb_strtoupper(mb_substr($sTitle, 0, 1)),
  378. 'content_url' => $this->getUrl($iAccountId),
  379. 'title' => $sTitle,
  380. 'title_attr' => bx_html_attribute($sTitle),
  381. 'bx_if:show_online' => array(
  382. 'condition' => $this->isOnline($iAccountId),
  383. 'content' => array()
  384. )
  385. );
  386. if(!empty($aTemplateVars) && is_array($aTemplateVars))
  387. $aTmplVars = array_merge ($aTmplVars, $aTemplateVars);
  388. return BxDolTemplate::getInstance()->parseHtmlByName($sTemplate, $aTmplVars);
  389. }
  390. /**
  391. * Get picture url
  392. */
  393. public function getPicture($iAccountId = false)
  394. {
  395. return BxDolTemplate::getInstance()->getImageUrl('account.svg');
  396. }
  397. /**
  398. * Get big (2x) avatar url
  399. */
  400. public function getAvatarBig($iAccountId = false)
  401. {
  402. return BxDolTemplate::getInstance()->getImageUrl('account.svg');
  403. }
  404. /**
  405. * Get avatar url
  406. */
  407. public function getAvatar($iAccountId = false)
  408. {
  409. return BxDolTemplate::getInstance()->getImageUrl('account.svg');
  410. }
  411. /**
  412. * Get thumb picture url
  413. */
  414. public function getThumb($iAccountId = false)
  415. {
  416. return BxDolTemplate::getInstance()->getImageUrl('account.svg');
  417. }
  418. /**
  419. * Get icon picture url
  420. */
  421. public function getIcon($iAccountId = false)
  422. {
  423. return BxDolTemplate::getInstance()->getImageUrl('account.svg');
  424. }
  425. /**
  426. * Get account email
  427. */
  428. public function getEmail($iAccountId = false)
  429. {
  430. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  431. $aAccountInfo = $this->getInfo($iAccountId);
  432. return $aAccountInfo['email'];
  433. }
  434. /**
  435. * Get language ID
  436. */
  437. public function getLanguageId($iAccountId = false)
  438. {
  439. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  440. $aAccountInfo = $this->getInfo($iAccountId);
  441. return $aAccountInfo['lang_id'];
  442. }
  443. /**
  444. * Is account online
  445. */
  446. public function isOnline($iAccountId = false)
  447. {
  448. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  449. return $this->_oQuery->isOnline($iAccountId);
  450. }
  451. /**
  452. * Validate account.
  453. * @param $s - account identifier (id or email)
  454. * @return account id or false if account was not found
  455. */
  456. static public function getID($s)
  457. {
  458. $oQuery = BxDolAccountQuery::getInstance();
  459. bx_import('BxDolForm');
  460. if (BxDolFormCheckerHelper::checkEmail($s)) {
  461. $iId = (int)$oQuery->getIdByEmail($s);
  462. return $iId ? $iId : false;
  463. }
  464. if (preg_match("/^\+[0-9\s]+$/", $s)) {
  465. $iId = (int)$oQuery->getIdByPhone($s);
  466. return $iId ? $iId : false;
  467. }
  468. $iId = $oQuery->getIdById((int)$s);
  469. return $iId ? $iId : false;
  470. }
  471. /**
  472. * Check if profiles limit reached
  473. */
  474. public function isProfilesLimitReached ()
  475. {
  476. $iProfilesLimit = (int)getParam('sys_account_limit_profiles_number');
  477. bx_alert('account', 'get_limit_profiles_number', 0, 0, array('account_id' => $this->_iAccountID, 'number' => &$iProfilesLimit));
  478. if (!isAdmin() && $iProfilesLimit && ($iProfilesNum = $this->getProfilesNumber()) && $iProfilesNum >= $iProfilesLimit)
  479. return true;
  480. return false;
  481. }
  482. /**
  483. * Get number of profiles associated with the account
  484. */
  485. public function getProfilesNumber ($isFilterNonSwitchableProfiles = true)
  486. {
  487. $a = $this->getProfilesIds($isFilterNonSwitchableProfiles);
  488. return count($a);
  489. }
  490. /**
  491. * Get all profile ids associated with the account
  492. */
  493. public function getProfilesIds ($isFilterNonSwitchableProfiles = true, $isFilterSystemProfiles = true)
  494. {
  495. $a = $this->getProfiles ($isFilterNonSwitchableProfiles, $isFilterSystemProfiles);
  496. return $a ? array_keys($a) : array();
  497. }
  498. /**
  499. * Get all profiles associated with the account
  500. */
  501. public function getProfiles ($isFilterNonSwitchableProfiles = true, $isFilterSystemProfiles = true)
  502. {
  503. $oProfileQuery = BxDolProfileQuery::getInstance();
  504. $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
  505. if ($isFilterNonSwitchableProfiles) {
  506. foreach ($aProfiles as $iProfileId => $aProfile) {
  507. if ('system' == $aProfile['type'])
  508. continue;
  509. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  510. unset($aProfiles[$iProfileId]);
  511. }
  512. }
  513. if ($isFilterSystemProfiles) {
  514. foreach ($aProfiles as $iProfileId => $aProfile) {
  515. if ('system' == $aProfile['type'])
  516. unset($aProfiles[$iProfileId]);
  517. }
  518. }
  519. return $aProfiles;
  520. }
  521. /**
  522. * Delete profile.
  523. * @param $bDeleteWithContent - delete associated profiles with all its contents
  524. */
  525. function delete($bDeleteWithContent = false)
  526. {
  527. $aAccountInfo = $this->_oQuery->getInfoById($this->_iAccountID);
  528. if (!$aAccountInfo)
  529. return false;
  530. // create system event before deletion
  531. $isStopDeletion = false;
  532. bx_alert('account', 'before_delete', $this->_iAccountID, 0, array('delete_with_content' => $bDeleteWithContent, 'stop_deletion' => &$isStopDeletion));
  533. if ($isStopDeletion)
  534. return false;
  535. $oAccountQuery = BxDolAccountQuery::getInstance();
  536. $oProfileQuery = BxDolProfileQuery::getInstance();
  537. $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
  538. foreach ($aProfiles as $iProfileId => $aRow) {
  539. $oProfile = BxDolProfile::getInstance($iProfileId);
  540. if (!$oProfile)
  541. continue;
  542. $oProfile->delete(false, $bDeleteWithContent, true);
  543. }
  544. // delete profile
  545. if (!$oAccountQuery->delete($this->_iAccountID))
  546. return false;
  547. // unset class instance to prevent creating the instance again
  548. $sClass = __CLASS__ . '_' . $this->_iAccountID;
  549. unset($GLOBALS['bxDolClasses'][$sClass]);
  550. // create system event
  551. bx_alert('account', 'delete', $this->_iAccountID, 0, array ('delete_with_content' => $bDeleteWithContent));
  552. $this->doAudit($this->_iAccountID, $bDeleteWithContent ? '_sys_audit_action_account_deleted_with_content' : '_sys_audit_action_account_deleted');
  553. return true;
  554. }
  555. /**
  556. * Add permament messages.
  557. */
  558. public function addInformerPermanentMessages ($oInformer)
  559. {
  560. if (!$this->isConfirmed()) {
  561. if (!$this->isConfirmedEmail()) {
  562. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email') . '&resend=1';
  563. $aAccountInfo = $this->getInfo();
  564. $oInformer->add('sys-account-unconfirmed-email', _t('_sys_txt_account_unconfirmed_email', $sUrl, $aAccountInfo['email']), BX_INFORMER_ALERT);
  565. }
  566. if (!$this->isConfirmedPhone()) {
  567. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-phone') . '';
  568. $aAccountInfo = $this->getInfo();
  569. $oInformer->add('sys-account-unconfirmed-phone', _t('_sys_txt_account_unconfirmed_phone', $sUrl), BX_INFORMER_ALERT);
  570. }
  571. }
  572. $this->isNeedChangePassword();
  573. }
  574. /**
  575. * Get unsubscribe link for the specified mesage type
  576. */
  577. public function getUnsubscribeLink($iEmailType, $iAccountId = false)
  578. {
  579. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  580. $sUrl = '';
  581. switch ($iEmailType) {
  582. case BX_EMAIL_NOTIFY:
  583. $sUrl = 'page.php?i=unsubscribe-notifications';
  584. break;
  585. case BX_EMAIL_MASS:
  586. $sUrl = 'page.php?i=unsubscribe-news';
  587. break;
  588. default:
  589. return '';
  590. }
  591. return BxDolPermalinks::getInstance()->permalink($sUrl) . '&id=' . $iAccountId . '&code=' . $this->getEmailHash();
  592. }
  593. public function getEmailHash($iAccountId = false)
  594. {
  595. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  596. $a = $this->getInfo();
  597. return md5($a['email'] . $a['salt'] . BX_DOL_SECRET);
  598. }
  599. public function getPasswordExpiredDate($iPasswordExpiredForMembership, $iAccountId = false)
  600. {
  601. if ($iPasswordExpiredForMembership == 0)
  602. return 0;
  603. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  604. $aAccountInfo = $this->_oQuery->getInfoById($iAccountId);
  605. $iLastPassChanged = $this->_oQuery->getLastPasswordChanged($iAccountId);
  606. if ($iLastPassChanged == 0)
  607. $iLastPassChanged = $aAccountInfo['added'];
  608. return $iPasswordExpiredForMembership * 86400 + $iLastPassChanged;
  609. }
  610. public function getPasswordExpiredDateByAccount($iAccountId = false)
  611. {
  612. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  613. $oACL = BxDolAcl::getInstance();
  614. $aProfiles = BxDolAccount::getInstance($iAccountId)->getProfiles();
  615. $iPasswordExpiredForMembership = 0;
  616. foreach ($aProfiles as $aProfile) {
  617. $aMembersipInfo = $oACL->getMemberMembershipInfo($aProfile['id']);
  618. $Memberships = [];
  619. BxDolAclQuery::getInstance()->getLevels(['type' => 'by_id', 'value' => $aMembersipInfo['id']], $aMembership);
  620. if($aMembership['password_expired'] > 0){
  621. if ($iPasswordExpiredForMembership > 0 && $aMembership['password_expired'] < $iExpired)
  622. $iPasswordExpiredForMembership = $aMembership['password_expired'];
  623. if ($iPasswordExpiredForMembership == 0 )
  624. $iPasswordExpiredForMembership = $aMembership['password_expired'];
  625. }
  626. }
  627. return $this->getPasswordExpiredDate($iPasswordExpiredForMembership, $iAccountId);
  628. }
  629. public function isNeedChangePassword($iAccountId = false)
  630. {
  631. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  632. $aAccountInfo = $this->getInfo();
  633. list($sPageLink, $aPageParams) = bx_get_base_url_inline();
  634. $bNeedRedirectToChangePassword = true;
  635. if (isset($aPageParams['i']) && $aPageParams['i'] == 'account-settings-password')
  636. $bNeedRedirectToChangePassword = false;
  637. bx_alert('account', 'is_need_to_change_password', $iAccountId, false, array('override_result' => &$bNeedRedirectToChangePassword));
  638. if ($aAccountInfo['password_expired'] >0 && $aAccountInfo['password_expired'] < time() && $bNeedRedirectToChangePassword){
  639. if (getParam('sys_account_accounts_force_password_change_after_expiration') == 'on'){
  640. header('Location: ' . BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=account-settings-password'));
  641. exit;
  642. }
  643. else{
  644. $oInformer = BxDolInformer::getInstance(BxDolTemplate::getInstance());
  645. $oInformer->add('sys-account-need-to-change-password', _t('_sys_txt_account_need_to_change_password', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=account-settings-password')), BX_INFORMER_ALERT);
  646. }
  647. }
  648. }
  649. public function doAudit($iAccountId, $sAction, $aData = array())
  650. {
  651. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  652. bx_audit(
  653. $iAccountId,
  654. 'bx_accounts',
  655. $sAction,
  656. array('content_title' => $this->getEmail(), 'data' => $aData)
  657. );
  658. }
  659. /**
  660. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  661. */
  662. static public function isAllowedCreate ($iProfileId, $isPerformAction = false)
  663. {
  664. $aCheck = checkActionModule($iProfileId, 'create account', 'system', $isPerformAction);
  665. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  666. return MsgBox($aCheck[CHECK_ACTION_MESSAGE]);
  667. return CHECK_ACTION_RESULT_ALLOWED;
  668. }
  669. static public function isAllowedCreateMultiple ($iProfileId, $isPerformAction = false)
  670. {
  671. $iLimit = (int)getParam('sys_account_limit_profiles_number');
  672. $bResult = false;
  673. if(isAdmin() || $iLimit != 1)
  674. $bResult = true;
  675. $oProfile = BxDolProfile::getInstance($iProfileId);
  676. if($iLimit == 1 && $oProfile->getModule() == 'system')
  677. $bResult = true;
  678. if($oProfile && $oProfile->getAccountId() != getLoggedId())
  679. $bResult = false;
  680. bx_alert('profile', 'allow_create_another_profile', $iProfileId, 0, [
  681. 'override_result' => &$bResult
  682. ]);
  683. return $bResult;
  684. }
  685. /**
  686. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  687. */
  688. static public function isAllowedEdit ($iProfileId, $aContentInfo, $isPerformAction = false)
  689. {
  690. $oProfile = BxDolProfile::getInstance($iProfileId);
  691. if (!$oProfile)
  692. return _t('_sys_txt_access_denied');
  693. $aProfileInfo = $oProfile->getInfo();
  694. if (!$aProfileInfo || getLoggedId() != $aProfileInfo['account_id'])
  695. return _t('_sys_txt_access_denied');
  696. return CHECK_ACTION_RESULT_ALLOWED;
  697. }
  698. /**
  699. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  700. */
  701. static public function isAllowedDelete ($iProfileId, $aContentInfo, $isPerformAction = false)
  702. {
  703. $iAccountId = (int)BxDolProfile::getInstance($iProfileId)->getAccountId();
  704. if(isAdmin($iAccountId) && $iAccountId == (int)$aContentInfo['id'])
  705. return _t('_sys_txt_account_cannot_delete');
  706. $aCheck = checkActionModule($iProfileId, 'delete account', 'system', $isPerformAction);
  707. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  708. return $aCheck[CHECK_ACTION_MESSAGE];
  709. return CHECK_ACTION_RESULT_ALLOWED;
  710. }
  711. static public function pruning ()
  712. {
  713. $iCount = 0;
  714. $iTime = time() - getParam('sys_account_accounts_pruning_interval') * 86400;
  715. $oAccountQuery = BxDolAccountQuery::getInstance();
  716. $aAccounts = [];
  717. $bSuspend = false;
  718. switch(getParam('sys_account_accounts_pruning')) {
  719. case 'no_login_delete':
  720. $aAccounts = $oAccountQuery->getAccountsForPruning('no_login', $iTime);
  721. break;
  722. case 'no_login_suspend':
  723. $bSuspend = true;
  724. $aAccounts = $oAccountQuery->getAccountsForPruning('no_login', $iTime);
  725. break;
  726. case 'no_confirm_delete':
  727. $aAccounts = $oAccountQuery->getAccountsForPruning('no_confirm', $iTime);
  728. foreach ($aAccounts as $k => $aAccount) {
  729. $oAccount = BxDolAccount::getInstance($aAccount['id']);
  730. if($oAccount->isConfirmed()){
  731. unset($aAccounts[$k]);
  732. }
  733. }
  734. break;
  735. case 'no_profile_delete':
  736. $aAccounts = $oAccountQuery->getAccountsForPruning('no_profile', $iTime);
  737. break;
  738. }
  739. if ($bSuspend){
  740. foreach ($aAccounts as $k => $aAccount) {
  741. $oAccount = BxDolAccount::getInstance($aAccount['id']);
  742. $oProfile = BxDolProfile::getInstanceAccountProfile($aAccount['id']);
  743. $oProfile->suspend(BX_PROFILE_ACTION_AUTO, 0 ,false);
  744. $aProfiles = $oAccount->getProfiles();
  745. foreach($aProfiles as $aProfile){
  746. BxDolProfile::getInstance($aProfile['id'])->suspend(BX_PROFILE_ACTION_AUTO, 0 ,false);
  747. }
  748. $iCount++;
  749. }
  750. }
  751. else{
  752. foreach ($aAccounts as $k => $aAccount) {
  753. $oAccount = BxDolAccount::getInstance($aAccount['id']);
  754. $oAccount->delete(false);
  755. $iCount++;
  756. }
  757. }
  758. return $iCount;
  759. }
  760. }
  761. /** @} */