MDL-66999 theme_boost: @extend replace .alert
[moodle.git] / admin / mnet / peer_forms.php
CommitLineData
96bd2921
PL
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
96bd2921
PL
18/**
19 * This file contains two forms for adding/editing mnet hosts, used by peers.php
20 *
e7c1d021
DM
21 * @package core
22 * @subpackage mnet
96bd2921
PL
23 * @copyright 2010 Penny Leach
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
e7c1d021
DM
27defined('MOODLE_INTERNAL') || die();
28
96bd2921
PL
29require_once($CFG->libdir . '/formslib.php');
30
31/**
32 * The very basic first step add new host form - just wwwroot & application
33 * The second form is loaded up with the information from this one.
34 */
35class mnet_simple_host_form extends moodleform {
36 function definition() {
37 global $DB;
96bd2921 38
e7c1d021
DM
39 $mform = $this->_form;
40
1322a56d 41 $mform->addElement('text', 'wwwroot', get_string('hostname', 'mnet'), array('maxlength' => 255, 'size' => 50));
96bd2921
PL
42 $mform->setType('wwwroot', PARAM_URL);
43 $mform->addRule('wwwroot', null, 'required', null, 'client');
1322a56d 44 $mform->addRule('wwwroot', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
e7c1d021
DM
45
46 $mform->addElement('select', 'applicationid', get_string('applicationtype', 'mnet'),
47 $DB->get_records_menu('mnet_application', array(), 'id,display_name'));
96bd2921
PL
48 $mform->addRule('applicationid', null, 'required', null, 'client');
49
50 $this->add_action_buttons(false, get_string('addhost', 'mnet'));
51 }
52
82195023 53 function validation($data, $files) {
96bd2921 54 global $DB;
e7c1d021 55
96bd2921
PL
56 $wwwroot = $data['wwwroot'];
57 // ensure the wwwroot starts with a http or https prefix
58 if (strtolower(substr($wwwroot, 0, 4)) != 'http') {
59 $wwwroot = 'http://'.$wwwroot;
60 }
61 if ($host = $DB->get_record('mnet_host', array('wwwroot' => $wwwroot))) {
62 global $CFG;
20ec5650 63 return array('wwwroot' => get_string('hostexists', 'mnet',
4bd8e652 64 new moodle_url('/admin/mnet/peers.php', array('hostid' => $host->id))));
96bd2921
PL
65 }
66 return array();
67 }
68}
69
70/**
71 * The second step of the form - reviewing the host details
72 * This is also the same form that is used for editing an existing host
73 */
74class mnet_review_host_form extends moodleform {
75 function definition() {
76 global $OUTPUT;
e7c1d021
DM
77
78 $mform = $this->_form;
96bd2921
PL
79 $mnet_peer = $this->_customdata['peer'];
80
81 $mform->addElement('hidden', 'last_connect_time');
c71d9297 82 $mform->setType('last_connect_time', PARAM_INT);
96bd2921 83 $mform->addElement('hidden', 'id');
c71d9297 84 $mform->setType('id', PARAM_INT);
96bd2921 85 $mform->addElement('hidden', 'applicationid');
c71d9297 86 $mform->setType('applicationid', PARAM_INT);
96bd2921 87 $mform->addElement('hidden', 'oldpublickey');
c71d9297 88 $mform->setType('oldpublickey', PARAM_PEM);
96bd2921 89
1322a56d 90 $mform->addElement('text', 'name', get_string('site'), array('maxlength' => 80, 'size' => 50));
96bd2921 91 $mform->setType('name', PARAM_NOTAGS);
1322a56d 92 $mform->addRule('name', get_string('maximumchars', '', 80), 'maxlength', 80, 'client');
e7c1d021 93
1322a56d 94 $mform->addElement('text', 'wwwroot', get_string('hostname', 'mnet'), array('maxlength' => 255, 'size' => 50));
3cd7e90a 95 $mform->setType('wwwroot', PARAM_URL);
1322a56d 96 $mform->addRule('wwwroot', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
7c70229e 97
93539c44
FM
98 $options = array(
99 mnet_peer::SSL_NONE => get_string('none'),
100 mnet_peer::SSL_HOST => get_string('verifyhostonly', 'core_mnet'),
101 mnet_peer::SSL_HOST_AND_PEER => get_string('verifyhostandpeer', 'core_mnet')
102 );
103 $mform->addElement('select', 'sslverification', get_string('sslverification', 'core_mnet'), $options);
104 $mform->setDefault('sslverification', mnet_peer::SSL_HOST_AND_PEER);
105 $mform->addHelpButton('sslverification', 'sslverification', 'core_mnet');
106
7c70229e 107 $themes = array('' => get_string('forceno'));
bd3b3bba 108 foreach (array_keys(core_component::get_plugin_list('theme')) as $themename) {
7c70229e
DM
109 $themes[$themename] = get_string('pluginname', 'theme_'.$themename);
110 }
111 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
112
96bd2921 113 $mform->addElement('textarea', 'public_key', get_string('publickey', 'mnet'), array('rows' => 17, 'cols' => 100, 'class' => 'smalltext'));
fa276a0c 114 $mform->setType('public_key', PARAM_PEM);
4460312d 115 $mform->addRule('public_key', get_string('required'), 'required');
96bd2921 116
96bd2921
PL
117 // finished with form controls, now the static informational stuff
118 if ($mnet_peer && !empty($mnet_peer->bootstrapped)) {
119 $expires = '';
120 if ($mnet_peer->public_key_expires < time()) {
121 $expires = get_string('expired', 'mnet') . ' ';
122 }
123 $expires .= userdate($mnet_peer->public_key_expires);
124 $mform->addElement('static', 'validuntil', get_string('expires', 'mnet'), $expires);
125
126 $lastconnect = '';
127 if ($mnet_peer->last_connect_time == 0) {
128 $lastconnect = get_string('never', 'mnet');
129 } else {
130 $lastconnect = date('H:i:s d/m/Y',$mnet_peer->last_connect_time);
131 }
132
133 $mform->addElement('static', 'lastconnect', get_string('last_connect_time', 'mnet'), $lastconnect);
134 $mform->addElement('static', 'ipaddress', get_string('ipaddress', 'mnet'), $mnet_peer->ip_address);
135
fa276a0c
PL
136 if (isset($mnet_peer->currentkey)) { // key being published is not the same as our records
137 $currentkeystr = '<b>' . get_string('keymismatch', 'mnet') . '</b><br /><br /> ' . $OUTPUT->box('<pre>' . $mnet_peer->currentkey . '</pre>');
138 $mform->addElement('static', 'keymismatch', get_string('currentkey', 'mnet'), $currentkeystr);
139 }
96bd2921
PL
140
141 $credstr = '';
fa276a0c
PL
142 if ($credentials = $mnet_peer->check_credentials($mnet_peer->public_key)) {
143 foreach($credentials['subject'] as $key => $credential) {
144 if (is_scalar($credential)) {
145 $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.$credential."\n";
146 } else {
147 $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.var_export($credential,1)."\n";
148 }
96bd2921
PL
149 }
150 }
151
8a6f2291
DM
152 $mform->addElement('static', 'certdetails', get_string('certdetails', 'mnet'),
153 $OUTPUT->box('<pre>' . $credstr . '</pre>', 'generalbox certdetails'));
154 }
155
156 if ($mnet_peer && !empty($mnet_peer->deleted)) {
157 $radioarray = array();
f20edd52 158 $radioarray[] = $mform->createElement('static', 'deletedinfo', '',
579ca95f 159 $OUTPUT->container(get_string('deletedhostinfo', 'mnet'), 'alert alert-warning'));
f20edd52
PS
160 $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('yes'), 1);
161 $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('no'), 0);
8a6f2291
DM
162 $mform->addGroup($radioarray, 'radioar', get_string('deleted'), array(' ', ' '), false);
163 } else {
164 $mform->addElement('hidden', 'deleted');
c71d9297 165 $mform->setType('deleted', PARAM_BOOL);
96bd2921
PL
166 }
167
96bd2921
PL
168 // finished with static stuff, print save button
169 $this->add_action_buttons(false);
170 }
171
82195023 172 function validation($data, $files) {
96bd2921
PL
173 $errors = array();
174 if ($data['oldpublickey'] == $data['public_key']) {
175 return;
176 }
177 $mnet_peer = new mnet_peer(); // idiotic api
178 $mnet_peer->wwwroot = $data['wwwroot']; // just hard-set this rather than bootstrap the object
4460312d
SH
179 if (empty($data['public_key'])) {
180 $errors['public_key'] = get_string('publickeyrequired', 'mnet');
181 } else if (!$credentials = $mnet_peer->check_credentials($data['public_key'])) {
96bd2921
PL
182 $errmsg = '';
183 foreach ($mnet_peer->error as $err) {
184 $errmsg .= $err['code'] . ': ' . $err['text'].'<br />';
185 }
186 $errors['public_key'] = get_string('invalidpubkey', 'mnet', $errmsg);
187 }
188 unset($mnet_peer);
189 return $errors;
190 }
191}