|
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 require('../../config.php'); 00028 require_once($CFG->libdir.'/adminlib.php'); 00029 require_once("$CFG->libdir/externallib.php"); 00030 require_once($CFG->dirroot . "/" . $CFG->admin . "/webservice/testclient_forms.php"); 00031 00032 $function = optional_param('function', '', PARAM_SAFEDIR); 00033 $protocol = optional_param('protocol', '', PARAM_SAFEDIR); 00034 $authmethod = optional_param('authmethod', '', PARAM_SAFEDIR); 00035 00036 $PAGE->set_url('/' . $CFG->admin . '/webservice/testclient.php'); 00037 $PAGE->navbar->ignore_active(true); 00038 $PAGE->navbar->add(get_string('administrationsite')); 00039 $PAGE->navbar->add(get_string('development', 'admin')); 00040 $PAGE->navbar->add(get_string('testclient', 'webservice'), 00041 new moodle_url('/' . $CFG->admin . '/webservice/testclient.php')); 00042 if (!empty($function)) { 00043 $PAGE->navbar->add($function); 00044 } 00045 00046 admin_externalpage_setup('testclient'); 00047 00048 // list of all available functions for testing 00049 $allfunctions = $DB->get_records('external_functions', array(), 'name ASC'); 00050 $functions = array(); 00051 foreach ($allfunctions as $f) { 00052 $finfo = external_function_info($f); 00053 if (!empty($finfo->testclientpath) and file_exists($CFG->dirroot.'/'.$finfo->testclientpath)) { 00054 //some plugins may want to have own test client forms 00055 include_once($CFG->dirroot.'/'.$finfo->testclientpath); 00056 } 00057 $class = $f->name.'_form'; 00058 if (class_exists($class)) { 00059 $functions[$f->name] = $f->name; 00060 continue; 00061 } 00062 } 00063 00064 // whitelisting security 00065 if (!isset($functions[$function])) { 00066 $function = ''; 00067 } 00068 00069 // list all enabled webservices 00070 $available_protocols = get_plugin_list('webservice'); 00071 $active_protocols = empty($CFG->webserviceprotocols) ? array() : explode(',', $CFG->webserviceprotocols); 00072 $protocols = array(); 00073 foreach ($active_protocols as $p) { 00074 if (empty($available_protocols[$p])) { 00075 continue; 00076 } 00077 include_once($available_protocols[$p].'/locallib.php'); 00078 if (!class_exists('webservice_'.$p.'_test_client')) { 00079 // test client support not implemented 00080 continue; 00081 } 00082 $protocols[$p] = get_string('pluginname', 'webservice_'.$p); 00083 } 00084 if (!isset($protocols[$protocol])) { // whitelisting security 00085 $protocol = ''; 00086 } 00087 00088 if (!$function or !$protocol) { 00089 $mform = new webservice_test_client_form(null, array($functions, $protocols)); 00090 echo $OUTPUT->header(); 00091 echo $OUTPUT->heading(get_string('testclient', 'webservice')); 00092 echo $OUTPUT->box_start(); 00093 $url = new moodle_url('/' . $CFG->admin . '/settings.php?section=debugging'); 00094 $atag =html_writer::start_tag('a', array('href' => $url)).get_string('debug', 'admin').html_writer::end_tag('a'); 00095 $descparams = new stdClass(); 00096 $descparams->atag = $atag; 00097 $descparams->mode = get_string('debugnormal', 'admin'); 00098 $amfclienturl = new moodle_url('/webservice/amf/testclient/index.php'); 00099 $amfclientatag =html_writer::tag('a', get_string('amftestclient', 'webservice'), 00100 array('href' => $amfclienturl)); 00101 $descparams->amfatag = $amfclientatag; 00102 echo get_string('testclientdescription', 'webservice', $descparams); 00103 echo $OUTPUT->box_end(); 00104 00105 $mform->display(); 00106 echo $OUTPUT->footer(); 00107 die; 00108 } 00109 00110 $class = $function.'_form'; 00111 00112 $mform = new $class(null, array('authmethod' => $authmethod)); 00113 $mform->set_data(array('function'=>$function, 'protocol'=>$protocol)); 00114 00115 if ($mform->is_cancelled()) { 00116 redirect('testclient.php'); 00117 00118 } else if ($data = $mform->get_data()) { 00119 00120 $functioninfo = external_function_info($function); 00121 00122 // first load lib of selected protocol 00123 require_once("$CFG->dirroot/webservice/$protocol/locallib.php"); 00124 00125 $testclientclass = "webservice_{$protocol}_test_client"; 00126 if (!class_exists($testclientclass)) { 00127 throw new coding_exception('Missing WS test class in protocol '.$protocol); 00128 } 00129 $testclient = new $testclientclass(); 00130 00131 $serverurl = "$CFG->wwwroot/webservice/$protocol/"; 00132 if ($authmethod == 'simple') { 00133 $serverurl .= 'simpleserver.php'; 00134 $serverurl .= '?wsusername='.urlencode($data->wsusername); 00135 $serverurl .= '&wspassword='.urlencode($data->wspassword); 00136 } else if ($authmethod == 'token') { 00137 $serverurl .= 'server.php'; 00138 $serverurl .= '?wstoken='.urlencode($data->token); 00139 } 00140 00141 // now get the function parameters 00142 $params = $mform->get_params(); 00143 00144 // now test the parameters, this also fixes PHP data types 00145 $params = external_api::validate_parameters($functioninfo->parameters_desc, $params); 00146 00147 echo $OUTPUT->header(); 00148 echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function); 00149 00150 echo 'URL: '.s($serverurl); 00151 echo $OUTPUT->box_start(); 00152 00153 try { 00154 $response = $testclient->simpletest($serverurl, $function, $params); 00155 echo str_replace("\n", '<br />', s(var_export($response, true))); 00156 } catch (Exception $ex) { 00157 //TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces! 00158 echo str_replace("\n", '<br />', s($ex)); 00159 } 00160 00161 echo $OUTPUT->box_end(); 00162 $mform->display(); 00163 echo $OUTPUT->footer(); 00164 die; 00165 00166 } else { 00167 echo $OUTPUT->header(); 00168 echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function); 00169 $mform->display(); 00170 echo $OUTPUT->footer(); 00171 die; 00172 }