Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

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