| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaCore UNA Core
- * @{
- */
- bx_import('BxDolAcl');
- class BxDolAccount extends BxDolFactory implements iBxDolSingleton
- {
- protected $_iAccountID;
- protected $_aInfo;
- protected $_oQuery;
- /**
- * Constructor
- */
- protected function __construct ($iAccountId)
- {
- $iAccountId = (int)$iAccountId;
- $sClass = get_class($this) . '_' . $iAccountId;
- if (isset($GLOBALS['bxDolClasses'][$sClass]))
- trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
- parent::__construct();
- $this->_iAccountID = $iAccountId; // since constructor is protected $iAccountId is always valid
- $this->_oQuery = BxDolAccountQuery::getInstance();
- }
- /**
- * Prevent cloning the instance
- */
- public function __clone()
- {
- $sClass = get_class($this) . '_' . $this->_iProfileID;
- if (isset($GLOBALS['bxDolClasses'][$sClass]))
- trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
- }
- /**
- * Get singleton instance of the class
- */
- public static function getInstance($mixedAccountId = false)
- {
- if (!$mixedAccountId)
- $mixedAccountId = getLoggedId();
- $iAccountId = self::getID($mixedAccountId);
- if (!$iAccountId)
- return false;
- $sClass = __CLASS__ . '_' . $iAccountId;
- if(!isset($GLOBALS['bxDolClasses'][$sClass]))
- $GLOBALS['bxDolClasses'][$sClass] = new BxDolAccount($iAccountId);
- return $GLOBALS['bxDolClasses'][$sClass];
- }
- /**
- * Get studio operator account singleton instance on the class
- */
- public static function getInstanceStudioOperator()
- {
- $oQuery = BxDolAccountQuery::getInstance();
- if (!($iId = $oQuery->getStudioOperatorId()))
- return false;
- return self::getInstance($iId);
- }
- /**
- * Get account id
- */
- public function id()
- {
- $a = $this->getInfo($this->_iAccountID);
- return isset($a['id']) ? $a['id'] : false;
- }
- /**
- * Check if account is confirmed, it is checked by email confirmation
- */
- public function isConfirmed($iAccountId = false)
- {
- $sConfirmationType = getParam('sys_account_confirmation_type');
- if ($sConfirmationType == BX_ACCOUNT_CONFIRMATION_NONE)
- return true;
-
- $a = $this->getInfo((int)$iAccountId);
-
- switch ($sConfirmationType) {
- case BX_ACCOUNT_CONFIRMATION_EMAIL:
- if ($a['email_confirmed'])
- return true;
- break;
- case BX_ACCOUNT_CONFIRMATION_PHONE:
- if ($a['phone_confirmed'])
- return true;
- break;
- case BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE:
- if ($a['email_confirmed'] && $a['phone_confirmed'])
- return true;
- break;
- }
- return false;
- }
-
- public function getCurrentConfirmationStatusValue($iAccountId = false)
- {
- $a = $this->getInfo((int)$iAccountId);
- $sTmp = $a['email_confirmed'] . $a['phone_confirmed'];
- switch ($sTmp) {
- case '01':
- return BX_ACCOUNT_CONFIRMATION_PHONE;
- case '10':
- return BX_ACCOUNT_CONFIRMATION_EMAIL;
- case '11':
- return BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE;
- }
- return BX_ACCOUNT_CONFIRMATION_NONE;
- }
-
- public function isConfirmedEmail($iAccountId = false)
- {
- if (!self::isNeedConfirmEmail())
- return true;
- $a = $this->getInfo((int)$iAccountId);
- return $a['email_confirmed'] ? true : false;
- }
-
- public function isConfirmedPhone($iAccountId = false)
- {
- if (!self::isNeedConfirmPhone())
- return true;
- $a = $this->getInfo((int)$iAccountId);
- return $a['phone_confirmed'] ? true : false;
- }
-
- static public function isNeedConfirmEmail()
- {
- if (getParam('sys_account_confirmation_type') == BX_ACCOUNT_CONFIRMATION_EMAIL || getParam('sys_account_confirmation_type') == BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE)
- return true;
- return false;
- }
-
- static public function isNeedConfirmPhone()
- {
- if (getParam('sys_account_confirmation_type') == BX_ACCOUNT_CONFIRMATION_PHONE || getParam('sys_account_confirmation_type') == BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE)
- return true;
- return false;
- }
-
- public function isLocked($iAccountId = false)
- {
- $a = $this->getInfo((int)$iAccountId);
- return $a['locked'] ? true : false;
- }
-
- //
- /**
- * Set account email to confirmed or unconfirmed
- * @param int $isConfirmed - false: mark email as unconfirmed, true: as confirmed
- * @param int $iAccountId - optional account id
- * @return true on success or false on error
- */
- public function updateEmailConfirmed($isConfirmed, $isAutoSendConfrmationEmail = true, $iAccountId = false)
- {
- $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
- if (!$isConfirmed && $isAutoSendConfrmationEmail && self::isNeedConfirmEmail()) // if email_confirmation procedure is enabled - send email confirmation letter
- $this->sendConfirmationEmail($iId);
- if ($this->_oQuery->updateEmailConfirmed($isConfirmed, $iId)) {
- $this->_aInfo = false;
- bx_alert('account', $this->isConfirmed() ? 'confirm' : 'unconfirm', $iId);
- return true;
- }
- return false;
- }
-
- /**
- * Set account phone to confirmed or unconfirmed
- * @param int $isConfirmed - false: mark phone as unconfirmed, true: as confirmed
- * @param int $iAccountId - optional account id
- * @return true on success or false on error
- */
- public function updatePhoneConfirmed($isConfirmed, $iAccountId = false)
- {
- $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
-
- if ($this->_oQuery->updatePhoneConfirmed($isConfirmed, $iId)) {
- $this->_aInfo = false;
- bx_alert('account', $this->isConfirmed() ? 'confirm' : 'unconfirm', $iId);
- return true;
- }
- return false;
- }
-
- /**
- * Set account phone
- * @param string $sPhone - phone number
- * @param int $iAccountId - optional account id
- * @return true on success or false on error
- */
- public function updatePhone($sPhone, $iAccountId = false)
- {
- $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
- if ($this->_oQuery->updatePhone($sPhone, $iId)) {
- bx_alert('account', 'set_phone', $iId);
- return true;
- }
- return false;
- }
- /**
- * Switch context automatically to the first available profile
- * @param $iProfileIdFilterOut profile ID to exclude from the list of possible profiles
- * @param $iAccountId account ID to use istead of current account
- * @return true on success or false on error
- */
- public function updateProfileContextAuto($iProfileIdFilterOut = false, $iAccountId = false)
- {
- $oAccount = (!$iAccountId || $iAccountId == $this->_iAccountID ? $this : BxDolAccount::getInstance ($iAccountId));
- if (!$oAccount)
- return false;
- $aAccountInfo = $oAccount->getInfo();
- $aProfiles = $oAccount->getProfiles();
- $oProfileAccount = BxDolProfile::getInstanceAccountProfile($oAccount->id());
- // unset deleted profile and account profile
- if ($iProfileIdFilterOut)
- unset($aProfiles[$iProfileIdFilterOut]);
- unset($aProfiles[$oProfileAccount->id()]);
- if ($aProfiles) {
- // try to use another profile
- reset($aProfiles);
- $iProfileId = key($aProfiles);
- }
- else {
- // if no profiles exist, use account profile
- $iProfileId = $oProfileAccount->id();
- }
- return $oAccount->updateProfileContext($iProfileId);
- }
-
- public function updateProfileContext($iSwitchToProfileId, $iAccountId = false)
- {
- $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
- $aInfo = $this->getInfo((int)$iId);
- if (!$aInfo)
- return false;
- $ret = null;
- bx_alert('account', 'before_switch_context', $iId, $iSwitchToProfileId, array('profile_id_current' => $aInfo['profile_id'], 'override_result' => &$ret));
- if ($ret !== null)
- return $ret;
- if (!$this->_oQuery->updateCurrentProfile($iId, $iSwitchToProfileId))
- return false;
- $this->_aInfo = false;
-
- bx_alert('account', 'switch_context', $iId, $iSwitchToProfileId, array('profile_id_old' => $aInfo['profile_id']));
- return true;
- }
- /**
- * Send "confirmation" email
- */
- public function sendConfirmationEmail($iAccountId = false)
- {
- $sName = $this->getDisplayName($iAccountId);
- $sEmail = $this->getEmail($iAccountId);
- $oKey = BxDolKey::getInstance();
- $sConfirmationCode = $oKey->getNewKey(array('account_id' => $iAccountId));
- $sConfirmationLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email') . '&code=' . urlencode($sConfirmationCode);
- $aPlus = array(
- 'name' => $sName,
- 'email' => $sEmail,
- 'conf_code' => $sConfirmationCode,
- 'conf_link' => $sConfirmationLink,
- 'conf_form_link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email')
- );
- $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Confirmation', $aPlus);
- return $aTemplate && sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM);
- }
- /**
- * Get account info
- */
- public function getInfo($iAccountId = false)
- {
- if ($iAccountId && $iAccountId != $this->_iAccountID)
- return $this->_oQuery->getInfoById((int)$iAccountId ? (int)$iAccountId : $this->_iAccountID);
- if ($this->_aInfo)
- return $this->_aInfo;
- $this->_aInfo = $this->_oQuery->getInfoById($this->_iAccountID);
- return $this->_aInfo;
- }
- /**
- * Get account display name
- */
- public function getDisplayName($iAccountId = false)
- {
- $aInfo = $this->getInfo($iAccountId);
- return bx_process_output(!empty($aInfo['name']) ? $aInfo['name'] : _t('_sys_txt_user_n', $aInfo['id']));
- }
- /**
- * Get account url
- */
- public function getUrl($iAccountId = false)
- {
- return 'javascript:void(0);';
- }
- /**
- * Get account url
- */
- public function getUnit($iAccountId = false, $aParams = array())
- {
-
- $sTemplate = 'unit';
- $sTemplateSize = false;
- $aTemplateVars = array();
- if(!empty($aParams['template'])) {
- if(is_string($aParams['template']))
- $sTemplate = $aParams['template'];
- else if(is_array($aParams['template'])) {
- if(!empty($aParams['template']['name']))
- $sTemplate = $aParams['template']['name'];
- if(!empty($aParams['template']['size']))
- $sTemplateSize = $aParams['template']['size'];
- if(!empty($aParams['template']['vars']))
- $aTemplateVars = $aParams['template']['vars'];
- }
- }
- $sTemplate = 'account_' . $sTemplate . '.html';
- if(empty($sTemplateSize))
- $sTemplateSize = 'thumb';
- $sTitle = $this->getDisplayName($iAccountId);
- $aTmplVars = array(
- 'size' => $sTemplateSize,
- 'color' => implode(', ', BxDolTemplate::getColorCode(($iAccountId ? $iAccountId : $this->_iAccountID), 1.0)),
- 'letter' => mb_strtoupper(mb_substr($sTitle, 0, 1)),
- 'content_url' => $this->getUrl($iAccountId),
- 'title' => $sTitle,
- 'title_attr' => bx_html_attribute($sTitle),
- 'bx_if:show_online' => array(
- 'condition' => $this->isOnline($iAccountId),
- 'content' => array()
- )
- );
- if(!empty($aTemplateVars) && is_array($aTemplateVars))
- $aTmplVars = array_merge ($aTmplVars, $aTemplateVars);
- return BxDolTemplate::getInstance()->parseHtmlByName($sTemplate, $aTmplVars);
- }
- /**
- * Get picture url
- */
- public function getPicture($iAccountId = false)
- {
- return BxDolTemplate::getInstance()->getImageUrl('account-picture.png');
- }
- /**
- * Get avatar url
- */
- public function getAvatar($iAccountId = false)
- {
- return BxDolTemplate::getInstance()->getImageUrl('account-avatar.png');
- }
- /**
- * Get thumb picture url
- */
- public function getThumb($iAccountId = false)
- {
- return BxDolTemplate::getInstance()->getImageUrl('account-thumb.png');
- }
- /**
- * Get icon picture url
- */
- public function getIcon($iAccountId = false)
- {
- return BxDolTemplate::getInstance()->getImageUrl('account-icon.png');
- }
- /**
- * Get account email
- */
- public function getEmail($iAccountId = false)
- {
- $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
- $aAccountInfo = $this->getInfo($iAccountId);
- return $aAccountInfo['email'];
- }
- /**
- * Get language ID
- */
- public function getLanguageId($iAccountId = false)
- {
- $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
- $aAccountInfo = $this->getInfo($iAccountId);
- return $aAccountInfo['lang_id'];
- }
- /**
- * Is account online
- */
- public function isOnline($iAccountId = false)
- {
- $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
- return $this->_oQuery->isOnline($iAccountId);
- }
- /**
- * Validate account.
- * @param $s - account identifier (id or email)
- * @return account id or false if account was not found
- */
- static public function getID($s)
- {
- $oQuery = BxDolAccountQuery::getInstance();
- bx_import('BxDolForm');
- if (BxDolFormCheckerHelper::checkEmail($s)) {
- $iId = (int)$oQuery->getIdByEmail($s);
- return $iId ? $iId : false;
- }
- if (preg_match("/^\+[0-9\s]+$/", $s)) {
- $iId = (int)$oQuery->getIdByPhone($s);
- return $iId ? $iId : false;
- }
-
- $iId = $oQuery->getIdById((int)$s);
- return $iId ? $iId : false;
- }
- /**
- * Check if profiles limit reached
- */
- public function isProfilesLimitReached ()
- {
- $iProfilesLimit = (int)getParam('sys_account_limit_profiles_number');
- if ($iProfilesLimit && ($iProfilesNum = $this->getProfilesNumber()) && $iProfilesNum >= $iProfilesLimit)
- return true;
- return false;
- }
- /**
- * Get number of profiles associated with the account
- */
- public function getProfilesNumber ($isFilterNonSwitchableProfiles = true)
- {
- $a = $this->getProfilesIds($isFilterNonSwitchableProfiles);
- return count($a);
- }
-
- /**
- * Get all profile ids associated with the account
- */
- public function getProfilesIds ($isFilterNonSwitchableProfiles = true)
- {
- $a = $this->getProfiles ($isFilterNonSwitchableProfiles);
- return $a ? array_keys($a) : array();
- }
- /**
- * Get all profiles associated with the account
- */
- public function getProfiles ($isFilterNonSwitchableProfiles = true)
- {
- $oProfileQuery = BxDolProfileQuery::getInstance();
- $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
- if ($isFilterNonSwitchableProfiles) {
- foreach ($aProfiles as $iProfileId => $aProfile)
- if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
- unset($aProfiles[$iProfileId]);
- }
- return $aProfiles;
- }
- /**
- * Delete profile.
- * @param $bDeleteWithContent - delete associated profiles with all its contents
- */
- function delete($bDeleteWithContent = false)
- {
- $aAccountInfo = $this->_oQuery->getInfoById($this->_iAccountID);
- if (!$aAccountInfo)
- return false;
- // create system event before deletion
- $isStopDeletion = false;
- bx_alert('account', 'before_delete', $this->_iAccountID, 0, array('delete_with_content' => $bDeleteWithContent, 'stop_deletion' => &$isStopDeletion));
- if ($isStopDeletion)
- return false;
- $oAccountQuery = BxDolAccountQuery::getInstance();
- $oProfileQuery = BxDolProfileQuery::getInstance();
- $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
- foreach ($aProfiles as $iProfileId => $aRow) {
- $oProfile = BxDolProfile::getInstance($iProfileId);
- if (!$oProfile)
- continue;
- $oProfile->delete(false, $bDeleteWithContent, true);
- }
- // delete profile
- if (!$oAccountQuery->delete($this->_iAccountID))
- return false;
- // unset class instance to prevent creating the instance again
- $sClass = __CLASS__ . '_' . $this->_iAccountID;
- unset($GLOBALS['bxDolClasses'][$sClass]);
- // create system event
- bx_alert('account', 'delete', $this->_iAccountID, 0, array ('delete_with_content' => $bDeleteWithContent));
-
- return true;
- }
- /**
- * Add permament messages.
- */
- public function addInformerPermanentMessages ($oInformer)
- {
- if (!$this->isConfirmedEmail()) {
- $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email') . '&resend=1';
- $aAccountInfo = $this->getInfo();
- $oInformer->add('sys-account-unconfirmed-email', _t('_sys_txt_account_unconfirmed_email', $sUrl, $aAccountInfo['email']), BX_INFORMER_ALERT);
- }
- if (!$this->isConfirmedPhone()) {
- $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-phone') . '';
- $aAccountInfo = $this->getInfo();
- $oInformer->add('sys-account-unconfirmed-phone', _t('_sys_txt_account_unconfirmed_phone', $sUrl), BX_INFORMER_ALERT);
- }
- }
- /**
- * Get unsubscribe link for the specified mesage type
- */
- public function getUnsubscribeLink($iEmailType, $iAccountId = false)
- {
- $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
- $sUrl = '';
- switch ($iEmailType) {
- case BX_EMAIL_NOTIFY:
- $sUrl = 'page.php?i=unsubscribe-notifications';
- break;
- case BX_EMAIL_MASS:
- $sUrl = 'page.php?i=unsubscribe-news';
- break;
- default:
- return '';
- }
- return BxDolPermalinks::getInstance()->permalink($sUrl) . '&id=' . $iAccountId . '&code=' . $this->getEmailHash();
- }
- public function getEmailHash($iAccountId = false)
- {
- $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
- $a = $this->getInfo();
- return md5($a['email'] . $a['salt'] . BX_DOL_SECRET);
- }
- /**
- * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
- */
- static public function isAllowedCreate ($iProfileId, $isPerformAction = false)
- {
- $aCheck = checkActionModule($iProfileId, 'create account', 'system', $isPerformAction);
- if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
- return MsgBox($aCheck[CHECK_ACTION_MESSAGE]);
- return CHECK_ACTION_RESULT_ALLOWED;
- }
- /**
- * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
- */
- static public function isAllowedEdit ($iProfileId, $aContentInfo, $isPerformAction = false)
- {
- $oProfile = BxDolProfile::getInstance($iProfileId);
- if (!$oProfile)
- return _t('_sys_txt_access_denied');
- $aProfileInfo = $oProfile->getInfo();
- if (!$aProfileInfo || getLoggedId() != $aProfileInfo['account_id'])
- return _t('_sys_txt_access_denied');
- return CHECK_ACTION_RESULT_ALLOWED;
- }
- /**
- * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
- */
- static public function isAllowedDelete ($iProfileId, $aContentInfo, $isPerformAction = false)
- {
- $iAccountId = (int)BxDolProfile::getInstance($iProfileId)->getAccountId();
- if(isAdmin($iAccountId) && $iAccountId == (int)$aContentInfo['id'])
- return _t('_sys_txt_access_denied');
- $aCheck = checkActionModule($iProfileId, 'delete account', 'system', $isPerformAction);
- if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
- return $aCheck[CHECK_ACTION_MESSAGE];
- return CHECK_ACTION_RESULT_ALLOWED;
- }
- }
- /** @} */
|