Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/mnet/peers.php
Go to the documentation of this file.
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 
00018 
00030 require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
00031 require_once($CFG->libdir.'/adminlib.php');
00032 require_once($CFG->dirroot.'/mnet/lib.php');
00033 require_once($CFG->dirroot.'/admin/mnet/peer_forms.php');
00034 
00035 require_login();
00036 
00037 $context = get_context_instance(CONTEXT_SYSTEM);
00038 require_capability('moodle/site:config', $context, $USER->id, true, 'nopermissions');
00039 
00041 $hostid = optional_param('hostid', 0, PARAM_INT);
00042 $updra = optional_param('updateregisterall', 0, PARAM_INT);
00043 
00044 // first process the register all hosts setting if required
00045 if (!empty($updra)) {
00046     set_config('mnet_register_allhosts', optional_param('registerallhosts', 0, PARAM_INT));
00047     redirect(new moodle_url('/admin/mnet/peers.php'), get_string('changessaved'));
00048 }
00049 
00050 $adminsection = 'mnetpeers';
00051 if ($hostid && $DB->get_field('mnet_host', 'deleted', array('id' => $hostid)) != 1) {
00052     $adminsection = 'mnetpeer' . $hostid;
00053 }
00054 
00055 $PAGE->set_url('/admin/mnet/peers.php');
00056 admin_externalpage_setup($adminsection);
00057 
00058 if (!extension_loaded('openssl')) {
00059     print_error('requiresopenssl', 'mnet');
00060 }
00061 
00062 if (!function_exists('curl_init') ) {
00063     print_error('nocurl', 'mnet');
00064 }
00065 
00066 if (!function_exists('xmlrpc_encode_request')) {
00067     print_error('xmlrpc-missing', 'mnet');
00068 }
00069 
00070 if (!isset($CFG->mnet_dispatcher_mode)) {
00071     set_config('mnet_dispatcher_mode', 'off');
00072 }
00073 
00074 $mnet_peer = new mnet_peer();
00075 $simpleform = new mnet_simple_host_form(); // the one that goes on the bottom of the main page
00076 $reviewform = null; // set up later in different code branches, so mnet_peer can be passed to the constructor
00077 
00078 // if the first form has been submitted, bootstrap the peer and load up the review form
00079 if ($formdata = $simpleform->get_data()) {
00080     // ensure we remove trailing slashes
00081     $formdata->wwwroot = trim($formdata->wwwroot);
00082     $formdata->wwwroot = rtrim($formdata->wwwroot, '/');
00083 
00084     // ensure the wwwroot starts with a http or https prefix
00085     if (strtolower(substr($formdata->wwwroot, 0, 4)) != 'http') {
00086         $formdata->wwwroot = 'http://'.$formdata->wwwroot;
00087     }
00088 
00089     $mnet_peer->set_applicationid($formdata->applicationid);
00090     $application = $DB->get_field('mnet_application', 'name', array('id'=>$formdata->applicationid));
00091     $mnet_peer->bootstrap($formdata->wwwroot, null, $application);
00092     // bootstrap the second form straight with the data from the first form
00093     $reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer)); // the second step (also the edit host form)
00094     $formdata->oldpublickey = $mnet_peer->public_key; // set this so we can confirm on form post without having to recreate the mnet_peer object
00095     $reviewform->set_data($mnet_peer);
00096     echo $OUTPUT->header();
00097     echo $OUTPUT->box_start();
00098     $reviewform->display();
00099     echo $OUTPUT->box_end();
00100     echo $OUTPUT->footer();
00101     exit;
00102 } else if ($simpleform->is_submitted()) { // validation failed
00103     $noreviewform = true;
00104 }
00105 
00106 // editing a host - load up the review form
00107 if (!empty($hostid)) {
00108     // TODO print a nice little heading
00109     $mnet_peer->set_id($hostid);
00110     echo $OUTPUT->header();
00111     $currenttab = 'mnetdetails';
00112     require_once($CFG->dirroot . '/admin/mnet/tabs.php');
00113 
00114     if ($hostid != $CFG->mnet_all_hosts_id) {
00115         $mnet_peer->currentkey = mnet_get_public_key($mnet_peer->wwwroot, $mnet_peer->application);
00116         if ($mnet_peer->currentkey == $mnet_peer->public_key) {
00117             unset($mnet_peer->currentkey);
00118         } else {
00119             error_log($mnet_peer->currentkey);
00120             error_log($mnet_peer->public_key);
00121             error_log(md5($mnet_peer->currentkey));
00122             error_log(md5($mnet_peer->public_key));
00123         }
00124         $credentials = $mnet_peer->check_credentials($mnet_peer->public_key);
00125         $reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer)); // the second step (also the edit host form)
00126         $mnet_peer->oldpublickey = $mnet_peer->public_key; // set this so we can confirm on form post without having to recreate the mnet_peer object
00127         $reviewform->set_data((object)$mnet_peer);
00128         echo $OUTPUT->box_start();
00129         $reviewform->display();
00130         echo $OUTPUT->box_end();
00131     } else {
00132         // no options for allhosts host - just let the tabs display and print a notification
00133         echo $OUTPUT->notification(get_string('allhosts_no_options', 'mnet'));
00134     }
00135     echo $OUTPUT->footer();
00136     exit;
00137 }
00138 
00139 // either we're in the second step of setting up a new host
00140 // or editing an existing host
00141 // try our best to set up the mnet_peer object to pass to the form definition
00142 // unless validation on simpleform failed, in which case fall through.
00143 if (empty($noreviewform) && $id = optional_param('id', 0, PARAM_INT)) {
00144     // we're editing an existing one, so set up the tabs
00145     $currenttab = 'mnetdetails';
00146     $mnet_peer->set_id($id);
00147     require_once($CFG->dirroot . '/admin/mnet/tabs.php');
00148 } else if (empty($noreviewform) && ($wwwroot = optional_param('wwwroot', '', PARAM_URL)) && ($applicationid = optional_param('applicationid', 0, PARAM_INT))) {
00149     $application = $DB->get_field('mnet_application', 'name', array('id'=>$applicationid));
00150     $mnet_peer->bootstrap($wwwroot, null, $application);
00151 }
00152 $reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer));
00153 if ($formdata = $reviewform->get_data()) {
00154 
00155     $mnet_peer->set_applicationid($formdata->applicationid);
00156     $application = $DB->get_field('mnet_application', 'name', array('id'=>$formdata->applicationid));
00157     $mnet_peer->bootstrap($formdata->wwwroot, null, $application);
00158 
00159     if (!empty($formdata->name) && $formdata->name != $mnet_peer->name) {
00160         $mnet_peer->set_name($formdata->name);
00161     }
00162 
00163     if (empty($formdata->theme)) {
00164         $mnet_peer->force_theme = 0;
00165         $mnet_peer->theme = null;
00166     } else {
00167         $mnet_peer->force_theme = 1;
00168         $mnet_peer->theme = $formdata->theme;
00169     }
00170 
00171     $mnet_peer->deleted             = $formdata->deleted;
00172     $mnet_peer->public_key          = $formdata->public_key;
00173     $credentials                    = $mnet_peer->check_credentials($mnet_peer->public_key);
00174     $mnet_peer->public_key_expires  = $credentials['validTo_time_t'];
00175 
00176     if ($mnet_peer->commit()) {
00177         redirect(new moodle_url('/admin/mnet/peers.php', array('hostid' => $mnet_peer->id)), get_string('changessaved'));
00178     } else {
00179         print_error('invalidaction', 'error', 'index.php');
00180     }
00181 } else if ($reviewform->is_submitted()) { // submitted, but errors
00182     echo $OUTPUT->header();
00183     echo $OUTPUT->box_start();
00184     $reviewform->display();
00185     echo $OUTPUT->box_end();
00186     echo $OUTPUT->footer();
00187     exit;
00188 }
00189 
00190 
00191 // normal flow - just display all hosts with links
00192 echo $OUTPUT->header();
00193 $hosts = mnet_get_hosts(true);
00194 
00195 // print the table to display the register all hosts setting
00196 $table = new html_table();
00197 $table->head = array(get_string('registerallhosts', 'mnet'));
00198 
00199 $registerrow = '';
00200 $registerstr = '';
00201 $registerurl = new moodle_url('/admin/mnet/peers.php', array('updateregisterall' => 1));
00202 if (!empty($CFG->mnet_register_allhosts)) {
00203     $registerrow = get_string('registerhostson', 'mnet');
00204     $registerurl->param('registerallhosts', 0);
00205     $registerstr = get_string('turnitoff', 'mnet');
00206 } else {
00207     $registerrow = get_string('registerhostsoff', 'mnet');
00208     $registerurl->param('registerallhosts', 1);
00209     $registerstr = get_string('turniton', 'mnet');
00210 }
00211 $registerrow .= $OUTPUT->single_button($registerurl, $registerstr);
00212 
00213 // simple table with two rows of a single cell
00214 $table->data = array(
00215     array(
00216         get_string('registerallhostsexplain', 'mnet'),
00217     ),
00218     array(
00219         $registerrow
00220     ),
00221 );
00222 echo html_writer::table($table);
00223 
00224 // print the list of all hosts, with little action links and buttons
00225 $table = new html_table();
00226 $table->head = array(
00227     get_string('site'),
00228     get_string('system', 'mnet'),
00229     get_string('last_connect_time', 'mnet'),
00230     '',
00231 );
00232 $table->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap');
00233 $baseurl = new moodle_url('/admin/mnet/peers.php');
00234 $deleted = array();
00235 foreach($hosts as $host) {
00236     $hosturl = new moodle_url($baseurl, array('hostid' => $host->id));
00237     if (trim($host->name) === '') {
00238         // should not happen but...
00239         $host->name = '???';
00240     }
00241     // process all hosts first since it's the easiest
00242     if ($host->id == $CFG->mnet_all_hosts_id) {
00243         $table->data[] = array(html_writer::link($hosturl, get_string('allhosts', 'core_mnet')), '*', '', '');
00244         continue;
00245     }
00246 
00247     // populate the list of deleted hosts
00248     if ($host->deleted) {
00249         $deleted[] = html_writer::link($hosturl, $host->name);
00250         continue;
00251     }
00252 
00253     if ($host->last_connect_time == 0) {
00254         $last_connect = get_string('never');
00255     } else {
00256         $last_connect = userdate($host->last_connect_time, get_string('strftimedatetime', 'core_langconfig'));
00257     }
00258     $table->data[] = array(
00259         html_writer::link($hosturl, $host->name),
00260         html_writer::link($host->wwwroot, $host->wwwroot),
00261         $last_connect,
00262         $OUTPUT->single_button(new moodle_url('/admin/mnet/delete.php', array('hostid' => $host->id)), get_string('delete'))
00263     );
00264 }
00265 echo html_writer::table($table);
00266 
00267 if ($deleted) {
00268     echo $OUTPUT->box(get_string('deletedhosts', 'core_mnet', join(', ', $deleted)), 'deletedhosts');
00269 }
00270 
00271 // finally, print the initial form to add a new host
00272 echo $OUTPUT->box_start();
00273 echo $OUTPUT->heading(get_string('addnewhost', 'mnet'), 3);
00274 $simpleform->display();
00275 echo $OUTPUT->box_end();
00276 
00277 // done
00278 echo $OUTPUT->footer();
00279 exit;
00280 
 All Data Structures Namespaces Files Functions Variables Enumerations