Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

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