BxDolAccount.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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. /**
  103. * @hooks
  104. * @hookdef hook-account-is_confirmed 'account', 'is_confirmed' - hook in $oAccount->isConfirmed check
  105. * - $unit_name - equals `account`
  106. * - $action - equals `is_confirmed`
  107. * - $object_id - account id
  108. * - $sender_id - not used
  109. * - $extra_params - array of additional params with the following array keys:
  110. * - `type` - [string] confirmation type can be none/phone/email/email_and_phone/email_or_phone
  111. * - `override_result` - [bool] by ref, if account confirmed = true, otherwise false, can be overridden in hook processing
  112. * @hook @ref hook-account-is_confirmed
  113. */
  114. bx_alert('account', 'is_confirmed', $iId, false, array('type' => $sConfirmationType, 'override_result' => &$bResult));
  115. return $bResult;
  116. }
  117. public function getCurrentConfirmationStatusValue($iAccountId = false)
  118. {
  119. $a = $this->getInfo((int)$iAccountId);
  120. $sTmp = $a['email_confirmed'] . $a['phone_confirmed'];
  121. switch ($sTmp) {
  122. case '01':
  123. return BX_ACCOUNT_CONFIRMATION_PHONE;
  124. case '10':
  125. return BX_ACCOUNT_CONFIRMATION_EMAIL;
  126. case '11':
  127. return BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE;
  128. }
  129. return BX_ACCOUNT_CONFIRMATION_NONE;
  130. }
  131. public function isConfirmedEmail($iAccountId = false)
  132. {
  133. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  134. $bResult = false;
  135. if(self::isNeedConfirmEmail()) {
  136. $a = $this->getInfo($iId);
  137. $bResult = $a['email_confirmed'] ? true : false;
  138. }
  139. else
  140. $bResult = true;
  141. /**
  142. * @hooks
  143. * @hookdef hook-account-is_confirmed 'account', 'is_confirmed_email' - hook in $oAccount->isConfirmedEmail check
  144. * - $unit_name - equals `account`
  145. * - $action - equals `is_confirmed_email`
  146. * - $object_id - account id
  147. * - $sender_id - not used
  148. * - $extra_params - array of additional params with the following array keys:
  149. * - `override_result` - [bool] by ref, if email confirmed = true, otherwise false, can be overridden in hook processing
  150. * @hook @ref hook-account-is_confirmed_email
  151. */
  152. bx_alert('account', 'is_confirmed_email', $iId, false, array('override_result' => &$bResult));
  153. return $bResult;
  154. }
  155. public function isConfirmedPhone($iAccountId = false)
  156. {
  157. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  158. $bResult = false;
  159. if(self::isNeedConfirmPhone()) {
  160. $a = $this->getInfo((int)$iAccountId);
  161. $bResult = $a['phone_confirmed'] ? true : false;
  162. }
  163. else
  164. $bResult = true;
  165. /**
  166. * @hooks
  167. * @hookdef hook-account-is_confirmed_phone 'account', 'is_confirmed_phone' - hook in $oAccount->isConfirmedPhone check
  168. * - $unit_name - equals `account`
  169. * - $action - equals `is_confirmed_phone`
  170. * - $object_id - account id
  171. * - $sender_id - not used
  172. * - $extra_params - array of additional params with the following array keys:
  173. * - `override_result` - [bool] by ref, if phone confirmed = true, otherwise false, can be overridden in hook processing
  174. * @hook @ref hook-account-is_confirmed_phone
  175. */
  176. bx_alert('account', 'is_confirmed_phone', $iId, false, array('override_result' => &$bResult));
  177. return $bResult;
  178. }
  179. static public function isNeedConfirmEmail()
  180. {
  181. if(in_array(getParam('sys_account_confirmation_type'), array(BX_ACCOUNT_CONFIRMATION_EMAIL, BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE, BX_ACCOUNT_CONFIRMATION_EMAIL_OR_PHONE)))
  182. return true;
  183. return false;
  184. }
  185. static public function isNeedConfirmPhone()
  186. {
  187. if(in_array(getParam('sys_account_confirmation_type'), array(BX_ACCOUNT_CONFIRMATION_PHONE, BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE, BX_ACCOUNT_CONFIRMATION_EMAIL_OR_PHONE)))
  188. return true;
  189. return false;
  190. }
  191. public function isLocked($iAccountId = false)
  192. {
  193. $a = $this->getInfo((int)$iAccountId);
  194. return $a['locked'] ? true : false;
  195. }
  196. //
  197. /**
  198. * Set account email to confirmed or unconfirmed
  199. * @param int $isConfirmed - false: mark email as unconfirmed, true: as confirmed
  200. * @param int $iAccountId - optional account id
  201. * @return true on success or false on error
  202. */
  203. public function updateEmailConfirmed($isConfirmed, $isAutoSendConfrmationEmail = true, $iAccountId = false)
  204. {
  205. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  206. if (!$isConfirmed && $isAutoSendConfrmationEmail && self::isNeedConfirmEmail()) // if email_confirmation procedure is enabled - send email confirmation letter
  207. $this->sendConfirmationEmail($iId);
  208. if ($this->_oQuery->updateEmailConfirmed($isConfirmed, $iId)) {
  209. $this->_aInfo = false;
  210. /**
  211. * @hooks
  212. * @hookdef hook-account-confirm 'account', 'confirm' - hook in email confirmation $oAccount->updateEmailConfirmed
  213. * - $unit_name - equals `account`
  214. * - $action - can be confirm/unconfirm
  215. * - $object_id - account id
  216. * - $sender_id - not used
  217. * - $extra_params - not used
  218. * @hook @ref hook-account-confirm
  219. */
  220. bx_alert('account', $this->isConfirmed() ? 'confirm' : 'unconfirm', $iId);
  221. return true;
  222. }
  223. return false;
  224. }
  225. /**
  226. * Set account phone to confirmed or unconfirmed
  227. * @param int $isConfirmed - false: mark phone as unconfirmed, true: as confirmed
  228. * @param int $iAccountId - optional account id
  229. * @return true on success or false on error
  230. */
  231. public function updatePhoneConfirmed($isConfirmed, $iAccountId = false)
  232. {
  233. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  234. if ($this->_oQuery->updatePhoneConfirmed($isConfirmed, $iId)) {
  235. $this->_aInfo = false;
  236. bx_alert('account', $this->isConfirmed() ? 'confirm' : 'unconfirm', $iId);
  237. return true;
  238. }
  239. return false;
  240. }
  241. /**
  242. * Change account password
  243. * @param string $sPassword - new password
  244. * @param int $iAccountId - optional account id
  245. * @return true on success or false on error
  246. */
  247. public function updatePassword($sPassword, $iAccountId = false)
  248. {
  249. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  250. $sSalt = genRndSalt();
  251. $sPasswordHash = encryptUserPwd($sPassword, $sSalt);
  252. $this->_oQuery->logPassword($iId);
  253. if((int)$this->_oQuery->updatePassword($sPasswordHash, $sSalt, $iId) > 0) {
  254. /**
  255. * @hooks
  256. * @hookdef hook-account-edited 'account', 'edited' - hook on account edited $oAccount->updatePassword
  257. * - $unit_name - equals `account`
  258. * - $action - equals edited
  259. * - $object_id - account id
  260. * - $sender_id - account sender id
  261. * - $extra_params - array of additional params with the following array keys:
  262. * - `action` - [string] action's name, can be reset_password
  263. * @hook @ref hook-account-edited
  264. */
  265. bx_alert('account', 'edited', $iId, ($iSenderId = getLoggedId()) != 0 ? $iSenderId : $iId, [
  266. 'action' => 'reset_password'
  267. ]);
  268. $this->doAudit($iId, '_sys_audit_action_account_reset_password');
  269. return true;
  270. }
  271. return false;
  272. }
  273. /**
  274. * Set account phone
  275. * @param string $sPhone - phone number
  276. * @param int $iAccountId - optional account id
  277. * @return true on success or false on error
  278. */
  279. public function updatePhone($sPhone, $iAccountId = false)
  280. {
  281. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  282. if ($this->_oQuery->updatePhone($sPhone, $iId)) {
  283. /**
  284. * @hooks
  285. * @hookdef hook-account-set_phone 'account', 'set_phone' - hook after accout password changed
  286. * - $unit_name - equals `account`
  287. * - $action - equals `set_phone`
  288. * - $object_id - account id
  289. * - $sender_id - not used
  290. * - $extra_params - not used
  291. * @hook @ref hook-account-set_phone
  292. */
  293. bx_alert('account', 'set_phone', $iId);
  294. return true;
  295. }
  296. return false;
  297. }
  298. /**
  299. * Switch context automatically to the first available profile
  300. * @param $iProfileIdFilterOut profile ID to exclude from the list of possible profiles
  301. * @param $iAccountId account ID to use istead of current account
  302. * @return true on success or false on error
  303. */
  304. public function updateProfileContextAuto($iProfileIdFilterOut = false, $iAccountId = false)
  305. {
  306. $oAccount = (!$iAccountId || $iAccountId == $this->_iAccountID ? $this : BxDolAccount::getInstance ($iAccountId));
  307. if (!$oAccount)
  308. return false;
  309. $aAccountInfo = $oAccount->getInfo();
  310. $aProfiles = $oAccount->getProfiles();
  311. $oProfileAccount = BxDolProfile::getInstanceAccountProfile($oAccount->id());
  312. // unset deleted profile and account profile
  313. if ($iProfileIdFilterOut)
  314. unset($aProfiles[$iProfileIdFilterOut]);
  315. unset($aProfiles[$oProfileAccount->id()]);
  316. if ($aProfiles) {
  317. // try to use another profile
  318. reset($aProfiles);
  319. $iProfileId = key($aProfiles);
  320. }
  321. else {
  322. // if no profiles exist, use account profile
  323. $iProfileId = $oProfileAccount->id();
  324. }
  325. return $oAccount->updateProfileContext($iProfileId);
  326. }
  327. public function updateProfileContext($iSwitchToProfileId, $iAccountId = false)
  328. {
  329. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  330. $aInfo = $this->getInfo((int)$iId);
  331. if (!$aInfo)
  332. return false;
  333. $ret = null;
  334. /**
  335. * @hooks
  336. * @hookdef hook-account-before_switch_context 'account', 'before_switch_context' - hook before switch profile_id frof current logged user
  337. * - $unit_name - equals `account`
  338. * - $action - equals `before_switch_context`
  339. * - $object_id - account id
  340. * - $sender_id - profile_id to switch to
  341. * - $extra_params - array of additional params with the following array keys:
  342. * - `profile_id_current` - [int] current profile_id
  343. * - `override_result` - [int] by ref, profile_id to switch to, can be overridden in hook processing
  344. * @hook @ref hook-account-before_switch_context
  345. */
  346. bx_alert('account', 'before_switch_context', $iId, $iSwitchToProfileId, array('profile_id_current' => $aInfo['profile_id'], 'override_result' => &$ret));
  347. if ($ret !== null)
  348. return $ret;
  349. if (!$this->_oQuery->updateCurrentProfile($iId, $iSwitchToProfileId))
  350. return false;
  351. $this->_aInfo = false;
  352. /**
  353. * @hooks
  354. * @hookdef hook-account-switch_context 'account', 'switch_context' - hook before switch profile_id frof current logged user
  355. * - $unit_name - equals `account`
  356. * - $action - equals `switch_context`
  357. * - $object_id - account id
  358. * - $sender_id - profile_id to switch to
  359. * - $extra_params - array of additional params with the following array keys:
  360. * - `profile_id_old` - [int] old profile_id
  361. * @hook @ref hook-account-switch_context
  362. */
  363. bx_alert('account', 'switch_context', $iId, $iSwitchToProfileId, array('profile_id_old' => $aInfo['profile_id']));
  364. return true;
  365. }
  366. /**
  367. * Send "confirmation" email
  368. */
  369. public function sendConfirmationEmail($iAccountId = false)
  370. {
  371. $iAccountId = (int)$iAccountId;
  372. if(!$iAccountId)
  373. $iAccountId = $this->_iAccountID;
  374. $aAccountInfo = $this->getInfo($iAccountId);
  375. if(empty($aAccountInfo) || !is_array($aAccountInfo))
  376. return false;
  377. $oKey = BxDolKey::getInstance();
  378. $sConfirmationCode = $oKey->getNewKeyNumeric(['account_id' => $iAccountId]);
  379. $sConfirmationLink = bx_append_url_params(BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email'), ['code' => $sConfirmationCode]);
  380. $sEmailTemplate = 't_Confirmation';
  381. $aEmailReplaceVars = [
  382. 'name' => $this->getDisplayName($iAccountId),
  383. 'email' => $aAccountInfo['email'],
  384. 'conf_code' => $sConfirmationCode,
  385. 'conf_link' => $sConfirmationLink,
  386. 'conf_form_link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email')
  387. ];
  388. $bResult = sendMailTemplate($sEmailTemplate, $iAccountId, (int)$aAccountInfo['profile_id'], $aEmailReplaceVars, BX_EMAIL_SYSTEM);
  389. if($bResult)
  390. $this->doAudit($iAccountId, '_sys_audit_action_account_resend_confirmation_email');
  391. return $bResult;
  392. }
  393. public function sendResetPasswordEmail($iAccountId = false)
  394. {
  395. $iAccountId = (int)$iAccountId;
  396. if(!$iAccountId)
  397. $iAccountId = $this->_iAccountID;
  398. $aAccountInfo = $this->getInfo($iAccountId);
  399. if(empty($aAccountInfo) || !is_array($aAccountInfo))
  400. return false;
  401. $sKey = bx_get_reset_password_key($aAccountInfo['email']);
  402. $aReplaceVars = array(
  403. 'name' => $this->getDisplayName($iAccountId),
  404. 'email' => $aAccountInfo['email'],
  405. 'key' => $sKey,
  406. 'forgot_password_url' => bx_get_reset_password_link_by_key($sKey)
  407. );
  408. return sendMailTemplate('t_Forgot', $iAccountId, (int)$aAccountInfo['profile_id'], $aReplaceVars, BX_EMAIL_SYSTEM);
  409. }
  410. /**
  411. * Get account info
  412. */
  413. public function getInfo($iAccountId = false)
  414. {
  415. if ($iAccountId && $iAccountId != $this->_iAccountID)
  416. return $this->_oQuery->getInfoById((int)$iAccountId ? (int)$iAccountId : $this->_iAccountID);
  417. if ($this->_aInfo)
  418. return $this->_aInfo;
  419. $this->_aInfo = $this->_oQuery->getInfoById($this->_iAccountID);
  420. return $this->_aInfo;
  421. }
  422. /**
  423. * Get account display name
  424. */
  425. public function getDisplayName($iAccountId = false)
  426. {
  427. $aInfo = $this->getInfo($iAccountId);
  428. $sDisplayName = !empty($aInfo['name']) ? $aInfo['name'] : _t('_sys_txt_user_n', $aInfo['id']);
  429. /**
  430. * @hooks
  431. * @hookdef hook-account-account_name 'account', 'account_name' - hook on get account display name
  432. * - $unit_name - equals `account`
  433. * - $action - equals `account_name`
  434. * - $object_id - account id
  435. * - $sender_id - not used
  436. * - $extra_params - array of additional params with the following array keys:
  437. * - `info` - [array] contains account info from $oAccount->getInfo()
  438. * - `display_name` - [string] by ref, account display name, can be overridden in hook processing
  439. * @hook @ref hook-account-account_name
  440. */
  441. bx_alert('account', 'account_name', $iAccountId, 0, array('info' => $aInfo, 'display_name' => &$sDisplayName));
  442. return bx_process_output($sDisplayName);
  443. }
  444. /**
  445. * Get account url
  446. */
  447. public function getUrl($iAccountId = false)
  448. {
  449. return 'javascript:void(0);';
  450. }
  451. /**
  452. * Get account url
  453. */
  454. public function getUnit($iAccountId = false, $aParams = array())
  455. {
  456. $sTemplate = 'unit';
  457. $sTemplateSize = false;
  458. $aTemplateVars = array();
  459. if(!empty($aParams['template'])) {
  460. if(is_string($aParams['template']))
  461. $sTemplate = $aParams['template'];
  462. else if(is_array($aParams['template'])) {
  463. if(!empty($aParams['template']['name']))
  464. $sTemplate = $aParams['template']['name'];
  465. if(!empty($aParams['template']['size']))
  466. $sTemplateSize = $aParams['template']['size'];
  467. if(!empty($aParams['template']['vars']))
  468. $aTemplateVars = $aParams['template']['vars'];
  469. }
  470. }
  471. $sTemplate = 'account_' . $sTemplate . '.html';
  472. if(empty($sTemplateSize))
  473. $sTemplateSize = 'thumb';
  474. $sTitle = $this->getDisplayName($iAccountId);
  475. $aTmplVars = array(
  476. 'size' => $sTemplateSize,
  477. 'color' => implode(', ', BxDolTemplate::getColorCode(($iAccountId ? $iAccountId : $this->_iAccountID), 1.0)),
  478. 'letter' => mb_strtoupper(mb_substr($sTitle, 0, 1)),
  479. 'content_url' => $this->getUrl($iAccountId),
  480. 'title' => $sTitle,
  481. 'title_attr' => bx_html_attribute($sTitle),
  482. 'bx_if:show_online' => array(
  483. 'condition' => $this->isOnline($iAccountId),
  484. 'content' => array()
  485. )
  486. );
  487. if(!empty($aTemplateVars) && is_array($aTemplateVars))
  488. $aTmplVars = array_merge ($aTmplVars, $aTemplateVars);
  489. return BxDolTemplate::getInstance()->parseHtmlByName($sTemplate, $aTmplVars);
  490. }
  491. /**
  492. * Get picture url
  493. */
  494. public function getPicture($iAccountId = false)
  495. {
  496. return BxDolTemplate::getInstance()->getImageUrl('account.svg');
  497. }
  498. /**
  499. * Get big (2x) avatar url
  500. */
  501. public function getAvatarBig($iAccountId = false)
  502. {
  503. return BxDolTemplate::getInstance()->getImageUrl('account.svg');
  504. }
  505. /**
  506. * Get avatar url
  507. */
  508. public function getAvatar($iAccountId = false)
  509. {
  510. return BxDolTemplate::getInstance()->getImageUrl('account.svg');
  511. }
  512. /**
  513. * Get thumb picture url
  514. */
  515. public function getThumb($iAccountId = false)
  516. {
  517. return BxDolTemplate::getInstance()->getImageUrl('account.svg');
  518. }
  519. /**
  520. * Get icon picture url
  521. */
  522. public function getIcon($iAccountId = false)
  523. {
  524. return BxDolTemplate::getInstance()->getImageUrl('account.svg');
  525. }
  526. /**
  527. * Get account email
  528. */
  529. public function getEmail($iAccountId = false)
  530. {
  531. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  532. $aAccountInfo = $this->getInfo($iAccountId);
  533. return $aAccountInfo['email'];
  534. }
  535. /**
  536. * Get language ID
  537. */
  538. public function getLanguageId($iAccountId = false)
  539. {
  540. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  541. $aAccountInfo = $this->getInfo($iAccountId);
  542. return $aAccountInfo['lang_id'];
  543. }
  544. /**
  545. * Is account online
  546. */
  547. public function isOnline($iAccountId = false)
  548. {
  549. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  550. return $this->_oQuery->isOnline($iAccountId);
  551. }
  552. /**
  553. * Validate account.
  554. * @param $s - account identifier (id or email)
  555. * @return account id or false if account was not found
  556. */
  557. static public function getID($s)
  558. {
  559. $oQuery = BxDolAccountQuery::getInstance();
  560. bx_import('BxDolForm');
  561. if (BxDolFormCheckerHelper::checkEmail($s)) {
  562. $iId = (int)$oQuery->getIdByEmail($s);
  563. return $iId ? $iId : false;
  564. }
  565. if (preg_match("/^\+[0-9\s]+$/", $s)) {
  566. $iId = (int)$oQuery->getIdByPhone($s);
  567. return $iId ? $iId : false;
  568. }
  569. $iId = $oQuery->getIdById((int)$s);
  570. return $iId ? $iId : false;
  571. }
  572. /**
  573. * Check if profiles limit reached
  574. */
  575. public function isProfilesLimitReached ()
  576. {
  577. $iProfilesLimit = (int)getParam('sys_account_limit_profiles_number');
  578. /**
  579. * @hooks
  580. * @hookdef hook-account-get_limit_profiles_number 'account', 'get_limit_profiles_number' - hook on get account limit on the number of profiles
  581. * - $unit_name - equals `account`
  582. * - $action - equals `get_limit_profiles_number`
  583. * - $object_id - not used
  584. * - $sender_id - not used
  585. * - $extra_params - array of additional params with the following array keys:
  586. * - `account_id` - [int] account id
  587. * - `number` - [int] by ref, account limit on the number of profiles, can be overridden in hook processing
  588. * @hook @ref hook-account-get_limit_profiles_number
  589. */
  590. bx_alert('account', 'get_limit_profiles_number', 0, 0, array('account_id' => $this->_iAccountID, 'number' => &$iProfilesLimit));
  591. if (!isAdmin() && $iProfilesLimit && ($iProfilesNum = $this->getProfilesNumber()) && $iProfilesNum >= $iProfilesLimit)
  592. return true;
  593. return false;
  594. }
  595. /**
  596. * Get number of profiles associated with the account
  597. */
  598. public function getProfilesNumber ($isFilterNonSwitchableProfiles = true)
  599. {
  600. $a = $this->getProfilesIds($isFilterNonSwitchableProfiles);
  601. return count($a);
  602. }
  603. /**
  604. * Get all profile ids associated with the account
  605. */
  606. public function getProfilesIds ($isFilterNonSwitchableProfiles = true, $isFilterSystemProfiles = true)
  607. {
  608. $a = $this->getProfiles ($isFilterNonSwitchableProfiles, $isFilterSystemProfiles);
  609. return $a ? array_keys($a) : array();
  610. }
  611. /**
  612. * Get all profiles associated with the account
  613. */
  614. public function getProfiles ($isFilterNonSwitchableProfiles = true, $isFilterSystemProfiles = true)
  615. {
  616. $oProfileQuery = BxDolProfileQuery::getInstance();
  617. $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
  618. if ($isFilterNonSwitchableProfiles) {
  619. foreach ($aProfiles as $iProfileId => $aProfile) {
  620. if ('system' == $aProfile['type'])
  621. continue;
  622. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  623. unset($aProfiles[$iProfileId]);
  624. }
  625. }
  626. if ($isFilterSystemProfiles) {
  627. foreach ($aProfiles as $iProfileId => $aProfile) {
  628. if ('system' == $aProfile['type'])
  629. unset($aProfiles[$iProfileId]);
  630. }
  631. }
  632. return $aProfiles;
  633. }
  634. /**
  635. * Delete profile.
  636. * @param $bDeleteWithContent - delete associated profiles with all its contents
  637. * @param $bScheduled - delete account using background jobs.
  638. */
  639. function delete($bDeleteWithContent = false, $bScheduled = false)
  640. {
  641. if ($bScheduled)
  642. return BxDolBackgroundJobs::getInstance()->add('account_delete_' . $this->_iAccountID, ['system', 'account_delete', [$this->_iAccountID, $bDeleteWithContent], 'TemplServiceAccount']);
  643. $aAccountInfo = $this->_oQuery->getInfoById($this->_iAccountID);
  644. if (!$aAccountInfo)
  645. return false;
  646. // create system event before deletion
  647. $isStopDeletion = false;
  648. /**
  649. * @hooks
  650. * @hookdef hook-account-before_delete 'account', 'before_delete' - hook on before delete account,
  651. * - $unit_name - equals `account`
  652. * - $action - equals `before_delete`
  653. * - $object_id - account id
  654. * - $sender_id - not used
  655. * - $extra_params - array of additional params with the following array keys:
  656. * - `delete_with_content` - [bool] if account will delete with content = true, otherwise = false
  657. * - `stop_deletion` - [bool] by ref, if it set to true account deletion will stopped, can be overridden in hook processing
  658. * @hook @ref hook-account-before_delete
  659. */
  660. bx_alert('account', 'before_delete', $this->_iAccountID, 0, array('delete_with_content' => $bDeleteWithContent, 'stop_deletion' => &$isStopDeletion));
  661. if ($isStopDeletion)
  662. return false;
  663. $oAccountQuery = BxDolAccountQuery::getInstance();
  664. $oProfileQuery = BxDolProfileQuery::getInstance();
  665. $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
  666. foreach ($aProfiles as $iProfileId => $aRow) {
  667. $oProfile = BxDolProfile::getInstance($iProfileId);
  668. if (!$oProfile)
  669. continue;
  670. $oProfile->delete(false, $bDeleteWithContent, true);
  671. }
  672. // delete profile
  673. if (!$oAccountQuery->delete($this->_iAccountID))
  674. return false;
  675. // unset class instance to prevent creating the instance again
  676. $sClass = __CLASS__ . '_' . $this->_iAccountID;
  677. unset($GLOBALS['bxDolClasses'][$sClass]);
  678. /**
  679. * @hooks
  680. * @hookdef hook-account-delete 'account', 'delete' - hook on after delete account
  681. * - $unit_name - equals `account`
  682. * - $action - equals `delete`
  683. * - $object_id - account id
  684. * - $sender_id - not used
  685. * - $extra_params - array of additional params with the following array keys:
  686. * - `delete_with_content` - [bool] if account will delete with content = true, otherwise = false
  687. * @hook @ref hook-account-delete
  688. */
  689. bx_alert('account', 'delete', $this->_iAccountID, 0, array ('delete_with_content' => $bDeleteWithContent));
  690. $this->doAudit($this->_iAccountID, $bDeleteWithContent ? '_sys_audit_action_account_deleted_with_content' : '_sys_audit_action_account_deleted');
  691. return true;
  692. }
  693. /**
  694. * Add permament messages.
  695. */
  696. public function addInformerPermanentMessages ($oInformer)
  697. {
  698. if (!$this->isConfirmed()) {
  699. if (!$this->isConfirmedEmail()) {
  700. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email');
  701. $aAccountInfo = $this->getInfo();
  702. $oInformer->add('sys-account-unconfirmed-email', _t('_sys_txt_account_unconfirmed_email', $sUrl . '&resend=1', $aAccountInfo['email'], $sUrl), BX_INFORMER_ALERT);
  703. }
  704. if (!$this->isConfirmedPhone()) {
  705. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-phone') . '';
  706. $aAccountInfo = $this->getInfo();
  707. $oInformer->add('sys-account-unconfirmed-phone', _t('_sys_txt_account_unconfirmed_phone', $sUrl), BX_INFORMER_ALERT);
  708. }
  709. }
  710. $this->isNeedChangePassword(false, $oInformer);
  711. }
  712. public function addInformerDeletionScheduled($oInformer)
  713. {
  714. if(BxDolBackgroundJobs::getInstance()->exists('account_delete_' . $this->_iAccountID))
  715. $oInformer->add('sys-account-deletion-scheduled', _t('_sys_txt_account_deletion_scheduled'), BX_INFORMER_ALERT);
  716. }
  717. /**
  718. * Get unsubscribe link for the specified mesage type
  719. */
  720. public function getUnsubscribeLink($iEmailType, $iAccountId = false)
  721. {
  722. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  723. $sUrl = '';
  724. switch ($iEmailType) {
  725. case BX_EMAIL_NOTIFY:
  726. $sUrl = 'page.php?i=unsubscribe-notifications';
  727. break;
  728. case BX_EMAIL_MASS:
  729. $sUrl = 'page.php?i=unsubscribe-news';
  730. break;
  731. default:
  732. return '';
  733. }
  734. return BxDolPermalinks::getInstance()->permalink($sUrl) . '&id=' . $iAccountId . '&code=' . $this->getEmailHash();
  735. }
  736. public function getEmailHash($iAccountId = false)
  737. {
  738. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  739. $a = $this->getInfo();
  740. return md5($a['email'] . $a['salt'] . BX_DOL_SECRET);
  741. }
  742. public function getPasswordChangedDate($mixedAccount = false)
  743. {
  744. if(($bEmpty = empty($mixedAccount)) || !is_array($mixedAccount))
  745. $mixedAccount = $this->_oQuery->getInfoById(!$bEmpty ? (int)$mixedAccount : $this->_iAccountID);
  746. $iLastChanged = (int)$mixedAccount['password_changed'];
  747. return $iLastChanged ? $iLastChanged : (int)$mixedAccount['added'];
  748. }
  749. public function getPasswordExpiredDate($iPasswordExpiredForMembership, $mixedAccount = false)
  750. {
  751. if($iPasswordExpiredForMembership == 0)
  752. return 0;
  753. return $iPasswordExpiredForMembership * 86400 + $this->getPasswordChangedDate($mixedAccount);
  754. }
  755. public function isNeedChangePassword($iAccountId = false, $oInformer = false)
  756. {
  757. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  758. list($sPageLink, $aPageParams) = bx_get_base_url_inline();
  759. $sChangePasswordUri = 'account-settings-password';
  760. $bNeedRedirectToChangePassword = true;
  761. if(isset($aPageParams['i']) && $aPageParams['i'] == $sChangePasswordUri)
  762. $bNeedRedirectToChangePassword = false;
  763. /**
  764. * @hooks
  765. * @hookdef hook-account-is_need_to_change_password 'account', 'is_need_to_change_password' - hook on after delete account
  766. * - $unit_name - equals `account`
  767. * - $action - equals `is_need_to_change_password`
  768. * - $object_id - account id
  769. * - $sender_id - not used
  770. * - $extra_params - array of additional params with the following array keys:
  771. * - `override_result` - [bool] by ref, if Need Redirect To Change Password = true, otherwise = false, can be overridden in hook processing
  772. * @hook @ref hook-account-is_need_to_change_password
  773. */
  774. bx_alert('account', 'is_need_to_change_password', $iAccountId, false, [
  775. 'override_result' => &$bNeedRedirectToChangePassword
  776. ]);
  777. if(!$bNeedRedirectToChangePassword)
  778. return;
  779. $aAccountInfo = $this->getInfo();
  780. $aMembershipInfo = BxDolAcl::getInstance()->getMemberMembershipInfo($aAccountInfo['profile_id']);
  781. $sChangePasswordUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $sChangePasswordUri);
  782. if(($iPasswordExpiredDate = $this->getPasswordExpiredDate($aMembershipInfo['password_expired'], $aAccountInfo)) && $iPasswordExpiredDate < time()) {
  783. if(getParam('sys_account_accounts_force_password_change_after_expiration') == 'on') {
  784. header('Location: ' . $sChangePasswordUrl);
  785. exit;
  786. }
  787. else {
  788. if(!$oInformer)
  789. $oInformer = BxDolInformer::getInstance();
  790. $oInformer->add('sys-account-need-to-change-password', _t('_sys_txt_account_need_to_change_password', $sChangePasswordUrl), BX_INFORMER_ALERT);
  791. }
  792. }
  793. }
  794. public function doAudit($iAccountId, $sAction, $aData = [])
  795. {
  796. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  797. bx_audit($iAccountId, 'bx_accounts', $sAction, [
  798. 'content_title' => $this->getEmail(),
  799. 'data' => $aData
  800. ]);
  801. }
  802. /**
  803. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  804. */
  805. static public function isAllowedCreate ($iProfileId, $isPerformAction = false)
  806. {
  807. $aCheck = checkActionModule($iProfileId, 'create account', 'system', $isPerformAction);
  808. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  809. return MsgBox($aCheck[CHECK_ACTION_MESSAGE]);
  810. return CHECK_ACTION_RESULT_ALLOWED;
  811. }
  812. static public function isAllowedCreateMultiple ($iProfileId, $isPerformAction = false)
  813. {
  814. $iLimit = (int)getParam('sys_account_limit_profiles_number');
  815. $bResult = false;
  816. if(isAdmin() || $iLimit != 1)
  817. $bResult = true;
  818. $oProfile = BxDolProfile::getInstance($iProfileId);
  819. if($iLimit == 1 && $oProfile->getModule() == 'system')
  820. $bResult = true;
  821. if($oProfile && $oProfile->getAccountId() != getLoggedId())
  822. $bResult = false;
  823. /**
  824. * @hooks
  825. * @hookdef hook-account-allow_create_another_profile 'profile', 'allow_create_another_profile' - hook on check allow create profile
  826. * - $unit_name - equals `profile`
  827. * - $action - equals `allow_create_another_profile`
  828. * - $object_id - profile id
  829. * - $sender_id - not used
  830. * - $extra_params - array of additional params with the following array keys:
  831. * - `override_result` - [bool] by ref, if allow create another profile = true, otherwise = false, can be overridden in hook processing
  832. * @hook @ref hook-account-allow_create_another_profile
  833. */
  834. bx_alert('profile', 'allow_create_another_profile', $iProfileId, 0, [
  835. 'override_result' => &$bResult
  836. ]);
  837. return $bResult;
  838. }
  839. /**
  840. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  841. */
  842. static public function isAllowedEdit ($iProfileId, $aContentInfo, $isPerformAction = false)
  843. {
  844. $oProfile = BxDolProfile::getInstance($iProfileId);
  845. if (!$oProfile)
  846. return _t('_sys_txt_access_denied');
  847. $aProfileInfo = $oProfile->getInfo();
  848. if (!$aProfileInfo || getLoggedId() != $aProfileInfo['account_id'])
  849. return _t('_sys_txt_access_denied');
  850. return CHECK_ACTION_RESULT_ALLOWED;
  851. }
  852. /**
  853. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  854. */
  855. static public function isAllowedDelete ($iProfileId, $aContentInfo, $isPerformAction = false)
  856. {
  857. $iAccountId = (int)BxDolProfile::getInstance($iProfileId)->getAccountId();
  858. if(isAdmin($iAccountId) && $iAccountId == (int)$aContentInfo['id'])
  859. return _t('_sys_txt_account_cannot_delete');
  860. $aCheck = checkActionModule($iProfileId, 'delete account', 'system', $isPerformAction);
  861. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  862. return $aCheck[CHECK_ACTION_MESSAGE];
  863. return CHECK_ACTION_RESULT_ALLOWED;
  864. }
  865. static public function pruning ()
  866. {
  867. $iCount = 0;
  868. $iTime = time() - getParam('sys_account_accounts_pruning_interval') * 86400;
  869. $oAccountQuery = BxDolAccountQuery::getInstance();
  870. $aAccounts = [];
  871. $aAccountsSuspend = [];
  872. $bSuspend = false;
  873. $aPruning = explode(',', getParam('sys_account_accounts_pruning'));
  874. foreach ($aPruning as $sPruning) {
  875. switch($sPruning) {
  876. case 'no_login_suspend':
  877. $aAccountsSuspend = $oAccountQuery->getAccountsForPruning('no_login', $iTime);
  878. break;
  879. case 'no_login_delete':
  880. $aAccounts = array_merge($aAccounts, $oAccountQuery->getAccountsForPruning('no_login', $iTime));
  881. break;
  882. case 'no_confirm_delete':
  883. $aAccounts1 = $oAccountQuery->getAccountsForPruning('no_confirm', $iTime);
  884. foreach ($aAccounts1 as $k => $aAccount) {
  885. $oAccount = BxDolAccount::getInstance($aAccount['id']);
  886. if($oAccount->isConfirmed()){
  887. unset($aAccounts1[$k]);
  888. }
  889. }
  890. $aAccounts = array_merge($aAccounts, $aAccounts1);
  891. break;
  892. case 'no_profile_delete':
  893. $aAccounts = array_merge($aAccounts, $aAccounts = $oAccountQuery->getAccountsForPruning('no_profile', $iTime));
  894. break;
  895. }
  896. }
  897. foreach ($aAccountsSuspend as $k => $aAccount) {
  898. $oAccount = BxDolAccount::getInstance($aAccount['id']);
  899. $oProfile = BxDolProfile::getInstanceAccountProfile($aAccount['id']);
  900. $oProfile->suspend(BX_PROFILE_ACTION_AUTO, 0 ,false);
  901. $aProfiles = $oAccount->getProfiles();
  902. foreach($aProfiles as $aProfile){
  903. BxDolProfile::getInstance($aProfile['id'])->suspend(BX_PROFILE_ACTION_AUTO, 0 ,false);
  904. }
  905. $iCount++;
  906. }
  907. foreach ($aAccounts as $k => $aAccount) {
  908. $oAccount = BxDolAccount::getInstance($aAccount['id']);
  909. $oAccount->delete(false);
  910. $iCount++;
  911. }
  912. return $iCount;
  913. }
  914. }
  915. /** @} */