register.php 8.59 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * This program 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 3 of the License, or
 * (at your option) any later version.
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * This program 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.
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21
 *
 * @package    mahara
 * @subpackage core
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
29
 *
 */

define('INTERNAL', 1);
define('PUBLIC', 1);
30
31
32
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'site');
define('SECTION_PAGE', 'register');
33
require('init.php');
34
require_once('pieforms/pieform.php');
35
require_once('lib/antispam.php');
36
require_once('lib/institution.php');
Martyn Smith's avatar
Martyn Smith committed
37
define('TITLE', get_string('register'));
38
$key = param_alphanum('key', null);
39

40
41
42
43
44
45
46
47
48
49
50
51
/*
 * This page handles three different tasks:
 *   1) Showing a visitor the registration form
 *   2) Telling the visitor to check their e-mail for a message
 *   3) Given a key, display profile information to edit
 *
 * It uses the session to store some state
 */
if (!session_id()) {
    session_start();
}

52
53
// Logged in people can't register. If someone passes a key however, log the 
// user out and see if this key registers someone
Nigel McNie's avatar
Nigel McNie committed
54
if (is_logged_in()) {
55
56
57
58
59
60
    if ($key) {
        $USER->logout();
    }
    else {
        redirect();
    }
61
62
}

63
64
// Step two of registration (first as it's the easiest): the user has
// registered, show them a screen telling them this.
65
if (!$key && !empty($_SESSION['registered'])) {
66
    unset($_SESSION['registered']);
67
    die_info(get_string('registeredok', 'auth.internal'));
68
69
}

70
71
72
73
// The user has registered with an institution that requires approval,
// tell them to wait.
if (!empty($_SESSION['registeredokawaiting'])) {
    unset($_SESSION['registeredokawaiting']);
74
    die_info(get_string('registeredokawaitingemail2', 'auth.internal'));
75
76
}

77
78
79
80
81
if (!empty($_SESSION['registrationcancelled'])) {
    unset($_SESSION['registrationcancelled']);
    die_info(get_string('registrationcancelledok', 'auth.internal'));
}

