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 39 and 400]

   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              $str = get_string('hostexists', 'mnet', (new moodle_url('/admin/mnet/peers.php', ['hostid' => $host->id]))->out());
  63              return array('wwwroot' => $str);
  64          }
  65          return array();
  66      }
  67  }
  68  
  69  /**
  70   * The second step of the form - reviewing the host details
  71   * This is also the same form that is used for editing an existing host
  72   */
  73  class mnet_review_host_form extends moodleform {
  74      function definition() {
  75          global $OUTPUT;
  76  
  77          $mform = $this->_form;
  78          $mnet_peer = $this->_customdata['peer'];
  79  
  80          $mform->addElement('hidden', 'last_connect_time');
  81          $mform->setType('last_connect_time', PARAM_INT);
  82          $mform->addElement('hidden', 'id');
  83          $mform->setType('id', PARAM_INT);
  84          $mform->addElement('hidden', 'applicationid');
  85          $mform->setType('applicationid', PARAM_INT);
  86          $mform->addElement('hidden', 'oldpublickey');
  87          $mform->setType('oldpublickey', PARAM_PEM);
  88  
  89          $mform->addElement('text', 'name', get_string('site'), array('maxlength' => 80, 'size' => 50));
  90          $mform->setType('name', PARAM_NOTAGS);
  91          $mform->addRule('name', get_string('maximumchars', '', 80), 'maxlength', 80, 'client');
  92  
  93          $mform->addElement('text', 'wwwroot', get_string('hostname', 'mnet'), array('maxlength' => 255, 'size' => 50));
  94          $mform->setType('wwwroot', PARAM_URL);
  95          $mform->addRule('wwwroot', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  96  
  97          $options = array(
  98              mnet_peer::SSL_NONE => get_string('none'),
  99              mnet_peer::SSL_HOST => get_string('verifyhostonly', 'core_mnet'),
 100              mnet_peer::SSL_HOST_AND_PEER => get_string('verifyhostandpeer', 'core_mnet')
 101          );
 102          $mform->addElement('select', 'sslverification', get_string('sslverification', 'core_mnet'), $options);
 103          $mform->setDefault('sslverification', mnet_peer::SSL_HOST_AND_PEER);
 104          $mform->addHelpButton('sslverification', 'sslverification', 'core_mnet');
 105  
 106          $themes = array('' => get_string('forceno'));
 107          foreach (array_keys(core_component::get_plugin_list('theme')) as $themename) {
 108              $themes[$themename] = get_string('pluginname', 'theme_'.$themename);
 109          }
 110          $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
 111  
 112          $mform->addElement('textarea', 'public_key', get_string('publickey', 'mnet'), array('rows' => 17, 'cols' => 100, 'class' => 'smalltext'));
 113          $mform->setType('public_key', PARAM_PEM);
 114          $mform->addRule('public_key', get_string('required'), 'required');
 115  
 116          // finished with form controls, now the static informational stuff
 117          if ($mnet_peer && !empty($mnet_peer->bootstrapped)) {
 118              $expires = '';
 119              if ($mnet_peer->public_key_expires < time()) {
 120                  $expires = get_string('expired', 'mnet')  . ' ';
 121              }
 122              $expires .= userdate($mnet_peer->public_key_expires);
 123              $mform->addElement('static', 'validuntil', get_string('expires', 'mnet'), $expires);
 124  
 125              $lastconnect = '';
 126              if ($mnet_peer->last_connect_time == 0) {
 127                  $lastconnect = get_string('never', 'mnet');
 128              } else {
 129                  $lastconnect = date('H:i:s d/m/Y',$mnet_peer->last_connect_time);
 130              }
 131  
 132              $mform->addElement('static', 'lastconnect', get_string('last_connect_time', 'mnet'), $lastconnect);
 133              $mform->addElement('static', 'ipaddress', get_string('ipaddress', 'mnet'), $mnet_peer->ip_address);
 134  
 135              if (isset($mnet_peer->currentkey)) { // key being published is not the same as our records
 136                  $currentkeystr = '<b>' . get_string('keymismatch', 'mnet') . '</b><br /><br /> ' . $OUTPUT->box('<pre>' . $mnet_peer->currentkey . '</pre>');
 137                  $mform->addElement('static', 'keymismatch', get_string('currentkey', 'mnet'), $currentkeystr);
 138              }
 139  
 140              $credstr = '';
 141              if ($credentials = $mnet_peer->check_credentials($mnet_peer->public_key)) {
 142                  foreach($credentials['subject'] as $key => $credential) {
 143                      if (is_scalar($credential)) {
 144                          $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.$credential."\n";
 145                      } else {
 146                          $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.var_export($credential,1)."\n";
 147                      }
 148                  }
 149              }
 150  
 151              $mform->addElement('static', 'certdetails', get_string('certdetails', 'mnet'),
 152                  $OUTPUT->box('<pre>' . $credstr . '</pre>', 'generalbox certdetails'));
 153          }
 154  
 155          if ($mnet_peer && !empty($mnet_peer->deleted)) {
 156              $radioarray = array();
 157              $radioarray[] = $mform->createElement('static', 'deletedinfo', '',
 158                  $OUTPUT->container(get_string('deletedhostinfo', 'mnet'), 'alert alert-warning'));
 159              $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('yes'), 1);
 160              $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('no'), 0);
 161              $mform->addGroup($radioarray, 'radioar', get_string('deleted'), array(' ', ' '), false);
 162          } else {
 163              $mform->addElement('hidden', 'deleted');
 164              $mform->setType('deleted', PARAM_BOOL);
 165          }
 166  
 167          // finished with static stuff, print save button
 168          $this->add_action_buttons(false);
 169      }
 170  
 171      function validation($data, $files) {
 172          $errors = array();
 173          if ($data['oldpublickey'] == $data['public_key']) {
 174              return;
 175          }
 176          $mnet_peer = new mnet_peer(); // idiotic api
 177          $mnet_peer->wwwroot = $data['wwwroot']; // just hard-set this rather than bootstrap the object
 178          if (empty($data['public_key'])) {
 179              $errors['public_key'] = get_string('publickeyrequired', 'mnet');
 180          } else if (!$credentials = $mnet_peer->check_credentials($data['public_key'])) {
 181              $errmsg = '';
 182              foreach ($mnet_peer->error as $err) {
 183                  $errmsg .= $err['code'] . ': ' . $err['text'].'<br />';
 184              }
 185              $errors['public_key'] = get_string('invalidpubkey', 'mnet', $errmsg);
 186          }
 187          unset($mnet_peer);
 188          return $errors;
 189      }
 190  }