source: trunk/phpgwapi/inc/class.xmlrpc_server_php.inc.php @ 5509

Revision 5509, 13.9 KB checked in by gustavo, 12 years ago (diff)

Ticket #2488 - Adicionar cabecalho de licenca em arquivos que nao o possuem

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2                /***************************************************************************
3                * Expresso Livre                                                           *
4                * http://www.expressolivre.org                                             *
5                * --------------------------------------------                             *
6                *  This program is free software; you can redistribute it and/or modify it *
7                *  under the terms of the GNU General Public License as published by the   *
8                *  Free Software Foundation; either version 2 of the License, or (at your  *
9                *  option) any later version.                                              *
10                \**************************************************************************/
11               
12// Copyright (c) 1999,2000,2001 Edd Dumbill.
13// All rights reserved.
14//
15// Redistribution and use in source and binary forms, with or without
16// modification, are permitted provided that the following conditions
17// are met:
18//
19//    * Redistributions of source code must retain the above copyright
20//      notice, this list of conditions and the following disclaimer.
21//
22//    * Redistributions in binary form must reproduce the above
23//      copyright notice, this list of conditions and the following
24//      disclaimer in the documentation and/or other materials provided
25//      with the distribution.
26//
27//    * Neither the name of the "XML-RPC for PHP" nor the names of its
28//      contributors may be used to endorse or promote products derived
29//      from this software without specific prior written permission.
30//
31// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35// REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
36// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42// OF THE POSSIBILITY OF SUCH DAMAGE.
43
44
45        /* BEGIN server class */
46        class xmlrpc_server extends xmlrpc_server_shared
47        {
48                var $dmap = array();
49                var $authed = False;
50                var $req_array = array();
51                var $resp_struct = array();
52                var $debug = False;
53                var $method_requested;
54                var $log = False;       //'/tmp/xmlrpc.log';
55
56                function xmlrpc_server($dispMap='', $serviceNow=0)
57                {
58                        global $HTTP_RAW_POST_DATA;
59
60                        // dispMap is a despatch array of methods
61                        // mapped to function names and signatures
62                        // if a method
63                        // doesn't appear in the map then an unknown
64                        // method error is generated
65                        if($dispMap)
66                        {
67                                $this->dmap = $dispMap;
68                                if ($serviceNow)
69                                {
70                                        $this->service();
71                                }
72                        }
73                }
74
75                function serializeDebug()
76                {
77                        if ($GLOBALS['_xmlrpc_debuginfo'] != '')
78                        {
79                                return "<!-- DEBUG INFO:\n\n" . $GLOBALS['_xmlrpc_debuginfo'] . "\n-->\n";
80                        }
81                        else
82                        {
83                                return '';
84                        }
85                }
86
87                function service($r=False)
88                {
89                        global $HTTP_RAW_POST_DATA;
90
91                        if (!$r)        // do we have a response, or we need to parse the request
92                        {
93                                $r = $this->parseRequest();
94                        }
95                        if (!$r)
96                        {
97                                header('WWW-Authenticate: Basic realm="eGroupWare xmlrpc"');
98                                header('HTTP/1.0 401 Unauthorized');
99                                // for the log:
100                                $payload = "WWW-Authenticate: Basic realm=\"eGroupWare xmlrpc\"\nHTTP/1.0 401 Unauthorized\n";
101                        }
102                        else
103                        {
104                                $payload = "<?xml version=\"1.0\"?>\n" . $this->serializeDebug() . $r->serialize();
105                                Header("Content-type: text/xml\r\nContent-length: " . strlen($payload));
106                                echo $GLOBALS['phpgw']->translation->convert($payload,$GLOBALS['phpgw']->translation->charset(),'utf-8');
107                        }
108
109                        if ($this->log)
110                        {
111                                $fp = fopen($this->log,'a+');
112                                fwrite($fp,"\n\n".date('Y-m-d H:i:s')." authorized=".
113                                        ($this->authed?$GLOBALS['phpgw_info']['user']['account_lid']:'False').
114                                        ", method='$this->last_method'\n");
115                                fwrite($fp,"==== GOT ============================\n".$HTTP_RAW_POST_DATA.
116                                        "\n==== RETURNED =======================\n");
117                                fputs($fp,$payload);
118                                fclose($fp);
119                        }
120
121                        if ($this->debug)
122                        {
123                                $this->echoInput();
124
125                                $fp = fopen('/tmp/xmlrpc_debug.out','w');
126                                fputs($fp,$payload);
127                                fclose($fp);
128                        }
129
130                }
131
132                /*
133                add a method to the dispatch map
134                */
135                function add_to_map($methodname,$function,$sig,$doc)
136                {
137                        $this->dmap[$methodname] = array(
138                                'function'  => $function,
139                                'signature' => $sig,
140                                'docstring' => $doc
141                        );
142                }
143
144                function verifySignature($in, $sig)
145                {
146                        for($i=0; $i<sizeof($sig); $i++)
147                        {
148                                // check each possible signature in turn
149                                $cursig = $sig[$i];
150                                if (sizeof($cursig) == $in->getNumParams()+1)
151                                {
152                                        $itsOK = 1;
153                                        for($n=0; $n<$in->getNumParams(); $n++)
154                                        {
155                                                $p = $in->getParam($n);
156                                                // print "<!-- $p -->\n";
157                                                if ($p->kindOf() == 'scalar')
158                                                {
159                                                        $pt = $p->scalartyp();
160                                                }
161                                                else
162                                                {
163                                                        $pt = $p->kindOf();
164                                                }
165                                                // $n+1 as first type of sig is return type
166                                                if ($pt != $cursig[$n+1])
167                                                {
168                                                        $itsOK  = 0;
169                                                        $pno    = $n+1;
170                                                        $wanted = $cursig[$n+1];
171                                                        $got    = $pt;
172                                                        break;
173                                                }
174                                        }
175                                        if ($itsOK)
176                                        {
177                                                return array(1);
178                                        }
179                                }
180                        }
181                        return array(0, "Wanted $wanted, got $got at param $pno)");
182                }
183
184                function reqtoarray($_req,$recursed=False)
185                {
186                        switch(gettype($_req))
187                        {
188                                case 'object':
189                                        if($recursed)
190                                        {
191                                                return $_req->getval();
192                                        }
193                                        else
194                                        {
195                                                $this->req_array = $_req->getval();
196                                        }
197                                        break;
198                                case 'array':
199                                        @reset($_req);
200                                        $ele = array();
201                                        while(list($key,$val) = @each($_req))
202                                        {
203                                                if($recursed)
204                                                {
205                                                        $ele[$key] = $this->reqtoarray($val,True);
206                                                }
207                                                else
208                                                {
209                                                        $this->req_array[$key] = $this->reqtoarray($val,True);
210                                                }
211                                        }
212                                        if($recursed)
213                                        {
214                                                return $ele;
215                                        }
216                                        break;
217                                case 'string':
218                                case 'integer':
219                                        if($recursed)
220                                        {
221                                                return $_req;
222                                        }
223                                        else
224                                        {
225                                                $this->req_array[] = $_req;
226                                        }
227                                        break;
228                                default:
229                                        break;
230                        }
231                }
232
233                function build_resp($_res)
234                {
235                        if (is_array($_res))
236                        {
237                                $i = 0;
238                                $is_array = True;
239                                foreach($_res as $key => $val)
240                                {
241                                        $ele[$key] = $this->build_resp($val,True);
242                                        $is_array = $is_array && $i === $key;
243                                        ++$i;
244                                }
245                                return CreateObject('phpgwapi.xmlrpcval',$ele,$is_array ? 'array' : 'struct');
246                        }
247                        $_type = (is_integer($_res) ? 'int' : gettype($_res));
248
249                        if ($_type == 'string' && (ereg('^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$',$_res)
250                                                   || ereg('^[0-9]{4}[0-9]{2}[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$',$_res) ) )
251                        {
252                                $_type = 'dateTime.iso8601';
253                        }
254                        // Passing an integer of 0 to the xmlrpcval constructor results in the value being lost. (jengo)
255                        if ($_type == 'int' && $_res == 0)
256                        {
257                                return CreateObject('phpgwapi.xmlrpcval','0',$_type);
258                        }
259                        return CreateObject('phpgwapi.xmlrpcval',$_res,$_type);
260                }
261
262                function parseRequest($data='')
263                {
264                        global $HTTP_RAW_POST_DATA;
265
266                        $r = False;
267
268                        if ($data == '')
269                        {
270                                $data = $HTTP_RAW_POST_DATA;
271                        }
272                        $parser = xml_parser_create($GLOBALS['xmlrpc_defencoding']);
273
274                        $GLOBALS['_xh'][$parser] = array();
275                        $GLOBALS['_xh'][$parser]['st']     = '';
276                        $GLOBALS['_xh'][$parser]['cm']     = 0;
277                        $GLOBALS['_xh'][$parser]['isf']    = 0;
278                        $GLOBALS['_xh'][$parser]['params'] = array();
279                        $GLOBALS['_xh'][$parser]['method'] = '';
280
281                        // decompose incoming XML into request structure
282                        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
283                        xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee');
284                        xml_set_character_data_handler($parser, 'xmlrpc_cd');
285                        xml_set_default_handler($parser, 'xmlrpc_dh');
286                        if (!xml_parse($parser, $data, 1))
287                        {
288                                // return XML error as a faultCode
289                                $r = CreateObject('phpgwapi.xmlrpcresp','',
290                                        $GLOBALS['xmlrpcerrxml'] + xml_get_error_code($parser),
291                                        sprintf('XML error: %s at line %d',
292                                        xml_error_string(xml_get_error_code($parser)),
293                                        xml_get_current_line_number($parser))
294                                );
295                                xml_parser_free($parser);
296                        }
297                        else
298                        {
299                                xml_parser_free($parser);
300                                $m = CreateObject('phpgwapi.xmlrpcmsg',$GLOBALS['_xh'][$parser]['method']);
301                                // now add parameters in
302                                $plist = '';
303                                for($i=0; $i<sizeof($GLOBALS['_xh'][$parser]['params']); $i++)
304                                {
305                                        //print "<!-- " . $GLOBALS['_xh'][$parser]['params'][$i]. "-->\n";
306                                        $plist .= "$i - " . $GLOBALS['_xh'][$parser]['params'][$i]. " \n";
307                                        $code = '$m->addParam(' . $GLOBALS['_xh'][$parser]['params'][$i] . ');';
308                                        $code = str_replace(',,',",'',",$code);
309                                        eval($code);
310                                }
311                                // uncomment this to really see what the server's getting!
312                                // xmlrpc_debugmsg($plist);
313                                // now to deal with the method
314                                $methName  = $GLOBALS['_xh'][$parser]['method'];
315                                $_methName = $GLOBALS['_xh'][$parser]['method'];
316                                $this->last_method = $methName;
317
318                                if (ereg("^system\.", $methName))
319                                {
320                                        $dmap = $GLOBALS['_xmlrpcs_dmap'];
321                                        $sysCall=1;
322                                }
323                                else
324                                {
325                                        $dmap = $this->dmap;
326                                        $sysCall=0;
327                                }
328
329                                if (!isset($dmap[$methName]['function']))
330                                {
331                                        if($sysCall && $this->authed)
332                                        {
333                                                $r = CreateObject('phpgwapi.xmlrpcresp',
334                                                        '',
335                                                        $GLOBALS['xmlrpcerr']['unknown_method'],
336                                                        $GLOBALS['xmlrpcstr']['unknown_method'] . ': ' . $methName
337                                                );
338                                                return $r;
339                                        }
340                                        if ($this->authed)
341                                        {
342                                                /* phpgw mod - fetch the (bo) class methods to create the dmap */
343                                                // This part is to update session action to match
344                                                $this->method_requested = $methName;
345
346                                                $method = $methName;
347                                                $tmp = explode('.',$methName);
348                                                $methName = $tmp[2];
349                                                $service  = $tmp[1];
350                                                $class    = $tmp[0];
351
352                                                if (ereg('^service',$method))
353                                                {
354                                                        $t = 'phpgwapi.' . $class . '.exec';
355                                                        $dmap = ExecMethod($t,array($service,'list_methods','xmlrpc'));
356                                                }
357                                                elseif($GLOBALS['phpgw']->acl->check('run',1,$class))
358                                                {
359                                                        /* This only happens if they have app access.  If not, we will
360                                                         * return a fault below.
361                                                         */
362                                                        $listmeth = $class . '.' . $service . '.' . 'list_methods';
363                                                        $dmap = ExecMethod($listmeth,'xmlrpc');
364                                                }
365                                                else
366                                                {
367                                                        $r = CreateObject('phpgwapi.xmlrpcresp',
368                                                                '',
369                                                                $GLOBALS['xmlrpcerr']['no_access'],
370                                                                $GLOBALS['xmlrpcstr']['no_access']
371                                                        );
372                                                        return $r;
373                                                }
374
375                                                $this->dmap = $dmap;
376                                                /* _debug_array($this->dmap);exit; */
377                                        }
378                                }
379
380                                if (isset($dmap[$methName]['function']))
381                                {
382                                        // dispatch if exists
383                                        if (isset($dmap[$methName]['signature']))
384                                        {
385                                                $sr = $this->verifySignature($m, $dmap[$methName]['signature'] );
386                                        }
387                                        if ( (!isset($dmap[$methName]['signature'])) || $sr[0])
388                                        {
389                                                // if no signature or correct signature
390                                                if ($sysCall)
391                                                {
392                                                        $code = '$r=' . $dmap[$methName]['function'] . '($this, $m);';
393                                                        $code = str_replace(',,',",'',",$code);
394                                                        eval($code);
395                                                }
396                                                else
397                                                {
398                                                        if (function_exists($dmap[$methName]['function']))
399                                                        {
400                                                                $code = '$r =' . $dmap[$methName]['function'] . '($m);';
401                                                                $code = str_replace(',,',",'',",$code);
402                                                                eval($code);
403                                                        }
404                                                        else
405                                                        {
406                                                                /* phpgw mod - finally, execute the function call and return the values */
407                                                                $params = $GLOBALS['_xh'][$parser]['params'][0];
408                                                                $code = '$p = '  . $params . ';';
409                                                                if (count($params) != 0)
410                                                                {
411                                                                        eval($code);
412                                                                        $params = $p->getval();
413                                                                }
414
415                                                                // _debug_array($params);
416                                                                $this->reqtoarray($params);
417                                                                // decode from utf-8 to our charset
418                                                                $this->req_array = $GLOBALS['phpgw']->translation->convert($this->req_array,'utf-8');
419                                                                //_debug_array($this->req_array);
420                                                                if (ereg('^service',$method))
421                                                                {
422                                                                        $res = ExecMethod('phpgwapi.service.exec',array($service,$methName,$this->req_array));
423                                                                }
424                                                                else
425                                                                {
426                                                                        list($s,$c,$m) = explode('.',$_methName);
427                                                                        $res = ExecMethod($s . '.' . $c . '.' . $dmap[$methName]['function'],$this->req_array);
428                                                                }
429                                                                //$this->resp_struct = array($this->build_resp($res,True));
430                                                                //@reset($this->resp_struct);
431                                                                //$r = CreateObject('phpgwapi.xmlrpcresp',CreateObject('phpgwapi.xmlrpcval',$this->resp_struct,'struct'));
432                                                                // this fixes the unnecessary (and not standard-conform) array/xmlrpc struct around everything
433                                                                $r = CreateObject('phpgwapi.xmlrpcresp',$this->build_resp($res,True));
434                                                                /* _debug_array($r); */
435                                                        }
436                                                }
437                                        }
438                                        else
439                                        {
440                                                $r = CreateObject('phpgwapi.xmlrpcresp',
441                                                        '',
442                                                        $GLOBALS['xmlrpcerr']['incorrect_params'],
443                                                        $GLOBALS['xmlrpcstr']['incorrect_params'] . ': ' . $sr[1]
444                                                );
445                                        }
446                                }
447                                else
448                                {
449                                        // else prepare error response
450                                        if(!$this->authed)
451                                        {
452                                                $r = False;     // send 401 header to force authorization
453                                                        /*CreateObject('phpgwapi.xmlrpcresp',
454                                                        CreateObject('phpgwapi.xmlrpcval',
455                                                                'UNAUTHORIZED',
456                                                                'string'
457                                                        )
458                                                );*/
459                                        }
460                                        else
461                                        {
462                                                $r = CreateObject('phpgwapi.xmlrpcresp',
463                                                        '',
464                                                        $GLOBALS['xmlrpcerr']['unknown_method'],
465                                                        $GLOBALS['xmlrpcstr']['unknown_method'] . ': ' . $methName
466                                                );
467                                        }
468                                }
469                        }
470                        return $r;
471                }
472
473                function echoInput()
474                {
475                        global $HTTP_RAW_POST_DATA;
476
477                        // a debugging routine: just echos back the input
478                        // packet as a string value
479
480                        $r = CreateObject('phpgwapi.xmlrpcresp',CreateObject('phpgwapi.xmlrpcval',"'Aha said I: '" . $HTTP_RAW_POST_DATA,'string'));
481                        //echo $r->serialize();
482
483                        $fp = fopen('/tmp/xmlrpc_debug.in','w');
484                        fputs($fp,$r->serialize);
485                        fputs($fp,$HTTP_RAW_POST_DATA);
486                        fclose($fp);
487                }
488
489                function xmlrpc_error($error_number, $error_string)
490                {
491                        $r = CreateObject('phpgwapi.xmlrpcresp',
492                                '',
493                                $error_number,
494                                $error_string . ': ' . $this->last_method
495                        );
496                        $this->service($r);
497                        exit;
498                }
499        }
500?>
Note: See TracBrowser for help on using the repository browser.