BxDolAccount.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. var $_iAccountID;
  12. var $_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. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  97. $aAccountInfo = $this->getInfo($iAccountId);
  98. $sEmail = $aAccountInfo['email'];
  99. bx_import('BxDolKey');
  100. $oKey = BxDolKey::getInstance();
  101. $sConfirmationCode = $oKey->getNewKey(array('account_id' => $iAccountId));
  102. bx_import('BxDolPermalinks');
  103. $sConfirmationLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email') . '&code=' . urlencode($sConfirmationCode);
  104. $aPlus = array();
  105. $aPlus['email'] = $sEmail;
  106. $aPlus['conf_code'] = $sConfirmationCode;
  107. $aPlus['conf_link'] = $sConfirmationLink;
  108. $aPlus['conf_form_link'] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email');
  109. bx_import('BxDolEmailTemplates');
  110. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Confirmation', $aPlus);
  111. return $aTemplate && sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM);
  112. }
  113. /**
  114. * Get account info
  115. */
  116. public function getInfo($iAccountId = false) {
  117. return $this->_oQuery->getInfoById((int)$iAccountId ? (int)$iAccountId : $this->_iAccountID);
  118. }
  119. /**
  120. * Get account display name
  121. */
  122. public function getDisplayName($iAccountId = false) {
  123. $aInfo = $this->getInfo($iAccountId);
  124. return $aInfo['name'];
  125. }
  126. /**
  127. * Get account url
  128. */
  129. public function getUrl($iAccountId = false) {
  130. return 'javascript:void(0);';
  131. }
  132. /**
  133. * Get account url
  134. */
  135. public function getUnit($iAccountId = false) {
  136. return '<div>' . $this->getDisplayName($iAccountId) . '</div>';
  137. }
  138. /**
  139. * Get avatar picture url
  140. */
  141. public function getAvatar($iAccountId = false) {
  142. return '';
  143. }
  144. /**
  145. * Get thumb picture url
  146. */
  147. public function getThumb($iAccountId = false) {
  148. return '';
  149. }
  150. /**
  151. * Get icon picture url
  152. */
  153. public function getIcon($iAccountId = false) {
  154. return '';
  155. }
  156. /**
  157. * Validate account.
  158. * @param $s - account identifier (id or email)
  159. * @return account id or false if account was not found
  160. */
  161. static public function getID($s) {
  162. $oQuery = BxDolAccountQuery::getInstance();
  163. if (preg_match("/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/i", $s)) {
  164. $iId = (int)$oQuery->getIdByEmail($s);
  165. return $iId ? $iId : false;
  166. }
  167. $iId = $oQuery->getIdById((int)$s);
  168. return $iId ? $iId : false;
  169. }
  170. /**
  171. * Delete profile.
  172. */
  173. function delete($iAccountId = false) {
  174. $ID = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  175. $aAccountInfo = $this->_oQuery->getInfoById($ID);
  176. if (!$aAccountInfo)
  177. return false;
  178. // create system event before deletion
  179. $isStopDeletion = false;
  180. bx_alert('account', 'before_delete', $ID, 0, array('stop_deletion' => &$isStopDeletion));
  181. if ($isStopDeletion)
  182. return false;
  183. bx_import('BxDolAccountQuery');
  184. $oAccountQuery = BxDolAccountQuery::getInstance();
  185. bx_import('BxDolProfile');
  186. $oProfileQuery = BxDolProfileQuery::getInstance();
  187. $aProfiles = $oProfileQuery->getProfilesByAccount($ID);
  188. foreach ($aProfiles as $iProfileId => $aRow) {
  189. $oProfile = BxDolProfile::getInstance($iProfileId);
  190. if (!$oProfile)
  191. continue;
  192. $oProfile->delete(false, true);
  193. }
  194. // delete profile
  195. if (!$oAccountQuery->delete($ID))
  196. return false;
  197. // create system event
  198. bx_alert('account', 'delete', $ID);
  199. // unset class instance to prevent creating the instance again
  200. $this->_iAccountID = 0;
  201. $sClass = get_class($this) . '_' . $ID;
  202. unset($GLOBALS['bxDolClasses'][$sClass]);
  203. return true;
  204. }
  205. /**
  206. * Add permament messages.
  207. */
  208. public function addInformerPermanentMessages ($oInformer) {
  209. if (!$this->isConfirmed()) {
  210. bx_import('BxDolPermalinks');
  211. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-email') . '&resend=1';
  212. $aAccountInfo = $this->getInfo();
  213. $oInformer->add('sys-account-unconfirmed', _t('_sys_txt_account_unconfirmed', $sUrl, $aAccountInfo['email']), BX_INFORMER_ALERT);
  214. }
  215. }
  216. /**
  217. * Get unsubscribe link for the specified mesage type
  218. */
  219. public function getUnsubscribeLink($iEmailType, $iAccountId = false) {
  220. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  221. bx_import('BxDolPermalinks');
  222. $sUrl = '';
  223. switch ($iEmailType) {
  224. case BX_EMAIL_NOTIFY:
  225. $sUrl = 'page.php?i=unsubscribe-notifications';
  226. break;
  227. case BX_EMAIL_MASS:
  228. $sUrl = 'page.php?i=unsubscribe-news';
  229. break;
  230. default:
  231. return '';
  232. }
  233. return BxDolPermalinks::getInstance()->permalink($sUrl) . '&id=' . $iAccountId . '&code=' . $this->getEmailHash();
  234. }
  235. public function getEmailHash($iAccountId = false) {
  236. $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
  237. $a = $this->getInfo();
  238. return md5($a['email'] . $a['salt'] . BX_DOL_SECRET);
  239. }
  240. /**
  241. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  242. */
  243. static public function isAllowedCreate ($iProfileId, $isPerformAction = false) {
  244. $aCheck = checkActionModule($iProfileId, 'create account', 'system', $isPerformAction);
  245. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  246. return MsgBox($aCheck[CHECK_ACTION_MESSAGE]);
  247. return CHECK_ACTION_RESULT_ALLOWED;
  248. }
  249. /**
  250. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  251. */
  252. static public function isAllowedEdit ($iProfileId, $aContentInfo, $isPerformAction = false) {
  253. bx_import('BxDolProfile');
  254. $oProfile = BxDolProfile::getInstance($iProfileId);
  255. if (!$oProfile)
  256. return _t('_sys_txt_access_denied');
  257. $aProfileInfo = $oProfile->getInfo();
  258. if (!$aProfileInfo || getLoggedId() != $aProfileInfo['account_id'])
  259. return _t('_sys_txt_access_denied');
  260. return CHECK_ACTION_RESULT_ALLOWED;
  261. }
  262. /**
  263. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  264. */
  265. static public function isAllowedDelete ($iProfileId, $aContentInfo, $isPerformAction = false) {
  266. $aCheck = checkActionModule($iProfileId, 'delete account', 'system', $isPerformAction);
  267. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  268. return MsgBox($aCheck[CHECK_ACTION_MESSAGE]);
  269. return CHECK_ACTION_RESULT_ALLOWED;
  270. }
  271. }
  272. /** @} */