2019-07-17 22:08:50 +02:00
< ? php
/*
2019-07-17 22:31:04 +02:00
* @ copyright Copyright ( C ) 2005 - 2010 Keyboard Monkeys Ltd . http :// www . kb - m . com
2019-07-17 22:08:50 +02:00
* @ license http :// creativecommons . org / licenses / BSD / BSD License
* @ author Keyboard Monkey Ltd
* @ since CommunityID 0.9
* @ package CommunityID
* @ packager Keyboard Monkeys
*/
2019-07-17 22:16:19 +02:00
class Users_RegisterController extends CommunityID_Controller_Action
2019-07-17 22:08:50 +02:00
{
protected $_numCols = 1 ;
public function init ()
{
parent :: init ();
2019-07-17 22:16:19 +02:00
if ( $this -> user -> role != Users_Model_User :: ROLE_ADMIN && $this -> underMaintenance ) {
2019-07-17 22:08:50 +02:00
return $this -> _redirectForMaintenance ();
}
if ( ! $this -> _config -> environment -> registrations_enabled ) {
$this -> _helper -> FlashMessenger -> addMessage ( $this -> view -> translate (
'Sorry, registrations are currently disabled'
));
return $this -> _redirect ( '' );
}
}
public function indexAction ()
{
$appSession = Zend_Registry :: get ( 'appSession' );
if ( isset ( $appSession -> registerForm )) {
$form = $appSession -> registerForm ;
unset ( $appSession -> registerForm );
} else {
2019-07-17 22:16:19 +02:00
$form = new Users_Form_Register ( null , $this -> view -> base );
2019-07-17 22:08:50 +02:00
}
$this -> view -> form = $form ;
}
public function saveAction ()
{
2019-07-17 22:16:19 +02:00
$form = new Users_Form_Register ( null , $this -> view -> base );
2019-07-17 22:08:50 +02:00
$formData = $this -> _request -> getPost ();
$form -> populate ( $formData );
if ( ! $form -> isValid ( $formData )) {
$appSession = Zend_Registry :: get ( 'appSession' );
$appSession -> registerForm = $form ;
return $this -> _forward ( 'index' , null , null );
}
2019-07-17 22:16:19 +02:00
$users = new Users_Model_Users ();
2019-07-17 22:08:50 +02:00
2019-07-17 22:31:04 +02:00
if ( $users -> getUserWithUsername ( $form -> getValue ( 'username' ), false , $this -> view )) {
2019-07-17 22:08:50 +02:00
$form -> username -> addError ( $this -> view -> translate ( 'This username is already in use' ));
$appSession = Zend_Registry :: get ( 'appSession' );
$appSession -> registerForm = $form ;
return $this -> _forward ( 'index' , null , null );
}
if ( $users -> getUserWithEmail ( $form -> getValue ( 'email' ))) {
$form -> email -> addError ( $this -> view -> translate ( 'This E-mail is already in use' ));
$appSession = Zend_Registry :: get ( 'appSession' );
$appSession -> registerForm = $form ;
return $this -> _forward ( 'index' , null , null );
}
$user = $users -> createRow ();
$user -> firstname = $form -> getValue ( 'firstname' );
$user -> lastname = $form -> getValue ( 'lastname' );
$user -> email = $form -> getValue ( 'email' );
$user -> username = $form -> getValue ( 'username' );
2019-07-17 22:31:04 +02:00
preg_match ( '#(.*)/users/register/save#' , Zend_OpenId :: selfURL (), $matches );
$user -> generateOpenId ( $matches [ 1 ]);
2019-07-17 22:08:50 +02:00
2019-07-17 22:31:04 +02:00
if ( $this -> _config -> ldap -> enabled ) {
// when using ldap, unconfirmed users' password is saved unhashed temporarily, while he registers,
// and then it's stored in LDAP and cleared from the db
$user -> setPassword ( $form -> getValue ( 'password1' ));
} else {
$user -> setClearPassword ( $form -> getValue ( 'password1' ));
2019-07-17 22:08:50 +02:00
}
2019-07-17 22:16:19 +02:00
$user -> role = Users_Model_User :: ROLE_GUEST ;
2019-07-17 22:31:04 +02:00
$user -> token = Users_Model_User :: generateToken ();
2019-07-17 22:08:50 +02:00
$user -> accepted_eula = 0 ;
$user -> registration_date = date ( 'Y-m-d' );
2019-07-17 22:16:19 +02:00
$mail = self :: getMail ( $user , $this -> view -> translate ( 'Community-ID registration confirmation' ));
2019-07-17 22:08:50 +02:00
try {
$mail -> send ();
2019-07-17 22:31:04 +02:00
$user -> save ();
$user -> createDefaultProfile ( $this -> view );
2019-07-17 22:08:50 +02:00
$this -> _helper -> FlashMessenger -> addMessage ( $this -> view -> translate ( 'Thank you.' ));
$this -> _helper -> FlashMessenger -> addMessage ( $this -> view -> translate ( 'You will receive an E-mail with instructions to activate the account.' ));
2019-07-17 22:31:04 +02:00
} catch ( Zend_Mail_Exception $e ) {
if ( $this -> _config -> environment -> production ) {
$this -> _helper -> FlashMessenger -> addMessage ( $this -> view -> translate ( 'The confirmation E-mail could not be sent, so the account creation was cancelled. Please contact support.' ));
} else {
$this -> _helper -> FlashMessenger -> addMessage ( $this -> view -> translate ( 'The account was created but the E-mail could not be sent' ));
// I still wanna create the user when in development mode
$user -> save ();
}
2019-07-17 22:08:50 +02:00
if ( $this -> _config -> logging -> level == Zend_Log :: DEBUG ) {
$this -> _helper -> FlashMessenger -> addMessage ( $e -> getMessage ());
}
}
$this -> _redirect ( '' );
}
public function eulaAction ()
{
2019-07-17 22:16:19 +02:00
$users = new Users_Model_Users ();
2019-07-17 22:08:50 +02:00
if ( $this -> _request -> getParam ( 'token' ) == ''
|| ! ( $user = $users -> getUserWithToken ( $this -> _request -> getParam ( 'token' )))) {
2019-07-17 22:10:53 +02:00
$this -> _helper -> FlashMessenger -> addMessage ( $this -> view -> translate ( 'Invalid token' ));
2019-07-17 22:08:50 +02:00
$this -> _redirect ( '' );
2019-07-17 22:19:00 +02:00
return ;
2019-07-17 22:08:50 +02:00
}
$this -> view -> token = $user -> token ;
2019-07-17 22:31:04 +02:00
$file = CommunityID_Resources :: getResourcePath ( 'eula.txt' );
2019-07-17 22:08:50 +02:00
$this -> view -> eula = file_get_contents ( $file );
}
public function declineeulaAction ()
{
2019-07-17 22:16:19 +02:00
$users = new Users_Model_Users ();
2019-07-17 22:10:53 +02:00
if ( $this -> _request -> getParam ( 'token' ) == ''
|| ! ( $user = $users -> getUserWithToken ( $this -> _request -> getParam ( 'token' )))) {
2019-07-17 22:08:50 +02:00
Zend_Registry :: get ( 'logger' ) -> log ( 'invalid token' , Zend_Log :: DEBUG );
2019-07-17 22:10:53 +02:00
$this -> _helper -> FlashMessenger -> addMessage ( $this -> view -> translate ( 'Invalid token' ));
2019-07-17 22:08:50 +02:00
$this -> _redirect ( '' );
2019-07-17 22:19:00 +02:00
return ;
2019-07-17 22:08:50 +02:00
}
$user -> delete ();
2019-07-17 22:10:53 +02:00
$this -> _helper -> FlashMessenger -> addMessage ( $this -> view -> translate ( 'Your account has been deleted' ));
2019-07-17 22:08:50 +02:00
$this -> _redirect ( '' );
}
public function accepteulaAction ()
{
2019-07-17 22:16:19 +02:00
$users = new Users_Model_Users ();
2019-07-17 22:10:53 +02:00
if ( $this -> _request -> getParam ( 'token' ) == ''
|| ! ( $user = $users -> getUserWithToken ( $this -> _request -> getParam ( 'token' )))) {
$this -> _helper -> FlashMessenger -> addMessage ( $this -> view -> translate ( 'Invalid token' ));
2019-07-17 22:08:50 +02:00
$this -> _redirect ( '' );
2019-07-17 22:19:00 +02:00
return ;
2019-07-17 22:08:50 +02:00
}
2019-07-17 22:16:19 +02:00
$user -> role = Users_Model_User :: ROLE_REGISTERED ;
2019-07-17 22:08:50 +02:00
$user -> accepted_eula = 1 ;
$user -> registration_date = date ( 'Y-m-d' );
$user -> token = '' ;
2019-07-17 22:31:04 +02:00
if ( $this -> _config -> ldap -> enabled ) {
$ldap = Monkeys_Ldap :: getInstance ();
$ldap -> add ( $user );
// clear unencrypted password
$user -> setPassword ( '' );
}
2019-07-17 22:08:50 +02:00
$user -> save ();
$auth = Zend_Auth :: getInstance ();
$auth -> getStorage () -> write ( $user );
$this -> _redirect ( '/users/profile' );
}
/**
* @ return Zend_Mail
* @ throws Zend_Mail_Protocol_Exception
*/
2019-07-17 22:16:19 +02:00
public static function getMail ( Users_Model_User $user , $subject )
2019-07-17 22:08:50 +02:00
{
2019-07-17 22:31:04 +02:00
$file = CommunityID_Resources :: getResourcePath ( 'registration_mail.txt' );
2019-07-17 22:08:50 +02:00
$emailTemplate = file_get_contents ( $file );
$emailTemplate = str_replace ( '{userName}' , $user -> getFullName (), $emailTemplate );
$currentUrl = Zend_OpenId :: selfURL ();
preg_match ( '#(.*)/register/save#' , $currentUrl , $matches );
$emailTemplate = str_replace ( '{registrationURL}' , $matches [ 1 ] . '/register/eula?token=' . $user -> token , $emailTemplate );
// can't use $this-_config 'cause it's a static function
$configEmail = Zend_Registry :: get ( 'config' ) -> email ;
switch ( strtolower ( $configEmail -> transport )) {
case 'smtp' :
Zend_Mail :: setDefaultTransport (
new Zend_Mail_Transport_Smtp (
$configEmail -> host ,
$configEmail -> toArray ()
)
);
break ;
case 'mock' :
Zend_Mail :: setDefaultTransport ( new Zend_Mail_Transport_Mock ());
break ;
default :
Zend_Mail :: setDefaultTransport ( new Zend_Mail_Transport_Sendmail ());
}
2019-07-17 22:10:53 +02:00
$mail = new Zend_Mail ( 'UTF-8' );
2019-07-17 22:08:50 +02:00
$mail -> setBodyText ( $emailTemplate );
2019-07-17 22:16:19 +02:00
$mail -> setFrom ( $configEmail -> supportemail );
2019-07-17 22:08:50 +02:00
$mail -> addTo ( $user -> email );
2019-07-17 22:16:19 +02:00
$mail -> setSubject ( $subject );
2019-07-17 22:08:50 +02:00
return $mail ;
}
}