source: trunk/phpgwapi/inc/class.interserver.inc.php @ 2

Revision 2, 18.7 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
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
25        class interserver
26        {
27                var $db;
28                var $accounts;
29                var $table = 'phpgw_interserv';
30                var $total = 0;
31                var $result = '';
32
33                var $servers = array();
34                var $serverid = 0;
35                var $security = '';
36                var $mode = '';
37                var $authed = False;
38                var $sessionid = '';
39                var $kp3 = '';
40
41                /* These are now entered as defaults if the admin forgot to enter the full URL */
42                var $urlparts = array();
43
44                /*
45                0/none == no access
46                1/apps == read app data only
47                99/all == read accounts and other api data
48                Two servers must have each other setup as 'all' for full coop
49                */
50                var $trust_levels = array(
51                        'none' => 0,
52                        'apps' => 1,
53                        'all'  => 99
54                );
55
56                /*
57                0 - No trust, but they may trust us
58                1 - Trust to make requests of us
59                2 - Trust remote server's trusts also
60                3 - We both trust each other
61                4 - We both trust each other, and we trust the remote server's trusts also
62                */
63                var $trust_relationships = array(
64                        'outbound'       => 0,
65                        'inbound'        => 1,
66                        'passthrough'    => 2,
67                        'bi-directional' => 3,
68                        'bi-dir passthrough' => 4
69                );
70
71                var $security_types = array(
72                        'standard' => '',
73                        'ssl'      => 'ssl'
74                );
75
76                var $server_modes = array(
77                        'XML-RPC' => 'xmlrpc',
78                        'SOAP'    => 'soap'
79                );
80
81                function interserver($serverid='')
82                {
83                        $url = eregi_replace('https*://[^/]*/','',$GLOBALS['phpgw_info']['server']['webserver_url']);
84                        $this->urlparts = array(
85                                'xmlrpc' => $url.'/xmlrpc.php',
86                                'soap'   => $url.'/soap.php'
87                        );
88
89                        $this->db = $GLOBALS['phpgw']->db;
90                        if($serverid)
91                        {
92                                $this->serverid = (int)$serverid;
93                                $this->setup();
94                        }
95                }
96
97                function debug($str,$debug=False)
98                {
99                        if($debug)
100                        {
101                                $this->_debug($str);
102                        }
103                }
104
105                function _debug($err='')
106                {
107                        if(!$err)
108                        {
109                                return;
110                        }
111                        echo $err . '&nbsp;';
112                }
113
114                function setup()
115                {
116                        $this->read_repository();
117                        if($this->server['trust_level'])
118                        {
119                                $this->accounts = CreateObject('phpgwapi.accounts');
120                                $this->accounts->server = $this->serverid;
121                        }
122                        $this->security = $this->server['server_security'];
123                        $this->mode = $this->server['server_mode'];
124                }
125
126                /* send command to remote server */
127                function send($method_name, $args, $url, $debug=True)
128                {
129                        $cmd = '_send_' . ($this->mode ? $this->mode : 'xmlrpc') . '_' . $this->security;
130                        $this->$cmd($method_name, $args, $url, $debug);
131                        return $this->result;
132                }
133
134                function _split_url($url)
135                {
136                        preg_match('/^(.*?\/\/.*?)(\/.*)/',$url,$matches);
137                        $hostpart = $matches[1];
138                        $hostpart = str_replace('https://','',$hostpart);
139                        $hostpart = str_replace('http://','',$hostpart);
140                        switch($this->mode)
141                        {
142                                case 'soap':
143                                        if(!strstr($matches[2],'soap.php'))
144                                        {
145                                                $matches[2] .= $this->urlparts['soap'];
146                                        }
147                                        break;
148                                case 'xmlrpc':
149                                        if(!strstr($matches[2],'xmlrpc.php'))
150                                        {
151                                                $matches[2] .= $this->urlparts['xmlrpc'];
152                                        }
153                                        break;
154                                default:
155                                        break;
156                        }
157                        $uri = $matches[2];
158                        return array($uri,$hostpart);
159                }
160
161                function _send_xmlrpc_ssl($method_name, $args, $url, $debug=True)
162                {
163                        list($uri,$hostpart) = $this->_split_url($url);
164                        if(!@is_array($args))
165                        {
166                                $arr[] = CreateObject('phpgwapi.xmlrpcval',$args,'string');
167                                $f = CreateObject('phpgwapi.xmlrpcmsg', $method_name, $arr,'string');
168                        }
169                        else
170                        {
171                                while(list($key,$val) = @each($args))
172                                {
173                                        if(@is_array($val))
174                                        {
175                                                while(list($x,$y) = each($val))
176                                                {
177                                                        $tmp[$x] = CreateObject('phpgwapi.xmlrpcval',$y, 'string');
178                                                }
179                                                $ele[$key] = CreateObject('phpgwapi.xmlrpcval',$tmp,'struct');
180                                        }
181                                        else
182                                        {
183                                                $ele[$key] = CreateObject('phpgwapi.xmlrpcval',$val, 'string');
184                                        }
185                                }
186                                $arr[] = CreateObject('phpgwapi.xmlrpcval',$ele,'struct');
187                                $f = CreateObject('phpgwapi.xmlrpcmsg', $method_name, $arr,'struct');
188                        }
189
190                        $this->debug("<pre>" . htmlentities($f->serialize()) . "</pre>\n",$debug);
191                        $c = CreateObject('phpgwapi.xmlrpc_client',$uri, $hostpart, 443);
192                        $c->setCredentials($this->sessionid,$this->kp3);
193                        $c->setDebug(0);
194                        $r = $c->send($f,0,'https');
195                        if (!$r)
196                        {
197                                $this->debug('send failed');
198                        }
199                        $v = $r->value();
200                        if (!$r->faultCode())
201                        {
202                                $this->debug('<hr>I got this value back<br><pre>' . htmlentities($r->serialize()) . '</pre><hr>',$debug);
203                                $this->result = phpgw_xmlrpc_decode($v);
204                        }
205                        else
206                        {
207                                $this->debug('Fault Code: ' . $r->faultCode() . ' Reason "' . $r->faultString() . '"<br>',$debug);
208                                $this->result = htmlentities($r->serialize());
209                        }
210                        return $this->result;
211                }
212
213                function _send_xmlrpc_($method_name, $args, $url, $debug=True)
214                {
215                        list($uri,$hostpart) = $this->_split_url($url);
216                        if(!@is_array($args))
217                        {
218                                $arr[] = CreateObject('phpgwapi.xmlrpcval',$args,'string');
219                                $f = CreateObject('phpgwapi.xmlrpcmsg', $method_name, $arr,'string');
220                        }
221                        else
222                        {
223                                while(list($key,$val) = @each($args))
224                                {
225                                        if(@is_array($val))
226                                        {
227                                                while(list($x,$y) = each($val))
228                                                {
229                                                        $tmp[$x] = CreateObject('phpgwapi.xmlrpcval',$y, 'string');
230                                                }
231                                                $ele[$key] = CreateObject('phpgwapi.xmlrpcval',$tmp,'struct');
232                                        }
233                                        else
234                                        {
235                                                $ele[$key] = CreateObject('phpgwapi.xmlrpcval',$val, 'string');
236                                        }
237                                }
238                                $arr[] = CreateObject('phpgwapi.xmlrpcval',$ele,'struct');
239                                $f = CreateObject('phpgwapi.xmlrpcmsg', $method_name, $arr,'struct');
240                        }
241
242                        $this->debug('<pre>' . htmlentities($f->serialize()) . '</pre>' . "\n",$debug);
243                        $c = CreateObject('phpgwapi.xmlrpc_client',$uri, $hostpart, 80);
244                        $c->setCredentials($this->sessionid,$this->kp3);
245//                      _debug_array($c);
246                        $c->setDebug(0);
247                        $r = $c->send($f);
248                        if (!$r)
249                        {
250                                $this->debug('send failed');
251                        }
252                        $v = $r->value();
253                        if (!$r->faultCode())
254                        {
255                                $this->debug('<hr>I got this value back<br><pre>' . htmlentities($r->serialize()) . '</pre><hr>',$debug);
256                                $this->result = phpgw_xmlrpc_decode($v);
257                        }
258                        else
259                        {
260                                $this->debug('Fault Code: ' . $r->faultCode() . ' Reason "' . $r->faultString() . '"<br>',$debug);
261                                $this->result = htmlentities($r->serialize());
262                        }
263                        return $this->result;
264                }
265
266                function _send_soap_ssl($method_name, $args, $url, $debug=True)
267                {
268                        $method_name = str_replace('.','_',$method_name);
269                        list($uri,$hostpart) = $this->_split_url($url);
270                        if(!@is_array($args))
271                        {
272                                $arr[] = CreateObject('phpgwapi.soapval','','string',$args);
273                        }
274                        else
275                        {
276                                while(list($key,$val) = @each($args))
277                                {
278                                        if(@is_array($val))
279                                        {
280                                                while(list($x,$y) = each($val))
281                                                {
282                                                        $tmp[] = CreateObject('phpgwapi.soapval',$x,'string',$y);
283                                                }
284                                                $arr[] = CreateObject('phpgwapi.soapval',$key, 'array',$tmp);
285                                        }
286                                        else
287                                        {
288                                                $arr[] = CreateObject('phpgwapi.soapval',$key, 'string',$val);
289                                        }
290                                }
291                        }
292
293                        $soap_message = CreateObject('phpgwapi.soapmsg',$method_name,$arr);
294                        /* print_r($soap_message);exit; */
295                        $soap = CreateObject('phpgwapi.soap_client',$uri,$hostpart);
296                        $soap->username = $this->sessionid;
297                        $soap->password = $this->kp3;
298                        /* _debug_array($soap);exit; */
299                        if($r = $soap->send($soap_message,$method_name))
300                        {
301                                $this->debug('<hr>I got this value back<br><pre>' . htmlentities($r->serialize()) . '</pre><hr>',$debug);
302                                $this->result = $r->decode();
303                                return $this->result;
304                        }
305                        else
306                        {
307                                $this->debug('Fault Code: ' . $r->ernno . ' Reason "' . $r->errstring . '"<br>',$debug);
308                        }
309                }
310
311                function _send_soap_($method_name, $args, $url, $debug=True)
312                {
313                        $method_name = str_replace('.','_',$method_name);
314                        list($uri,$hostpart) = $this->_split_url($url);
315
316                        if(!$args)
317                        {
318                                $arr = '';
319                        }
320                        elseif(@is_array($args))
321                        {
322                                while(list($key,$val) = @each($args))
323                                {
324                                        if(@is_array($val))
325                                        {
326                                                while(list($x,$y) = each($val))
327                                                {
328                                                        $tmp[] = CreateObject('phpgwapi.soapval',$x,'string',$y);
329                                                }
330                                                $ele[] = CreateObject('phpgwapi.soapval',$key, 'array',$tmp);
331                                                $complex = True;
332                                        }
333                                        else
334                                        {
335                                                $ele[] = CreateObject('phpgwapi.soapval',$key, 'string',$val);
336                                        }
337                                }
338                                $arr[] = CreateObject('phpgwapi.soapval','','struct',$ele);
339                        }
340                        else
341                        {
342                                $arr[] = CreateObject('phpgwapi.soapval','','string',$args);
343                        }
344                        $this->request = $arr;
345
346                        $soap_message = CreateObject('phpgwapi.soapmsg',$method_name,$this->request);
347                        $soap = CreateObject('phpgwapi.soap_client',$uri,$hostpart);
348                        $soap->username = $this->sessionid;
349                        $soap->password = $this->kp3;
350                        if($r = $soap->send($soap_message,$method_name))
351                        {
352                                _debug_array(htmlentities($soap->outgoing_payload));
353                                _debug_array(htmlentities($soap->incoming_payload));
354                                $this->debug('<hr>I got this value back<br><pre>' . htmlentities($r->serialize()) . '</pre><hr>',$debug);
355                                $this->result = $r->decode();
356                                return $this->result;
357                        }
358                        else
359                        {
360                                _debug_array($soap->outgoing_payload);
361                                $this->debug('Fault Code: ' . $r->ernno . ' Reason "' . $r->errstring . '"<br>',$debug);
362                        }
363                }
364
365                function build_request($_req,$recursed=False,$ext='')
366                {
367                        if(@is_array($_req))
368                        {
369                                $ele = array();
370                                @reset($_req);
371                                while(list($key,$val) = @each($_req))
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                        while (list($key,$val) = each($slist))
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['phpgw_info']['server']['site_username'] &&
645                                        $password == $GLOBALS['phpgw_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?>
Note: See TracBrowser for help on using the repository browser.