PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/mnet/peers.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 280 lines | 195 code | 38 blank | 47 comment | 46 complexity | b61a5f57cd9e496658e82f7aac95e906 MD5 | raw file
  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. * Page to allow the administrator to configure networked hosts, and add new ones
  18. *
  19. * @package core
  20. * @subpackage mnet
  21. * @copyright 2007 Donal McMullan
  22. * @copyright 2007 Martin Langhoff
  23. * @copyright 2010 Penny Leach
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
  27. require_once($CFG->libdir.'/adminlib.php');
  28. require_once($CFG->dirroot.'/mnet/lib.php');
  29. require_once($CFG->dirroot.'/'.$CFG->admin.'/mnet/peer_forms.php');
  30. require_login();
  31. $context = context_system::instance();
  32. require_capability('moodle/site:config', $context, $USER->id, true, 'nopermissions');
  33. /// Initialize variables.
  34. $hostid = optional_param('hostid', 0, PARAM_INT);
  35. $updra = optional_param('updateregisterall', 0, PARAM_INT);
  36. // first process the register all hosts setting if required
  37. if (!empty($updra)) {
  38. set_config('mnet_register_allhosts', optional_param('registerallhosts', 0, PARAM_INT));
  39. redirect(new moodle_url('/admin/mnet/peers.php'), get_string('changessaved'));
  40. }
  41. $adminsection = 'mnetpeers';
  42. if ($hostid && $DB->get_field('mnet_host', 'deleted', array('id' => $hostid)) != 1) {
  43. $adminsection = 'mnetpeer' . $hostid;
  44. }
  45. $PAGE->set_url('/admin/mnet/peers.php');
  46. admin_externalpage_setup($adminsection);
  47. if (!extension_loaded('openssl')) {
  48. print_error('requiresopenssl', 'mnet');
  49. }
  50. if (!function_exists('curl_init') ) {
  51. print_error('nocurl', 'mnet');
  52. }
  53. if (!function_exists('xmlrpc_encode_request')) {
  54. print_error('xmlrpc-missing', 'mnet');
  55. }
  56. if (!isset($CFG->mnet_dispatcher_mode)) {
  57. set_config('mnet_dispatcher_mode', 'off');
  58. }
  59. $mnet_peer = new mnet_peer();
  60. $simpleform = new mnet_simple_host_form(); // the one that goes on the bottom of the main page
  61. $reviewform = null; // set up later in different code branches, so mnet_peer can be passed to the constructor
  62. // if the first form has been submitted, bootstrap the peer and load up the review form
  63. if ($formdata = $simpleform->get_data()) {
  64. // ensure we remove trailing slashes
  65. $formdata->wwwroot = trim($formdata->wwwroot);
  66. $formdata->wwwroot = rtrim($formdata->wwwroot, '/');
  67. // ensure the wwwroot starts with a http or https prefix
  68. if (strtolower(substr($formdata->wwwroot, 0, 4)) != 'http') {
  69. $formdata->wwwroot = 'http://'.$formdata->wwwroot;
  70. }
  71. $mnet_peer->set_applicationid($formdata->applicationid);
  72. $application = $DB->get_field('mnet_application', 'name', array('id'=>$formdata->applicationid));
  73. $mnet_peer->bootstrap($formdata->wwwroot, null, $application);
  74. // bootstrap the second form straight with the data from the first form
  75. $reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer)); // the second step (also the edit host form)
  76. $formdata->oldpublickey = $mnet_peer->public_key; // set this so we can confirm on form post without having to recreate the mnet_peer object
  77. $reviewform->set_data($mnet_peer);
  78. echo $OUTPUT->header();
  79. echo $OUTPUT->box_start();
  80. $reviewform->display();
  81. echo $OUTPUT->box_end();
  82. echo $OUTPUT->footer();
  83. exit;
  84. } else if ($simpleform->is_submitted()) { // validation failed
  85. $noreviewform = true;
  86. }
  87. // editing a host - load up the review form
  88. if (!empty($hostid)) {
  89. // TODO print a nice little heading
  90. $mnet_peer->set_id($hostid);
  91. echo $OUTPUT->header();
  92. $currenttab = 'mnetdetails';
  93. require_once($CFG->dirroot . '/admin/mnet/tabs.php');
  94. if ($hostid != $CFG->mnet_all_hosts_id) {
  95. $mnet_peer->currentkey = mnet_get_public_key($mnet_peer->wwwroot, $mnet_peer->application);
  96. if ($mnet_peer->currentkey == $mnet_peer->public_key) {
  97. unset($mnet_peer->currentkey);
  98. } else {
  99. error_log($mnet_peer->currentkey);
  100. error_log($mnet_peer->public_key);
  101. error_log(md5($mnet_peer->currentkey));
  102. error_log(md5($mnet_peer->public_key));
  103. }
  104. $credentials = $mnet_peer->check_credentials($mnet_peer->public_key);
  105. $reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer)); // the second step (also the edit host form)
  106. $mnet_peer->oldpublickey = $mnet_peer->public_key; // set this so we can confirm on form post without having to recreate the mnet_peer object
  107. $reviewform->set_data((object)$mnet_peer);
  108. echo $OUTPUT->box_start();
  109. $reviewform->display();
  110. echo $OUTPUT->box_end();
  111. } else {
  112. // no options for allhosts host - just let the tabs display and print a notification
  113. echo $OUTPUT->notification(get_string('allhosts_no_options', 'mnet'));
  114. }
  115. echo $OUTPUT->footer();
  116. exit;
  117. }
  118. // either we're in the second step of setting up a new host
  119. // or editing an existing host
  120. // try our best to set up the mnet_peer object to pass to the form definition
  121. // unless validation on simpleform failed, in which case fall through.
  122. if (empty($noreviewform) && $id = optional_param('id', 0, PARAM_INT)) {
  123. // we're editing an existing one, so set up the tabs
  124. $currenttab = 'mnetdetails';
  125. $mnet_peer->set_id($id);
  126. require_once($CFG->dirroot . '/admin/mnet/tabs.php');
  127. } else if (empty($noreviewform) && ($wwwroot = optional_param('wwwroot', '', PARAM_URL)) && ($applicationid = optional_param('applicationid', 0, PARAM_INT))) {
  128. $application = $DB->get_field('mnet_application', 'name', array('id'=>$applicationid));
  129. $mnet_peer->bootstrap($wwwroot, null, $application);
  130. }
  131. $reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer));
  132. if ($formdata = $reviewform->get_data()) {
  133. $mnet_peer->set_applicationid($formdata->applicationid);
  134. $application = $DB->get_field('mnet_application', 'name', array('id'=>$formdata->applicationid));
  135. $mnet_peer->bootstrap($formdata->wwwroot, null, $application);
  136. if (!empty($formdata->name) && $formdata->name != $mnet_peer->name) {
  137. $mnet_peer->set_name($formdata->name);
  138. }
  139. if (empty($formdata->theme)) {
  140. $mnet_peer->force_theme = 0;
  141. $mnet_peer->theme = null;
  142. } else {
  143. $mnet_peer->force_theme = 1;
  144. $mnet_peer->theme = $formdata->theme;
  145. }
  146. $mnet_peer->deleted = $formdata->deleted;
  147. $mnet_peer->public_key = $formdata->public_key;
  148. $credentials = $mnet_peer->check_credentials($mnet_peer->public_key);
  149. $mnet_peer->public_key_expires = $credentials['validTo_time_t'];
  150. if ($mnet_peer->commit()) {
  151. redirect(new moodle_url('/admin/mnet/peers.php', array('hostid' => $mnet_peer->id)), get_string('changessaved'));
  152. } else {
  153. print_error('invalidaction', 'error', 'index.php');
  154. }
  155. } else if ($reviewform->is_submitted()) { // submitted, but errors
  156. echo $OUTPUT->header();
  157. echo $OUTPUT->box_start();
  158. $reviewform->display();
  159. echo $OUTPUT->box_end();
  160. echo $OUTPUT->footer();
  161. exit;
  162. }
  163. // normal flow - just display all hosts with links
  164. echo $OUTPUT->header();
  165. $hosts = mnet_get_hosts(true);
  166. // print the table to display the register all hosts setting
  167. $table = new html_table();
  168. $table->head = array(get_string('registerallhosts', 'mnet'));
  169. $registerrow = '';
  170. $registerstr = '';
  171. $registerurl = new moodle_url('/admin/mnet/peers.php', array('updateregisterall' => 1));
  172. if (!empty($CFG->mnet_register_allhosts)) {
  173. $registerrow = get_string('registerhostson', 'mnet');
  174. $registerurl->param('registerallhosts', 0);
  175. $registerstr = get_string('turnitoff', 'mnet');
  176. } else {
  177. $registerrow = get_string('registerhostsoff', 'mnet');
  178. $registerurl->param('registerallhosts', 1);
  179. $registerstr = get_string('turniton', 'mnet');
  180. }
  181. $registerrow .= $OUTPUT->single_button($registerurl, $registerstr);
  182. // simple table with two rows of a single cell
  183. $table->data = array(
  184. array(
  185. get_string('registerallhostsexplain', 'mnet'),
  186. ),
  187. array(
  188. $registerrow
  189. ),
  190. );
  191. echo html_writer::table($table);
  192. // print the list of all hosts, with little action links and buttons
  193. $table = new html_table();
  194. $table->head = array(
  195. get_string('site'),
  196. get_string('system', 'mnet'),
  197. get_string('last_connect_time', 'mnet'),
  198. '',
  199. );
  200. $table->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap');
  201. $baseurl = new moodle_url('/admin/mnet/peers.php');
  202. $deleted = array();
  203. foreach($hosts as $host) {
  204. $hosturl = new moodle_url($baseurl, array('hostid' => $host->id));
  205. if (trim($host->name) === '') {
  206. // should not happen but...
  207. $host->name = '???';
  208. }
  209. // process all hosts first since it's the easiest
  210. if ($host->id == $CFG->mnet_all_hosts_id) {
  211. $table->data[] = array(html_writer::link($hosturl, get_string('allhosts', 'core_mnet')), '*', '', '');
  212. continue;
  213. }
  214. // populate the list of deleted hosts
  215. if ($host->deleted) {
  216. $deleted[] = html_writer::link($hosturl, $host->name);
  217. continue;
  218. }
  219. if ($host->last_connect_time == 0) {
  220. $last_connect = get_string('never');
  221. } else {
  222. $last_connect = userdate($host->last_connect_time, get_string('strftimedatetime', 'core_langconfig'));
  223. }
  224. $table->data[] = array(
  225. html_writer::link($hosturl, $host->name),
  226. html_writer::link($host->wwwroot, $host->wwwroot),
  227. $last_connect,
  228. $OUTPUT->single_button(new moodle_url('/admin/mnet/delete.php', array('hostid' => $host->id)), get_string('delete'))
  229. );
  230. }
  231. echo html_writer::table($table);
  232. if ($deleted) {
  233. echo $OUTPUT->box(get_string('deletedhosts', 'core_mnet', join(', ', $deleted)), 'deletedhosts');
  234. }
  235. // finally, print the initial form to add a new host
  236. echo $OUTPUT->box_start();
  237. echo $OUTPUT->heading(get_string('addnewhost', 'mnet'), 3);
  238. $simpleform->display();
  239. echo $OUTPUT->box_end();
  240. // done
  241. echo $OUTPUT->footer();
  242. exit;