/** * 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; }
/** * * @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>"; }