From 13036898c71a432cc699f64e1270fd6a216e8c04 Mon Sep 17 00:00:00 2001 From: jerome Date: Wed, 25 Feb 2009 00:50:47 +0000 Subject: [PATCH] web service MDL-12886 fix empty optional description for REST server --- webservice/rest/locallib.php | 144 ++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 71 deletions(-) diff --git a/webservice/rest/locallib.php b/webservice/rest/locallib.php index a19ff3aeb7..27125aa7f1 100644 --- a/webservice/rest/locallib.php +++ b/webservice/rest/locallib.php @@ -38,18 +38,18 @@ */ function call_moodle_function ($rest_arguments) { global $CFG, $USER; - -///REST params conversion + + ///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 + //$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 + /// 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 != 'tmp_get_token') { @@ -74,14 +74,14 @@ function call_moodle_function ($rest_arguments) { } } -/// load the external class + /// 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 + /// This following line is only REST protocol $params = retrieve_params ($description[$functionname]); //retrieve the REST params -/// Generic part to any protocols + /// Generic part to any protocols if ($params === false) { //return an error message, the REST params doesn't match with the web service description } @@ -91,11 +91,11 @@ function call_moodle_function ($rest_arguments) { } catch (moodle_exception $e) { return "".$e->getMessage().""; } - -///Transform result into xml in order to send the REST response + + ///Transform result into xml in order to send the REST response $return = mdl_conn_rest_object_to_xml ($res,key($description[$functionname]['return'])); - return "$return"; + return "$return"; } /** @@ -114,7 +114,7 @@ function mock_check_token($token) { if (empty($user)) { return false; } - + return $user; } else { return false; @@ -126,28 +126,28 @@ function mock_check_token($token) { * @param integer $param * @return string */ - function convert_paramtype($param) { - switch ($param) { - case "integer": - return PARAM_NUMBER; - break; - case "integer": - return PARAM_INT; - break; - case "boolean": - return PARAM_BOOL; - break; - case "string": - return PARAM_ALPHANUM; - break; - case "object": - return PARAM_RAW; - break; - default: - return PARAM_RAW; - break; - } +function convert_paramtype($param) { + switch ($param) { + case "integer": + return PARAM_NUMBER; + break; + case "integer": + return PARAM_INT; + break; + case "boolean": + return PARAM_BOOL; + break; + case "string": + return PARAM_ALPHANUM; + break; + case "object": + return PARAM_RAW; + break; + default: + return PARAM_RAW; + break; } +} /** * @@ -162,15 +162,17 @@ function retrieve_params ($description) { $paramtype = convert_paramtype($paramtype); $value = optional_param($paramname,null,$paramtype); if (!empty($value)) { - $params[$paramname] = $value; + $params[$paramname] = $value; } } //retrieve REST optional params - foreach ($description['optional'] as $paramname => $paramtype) { - $paramtype = convert_paramtype($paramtype); - $value = optional_param($paramname,null,$paramtype); - if (!empty($value)) { + if (!empty($description['optional'])) { + foreach ($description['optional'] as $paramname => $paramtype) { + $paramtype = convert_paramtype($paramtype); + $value = optional_param($paramname,null,$paramtype); + if (!empty($value)) { $params[$paramname] = $value; + } } } return $params; @@ -185,39 +187,39 @@ function retrieve_params ($description) { * @return string */ function mdl_conn_rest_object_to_xml ($obj, $tag,$atts=false) { - $res = ''; - $tag_atts = ''; - if ($atts) { - $main_atts = array(); - foreach ($atts as $att=>$val) { - $main_atts[] = "$att=\"".urlencode($val)."\""; - } - if (count($main_atts)) $tag_atts = ' '.implode(' ',$main_atts); - } - - //if is an object - if (is_object($obj)) { - $parts = get_object_vars($obj); - foreach ($parts as $tag2 => $val) { - $res.= mdl_conn_rest_object_to_xml ($val, $tag2); - } - return "<$tag$tag_atts>\n$res\n"; - } - //if it's an array all elements will be inside te same tag but with a new atribute key - if (is_array($obj)){ - if (!$atts) $atts = array(); - //we came from another array - if (isset($atts['keys'])) $atts = array(); - foreach ($obj as $key=>$val) { - $array_atts = $atts; - $array_atts['key'] = $key; - $res.= mdl_conn_rest_object_to_xml ($val, $tag,$array_atts); - } - return $res; - } - //any other type, just encapsule it - $obj = htmlentities($obj); - return "<$tag$tag_atts>$obj\n"; + $res = ''; + $tag_atts = ''; + if ($atts) { + $main_atts = array(); + foreach ($atts as $att=>$val) { + $main_atts[] = "$att=\"".urlencode($val)."\""; + } + if (count($main_atts)) $tag_atts = ' '.implode(' ',$main_atts); + } + + //if is an object + if (is_object($obj)) { + $parts = get_object_vars($obj); + foreach ($parts as $tag2 => $val) { + $res.= mdl_conn_rest_object_to_xml ($val, $tag2); + } + return "<$tag$tag_atts>\n$res\n"; + } + //if it's an array all elements will be inside te same tag but with a new atribute key + if (is_array($obj)){ + if (!$atts) $atts = array(); + //we came from another array + if (isset($atts['keys'])) $atts = array(); + foreach ($obj as $key=>$val) { + $array_atts = $atts; + $array_atts['key'] = $key; + $res.= mdl_conn_rest_object_to_xml ($val, $tag,$array_atts); + } + return $res; + } + //any other type, just encapsule it + $obj = htmlentities($obj); + return "<$tag$tag_atts>$obj\n"; } -- 2.39.5