BxDolAccount.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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)
  41. {
  42. if (!$mixedAccountId)
  43. $mixedAccountId = getLoggedId();
  44. $iAccountId = self::getID($mixedAccountId);
  45. if (!$iAccountId)
  46. return false;
  47. $sClass = __CLASS__ . '_' . $iAccountId;
  48. if(!isset($GLOBALS['bxDolClasses'][$sClass]))
  49. $GLOBALS['bxDolClasses'][$sClass] = new BxDolAccount($iAccountId);
  50. return $GLOBALS['bxDolClasses'][$sClass];
  51. }
  52. /**
  53. * Get studio operator account singleton instance on the class
  54. */
  55. public static function getInstanceStudioOperator()
  56. {
  57. $oQuery = BxDolAccountQuery::getInstance();
  58. if (!($iId = $oQuery->getStudioOperatorId()))
  59. return false;
  60. return self::getInstance($iId);
  61. }
  62. /**
  63. * Get account id
  64. */
  65. public function id()
  66. {
  67. $a = $this->getInfo($this->_iAccountID);
  68. return isset($a['id']) ? $a['id'] : false;
  69. }
  70. /**
  71. * Check if account is confirmed, it is checked by email confirmation
  72. */
  73. public function isConfirmed($iAccountId = false)
  74. {
  75. if (!getParam('sys_email_confirmation')) // if email_confirmation procedure is disabled, always return true
  76. return true;
  77. $a = $this->getInfo((int)$iAccountId);
  78. return $a['email_confirmed'] ? true : false;
  79. }
  80. /**
  81. * Set account email to confirmed or unconfirmed
  82. * @param int $isConfirmed - false: mark email as unconfirmed, true: as confirmed
  83. * @param int $iAccountId - optional account id
  84. * @return true on success or false on error
  85. */
  86. public function updateEmailConfirmed($isConfirmed, $isAutoSendConfrmationEmail = true, $iAccountId = false)
  87. {
  88. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  89. if (!$isConfirmed && $isAutoSendConfrmationEmail && getParam('sys_email_confirmation')) // if email_confirmation procedure is enabled - send email confirmation letter
  90. $this->sendConfirmationEmail($iId);
  91. if ($this->_oQuery->updateEmailConfirmed($isConfirmed, $iId)) {
  92. $this->_aInfo = false;
  93. bx_alert('account', $isConfirmed ? 'confirm' : 'unconfirm', $iId);
  94. return true;
  95. }
  96. return false;
  97. }
  98. /**
  99. * Switch context automatically to the first available profile
  100. * @param $iProfileIdFilterOut profile ID to exclude from the list of possible profiles
  101. * @param $iAccountId account ID to use istead of current account
  102. * @return true on success or false on error
  103. */
  104. public function updateProfileContextAuto($iProfileIdFilterOut = false, $iAccountId = false)
  105. {
  106. $oAccount = (!$iAccountId || $iAccountId == $this->_iAccountID ? $this : BxDolAccount::getInstance ($iAccountId));
  107. if (!$oAccount)
  108. return false;
  109. $aAccountInfo = $oAccount->getInfo();
  110. $aProfiles = $oAccount->getProfiles();
  111. $oProfileAccount = BxDolProfile::getInstanceAccountProfile($oAccount->id());
  112. // unset deleted profile and account profile
  113. if ($iProfileIdFilterOut)
  114. unset($aProfiles[$iProfileIdFilterOut]);
  115. unset($aProfiles[$oProfileAccount->id()]);
  116. if ($aProfiles) {
  117. // try to use another profile
  118. reset($aProfiles);
  119. $iProfileId = key($aProfiles);
  120. }
  121. else {
  122. // if no profiles exist, use account profile
  123. $iProfileId = $oProfileAccount->id();
  124. }
  125. return $oAccount->updateProfileContext($iProfileId);
  126. }
  127. public function updateProfileContext($iSwitchToProfileId, $iAccountId = false)
  128. {
  129. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  130. $aInfo = $this->getInfo((int)$iId);
  131. if (!$aInfo)
  132. return false;
  133. $ret = null;
  134. bx_alert('account', 'before_switch_context', $iId, $iSwitchToProfileId, array('profile_id_current' => $aInfo['profile_id'], 'override_result' => &$ret));
  135. if ($ret !== null)
  136. return $ret;
  137. if (!$this->_oQuery->updateCurrentProfile($iId, $iSwitchToProfileId))
  138. return false;
  139. $this->_aInfo = false;
  140. bx_alert('account', 'switch_context', $iId, $iSwitchToProfileId, array('profile_id_old' => $aInfo['profile_id']));
  141. return true;
  142. }
  143. /**
  144. * Send "confirmation" email
  145. */
  146. public function sendConfirmationEmail($iAccountId = false)
  147. {
  148. $sName = $this->getDisplayName($iAccountId);
  149. $sEmail = $this->getEmail($iAccountId);
  150. $oKey = BxDolKey::getInstance();
  151. $sConfirmationCode = $oKey->getNewKey(array('account_id' => $iAccountId));
  152. $sConfirmationLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email') . '&code=' . urlencode($sConfirmationCode);
  153. $aPlus = array(
  154. 'name' => $sName,
  155. 'email' => $sEmail,
  156. 'conf_code' => $sConfirmationCode,
  157. 'conf_link' => $sConfirmationLink,
  158. 'conf_form_link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email')
  159. );
  160. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Confirmation', $aPlus);
  161. return $aTemplate && sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM);
  162. }
  163. /**
  164. * Get account info
  165. */
  166. public function getInfo($iAccountId = false)
  167. {
  168. if ($iAccountId && $iAccountId != $this->_iAccountID)
  169. return $this->_oQuery->getInfoById((int)$iAccountId ? (int)$iAccountId : $this->_iAccountID);
  170. if ($this->_aInfo)
  171. return $this->_aInfo;
  172. $this->_aInfo = $this->_oQuery->getInfoById($this->_iAccountID);
  173. return $this->_aInfo;
  174. }
  175. /**
  176. * Get account display name
  177. */
  178. public function getDisplayName($iAccountId = false)
  179. {
  180. $aInfo = $this->getInfo($iAccountId);
  181. return bx_process_output(!empty($aInfo['name']) ? $aInfo['name'] : _t('_sys_txt_user_n', $aInfo['id']));
  182. }
  183. /**
  184. * Get account url
  185. */
  186. public function getUrl($iAccountId = false)
  187. {
  188. return 'javascript:void(0);';
  189. }
  190. /**
  191. * Get account url
  192. */
  193. public function getUnit($iAccountId = false)
  194. {
  195. $sTitle = $this->getDisplayName($iAccountId);
  196. return BxDolTemplate::getInstance()->parseHtmlByName('unit_account.html', array(
  197. 'color' => implode(', ', BxDolTemplate::getColorCode(($iAccountId ? $iAccountId : $this->_iAccountID), 0.5)),
  198. 'letter' => mb_strtoupper(mb_substr($sTitle, 0, 1))
  199. ));
  200. }
  201. /**
  202. * Get picture url
  203. */
  204. public function getPicture($iAccountId = false)
  205. {
  206. return BxDolTemplate::getInstance()->getImageUrl('account-picture.png');
  207. }
  208. /**
  209. * Get avatar url
  210. */
  211. public function getAvatar($iAccountId = false)
  212. {
  213. return BxDolTemplate::getInstance()->getImageUrl('account-avatar.png');
  214. }
  215. /**
  216. * Get thumb picture url
  217. */
  218. public function getThumb($iAccountId = false)
  219. {
  220. return BxDolTemplate::getInstance()->getImageUrl('account-thumb.png');
  221. }
  222. /**
  223. * Get icon picture url
  224. */
  225. public function getIcon($iAccountId = false)
  226. {
  227. return BxDolTemplate::getInstance()->getImageUrl('account-icon.png');
  228. }
  229. /**
  230. * Get account email
  231. */
  232. public function getEmail($iAccountId = false)
  233. {
  234. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  235. $aAccountInfo = $this->getInfo($iAccountId);
  236. return $aAccountInfo['email'];
  237. }
  238. /**
  239. * Is account online
  240. */
  241. public function isOnline($iAccountId = false)
  242. {
  243. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  244. return $this->_oQuery->isOnline($iAccountId);
  245. }
  246. /**
  247. * Validate account.
  248. * @param $s - account identifier (id or email)
  249. * @return account id or false if account was not found
  250. */
  251. static public function getID($s)
  252. {
  253. $oQuery = BxDolAccountQuery::getInstance();
  254. bx_import('BxDolForm');
  255. if (BxDolFormCheckerHelper::checkEmail($s)) {
  256. $iId = (int)$oQuery->getIdByEmail($s);
  257. return $iId ? $iId : false;
  258. }
  259. $iId = $oQuery->getIdById((int)$s);
  260. return $iId ? $iId : false;
  261. }
  262. /**
  263. * Check if profiles limit reached
  264. */
  265. public function isProfilesLimitReached ()
  266. {
  267. $iProfilesLimit = (int)getParam('sys_account_limit_profiles_number');
  268. if ($iProfilesLimit && ($iProfilesNum = $this->getProfilesNumber()) && $iProfilesNum >= $iProfilesLimit)
  269. return true;
  270. return false;
  271. }
  272. /**
  273. * Get number of profiles associated with the account
  274. */
  275. public function getProfilesNumber ($isFilterNonSwitchableProfiles = true)
  276. {
  277. $a = $this->getProfilesIds($isFilterNonSwitchableProfiles);
  278. return count($a);
  279. }
  280. /**
  281. * Get all profile ids associated with the account
  282. */
  283. public function getProfilesIds ($isFilterNonSwitchableProfiles = true)
  284. {
  285. $a = $this->getProfiles ($isFilterNonSwitchableProfiles);
  286. return $a ? array_keys($a) : array();
  287. }
  288. /**
  289. * Get all profiles associated with the account
  290. */
  291. public function getProfiles ($isFilterNonSwitchableProfiles = true)
  292. {
  293. $oProfileQuery = BxDolProfileQuery::getInstance();
  294. $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
  295. if ($isFilterNonSwitchableProfiles) {
  296. foreach ($aProfiles as $iProfileId => $aProfile)
  297. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  298. unset($aProfiles[$iProfileId]);
  299. }
  300. return $aProfiles;
  301. }
  302. /**
  303. * Delete profile.
  304. * @param $bDeleteWithContent - delete associated profiles with all its contents
  305. */
  306. function delete($bDeleteWithContent = false)
  307. {
  308. $aAccountInfo = $this->_oQuery->getInfoById($this->_iAccountID);
  309. if (!$aAccountInfo)
  310. return false;
  311. // create system event before deletion
  312. $isStopDeletion = false;
  313. bx_alert('account', 'before_delete', $this->_iAccountID, 0, array('delete_with_content' => $bDeleteWithContent, 'stop_deletion' => &$isStopDeletion));
  314. if ($isStopDeletion)
  315. return false;
  316. $oAccountQuery = BxDolAccountQuery::getInstance();
  317. $oProfileQuery = BxDolProfileQuery::getInstance();
  318. $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
  319. foreach ($aProfiles as $iProfileId => $aRow) {
  320. $oProfile = BxDolProfile::getInstance($iProfileId);
  321. if (!$oProfile)
  322. continue;
  323. $oProfile->delete(false, $bDeleteWithContent, true);
  324. }
  325. // delete profile
  326. if (!$oAccountQuery->delete($this->_iAccountID))
  327. return false;
  328. // unset class instance to prevent creating the instance again
  329. $sClass = __CLASS__ . '_' . $this->_iAccountID;
  330. unset($GLOBALS['bxDolClasses'][$sClass]);
  331. // create system event
  332. bx_alert('account', 'delete', $this->_iAccountID, 0, array ('delete_with_content' => $bDeleteWithContent));
  333. return true;
  334. }
  335. /**
  336. * Add permament messages.
  337. */
  338. public function addInformerPermanentMessages ($oInformer)
  339. {
  340. if (!$this->isConfirmed()) {
  341. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email') . '&resend=1';
  342. $aAccountInfo = $this->getInfo();
  343. $oInformer->add('sys-account-unconfirmed', _t('_sys_txt_account_unconfirmed', $sUrl, $aAccountInfo['email']), BX_INFORMER_ALERT);
  344. }
  345. }
  346. /**
  347. * Get unsubscribe link for the specified mesage type
  348. */
  349. public function getUnsubscribeLink($iEmailType, $iAccountId = false)
  350. {
  351. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  352. $sUrl = '';
  353. switch ($iEmailType) {
  354. case BX_EMAIL_NOTIFY:
  355. $sUrl = 'page.php?i=unsubscribe-notifications';
  356. break;
  357. case BX_EMAIL_MASS:
  358. $sUrl = 'page.php?i=unsubscribe-news';
  359. break;
  360. default:
  361. return '';
  362. }
  363. return BxDolPermalinks::getInstance()->permalink($sUrl) . '&id=' . $iAccountId . '&code=' . $this->getEmailHash();
  364. }
  365. public function getEmailHash($iAccountId = false)
  366. {
  367. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  368. $a = $this->getInfo();
  369. return md5($a['email'] . $a['salt'] . BX_DOL_SECRET);
  370. }
  371. /**
  372. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  373. */
  374. static public function isAllowedCreate ($iProfileId, $isPerformAction = false)
  375. {
  376. $aCheck = checkActionModule($iProfileId, 'create account', 'system', $isPerformAction);
  377. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  378. return MsgBox($aCheck[CHECK_ACTION_MESSAGE]);
  379. return CHECK_ACTION_RESULT_ALLOWED;
  380. }
  381. /**
  382. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  383. */
  384. static public function isAllowedEdit ($iProfileId, $aContentInfo, $isPerformAction = false)
  385. {
  386. $oProfile = BxDolProfile::getInstance($iProfileId);
  387. if (!$oProfile)
  388. return _t('_sys_txt_access_denied');
  389. $aProfileInfo = $oProfile->getInfo();
  390. if (!$aProfileInfo || getLoggedId() != $aProfileInfo['account_id'])
  391. return _t('_sys_txt_access_denied');
  392. return CHECK_ACTION_RESULT_ALLOWED;
  393. }
  394. /**
  395. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  396. */
  397. static public function isAllowedDelete ($iProfileId, $aContentInfo, $isPerformAction = false)
  398. {
  399. $iAccountId = (int)BxDolProfile::getInstance($iProfileId)->getAccountId();
  400. if(isAdmin($iAccountId) && $iAccountId == (int)$aContentInfo['id'])
  401. return _t('_sys_txt_access_denied');
  402. $aCheck = checkActionModule($iProfileId, 'delete account', 'system', $isPerformAction);
  403. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  404. return $aCheck[CHECK_ACTION_MESSAGE];
  405. return CHECK_ACTION_RESULT_ALLOWED;
  406. }
  407. }
  408. /** @} */