We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30585
    • 833 Posts
    Thanks all for taking the time. I guess I’m really out of luck here
    On the activation resource (ID:21), I have this snippet call:
    CODE:
    [[!ConfirmRegister]]

    I have that call as well and my activation ID is 19.

    Here’s my Register snippet code:
    <?php
    /**
     * Register
     *
     * Copyright 2010 by Shaun McCormick <[email protected]>
     *
     * Register is free software; you can redistribute it and/or modify it under the
     * terms of the GNU General Public License as published by the Free Software
     * Foundation; either version 2 of the License, or (at your option) any later
     * version.
     *
     * Register is distributed in the hope that it will be useful, but WITHOUT ANY
     * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License along with
     * Register; if not, write to the Free Software Foundation, Inc., 59 Temple
     * Place, Suite 330, Boston, MA 02111-1307 USA
     *
     * @package login
     */
    /**
     * MODx Register Snippet. Handles User registrations.
     * 
     * @package login
     */
    $corePath = $modx->getOption('login.core_path',$scriptProperties,$modx->getOption('core_path',null,MODX_CORE_PATH).'components/login/');
    $login = $modx->getService('login','Login',$corePath.'model/login/',$scriptProperties);
    if (!is_object($login) || !($login instanceof Login)) return '';
    
    /* setup default properties */
    $preHooks = $modx->getOption('preHooks',$scriptProperties,'');
    $submitVar = $modx->getOption('submitVar',$scriptProperties,'login-register-btn');
    $errTpl = $modx->getOption('errTpl',$scriptProperties,'<span class="error">[[+error]]</span>');
    
    /* if using recaptcha, load recaptcha html */
    if (strpos($preHooks,'recaptcha') !== false) {
        $recaptcha = $modx->getService('recaptcha','reCaptcha',$login->config['modelPath'].'recaptcha/');
        if ($recaptcha instanceof reCaptcha) {
            $modx->lexicon->load('login:recaptcha');
            $recaptchaTheme = $modx->getOption('recaptchaTheme',$scriptProperties,'clean');
            $recaptchaWidth = $modx->getOption('recaptchaWidth',$scriptProperties,500);
            $recaptchaHeight = $modx->getOption('recaptchaHeight',$scriptProperties,300);
            $html = $recaptcha->getHtml($recaptchaTheme,$recaptchaWidth,$recaptchaHeight);
            $modx->setPlaceholder('register.recaptcha_html',$html);
        } else {
            $modx->log(modX::LOG_LEVEL_ERROR,'[Register] '.$this->modx->lexicon('register.recaptcha_err_load'));
        }
    }
    
    /* check for POST */
    if (!empty($_POST) && (empty($submitVar) || !empty($_POST[$submitVar]))) {
        $modx->lexicon->load('login:register');
    
        /* set default properties */
        $usernameField = $modx->getOption('usernameField',$scriptProperties,'username');
        $emailField = $modx->getOption('emailField',$scriptProperties,'email');
        $passwordField = $modx->getOption('passwordField',$scriptProperties,'password');
        $properties = array();
    
        /* handle validation */
        $login->loadValidator();
        $fields = $login->validator->validateFields($_POST);
    
        if (empty($fields[$usernameField])) {
            $login->validator->errors[$usernameField] = $modx->lexicon('register.field_required');
        } else {
            /* make sure username isnt taken */
            $alreadyExists = $modx->getObject('modUser',array('username' => $fields[$usernameField]));
            if ($alreadyExists) {
                $cachePwd = $alreadyExists->get('cachepwd');
                if ($alreadyExists->get('active') == 0 && !empty($cachePwd)) {
                    /* if inactive and has a cachepwd, probably an expired
                     * activation account, so let's remove it
                     * and let user re-register
                     */
                    if (!$alreadyExists->remove()) {
                        $modx->log(modX::LOG_LEVEL_ERROR,'[Login] Could not remove old, deactive user with cachepwd.');
                    }
                } else {
                    $login->validator->errors[$usernameField] = $modx->lexicon('register.username_taken');
                }
            }
        }
        
        if (empty($fields[$passwordField])) {
            $login->validator->errors[$passwordField] = $modx->lexicon('register.field_required');
        }
        if (empty($fields[$emailField])) {
            $login->validator->errors[$emailField] = $modx->lexicon('register.field_required');
        }
    
        if (empty($login->validator->errors)) {
            /* do pre-register hooks */
            $login->loadHooks('preHooks');
            $login->preHooks->loadMultiple($preHooks,$fields,array(
                'submitVar' => $submitVar,
                'usernameField' => $usernameField,
            ));
            if (!empty($login->preHooks->fields)) {
                $fields = $login->preHooks->fields;
            }
    
            /* process hooks */
            if (!empty($login->preHooks->errors)) {
                $errors = array();
                foreach ($login->preHooks->errors as $key => $error) {
                    $errors[$key] = str_replace('[[+error]]',$error,$errTpl);
                }
                $modx->toPlaceholders($errors,'error');
    
                $errorMsg = $login->preHooks->getErrorMessage();
                $modx->toPlaceholder('message',$errorMsg,'error');
            } else {
                /* everything good, go ahead and register */
                $result = require_once $login->config['processorsPath'].'register.php';
                if ($result !== true) {
                    $modx->setPlaceholder('error.message',$result);
                }
            }
        } else {
            $errors = array();
            foreach ($login->validator->errors as $key => $error) {
                $errors[$key] = str_replace('[[+error]]',$error,$errTpl);
            }
            $modx->toPlaceholders($errors,'error');
        }
        $modx->toPlaceholders($fields);
    }
    
    return '';



    and my ConfirmRegister snippet:
    <?php
    /**
     * Register
     *
     * Copyright 2010 by Shaun McCormick <[email protected]>
     *
     * Register is free software; you can redistribute it and/or modify it under the
     * terms of the GNU General Public License as published by the Free Software
     * Foundation; either version 2 of the License, or (at your option) any later
     * version.
     *
     * Register is distributed in the hope that it will be useful, but WITHOUT ANY
     * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License along with
     * Register; if not, write to the Free Software Foundation, Inc., 59 Temple
     * Place, Suite 330, Boston, MA 02111-1307 USA
     *
     * @package login
     */
    /**
     * MODx Confirm Register Activation Snippet. Snippet to place on an activation
     * page that the user using the Register snippet would be sent to via the
     * activation email.
     *
     * @package login
     */
    $model_path = $modx->getOption('core_path').'components/login/model/login/';
    $Login = $modx->getService('login','Login',$model_path,$scriptProperties);
    $modx->lexicon->load('login:register');
    
    /* get user from query params */
    if (empty($_REQUEST['lp']) || empty($_REQUEST['lu'])) $modx->sendErrorPage();
    $username = base64_decode(urldecode($_REQUEST['lu']));
    $password = base64_decode(urldecode($_REQUEST['lp']));
    
    /* validate we have correct user */
    $user = $modx->getObject('modUser',array('username' => $username));
    if ($user == null) { $modx->sendErrorPage(); }
    if ($user->get('active')) { $modx->sendErrorPage(); }
    
    /* validate password to prevent middleman attacks */
    $modx->getService('registry', 'registry.modRegistry');
    $modx->registry->addRegister('login','registry.modFileRegister');
    $modx->registry->login->connect();
    $modx->registry->login->subscribe('/useractivation/'.$user->get('username'));
    $msgs = $modx->registry->login->read();
    if (empty($msgs)) $modx->sendErrorPage();
    $found = false;
    foreach ($msgs as $msg) {
        if ($msg == $password) $found = true;
    }
    if (!$found) $modx->sendErrorPage();
    
    /* invoke OnBeforeUserActivateEvent, if result returns anything, do not proceed */
    $result = $modx->invokeEvent('OnBeforeUserActivate',array(
        'user' => &$user,
    ));
    if (!empty($result)) {
        $modx->log(modX::LOG_LEVEL_ERROR,'[Register] OnBeforeUserActivate event prevented activation for "'.$user->get('username').'" by returning false.');
        $modx->sendErrorPage();
    }
    
    /* activate user */
    $user->set('active',1);
    $user->set('cachepwd','');
    $user->save();
    
    /* invoke OnUserActivate event */
    $modx->invokeEvent('OnUserActivate',array(
        'user' => &$user,
    ));
    
    return '';


    I should mention that these snippets haven’t been modified from the original shipment.


      A MODx Fanatic
      • 11055 ☆ A M B ☆
      • 3,112 Posts
      I found an error log on my testing, related with modAccess.
      How about you?
        Rico
        Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
        MODx is great, but knowing how to use it well makes it perfect!

        www.virtudraft.com

        Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

        Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

        Maintainter/contributor of Babel

        Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
        • 30585
        • 833 Posts
        Quote from: goldsky at Nov 26, 2010, 08:48 PM

        I found an error log on my testing, related with modAccess.
        How about you?

        I have a very loaded erro log. So big it’s taking long for dreamweaver to list it.

        But to answer your question, here’s the error I’m receiving with modAccess:

        [2010-11-23 06:07:57] (ERROR @ /index.php) Error 42000 executing statement: 
        Array
        (
            [0] => 42000
            [1] => 1064
            [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `modAccess` WHERE `modAccess`.`principal` = 7' at line 1
        [2010-11-23 06:08:14] (ERROR @ /index.php) modUserProfile: Attempt to set NOT NULL field internalKey to NULL
        [2010-11-23 06:08:14] (ERROR @ /index.php) You must provide an email address to send to.
        [2010-11-23 06:11:31] (ERROR @ /index.php) Could not get table name for class: modAccess
        [2010-11-23 06:11:31] (ERROR @ /index.php) Error 42000 executing statement: 
        Array
        (
            [0] => 42000
            [1] => 1064
            [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `modAccess` WHERE `modAccess`.`principal` = 8' at line 1
        )
        
        [2010-11-23 06:11:48] (ERROR @ /index.php) modUserProfile: Attempt to set NOT NULL field internalKey to NULL
        [2010-11-23 06:11:48] (ERROR @ /index.php) You must provide an email address to send to.
        [2010-11-23 06:14:34] (ERROR @ /index.php) Could not get table name for class: modAccess
        [2010-11-23 06:14:34] (ERROR @ /index.php) Error 42000 executing statement: 
        Array
        (
            [0] => 42000
            [1] => 1064
            [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `modAccess` WHERE `modAccess`.`principal` = 9' at line 1
        )
        
        [2010-11-23 06:15:00] (ERROR @ /index.php) modUserProfile: Attempt to set NOT NULL field internalKey to NULL
        [2010-11-23 06:15:00] (ERROR @ /index.php) You must provide an email address to send to.
        [2010-11-23 06:18:49] (ERROR @ /index.php) modUserProfile: Attempt to set NOT NULL field internalKey to NULL
        [2010-11-23 06:18:49] (ERROR @ /index.php) You must provide an email address to send to.
        [2010-11-23 06:20:58] (ERROR @ /index.php) Could not get table name for class: modAccess
        [2010-11-23 06:20:58] (ERROR @ /index.php) Error 42000 executing statement: 
        Array
        (
            [0] => 42000
            [1] => 1064
            [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `modAccess` WHERE `modAccess`.`principal` = 11' at line 1
        )
        
        [2010-11-23 06:21:15] (ERROR @ /index.php) modUserProfile: Attempt to set NOT NULL field internalKey to NULL
        [2010-11-23 06:21:15] (ERROR @ /index.php) You must provide an email address to send to.
        [2010-11-23 06:21:15] (ERROR @ /index.php) Could not get table name for class: modAccess
        [2010-11-23 06:21:15] (ERROR @ /index.php) Error 42000 executing statement: 
        Array
        (
            [0] => 42000
            [1] => 1064
            [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `modAccess` WHERE `modAccess`.`principal` = 12' at line 1
        )
        
          A MODx Fanatic
          • 11055 ☆ A M B ☆
          • 3,112 Posts
          I remember that login snippet has been updated several times. Which version do you have?
            Rico
            Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
            MODx is great, but knowing how to use it well makes it perfect!

            www.virtudraft.com

            Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

            Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

            Maintainter/contributor of Babel

            Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
            • 30585
            • 833 Posts
            Quote from: goldsky at Nov 26, 2010, 10:53 PM

            I remember that login snippet has been updated several times. Which version do you have?

            hey thanks for baring with me.

            I’m using Login 1.4.0 RC1

            What did u think of those error messages. there seems to be an issue with some tables
              A MODx Fanatic
              • 11055 ☆ A M B ☆
              • 3,112 Posts
              I guess it’s the class mapping.
              I’m on the road.
              I’ll dig it deeper when I get home.
                Rico
                Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
                MODx is great, but knowing how to use it well makes it perfect!

                www.virtudraft.com

                Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

                Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

                Maintainter/contributor of Babel

                Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
                • 30585
                • 833 Posts
                Thanks for all your help. i appreciate it
                  A MODx Fanatic
                  • 11055 ☆ A M B ☆
                  • 3,112 Posts
                    Rico
                    Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
                    MODx is great, but knowing how to use it well makes it perfect!

                    www.virtudraft.com

                    Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

                    Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

                    Maintainter/contributor of Babel

                    Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
                    • 30585
                    • 833 Posts
                    Quote from: goldsky at Nov 27, 2010, 06:16 PM

                    done.
                    http://bugs.modx.com/issues/2870

                    thx.

                    Actually, I’m hunting another bug here:
                    http://modxcms.com/forums/index.php/topic,57660.msg330304.html#msg330304

                    Perhaps you can catch it faster. laugh


                    I believe, this also needs to be changed:
                    <?php
                    
                    //    $activationEmail = $modx->getOption('activationEmail', $scriptProperties, $user->get('email'));
                        $activationEmail = $user->get('email');
                    


                    And I add this into the register.php
                    <?php // highlighting
                    
                        /* now set extended data */
                        if (!empty($extended))
                            $profile->set('extended', $extended);
                    


                    Hey Goldsky, I changed line 127 and 129 as suggested your post and here:https://github.com/splittingred/Login/issues/#issue/27 . I also added the additional code you suggested on line 135:

                    He’re what I have:
                    <?php
                    /**
                     * Login
                     *
                     * Copyright 2010 by Jason Coward <[email protected]> and Shaun McCormick
                     * <[email protected]>
                     *
                     * Login is free software; you can redistribute it and/or modify it
                     * under the terms of the GNU General Public License as published by the Free
                     * Software Foundation; either version 2 of the License, or (at your option) any
                     * later version.
                     *
                     * Login is distributed in the hope that it will be useful, but WITHOUT ANY
                     * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
                     * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
                     *
                     * You should have received a copy of the GNU General Public License along with
                     * Login; if not, write to the Free Software Foundation, Inc., 59 Temple
                     * Place, Suite 330, Boston, MA 02111-1307 USA
                     *
                     * @package login
                     */
                    /**
                     * Handle register form
                     *
                     * @package login
                     * @subpackage processors
                     */
                    /* get rid of spam fields, submitVar field */
                    unset($fields['nospam'],$fields['blank']);
                    if (!empty($submitVar)) unset($fields[$submitVar]);
                    
                    /* create user and profile */
                    $user = $modx->newObject('modUser');
                    $profile = $modx->newObject('modUserProfile');
                    
                    /* set extended data if any */
                    if ($modx->getOption('useExtended',$scriptProperties,true)) {
                        /* first cut out regular and unwanted fields */
                        $excludeExtended = $modx->getOption('excludeExtended',$scriptProperties,'');
                        $excludeExtended = explode(',',$excludeExtended);
                        $profileFields = $profile->toArray();
                        $userFields = $user->toArray();
                        $extended = array();
                        foreach ($fields as $field => $value) {
                            if (!isset($profileFields[$field]) && !isset($userFields[$field]) && $field != 'password_confirm' && $field != 'passwordconfirm' && !in_array($field,$excludeExtended)) {
                                $extended[$field] = $value;
                            }
                        }
                        /* now set extended data */
                        $profile->set('extended',$extended);
                    }
                    
                    /* set user and profile */
                    $user->fromArray($fields);
                    $user->set('class_key','modUser');
                    $user->set('username',$fields[$usernameField]);
                    $user->set('active',0);
                    $user->set('password',md5($fields['password']));
                    $profile->fromArray($fields);
                    $user->addOne($profile,'Profile');
                    
                    /* if usergroups set */
                    $usergroups = $modx->getOption('usergroups',$scriptProperties,'');
                    if (!empty($usergroups)) {
                        $usergroups = explode(',',$usergroups);
                    
                        foreach ($usergroups as $usergroupPk) {
                            $pk = array();
                            if (is_numeric($usergroupPk)) {
                                $pk['id'] = $usergroupPk;
                            } else {
                                $pk['name'] = $usergroupPk;
                            }
                            $usergroup = $modx->getObject('modUserGroup',$pk);
                            if (!$usergroup) continue;
                    
                            $member = $modx->newObject('modUserGroupMember');
                            $member->set('member',$user->get('id'));
                            $member->set('user_group',$usergroup->get('id'));
                            $user->addMany($member,'UserGroupMembers');
                        }
                    }
                    
                    /* save user */
                    if (!$user->save()) {
                        $modx->log(modX::LOG_LEVEL_ERROR,'[Login] Could not save newly registered user: '.$user->get('id').' with username: '.$user->get('username'));
                        return $modx->lexicon('register.user_err_save');
                    }
                    
                    /* send activation email (if chosen) */
                    $email = $user->get('email');
                    $activation = $modx->getOption('activation',$scriptProperties,true);
                    $activateResourceId = $modx->getOption('activationResourceId',$scriptProperties,'');
                    if ($activation && !empty($email) && !empty($activateResourceId)) {
                        /* generate a password and encode it and the username into the url */
                        $pword = $login->generatePassword();
                        $confirmParams = 'lp='.urlencode(base64_encode($pword));
                        $confirmParams .= '&lu='.urlencode(base64_encode($user->get('username')));
                        $confirmUrl = $modx->makeUrl($activateResourceId,'',$confirmParams,'full');
                    
                        /* set the email properties */
                        $emailTpl = $modx->getOption('activationEmailTpl',$scriptProperties,'lgnActivateEmail');
                        $emailTplType = $modx->getOption('activationEmailTplType',$scriptProperties,'modChunk');
                        $emailProperties = $user->toArray();
                        $emailProperties['confirmUrl'] = $confirmUrl;
                        $emailProperties['tpl'] = $emailTpl;
                        $emailProperties['tplType'] = $emailTplType;
                        $emailProperties['password'] = $fields['password'];
                    
                        /* now set new password to registry to prevent middleman attacks.
                         * Will read from the registry on the confirmation page. */
                    
                        $modx->getService('registry', 'registry.modRegistry');
                        $modx->registry->addRegister('login','registry.modFileRegister');
                        $modx->registry->login->connect();
                        $modx->registry->login->subscribe('/useractivation/');
                        $modx->registry->login->send('/useractivation/',array($user->get('username') => $pword),array(
                            'ttl' => ($modx->getOption('activationttl',$scriptProperties,180)*60),
                        ));
                        /* set cachepwd here to prevent re-registration of inactive users */
                        $user->set('cachepwd',md5($pword));
                        if (!$user->save()) {
                            $modx->log(modX::LOG_LEVEL_ERROR,'[Login] Could not update cachepwd for activation for User: '.$user->get('username'));
                        }
                    
                        $activationEmail = $user->get('email');//$activationEmail = $modx->getOption('activationEmail',$scriptProperties,$user->get('email'));
                        $subject = $modx->getOption('activationEmailSubject',$scriptProperties,$modx->lexicon('register.activation_email_subject'));
                        $login->sendEmail($user->get('email'),$user->get('username'),$subject,$emailProperties);//$login->sendEmail($activationEmail,$user->get('username'),$subject,$emailProperties);
                    
                    } else {
                        $user->set('active',1);
                        $user->save();
                    }
                    
                    /* now set extended data */
                        if (!empty($extended))
                            $profile->set('extended', $extended);
                    
                    /* do post-register hooks */
                    $postHooks = $modx->getOption('postHooks',$scriptProperties,'');
                    $login->loadHooks('posthooks');
                    $fields['register.user'] = &$user;
                    $fields['register.profile'] =& $profile;
                    $fields['register.usergroups'] = $usergroups;
                    $login->posthooks->loadMultiple($postHooks,$fields);
                    
                    /* process hooks */
                    if (!empty($login->posthooks->errors)) {
                        $errors = array();
                        foreach ($login->posthooks->errors as $key => $error) {
                            $errors[$key] = str_replace('[[+error]]',$error,$errTpl);
                        }
                        $modx->toPlaceholders($errors,'error');
                    
                        $errorMsg = $login->posthooks->getErrorMessage();
                        $modx->toPlaceholder('message',$errorMsg,'error');
                    }
                    
                    /* if provided a redirect id, will redirect to that resource, with the
                     * GET params `username` and `email` for you to use */
                    $submittedResourceId = $modx->getOption('submittedResourceId',$scriptProperties,'');
                    if (!empty($submittedResourceId)) {
                        $url = $modx->makeUrl($submittedResourceId,'',array(
                            'username' => $user->get('username'),
                            'email' => $profile->get('email'),
                        ));
                        $modx->sendRedirect($url);
                    } else {
                        $successMsg = $modx->getOption('successMsg',$scriptProperties,'');
                        $modx->toPlaceholder('error.message',$successMsg);
                    }
                    
                    return true;
                    


                    Do you see any issue with this code?

                    It’s still hanging. i think we’re almost there
                      A MODx Fanatic
                      • 11055 ☆ A M B ☆
                      • 3,112 Posts
                      what’s hanging?
                      i’ve gotten my confirmation email on my testing.

                      The $extended is a replacement. Not actually a brand new adding.

                      register.php
                      <?php
                      
                      /**
                       * Login
                       *
                       * Copyright 2010 by Jason Coward <[email protected]> and Shaun McCormick
                       * <[email protected]>
                       *
                       * Login is free software; you can redistribute it and/or modify it
                       * under the terms of the GNU General Public License as published by the Free
                       * Software Foundation; either version 2 of the License, or (at your option) any
                       * later version.
                       *
                       * Login is distributed in the hope that it will be useful, but WITHOUT ANY
                       * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
                       * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
                       *
                       * You should have received a copy of the GNU General Public License along with
                       * Login; if not, write to the Free Software Foundation, Inc., 59 Temple
                       * Place, Suite 330, Boston, MA 02111-1307 USA
                       *
                       * @package login
                       */
                      /**
                       * Handle register form
                       *
                       * @package login
                       * @subpackage processors
                       */
                      /* get rid of spam fields, submitVar field */
                      unset($fields['nospam'], $fields['blank']);
                      if (!empty($submitVar))
                          unset($fields[$submitVar]);
                      
                      /* create user and profile */
                      $user = $modx->newObject('modUser');
                      $profile = $modx->newObject('modUserProfile');
                      
                      /* set extended data if any */
                      if ($modx->getOption('useExtended', $scriptProperties, true)) {
                          /* first cut out regular and unwanted fields */
                          $excludeExtended = $modx->getOption('excludeExtended', $scriptProperties, '');
                          $excludeExtended = explode(',', $excludeExtended);
                          $profileFields = $profile->toArray();
                          $userFields = $user->toArray();
                          $extended = array();
                          foreach ($fields as $field => $value) {
                              if (!isset($profileFields[$field])
                                      && !isset($userFields[$field])
                                      && $field != 'password_confirm'
                                      && $field != 'passwordconfirm'
                                      && !in_array($field, $excludeExtended)
                              ) {
                                  $extended[$field] = $value;
                              }
                          }
                      
                          /* now set extended data */
                          if (!empty($extended))
                              $profile->set('extended', $extended);
                      }
                      
                      /* set user and profile */
                      $user->fromArray($fields);
                      $user->set('class_key', 'modUser');
                      $user->set('username', $fields[$usernameField]);
                      $user->set('active', 0);
                      $user->set('password', md5($fields['password']));
                      $profile->fromArray($fields);
                      $user->addOne($profile, 'Profile');
                      
                      /* if usergroups set */
                      $usergroups = $modx->getOption('usergroups', $scriptProperties, '');
                      if (!empty($usergroups)) {
                          $usergroups = explode(',', $usergroups);
                      
                          foreach ($usergroups as $usergroupPk) {
                              $pk = array();
                              if (is_numeric($usergroupPk)) {
                                  $pk['id'] = $usergroupPk;
                              } else {
                                  $pk['name'] = $usergroupPk;
                              }
                              $usergroup = $modx->getObject('modUserGroup', $pk);
                              if (!$usergroup)
                                  continue;
                      
                              $member = $modx->newObject('modUserGroupMember');
                              $member->set('member', $user->get('id'));
                              $member->set('user_group', $usergroup->get('id'));
                              $user->addMany($member, 'UserGroupMembers');
                          }
                      }
                      
                      /* save user */
                      if (!$user->save()) {
                          $modx->log(modX::LOG_LEVEL_ERROR, '[Login] Could not save newly registered user: ' . $user->get('id')
                                  . ' with username: ' . $user->get('username'));
                          return $modx->lexicon('register.user_err_save');
                      }
                      
                      /* send activation email (if chosen) */
                      $email = $user->get('email');
                      $activation = $modx->getOption('activation', $scriptProperties, true);
                      $activateResourceId = $modx->getOption('activationResourceId', $scriptProperties, '');
                      
                      if ($activation && !empty($email) && !empty($activateResourceId)) {
                      
                          /* generate a password and encode it and the username into the url */
                          $pword = $login->generatePassword();
                          $confirmParams = 'lp=' . urlencode(base64_encode($pword));
                          $confirmParams .= '&lu=' . urlencode(base64_encode($user->get('username')));
                          $confirmUrl = $modx->makeUrl($activateResourceId, '', $confirmParams, 'full');
                      
                          /* set the email properties */
                          $emailTpl = $modx->getOption('activationEmailTpl', $scriptProperties, 'lgnActivateEmail');
                          $emailTplType = $modx->getOption('activationEmailTplType', $scriptProperties, 'modChunk');
                          $emailProperties = $user->toArray();
                          $emailProperties['confirmUrl'] = $confirmUrl;
                          $emailProperties['tpl'] = $emailTpl;
                          $emailProperties['tplType'] = $emailTplType;
                          $emailProperties['password'] = $fields['password'];
                      
                          /* now set new password to registry to prevent middleman attacks.
                           * Will read from the registry on the confirmation page. */
                      
                          $modx->getService('registry', 'registry.modRegistry');
                          $modx->registry->addRegister('login', 'registry.modFileRegister');
                          $modx->registry->login->connect();
                          $modx->registry->login->subscribe('/useractivation/');
                          $modx->registry->login->send('/useractivation/', array($user->get('username') => $pword), array(
                              'ttl' => ($modx->getOption('activationttl', $scriptProperties, 180) * 60),
                          ));
                          /* set cachepwd here to prevent re-registration of inactive users */
                          $user->set('cachepwd', md5($pword));
                          if (!$user->save()) {
                              $modx->log(modX::LOG_LEVEL_ERROR, '[Login] Could not update cachepwd for activation for User: ' . $user->get('username'));
                          }
                      
                      //    $activationEmail = $modx->getOption('activationEmail', $scriptProperties, $user->get('email'));
                          $activationEmail = $user->get('email');
                      
                          $subject = $modx->getOption('activationEmailSubject', $scriptProperties, $modx->lexicon('register.activation_email_subject'));
                          $login->sendEmail($activationEmail, $user->get('username'), $subject, $emailProperties);
                      } else {
                          $user->set('active', 1);
                          $user->save();
                      }
                      
                      /* do post-register hooks */
                      $postHooks = $modx->getOption('postHooks', $scriptProperties, '');
                      $login->loadHooks('posthooks');
                      $fields['register.user'] = &$user;
                      $fields['register.profile'] = & $profile;
                      $fields['register.usergroups'] = $usergroups;
                      $login->posthooks->loadMultiple($postHooks, $fields);
                      
                      /* process hooks */
                      if (!empty($login->posthooks->errors)) {
                          $errors = array();
                          foreach ($login->posthooks->errors as $key => $error) {
                              $errors[$key] = str_replace('[[+error]]', $error, $errTpl);
                          }
                          $modx->toPlaceholders($errors, 'error');
                      
                          $errorMsg = $login->posthooks->getErrorMessage();
                          $modx->toPlaceholder('message', $errorMsg, 'error');
                      }
                      
                      /* if provided a redirect id, will redirect to that resource, with the
                       * GET params `username` and `email` for you to use */
                      $submittedResourceId = $modx->getOption('submittedResourceId', $scriptProperties, '');
                      if (!empty($submittedResourceId)) {
                          $url = $modx->makeUrl($submittedResourceId, '', array(
                                      'username' => $user->get('username'),
                                      'email' => $profile->get('email'),
                                  ));
                          $modx->sendRedirect($url);
                      } else {
                          $successMsg = $modx->getOption('successMsg', $scriptProperties, '');
                          $modx->toPlaceholder('error.message', $successMsg);
                      }
                      
                      return true;
                      
                      
                        Rico
                        Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
                        MODx is great, but knowing how to use it well makes it perfect!

                        www.virtudraft.com

                        Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

                        Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

                        Maintainter/contributor of Babel

                        Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.