Trac

source: trunk/inc/classes/BxDolAccount.php

Last change on this file was 16297, checked in by Alexander Trofimov, 2 years ago

Notes

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