source: moodle/trunk/fuentes/admin/mnet/peers.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 10.3 KB
Line 
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
19/**
20 * Page to allow the administrator to configure networked hosts, and add new ones
21 *
22 * @package    core
23 * @subpackage mnet
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
28 */
29
30require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
31require_once($CFG->libdir.'/adminlib.php');
32require_once($CFG->dirroot.'/mnet/lib.php');
33require_once($CFG->dirroot.'/'.$CFG->admin.'/mnet/peer_forms.php');
34
35require_login();
36
37$context = context_system::instance();
38require_capability('moodle/site:config', $context, $USER->id, true, 'nopermissions');
39
40/// Initialize variables.
41$hostid = optional_param('hostid', 0, PARAM_INT);
42$updra = optional_param('updateregisterall', 0, PARAM_INT);
43
44// first process the register all hosts setting if required
45if (!empty($updra)) {
46    set_config('mnet_register_allhosts', optional_param('registerallhosts', 0, PARAM_INT));
47    redirect(new moodle_url('/admin/mnet/peers.php'), get_string('changessaved'));
48}
49
50$adminsection = 'mnetpeers';
51if ($hostid && $DB->get_field('mnet_host', 'deleted', array('id' => $hostid)) != 1) {
52    $adminsection = 'mnetpeer' . $hostid;
53}
54
55$PAGE->set_url('/admin/mnet/peers.php');
56admin_externalpage_setup($adminsection);
57
58if (!extension_loaded('openssl')) {
59    print_error('requiresopenssl', 'mnet');
60}
61
62if (!function_exists('curl_init') ) {
63    print_error('nocurl', 'mnet');
64}
65
66if (!function_exists('xmlrpc_encode_request')) {
67    print_error('xmlrpc-missing', 'mnet');
68}
69
70if (!isset($CFG->mnet_dispatcher_mode)) {
71    set_config('mnet_dispatcher_mode', 'off');
72}
73
74$mnet_peer = new mnet_peer();
75$simpleform = new mnet_simple_host_form(); // the one that goes on the bottom of the main page
76$reviewform = null; // set up later in different code branches, so mnet_peer can be passed to the constructor
77
78// if the first form has been submitted, bootstrap the peer and load up the review form
79if ($formdata = $simpleform->get_data()) {
80    // ensure we remove trailing slashes
81    $formdata->wwwroot = trim($formdata->wwwroot);
82    $formdata->wwwroot = rtrim($formdata->wwwroot, '/');
83
84    // ensure the wwwroot starts with a http or https prefix
85    if (strtolower(substr($formdata->wwwroot, 0, 4)) != 'http') {
86        $formdata->wwwroot = 'http://'.$formdata->wwwroot;
87    }
88
89    $mnet_peer->set_applicationid($formdata->applicationid);
90    $application = $DB->get_field('mnet_application', 'name', array('id'=>$formdata->applicationid));
91    $mnet_peer->bootstrap($formdata->wwwroot, null, $application);
92    // bootstrap the second form straight with the data from the first form
93    $reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer)); // the second step (also the edit host form)
94    $formdata->oldpublickey = $mnet_peer->public_key; // set this so we can confirm on form post without having to recreate the mnet_peer object
95    $reviewform->set_data($mnet_peer);
96    echo $OUTPUT->header();
97    echo $OUTPUT->box_start();
98    $reviewform->display();
99    echo $OUTPUT->box_end();
100    echo $OUTPUT->footer();
101    exit;
102} else if ($simpleform->is_submitted()) { // validation failed
103    $noreviewform = true;
104}
105
106// editing a host - load up the review form
107if (!empty($hostid)) {
108    // TODO print a nice little heading
109    $mnet_peer->set_id($hostid);
110    echo $OUTPUT->header();
111    $currenttab = 'mnetdetails';
112    require_once($CFG->dirroot . '/admin/mnet/tabs.php');
113
114    if ($hostid != $CFG->mnet_all_hosts_id) {
115        $mnet_peer->currentkey = mnet_get_public_key($mnet_peer->wwwroot, $mnet_peer->application);
116        if ($mnet_peer->currentkey == $mnet_peer->public_key) {
117            unset($mnet_peer->currentkey);
118        } else {
119            error_log($mnet_peer->currentkey);
120            error_log($mnet_peer->public_key);
121            error_log(md5($mnet_peer->currentkey));
122            error_log(md5($mnet_peer->public_key));
123        }
124        $credentials = $mnet_peer->check_credentials($mnet_peer->public_key);
125        $reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer)); // the second step (also the edit host form)
126        $mnet_peer->oldpublickey = $mnet_peer->public_key; // set this so we can confirm on form post without having to recreate the mnet_peer object
127        $reviewform->set_data((object)$mnet_peer);
128        echo $OUTPUT->box_start();
129        $reviewform->display();
130        echo $OUTPUT->box_end();
131    } else {
132        // no options for allhosts host - just let the tabs display and print a notification
133        echo $OUTPUT->notification(get_string('allhosts_no_options', 'mnet'));
134    }
135    echo $OUTPUT->footer();
136    exit;
137}
138
139// either we're in the second step of setting up a new host
140// or editing an existing host
141// try our best to set up the mnet_peer object to pass to the form definition
142// unless validation on simpleform failed, in which case fall through.
143if (empty($noreviewform) && $id = optional_param('id', 0, PARAM_INT)) {
144    // we're editing an existing one, so set up the tabs
145    $currenttab = 'mnetdetails';
146    $mnet_peer->set_id($id);
147    require_once($CFG->dirroot . '/admin/mnet/tabs.php');
148} else if (empty($noreviewform) && ($wwwroot = optional_param('wwwroot', '', PARAM_URL)) && ($applicationid = optional_param('applicationid', 0, PARAM_INT))) {
149    $application = $DB->get_field('mnet_application', 'name', array('id'=>$applicationid));
150    $mnet_peer->bootstrap($wwwroot, null, $application);
151}
152$reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer));
153if ($formdata = $reviewform->get_data()) {
154
155    $mnet_peer->set_applicationid($formdata->applicationid);
156    $application = $DB->get_field('mnet_application', 'name', array('id'=>$formdata->applicationid));
157    $mnet_peer->bootstrap($formdata->wwwroot, null, $application);
158
159    if (!empty($formdata->name) && $formdata->name != $mnet_peer->name) {
160        $mnet_peer->set_name($formdata->name);
161    }
162
163    if (empty($formdata->theme)) {
164        $mnet_peer->force_theme = 0;
165        $mnet_peer->theme = null;
166    } else {
167        $mnet_peer->force_theme = 1;
168        $mnet_peer->theme = $formdata->theme;
169    }
170
171    $mnet_peer->deleted             = $formdata->deleted;
172    $mnet_peer->public_key          = $formdata->public_key;
173    $credentials                    = $mnet_peer->check_credentials($mnet_peer->public_key);
174    $mnet_peer->public_key_expires  = $credentials['validTo_time_t'];
175
176    if ($mnet_peer->commit()) {
177        redirect(new moodle_url('/admin/mnet/peers.php', array('hostid' => $mnet_peer->id)), get_string('changessaved'));
178    } else {
179        print_error('invalidaction', 'error', 'index.php');
180    }
181} else if ($reviewform->is_submitted()) { // submitted, but errors
182    echo $OUTPUT->header();
183    echo $OUTPUT->box_start();
184    $reviewform->display();
185    echo $OUTPUT->box_end();
186    echo $OUTPUT->footer();
187    exit;
188}
189
190
191// normal flow - just display all hosts with links
192echo $OUTPUT->header();
193$hosts = mnet_get_hosts(true);
194
195// print the table to display the register all hosts setting
196$table = new html_table();
197$table->head = array(get_string('registerallhosts', 'mnet'));
198
199$registerrow = '';
200$registerstr = '';
201$registerurl = new moodle_url('/admin/mnet/peers.php', array('updateregisterall' => 1));
202if (!empty($CFG->mnet_register_allhosts)) {
203    $registerrow = get_string('registerhostson', 'mnet');
204    $registerurl->param('registerallhosts', 0);
205    $registerstr = get_string('turnitoff', 'mnet');
206} else {
207    $registerrow = get_string('registerhostsoff', 'mnet');
208    $registerurl->param('registerallhosts', 1);
209    $registerstr = get_string('turniton', 'mnet');
210}
211$registerrow .= $OUTPUT->single_button($registerurl, $registerstr);
212
213// simple table with two rows of a single cell
214$table->data = array(
215    array(
216        get_string('registerallhostsexplain', 'mnet'),
217    ),
218    array(
219        $registerrow
220    ),
221);
222echo html_writer::table($table);
223
224// print the list of all hosts, with little action links and buttons
225$table = new html_table();
226$table->head = array(
227    get_string('site'),
228    get_string('system', 'mnet'),
229    get_string('last_connect_time', 'mnet'),
230    '',
231);
232$table->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap');
233$baseurl = new moodle_url('/admin/mnet/peers.php');
234$deleted = array();
235foreach($hosts as $host) {
236    $hosturl = new moodle_url($baseurl, array('hostid' => $host->id));
237    if (trim($host->name) === '') {
238        // should not happen but...
239        $host->name = '???';
240    }
241    // process all hosts first since it's the easiest
242    if ($host->id == $CFG->mnet_all_hosts_id) {
243        $table->data[] = array(html_writer::link($hosturl, get_string('allhosts', 'core_mnet')), '*', '', '');
244        continue;
245    }
246
247    // populate the list of deleted hosts
248    if ($host->deleted) {
249        $deleted[] = html_writer::link($hosturl, $host->name);
250        continue;
251    }
252
253    if ($host->last_connect_time == 0) {
254        $last_connect = get_string('never');
255    } else {
256        $last_connect = userdate($host->last_connect_time, get_string('strftimedatetime', 'core_langconfig'));
257    }
258    $table->data[] = array(
259        html_writer::link($hosturl, $host->name),
260        html_writer::link($host->wwwroot, $host->wwwroot),
261        $last_connect,
262        $OUTPUT->single_button(new moodle_url('/admin/mnet/delete.php', array('hostid' => $host->id)), get_string('delete'))
263    );
264}
265echo html_writer::table($table);
266
267if ($deleted) {
268    echo $OUTPUT->box(get_string('deletedhosts', 'core_mnet', join(', ', $deleted)), 'deletedhosts');
269}
270
271// finally, print the initial form to add a new host
272echo $OUTPUT->box_start();
273echo $OUTPUT->heading(get_string('addnewhost', 'mnet'), 3);
274$simpleform->display();
275echo $OUTPUT->box_end();
276
277// done
278echo $OUTPUT->footer();
279exit;
280
Note: See TracBrowser for help on using the repository browser.