diff --git a/bootstrap.php b/bootstrap.php index 832fe96e50a6b89f9c7d952c5fa355295fcd8ca5..ce879a5cab316a7cd6f39b994edef2d9546b6c70 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -174,6 +174,7 @@ class Bootstrap { function loadCode() { #include required files require(INCLUDE_DIR.'class.signal.php'); + require(INCLUDE_DIR.'class.user.php'); require(INCLUDE_DIR.'class.auth.php'); require(INCLUDE_DIR.'class.pagenate.php'); //Pagenate helper! require(INCLUDE_DIR.'class.log.php'); diff --git a/include/class.auth.php b/include/class.auth.php index a1b8c52209cbcd272bd44b025172a3b1bc47a350..f17d4d630aaf0530e9e2d10ed8ef33be04ccab34 100644 --- a/include/class.auth.php +++ b/include/class.auth.php @@ -497,6 +497,11 @@ abstract class UserAuthenticationBackend extends AuthenticationBackend { $user->refreshSession(true); //set the hash. + if (($acct = $user->getAccount()) && ($tid = $acct->get('timezone_id'))) { + $_SESSION['TZ_OFFSET'] = Timezone::getOffsetById($tid); + $_SESSION['TZ_DST'] = $acct->get('dst'); + } + //Log login info... $msg=sprintf('%s (%s) logged in [%s]', $user->getUserName(), $user->getId(), $_SERVER['REMOTE_ADDR']); diff --git a/include/class.client.php b/include/class.client.php index 7d1dfbcb7c93e6852070098c10774cee7f891dab..707418456e3e15b1ebefc53d2f10d0ba360b258d 100644 --- a/include/class.client.php +++ b/include/class.client.php @@ -269,43 +269,7 @@ class EndUser extends AuthenticatedUser { } } -require_once INCLUDE_DIR.'class.orm.php'; -class ClientAccountModel extends VerySimpleModel { - static $meta = array( - 'table' => USER_ACCOUNT_TABLE, - 'pk' => array('id'), - 'joins' => array( - 'user' => array( - 'null' => false, - 'constraint' => array('user_id' => 'UserModel.id') - ), - ), - ); -} - -class ClientAccount extends ClientAccountModel { - var $_options = null; - var $timezone; - - const CONFIRMED = 0x0001; - const LOCKED = 0x0002; - const PASSWD_RESET_REQUIRED = 0x0004; - - function __onload() { - if ($this->get('timezone_id')) { - $this->timezone = Timezone::getOffsetById($this->ht['timezone_id']); - $_SESSION['TZ_OFFSET'] = $this->timezone; - $_SESSION['TZ_DST'] = $this->get('dst'); - } - } - - function getId() { - return $this->get('id'); - } - - function getUserId() { - return $this->get('user_id'); - } +class ClientAccount extends UserAccount { function checkPassword($password, $autoupdate=true) { @@ -331,87 +295,6 @@ class ClientAccount extends ClientAccountModel { return $this->checkPassword($password, false); } - function hasPassword() { - return (bool) $this->get('passwd'); - } - - function sendResetEmail($template='pwreset-client') { - global $ost, $cfg; - - $token = Misc::randCode(48); // 290-bits - - $email = $cfg->getDefaultEmail(); - $content = Page::lookup(Page::getIdByType($template)); - - if (!$email || !$content) - return new Error('Unable to retrieve password reset email template'); - - $vars = array( - 'url' => $ost->getConfig()->getBaseUrl(), - 'token' => $token, - 'user' => $this->getUser(), - 'recipient' => $this->getUser(), - 'link' => sprintf( - "%s/pwreset.php?token=%s", - $ost->getConfig()->getBaseUrl(), - $token), - ); - $vars['reset_link'] = &$vars['link']; - - $info = array('email' => $email, 'vars' => &$vars, 'log'=>true); - Signal::send('auth.pwreset.email', $this, $info); - - $msg = $ost->replaceTemplateVariables(array( - 'subj' => $content->getName(), - 'body' => $content->getBody(), - ), $vars); - - $_config = new Config('pwreset'); - $_config->set($vars['token'], $this->user->getId()); - - $email->send($this->user->default_email->get('address'), - Format::striptags($msg['subj']), $msg['body']); - } - - function confirm() { - $this->_setStatus(self::CONFIRMED); - return $this->save(); - } - - function isConfirmed() { - return $this->_getStatus(self::CONFIRMED); - } - - function lock() { - $this->_setStatus(self::LOCKED); - $this->save(); - } - - function isLocked() { - return $this->_getStatus(self::LOCKED); - } - - function forcePasswdReset() { - $this->_setStatus(self::PASSWD_RESET_REQUIRED); - return $this->save(); - } - - function isPasswdResetForced() { - return $this->_getStatus(self::PASSWD_RESET_REQUIRED); - } - - function _getStatus($flag) { - return 0 !== ($this->get('status') & $flag); - } - - function _clearStatus($flag) { - return $this->set('status', $this->get('status') & ~$flag); - } - - function _setStatus($flag) { - return $this->set('status', $this->get('status') | $flag); - } - function cancelResetTokens() { // TODO: Drop password-reset tokens from the config table for // this user id @@ -424,17 +307,11 @@ class ClientAccount extends ClientAccountModel { } function getInfo() { - $base = $this->ht; + $base = parent::getInfo(); $base['tz_offset'] = $this->timezone; return $base; } - function getUser() { - $user = User::lookup($this->get('user_id')); - $user->set('account', $this); - return $user; - } - function update($vars, &$errors) { $rtoken = $_SESSION['_client']['reset-token']; if ($vars['passwd1'] || $vars['passwd2'] || $vars['cpasswd'] || $rtoken) { @@ -490,20 +367,6 @@ class ClientAccount extends ClientAccountModel { return $this->save(); } - - static function createForUser($user) { - return static::create(array('user_id'=>$user->getId())); - } - - static function lookupByUsername($username) { - if (strpos($username, '@') !== false) - $user = self::lookup(array('user__emails__address'=>$username)); - else - $user = self::lookup(array('username'=>$username)); - - return $user; - } } -ClientAccount::_inspect(); ?> diff --git a/include/class.user.php b/include/class.user.php index 35c5b3919e9b36d2047aac12a88256a5647ea712..a73bdd5cd1f6b40b7d3ca926128f9c91ee0493b1 100644 --- a/include/class.user.php +++ b/include/class.user.php @@ -149,10 +149,6 @@ class User extends UserModel { return $this->created; } - function getAccount() { - return $this->account; - } - function to_json() { $info = array( @@ -228,7 +224,6 @@ class User extends UserModel { return $this->_forms; } - function getAccount() { // XXX: return $this->account; @@ -369,7 +364,6 @@ class User extends UserModel { return parent::delete(); } } -User::_inspect(); class PersonsName { var $parts; @@ -569,24 +563,54 @@ class UserAccountModel extends VerySimpleModel { class UserAccount extends UserAccountModel { var $_options = null; - var $timezone; const CONFIRMED = 0x0001; const LOCKED = 0x0002; const PASSWD_RESET_REQUIRED = 0x0004; - private function hasStatus($flag) { + protected function hasStatus($flag) { return 0 !== ($this->get('status') & $flag); } - private function clearStatus($flag) { + protected function clearStatus($flag) { return $this->set('status', $this->get('status') & ~$flag); } - private function setStatus($flag) { + protected function setStatus($flag) { return $this->set('status', $this->get('status') | $flag); } + function confirm() { + $this->setStatus(self::CONFIRMED); + return $this->save(); + } + + function isConfirmed() { + return $this->hasStatus(self::CONFIRMED); + } + + function lock() { + $this->setStatus(self::LOCKED); + $this->save(); + } + + function isLocked() { + return $this->hasStatus(self::LOCKED); + } + + function forcePasswdReset() { + $this->setStatus(self::PASSWD_RESET_REQUIRED); + return $this->save(); + } + + function isPasswdResetForced() { + return $this->hasStatus(self::PASSWD_RESET_REQUIRED); + } + + function hasPassword() { + return (bool) $this->get('passwd'); + } + function getStatus() { return $this->get('status'); } @@ -595,6 +619,67 @@ class UserAccount extends UserAccountModel { return $this->ht; } + function getId() { + return $this->get('id'); + } + + function getUserId() { + return $this->get('user_id'); + } + + function getUser() { + $user = User::lookup($this->getUserId()); + $user->set('account', $this); + return $user; + } + + function sendResetEmail() { + return static::sendUnlockEmail('pwreset-client'); + } + + function sendConfirmEmail() { + return static::sendUnlockEmail('registration-confirm'); + } + + protected function sendUnlockEmail($template) { + global $ost, $cfg; + + $token = Misc::randCode(48); // 290-bits + + $email = $cfg->getDefaultEmail(); + $content = Page::lookup(Page::getIdByType($template)); + + if (!$email || !$content) + return new Error('Unable to retrieve password reset email template'); + + $vars = array( + 'url' => $ost->getConfig()->getBaseUrl(), + 'token' => $token, + 'user' => $this->getUser(), + 'recipient' => $this->getUser(), + 'link' => sprintf( + "%s/pwreset.php?token=%s", + $ost->getConfig()->getBaseUrl(), + $token), + ); + $vars['reset_link'] = &$vars['link']; + + $info = array('email' => $email, 'vars' => &$vars, 'log'=>true); + Signal::send('auth.pwreset.email', $this, $info); + + $msg = $ost->replaceTemplateVariables(array( + 'subj' => $content->getName(), + 'body' => $content->getBody(), + ), $vars); + + $_config = new Config('pwreset'); + $_config->set($vars['token'], $this->user->getId()); + + $email->send($this->user->default_email->get('address'), + Format::striptags($msg['subj']), $msg['body']); + } + + function __toString() { return (string) $this->getStatus(); } @@ -645,6 +730,18 @@ class UserAccount extends UserAccountModel { return $this->save(true); } + static function createForUser($user) { + return static::create(array('user_id'=>$user->getId())); + } + + static function lookupByUsername($username) { + if (strpos($username, '@') !== false) + $user = static::lookup(array('user__emails__address'=>$username)); + else + $user = static::lookup(array('username'=>$username)); + + return $user; + } } @@ -697,5 +794,6 @@ class UserList implements IteratorAggregate, ArrayAccess { return $list ? implode(', ', $list) : ''; } } +User::_inspect(); ?>