BxDolAccount.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. bx_import('BxDolAcl');
  10. class BxDolAccount extends BxDol
  11. {
  12. protected $_iAccountID;
  13. protected $_oQuery;
  14. /**
  15. * Constructor
  16. */
  17. protected function __construct ($iAccountId)
  18. {
  19. $iAccountId = (int)$iAccountId;
  20. $sClass = get_class($this) . '_' . $iAccountId;
  21. if (isset($GLOBALS['bxDolClasses'][$sClass]))
  22. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  23. parent::__construct();
  24. $this->_iAccountID = $iAccountId; // since constructor is protected $iAccountId is always valid
  25. $this->_oQuery = BxDolAccountQuery::getInstance();
  26. }
  27. /**
  28. * Prevent cloning the instance
  29. */
  30. public function __clone()
  31. {
  32. $sClass = get_class($this) . '_' . $this->_iProfileID;
  33. if (isset($GLOBALS['bxDolClasses'][$sClass]))
  34. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  35. }
  36. /**
  37. * Get singleton instance of the class
  38. */
  39. public static function getInstance($mixedAccountId = false)
  40. {
  41. if (!$mixedAccountId)
  42. $mixedAccountId = getLoggedId();
  43. $iAccountId = self::getID($mixedAccountId);
  44. if (!$iAccountId)
  45. return false;
  46. $sClass = __CLASS__ . '_' . $iAccountId;
  47. if(!isset($GLOBALS['bxDolClasses'][$sClass]))
  48. $GLOBALS['bxDolClasses'][$sClass] = new BxDolAccount($iAccountId);
  49. return $GLOBALS['bxDolClasses'][$sClass];
  50. }
  51. /**
  52. * Get studio operator account singleton instance on the class
  53. */
  54. public static function getInstanceStudioOperator()
  55. {
  56. $oQuery = BxDolAccountQuery::getInstance();
  57. if (!($iId = $oQuery->getStudioOperatorId()))
  58. return false;
  59. return self::getInstance($iId);
  60. }
  61. /**
  62. * Get account id
  63. */
  64. public function id()
  65. {
  66. return $this->_oQuery->getIdById($this->_iAccountID);
  67. }
  68. /**
  69. * Check if account is confirmed, it is checked by email confirmation
  70. */
  71. public function isConfirmed($iAccountId = false)
  72. {
  73. if (!getParam('sys_email_confirmation')) // if email_confirmation procedure is disabled, always return true
  74. return true;
  75. $a = $this->getInfo((int)$iAccountId);
  76. return $a['email_confirmed'] ? true : false;
  77. }
  78. /**
  79. * Set account email to confirmed or unconfirmed
  80. * @param int $isConfirmed - false: mark email as unconfirmed, true: as confirmed
  81. * @param int $iAccountId - optional account id
  82. * @return true on success or false on error
  83. */
  84. public function updateEmailConfirmed($isConfirmed, $isAutoSendConfrmationEmail = true, $iAccountId = false)
  85. {
  86. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  87. if (!$isConfirmed && $isAutoSendConfrmationEmail && getParam('sys_email_confirmation')) // if email_confirmation procedure is enabled - send email confirmation letter
  88. $this->sendConfirmationEmail($iId);
  89. if ($this->_oQuery->updateEmailConfirmed($isConfirmed, $iId)) {
  90. bx_alert('account', $isConfirmed ? 'confirm' : 'unconfirm', $iId);
  91. return true;
  92. }
  93. return false;
  94. }
  95. public function updateProfileContext($iSwitchToProfileId, $iAccountId = false)
  96. {
  97. $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  98. $aInfo = $this->getInfo((int)$iId);
  99. if (!$aInfo)
  100. return false;
  101. $ret = null;
  102. bx_alert('account', 'before_switch_context', $iId, $iSwitchToProfileId, array('profile_id_current' => $aInfo['profile_id'], 'override_result' => &$ret));
  103. if ($ret !== null)
  104. return $ret;
  105. if (!$this->_oQuery->updateCurrentProfile($iId, $iSwitchToProfileId))
  106. return false;
  107. bx_alert('account', 'switch_context', $iId, $iSwitchToProfileId, array('profile_id_old' => $aInfo['profile_id']));
  108. return true;
  109. }
  110. /**
  111. * Send "confirmation" email
  112. */
  113. public function sendConfirmationEmail($iAccountId = false)
  114. {
  115. $sEmail = $this->getEmail($iAccountId);
  116. $oKey = BxDolKey::getInstance();
  117. $sConfirmationCode = $oKey->getNewKey(array('account_id' => $iAccountId));
  118. $sConfirmationLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email') . '&code=' . urlencode($sConfirmationCode);
  119. $aPlus = array();
  120. $aPlus['email'] = $sEmail;
  121. $aPlus['conf_code'] = $sConfirmationCode;
  122. $aPlus['conf_link'] = $sConfirmationLink;
  123. $aPlus['conf_form_link'] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email');
  124. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Confirmation', $aPlus);
  125. return $aTemplate && sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM);
  126. }
  127. /**
  128. * Get account info
  129. */
  130. public function getInfo($iAccountId = false)
  131. {
  132. return $this->_oQuery->getInfoById((int)$iAccountId ? (int)$iAccountId : $this->_iAccountID);
  133. }
  134. /**
  135. * Get account display name
  136. */
  137. public function getDisplayName($iAccountId = false)
  138. {
  139. $aInfo = $this->getInfo($iAccountId);
  140. return bx_process_output($aInfo['name']);
  141. }
  142. /**
  143. * Get account url
  144. */
  145. public function getUrl($iAccountId = false)
  146. {
  147. return 'javascript:void(0);';
  148. }
  149. /**
  150. * Get account url
  151. */
  152. public function getUnit($iAccountId = false)
  153. {
  154. return '<div>' . $this->getDisplayName($iAccountId) . '</div>';
  155. }
  156. /**
  157. * Get picture url
  158. */
  159. public function getPicture($iAccountId = false)
  160. {
  161. return BxDolTemplate::getInstance()->getImageUrl('account-picture.png');
  162. }
  163. /**
  164. * Get avatar url
  165. */
  166. public function getAvatar($iAccountId = false)
  167. {
  168. return BxDolTemplate::getInstance()->getImageUrl('account-avatar.png');
  169. }
  170. /**
  171. * Get thumb picture url
  172. */
  173. public function getThumb($iAccountId = false)
  174. {
  175. return BxDolTemplate::getInstance()->getImageUrl('account-thumb.png');
  176. }
  177. /**
  178. * Get icon picture url
  179. */
  180. public function getIcon($iAccountId = false)
  181. {
  182. return BxDolTemplate::getInstance()->getImageUrl('account-icon.png');
  183. }
  184. /**
  185. * Get account email
  186. */
  187. public function getEmail($iAccountId = false)
  188. {
  189. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  190. $aAccountInfo = $this->getInfo($iAccountId);
  191. return $aAccountInfo['email'];
  192. }
  193. /**
  194. * Validate account.
  195. * @param $s - account identifier (id or email)
  196. * @return account id or false if account was not found
  197. */
  198. static public function getID($s)
  199. {
  200. $oQuery = BxDolAccountQuery::getInstance();
  201. if (preg_match("/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/i", $s)) {
  202. $iId = (int)$oQuery->getIdByEmail($s);
  203. return $iId ? $iId : false;
  204. }
  205. $iId = $oQuery->getIdById((int)$s);
  206. return $iId ? $iId : false;
  207. }
  208. /**
  209. * Get all profile ids associated with the account
  210. */
  211. public function getProfilesIds ($isFilterNonSwitchableProfiles = true)
  212. {
  213. $a = $this->getProfiles ($isFilterNonSwitchableProfiles);
  214. return $a ? array_keys($a) : array();
  215. }
  216. /**
  217. * Get all profiles associated with the account
  218. */
  219. public function getProfiles ($isFilterNonSwitchableProfiles = true)
  220. {
  221. $oProfileQuery = BxDolProfileQuery::getInstance();
  222. $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
  223. if ($isFilterNonSwitchableProfiles) {
  224. foreach ($aProfiles as $iProfileId => $aProfile)
  225. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  226. unset($aProfiles[$iProfileId]);
  227. }
  228. return $aProfiles;
  229. }
  230. /**
  231. * Delete profile.
  232. * @param $bDeleteWithContent - delete associated profiles with all its contents
  233. */
  234. function delete($bDeleteWithContent = false)
  235. {
  236. $aAccountInfo = $this->_oQuery->getInfoById($this->_iAccountID);
  237. if (!$aAccountInfo)
  238. return false;
  239. // create system event before deletion
  240. $isStopDeletion = false;
  241. bx_alert('account', 'before_delete', $this->_iAccountID, 0, array('delete_with_content' => $bDeleteWithContent, 'stop_deletion' => &$isStopDeletion));
  242. if ($isStopDeletion)
  243. return false;
  244. $oAccountQuery = BxDolAccountQuery::getInstance();
  245. $oProfileQuery = BxDolProfileQuery::getInstance();
  246. $aProfiles = $oProfileQuery->getProfilesByAccount($this->_iAccountID);
  247. foreach ($aProfiles as $iProfileId => $aRow) {
  248. $oProfile = BxDolProfile::getInstance($iProfileId);
  249. if (!$oProfile)
  250. continue;
  251. $oProfile->delete(false, $bDeleteWithContent, true);
  252. }
  253. // delete profile
  254. if (!$oAccountQuery->delete($this->_iAccountID))
  255. return false;
  256. // create system event
  257. bx_alert('account', 'delete', $this->_iAccountID, 0, array ('delete_with_content' => $bDeleteWithContent));
  258. // unset class instance to prevent creating the instance again
  259. $this->_iAccountID = 0;
  260. $sClass = get_class($this) . '_' . $this->_iAccountID;
  261. unset($GLOBALS['bxDolClasses'][$sClass]);
  262. return true;
  263. }
  264. /**
  265. * Add permament messages.
  266. */
  267. public function addInformerPermanentMessages ($oInformer)
  268. {
  269. if (!$this->isConfirmed()) {
  270. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email') . '&resend=1';
  271. $aAccountInfo = $this->getInfo();
  272. $oInformer->add('sys-account-unconfirmed', _t('_sys_txt_account_unconfirmed', $sUrl, $aAccountInfo['email']), BX_INFORMER_ALERT);
  273. }
  274. }
  275. /**
  276. * Get unsubscribe link for the specified mesage type
  277. */
  278. public function getUnsubscribeLink($iEmailType, $iAccountId = false)
  279. {
  280. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  281. $sUrl = '';
  282. switch ($iEmailType) {
  283. case BX_EMAIL_NOTIFY:
  284. $sUrl = 'page.php?i=unsubscribe-notifications';
  285. break;
  286. case BX_EMAIL_MASS:
  287. $sUrl = 'page.php?i=unsubscribe-news';
  288. break;
  289. default:
  290. return '';
  291. }
  292. return BxDolPermalinks::getInstance()->permalink($sUrl) . '&id=' . $iAccountId . '&code=' . $this->getEmailHash();
  293. }
  294. public function getEmailHash($iAccountId = false)
  295. {
  296. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  297. $a = $this->getInfo();
  298. return md5($a['email'] . $a['salt'] . BX_DOL_SECRET);
  299. }
  300. /**
  301. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  302. */
  303. static public function isAllowedCreate ($iProfileId, $isPerformAction = false)
  304. {
  305. $aCheck = checkActionModule($iProfileId, 'create account', 'system', $isPerformAction);
  306. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  307. return MsgBox($aCheck[CHECK_ACTION_MESSAGE]);
  308. return CHECK_ACTION_RESULT_ALLOWED;
  309. }
  310. /**
  311. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  312. */
  313. static public function isAllowedEdit ($iProfileId, $aContentInfo, $isPerformAction = false)
  314. {
  315. $oProfile = BxDolProfile::getInstance($iProfileId);
  316. if (!$oProfile)
  317. return _t('_sys_txt_access_denied');
  318. $aProfileInfo = $oProfile->getInfo();
  319. if (!$aProfileInfo || getLoggedId() != $aProfileInfo['account_id'])
  320. return _t('_sys_txt_access_denied');
  321. return CHECK_ACTION_RESULT_ALLOWED;
  322. }
  323. /**
  324. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  325. */
  326. static public function isAllowedDelete ($iProfileId, $aContentInfo, $isPerformAction = false)
  327. {
  328. $aCheck = checkActionModule($iProfileId, 'delete account', 'system', $isPerformAction);
  329. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  330. return MsgBox($aCheck[CHECK_ACTION_MESSAGE]);
  331. return CHECK_ACTION_RESULT_ALLOWED;
  332. }
  333. }
  334. /** @} */