[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/mnet/ -> peer_forms.php (source)

   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', $CFG->wwwroot . '/admin/mnet/peers.php?hostid=' . $host->id));
  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          $themes = array('' => get_string('forceno'));
  98          foreach (array_keys(core_component::get_plugin_list('theme')) as $themename) {
  99              $themes[$themename] = get_string('pluginname', 'theme_'.$themename);
 100          }
 101          $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
 102  
 103          $mform->addElement('textarea', 'public_key', get_string('publickey', 'mnet'), array('rows' => 17, 'cols' => 100, 'class' => 'smalltext'));
 104          $mform->setType('public_key', PARAM_PEM);
 105          $mform->addRule('public_key', get_string('required'), 'required');
 106  
 107          // finished with form controls, now the static informational stuff
 108          if ($mnet_peer && !empty($mnet_peer->bootstrapped)) {
 109              $expires = '';
 110              if ($mnet_peer->public_key_expires < time()) {
 111                  $expires = get_string('expired', 'mnet')  . ' ';
 112              }
 113              $expires .= userdate($mnet_peer->public_key_expires);
 114              $mform->addElement('static', 'validuntil', get_string('expires', 'mnet'), $expires);
 115  
 116              $lastconnect = '';
 117              if ($mnet_peer->last_connect_time == 0) {
 118                  $lastconnect = get_string('never', 'mnet');
 119              } else {
 120                  $lastconnect = date('H:i:s d/m/Y',$mnet_peer->last_connect_time);
 121              }
 122  
 123              $mform->addElement('static', 'lastconnect', get_string('last_connect_time', 'mnet'), $lastconnect);
 124              $mform->addElement('static', 'ipaddress', get_string('ipaddress', 'mnet'), $mnet_peer->ip_address);
 125  
 126              if (isset($mnet_peer->currentkey)) { // key being published is not the same as our records
 127                  $currentkeystr = '<b>' . get_string('keymismatch', 'mnet') . '</b><br /><br /> ' . $OUTPUT->box('<pre>' . $mnet_peer->currentkey . '</pre>');
 128                  $mform->addElement('static', 'keymismatch', get_string('currentkey', 'mnet'), $currentkeystr);
 129              }
 130  
 131              $credstr = '';
 132              if ($credentials = $mnet_peer->check_credentials($mnet_peer->public_key)) {
 133                  foreach($credentials['subject'] as $key => $credential) {
 134                      if (is_scalar($credential)) {
 135                          $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.$credential."\n";
 136                      } else {
 137                          $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.var_export($credential,1)."\n";
 138                      }
 139                  }
 140              }
 141  
 142              $mform->addElement('static', 'certdetails', get_string('certdetails', 'mnet'),
 143                  $OUTPUT->box('<pre>' . $credstr . '</pre>', 'generalbox certdetails'));
 144          }
 145  
 146          if ($mnet_peer && !empty($mnet_peer->deleted)) {
 147              $radioarray = array();
 148              $radioarray[] = $mform->createElement('static', 'deletedinfo', '',
 149                  $OUTPUT->container(get_string('deletedhostinfo', 'mnet'), 'deletedhostinfo'));
 150              $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('yes'), 1);
 151              $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('no'), 0);
 152              $mform->addGroup($radioarray, 'radioar', get_string('deleted'), array(' ', ' '), false);
 153          } else {
 154              $mform->addElement('hidden', 'deleted');
 155              $mform->setType('deleted', PARAM_BOOL);
 156          }
 157  
 158          // finished with static stuff, print save button
 159          $this->add_action_buttons(false);
 160      }
 161  
 162      function validation($data, $files) {
 163          $errors = array();
 164          if ($data['oldpublickey'] == $data['public_key']) {
 165              return;
 166          }
 167          $mnet_peer = new mnet_peer(); // idiotic api
 168          $mnet_peer->wwwroot = $data['wwwroot']; // just hard-set this rather than bootstrap the object
 169          if (empty($data['public_key'])) {
 170              $errors['public_key'] = get_string('publickeyrequired', 'mnet');
 171          } else if (!$credentials = $mnet_peer->check_credentials($data['public_key'])) {
 172              $errmsg = '';
 173              foreach ($mnet_peer->error as $err) {
 174                  $errmsg .= $err['code'] . ': ' . $err['text'].'<br />';
 175              }
 176              $errors['public_key'] = get_string('invalidpubkey', 'mnet', $errmsg);
 177          }
 178          unset($mnet_peer);
 179          return $errors;
 180      }
 181  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1