| 1 | <?php␊ |
| 2 | /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */␊ |
| 3 | /*␊ |
| 4 | # ***** BEGIN LICENSE BLOCK *****␊ |
| 5 | # This file is part of InDefero, an open source project management application.␊ |
| 6 | # Copyright (C) 2008-2011 CĂ©ondo Ltd and contributors.␊ |
| 7 | #␊ |
| 8 | # InDefero is free software; you can redistribute it and/or modify␊ |
| 9 | # it under the terms of the GNU General Public License as published by␊ |
| 10 | # the Free Software Foundation; either version 2 of the License, or␊ |
| 11 | # (at your option) any later version.␊ |
| 12 | #␊ |
| 13 | # InDefero is distributed in the hope that it will be useful,␊ |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of␊ |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␊ |
| 16 | # GNU General Public License for more details.␊ |
| 17 | #␊ |
| 18 | # You should have received a copy of the GNU General Public License␊ |
| 19 | # along with this program; if not, write to the Free Software␊ |
| 20 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA␊ |
| 21 | #␊ |
| 22 | # ***** END LICENSE BLOCK ***** */␊ |
| 23 | ␊ |
| 24 | /**␊ |
| 25 | * Create a new user account.␊ |
| 26 | *␊ |
| 27 | */␊ |
| 28 | class IDF_Form_Register extends Pluf_Form␊ |
| 29 | {␊ |
| 30 | protected $request;␊ |
| 31 | ␊ |
| 32 | public function initFields($extra=array())␊ |
| 33 | {␊ |
| 34 | $this->request = $extra['request'];␊ |
| 35 | $login = '';␊ |
| 36 | if (isset($extra['initial']['login'])) {␊ |
| 37 | $login = $extra['initial']['login'];␊ |
| 38 | }␊ |
| 39 | $this->fields['login'] = new Pluf_Form_Field_Varchar(␊ |
| 40 | array('required' => true,␊ |
| 41 | 'label' => __('Your login'),␊ |
| 42 | 'max_length' => 15,␊ |
| 43 | 'min_length' => 3,␊ |
| 44 | 'initial' => $login,␊ |
| 45 | 'help_text' => __('The login must be between 3 and 15 characters long and contain only letters and digits.'),␊ |
| 46 | 'widget_attrs' => array(␊ |
| 47 | 'maxlength' => 15,␊ |
| 48 | 'size' => 10,␊ |
| 49 | ),␊ |
| 50 | ));␊ |
| 51 | $this->fields['email'] = new Pluf_Form_Field_Email(␊ |
| 52 | array('required' => true,␊ |
| 53 | 'label' => __('Your email'),␊ |
| 54 | 'initial' => '',␊ |
| 55 | 'help_text' => __('We will never send you any unsolicited emails. We hate spam too!'),␊ |
| 56 | ));␊ |
| 57 | ␊ |
| 58 | $this->fields['terms'] = new Pluf_Form_Field_Boolean(␊ |
| 59 | array('required' => true,␊ |
| 60 | 'label' => __('I agree to the terms and conditions.'),␊ |
| 61 | 'initial' => '',␊ |
| 62 | ));␊ |
| 63 | }␊ |
| 64 | ␊ |
| 65 | /**␊ |
| 66 | * Validate the interconnection in the form.␊ |
| 67 | */␊ |
| 68 | public function clean_login()␊ |
| 69 | {␊ |
| 70 | $this->cleaned_data['login'] = mb_strtolower(trim($this->cleaned_data['login']));␊ |
| 71 | if (preg_match('/[^A-Za-z0-9]/', $this->cleaned_data['login'])) {␊ |
| 72 | throw new Pluf_Form_Invalid(sprintf(__('The login "%s" can only contain letters and digits.'), $this->cleaned_data['login']));␊ |
| 73 | }␊ |
| 74 | $guser = new Pluf_User();␊ |
| 75 | $sql = new Pluf_SQL('login=%s', $this->cleaned_data['login']);␊ |
| 76 | if ($guser->getCount(array('filter' => $sql->gen())) > 0) {␊ |
| 77 | throw new Pluf_Form_Invalid(sprintf(__('The login "%s" is already used, please find another one.'), $this->cleaned_data['login']));␊ |
| 78 | }␊ |
| 79 | return $this->cleaned_data['login'];␊ |
| 80 | }␊ |
| 81 | ␊ |
| 82 | /**␊ |
| 83 | * Check the terms.␊ |
| 84 | */␊ |
| 85 | public function clean_terms()␊ |
| 86 | {␊ |
| 87 | if (!$this->cleaned_data['terms']) {␊ |
| 88 | throw new Pluf_Form_Invalid(__('We know, this is boring, but you need to agree with the terms and conditions.'));␊ |
| 89 | }␊ |
| 90 | return $this->cleaned_data['terms'];␊ |
| 91 | }␊ |
| 92 | ␊ |
| 93 | function clean_email()␊ |
| 94 | {␊ |
| 95 | $this->cleaned_data['email'] = mb_strtolower(trim($this->cleaned_data['email']));␊ |
| 96 | if (Pluf::factory('IDF_EmailAddress')->get_user_for_email_address($this->cleaned_data['email']) != null) {␊ |
| 97 | throw new Pluf_Form_Invalid(sprintf(__('The email "%1$s" is already used. If you need to, you can <a href="%2$s">recover your password</a>.'), $this->cleaned_data['email'], Pluf_HTTP_URL_urlForView('IDF_Views::passwordRecoveryAsk')));␊ |
| 98 | }␊ |
| 99 | return $this->cleaned_data['email'];␊ |
| 100 | }␊ |
| 101 | ␊ |
| 102 | /**␊ |
| 103 | * Save the model in the database.␊ |
| 104 | *␊ |
| 105 | * @param bool Commit in the database or not. If not, the object␊ |
| 106 | * is returned but not saved in the database.␊ |
| 107 | * @return Object Model with data set from the form.␊ |
| 108 | */␊ |
| 109 | function save($commit=true)␊ |
| 110 | {␊ |
| 111 | if (!$this->isValid()) {␊ |
| 112 | throw new Exception(__('Cannot save the model from an invalid form.'));␊ |
| 113 | }␊ |
| 114 | $user = new Pluf_User();␊ |
| 115 | $user->first_name = '---'; // with both this set and␊ |
| 116 | // active==false we can find later␊ |
| 117 | // on, all the unconfirmed accounts␊ |
| 118 | // that could be purged.␊ |
| 119 | $user->last_name = $this->cleaned_data['login'];␊ |
| 120 | $user->login = $this->cleaned_data['login'];␊ |
| 121 | $user->email = $this->cleaned_data['email'];␊ |
| 122 | $user->language = $this->request->language_code;␊ |
| 123 | $user->active = false;␊ |
| 124 | $user->create();␊ |
| 125 | self::sendVerificationEmail($user);␊ |
| 126 | return $user;␊ |
| 127 | }␊ |
| 128 | ␊ |
| 129 | public static function sendVerificationEmail($user)␊ |
| 130 | {␊ |
| 131 | Pluf::loadFunction('Pluf_HTTP_URL_urlForView');␊ |
| 132 | $from_email = Pluf::f('from_email');␊ |
| 133 | $cr = new Pluf_Crypt(md5(Pluf::f('secret_key')));␊ |
| 134 | $encrypted = trim($cr->encrypt($user->email.':'.$user->id), '~');␊ |
| 135 | $key = substr(md5(Pluf::f('secret_key').$encrypted), 0, 2).$encrypted;␊ |
| 136 | $url = Pluf::f('url_base').Pluf_HTTP_URL_urlForView('IDF_Views::registerConfirmation', array($key), array(), false);␊ |
| 137 | $urlik = Pluf::f('url_base').Pluf_HTTP_URL_urlForView('IDF_Views::registerInputKey', array(), array(), false);␊ |
| 138 | $context = new Pluf_Template_Context(␊ |
| 139 | array('key' => $key,␊ |
| 140 | 'url' => $url,␊ |
| 141 | 'urlik' => $urlik,␊ |
| 142 | 'user'=> $user,␊ |
| 143 | )␊ |
| 144 | );␊ |
| 145 | $tmpl = new Pluf_Template('idf/register/confirmation-email.txt');␊ |
| 146 | $text_email = $tmpl->render($context);␊ |
| 147 | $email = new Pluf_Mail($from_email, $user->email,␊ |
| 148 | __('Confirm the creation of your account.'));␊ |
| 149 | $email->addTextMessage($text_email);␊ |
| 150 | $email->sendMail();␊ |
| 151 | }␊ |
| 152 | }␊ |
| 153 | |