BxDolAccount.php 12 KB

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