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 admin_externalpage_setup('mnetpeers');
12 $context = get_context_instance(CONTEXT_SYSTEM
);
14 require_capability('moodle/site:config', $context, $USER->id
, true, "nopermissions");
16 if (!extension_loaded('openssl')) {
17 admin_externalpage_print_header();
18 print_error('requiresopenssl', 'mnet');
21 if (!$site = get_site()) {
22 admin_externalpage_print_header();
23 print_error('nosite', '');
26 if (!function_exists('curl_init') ) {
27 admin_externalpage_print_header();
28 print_error('nocurl', 'mnet');
31 /// Initialize variables.
33 // Step must be one of:
34 // input Parse the details of a new host and fetch its public key
35 // commit Save our changes (to a new OR existing host)
36 $step = optional_param('step', NULL, PARAM_ALPHA
);
37 $hostid = optional_param('hostid', NULL, PARAM_INT
);
39 // Fetch some strings for the HTML templates
40 $strmnetservices = get_string('mnetservices', 'mnet');
41 $strmnetlog = get_string('mnetlog', 'mnet');
42 $strmnetedithost = get_string('reviewhostdetails', 'mnet');
43 $strmnetthemes = get_string('mnetthemes', '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 // ensure the wwwroot starts with a http or https prefix
55 if (strtolower(substr($form->wwwroot
, 0, 4)) != 'http') {
56 $form->wwwroot
= 'http://'.$form->wwwroot
;
60 if(!function_exists('xmlrpc_encode_request')) {
61 trigger_error("You must have xml-rpc enabled in your PHP build to use this feature.");
62 print_error('xmlrpc-missing', 'mnet','peers.php');
66 if (!empty($form->updateregisterall
)) {
67 if (!empty($form->registerallhosts
)) {
68 set_config('mnet_register_allhosts',1);
70 set_config('mnet_register_allhosts',0);
72 redirect('peers.php', get_string('changessaved'));
75 $mnet_peer = new mnet_peer();
77 if (!empty($form->id
)) {
78 $form->id
= clean_param($form->id
, PARAM_INT
);
79 $mnet_peer->set_id($form->id
);
81 // PARAM_URL requires a genuine TLD (I think) This breaks my testing
82 $temp_wwwroot = clean_param($form->wwwroot
, PARAM_URL
);
83 if ($temp_wwwroot !== $form->wwwroot
) {
84 trigger_error("We now parse the wwwroot with PARAM_URL. Your URL will need to have a valid TLD, etc.");
85 print_error("invalidurl", 'mnet','peers.php');
89 $mnet_peer->set_applicationid($form->applicationid
);
90 $application = get_field('mnet_application', 'name', 'id', $form->applicationid
);
91 $mnet_peer->bootstrap($form->wwwroot
, null, $application);
94 if (isset($form->name
) && $form->name
!= $mnet_peer->name
) {
95 $form->name
= clean_param($form->name
, PARAM_NOTAGS
);
96 $mnet_peer->set_name($form->name
);
99 if (isset($form->deleted
) && ($form->deleted
== '0' ||
$form->deleted
== '1')) {
100 $mnet_peer->deleted
= $form->deleted
;
103 if (isset($form->public_key
)) {
104 $form->public_key
= clean_param($form->public_key
, PARAM_PEM
);
105 if (empty($form->public_key
)) {
106 print_error("invalidpubkey", 'mnet', 'peers.php?step=update&hostid='.$mnet_peer->id
);
109 $oldkey = $mnet_peer->public_key
;
110 $mnet_peer->public_key
= $form->public_key
;
111 $mnet_peer->public_key_expires
= $mnet_peer->check_common_name($form->public_key
);
112 if ($mnet_peer->public_key_expires
== false) {
113 $mnet_peer->public_key
== $oldkey;
115 foreach ($mnet_peer->error
as $err) {
116 $errmsg .= $err['code'] . ': ' . $err['text'].'<br />';
118 error(get_string("invalidpubkey", 'mnet') . $errmsg ,'peers.php?step=update&hostid='.$mnet_peer->id
);
119 //print_error("invalidpubkey", 'mnet', 'peers.php?step=update&hostid='.$mnet_peer->id, $errmsg);
125 // PREVENT DUPLICATE RECORDS ///////////////////////////////////////////
126 if ('input' == $form->step
) {
127 if ( isset($mnet_peer->id
) && $mnet_peer->id
> 0 ) {
128 print_error("hostexists", 'mnet', 'peers.php?step=update&hostid='.$mnet_peer->id
, $mnet_peer->id
);
132 if ('input' == $form->step
) {
133 include('./mnet_review.html');
134 } elseif ('commit' == $form->step
) {
135 $bool = $mnet_peer->commit();
137 redirect('peers.php?step=update&hostid='.$mnet_peer->id
, get_string('changessaved'));
139 error('Invalid action parameter.', 'index.php');
143 } elseif (is_int($hostid)) {
144 $mnet_peer = new mnet_peer();
145 $mnet_peer->set_id($hostid);
146 $currentkey = mnet_get_public_key($mnet_peer->wwwroot
, $mnet_peer->application
);
147 if($currentkey == $mnet_peer->public_key
) unset($currentkey);
148 $form = new stdClass();
149 if ($hostid != $CFG->mnet_all_hosts_id
) {
150 include('./mnet_review.html');
152 include('./mnet_review_allhosts.html');
155 $hosts = get_records_sql(' SELECT
161 h.public_key_expires,
168 a.display_name as app_display_name,
171 '.$CFG->prefix
.'mnet_host h,
172 '.$CFG->prefix
.'mnet_application a
174 h.id != \''.$CFG->mnet_localhost_id
.'\' AND
175 h.deleted = \'0\' AND
176 h.applicationid=a.id');
178 if (empty($hosts)) $hosts = array();
179 $applications = get_records('mnet_application');
180 include('./peers.html');