forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
202 lines (181 loc) · 8.19 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* AMF web service implementation classes and methods.
*
* @package webservice
* @copyright 2009 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("$CFG->dirroot/webservice/lib.php");
require_once( "{$CFG->dirroot}/webservice/amf/introspector.php");
require_once 'Zend/Amf/Server.php';
/**
* Exception indicating an invalid return value from a function.
* Used when an externallib function does not return values of the expected structure.
*/
class invalid_return_value_exception extends moodle_exception {
/**
* Constructor
* @param string $debuginfo some detailed information
*/
function __construct($debuginfo=null) {
parent::__construct('invalidreturnvalue', 'webservice_amf', '', $debuginfo, $debuginfo);
}
}
/**
* AMF service server implementation.
* @author Petr Skoda (skodak)
*/
class webservice_amf_server extends webservice_zend_server {
/**
* Contructor
* @param integer $authmethod authentication method - one of WEBSERVICE_AUTHMETHOD_*
*/
public function __construct($authmethod) {
parent::__construct($authmethod, 'Moodle_Amf_Server');
$this->wsname = 'amf';
}
protected function init_service_class(){
parent::init_service_class();
//allow access to data about methods available.
$this->zend_server->setClass( "MethodDescriptor" );
MethodDescriptor::$classnametointrospect = $this->service_class;
}
protected function service_class_method_body($function, $params){
//cast the param from object to array (validate_parameters except array only)
$castingcode = '';
if ($params){
$paramstocast = explode(',', $params);
foreach ($paramstocast as $paramtocast) {
$paramtocast = trim($paramtocast);
$castingcode .= $paramtocast .
'=webservice_zend_server::cast_objects_to_array('.$paramtocast.');';
}
}
$externallibcall = $function->classname.'::'.$function->methodname.'('.$params.')';
$descriptionmethod = $function->methodname.'_returns()';
$callforreturnvaluedesc = $function->classname.'::'.$descriptionmethod;
return $castingcode .
' return webservice_amf_server::validate_and_cast_values('.$callforreturnvaluedesc.', '.$externallibcall.');';
}
/**
* Validates submitted value, comparing it to a description. If anything is incorrect
* invalid_return_value_exception is thrown. Also casts the values to the type specified in
* the description.
* @param mixed $description description of parameters or null if no return value
* @param mixed $value the actual values
* @param boolean $singleasobject specifies whether a external_single_structure should be cast to a stdClass object
* should always be false for use in validating parameters in externallib functions.
* @return mixed params with added defaults for optional items, invalid_parameters_exception thrown if any problem found
*/
public static function validate_and_cast_values($description, $value) {
if (is_null($description)){
return;
}
if ($description instanceof external_value) {
if (is_array($value) or is_object($value)) {
throw new invalid_return_value_exception('Scalar type expected, array or object received.');
}
if ($description->type == PARAM_BOOL) {
// special case for PARAM_BOOL - we want true/false instead of the usual 1/0 - we can not be too strict here ;-)
if (is_bool($value) or $value === 0 or $value === 1 or $value === '0' or $value === '1') {
return (bool)$value;
}
}
return validate_param($value, $description->type, $description->allownull, 'Invalid external api parameter');
} else if ($description instanceof external_single_structure) {
if (!is_array($value)) {
throw new invalid_return_value_exception('Only arrays accepted.');
}
$result = array();
foreach ($description->keys as $key=>$subdesc) {
if (!array_key_exists($key, $value)) {
if ($subdesc->required == VALUE_REQUIRED) {
throw new invalid_return_value_exception('Missing required key in single structure: '.$key);
}
if ($subdesc instanceof external_value) {
if ($subdesc->required == VALUE_DEFAULT) {
$result[$key] = self::validate_and_cast_values($subdesc, $subdesc->default);
}
}
} else {
$result[$key] = self::validate_and_cast_values($subdesc, $value[$key]);
}
unset($value[$key]);
}
/* Was decided that extra keys should just be ignored and not returned.
* if (!empty($value)) {
throw new invalid_return_value_exception('Unexpected keys detected in parameter array.');
}*/
return (object)$result;
} else if ($description instanceof external_multiple_structure) {
if (!is_array($value)) {
throw new invalid_return_value_exception('Only arrays accepted.');
}
$result = array();
foreach ($value as $param) {
$result[] = self::validate_and_cast_values($description->content, $param);
}
return $result;
} else {
throw new invalid_return_value_exception('Invalid external api description.');
}
}
/**
* Set up zend service class
* @return void
*/
protected function init_zend_server() {
parent::init_zend_server();
$this->zend_server->setProduction(false); //set to false for development mode
//(complete error message displayed into your AMF client)
// TODO: add some exception handling
}
}
class Moodle_Amf_Server extends Zend_Amf_Server{
/**
* Raise a server fault
*
* @param string|Exception $fault
* @return void
*/
public function fault($fault = null, $code = 404)
{
if (!$fault instanceof Exception) {
$fault = new Exception($fault);
}
$request = $this->getRequest();
// Get the object encoding of the request.
$objectEncoding = $request->getObjectEncoding();
// create a response object to place the output from the services.
$response = $this->getResponse();
// set reponse encoding
$response->setObjectEncoding($objectEncoding);
$responseBody = $request->getAmfBodies();
foreach($responseBody as $body){
$return = $this->_errorMessage($objectEncoding, $fault->getMessage(),
$fault->getMessage(), $fault->getTraceAsString(),$fault->getCode(), $fault->getLine());
$responseType = Zend_AMF_Constants::STATUS_METHOD;
$responseURI = $body->getResponseURI() . $responseType;
$newBody = new Zend_Amf_Value_MessageBody($responseURI, null, $return);
$response->addAmfBody($newBody);
}
$response->finalize();
echo $response;
}
}
// TODO: implement AMF test client somehow, maybe we could use moodle form to feed the data to the flash app somehow