public function run() { $enable = $this->get_enable(); if (empty($enable)) { die; } include "Zend/Loader.php"; Zend_Loader::registerAutoload(); Zend_XmlRpc_Server_Fault::attachFaultException('moodle_exception'); // retrieve the token from the url // if the token doesn't exist, set a class containing only get_token() $token = optional_param('token', null, PARAM_ALPHANUM); if (empty($token)) { $server = new Zend_XmlRpc_Server(); $server->setClass("ws_authentication", "authentication"); echo $server->handle(); } else { // if token exist, do the authentication here /// TODO: following function will need to be modified $user = webservice_lib::mock_check_token($token); if (empty($user)) { throw new moodle_exception('wrongidentification'); } else { /// TODO: probably change this global $USER; $USER = $user; } //retrieve the api name $classpath = optional_param(classpath, null, PARAM_ALPHA); require_once dirname(__FILE__) . '/../../' . $classpath . '/external.php'; /// run the server $server = new Zend_XmlRpc_Server(); $server->setClass($classpath . "_external", $classpath); echo $server->handle(); } }
/** * Generate web service function list * @global object $CFG */ function generate_functionlist() { global $CFG; $documentation = "<H2>" . get_string('functionlist', 'webservice') . "</H2>"; //retrieve all external file $externalfiles = array(); $externalfunctions = array(); setListApiFiles($externalfiles, $CFG->dirroot); foreach ($externalfiles as $file) { require $file; $classpath = substr($file, strlen($CFG->dirroot) + 1); //remove the dir root + / from the file path $classpath = substr($classpath, 0, strlen($classpath) - 13); //remove /external.php from the classpath $classpath = str_replace('/', '_', $classpath); //convert all / into _ $classname = $classpath . "_external"; $api = new $classname(); $documentation .= "<H3><u>" . get_string('moodlepath', 'webservice') . ": " . $classpath . "</u></H3><ul>"; $description = webservice_lib::generate_webservice_description($file, $classname); foreach ($description as $functionname => $functiondescription) { $documentation .= <<<EOF <li><b>{$functionname}(</b> EOF; $arrayparams = array(); $comma = ""; foreach ($functiondescription['params'] as $param => $type) { $documentation .= <<<EOF {$comma} {$type} {$param} EOF; if (empty($comma)) { $comma = ','; } } $documentation .= <<<EOF <b>)</b> : EOF; if (array_key_exists('return', $functiondescription)) { foreach ($functiondescription['return'] as $return => $type) { $documentation .= <<<EOF <i> {$type}</i> EOF; if (is_array($type)) { $arraytype = "<pre>" . print_r($type, true) . "</pre>"; $documentation .= <<<EOF <i> {$return} {$arraytype} <br><br></i> EOF; } } } foreach ($functiondescription['params'] as $param => $type) { if (is_array($type)) { $arraytype = "<pre>" . print_r($type, true) . "</pre>"; $documentation .= <<<EOF <u>{$param}</u> : {$arraytype} <br> EOF; } } } $documentation .= <<<EOF </ul> EOF; } echo $documentation; }
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com * @license http://www.gnu.org/copyleft/gpl.html GNU GPL License */ /* * Zend Soap sclient */ require_once '../../../config.php'; require_once '../lib.php'; include "Zend/Loader.php"; Zend_Loader::registerAutoload(); ///Display Moodle page header print_header('Soap test client', 'Soap test client' . ":", true); /// check that webservices are enable into your Moodle /// WARNING: it makes sens here only because this client runs on the same machine as the /// server, if you change the WSDL url, please comment the following if statement if (!webservice_lib::display_webservices_availability("soap")) { echo "<br/><br/>"; echo "Please fix the previous problem(s), the testing session has been interupted."; print_footer(); exit; } /// authenticate => get a conversation token from the server /// You need a wsuser/wspassword user in the remote Moodle $client = new Zend_Soap_Client($CFG->wwwroot . "/webservice/soap/server.php?wsdl"); try { $token = $client->get_token(array('username' => "wsuser", 'password' => "wspassword")); print "<pre>\n"; print "<br><br><strong>Token: </strong>" . $token; print "</pre>"; } catch (exception $exception) { print "<br><br><strong>An exception occured during authentication: \n</strong>";
/** * Builds XHTML to display the control * * @param string $data * @param string $query * @return string XHTML */ public function output_html($data, $query = '') { global $CFG, $OUTPUT; $namestr = get_string('name'); $settingsstr = get_string('settings'); $hiddenstr = get_string('hiddenshow', 'repository'); require_once "../webservice/lib.php"; $protocols = webservice_lib::get_list_protocols(); $table = new html_table(); $table->head = array($namestr, $hiddenstr, $settingsstr); $table->align = array('left', 'center', 'center'); $table->data = array(); foreach ($protocols as $i) { $hidetitle = $i->get_protocolid() ? get_string('clicktohide', 'repository') : get_string('clicktoshow', 'repository'); $hiddenshow = ' <a href="' . $this->baseurl . '&hide=' . $i->get_protocolid() . '">' . '<img src="' . $OUTPUT->old_icon_url('i/' . ($i->get_enable() ? 'hide' : 'show')) . '"' . ' alt="' . $hidetitle . '" ' . ' title="' . $hidetitle . '" />' . '</a>' . "\n"; $settingnames = $i->get_setting_names(); if (!empty($settingnames)) { $settingsshow = ' <a href="' . $this->baseurl . '&settings=' . $i->get_protocolid() . '">' . $settingsstr . '</a>' . "\n"; } else { $settingsshow = ""; } $table->data[] = array($i->get_protocolname(), $hiddenshow, $settingsshow); //display a grey row if the type is defined as not visible if (!$i->get_enable()) { $table->rowclasses[] = 'dimmed_text'; } else { $table->rowclasses[] = ''; } } $output = $OUTPUT->table($table); return highlight($query, $output); }
/** * Check if the Moodle site has the web service protocol enable * @global object $CFG * @param string $protocol */ function display_webservices_availability($protocol) { global $CFG; $available = true; echo get_string('webservicesenable', 'webservice') . ": "; if (empty($CFG->enablewebservices)) { echo "<strong style=\"color:red\">" . get_string('fail', 'webservice') . "</strong>"; $available = false; } else { echo "<strong style=\"color:green\">" . get_string('ok', 'webservice') . "</strong>"; } echo "<br/>"; foreach (webservice_lib::get_list_protocols() as $wsprotocol) { if (strtolower($wsprotocol->get_protocolid()) == strtolower($protocol)) { echo get_string('protocolenable', 'webservice', array($wsprotocol->get_protocolid())) . ": "; if (get_config($wsprotocol->get_protocolid(), "enable")) { echo "<strong style=\"color:green\">" . get_string('ok', 'webservice') . "</strong>"; } else { echo "<strong style=\"color:red\">" . get_string('fail', 'webservice') . "</strong>"; $available = false; } echo "<br/>"; continue; } } //check debugging if ($CFG->debugdisplay) { echo "<strong style=\"color:red\">" . get_string('debugdisplayon', 'webservice') . "</strong>"; $available = false; } return $available; }
/** * * @author Jerome Mouneyrac * @global object $CFG * @param string $rest_arguments example: /mod/forum/get_discussion * @return string xml object */ function call_moodle_function($rest_arguments) { global $CFG, $USER; ///REST params conversion $functionname = substr($rest_arguments, strrpos($rest_arguments, "/") + 1); //retrieve the function name (it's located after the last '/') in $rest_arguments //$rest_argument $apipath = substr($rest_arguments, 0, strlen($rest_arguments) - strlen($functionname)); //api path is the other part of $rest_arguments $classname = str_replace('/', '_', $apipath); // convert '/' into '_' (e.g. /mod/forum/ => _mod_forum_) $classname = substr($classname, 1, strlen($classname) - 1); //remove first _ (e.g. _mod_forum => mod_forum) $classname .= 'external'; /// Authentication process /// TODO: this use a fake token => need to implement token generation $token = optional_param('token', null, PARAM_ALPHANUM); if (empty($token)) { if ($functionname != 'get_token') { throw new moodle_exception('identifyfirst'); } else { /// TODO: authentication + token generation need to be implemented if (optional_param('username', null, PARAM_ALPHANUM) == 'wsuser' && optional_param('password', null, PARAM_ALPHANUM) == 'wspassword') { return '456'; } else { throw new moodle_exception('wrongusernamepassword'); } } } else { /// TODO: following function will need to be modified $user = mock_check_token($token); if (empty($user)) { throw new moodle_exception('wrongidentification'); } else { /// TODO: probably change this $USER = $user; } } /// load the external class $file = $CFG->dirroot . $apipath . 'external.php'; $description = webservice_lib::generate_webservice_description($file, $classname); /// This following line is only REST protocol $params = retrieve_params($description[$functionname]); //retrieve the REST params /// Generic part to any protocols if ($params === false) { //return an error message, the REST params doesn't match with the web service description } try { $res = call_user_func_array($classname . '::' . $functionname, array($params)); } catch (moodle_exception $e) { return "<Result>" . $e->getMessage() . "</Result>"; } ///Transform result into xml in order to send the REST response $key = key($description[$functionname]['return']); if (strpos($key, ":") !== false) { $key = substr($key, strpos($key, ":") + 1); } else { $key = 'return'; } $return = mdl_conn_rest_object_to_xml($res, $key); return "<Result>{$return}</Result>"; }
/** * Run Zend SOAP server * @global <type> $CFG * @global <type> $USER */ public function run() { $enable = $this->get_enable(); if (empty($enable)) { die; } global $CFG; include "Zend/Loader.php"; Zend_Loader::registerAutoload(); // retrieve the token from the url // if the token doesn't exist, set a class containing only get_token() $token = optional_param('token', null, PARAM_ALPHANUM); ///this is a hack, because there is a bug in Zend framework (http://framework.zend.com/issues/browse/ZF-5736) if (empty($token)) { $relativepath = get_file_argument(); $args = explode('/', trim($relativepath, '/')); if (count($args) == 2) { $token = (int) $args[0]; $classpath = $args[1]; } } if (empty($token)) { if (isset($_GET['wsdl'])) { $autodiscover = new Zend_Soap_AutoDiscover(); /* $autodiscover->setComplexTypeStrategy('Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex'); $autodiscover->setOperationBodyStyle( array('use' => 'literal', 'namespace' => $CFG->wwwroot) ); $autodiscover->setBindingStyle( array('style' => 'rpc') ); */ $autodiscover->setClass('ws_authentication'); $autodiscover->handle(); } else { $soap = new Zend_Soap_Server($CFG->wwwroot . "/webservice/soap/server.php?wsdl"); // this current file here $soap->registerFaultException('moodle_exception'); $soap->setClass('ws_authentication'); $soap->handle(); } } else { // if token exist, do the authentication here /// TODO: following function will need to be modified $user = webservice_lib::mock_check_token($token); if (empty($user)) { throw new moodle_exception('wrongidentification'); } else { /// TODO: probably change this global $USER; $USER = $user; } //retrieve the api name if (empty($classpath)) { $classpath = optional_param('classpath', null, PARAM_ALPHANUM); } require_once dirname(__FILE__) . '/../../' . $classpath . '/external.php'; /// run the server if (isset($_GET['wsdl'])) { $autodiscover = new Zend_Soap_AutoDiscover(); //this is a hack, because there is a bug in Zend framework (http://framework.zend.com/issues/browse/ZF-5736) $autodiscover->setUri($CFG->wwwroot . "/webservice/soap/server.php/" . $token . "/" . $classpath); $autodiscover->setClass($classpath . "_external"); $autodiscover->handle(); } else { $soap = new Zend_Soap_Server($CFG->wwwroot . "/webservice/soap/server.php?token=" . $token . "&classpath=" . $classpath . "&wsdl"); // this current file here $soap->setClass($classpath . "_external"); $soap->registerFaultException('moodle_exception'); $soap->handle(); } } }