|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->libdir . '/formslib.php'); 00030 00035 class mnet_simple_host_form extends moodleform { 00036 function definition() { 00037 global $DB; 00038 00039 $mform = $this->_form; 00040 00041 $mform->addElement('text', 'wwwroot', get_string('hostname', 'mnet'), array('maxlength' => 255, 'size' => 50)); 00042 $mform->setType('wwwroot', PARAM_URL); 00043 $mform->addRule('wwwroot', null, 'required', null, 'client'); 00044 $mform->addRule('wwwroot', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); 00045 00046 $mform->addElement('select', 'applicationid', get_string('applicationtype', 'mnet'), 00047 $DB->get_records_menu('mnet_application', array(), 'id,display_name')); 00048 $mform->addRule('applicationid', null, 'required', null, 'client'); 00049 00050 $this->add_action_buttons(false, get_string('addhost', 'mnet')); 00051 } 00052 00053 function validation($data) { 00054 global $DB; 00055 00056 $wwwroot = $data['wwwroot']; 00057 // ensure the wwwroot starts with a http or https prefix 00058 if (strtolower(substr($wwwroot, 0, 4)) != 'http') { 00059 $wwwroot = 'http://'.$wwwroot; 00060 } 00061 if ($host = $DB->get_record('mnet_host', array('wwwroot' => $wwwroot))) { 00062 global $CFG; 00063 return array('wwwroot' => get_string('hostexists', 'mnet', $CFG->wwwroot . '/admin/mnet/peers.php?hostid=' . $host->id)); 00064 } 00065 return array(); 00066 } 00067 } 00068 00073 class mnet_review_host_form extends moodleform { 00074 function definition() { 00075 global $OUTPUT; 00076 00077 $mform = $this->_form; 00078 $mnet_peer = $this->_customdata['peer']; 00079 00080 $mform->addElement('hidden', 'last_connect_time'); 00081 $mform->addElement('hidden', 'id'); 00082 $mform->addElement('hidden', 'applicationid'); 00083 $mform->addElement('hidden', 'oldpublickey'); 00084 00085 $mform->addElement('text', 'name', get_string('site'), array('maxlength' => 80, 'size' => 50)); 00086 $mform->setType('name', PARAM_NOTAGS); 00087 $mform->addRule('name', get_string('maximumchars', '', 80), 'maxlength', 80, 'client'); 00088 00089 $mform->addElement('text', 'wwwroot', get_string('hostname', 'mnet'), array('maxlength' => 255, 'size' => 50)); 00090 $mform->setType('wwwroot', PARAM_URL); 00091 $mform->addRule('wwwroot', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); 00092 00093 $themes = array('' => get_string('forceno')); 00094 foreach (array_keys(get_plugin_list('theme')) as $themename) { 00095 $themes[$themename] = get_string('pluginname', 'theme_'.$themename); 00096 } 00097 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes); 00098 00099 $mform->addElement('textarea', 'public_key', get_string('publickey', 'mnet'), array('rows' => 17, 'cols' => 100, 'class' => 'smalltext')); 00100 $mform->setType('public_key', PARAM_PEM); 00101 $mform->addRule('public_key', get_string('required'), 'required'); 00102 00103 // finished with form controls, now the static informational stuff 00104 if ($mnet_peer && !empty($mnet_peer->bootstrapped)) { 00105 $expires = ''; 00106 if ($mnet_peer->public_key_expires < time()) { 00107 $expires = get_string('expired', 'mnet') . ' '; 00108 } 00109 $expires .= userdate($mnet_peer->public_key_expires); 00110 $mform->addElement('static', 'validuntil', get_string('expires', 'mnet'), $expires); 00111 00112 $lastconnect = ''; 00113 if ($mnet_peer->last_connect_time == 0) { 00114 $lastconnect = get_string('never', 'mnet'); 00115 } else { 00116 $lastconnect = date('H:i:s d/m/Y',$mnet_peer->last_connect_time); 00117 } 00118 00119 $mform->addElement('static', 'lastconnect', get_string('last_connect_time', 'mnet'), $lastconnect); 00120 $mform->addElement('static', 'ipaddress', get_string('ipaddress', 'mnet'), $mnet_peer->ip_address); 00121 00122 if (isset($mnet_peer->currentkey)) { // key being published is not the same as our records 00123 $currentkeystr = '<b>' . get_string('keymismatch', 'mnet') . '</b><br /><br /> ' . $OUTPUT->box('<pre>' . $mnet_peer->currentkey . '</pre>'); 00124 $mform->addElement('static', 'keymismatch', get_string('currentkey', 'mnet'), $currentkeystr); 00125 } 00126 00127 $credstr = ''; 00128 if ($credentials = $mnet_peer->check_credentials($mnet_peer->public_key)) { 00129 foreach($credentials['subject'] as $key => $credential) { 00130 if (is_scalar($credential)) { 00131 $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.$credential."\n"; 00132 } else { 00133 $credstr .= str_pad($key, 16, " ", STR_PAD_LEFT).': '.var_export($credential,1)."\n"; 00134 } 00135 } 00136 } 00137 00138 $mform->addElement('static', 'certdetails', get_string('certdetails', 'mnet'), 00139 $OUTPUT->box('<pre>' . $credstr . '</pre>', 'generalbox certdetails')); 00140 } 00141 00142 if ($mnet_peer && !empty($mnet_peer->deleted)) { 00143 $radioarray = array(); 00144 $radioarray[] = MoodleQuickForm::createElement('static', 'deletedinfo', '', 00145 $OUTPUT->container(get_string('deletedhostinfo', 'mnet'), 'deletedhostinfo')); 00146 $radioarray[] = MoodleQuickForm::createElement('radio', 'deleted', '', get_string('yes'), 1); 00147 $radioarray[] = MoodleQuickForm::createElement('radio', 'deleted', '', get_string('no'), 0); 00148 $mform->addGroup($radioarray, 'radioar', get_string('deleted'), array(' ', ' '), false); 00149 } else { 00150 $mform->addElement('hidden', 'deleted'); 00151 } 00152 00153 // finished with static stuff, print save button 00154 $this->add_action_buttons(false); 00155 } 00156 00157 function validation($data) { 00158 $errors = array(); 00159 if ($data['oldpublickey'] == $data['public_key']) { 00160 return; 00161 } 00162 $mnet_peer = new mnet_peer(); // idiotic api 00163 $mnet_peer->wwwroot = $data['wwwroot']; // just hard-set this rather than bootstrap the object 00164 if (empty($data['public_key'])) { 00165 $errors['public_key'] = get_string('publickeyrequired', 'mnet'); 00166 } else if (!$credentials = $mnet_peer->check_credentials($data['public_key'])) { 00167 $errmsg = ''; 00168 foreach ($mnet_peer->error as $err) { 00169 $errmsg .= $err['code'] . ': ' . $err['text'].'<br />'; 00170 } 00171 $errors['public_key'] = get_string('invalidpubkey', 'mnet', $errmsg); 00172 } 00173 unset($mnet_peer); 00174 return $errors; 00175 } 00176 }