MDL-49003 login: prevent signup when user is logged in
[moodle.git] / login / signup.php
CommitLineData
8570cff0 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
c30949a9 19 * user signup page.
8570cff0 20 *
c30949a9
PS
21 * @package core
22 * @subpackage auth
23 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
8570cff0 25 */
26
c30949a9 27require('../config.php');
0859a08d 28require_once($CFG->dirroot . '/user/editlib.php');
8570cff0 29
0448818b
PS
30// Try to prevent searching for sites that allow sign-up.
31if (!isset($CFG->additionalhtmlhead)) {
32 $CFG->additionalhtmlhead = '';
33}
34$CFG->additionalhtmlhead .= '<meta name="robots" content="noindex" />';
35
8570cff0 36if (empty($CFG->registerauth)) {
c30949a9 37 print_error('notlocalisederrormessage', 'error', '', 'Sorry, you may not use this page.');
8570cff0 38}
39$authplugin = get_auth_plugin($CFG->registerauth);
40
41if (!$authplugin->can_signup()) {
c30949a9 42 print_error('notlocalisederrormessage', 'error', '', 'Sorry, you may not use this page.');
8570cff0 43}
44
17c70aa0
PS
45//HTTPS is required in this page when $CFG->loginhttps enabled
46$PAGE->https_required();
47
c93fdc7b 48$PAGE->set_url('/login/signup.php');
a689cd1d 49$PAGE->set_context(context_system::instance());
c30949a9 50
6a2c10a8
MG
51// Override wanted URL, we do not want to end up here again if user clicks "Login".
52$SESSION->wantsurl = $CFG->wwwroot . '/';
53
54if (isloggedin() and !isguestuser()) {
55 // Prevent signing up when already logged in.
56 echo $OUTPUT->header();
57 echo $OUTPUT->box_start();
58 $logout = new single_button(new moodle_url($CFG->httpswwwroot . '/login/logout.php',
59 array('sesskey' => sesskey(), 'loginpage' => 1)), get_string('logout'), 'post');
60 $continue = new single_button(new moodle_url('/'), get_string('cancel'), 'get');
61 echo $OUTPUT->confirm(get_string('cannotsignup', 'error', fullname($USER)), $logout, $continue);
62 echo $OUTPUT->box_end();
63 echo $OUTPUT->footer();
64 exit;
65}
66
57d38adc 67$mform_signup = $authplugin->signup_form();
8570cff0 68
69if ($mform_signup->is_cancelled()) {
70 redirect(get_login_url());
71
72} else if ($user = $mform_signup->get_data()) {
73 $user->confirmed = 0;
74 $user->lang = current_language();
f720c2c0 75 $user->firstaccess = 0;
d3d393ab 76 $user->timecreated = time();
8570cff0 77 $user->mnethostid = $CFG->mnet_localhost_id;
78 $user->secret = random_string(15);
79 $user->auth = $CFG->registerauth;
0859a08d
AA
80 // Initialize alternate name fields to empty strings.
81 $namefields = array_diff(get_all_user_name_fields(), useredit_get_required_name_fields());
82 foreach ($namefields as $namefield) {
83 $user->$namefield = '';
84 }
c30949a9
PS
85
86 $authplugin->user_signup($user, true); // prints notice and link to login/index.php
8570cff0 87 exit; //never reached
88}
89
17c70aa0
PS
90// make sure we really are on the https page when https login required
91$PAGE->verify_https_required();
92
93
8570cff0 94$newaccount = get_string('newaccount');
95$login = get_string('login');
96
8570cff0 97$PAGE->navbar->add($login);
98$PAGE->navbar->add($newaccount);
c93fdc7b 99
8570cff0 100$PAGE->set_title($newaccount);
c93fdc7b 101$PAGE->set_heading($SITE->fullname);
8570cff0 102
103echo $OUTPUT->header();
104$mform_signup->display();
6c3ef410 105echo $OUTPUT->footer();