Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * This file contains two forms for adding/editing mnet hosts, used by peers.php
  20   *
  21   * @package    core
  22   * @subpackage mnet
  23   * @copyright  2010 Penny Leach
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->libdir . '/formslib.php');
  30  
  31  /**
  32   * The very basic first step add new host form - just wwwroot & application
  33   * The second form is loaded up with the information from this one.
  34   */
  35  class mnet_simple_host_form extends moodleform {
  36      function definition() {
  37          global $DB;
  38  
  39          $mform = $this->_form;
  40  
  41          $mform->addElement('text', 'wwwroot', get_string('hostname', 'mnet'), array('maxlength' => 255, 'size' => 50));
  42          $mform->setType('wwwroot', PARAM_URL);
  43          $mform->addRule('wwwroot', null, 'required', null, 'client');
  44          $mform->addRule('wwwroot', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  45  
  46          $mform->addElement('select', 'applicationid', get_string('applicationtype', 'mnet'),
  47                             $DB->get_records_menu('mnet_application', array(), 'id,display_name'));
  48          $mform->addRule('applicationid', null, 'required', null, 'client');
  49  
  50          $this->add_action_buttons(false, get_string('addhost', 'mnet'));
  51      }
  52  
  53      function validation($data, $files) {
  54          global $DB;
  55  
  56          $wwwroot = $data['wwwroot'];
  57          // ensure the wwwroot starts with a http or https prefix
  58          if (strtolower(substr($wwwroot, 0, 4)) != 'http') {
  59              $wwwroot = 'http://'.$wwwroot;
  60          }
  61          if ($host = $DB->get_record('mnet_host', array('wwwroot' => $wwwroot))) {
  62              global $CFG;
  63              return array('wwwroot' => get_string('hostexists', 'mnet',
  64                  new moodle_url('/admin/mnet/peers.php', array('hostid' => $host->id))));
  65          }
  66          return array();
  67      }
  68  }
  69  
  70  /**
  71   * The second step of the form - reviewing the host details
  72   * This is also the same form that is used for editing an existing host
  73   */
  74  class mnet_review_host_form extends moodleform {
  75      function definition() {
  76          global $OUTPUT;
  77  
  78          $mform = $this->_form;
  79          $mnet_peer = $this->_customdata['peer'];
  80  
  81          $mform->addElement('hidden', 'last_connect_time');
  82          $mform->setType('last_connect_time', PARAM_INT);
  83          $mform->addElement('hidden', 'id');
  84          $mform->setType('id', PARAM_INT);
  85          $mform->addElement('hidden', 'applicationid');
  86          $mform->setType('applicationid', PARAM_INT);
  87          $mform->addElement('hidden', 'oldpublickey');
  88          $mform->setType('oldpublickey', PARAM_PEM);
  89  
  90          $mform->addElement('text', 'name', get_string('site'), array('maxlength' => 80, 'size' => 50));
  91          $mform->setType('name', PARAM_NOTAGS);
  92          $mform->addRule('name', get_string('maximumchars', '', 80), 'maxlength', 80, 'client');
  93  
  94          $mform->addElement('text', 'wwwroot', get_string('hostname', 'mnet'), array('maxlength' => 255, 'size' => 50));
  95          $mform->setType('wwwroot', PARAM_URL);
  96          $mform->addRule('wwwroot', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  97  
  98          $options = array(
  99              mnet_peer::SSL_NONE => get_string('none'),
 100              mnet_peer::SSL_HOST => get_string('verifyhostonly', 'core_mnet'),
 101              mnet_peer::SSL_HOST_AND_PEER => get_string('verifyhostandpeer', 'core_mnet')
 102          );
 103          $mform->addElement('select', 'sslverification', get_string('sslverification', 'core_mnet'), $options);
 104          $mform->setDefault('sslverification', mnet_peer::SSL_HOST_AND_PEER);
 105          $mform->addHelpButton('sslverification', 'sslverification', 'core_mnet');
 106  
 107          $themes = array('' => get_string('forceno'));
 108          foreach (array_keys(core_component::get_plugin_list('theme')) as $themename) {
 109              $themes[$themename] = get_string('pluginname', 'theme_'.$themename);
 110          }
 111          $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
 112  
 113          $mform->addElement('textarea', 'public_key', get_string('publickey', 'mnet'), array('rows' => 17, 'cols' => 100, 'class' => 'smalltext'));
 114          $mform->setType('public_key', PARAM_PEM);
 115          $mform->addRule('public_key', get_string('required'), 'required');
 116  
 117          // finished with form controls, now the static informational stuff
 118          if ($mnet_peer && !empty($mnet_peer->bootstrapped)) {
 119              $expires = '';
 120              if ($mnet_peer->public_key_expires < time()) {
 121                  $expires = get_string('expired', 'mnet')  . ' ';
 122              }
 123              $expires .= userdate($mnet_peer->public_key_expires);
 124              $mform->addElement('static', 'validuntil', get_string('expires', 'mnet'), $expires);
 125  
 126              $lastconnect = '';
 127              if ($mnet_peer->last_connect_time == 0) {
 128                  $lastconnect = get_string('never', 'mnet');
 129              } else {
 130                  $lastconnect = date('H:i:s d/m/Y',$mnet_peer->last_connect_time);
 131              }
 132  
 133              $mform->addElement('static', 'lastconnect', get_string('last_connect_time', 'mnet'), $lastconnect);
 134              $mform->addElement('static', 'ipaddress', get_string('ipaddress', 'mnet'), $mnet_peer->ip_address);
 135  
 136              if (isset($mnet_peer->currentkey)) { // key being published is not the same as our records
 137                  $currentkeystr = '<b>' . get_string('keymismatch', 'mnet') . '</b><br /><br /> ' . $OUTPUT->box('<pre>' . $mnet_peer->currentkey . '</pre>');
 138                  $mform->addElement('static', 'keymismatch', get_string('currentkey', 'mnet'), $currentkeystr);
 139              }
 140  
 141              $credstr = '';
 142              if ($credentials = $mnet_peer->check_credentials($mnet_peer->public_key)) {
 143                  foreach($credentials['subject'] as $key => $credential) {
 144                      if (is_scalar($credential)) {
 145                          $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.$credential."\n";
 146                      } else {
 147                          $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.var_export($credential,1)."\n";
 148                      }
 149                  }
 150              }
 151  
 152              $mform->addElement('static', 'certdetails', get_string('certdetails', 'mnet'),
 153                  $OUTPUT->box('<pre>' . $credstr . '</pre>', 'generalbox certdetails'));
 154          }
 155  
 156          if ($mnet_peer && !empty($mnet_peer->deleted)) {
 157              $radioarray = array();
 158              $radioarray[] = $mform->createElement('static', 'deletedinfo', '',
 159                  $OUTPUT->container(get_string('deletedhostinfo', 'mnet'), 'alert alert-warning'));
 160              $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('yes'), 1);
 161              $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('no'), 0);
 162              $mform->addGroup($radioarray, 'radioar', get_string('deleted'), array(' ', ' '), false);
 163          } else {
 164              $mform->addElement('hidden', 'deleted');
 165              $mform->setType('deleted', PARAM_BOOL);
 166          }
 167  
 168          // finished with static stuff, print save button
 169          $this->add_action_buttons(false);
 170      }
 171  
 172      function validation($data, $files) {
 173          $errors = array();
 174          if ($data['oldpublickey'] == $data['public_key']) {
 175              return;
 176          }
 177          $mnet_peer = new mnet_peer(); // idiotic api
 178          $mnet_peer->wwwroot = $data['wwwroot']; // just hard-set this rather than bootstrap the object
 179          if (empty($data['public_key'])) {
 180              $errors['public_key'] = get_string('publickeyrequired', 'mnet');
 181          } else if (!$credentials = $mnet_peer->check_credentials($data['public_key'])) {
 182              $errmsg = '';
 183              foreach ($mnet_peer->error as $err) {
 184                  $errmsg .= $err['code'] . ': ' . $err['text'].'<br />';
 185              }
 186              $errors['public_key'] = get_string('invalidpubkey', 'mnet', $errmsg);
 187          }
 188          unset($mnet_peer);
 189          return $errors;
 190      }
 191  }