aboutsummaryrefslogtreecommitdiff
path: root/vendor/studip_ws/xmlrpc_dispatcher.php
blob: 4db5175674b4576d3fe7aef25249c62362d02ce1 (plain)
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
<?php

/*
 * xmlrpc_dispatcher.php - <short-description>
 *
 * Copyright (C) 2006 - Marcus Lunzenauer <mlunzena@uos.de>
 *
 * This program 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 2 of
 * the License, or (at your option) any later version.
 */


/**
 * <ClassDescription>
 *
 * @package     studip
 * @subpackage  ws
 *
 * @author    mlunzena
 * @copyright (c) Authors
 * @version   $Id: xmlrpc_dispatcher.php 3888 2006-09-06 13:27:19Z mlunzena $
 */

class Studip_Ws_XmlrpcDispatcher extends Studip_Ws_Dispatcher {


  /**
   * <MethodDescription>
   *
   * @param string <description>
   *
   * @return mixed <description>
   */
  function dispatch($msg = NULL) {

    # ensure correct invocation
    if (is_null($msg) || !is_a($msg, 'PhpXmlrpc\Request'))
      return $this->throw_exception('functions_parameters_type must not be '.
                                    'phpvals.');

    $encoder = new PhpXmlRpc\Encoder();

    # get decoded parameters
    $len = $msg->getNumParams();
    $argument_array = array();
    for ($i = 0; $i < $len; ++$i)
      $argument_array[] = $encoder->decode($msg->getParam($i));

    # return result
    return new PhpXmlRpc\Response(
      $encoder->encode($this->invoke($msg->method(), $argument_array)));
  }


  /**
   * <MethodDescription>
   *
   * @param string <description>
   *
   * @return mixed <description>
   */
  function throw_exception($message/*, ...*/) {
    $args = func_get_args();
    return new PhpXmlRpc\Response(0, $GLOBALS['xmlrpcerruser'] + 1,
                          vsprintf(array_shift($args), $args));
  }


  /**
   * Class method that composes the dispatch map from the available methods.
   *
   * @return array This service's dispatch map.
   *
   */
  function get_dispatch_map() {
    $dispatch_map = array();
    foreach ($this->api_methods as $method_name => $method)
      if ($mapped = $this->map_method($method)) {
        $dispatch_map[$method_name] = $mapped;
      }
    return $dispatch_map;
  }


  /**
   * <MethodDescription>
   *
   * @param mixed <description>
   *
   * @return array <description>
   */
  function map_method($method) {

    # TODO validate method
    try {
      $reflection = new ReflectionMethod($method->service, "{$method->name}_action");
      $parameters = $reflection->getParameters();
    } catch (Exception $e) {
      return false;
    }

    ## 1. function
    $function = array(&$this, 'dispatch');

    ## 2. signature
    $signature      = [[]];
    $signature_docs = [['']];

    # return value
    $signature[0][] = $this->translate_type($method->returns);

    # arguments
    foreach ($method->expects as $index => $type) {
        $signature[0][]      = $this->translate_type($type);
        $signature_docs[0][] = isset($parameters[$index]) ? $parameters[$index]->getName() : '';
    }

    ## 3. docstring
    $docstring = $method->description;

    return compact('function', 'signature', 'docstring', 'signature_docs');
  }


  /**
   * <MethodDescription>
   *
   * @param mixed <description>
   *
   * @return mixed <description>
   */
  function translate_type($type0) {
    switch ($type = Studip_Ws_Type::get_type($type0)) {
      case STUDIP_WS_TYPE_INT:
        return PhpXmlRpc\Value::$xmlrpcInt;

      case STUDIP_WS_TYPE_STRING:
        return PhpXmlRpc\Value::$xmlrpcString;

      case STUDIP_WS_TYPE_BASE64:
        return PhpXmlRpc\Value::$xmlrpcBase64;

      case STUDIP_WS_TYPE_BOOL:
        return PhpXmlRpc\Value::$xmlrpcBoolean;

      case STUDIP_WS_TYPE_FLOAT:
        return PhpXmlRpc\Value::$xmlrpcDouble;

      case STUDIP_WS_TYPE_NULL:
        return PhpXmlRpc\Value::$xmlrpcNull;

      case STUDIP_WS_TYPE_ARRAY:
        return PhpXmlRpc\Value::$xmlrpcArray;

      case STUDIP_WS_TYPE_STRUCT:
        return PhpXmlRpc\Value::$xmlrpcStruct;
    }

    trigger_error(sprintf('Type %s could not be found.',
                          var_export($type, TRUE)),
                  E_USER_ERROR);
    return PhpXmlRpc\Value::$xmlrpcString;
  }
}