3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * Page to allow the administrator to configure networked hosts, and add new ones
24 * @copyright 2007 Donal McMullan
25 * @copyright 2007 Martin Langhoff
26 * @copyright 2010 Penny Leach
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require(__DIR__
.'/../../config.php');
31 require_once($CFG->libdir
.'/adminlib.php');
32 require_once($CFG->dirroot
.'/mnet/lib.php');
33 require_once($CFG->dirroot
.'/'.$CFG->admin
.'/mnet/peer_forms.php');
36 /// Initialize variables.
37 $hostid = optional_param('hostid', 0, PARAM_INT
);
38 $updra = optional_param('updateregisterall', 0, PARAM_INT
);
40 // first process the register all hosts setting if required
42 set_config('mnet_register_allhosts', optional_param('registerallhosts', 0, PARAM_INT
));
43 redirect(new moodle_url('/admin/mnet/peers.php'), get_string('changessaved'));
46 $adminsection = 'mnetpeers';
47 if ($hostid && $DB->get_field('mnet_host', 'deleted', array('id' => $hostid)) != 1) {
48 $adminsection = 'mnetpeer' . $hostid;
51 $PAGE->set_url('/admin/mnet/peers.php');
52 admin_externalpage_setup($adminsection);
54 if (!extension_loaded('openssl')) {
55 print_error('requiresopenssl', 'mnet');
58 if (!function_exists('curl_init') ) {
59 print_error('nocurl', 'mnet');
62 if (!function_exists('xmlrpc_encode_request')) {
63 print_error('xmlrpc-missing', 'mnet');
66 if (!isset($CFG->mnet_dispatcher_mode
)) {
67 set_config('mnet_dispatcher_mode', 'off');
70 $mnet_peer = new mnet_peer();
71 $simpleform = new mnet_simple_host_form(); // the one that goes on the bottom of the main page
72 $reviewform = null; // set up later in different code branches, so mnet_peer can be passed to the constructor
74 // if the first form has been submitted, bootstrap the peer and load up the review form
75 if ($formdata = $simpleform->get_data()) {
76 // ensure we remove trailing slashes
77 $formdata->wwwroot
= trim($formdata->wwwroot
);
78 $formdata->wwwroot
= rtrim($formdata->wwwroot
, '/');
80 // ensure the wwwroot starts with a http or https prefix
81 if (strtolower(substr($formdata->wwwroot
, 0, 4)) != 'http') {
82 $formdata->wwwroot
= 'http://'.$formdata->wwwroot
;
85 $mnet_peer->set_applicationid($formdata->applicationid
);
86 $application = $DB->get_field('mnet_application', 'name', array('id'=>$formdata->applicationid
));
87 $mnet_peer->bootstrap($formdata->wwwroot
, null, $application);
88 // bootstrap the second form straight with the data from the first form
89 $reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer)); // the second step (also the edit host form)
90 $formdata->oldpublickey
= $mnet_peer->public_key
; // set this so we can confirm on form post without having to recreate the mnet_peer object
91 $reviewform->set_data($mnet_peer);
92 echo $OUTPUT->header();
93 echo $OUTPUT->box_start();
94 $reviewform->display();
95 echo $OUTPUT->box_end();
96 echo $OUTPUT->footer();
98 } else if ($simpleform->is_submitted()) { // validation failed
102 // editing a host - load up the review form
103 if (!empty($hostid)) {
104 // TODO print a nice little heading
105 $mnet_peer->set_id($hostid);
106 echo $OUTPUT->header();
107 $currenttab = 'mnetdetails';
108 require_once($CFG->dirroot
. '/' . $CFG->admin
. '/mnet/tabs.php');
110 if ($hostid != $CFG->mnet_all_hosts_id
) {
111 $mnet_peer->currentkey
= mnet_get_public_key($mnet_peer->wwwroot
, $mnet_peer->application
);
112 if ($mnet_peer->currentkey
== $mnet_peer->public_key
) {
113 unset($mnet_peer->currentkey
);
115 error_log($mnet_peer->currentkey
);
116 error_log($mnet_peer->public_key
);
117 error_log(md5($mnet_peer->currentkey
));
118 error_log(md5($mnet_peer->public_key
));
120 $credentials = $mnet_peer->check_credentials($mnet_peer->public_key
);
121 $reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer)); // the second step (also the edit host form)
122 $mnet_peer->oldpublickey
= $mnet_peer->public_key
; // set this so we can confirm on form post without having to recreate the mnet_peer object
123 $reviewform->set_data((object)$mnet_peer);
124 echo $OUTPUT->box_start();
125 $reviewform->display();
126 echo $OUTPUT->box_end();
128 // no options for allhosts host - just let the tabs display and print a notification
129 echo $OUTPUT->notification(get_string('allhosts_no_options', 'mnet'));
131 echo $OUTPUT->footer();
135 // either we're in the second step of setting up a new host
136 // or editing an existing host
137 // try our best to set up the mnet_peer object to pass to the form definition
138 // unless validation on simpleform failed, in which case fall through.
139 if (empty($noreviewform) && $id = optional_param('id', 0, PARAM_INT
)) {
140 // we're editing an existing one, so set up the tabs
141 $currenttab = 'mnetdetails';
142 $mnet_peer->set_id($id);
143 require_once($CFG->dirroot
. '/' . $CFG->admin
. '/mnet/tabs.php');
144 } else if (empty($noreviewform) && ($wwwroot = optional_param('wwwroot', '', PARAM_URL
)) && ($applicationid = optional_param('applicationid', 0, PARAM_INT
))) {
145 $application = $DB->get_field('mnet_application', 'name', array('id'=>$applicationid));
146 $mnet_peer->bootstrap($wwwroot, null, $application);
148 $reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer));
149 if ($formdata = $reviewform->get_data()) {
151 $mnet_peer->set_applicationid($formdata->applicationid
);
152 $application = $DB->get_field('mnet_application', 'name', array('id'=>$formdata->applicationid
));
153 $mnet_peer->bootstrap($formdata->wwwroot
, null, $application);
155 if (!empty($formdata->name
) && $formdata->name
!= $mnet_peer->name
) {
156 $mnet_peer->set_name($formdata->name
);
159 if (empty($formdata->theme
)) {
160 $mnet_peer->force_theme
= 0;
161 $mnet_peer->theme
= null;
163 $mnet_peer->force_theme
= 1;
164 $mnet_peer->theme
= $formdata->theme
;
167 $mnet_peer->deleted
= $formdata->deleted
;
168 $mnet_peer->public_key
= $formdata->public_key
;
169 $credentials = $mnet_peer->check_credentials($mnet_peer->public_key
);
170 $mnet_peer->public_key_expires
= $credentials['validTo_time_t'];
171 $mnet_peer->sslverification
= $formdata->sslverification
;
173 if ($mnet_peer->commit()) {
174 redirect(new moodle_url('/admin/mnet/peers.php', array('hostid' => $mnet_peer->id
)), get_string('changessaved'));
176 print_error('invalidaction', 'error', 'index.php');
178 } else if ($reviewform->is_submitted()) { // submitted, but errors
179 echo $OUTPUT->header();
180 echo $OUTPUT->box_start();
181 $reviewform->display();
182 echo $OUTPUT->box_end();
183 echo $OUTPUT->footer();
188 // normal flow - just display all hosts with links
189 echo $OUTPUT->header();
190 $hosts = mnet_get_hosts(true);
192 // print the table to display the register all hosts setting
193 $table = new html_table();
194 $table->head
= array(get_string('registerallhosts', 'mnet'));
198 $registerurl = new moodle_url('/admin/mnet/peers.php', array('updateregisterall' => 1));
199 if (!empty($CFG->mnet_register_allhosts
)) {
200 $registerrow = get_string('registerhostson', 'mnet');
201 $registerurl->param('registerallhosts', 0);
202 $registerstr = get_string('turnitoff', 'mnet');
204 $registerrow = get_string('registerhostsoff', 'mnet');
205 $registerurl->param('registerallhosts', 1);
206 $registerstr = get_string('turniton', 'mnet');
208 $registerrow .= $OUTPUT->single_button($registerurl, $registerstr);
210 // simple table with two rows of a single cell
211 $table->data
= array(
213 get_string('registerallhostsexplain', 'mnet'),
219 echo html_writer
::table($table);
221 // print the list of all hosts, with little action links and buttons
222 $table = new html_table();
223 $table->head
= array(
225 get_string('system', 'mnet'),
226 get_string('last_connect_time', 'mnet'),
229 $table->wrap
= array('nowrap', 'nowrap', 'nowrap', 'nowrap');
230 $baseurl = new moodle_url('/admin/mnet/peers.php');
232 foreach($hosts as $host) {
233 $hosturl = new moodle_url($baseurl, array('hostid' => $host->id
));
234 if (trim($host->name
) === '') {
235 // should not happen but...
238 // process all hosts first since it's the easiest
239 if ($host->id
== $CFG->mnet_all_hosts_id
) {
240 $table->data
[] = array(html_writer
::link($hosturl, get_string('allhosts', 'core_mnet')), '*', '', '');
244 // populate the list of deleted hosts
245 if ($host->deleted
) {
246 $deleted[] = html_writer
::link($hosturl, $host->name
);
250 if ($host->last_connect_time
== 0) {
251 $last_connect = get_string('never');
253 $last_connect = userdate($host->last_connect_time
, get_string('strftimedatetime', 'core_langconfig'));
255 $table->data
[] = array(
256 html_writer
::link($hosturl, $host->name
),
257 html_writer
::link($host->wwwroot
, $host->wwwroot
),
259 $OUTPUT->single_button(new moodle_url('/admin/mnet/delete.php', array('hostid' => $host->id
)), get_string('delete'))
262 echo html_writer
::table($table);
265 echo $OUTPUT->box(get_string('deletedhosts', 'core_mnet', join(', ', $deleted)), 'deletedhosts');
268 // finally, print the initial form to add a new host
269 echo $OUTPUT->box_start();
270 echo $OUTPUT->heading(get_string('addnewhost', 'mnet'), 3);
271 $simpleform->display();
272 echo $OUTPUT->box_end();
275 echo $OUTPUT->footer();