root/trunk/midgard/apis/soap/midgard-soap.php

Revision 7040, 4.9 kB (checked in by indeyets, 5 years ago)

comment left from another version, sorry

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2     require_once 'SOAP/Server.php';
3
4     define("MGD_SOAP_ERR_OK", 0);
5     define("MGD_SOAP_ERR_ERROR", -10);
6     define("MGD_SOAP_ERR_OBJ_NOEXISTS", -5);
7     define("MGD_SOAP_ERR_FUNCTION_NOEXISTS", -11);
8     define("MGD_SOAP_ERR_METHOD_NOEXISTS", -12);
9     define("MGD_SOAP_ERR_NO_OBJ", -13);
10     define("MGD_SOAP_ERR_NO_TYPE", -14);
11     define("MGD_SOAP_ERR_NO_VALUE", -15);
12
13 class MidgardResultSOAP
14 {
15     var $value;
16     var $type;
17     var $_errno;
18     var $_errstr;
19
20     function MidgardResultSOAP($value = NULL, $errno = NULL)
21     {
22         $this->value = $value ? $value : "";
23         $this->type = is_object($value) ? get_class($value) : get_class($this);
24
25         if ($errno) {
26             $this->_errno = $errno;
27             $this->_errstr = $this->errstr();
28         } else {
29             $this->_errno = mgd_errno();
30             $this->_errstr = mgd_errstr($this->_errno);
31         }
32     }
33
34     function to_soap()
35     {
36         return new SOAP_Value("Midgard", "{MidgardSOAP}"."MidgardResultSOAP", $this);
37     }
38
39     function errno()
40     {
41         return $this->_errno;
42     }
43
44     function errstr()
45     {
46         switch ($this->_errno) {
47             case MGD_SOAP_ERR_OK:
48                 $errstr = "No Error";
49             case MGD_SOAP_ERR_OBJ_NOEXISTS:
50                 $errstr = "Object doesn't exist";
51                 break;
52             case MGD_SOAP_ERR_FUNCTION_NOEXISTS:
53                 $errstr = "Function does not exist";
54                 break;
55             case MGD_SOAP_ERR_METHOD_NOEXISTS:
56                 $errstr = "Method does not exist";
57                 break;
58             case MGD_SOAP_ERR_NO_OBJ:
59                 $errstr = "Not an object";
60                 break;
61             case MGD_SOAP_ERR_NO_TYPE:
62                 $errstr = "No type";
63                 break;
64             case MGD_SOAP_ERR_NO_VALUE:
65                 $errstr = "No value";
66                 break;
67             case MGD_SOAP_ERR_ERROR:
68             default:
69                 $errstr = "Other error";
70         }
71
72         return $errstr;
73     }
74 }
75
76 class MidgardSOAPServer
77 {
78     function func($func, $param, $limit = 1)
79     {
80         $call_func = "mgd_".$func;
81
82         if (!function_exists($call_func))
83             return $this->__fail($call_func, MGD_SOAP_ERR_FUNCTION_NOEXISTS);
84
85         if (!$object = call_user_func_array($call_func, $param))
86             return $this->__fail($object, MGD_SOAP_ERR_OBJ_NOEXISTS);
87
88         if (isset($object->__res__)) {
89             // This is a list!
90             $ret = array();
91
92             while ($limit-- > 0 and $object->fetch()) {
93                 $ret[] = $this->__ok($object);
94             }
95         } else {
96             $ret = $this->__ok($object);
97         }
98
99         return $ret;
100     }
101
102     function meth($meth, $object, $param = NULL, $limit = 1)
103     {
104         if (!is_object($object))
105             return $this->__fail($object, MGD_SOAP_ERR_NO_OBJ);
106
107         if (!array_key_exists('type', get_object_vars($object)))
108             return $this->__fail("", MGD_SOAP_ERR_NO_TYPE);
109
110         if (!array_key_exists('value', get_object_vars($object)))
111             return $this->__fail("", MGD_SOAP_ERR_NO_VALUE);
112
113         // MidgardTopic => _topic
114         // MidgardPageElement => _page_element
115         $type = preg_replace(
116             array("/^Midgard/", "/([A-Z])/e"),
117             array("", "'_'.strtolower('\\1')"),
118             $object->type
119         );
120
121         $call_func = "mgd_get".$type; // Fetch pure object for this type
122
123         if (!function_exists($call_func))
124             return $this->__fail($call_func, MGD_SOAP_ERR_FUNCTION_NOEXISTS);
125
126         $o = call_user_func($call_func); // get an empty object
127
128         if (!method_exists($o, $meth))
129             return $this->__fail($meth, MGD_SOAP_ERR_METHOD_NOEXISTS);
130
131         if ($object->value) {
132             foreach ($object->value as $key => $val) {
133                 $o->$key = $val; // fill the values of out object
134             }
135         }
136
137         $result = call_user_func_array(array($o, $meth), $param);
138
139         if (isset($result->__res__)) {
140             // This is a list!
141             $ret = array();
142
143             while ($limit-- > 0 and $result->fetch()) {
144                 $ret[] = $this->__ok($result);
145             }
146         } else {
147             $ret = $this->__ok($result);
148         }
149
150         return $ret;
151     }
152
153     function ping()
154     {
155         return $this->__ok('pong');
156     }
157
158     function __ok($value = NULL)
159     {
160         $ms = new MidgardResultSOAP($value);
161         return $ms->to_soap();
162     }
163
164     function __fail($value = NULL, $errno = MGD_SOAP_ERR_ERROR)
165     {
166         $ms = new MidgardResultSOAP($value, $errno);
167         return $ms->to_soap();
168     }
169 }
170
171     $server = new SOAP_Server();
172
173     if (isset($_GET["charset"]) and $_GET["charset"] == 'utf-8')
174         $server->response_encoding = 'UTF-8';
175     else
176         $server->response_encoding = 'ISO-8859-1';
177
178     $server->addObjectMap(new MidgardSOAPServer(),'urn:MidgardServer');
179     $server->service($HTTP_RAW_POST_DATA);
180 ?>
181
Note: See TracBrowser for help on using the browser.