File indexing completed on 2024-05-12 05:58:42

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class Default_Form_Register extends Zend_Form
0024 {
0025 
0026     /**
0027      * @throws Zend_Exception
0028      * @throws Zend_Form_Exception
0029      * @throws Zend_Validate_Exception
0030      */
0031     public function init()
0032     {
0033         $this->setMethod('POST');
0034         $this->setAction('/register/');
0035         $this->addElementPrefixPath('Local', 'Local/');
0036         $this->setAttrib('id', 'registerForm');
0037         $this->setAttrib('class', 'standard-form row-fluid center');
0038         $redir = $this->createElement('hidden', 'redirect')->setDecorators(array('ViewHelper'));
0039         $this->addElement($redir);
0040 
0041         $usernameValidChars = new Zend_Validate_Regex('/^(?=.{4,20}$)(?![-])(?!.*[-]{2})[a-z0-9-]+(?<![-])$/');
0042         $userExistCheck = new Local_Validate_UsernameExists();
0043         $userExistCheck->setMessage('This username already exists.', Local_Validate_UsernameExists::EXISTS);
0044         $userExistCheck->setMessage('This username already exists.');
0045         $userEmptyCheck = new Zend_Validate_NotEmpty();
0046         $userEmptyCheck->setMessage('RegisterFormUsernameErr', Zend_Validate_NotEmpty::IS_EMPTY);
0047         $userNameLength = new Zend_Validate_StringLength(array('min' => 4, 'max' => 20));
0048 
0049         $fname = $this->createElement('text', 'username')
0050                       ->setDecorators(array('ViewHelper', 'Errors'))
0051                       ->setRequired(true)
0052                       ->addFilter(new Zend_Filter_StringTrim())
0053                       ->addFilter(new Zend_Filter_StripNewlines())
0054                       ->addValidator($userEmptyCheck, true)
0055                       ->addValidator($userNameLength, true)
0056                       ->addValidator($usernameValidChars, true)
0057                       ->addValidator($userExistCheck, true)
0058                       ->setAttrib('placeholder', 'Username (4 chars minimum)')
0059                       ->setAttrib('class', 'form-control');
0060 
0061         $opencode = Zend_Registry::get('config')->settings->server->opencode;
0062         if ($opencode->host) {
0063             $groupNameExists = new Local_Validate_GroupnameExistsInOpenCode();
0064             $fname->addValidator($groupNameExists, true);
0065         }
0066 
0067         $mailValidCheck = new Zend_Validate_EmailAddress();
0068         $mailValidCheck->setMessage('RegisterFormEmailErrNotValid', Zend_Validate_EmailAddress::INVALID)
0069                        ->setMessage('RegisterFormEmailErrNotValid', Zend_Validate_EmailAddress::INVALID_FORMAT)
0070                        ->setMessage('RegisterFormEmailErrNotValid', Zend_Validate_EmailAddress::INVALID_LOCAL_PART)
0071                        ->setMessage("RegisterFormEmailErrWrongHost", Zend_Validate_EmailAddress::INVALID_HOSTNAME)
0072                        ->setMessage("RegisterFormEmailErrWrongHost2", Zend_Validate_Hostname::INVALID_HOSTNAME)
0073                        ->setMessage("RegisterFormEmailErrHostLocal", Zend_Validate_Hostname::LOCAL_NAME_NOT_ALLOWED)
0074                        ->setOptions(array('domain' => true));
0075 
0076         $mailExistCheck = new Local_Validate_EmailExists();
0077         $mailExistCheck->setMessage('RegisterFormEmailErrAlreadyRegistered', Local_Validate_EmailExists::EXISTS);
0078 
0079         $mailEmpty = new Zend_Validate_NotEmpty();
0080         $mailEmpty->setMessage('RegisterFormEmailErrEmpty', Zend_Validate_NotEmpty::IS_EMPTY);
0081 
0082         $mailValidatorChain = new Zend_Validate();
0083         $mailValidatorChain->addValidator($mailEmpty, true)
0084                            ->addValidator($mailValidCheck, true)
0085                            ->addValidator($mailExistCheck, true);
0086 
0087         $mail = $this->createElement('text', 'mail')
0088                      ->setLabel('RegisterFormEmailLabel')
0089                      ->addValidator($mailEmpty, true)
0090                      ->addValidator($mailValidCheck, true)
0091                      ->addValidator($mailExistCheck, true)
0092                      ->setDecorators(array('ViewHelper', 'Errors'))
0093                      ->setRequired(true)
0094                      ->setAttrib('placeholder', 'Email')
0095                      ->setAttrib('class', 'form-control');
0096 
0097         $pass1 = $this->createElement('password', 'password1')
0098                       ->setLabel('RegisterFormPasswordLabel')
0099                       ->setRequired(true)//->addErrorMessage('RegisterFormPasswordErr')
0100                       ->setDecorators(array('ViewHelper', 'Errors'))
0101                       ->setAttrib('placeholder', 'Password')
0102                       ->addValidator('stringLength', true, array(6, 200))
0103                       ->setAttrib('placeholder', 'Password (6 chars minimum)')
0104                       ->setAttrib('class', 'form-control');
0105 
0106         $pass2 = $this->createElement('password', 'password2')
0107                       ->setLabel('RegisterFormPassword2Label')
0108                       ->setRequired(true)
0109                       ->addErrorMessage('RegisterFormPassword2Err')
0110                       ->setDecorators(array('ViewHelper', 'Errors'))
0111                       ->setAttrib('placeholder', 'Confirm Password')
0112                       ->setAttrib('class', 'form-control');
0113 
0114         $passValid = new Local_Validate_PasswordConfirm($pass2->getValue());
0115         $pass1->addValidator($passValid, true);
0116 
0117         $submit = $this->createElement('button', 'login');
0118         $submit->setLabel('Register');
0119         $submit->setDecorators(array('ViewHelper'));
0120         $submit->setAttrib('class', 'btn btn-native btn-min-width');
0121         $submit->setAttrib('type', 'submit');
0122 
0123         $this->addElement($fname)
0124              ->addElement($mail)
0125              ->addElement($pass1)
0126              ->addElement($pass2)
0127              ->addElement($submit);
0128 
0129         if (APPLICATION_ENV == 'development') {
0130             return;
0131         }
0132 
0133         $this->addPrefixPath('Cgsmith\\Form\\Element', APPLICATION_LIB . '/Cgsmith/Form/Element', Zend_Form::ELEMENT);
0134         $this->addElementPrefixPath('Cgsmith\\Validate\\', APPLICATION_LIB . '/Cgsmith/Validate/',
0135             Zend_Form_Element::VALIDATE);
0136         $captcha = $this->createElement('recaptcha', 'g-recaptcha-response', array(
0137             'siteKey'   => Zend_Registry::get('config')->recaptcha->sitekey,
0138             'secretKey' => Zend_Registry::get('config')->recaptcha->secretkey,
0139         ));
0140 
0141         $this->addElement($captcha);
0142     }
0143 
0144 }