BxDolAccount.php 11 KB

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