3 // Allows the admin to configure other Moodle hosts info
5 require_once(dirname(dirname(dirname(__FILE__
))) . '/config.php');
6 require_once($CFG->libdir
.'/adminlib.php');
7 include_once($CFG->dirroot
.'/mnet/lib.php');
10 $adminroot = admin_get_root();
11 admin_externalpage_setup('mnetpeers', $adminroot);
13 $context = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
15 require_capability('moodle/site:config', $context, $USER->id
, true, "nopermissions");
17 if (!extension_loaded('openssl')) {
18 admin_externalpage_print_header($adminroot);
19 print_error('requiresopenssl', 'mnet', '', NULL, true);
22 if (!$site = get_site()) {
23 admin_externalpage_print_header($adminroot);
24 print_error('nosite', '', '', NULL, true);
27 if (!function_exists('curl_init') ) {
28 admin_externalpage_print_header($adminroot);
29 print_error('nocurl', 'mnet', '', NULL, true);
32 /// Initialize variables.
34 // Step must be one of:
35 // input Parse the details of a new host and fetch its public key
36 // commit Save our changes (to a new OR existing host)
37 $step = optional_param('step', NULL, PARAM_ALPHA
);
38 $hostid = optional_param('hostid', NULL, PARAM_INT
);
40 // Fetch some strings for the HTML templates
41 $strmnetservices = get_string('mnetservices', 'mnet');
42 $strmnetlog = get_string('mnetlog', 'mnet');
43 $strmnetedithost = get_string('reviewhostdetails', 'mnet');
45 if (!isset($CFG->mnet_dispatcher_mode
)) set_config('mnet_dispatcher_mode', 'off');
47 /// If data submitted, process and store
48 if (($form = data_submitted()) && confirm_sesskey()) {
50 if (!empty($form->wwwroot
)) {
51 // ensure we remove trailing slashes
52 $form->wwwroot
= preg_replace(':/$:', '', $form->wwwroot
);
54 if (!empty($form->updateregisterall
)) {
55 if (!empty($form->registerallhosts
)) {
56 set_config('mnet_register_allhosts',1);
58 set_config('mnet_register_allhosts',0);
60 redirect('peers.php', get_string('changessaved'));
63 $mnet_peer = new mnet_peer();
65 if (!empty($form->id
)) {
66 $form->id
= clean_param($form->id
, PARAM_INT
);
67 $mnet_peer->set_id($form->id
);
69 // PARAM_URL requires a genuine TLD (I think) This breaks my testing
70 $temp_wwwroot = clean_param($form->wwwroot
, PARAM_URL
);
71 if ($temp_wwwroot !== $form->wwwroot
) {
72 trigger_error("We now parse the wwwroot with PARAM_URL. Your URL will need to have a valid TLD, etc.");
73 error(get_string("invalidurl", 'mnet'),'peers.php');
77 $mnet_peer->bootstrap($form->wwwroot
);
80 if (isset($form->name
) && $form->name
!= $mnet_peer->name
) {
81 $form->name
= clean_param($form->name
, PARAM_NOTAGS
);
82 $mnet_peer->set_name($form->name
);
85 if (isset($form->deleted
) && ($form->deleted
== '0' ||
$form->deleted
== '1')) {
86 $mnet_peer->deleted
= $form->deleted
;
89 if (isset($form->public_key
)) {
90 $form->public_key
= clean_param($form->public_key
, PARAM_PEM
);
91 if (empty($form->public_key
)) {
92 error(get_string("invalidpubkey", 'mnet'),'peers.php?step=update&hostid='.$mnet_peer->id
);
95 $oldkey = $mnet_peer->public_key
;
96 $mnet_peer->public_key
= $form->public_key
;
97 $mnet_peer->public_key_expires
= $mnet_peer->check_common_name($form->public_key
);
98 if ($mnet_peer->public_key_expires
== false) {
99 $mnet_peer->public_key
== $oldkey;
101 foreach ($mnet_peer->error
as $err) {
102 $errmsg .= $err['code'] . ': ' . $err['text'].'<br />';
104 error(get_string("invalidpubkey", 'mnet') . $errmsg ,'peers.php?step=update&hostid='.$mnet_peer->id
);
110 // PREVENT DUPLICATE RECORDS ///////////////////////////////////////////
111 if ('input' == $form->step
) {
112 if ( isset($mnet_peer->id
) && $mnet_peer->id
> 0 ) {
113 error(get_string("hostexists", 'mnet', $mnet_peer->id
),'peers.php?step=update&hostid='.$mnet_peer->id
);
117 if ('input' == $form->step
) {
118 include('./mnet_review.html');
119 } elseif ('commit' == $form->step
) {
120 $bool = $mnet_peer->commit();
122 redirect('peers.php?step=update&hostid='.$mnet_peer->id
, get_string('changessaved'));
124 error('Invalid action parameter.', 'index.php');
128 } elseif (is_int($hostid)) {
129 $mnet_peer = new mnet_peer();
130 $mnet_peer->set_id($hostid);
131 $currentkey = mnet_get_public_key($mnet_peer->wwwroot
);
132 if($currentkey == $mnet_peer->public_key
) unset($currentkey);
133 $form = new stdClass();
134 if ($hostid != $CFG->mnet_all_hosts_id
) {
135 include('./mnet_review.html');
137 include('./mnet_review_allhosts.html');
140 $hosts = get_records_select('mnet_host', " id != '{$CFG->mnet_localhost_id}' AND deleted = '0' ",'wwwroot ASC' );
141 if (empty($hosts)) $hosts = array();
142 include('./peers.html');