[1698] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[26461] | 5 | // | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | |
---|
[2297] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
[1698] | 23 | |
---|
| 24 | /**** WEB SERVICE CORE CLASSES************************************************ |
---|
| 25 | * PwgServer - main object - the link between web service methods, request |
---|
| 26 | * handler and response encoder |
---|
| 27 | * PwgRequestHandler - base class for handlers |
---|
| 28 | * PwgResponseEncoder - base class for response encoders |
---|
| 29 | * PwgError, PwgNamedArray, PwgNamedStruct - can be used by web service functions |
---|
| 30 | * as return values |
---|
| 31 | */ |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | define( 'WS_PARAM_ACCEPT_ARRAY', 0x010000 ); |
---|
| 35 | define( 'WS_PARAM_FORCE_ARRAY', 0x030000 ); |
---|
| 36 | define( 'WS_PARAM_OPTIONAL', 0x040000 ); |
---|
| 37 | |
---|
[25077] | 38 | define( 'WS_TYPE_BOOL', 0x01 ); |
---|
| 39 | define( 'WS_TYPE_INT', 0x02 ); |
---|
| 40 | define( 'WS_TYPE_FLOAT', 0x04 ); |
---|
| 41 | define( 'WS_TYPE_POSITIVE', 0x10 ); |
---|
| 42 | define( 'WS_TYPE_NOTNULL', 0x20 ); |
---|
| 43 | define( 'WS_TYPE_ID', WS_TYPE_INT | WS_TYPE_POSITIVE | WS_TYPE_NOTNULL); |
---|
| 44 | |
---|
[2543] | 45 | define( 'WS_ERR_INVALID_METHOD', 501 ); |
---|
[1698] | 46 | define( 'WS_ERR_MISSING_PARAM', 1002 ); |
---|
| 47 | define( 'WS_ERR_INVALID_PARAM', 1003 ); |
---|
| 48 | |
---|
| 49 | define( 'WS_XML_ATTRIBUTES', 'attributes_xml_'); |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * PwgError object can be returned from any web service function implementation. |
---|
| 53 | */ |
---|
| 54 | class PwgError |
---|
| 55 | { |
---|
[2543] | 56 | private $_code; |
---|
| 57 | private $_codeText; |
---|
[1698] | 58 | |
---|
| 59 | function PwgError($code, $codeText) |
---|
| 60 | { |
---|
[2543] | 61 | if ($code>=400 and $code<600) |
---|
| 62 | { |
---|
| 63 | set_status_header($code, $codeText); |
---|
| 64 | } |
---|
[2499] | 65 | |
---|
[1698] | 66 | $this->_code = $code; |
---|
| 67 | $this->_codeText = $codeText; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | function code() { return $this->_code; } |
---|
| 71 | function message() { return $this->_codeText; } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Simple wrapper around an array (keys are consecutive integers starting at 0). |
---|
| 76 | * Provides naming clues for xml output (xml attributes vs. xml child elements?) |
---|
| 77 | * Usually returned by web service function implementation. |
---|
| 78 | */ |
---|
| 79 | class PwgNamedArray |
---|
| 80 | { |
---|
| 81 | /*private*/ var $_content; |
---|
| 82 | /*private*/ var $_itemName; |
---|
| 83 | /*private*/ var $_xmlAttributes; |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Constructs a named array |
---|
| 87 | * @param arr array (keys must be consecutive integers starting at 0) |
---|
| 88 | * @param itemName string xml element name for values of arr (e.g. image) |
---|
| 89 | * @param xmlAttributes array of sub-item attributes that will be encoded as |
---|
| 90 | * xml attributes instead of xml child elements |
---|
| 91 | */ |
---|
[2543] | 92 | function PwgNamedArray($arr, $itemName, $xmlAttributes=array() ) |
---|
[1698] | 93 | { |
---|
| 94 | $this->_content = $arr; |
---|
| 95 | $this->_itemName = $itemName; |
---|
| 96 | $this->_xmlAttributes = array_flip($xmlAttributes); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | /** |
---|
| 100 | * Simple wrapper around a "struct" (php array whose keys are not consecutive |
---|
| 101 | * integers starting at 0). Provides naming clues for xml output (what is xml |
---|
| 102 | * attributes and what is element) |
---|
| 103 | */ |
---|
| 104 | class PwgNamedStruct |
---|
| 105 | { |
---|
| 106 | /*private*/ var $_content; |
---|
| 107 | /*private*/ var $_xmlAttributes; |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * Constructs a named struct (usually returned by web service function |
---|
| 111 | * implementation) |
---|
| 112 | * @param name string - containing xml element name |
---|
| 113 | * @param content array - the actual content (php array) |
---|
| 114 | * @param xmlAttributes array - name of the keys in $content that will be |
---|
| 115 | * encoded as xml attributes (if null - automatically prefer xml attributes |
---|
| 116 | * whenever possible) |
---|
| 117 | */ |
---|
[22729] | 118 | function PwgNamedStruct($content, $xmlAttributes=null, $xmlElements=null ) |
---|
[1698] | 119 | { |
---|
| 120 | $this->_content = $content; |
---|
| 121 | if ( isset($xmlAttributes) ) |
---|
| 122 | { |
---|
| 123 | $this->_xmlAttributes = array_flip($xmlAttributes); |
---|
| 124 | } |
---|
| 125 | else |
---|
| 126 | { |
---|
| 127 | $this->_xmlAttributes = array(); |
---|
| 128 | foreach ($this->_content as $key=>$value) |
---|
| 129 | { |
---|
| 130 | if (!empty($key) and (is_scalar($value) or is_null($value)) ) |
---|
| 131 | { |
---|
| 132 | if ( empty($xmlElements) or !in_array($key,$xmlElements) ) |
---|
| 133 | { |
---|
| 134 | $this->_xmlAttributes[$key]=1; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * Abstract base class for request handlers. |
---|
| 145 | */ |
---|
[25025] | 146 | abstract class PwgRequestHandler |
---|
[1698] | 147 | { |
---|
| 148 | /** Virtual abstract method. Decodes the request (GET or POST) handles the |
---|
| 149 | * method invocation as well as response sending. |
---|
| 150 | */ |
---|
[25025] | 151 | abstract function handleRequest(&$service); |
---|
[1698] | 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * |
---|
| 156 | * Base class for web service response encoder. |
---|
| 157 | */ |
---|
[25025] | 158 | abstract class PwgResponseEncoder |
---|
[1698] | 159 | { |
---|
| 160 | /** encodes the web service response to the appropriate output format |
---|
| 161 | * @param response mixed the unencoded result of a service method call |
---|
| 162 | */ |
---|
[25025] | 163 | abstract function encodeResponse($response); |
---|
[1698] | 164 | |
---|
| 165 | /** default "Content-Type" http header for this kind of response format |
---|
| 166 | */ |
---|
[25025] | 167 | abstract function getContentType(); |
---|
[1698] | 168 | |
---|
| 169 | /** |
---|
| 170 | * returns true if the parameter is a 'struct' (php array type whose keys are |
---|
| 171 | * NOT consecutive integers starting with 0) |
---|
| 172 | */ |
---|
[2543] | 173 | static function is_struct(&$data) |
---|
[1698] | 174 | { |
---|
| 175 | if (is_array($data) ) |
---|
| 176 | { |
---|
| 177 | if (range(0, count($data) - 1) !== array_keys($data) ) |
---|
| 178 | { # string keys, unordered, non-incremental keys, .. - whatever, make object |
---|
| 179 | return true; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | return false; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | /** |
---|
| 186 | * removes all XML formatting from $response (named array, named structs, etc) |
---|
| 187 | * usually called by every response encoder, except rest xml. |
---|
| 188 | */ |
---|
[22729] | 189 | static function flattenResponse(&$value) |
---|
[1698] | 190 | { |
---|
[22729] | 191 | self::flatten($value); |
---|
[1698] | 192 | } |
---|
| 193 | |
---|
[22729] | 194 | private static function flatten(&$value) |
---|
[1698] | 195 | { |
---|
[22729] | 196 | if (is_object($value)) |
---|
[1698] | 197 | { |
---|
[22729] | 198 | $class = strtolower( @get_class($value) ); |
---|
| 199 | if ($class == 'pwgnamedarray') |
---|
| 200 | { |
---|
| 201 | $value = $value->_content; |
---|
| 202 | } |
---|
| 203 | if ($class == 'pwgnamedstruct') |
---|
| 204 | { |
---|
| 205 | $value = $value->_content; |
---|
| 206 | } |
---|
[1698] | 207 | } |
---|
| 208 | |
---|
[22729] | 209 | if (!is_array($value)) |
---|
[1698] | 210 | return; |
---|
| 211 | |
---|
[22729] | 212 | if (self::is_struct($value)) |
---|
[1698] | 213 | { |
---|
| 214 | if ( isset($value[WS_XML_ATTRIBUTES]) ) |
---|
| 215 | { |
---|
| 216 | $value = array_merge( $value, $value[WS_XML_ATTRIBUTES] ); |
---|
| 217 | unset( $value[WS_XML_ATTRIBUTES] ); |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
[22729] | 221 | foreach ($value as $key=>&$v) |
---|
[1698] | 222 | { |
---|
[22729] | 223 | self::flatten($v); |
---|
[1698] | 224 | } |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | class PwgServer |
---|
| 231 | { |
---|
| 232 | var $_requestHandler; |
---|
| 233 | var $_requestFormat; |
---|
| 234 | var $_responseEncoder; |
---|
| 235 | var $_responseFormat; |
---|
| 236 | |
---|
[2478] | 237 | var $_methods = array(); |
---|
[1698] | 238 | |
---|
| 239 | function PwgServer() |
---|
| 240 | { |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | /** |
---|
| 244 | * Initializes the request handler. |
---|
| 245 | */ |
---|
| 246 | function setHandler($requestFormat, &$requestHandler) |
---|
| 247 | { |
---|
| 248 | $this->_requestHandler = &$requestHandler; |
---|
| 249 | $this->_requestFormat = $requestFormat; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | /** |
---|
| 253 | * Initializes the request handler. |
---|
| 254 | */ |
---|
| 255 | function setEncoder($responseFormat, &$encoder) |
---|
| 256 | { |
---|
| 257 | $this->_responseEncoder = &$encoder; |
---|
| 258 | $this->_responseFormat = $responseFormat; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /** |
---|
| 262 | * Runs the web service call (handler and response encoder should have been |
---|
| 263 | * created) |
---|
| 264 | */ |
---|
| 265 | function run() |
---|
| 266 | { |
---|
| 267 | if ( is_null($this->_responseEncoder) ) |
---|
| 268 | { |
---|
[2521] | 269 | set_status_header(400); |
---|
[1698] | 270 | @header("Content-Type: text/plain"); |
---|
| 271 | echo ("Cannot process your request. Unknown response format. |
---|
[2521] | 272 | Request format: ".@$this->_requestFormat." Response format: ".@$this->_responseFormat."\n"); |
---|
[1698] | 273 | var_export($this); |
---|
| 274 | die(0); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | if ( is_null($this->_requestHandler) ) |
---|
| 278 | { |
---|
[2543] | 279 | $this->sendResponse( new PwgError(400, 'Unknown request format') ); |
---|
[1698] | 280 | return; |
---|
| 281 | } |
---|
| 282 | |
---|
[25077] | 283 | // add reflection methods |
---|
| 284 | $this->addMethod( |
---|
| 285 | 'reflection.getMethodList', |
---|
| 286 | array('PwgServer', 'ws_getMethodList') |
---|
| 287 | ); |
---|
| 288 | $this->addMethod( |
---|
| 289 | 'reflection.getMethodDetails', |
---|
[1698] | 290 | array('PwgServer', 'ws_getMethodDetails'), |
---|
[25077] | 291 | array('methodName') |
---|
| 292 | ); |
---|
[1698] | 293 | |
---|
| 294 | trigger_action('ws_add_methods', array(&$this) ); |
---|
| 295 | uksort( $this->_methods, 'strnatcmp' ); |
---|
| 296 | $this->_requestHandler->handleRequest($this); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | * Encodes a response and sends it back to the browser. |
---|
| 301 | */ |
---|
| 302 | function sendResponse($response) |
---|
| 303 | { |
---|
| 304 | $encodedResponse = $this->_responseEncoder->encodeResponse($response); |
---|
| 305 | $contentType = $this->_responseEncoder->getContentType(); |
---|
| 306 | |
---|
[2585] | 307 | @header('Content-Type: '.$contentType.'; charset='.get_pwg_charset()); |
---|
[1698] | 308 | print_r($encodedResponse); |
---|
[9742] | 309 | trigger_action('sendResponse', $encodedResponse ); |
---|
[1698] | 310 | } |
---|
| 311 | |
---|
| 312 | /** |
---|
| 313 | * Registers a web service method. |
---|
| 314 | * @param methodName string - the name of the method as seen externally |
---|
| 315 | * @param callback mixed - php method to be invoked internally |
---|
[25077] | 316 | * @param params array - map of allowed parameter names with options |
---|
| 317 | * @option mixed default (optional) |
---|
| 318 | * @option int flags (optional) |
---|
| 319 | * possible values: WS_PARAM_ALLOW_ARRAY, WS_PARAM_FORCE_ARRAY, WS_PARAM_OPTIONAL |
---|
| 320 | * @option int type (optional) |
---|
| 321 | * possible values: WS_TYPE_BOOL, WS_TYPE_INT, WS_TYPE_FLOAT, WS_TYPE_ID |
---|
| 322 | * WS_TYPE_POSITIVE, WS_TYPE_NOTNULL |
---|
| 323 | * @option int|float maxValue (optional) |
---|
[1698] | 324 | * @param description string - a description of the method. |
---|
[23261] | 325 | * @param include_file string - a file to be included befaore the callback is executed |
---|
[25077] | 326 | * @param options array |
---|
[25115] | 327 | * @option bool hidden (optional) - if true, this method won't be visible by reflection.getMethodList |
---|
| 328 | * @option bool admin_only (optional) |
---|
| 329 | * @option bool post_only (optional) |
---|
[1698] | 330 | */ |
---|
[25077] | 331 | function addMethod($methodName, $callback, $params=array(), $description='', $include_file='', $options=array()) |
---|
[1698] | 332 | { |
---|
| 333 | if (!is_array($params)) |
---|
| 334 | { |
---|
| 335 | $params = array(); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | if ( range(0, count($params) - 1) === array_keys($params) ) |
---|
| 339 | { |
---|
| 340 | $params = array_flip($params); |
---|
| 341 | } |
---|
| 342 | |
---|
[23261] | 343 | foreach( $params as $param=>$data) |
---|
[1698] | 344 | { |
---|
[23261] | 345 | if ( !is_array($data) ) |
---|
[1698] | 346 | { |
---|
[25077] | 347 | $params[$param] = array('flags'=>0,'type'=>0); |
---|
[1698] | 348 | } |
---|
| 349 | else |
---|
| 350 | { |
---|
[25077] | 351 | if ( !isset($data['flags']) ) |
---|
| 352 | { |
---|
| 353 | $data['flags'] = 0; |
---|
| 354 | } |
---|
[23261] | 355 | if ( array_key_exists('default', $data) ) |
---|
[1698] | 356 | { |
---|
[25077] | 357 | $data['flags'] |= WS_PARAM_OPTIONAL; |
---|
[1698] | 358 | } |
---|
[25077] | 359 | if ( !isset($data['type']) ) |
---|
| 360 | { |
---|
| 361 | $data['type'] = 0; |
---|
| 362 | } |
---|
[23261] | 363 | $params[$param] = $data; |
---|
[1698] | 364 | } |
---|
| 365 | } |
---|
[2478] | 366 | |
---|
| 367 | $this->_methods[$methodName] = array( |
---|
| 368 | 'callback' => $callback, |
---|
| 369 | 'description' => $description, |
---|
| 370 | 'signature' => $params, |
---|
| 371 | 'include' => $include_file, |
---|
[23261] | 372 | 'options' => $options, |
---|
[2478] | 373 | ); |
---|
[1698] | 374 | } |
---|
| 375 | |
---|
| 376 | function hasMethod($methodName) |
---|
| 377 | { |
---|
| 378 | return isset($this->_methods[$methodName]); |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | function getMethodDescription($methodName) |
---|
| 382 | { |
---|
[2478] | 383 | $desc = @$this->_methods[$methodName]['description']; |
---|
[1698] | 384 | return isset($desc) ? $desc : ''; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | function getMethodSignature($methodName) |
---|
| 388 | { |
---|
[2478] | 389 | $signature = @$this->_methods[$methodName]['signature']; |
---|
[1698] | 390 | return isset($signature) ? $signature : array(); |
---|
| 391 | } |
---|
[25394] | 392 | |
---|
| 393 | /** |
---|
| 394 | * @since 2.6 |
---|
| 395 | */ |
---|
| 396 | function getMethodOptions($methodName) |
---|
| 397 | { |
---|
| 398 | $options = @$this->_methods[$methodName]['options']; |
---|
| 399 | return isset($options) ? $options : array(); |
---|
| 400 | } |
---|
[1698] | 401 | |
---|
[25115] | 402 | static function isPost() |
---|
[1698] | 403 | { |
---|
| 404 | return isset($HTTP_RAW_POST_DATA) or !empty($_POST); |
---|
| 405 | } |
---|
| 406 | |
---|
[25077] | 407 | static function makeArrayParam(&$param) |
---|
[1698] | 408 | { |
---|
| 409 | if ( $param==null ) |
---|
| 410 | { |
---|
| 411 | $param = array(); |
---|
| 412 | } |
---|
| 413 | else |
---|
| 414 | { |
---|
[25077] | 415 | if ( !is_array($param) ) |
---|
[1698] | 416 | { |
---|
| 417 | $param = array($param); |
---|
| 418 | } |
---|
| 419 | } |
---|
| 420 | } |
---|
[25077] | 421 | |
---|
| 422 | static function checkType(&$param, $type, $name) |
---|
| 423 | { |
---|
| 424 | $opts = array(); |
---|
| 425 | $msg = ''; |
---|
| 426 | if ( self::hasFlag($type, WS_TYPE_POSITIVE | WS_TYPE_NOTNULL) ) |
---|
| 427 | { |
---|
| 428 | $opts['options']['min_range'] = 1; |
---|
| 429 | $msg = ' positive and not null'; |
---|
| 430 | } |
---|
| 431 | else if ( self::hasFlag($type, WS_TYPE_POSITIVE) ) |
---|
| 432 | { |
---|
| 433 | $opts['options']['min_range'] = 0; |
---|
| 434 | $msg = ' positive'; |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | if ( is_array($param) ) |
---|
| 438 | { |
---|
| 439 | if ( self::hasFlag($type, WS_TYPE_BOOL) ) |
---|
| 440 | { |
---|
| 441 | foreach ($param as &$value) |
---|
| 442 | { |
---|
| 443 | if ( ($value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)) === null ) |
---|
| 444 | { |
---|
| 445 | return new PwgError(WS_ERR_INVALID_PARAM, $name.' must only contain booleans' ); |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | unset($value); |
---|
| 449 | } |
---|
| 450 | else if ( self::hasFlag($type, WS_TYPE_INT) ) |
---|
| 451 | { |
---|
| 452 | foreach ($param as &$value) |
---|
| 453 | { |
---|
| 454 | if ( ($value = filter_var($value, FILTER_VALIDATE_INT, $opts)) === false ) |
---|
| 455 | { |
---|
| 456 | return new PwgError(WS_ERR_INVALID_PARAM, $name.' must only contain'.$msg.' integers' ); |
---|
| 457 | } |
---|
| 458 | } |
---|
| 459 | unset($value); |
---|
| 460 | } |
---|
| 461 | else if ( self::hasFlag($type, WS_TYPE_FLOAT) ) |
---|
| 462 | { |
---|
| 463 | foreach ($param as &$value) |
---|
| 464 | { |
---|
| 465 | if ( |
---|
| 466 | ($value = filter_var($value, FILTER_VALIDATE_FLOAT)) === false |
---|
| 467 | or ( isset($opts['options']['min_range']) and $value < $opts['options']['min_range'] ) |
---|
| 468 | ) { |
---|
| 469 | return new PwgError(WS_ERR_INVALID_PARAM, $name.' must only contain'.$msg.' floats' ); |
---|
| 470 | } |
---|
| 471 | } |
---|
| 472 | unset($value); |
---|
| 473 | } |
---|
| 474 | } |
---|
| 475 | else if ( $param !== '' ) |
---|
| 476 | { |
---|
| 477 | if ( self::hasFlag($type, WS_TYPE_BOOL) ) |
---|
| 478 | { |
---|
| 479 | if ( ($param = filter_var($param, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)) === null ) |
---|
| 480 | { |
---|
| 481 | return new PwgError(WS_ERR_INVALID_PARAM, $name.' must be a boolean' ); |
---|
| 482 | } |
---|
| 483 | } |
---|
| 484 | else if ( self::hasFlag($type, WS_TYPE_INT) ) |
---|
| 485 | { |
---|
| 486 | if ( ($param = filter_var($param, FILTER_VALIDATE_INT, $opts)) === false ) |
---|
| 487 | { |
---|
| 488 | return new PwgError(WS_ERR_INVALID_PARAM, $name.' must be an'.$msg.' integer' ); |
---|
| 489 | } |
---|
| 490 | } |
---|
| 491 | else if ( self::hasFlag($type, WS_TYPE_FLOAT) ) |
---|
| 492 | { |
---|
| 493 | if ( |
---|
| 494 | ($param = filter_var($param, FILTER_VALIDATE_FLOAT)) === false |
---|
| 495 | or ( isset($opts['options']['min_range']) and $param < $opts['options']['min_range'] ) |
---|
| 496 | ) { |
---|
| 497 | return new PwgError(WS_ERR_INVALID_PARAM, $name.' must be a'.$msg.' float' ); |
---|
| 498 | } |
---|
| 499 | } |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | return null; |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | static function hasFlag($val, $flag) |
---|
| 506 | { |
---|
| 507 | return ($val & $flag) == $flag; |
---|
| 508 | } |
---|
[1698] | 509 | |
---|
| 510 | /** |
---|
| 511 | * Invokes a registered method. Returns the return of the method (or |
---|
| 512 | * a PwgError object if the method is not found) |
---|
| 513 | * @param methodName string the name of the method to invoke |
---|
| 514 | * @param params array array of parameters to pass to the invoked method |
---|
| 515 | */ |
---|
| 516 | function invoke($methodName, $params) |
---|
| 517 | { |
---|
[2478] | 518 | $method = @$this->_methods[$methodName]; |
---|
[1698] | 519 | |
---|
[25077] | 520 | if ( $method == null ) |
---|
[1698] | 521 | { |
---|
[6892] | 522 | return new PwgError(WS_ERR_INVALID_METHOD, 'Method name is not valid'); |
---|
[1698] | 523 | } |
---|
[25115] | 524 | |
---|
| 525 | if ( isset($method['options']['post_only']) and $method['options']['post_only'] and !self::isPost() ) |
---|
| 526 | { |
---|
| 527 | return new PwgError(405, 'This method requires HTTP POST'); |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | if ( isset($method['options']['admin_only']) and $method['options']['admin_only'] and !is_admin() ) |
---|
| 531 | { |
---|
| 532 | return new PwgError(401, 'Access denied'); |
---|
| 533 | } |
---|
[1698] | 534 | |
---|
[25077] | 535 | // parameter check and data correction |
---|
[2478] | 536 | $signature = $method['signature']; |
---|
[1698] | 537 | $missing_params = array(); |
---|
[25077] | 538 | |
---|
| 539 | foreach ($signature as $name => $options) |
---|
[1698] | 540 | { |
---|
| 541 | $flags = $options['flags']; |
---|
[25077] | 542 | |
---|
| 543 | // parameter not provided in the request |
---|
[1698] | 544 | if ( !array_key_exists($name, $params) ) |
---|
[25077] | 545 | { |
---|
| 546 | if ( !self::hasFlag($flags, WS_PARAM_OPTIONAL) ) |
---|
[1698] | 547 | { |
---|
| 548 | $missing_params[] = $name; |
---|
| 549 | } |
---|
[25077] | 550 | else if ( array_key_exists('default', $options) ) |
---|
[1698] | 551 | { |
---|
| 552 | $params[$name] = $options['default']; |
---|
[25077] | 553 | if ( self::hasFlag($flags, WS_PARAM_FORCE_ARRAY) ) |
---|
[1698] | 554 | { |
---|
[25077] | 555 | self::makeArrayParam($params[$name]); |
---|
[1698] | 556 | } |
---|
| 557 | } |
---|
| 558 | } |
---|
[25077] | 559 | // parameter provided but empty |
---|
| 560 | else if ( $params[$name]==='' and !self::hasFlag($flags, WS_PARAM_OPTIONAL) ) |
---|
| 561 | { |
---|
| 562 | $missing_params[] = $name; |
---|
| 563 | } |
---|
| 564 | // parameter provided - do some basic checks |
---|
[1698] | 565 | else |
---|
[25077] | 566 | { |
---|
[1698] | 567 | $the_param = $params[$name]; |
---|
[25077] | 568 | |
---|
| 569 | if ( is_array($the_param) and !self::hasFlag($flags, WS_PARAM_ACCEPT_ARRAY) ) |
---|
[1698] | 570 | { |
---|
| 571 | return new PwgError(WS_ERR_INVALID_PARAM, $name.' must be scalar' ); |
---|
| 572 | } |
---|
[25077] | 573 | |
---|
| 574 | if ( self::hasFlag($flags, WS_PARAM_FORCE_ARRAY) ) |
---|
[1698] | 575 | { |
---|
[25077] | 576 | self::makeArrayParam($the_param); |
---|
[1698] | 577 | } |
---|
[25077] | 578 | |
---|
| 579 | if ( $options['type'] > 0 ) |
---|
| 580 | { |
---|
| 581 | if ( ($ret = self::checkType($the_param, $options['type'], $name)) !== null ) |
---|
| 582 | { |
---|
| 583 | return $ret; |
---|
| 584 | } |
---|
| 585 | } |
---|
| 586 | |
---|
[1781] | 587 | if ( isset($options['maxValue']) and $the_param>$options['maxValue']) |
---|
| 588 | { |
---|
| 589 | $the_param = $options['maxValue']; |
---|
| 590 | } |
---|
[25077] | 591 | |
---|
[1698] | 592 | $params[$name] = $the_param; |
---|
| 593 | } |
---|
| 594 | } |
---|
[25077] | 595 | |
---|
[1698] | 596 | if (count($missing_params)) |
---|
| 597 | { |
---|
| 598 | return new PwgError(WS_ERR_MISSING_PARAM, 'Missing parameters: '.implode(',',$missing_params)); |
---|
| 599 | } |
---|
[25077] | 600 | |
---|
[1768] | 601 | $result = trigger_event('ws_invoke_allowed', true, $methodName, $params); |
---|
[4427] | 602 | if ( strtolower( @get_class($result) )!='pwgerror') |
---|
[1768] | 603 | { |
---|
[2478] | 604 | if ( !empty($method['include']) ) |
---|
| 605 | { |
---|
| 606 | include_once( $method['include'] ); |
---|
| 607 | } |
---|
| 608 | $result = call_user_func_array($method['callback'], array($params, &$this) ); |
---|
[1768] | 609 | } |
---|
[25077] | 610 | |
---|
[1698] | 611 | return $result; |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | /** |
---|
| 615 | * WS reflection method implementation: lists all available methods |
---|
| 616 | */ |
---|
[2543] | 617 | static function ws_getMethodList($params, &$service) |
---|
[1698] | 618 | { |
---|
[23261] | 619 | $methods = array_filter($service->_methods, |
---|
| 620 | create_function('$m', 'return empty($m["options"]["hidden"]) || !$m["options"]["hidden"];')); |
---|
| 621 | return array('methods' => new PwgNamedArray( array_keys($methods),'method' ) ); |
---|
[1698] | 622 | } |
---|
| 623 | |
---|
| 624 | /** |
---|
| 625 | * WS reflection method implementation: gets information about a given method |
---|
| 626 | */ |
---|
[2543] | 627 | static function ws_getMethodDetails($params, &$service) |
---|
[1698] | 628 | { |
---|
| 629 | $methodName = $params['methodName']; |
---|
[25077] | 630 | |
---|
[1698] | 631 | if (!$service->hasMethod($methodName)) |
---|
| 632 | { |
---|
[25077] | 633 | return new PwgError(WS_ERR_INVALID_PARAM, 'Requested method does not exist'); |
---|
[1698] | 634 | } |
---|
[25077] | 635 | |
---|
[1698] | 636 | $res = array( |
---|
| 637 | 'name' => $methodName, |
---|
| 638 | 'description' => $service->getMethodDescription($methodName), |
---|
| 639 | 'params' => array(), |
---|
[25394] | 640 | 'options' => $service->getMethodOptions($methodName), |
---|
[1698] | 641 | ); |
---|
[25077] | 642 | |
---|
| 643 | foreach ($service->getMethodSignature($methodName) as $name => $options) |
---|
[1698] | 644 | { |
---|
| 645 | $param_data = array( |
---|
| 646 | 'name' => $name, |
---|
[25077] | 647 | 'optional' => self::hasFlag($options['flags'], WS_PARAM_OPTIONAL), |
---|
| 648 | 'acceptArray' => self::hasFlag($options['flags'], WS_PARAM_ACCEPT_ARRAY), |
---|
| 649 | 'type' => 'mixed', |
---|
[1698] | 650 | ); |
---|
[25077] | 651 | |
---|
[1698] | 652 | if (isset($options['default'])) |
---|
| 653 | { |
---|
| 654 | $param_data['defaultValue'] = $options['default']; |
---|
| 655 | } |
---|
[25394] | 656 | if (isset($options['maxValue'])) |
---|
| 657 | { |
---|
| 658 | $param_data['maxValue'] = $options['maxValue']; |
---|
| 659 | } |
---|
[20750] | 660 | if (isset($options['info'])) |
---|
| 661 | { |
---|
| 662 | $param_data['info'] = $options['info']; |
---|
| 663 | } |
---|
[25077] | 664 | |
---|
| 665 | if ( self::hasFlag($options['type'], WS_TYPE_BOOL) ) |
---|
| 666 | { |
---|
| 667 | $param_data['type'] = 'bool'; |
---|
| 668 | } |
---|
| 669 | else if ( self::hasFlag($options['type'], WS_TYPE_INT) ) |
---|
| 670 | { |
---|
| 671 | $param_data['type'] = 'int'; |
---|
| 672 | } |
---|
| 673 | else if ( self::hasFlag($options['type'], WS_TYPE_FLOAT) ) |
---|
| 674 | { |
---|
| 675 | $param_data['type'] = 'float'; |
---|
| 676 | } |
---|
| 677 | if ( self::hasFlag($options['type'], WS_TYPE_POSITIVE) ) |
---|
| 678 | { |
---|
| 679 | $param_data['type'].= ' positive'; |
---|
| 680 | } |
---|
| 681 | if ( self::hasFlag($options['type'], WS_TYPE_NOTNULL) ) |
---|
| 682 | { |
---|
| 683 | $param_data['type'].= ' notnull'; |
---|
| 684 | } |
---|
| 685 | |
---|
[1698] | 686 | $res['params'][] = $param_data; |
---|
| 687 | } |
---|
| 688 | return $res; |
---|
| 689 | } |
---|
| 690 | } |
---|
[9742] | 691 | ?> |
---|