[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/inc/ -> class.xmlrpc_server_epi.inc.php (source)

   1  <?php
   2    /**************************************************************************\
   3    * eGroupWare API - XML-RPC Server                                          *
   4    * This file written by Miles Lott <milos@groupwhere.org>                   *
   5    * Copyright (C) 2003 Miles Lott                                            *
   6    * -------------------------------------------------------------------------*
   7    * This library is free software; you can redistribute it and/or modify it  *
   8    * under the terms of the GNU Lesser General Public License as published by *
   9    * the Free Software Foundation; either version 2.1 of the License,         *
  10    * or any later version.                                                    *
  11    * This library is distributed in the hope that it will be useful, but      *
  12    * WITHOUT ANY WARRANTY; without even the implied warranty of               *
  13    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
  14    * See the GNU Lesser General Public License for more details.              *
  15    * You should have received a copy of the GNU Lesser General Public License *
  16    * along with this library; if not, write to the Free Software Foundation,  *
  17    * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
  18    \**************************************************************************/
  19  
  20    /* $Id: class.xmlrpc_server_epi.inc.php 20295 2006-02-15 12:31:25Z  $ */
  21  
  22      class xmlrpc_server extends xmlrpc_server_shared
  23      {
  24          var $server = '';
  25          var $authed = True;
  26          var $log = False; //'/tmp/xmlrpc.log';
  27          var $last_method = '';
  28  
  29  		function xmlrpc_server($dispMap='', $serviceNow=0)
  30          {
  31              $this->server = xmlrpc_server_create();
  32              if($dispMap)
  33              {
  34                  $this->dmap = $dispMap;
  35                  if($serviceNow)
  36                  {
  37                      $this->service();
  38                  }
  39              }
  40          }
  41  
  42  		function serializeDebug()
  43          {
  44          }
  45  
  46  		function service($r = False)
  47          {
  48              if (!$r)    // do we have a response, or we need to parse the request
  49              {
  50                  $r = $this->parseRequest();
  51              }
  52              if(!$r)
  53              {
  54                  header('WWW-Authenticate: Basic realm="eGroupWare xmlrpc"');
  55                  header('HTTP/1.0 401 Unauthorized');
  56                  // for the log:
  57                  $payload = "WWW-Authenticate: Basic realm=\"eGroupWare xmlrpc\"\nHTTP/1.0 401 Unauthorized\n";
  58                  echo $payload;
  59              }
  60              else
  61              {
  62  //                $payload = '<?xml version="1.0"?\>' . "\n" . $this->serializeDebug() . $r->serialize();
  63  //                Header("Content-type: text/xml\r\nContent-length: " . strlen($payload));
  64  //                print $payload;
  65                  echo $r;
  66              }
  67  
  68              if($this->log)
  69              {
  70                  $fp = fopen($this->log,'a+');
  71                  fwrite($fp,"\n\n" . date('Y-m-d H:i:s') . " authorized="
  72                      . ($this->authed ? $GLOBALS['egw_info']['user']['account_lid'] : 'False')
  73                      . ", method='$this->last_method'\n");
  74                  fwrite($fp,"==== GOT ============================\n" . $GLOBALS['HTTP_RAW_POST_DATA']
  75                      . "\n==== RETURNED =======================\n");
  76                  fputs($fp,$payload);
  77                  fclose($fp);
  78              }
  79  
  80              if($this->debug)
  81              {
  82                  $this->echoInput();
  83  
  84                  $fp = fopen('/tmp/xmlrpc_debug.out','a+');
  85                  fputs($fp,$payload);
  86                  fclose($fp);
  87              }
  88          }
  89  
  90  		function add_to_map($methodname,$function,$sig,$doc)
  91          {
  92              xmlrpc_server_register_method($this->server,$methodname,$function);
  93  //            xmlrpc_server_register_method($this->server,$methodname,'xmlrpc_call_wrapper');
  94  //            $descr =  array(
  95  //                'function'  => $function,
  96  //                'signature' => $sig,
  97  //                'docstring' => $doc
  98  //            );
  99  //            xmlrpc_server_set_method_description($this->server,$methodname,$descr);
 100  
 101              $this->dmap[$methodname] = array(
 102                  'function'  => $function,
 103                  'signature' => $sig,
 104                  'docstring' => $doc
 105              );
 106          }
 107  
 108  		function verifySignature($in, $sig)
 109          {
 110              return array(1);
 111  
 112              for($i=0; $i<sizeof($sig); $i++)
 113              {
 114                  // check each possible signature in turn
 115                  $cursig = $sig[$i];
 116                  if (sizeof($cursig) == $in->getNumParams()+1)
 117                  {
 118                      $itsOK = 1;
 119                      for($n=0; $n<$in->getNumParams(); $n++)
 120                      {
 121                          $p = $in->getParam($n);
 122                          // print "<!-- $p -->\n";
 123                          if ($p->kindOf() == 'scalar')
 124                          {
 125                              $pt = $p->scalartyp();
 126                          }
 127                          else
 128                          {
 129                              $pt = $p->kindOf();
 130                          }
 131                          // $n+1 as first type of sig is return type
 132                          if ($pt != $cursig[$n+1])
 133                          {
 134                              $itsOK  = 0;
 135                              $pno    = $n+1;
 136                              $wanted = $cursig[$n+1];
 137                              $got    = $pt;
 138                              break;
 139                          }
 140                      }
 141                      if ($itsOK)
 142                      {
 143                          return array(1);
 144                      }
 145                  }
 146              }
 147              return array(0, "Wanted $wanted, got $got at param $pno)");
 148          }
 149  
 150  		function parseRequest($data='')
 151          {
 152              if($data == '')
 153              {
 154                  $data = $GLOBALS['HTTP_RAW_POST_DATA'];
 155              }
 156  //            return $this->echoInput($data);
 157  
 158              /* Decode to extract methodName */
 159              $params = xmlrpc_decode_request($data, &$methName);
 160              $this->last_method = $methName;
 161              $syscall = 0;
 162  
 163              /* Setup dispatch map based on the function, if this is a system call */
 164              if(ereg('^system\.', $methName))
 165              {
 166                  foreach($GLOBALS['_xmlrpcs_dmap'] as $meth => $dat)
 167                  {
 168                      $this->add_to_map($meth,$dat['function'],$dat['signature'],$dat['docstring']);
 169                  }
 170                  $sysCall = 1;
 171                  $dmap = $this->dmap;
 172              }
 173              elseif(ereg('^examples\.',$methName) ||
 174                  ereg('^validator1\.',$methName) ||
 175                  ereg('^interopEchoTests\.', $methName)
 176              )
 177              {
 178                  $dmap = $this->dmap;
 179                  $sysCall = 1;
 180              }
 181  
 182              /* verify dispatch map, or try to fix it for non-trivial system calls */
 183              if(!isset($this->dmap[$methName]['function']))
 184              {
 185                  if($sysCall)
 186                  {
 187                      /* Bad or non-existent system call, return error */
 188                      $r = CreateObject('phpgwapi.xmlrpcresp',
 189                          '',
 190                          $GLOBALS['xmlrpcerr']['unknown_method'],
 191                          $GLOBALS['xmlrpcstr']['unknown_method'] . ': ' . $methName
 192                      );
 193                      return $r;
 194                  }
 195                  if($this->authed)
 196                  {
 197                      $method = $methName;
 198                      list($app,$class,$method) = explode('.',$methName);
 199  
 200                      switch($app)
 201                      {
 202                          case 'server':
 203                          case 'phpgwapi':
 204                              /* Server role functions only - api access */
 205                              if($GLOBALS['egw']->acl->get_role() >= EGW_ACL_SERVER)
 206                              {
 207                                  $dmap = ExecMethod(sprintf('%s.%s.%s','phpgwapi',$class,'list_methods'),'xmlrpc');
 208                              }
 209                              break;
 210                          case 'service':
 211                              /* Service functions, user-level */
 212                              $t = 'phpgwapi.' . $class . '.exec';
 213                              $dmap = ExecMethod($t,array($service,'list_methods','xmlrpc'));
 214                              break;
 215                          default:
 216                              /* User-level application access */
 217                              if($GLOBALS['egw']->acl->check('run',EGW_ACL_READ,$app))
 218                              {
 219                                  $dmap = ExecMethod(sprintf('',$app,$class,'list_methods'),'xmlrpc');
 220                              }
 221                              else
 222                              {
 223                                  $r = CreateObject('phpgwapi.xmlrpcresp',
 224                                      '',
 225                                      $GLOBALS['xmlrpcerr']['no_access'],
 226                                      $GLOBALS['xmlrpcstr']['no_access']
 227                                  );
 228                                  return $r;
 229                              }
 230                      }
 231                  }
 232              }
 233  
 234              /* add the functions from preset $dmap OR list_methods() to the server map */
 235              foreach($dmap as $meth => $dat)
 236              {
 237                  $this->add_to_map($meth,$dat['function'],$dat['signature'],$dat['docstring']);
 238              }
 239          
 240              /* _debug_array($this->dmap);exit; */
 241  
 242              /* Now make the call */
 243              if(isset($dmap[$methName]['function']))
 244              {
 245                  // dispatch if exists
 246                  if(isset($dmap[$methName]['signature']))
 247                  {
 248                      $sr = $this->verifySignature($m, $dmap[$methName]['signature'] );
 249                  }
 250                  if((!isset($dmap[$methName]['signature'])) || $sr[0])
 251                  {
 252                      // if no signature or correct signature
 253                      $r = xmlrpc_server_call_method($this->server,$data,$params);
 254                  }
 255                  else
 256                  {
 257                      $r = CreateObject('phpgwapi.xmlrpcresp',
 258                          '',
 259                          $GLOBALS['xmlrpcerr']['incorrect_params'],
 260                          $GLOBALS['xmlrpcstr']['incorrect_params'] . ': ' . $sr[1]
 261                      );
 262                  }
 263              }
 264              else
 265              {
 266                  // else prepare error response
 267                  if(!$this->authed)
 268                  {
 269  //                    $r = False;
 270                      // send 401 header to force authorization
 271                      $r = CreateObject('phpgwapi.xmlrpcresp',
 272                          CreateObject('phpgwapi.xmlrpcval',
 273                              'UNAUTHORIZED',
 274                              'string'
 275                          )
 276                      );
 277                  }
 278                  else
 279                  {
 280                      $r = CreateObject('phpgwapi.xmlrpcresp',
 281                          '',
 282                          $GLOBALS['xmlrpcerr']['unknown_method'],
 283                          $GLOBALS['xmlrpcstr']['unknown_method'] . ': ' . $methName
 284                      );
 285                  }
 286              }
 287              xmlrpc_server_destroy($xmlrpc_server);
 288              return $r;
 289          }
 290  
 291  		function echoInput()
 292          {
 293              // a debugging routine: just echos back the input
 294              // packet as a string value
 295  
 296              /* TODO */
 297  //            $r = CreateObject('phpgwapi.xmlrpcresp',CreateObject('phpgwapi.xmlrpcval',"'Aha said I: '" . $HTTP_RAW_POST_DATA,'string'));
 298              return $GLOBALS['HTTP_RAW_POST_DATA'];
 299          }
 300  
 301  		function xmlrpc_custom_error($error_number, $error_string, $filename, $line, $vars)
 302          {
 303              if(error_reporting() & $error_number)
 304              {
 305                  $error_string .= sprintf("\nFilename: %s\nLine: %s",$filename,$line);
 306  
 307                  xmlrpc_error(1005,$error_string);
 308              }
 309          }
 310  /*
 311          function xmlrpc_error($error_number, $error_string)
 312          {
 313              $values = array(
 314                  'faultString' => $error_string,
 315                  'faultCode'   => $error_number
 316              );
 317  
 318              echo xmlrpc_encode_request(NULL,$values);
 319  
 320              xmlrpc_server_destroy($GLOBALS['xmlrpc_server']);
 321              exit;
 322          }
 323  */
 324  		function xmlrpc_error($error_number, $error_string)
 325          {
 326              $r = CreateObject('phpgwapi.xmlrpcresp',
 327                  '',
 328                  $error_number,
 329                  $error_string . ': ' . $this->last_method
 330              );
 331              $this->service($r);
 332              xmlrpc_server_destroy($GLOBALS['xmlrpc_server']);
 333              exit;
 334          }
 335      }


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7