PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/mnet/peer_forms.php

http://github.com/moodle/moodle
PHP | 191 lines | 130 code | 28 blank | 33 comment | 22 complexity | 6967c1a9d6c1b2cd0749ad1d9a866afa MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This file contains two forms for adding/editing mnet hosts, used by peers.php
  18. *
  19. * @package core
  20. * @subpackage mnet
  21. * @copyright 2010 Penny Leach
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once($CFG->libdir . '/formslib.php');
  26. /**
  27. * The very basic first step add new host form - just wwwroot & application
  28. * The second form is loaded up with the information from this one.
  29. */
  30. class mnet_simple_host_form extends moodleform {
  31. function definition() {
  32. global $DB;
  33. $mform = $this->_form;
  34. $mform->addElement('text', 'wwwroot', get_string('hostname', 'mnet'), array('maxlength' => 255, 'size' => 50));
  35. $mform->setType('wwwroot', PARAM_URL);
  36. $mform->addRule('wwwroot', null, 'required', null, 'client');
  37. $mform->addRule('wwwroot', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  38. $mform->addElement('select', 'applicationid', get_string('applicationtype', 'mnet'),
  39. $DB->get_records_menu('mnet_application', array(), 'id,display_name'));
  40. $mform->addRule('applicationid', null, 'required', null, 'client');
  41. $this->add_action_buttons(false, get_string('addhost', 'mnet'));
  42. }
  43. function validation($data, $files) {
  44. global $DB;
  45. $wwwroot = $data['wwwroot'];
  46. // ensure the wwwroot starts with a http or https prefix
  47. if (strtolower(substr($wwwroot, 0, 4)) != 'http') {
  48. $wwwroot = 'http://'.$wwwroot;
  49. }
  50. if ($host = $DB->get_record('mnet_host', array('wwwroot' => $wwwroot))) {
  51. global $CFG;
  52. return array('wwwroot' => get_string('hostexists', 'mnet',
  53. new moodle_url('/admin/mnet/peers.php', array('hostid' => $host->id))));
  54. }
  55. return array();
  56. }
  57. }
  58. /**
  59. * The second step of the form - reviewing the host details
  60. * This is also the same form that is used for editing an existing host
  61. */
  62. class mnet_review_host_form extends moodleform {
  63. function definition() {
  64. global $OUTPUT;
  65. $mform = $this->_form;
  66. $mnet_peer = $this->_customdata['peer'];
  67. $mform->addElement('hidden', 'last_connect_time');
  68. $mform->setType('last_connect_time', PARAM_INT);
  69. $mform->addElement('hidden', 'id');
  70. $mform->setType('id', PARAM_INT);
  71. $mform->addElement('hidden', 'applicationid');
  72. $mform->setType('applicationid', PARAM_INT);
  73. $mform->addElement('hidden', 'oldpublickey');
  74. $mform->setType('oldpublickey', PARAM_PEM);
  75. $mform->addElement('text', 'name', get_string('site'), array('maxlength' => 80, 'size' => 50));
  76. $mform->setType('name', PARAM_NOTAGS);
  77. $mform->addRule('name', get_string('maximumchars', '', 80), 'maxlength', 80, 'client');
  78. $mform->addElement('text', 'wwwroot', get_string('hostname', 'mnet'), array('maxlength' => 255, 'size' => 50));
  79. $mform->setType('wwwroot', PARAM_URL);
  80. $mform->addRule('wwwroot', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  81. $options = array(
  82. mnet_peer::SSL_NONE => get_string('none'),
  83. mnet_peer::SSL_HOST => get_string('verifyhostonly', 'core_mnet'),
  84. mnet_peer::SSL_HOST_AND_PEER => get_string('verifyhostandpeer', 'core_mnet')
  85. );
  86. $mform->addElement('select', 'sslverification', get_string('sslverification', 'core_mnet'), $options);
  87. $mform->setDefault('sslverification', mnet_peer::SSL_HOST_AND_PEER);
  88. $mform->addHelpButton('sslverification', 'sslverification', 'core_mnet');
  89. $themes = array('' => get_string('forceno'));
  90. foreach (array_keys(core_component::get_plugin_list('theme')) as $themename) {
  91. $themes[$themename] = get_string('pluginname', 'theme_'.$themename);
  92. }
  93. $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
  94. $mform->addElement('textarea', 'public_key', get_string('publickey', 'mnet'), array('rows' => 17, 'cols' => 100, 'class' => 'smalltext'));
  95. $mform->setType('public_key', PARAM_PEM);
  96. $mform->addRule('public_key', get_string('required'), 'required');
  97. // finished with form controls, now the static informational stuff
  98. if ($mnet_peer && !empty($mnet_peer->bootstrapped)) {
  99. $expires = '';
  100. if ($mnet_peer->public_key_expires < time()) {
  101. $expires = get_string('expired', 'mnet') . ' ';
  102. }
  103. $expires .= userdate($mnet_peer->public_key_expires);
  104. $mform->addElement('static', 'validuntil', get_string('expires', 'mnet'), $expires);
  105. $lastconnect = '';
  106. if ($mnet_peer->last_connect_time == 0) {
  107. $lastconnect = get_string('never', 'mnet');
  108. } else {
  109. $lastconnect = date('H:i:s d/m/Y',$mnet_peer->last_connect_time);
  110. }
  111. $mform->addElement('static', 'lastconnect', get_string('last_connect_time', 'mnet'), $lastconnect);
  112. $mform->addElement('static', 'ipaddress', get_string('ipaddress', 'mnet'), $mnet_peer->ip_address);
  113. if (isset($mnet_peer->currentkey)) { // key being published is not the same as our records
  114. $currentkeystr = '<b>' . get_string('keymismatch', 'mnet') . '</b><br /><br /> ' . $OUTPUT->box('<pre>' . $mnet_peer->currentkey . '</pre>');
  115. $mform->addElement('static', 'keymismatch', get_string('currentkey', 'mnet'), $currentkeystr);
  116. }
  117. $credstr = '';
  118. if ($credentials = $mnet_peer->check_credentials($mnet_peer->public_key)) {
  119. foreach($credentials['subject'] as $key => $credential) {
  120. if (is_scalar($credential)) {
  121. $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.$credential."\n";
  122. } else {
  123. $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.var_export($credential,1)."\n";
  124. }
  125. }
  126. }
  127. $mform->addElement('static', 'certdetails', get_string('certdetails', 'mnet'),
  128. $OUTPUT->box('<pre>' . $credstr . '</pre>', 'generalbox certdetails'));
  129. }
  130. if ($mnet_peer && !empty($mnet_peer->deleted)) {
  131. $radioarray = array();
  132. $radioarray[] = $mform->createElement('static', 'deletedinfo', '',
  133. $OUTPUT->container(get_string('deletedhostinfo', 'mnet'), 'alert alert-warning'));
  134. $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('yes'), 1);
  135. $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('no'), 0);
  136. $mform->addGroup($radioarray, 'radioar', get_string('deleted'), array(' ', ' '), false);
  137. } else {
  138. $mform->addElement('hidden', 'deleted');
  139. $mform->setType('deleted', PARAM_BOOL);
  140. }
  141. // finished with static stuff, print save button
  142. $this->add_action_buttons(false);
  143. }
  144. function validation($data, $files) {
  145. $errors = array();
  146. if ($data['oldpublickey'] == $data['public_key']) {
  147. return;
  148. }
  149. $mnet_peer = new mnet_peer(); // idiotic api
  150. $mnet_peer->wwwroot = $data['wwwroot']; // just hard-set this rather than bootstrap the object
  151. if (empty($data['public_key'])) {
  152. $errors['public_key'] = get_string('publickeyrequired', 'mnet');
  153. } else if (!$credentials = $mnet_peer->check_credentials($data['public_key'])) {
  154. $errmsg = '';
  155. foreach ($mnet_peer->error as $err) {
  156. $errmsg .= $err['code'] . ': ' . $err['text'].'<br />';
  157. }
  158. $errors['public_key'] = get_string('invalidpubkey', 'mnet', $errmsg);
  159. }
  160. unset($mnet_peer);
  161. return $errors;
  162. }
  163. }