[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /**************************************************************************\ 3 * eGroupWare API - phpgw Inter-server communications class * 4 * This file written by Miles Lott <milosch@groupwhere.org> * 5 * Maintain list and provide send interface to remote phpgw servers * 6 * Copyright (C) 2001 Miles Lott * 7 * -------------------------------------------------------------------------* 8 * This library is part of the eGroupWare API * 9 * http://www.egroupware.org * 10 * ------------------------------------------------------------------------ * 11 * This library is free software; you can redistribute it and/or modify it * 12 * under the terms of the GNU Lesser General Public License as published by * 13 * the Free Software Foundation; either version 2.1 of the License, * 14 * or any later version. * 15 * This library is distributed in the hope that it will be useful, but * 16 * WITHOUT ANY WARRANTY; without even the implied warranty of * 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 18 * See the GNU Lesser General Public License for more details. * 19 * You should have received a copy of the GNU Lesser General Public License * 20 * along with this library; if not, write to the Free Software Foundation, * 21 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 22 \**************************************************************************/ 23 24 /* $Id: class.interserver.inc.php 20295 2006-02-15 12:31:25Z $ */ 25 26 class interserver 27 { 28 var $db; 29 var $accounts; 30 var $table = 'egw_interserv'; 31 var $total = 0; 32 var $result = ''; 33 34 var $servers = array(); 35 var $serverid = 0; 36 var $security = ''; 37 var $mode = ''; 38 var $authed = False; 39 var $sessionid = ''; 40 var $kp3 = ''; 41 42 /* These are now entered as defaults if the admin forgot to enter the full URL */ 43 var $urlparts = array(); 44 45 /* 46 0/none == no access 47 1/apps == read app data only 48 99/all == read accounts and other api data 49 Two servers must have each other setup as 'all' for full coop 50 */ 51 var $trust_levels = array( 52 'none' => 0, 53 'apps' => 1, 54 'all' => 99 55 ); 56 57 /* 58 0 - No trust, but they may trust us 59 1 - Trust to make requests of us 60 2 - Trust remote server's trusts also 61 3 - We both trust each other 62 4 - We both trust each other, and we trust the remote server's trusts also 63 */ 64 var $trust_relationships = array( 65 'outbound' => 0, 66 'inbound' => 1, 67 'passthrough' => 2, 68 'bi-directional' => 3, 69 'bi-dir passthrough' => 4 70 ); 71 72 var $security_types = array( 73 'standard' => '', 74 'ssl' => 'ssl' 75 ); 76 77 var $server_modes = array( 78 'XML-RPC' => 'xmlrpc', 79 'SOAP' => 'soap' 80 ); 81 82 function interserver($serverid='') 83 { 84 $url = eregi_replace('https*://[^/]*/','',$GLOBALS['egw_info']['server']['webserver_url']); 85 $this->urlparts = array( 86 'xmlrpc' => $url.'/xmlrpc.php', 87 'soap' => $url.'/soap.php' 88 ); 89 90 $this->db = clone($GLOBALS['egw']->db); 91 if($serverid) 92 { 93 $this->serverid = (int)$serverid; 94 $this->setup(); 95 } 96 } 97 98 function debug($str,$debug=False) 99 { 100 if($debug) 101 { 102 $this->_debug($str); 103 } 104 } 105 106 function _debug($err='') 107 { 108 if(!$err) 109 { 110 return; 111 } 112 echo $err . ' '; 113 } 114 115 function setup() 116 { 117 $this->read_repository(); 118 if($this->server['trust_level']) 119 { 120 $this->accounts =& CreateObject('phpgwapi.accounts'); 121 $this->accounts->server = $this->serverid; 122 } 123 $this->security = $this->server['server_security']; 124 $this->mode = $this->server['server_mode']; 125 } 126 127 /* send command to remote server */ 128 function send($method_name, $args, $url, $debug=False) 129 { 130 $cmd = '_send_' . ($this->mode ? $this->mode : 'xmlrpc') . '_' . $this->security; 131 $this->$cmd($method_name, $args, $url, $debug); 132 return $this->result; 133 } 134 135 function _split_url($url) 136 { 137 preg_match('/^(.*?\/\/.*?)(\/.*)/',$url,$matches); 138 $hostpart = $matches[1]; 139 $hostpart = str_replace('https://','',$hostpart); 140 $hostpart = str_replace('http://','',$hostpart); 141 switch($this->mode) 142 { 143 case 'soap': 144 if(!strstr($matches[2],'soap.php')) 145 { 146 $matches[2] .= $this->urlparts['soap']; 147 } 148 break; 149 case 'xmlrpc': 150 if(!strstr($matches[2],'xmlrpc.php')) 151 { 152 $matches[2] .= $this->urlparts['xmlrpc']; 153 } 154 break; 155 default: 156 break; 157 } 158 $uri = $matches[2]; 159 return array($uri,$hostpart); 160 } 161 162 function _send_xmlrpc_ssl($method_name, $args, $url, $debug=True) 163 { 164 list($uri,$hostpart) = $this->_split_url($url); 165 if(!@is_array($args)) 166 { 167 $arr[] = CreateObject('phpgwapi.xmlrpcval',$args,'string'); 168 $f = CreateObject('phpgwapi.xmlrpcmsg', $method_name, $arr,'string'); 169 } 170 else 171 { 172 foreach($args as $key => $val) 173 { 174 if(@is_array($val)) 175 { 176 foreach($val as $x => $y) 177 { 178 $tmp[$x] = CreateObject('phpgwapi.xmlrpcval',$y, 'string'); 179 } 180 $ele[$key] = CreateObject('phpgwapi.xmlrpcval',$tmp,'struct'); 181 } 182 else 183 { 184 $ele[$key] = CreateObject('phpgwapi.xmlrpcval',$val, 'string'); 185 } 186 } 187 $arr[] = CreateObject('phpgwapi.xmlrpcval',$ele,'struct'); 188 $f = CreateObject('phpgwapi.xmlrpcmsg', $method_name, $arr,'struct'); 189 } 190 191 $this->debug('<pre>' . htmlentities($f->serialize()) . "</pre>\n",$debug); 192 $c = CreateObject('phpgwapi.xmlrpc_client',$uri, $hostpart, 443); 193 $c->setCredentials($this->sessionid,$this->kp3); 194 $c->setDebug($debug); 195 $r = $c->send($f,0,'https'); 196 if (!$r) 197 { 198 $this->debug('send failed'); 199 } 200 $v = $r->value(); 201 if (!$r->faultCode()) 202 { 203 $this->debug('<hr>I got this value back<br><pre>' . htmlentities($r->serialize()) . '</pre><hr>',$debug); 204 $this->result = phpgw_xmlrpc_decode($v); 205 } 206 else 207 { 208 $this->debug('Fault Code: ' . $r->faultCode() . ' Reason "' . $r->faultString() . '"<br>',$debug); 209 $this->result = htmlentities($r->serialize()); 210 } 211 return $this->result; 212 } 213 214 function _send_xmlrpc_($method_name, $args, $url, $debug=True) 215 { 216 list($uri,$hostpart) = $this->_split_url($url); 217 if(!@is_array($args)) 218 { 219 $arr[] = CreateObject('phpgwapi.xmlrpcval',$args,'string'); 220 $f = CreateObject('phpgwapi.xmlrpcmsg', $method_name, $arr,'string'); 221 } 222 else 223 { 224 foreach($args as $key => $val) 225 { 226 if(@is_array($val)) 227 { 228 foreach($val as $x => $y) 229 { 230 $tmp[$x] = CreateObject('phpgwapi.xmlrpcval',$y, 'string'); 231 } 232 $ele[$key] = CreateObject('phpgwapi.xmlrpcval',$tmp,'struct'); 233 } 234 else 235 { 236 $ele[$key] = CreateObject('phpgwapi.xmlrpcval',$val, 'string'); 237 } 238 } 239 $arr[] = CreateObject('phpgwapi.xmlrpcval',$ele,'struct'); 240 $f = CreateObject('phpgwapi.xmlrpcmsg', $method_name, $arr,'struct'); 241 } 242 243 $this->debug('<pre>' . htmlentities($f->serialize()) . '</pre>' . "\n",$debug); 244 $c = CreateObject('phpgwapi.xmlrpc_client',$uri, $hostpart, 80); 245 $c->setCredentials($this->sessionid,$this->kp3); 246 // _debug_array($c); 247 $c->setDebug($debug); 248 $r = $c->send($f); 249 if (!$r) 250 { 251 $this->debug('send failed'); 252 } 253 $v = $r->value(); 254 if (!$r->faultCode()) 255 { 256 $this->debug('<hr>I got this value back<br><pre>' . htmlentities($r->serialize()) . '</pre><hr>',$debug); 257 $this->result = phpgw_xmlrpc_decode($v); 258 } 259 else 260 { 261 $this->debug('Fault Code: ' . $r->faultCode() . ' Reason "' . $r->faultString() . '"<br>',$debug); 262 $this->result = htmlentities($r->serialize()); 263 } 264 return $this->result; 265 } 266 267 function _send_soap_ssl($method_name, $args, $url, $debug=True) 268 { 269 $method_name = str_replace('.','_',$method_name); 270 list($uri,$hostpart) = $this->_split_url($url); 271 if(!@is_array($args)) 272 { 273 $arr[] = CreateObject('phpgwapi.soapval','','string',$args); 274 } 275 else 276 { 277 foreach($args as $key => $val) 278 { 279 if(@is_array($val)) 280 { 281 foreach($val as $x => $y) 282 { 283 $tmp[] = CreateObject('phpgwapi.soapval',$x,'string',$y); 284 } 285 $arr[] = CreateObject('phpgwapi.soapval',$key, 'array',$tmp); 286 } 287 else 288 { 289 $arr[] = CreateObject('phpgwapi.soapval',$key, 'string',$val); 290 } 291 } 292 } 293 294 $soap_message = CreateObject('phpgwapi.soapmsg',$method_name,$arr); 295 /* print_r($soap_message);exit; */ 296 $soap = CreateObject('phpgwapi.soap_client',$uri,$hostpart); 297 $soap->username = $this->sessionid; 298 $soap->password = $this->kp3; 299 /* _debug_array($soap);exit; */ 300 if($r = $soap->send($soap_message,$method_name)) 301 { 302 $this->debug('<hr>I got this value back<br><pre>' . htmlentities($r->serialize()) . '</pre><hr>',$debug); 303 $this->result = $r->decode(); 304 return $this->result; 305 } 306 else 307 { 308 $this->debug('Fault Code: ' . $r->ernno . ' Reason "' . $r->errstring . '"<br>',$debug); 309 } 310 } 311 312 function _send_soap_($method_name, $args, $url, $debug=True) 313 { 314 $method_name = str_replace('.','_',$method_name); 315 list($uri,$hostpart) = $this->_split_url($url); 316 317 if(!$args) 318 { 319 $arr = ''; 320 } 321 elseif(@is_array($args)) 322 { 323 foreach($args as $key => $val) 324 { 325 if(@is_array($val)) 326 { 327 foreach($val as $x => $y) 328 { 329 $tmp[] = CreateObject('phpgwapi.soapval',$x,'string',$y); 330 } 331 $ele[] = CreateObject('phpgwapi.soapval',$key, 'array',$tmp); 332 $complex = True; 333 } 334 else 335 { 336 $ele[] = CreateObject('phpgwapi.soapval',$key, 'string',$val); 337 } 338 } 339 $arr[] = CreateObject('phpgwapi.soapval','','struct',$ele); 340 } 341 else 342 { 343 $arr[] = CreateObject('phpgwapi.soapval','','string',$args); 344 } 345 $this->request = $arr; 346 347 $soap_message = CreateObject('phpgwapi.soapmsg',$method_name,$this->request); 348 $soap = CreateObject('phpgwapi.soap_client',$uri,$hostpart); 349 $soap->username = $this->sessionid; 350 $soap->password = $this->kp3; 351 if($r = $soap->send($soap_message,$method_name)) 352 { 353 _debug_array(htmlentities($soap->outgoing_payload)); 354 _debug_array(htmlentities($soap->incoming_payload)); 355 $this->debug('<hr>I got this value back<br><pre>' . htmlentities($r->serialize()) . '</pre><hr>',$debug); 356 $this->result = $r->decode(); 357 return $this->result; 358 } 359 else 360 { 361 _debug_array($soap->outgoing_payload); 362 $this->debug('Fault Code: ' . $r->ernno . ' Reason "' . $r->errstring . '"<br>',$debug); 363 } 364 } 365 366 function build_request($_req,$recursed=False,$ext='') 367 { 368 if(@is_array($_req)) 369 { 370 $ele = array(); 371 foreach($_req as $key => $val) 372 { 373 $ele[$key] = $this->build_request($val,True,$key); 374 } 375 $this->request[] = CreateObject('phpgwapi.soapval',$ext,'struct',$ele); 376 $ext = ''; 377 } 378 else 379 { 380 $_type = (is_long($_req) ? 'int' : gettype($_req)); 381 if($recursed) 382 { 383 return CreateObject('phpgwapi.soapval',$ext,$_type,$_req); 384 } 385 else 386 { 387 $this->request[$ext] = CreateObject('phpgwapi.soapval',$ext,$_type,$_req); 388 } 389 } 390 } 391 392 /* Following are for server list management and query */ 393 function read_repository($serverid='') 394 { 395 if(!$serverid) 396 { 397 $serverid = $this->serverid; 398 } 399 $sql = "SELECT * FROM $this->table WHERE server_id=" . (int)$serverid; 400 $this->db->query($sql,__LINE__,__FILE__); 401 if($this->db->next_record()) 402 { 403 $this->server['server_name'] = $this->db->f('server_name'); 404 $this->server['server_url'] = $this->db->f('server_url'); 405 $this->server['server_mode'] = $this->db->f('server_mode'); 406 $this->server['server_security'] = $this->db->f('server_security'); 407 $this->server['trust_level'] = $this->db->f('trust_level'); 408 $this->server['trust_rel'] = $this->db->f('trust_rel'); 409 $this->server['username'] = $this->db->f('username'); 410 $this->server['password'] = $this->db->f('password'); 411 $this->server['admin_name'] = $this->db->f('admin_name'); 412 $this->server['admin_email'] = $this->db->f('admin_email'); 413 } 414 return $this->server; 415 } 416 417 function save_repository($serverid='') 418 { 419 if(!$serverid) 420 { 421 $serverid = $this->serverid; 422 } 423 if((int)$serverid && @is_array($this->server)) 424 { 425 $sql = "UPDATE $this->table SET " 426 . "server_name='" . $this->server['server_name'] . "'," 427 . "server_url='" . $this->server['server_url'] . "'," 428 . "server_mode='" . $this->server['server_mode'] . "'," 429 . "server_security='" . $this->server['server_security'] . "'," 430 . "trust_level=" . (int)$this->server['trust_level'] . "," 431 . "trust_rel=" . (int)$this->server['trust_rel'] . "," 432 . "username='" . $this->server['username'] . "'," 433 . "password='" . $this->server['password'] . "'," 434 . "admin_name='" . $this->server['admin_name'] . "'," 435 . "admin_email='" . $this->server['admin_email'] . "' " 436 . "WHERE server_id=" . (int)$serverid; 437 $this->db->query($sql,__LINE__,__FILE__); 438 return True; 439 } 440 return False; 441 } 442 443 function create($server_info='') 444 { 445 if(!@is_array($server_info)) 446 { 447 return False; 448 } 449 $sql = "INSERT INTO $this->table (server_name,server_url,server_mode,server_security," 450 . "trust_level,trust_rel,username,password,admin_name,admin_email) " 451 . "VALUES('" . $server_info['server_name'] . "','" . $server_info['server_url'] . "','" 452 . $server_info['server_mode'] . "','" . $server_info['server_security'] . "'," 453 . (int)$server_info['trust_level'] . "," . (int)$server_info['trust_rel'] . ",'" 454 . $server_info['username'] . "','" . $server_info['password'] . "','" 455 . $server_info['admin_name'] . "','" . $server_info['admin_email'] . "')"; 456 $this->db->query($sql,__LINE__,__FILE__); 457 458 $sql = "SELECT server_id FROM $this->table WHERE server_name='" . $server_info['server_name'] . "'"; 459 $this->db->query($sql,__LINE__,__FILE__); 460 if($this->db->next_record()) 461 { 462 $server_info['server_id'] = $this->db->f(0); 463 $this->serverid = $server_info['server_id']; 464 $this->server = $server_info; 465 return $this->serverid; 466 } 467 return False; 468 } 469 470 function delete($serverid='') 471 { 472 if(!$serverid) 473 { 474 $serverid = $this->serverid; 475 } 476 if($serverid) 477 { 478 $sql = "DELETE FROM $this->table WHERE server_id=$serverid"; 479 $this->db->query($sql,__LINE__,__FILE__); 480 return True; 481 } 482 return False; 483 } 484 485 function get_list($start='',$sort='',$order='',$query='',$offset='',&$total) 486 { 487 if (!$sort) 488 { 489 $sort = 'DESC'; 490 } 491 if ($query) 492 { 493 $whereclause = "WHERE server_name LIKE '%$query%'" 494 . "OR server_url LIKE '%$query%'" 495 . "OR server_mode LIKE '%$query%'" 496 . "OR admin_name LIKE '%$query%'" 497 . "OR admin_email LIKE '%$query%'"; 498 } 499 if ($order) 500 { 501 $orderclause = 'ORDER BY ' . $order . ' ' . $sort; 502 } 503 else 504 { 505 $orderclause = 'ORDER BY server_name ASC'; 506 } 507 508 $sql = "SELECT * FROM $this->table $whereclause $orderclause"; 509 $this->db->query($sql,__LINE__,__FILE__); 510 511 while ($this->db->next_record()) 512 { 513 $this->servers[$this->db->f('server_name')]['server_id'] = $this->db->f('server_id'); 514 $this->servers[$this->db->f('server_name')]['server_name'] = $this->db->f('server_name'); 515 $this->servers[$this->db->f('server_name')]['server_url'] = $this->db->f('server_url'); 516 $this->servers[$this->db->f('server_name')]['server_mode'] = $this->db->f('server_mode'); 517 $this->servers[$this->db->f('server_name')]['server_security'] = $this->db->f('server_security'); 518 $this->servers[$this->db->f('server_name')]['trust_level'] = $this->db->f('trust_level'); 519 $this->servers[$this->db->f('server_name')]['trust_rel'] = $this->db->f('trust_rel'); 520 $this->servers[$this->db->f('server_name')]['admin_name'] = $this->db->f('admin_name'); 521 $this->servers[$this->db->f('server_name')]['admin_email'] = $this->db->f('admin_email'); 522 } 523 $this->total = $this->db->num_rows(); 524 $total = $this->total; 525 return $this->servers; 526 } 527 528 function formatted_list($server_id=0,$java=False,$local=False) 529 { 530 if ($java) 531 { 532 $jselect = ' onChange="this.form.submit();"'; 533 } 534 $select = "\n" .'<select name="server_id"' . $jselect . ">\n"; 535 $select .= '<option value="0"'; 536 if(!$server_id) 537 { 538 $select .= ' selected'; 539 } 540 $selectlang = $local ? lang('Local') : lang('Please Select'); 541 $select .= '>' . $selectlang . '</option>'."\n"; 542 543 $x = ''; 544 $slist = $this->get_list('','','','','',$x); 545 foreach($slist as $key => $val) 546 { 547 $foundservers = True; 548 $select .= '<option value="' . $val['server_id'] . '"'; 549 if ($val['server_id'] == $server_id) 550 { 551 $select .= ' selected'; 552 } 553 $select .= '>' . $val['server_name'] . '</option>'."\n"; 554 } 555 556 $select .= '</select>'."\n"; 557 $select .= '<noscript><input type="submit" name="server_id_select" value="' . lang('Select') . '"></noscript>' . "\n"; 558 if(!$foundservers) 559 { 560 $select = ''; 561 } 562 563 return $select; 564 } 565 566 function name2id($server_name='') 567 { 568 if(!$server_name) 569 { 570 $server_name = $this->server['server_name']; 571 } 572 if($server_name) 573 { 574 $sql = "SELECT server_id FROM $this->table WHERE server_name='$server_name'"; 575 $this->db->query($sql,__LINE__,__FILE__); 576 if($this->db->next_record()) 577 { 578 $serverid = $this->db->f(0); 579 return $serverid; 580 } 581 } 582 return False; 583 } 584 585 function id2name($serverid='') 586 { 587 if(!$serverid) 588 { 589 $serverid = $this->serverid; 590 } 591 if($serverid) 592 { 593 $sql = "SELECT server_name FROM $this->table WHERE server_id=$serverid"; 594 $this->db->query($sql,__LINE__,__FILE__); 595 if($this->db->next_record()) 596 { 597 $server_name = $this->db->f(0); 598 return $server_name; 599 } 600 } 601 return False; 602 } 603 604 function exists($serverdata='') 605 { 606 if(!$serverdata) 607 { 608 return False; 609 } 610 if(is_int($serverdata)) 611 { 612 $serverid = $serverdata; 613 settype($server_name,'string'); 614 $server_name = $this->id2name($serverid); 615 } 616 else 617 { 618 $server_name = $serverdata; 619 } 620 $sql = "SELECT server_id FROM $this->table WHERE server_name='$server_name'"; 621 $this->db->query($sql,__LINE__,__FILE__); 622 if($this->db->next_record()) 623 { 624 return True; 625 } 626 return False; 627 } 628 629 /* TODO - Determine trust level here */ 630 function auth($serverdata='') 631 { 632 if(!$serverdata || !@is_array($serverdata)) 633 { 634 return False; 635 } 636 $server_name = $serverdata['server_name']; 637 $username = $serverdata['username']; 638 $password = $serverdata['password']; 639 640 $sql = "SELECT server_id,trust_rel FROM $this->table WHERE server_name='$server_name'"; 641 $this->db->query($sql,__LINE__,__FILE__); 642 if($this->db->next_record()) 643 { 644 if ($username == $GLOBALS['egw_info']['server']['site_username'] && 645 $password == $GLOBALS['egw_info']['server']['site_password'] && 646 $this->db->f('trust_rel') >= 1) 647 { 648 $this->authed = True; 649 return True; 650 } 651 } 652 return False; 653 } 654 } 655 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |