PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/mnet/peer_forms.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 181 lines | 121 code | 27 blank | 33 comment | 22 complexity | 6509be5b3fc08fa79fb0057033b27582 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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', $CFG->wwwroot . '/admin/mnet/peers.php?hostid=' . $host->id));
  53. }
  54. return array();
  55. }
  56. }
  57. /**
  58. * The second step of the form - reviewing the host details
  59. * This is also the same form that is used for editing an existing host
  60. */
  61. class mnet_review_host_form extends moodleform {
  62. function definition() {
  63. global $OUTPUT;
  64. $mform = $this->_form;
  65. $mnet_peer = $this->_customdata['peer'];
  66. $mform->addElement('hidden', 'last_connect_time');
  67. $mform->setType('last_connect_time', PARAM_INT);
  68. $mform->addElement('hidden', 'id');
  69. $mform->setType('id', PARAM_INT);
  70. $mform->addElement('hidden', 'applicationid');
  71. $mform->setType('applicationid', PARAM_INT);
  72. $mform->addElement('hidden', 'oldpublickey');
  73. $mform->setType('oldpublickey', PARAM_PEM);
  74. $mform->addElement('text', 'name', get_string('site'), array('maxlength' => 80, 'size' => 50));
  75. $mform->setType('name', PARAM_NOTAGS);
  76. $mform->addRule('name', get_string('maximumchars', '', 80), 'maxlength', 80, 'client');
  77. $mform->addElement('text', 'wwwroot', get_string('hostname', 'mnet'), array('maxlength' => 255, 'size' => 50));
  78. $mform->setType('wwwroot', PARAM_URL);
  79. $mform->addRule('wwwroot', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  80. $themes = array('' => get_string('forceno'));
  81. foreach (array_keys(core_component::get_plugin_list('theme')) as $themename) {
  82. $themes[$themename] = get_string('pluginname', 'theme_'.$themename);
  83. }
  84. $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
  85. $mform->addElement('textarea', 'public_key', get_string('publickey', 'mnet'), array('rows' => 17, 'cols' => 100, 'class' => 'smalltext'));
  86. $mform->setType('public_key', PARAM_PEM);
  87. $mform->addRule('public_key', get_string('required'), 'required');
  88. // finished with form controls, now the static informational stuff
  89. if ($mnet_peer && !empty($mnet_peer->bootstrapped)) {
  90. $expires = '';
  91. if ($mnet_peer->public_key_expires < time()) {
  92. $expires = get_string('expired', 'mnet') . ' ';
  93. }
  94. $expires .= userdate($mnet_peer->public_key_expires);
  95. $mform->addElement('static', 'validuntil', get_string('expires', 'mnet'), $expires);
  96. $lastconnect = '';
  97. if ($mnet_peer->last_connect_time == 0) {
  98. $lastconnect = get_string('never', 'mnet');
  99. } else {
  100. $lastconnect = date('H:i:s d/m/Y',$mnet_peer->last_connect_time);
  101. }
  102. $mform->addElement('static', 'lastconnect', get_string('last_connect_time', 'mnet'), $lastconnect);
  103. $mform->addElement('static', 'ipaddress', get_string('ipaddress', 'mnet'), $mnet_peer->ip_address);
  104. if (isset($mnet_peer->currentkey)) { // key being published is not the same as our records
  105. $currentkeystr = '<b>' . get_string('keymismatch', 'mnet') . '</b><br /><br /> ' . $OUTPUT->box('<pre>' . $mnet_peer->currentkey . '</pre>');
  106. $mform->addElement('static', 'keymismatch', get_string('currentkey', 'mnet'), $currentkeystr);
  107. }
  108. $credstr = '';
  109. if ($credentials = $mnet_peer->check_credentials($mnet_peer->public_key)) {
  110. foreach($credentials['subject'] as $key => $credential) {
  111. if (is_scalar($credential)) {
  112. $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.$credential."\n";
  113. } else {
  114. $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.var_export($credential,1)."\n";
  115. }
  116. }
  117. }
  118. $mform->addElement('static', 'certdetails', get_string('certdetails', 'mnet'),
  119. $OUTPUT->box('<pre>' . $credstr . '</pre>', 'generalbox certdetails'));
  120. }
  121. if ($mnet_peer && !empty($mnet_peer->deleted)) {
  122. $radioarray = array();
  123. $radioarray[] = $mform->createElement('static', 'deletedinfo', '',
  124. $OUTPUT->container(get_string('deletedhostinfo', 'mnet'), 'deletedhostinfo'));
  125. $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('yes'), 1);
  126. $radioarray[] = $mform->createElement('radio', 'deleted', '', get_string('no'), 0);
  127. $mform->addGroup($radioarray, 'radioar', get_string('deleted'), array(' ', ' '), false);
  128. } else {
  129. $mform->addElement('hidden', 'deleted');
  130. $mform->setType('deleted', PARAM_BOOL);
  131. }
  132. // finished with static stuff, print save button
  133. $this->add_action_buttons(false);
  134. }
  135. function validation($data, $files) {
  136. $errors = array();
  137. if ($data['oldpublickey'] == $data['public_key']) {
  138. return;
  139. }
  140. $mnet_peer = new mnet_peer(); // idiotic api
  141. $mnet_peer->wwwroot = $data['wwwroot']; // just hard-set this rather than bootstrap the object
  142. if (empty($data['public_key'])) {
  143. $errors['public_key'] = get_string('publickeyrequired', 'mnet');
  144. } else if (!$credentials = $mnet_peer->check_credentials($data['public_key'])) {
  145. $errmsg = '';
  146. foreach ($mnet_peer->error as $err) {
  147. $errmsg .= $err['code'] . ': ' . $err['text'].'<br />';
  148. }
  149. $errors['public_key'] = get_string('invalidpubkey', 'mnet', $errmsg);
  150. }
  151. unset($mnet_peer);
  152. return $errors;
  153. }
  154. }