82
// Step three of registration - given a key register the user
83
if (isset($key)) {
84

85
    // Begin the registration form buliding
86
    if (!$registration = get_record_select('usr_registration', '"key" = ? AND expiry >= ? AND pending != 1', array($key, db_format_timestamp(time())))) {
87
88
89
        die_info(get_string('registrationnosuchkey', 'auth.internal'));
    }

90
91
92
93
94
95
    // In case a new session has started, reset the session language
    // to the one selected during registration
    if (!empty($registration->lang)) {
        $SESSION->set('lang', $registration->lang);
    }

96
    function create_registered_user($profilefields=array()) {
97
        global $registration, $SESSION, $USER;
98
        require_once(get_config('libroot') . 'user.php');
99

100
        db_begin();
101
102
103
104

        // Move the user record to the usr table from the registration table
        $registrationid = $registration->id;
        unset($registration->id);
105
        unset($registration->expiry);
106
        if ($expirytime = get_config('defaultaccountlifetime')) {
107
108
            $registration->expiry = db_format_timestamp(time() + $expirytime);
        }
109
        $registration->lastlogin = db_format_timestamp(time());
110

111
        $authinstance = get_record('auth_instance', 'institution', $registration->institution, 'authname', $registration->authtype ? $registration->authtype : 'internal');
112
        if (false == $authinstance) {
113
            throw new ConfigException('No ' . ($registration->authtype ? $registration->authtype : 'internal') . ' auth instance for institution');
114
115
        }

116
117
118
119
120
        if (!empty($registration->extra)) {
            // Additional user settings were added during confirmation
            $extrafields = unserialize($registration->extra);
        }

121
122
123
124
125
        $user = new User();
        $user->active           = 1;
        $user->authinstance     = $authinstance->id;
        $user->firstname        = $registration->firstname;
        $user->lastname         = $registration->lastname;
126
        $user->email            = $registration->email;
127
128
        $user->username         = get_new_username($user->firstname . $user->lastname);
        $user->passwordchange   = 1;
129

130
131
132
133
134
135
136
137
        if ($registration->institution != 'mahara') {
            if (count_records_select('institution', "name != 'mahara'") == 1 || $registration->pending == 2) {
                if (get_config_plugin('artefact', 'file', 'institutionaloverride')) {
                    $user->quota = get_field('institution', 'defaultquota', 'name', $registration->institution);
                }
            }
        }

138
        create_user($user, $profilefields);
139

140
141
142
143
144
145
146
147
148
149
        // If the institution is 'mahara' then don't do anything
        if ($registration->institution != 'mahara') {
            $institutions = get_records_select_array('institution', "name != 'mahara'");

            // If there is only one available, join it without requiring approval
            if (count($institutions) == 1) {
                $user->join_institution($registration->institution);
            }
            // Else, since there are multiple, request to join
            else {
150
151
152
153
                if ($registration->pending == 2) {
                    if ($confirm = get_field('institution', 'registerconfirm', 'name', $registration->institution)) {
                        $user->join_institution($registration->institution);
                    }
154
155
                }
                else {
156
157
158
159
160
161
162
163
164
165
166
167
                    if ($registration->authtype && $registration->authtype != 'internal') {
                        $auth = AuthFactory::create($authinstance->id);
                        if ($auth->weautocreateusers) {
                            $user->join_institution($registration->institution);
                        }
                        else {
                            $user->add_institution_request($registration->institution);
                        }
                    }
                    else {
                        $user->add_institution_request($registration->institution);
                    }
168
                }
169
            }
170
171
172
173
174
175

            if (!empty($extrafields->institutionstaff)) {
                // If the user isn't a member yet, this does nothing, but that's okay, it'll
                // only be set after successful confirmation.
                set_field('usr_institution', 'staff', 1, 'usr', $user->id, 'institution', $registration->institution);
            }
176
177
        }

178

179
180
181
        if (!empty($registration->lang) && $registration->lang != 'default') {
            set_account_preference($user->id, 'lang', $registration->lang);
        }
182
183

        // Delete the old registration record
184
185
        delete_records('usr_registration', 'id', $registrationid);

186
187
        db_commit();

188
        // Log the user in and send them to the homepage
189
190
        $USER = new LiveUser();
        $USER->reanimate($user->id, $authinstance->id);
Nigel McNie's avatar
Nigel McNie committed
191

192
193
194
195
        if (function_exists('local_post_register')) {
            local_post_register($registration);
        }

196
        $SESSION->add_ok_msg(get_string('registrationcomplete', 'mahara', get_config('sitename')));
197
        $SESSION->set('resetusername', true);
198
        redirect();
199
    }
200
    create_registered_user();
201
202
}

203
204

// Default page - show the registration form
205
206
list($form, $registerconfirm) = auth_generate_registration_form('register', 'internal', '/register.php');
if (!$form) {
207
208
    die_info(get_string('registeringdisallowed'));
}
209
list($formhtml, $js) = auth_generate_registration_form_js($form, $registerconfirm);
210

211
$registerdescription = get_string('registerwelcome');
212
if ($registerterms = get_config('registerterms')) {
213
214
215
216
    $registerdescription .= ' ' . get_string('registeragreeterms');
}
$registerdescription .= ' ' . get_string('registerprivacy');

217
218
$smarty = smarty(array('jquery'));
$smarty->assign('register_form', $formhtml);
219
220
221
222
$smarty->assign('registerdescription', $registerdescription);
if ($registerterms) {
    $smarty->assign('termsandconditions', get_site_page_content('termsandconditions'));
}
223
$smarty->assign('PAGEHEADING', TITLE);
224
$smarty->assign('INLINEJAVASCRIPT', $js);
225
$smarty->display('register.tpl');