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

Revision 5509, 11.2 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/*
13
14        SOAPx4
15        by Dietrich Ayala (C) 2001 dietrich@ganx4.com
16
17        This project began based on code from the 2 projects below,
18        and still contains some original code. The licenses of both must be respected.
19
20        XML-RPC for PHP
21        originally by Edd Dumbill (C) 1999-2000
22
23        SOAP for PHP
24        by Victor Zou (C) 2000-2001 <victor@gigaideas.com.cn>
25
26*/
27
28        /*  changelog:
29        2001-07-04
30        - abstract type system to support either 1999 or 2001 schema (arg, typing still needs much
31        solidification.)
32        - implemented proxy support, based on sample code from miles lott <milos@speakeasy.net>
33        - much general cleanup of code & cleaned out what was left of original xml-rpc/gigaideas code
34        - implemented a transport argument into send() that allows you to specify different transports
35        (assuming you have implemented the function, and added it to the conditional statement in send()
36        - abstracted the determination of charset in Content-type header
37        2001-07-5
38        - fixed more weird type/namespace issues
39        */
40
41        // $path can be a complete endpoint url, with the other parameters left blank:
42        // $soap_client = new soap_client("http://path/to/soap/server");
43
44
45        // soap value object
46        class soapval
47        {
48        //      function soapval($name='',$type=False,$value=-1,$namespace=False,$type_namespace=False)
49                function soapval($name='',$type=False,$value=0,$namespace=False,$type_namespace=False)
50                {
51                        // detect type if not passed
52                        if(!$type)
53                        {
54                                if(is_array($value) && count($value) >= 1)
55                                {
56                                        if(ereg("[a-zA-Z0-9\-]*",key($v)))
57                                        {
58                                                $type = 'struct';
59                                        }
60                                        else
61                                        {
62                                                $type = 'array';
63                                        }
64                                }
65                                elseif(is_int($v))
66                                {
67                                        $type = 'int';
68                                }
69                                elseif(is_float($v) || $v == 'NaN' || $v == 'INF')
70                                {
71                                        $type = 'float';
72                                }
73                                else
74                                {
75                                        $type = gettype($value);
76                                }
77                        }
78                        // php type name mangle
79                        if($type == 'integer')
80                        {
81                                $type = 'int';
82                        }
83
84                        $this->soapTypes = $GLOBALS['soapTypes'];
85                        $this->name = $name;
86                        $this->value = '';
87                        $this->type = $type;
88                        $this->type_code = 0;
89                        $this->type_prefix = false;
90                        $this->array_type = '';
91                        $this->debug_flag = False;
92                        $this->debug_str = '';
93                        $this->debug("Entering soapval - name: '$name' type: '$type'");
94
95                        if($namespace)
96                        {
97                                $this->namespace = $namespace;
98                                if(!isset($GLOBALS['namespaces'][$namespace]))
99                                {
100                                        $GLOBALS['namespaces'][$namespace] = "ns".(count($GLOBALS['namespaces'])+1);
101                                }
102                                $this->prefix = $GLOBALS['namespaces'][$namespace];
103                        }
104
105                        // get type prefix
106                        if(ereg(":",$type))
107                        {
108                                $this->type = substr(strrchr($type,':'),1,strlen(strrchr($type,':')));
109                                $this->type_prefix = substr($type,0,strpos($type,':'));
110                        }
111                        elseif($type_namespace)
112                        {
113                                if(!isset($GLOBALS['namespaces'][$type_namespace]))
114                                {
115                                        $GLOBALS['namespaces'][$type_namespace] = 'ns'.(count($GLOBALS['namespaces'])+1);
116                                }
117                                $this->type_prefix = $GLOBALS['namespaces'][$type_namespace];
118                        }
119
120                        // if type namespace was not explicitly passed, and we're not in a method struct:
121                        elseif(!$this->type_prefix && !isset($this->namespace))
122                        {
123                                // try to get type prefix from typeMap
124                                if(!$this->type_prefix = $this->verify_type($type))
125                                {
126                                        // else default to method namespace
127                                        $this->type_prefix = $GLOBALS['namespaces'][$GLOBALS['methodNamespace']];
128                                }
129                        }
130
131                        // if scalar
132                        if($this->soapTypes[$this->type] == 1)
133                        {
134                                $this->type_code = 1;
135                                $this->addScalar($value,$this->type,$name);
136                        // if array
137                        }
138                        elseif($this->soapTypes[$this->type] == 2)
139                        {
140                                $this->type_code = 2;
141                                $this->addArray($value);
142                        // if struct
143                        }
144                        elseif($this->soapTypes[$this->type] == 3)
145                        {
146                                $this->type_code = 3;
147                                $this->addStruct($value);
148                        }
149                        else
150                        {
151                                //if($namespace == $GLOBALS['methodNamespace']){
152                                        $this->type_code = 3;
153                                        $this->addStruct($value);
154                                //}
155                        }
156                }
157
158                function addScalar($value, $type, $name="")
159                {
160                        $this->debug("adding scalar '$name' of type '$type'");
161                       
162                        // if boolean, change value to 1 or 0
163                        if ($type == "boolean")
164                        {
165                                if((strcasecmp($value,"true") == 0) || ($value == 1))
166                                {
167                                        $value = 1;
168                                }
169                                else
170                                {
171                                        $value = 0;
172                                }
173                        }
174
175                        $this->value = $value;
176                        return true;
177                }
178
179                function addArray($vals)
180                {
181                        $this->debug("adding array '$this->name' with ".count($vals)." vals");
182                        $this->value = array();
183                        if(is_array($vals) && count($vals) >= 1)
184                        {
185                                @reset($vals);
186                                while(list($k,$v) = @each($vals))
187                                /* foreach($vals as $k => $v) */
188                                {
189                                        $this->debug("checking value $k : $v");
190                                        // if soapval, add..
191                                        if(get_class($v) == 'soapval')
192                                        {
193                                                $this->value[] = $v;
194                                                $this->debug($v->debug_str);
195                                        // else make obj and serialize
196                                        }
197                                        else
198                                        {
199                                                if(is_array($v))
200                                                {
201                                                        if(ereg("[a-zA-Z\-]*",key($v)))
202                                                        {
203                                                                $type = 'struct';
204                                                        }
205                                                        else
206                                                        {
207                                                                $type = 'array';
208                                                        }
209                                                }
210                                                elseif(!ereg("^[0-9]*$",$k) && in_array($k,array_keys($this->soapTypes)))
211                                                {
212                                                        $type = $k;
213                                                }
214                                                elseif(is_int($v))
215                                                {
216                                                        $type = 'int';
217                                                }
218                                                elseif(is_float($v) || $v == 'NaN' || $v == 'INF')
219                                                {
220                                                        $type = 'float';
221                                                }
222                                                else
223                                                {
224                                                        $type = gettype($v);
225                                                }
226                                                $new_val = CreateObject('phpgwapi.soapval','item',$type,$v);
227                                                $this->debug($new_val->debug_str);
228                                                $this->value[] = $new_val;
229                                        }
230                                }
231                        }
232                        return true;
233                }
234
235                function addStruct($vals)
236                {
237                        $this->debug("adding struct '$this->name' with ".count($vals).' vals');
238                        if(is_array($vals) && count($vals) >= 1)
239                        {
240                                @reset($vals);
241                                while(list($k,$v) = @each($vals))
242                                /* foreach($vals as $k => $v) */
243                                {
244                                        // if serialize, if soapval
245                                        if(get_class($v) == 'soapval')
246                                        {
247                                                $this->value[] = $v;
248                                                $this->debug($v->debug_str);
249                                        // else make obj and serialize
250                                        }
251                                        else
252                                        {
253                                                if(is_array($v))
254                                                {
255                                                        @reset($v);
256                                                        while(list($a,$b) = @each($v))
257                                                        /* foreach($v as $a => $b) */
258                                                        {
259                                                                if($a == "0")
260                                                                {
261                                                                        $type = 'array';
262                                                                }
263                                                                else
264                                                                {
265                                                                        $type = 'struct';
266                                                                }
267                                                                break;
268                                                        }
269                                                }
270//                                              elseif(is_array($k) && in_array($k,array_keys($this->soapTypes)))
271                                                elseif(is_array($k,in_array($k,array_keys($this->soapTypes))))
272                                                {
273                                                        $this->debug("got type '$type' for value '$v' from soapTypes array!");
274                                                        $type = $k;
275                                                }
276                                                elseif(is_int($v))
277                                                {
278                                                        $type = 'int';
279                                                }
280                                                elseif(is_float($v) || $v == "NaN" || $v == "INF")
281                                                {
282                                                        $type = 'float';
283                                                }
284                                                else
285                                                {
286                                                        $type = gettype($v);
287                                                        $this->debug("got type '$type' for value '$v' from php gettype()!");
288                                                }
289                                                $new_val = CreateObject('phpgwapi.soapval',$k,$type,$v);
290                                                $this->debug($new_val->debug_str);
291                                                $this->value[] = $new_val;
292                                        }
293                                }
294                        }
295                        else
296                        {
297                                $this->value = array();
298                        }
299                        return true;
300                }
301
302                // turn soapvals into xml, woohoo!
303                function serializeval($soapval=false)
304                {
305                        if(!$soapval)
306                        {
307                                $soapval = $this;
308                        }
309                        $this->debug("serializing '$soapval->name' of type '$soapval->type'");
310                        if($soapval->name == '')
311                        {
312                                $soapval->name = 'return';
313                        }
314
315                        switch($soapval->type_code)
316                        {
317                                case 3:
318                                        // struct
319                                        $this->debug('got a struct');
320                                        if($soapval->prefix && $soapval->type_prefix)
321                                        {
322                                                $xml .= "<$soapval->prefix:$soapval->name xsi:type=\"$soapval->type_prefix:$soapval->type\">\n";
323                                        }
324                                        elseif($soapval->type_prefix)
325                                        {
326                                                $xml .= "<$soapval->name xsi:type=\"$soapval->type_prefix:$soapval->type\">\n";
327                                        }
328                                        elseif($soapval->prefix)
329                                        {
330                                                $xml .= "<$soapval->prefix:$soapval->name>\n";
331                                        }
332                                        else
333                                        {
334                                                $xml .= "<$soapval->name>\n";
335                                        }
336                                        if(is_array($soapval->value))
337                                        {
338                                                @reset($soapval->value);
339                                                while(list($k,$v) = @each($soapval->value))
340                                                /* foreach($soapval->value as $k => $v) */
341                                                {
342                                                        $xml .= $this->serializeval($v);
343                                                }
344                                        }
345                                        if($soapval->prefix)
346                                        {
347                                                $xml .= "</$soapval->prefix:$soapval->name>\n";
348                                        }
349                                        else
350                                        {
351                                                $xml .= "</$soapval->name>\n";
352                                        }
353                                        break;
354                                case 2:
355                                        // array
356                                        @reset($soapval->value);
357                                        while(list($null,$array_val) = @each($soapval->value))
358                                        /* foreach($soapval->value as $array_val) */
359                                        {
360                                                $array_types[$array_val->type] = 1;
361                                                $xml .= $this->serializeval($array_val);
362                                        }
363                                        if(count($array_types) > 1)
364                                        {
365                                                $array_type = 'xsd:ur-type';
366                                        }
367                                        elseif(count($array_types) >= 1)
368                                        {
369                                                $array_type = $array_val->type_prefix.":".$array_val->type;
370                                        }
371
372                                        $xml = "<$soapval->name xsi:type=\"SOAP-ENC:Array\" SOAP-ENC:arrayType=\"".$array_type."[".sizeof($soapval->value)."]\">\n".$xml."</$soapval->name>\n";
373                                        break;
374                                case 1:
375                                        $xml .= "<$soapval->name xsi:type=\"$soapval->type_prefix:$soapval->type\">$soapval->value</$soapval->name>\n";
376                                        break;
377                                default:
378                                        break;
379                        }
380                        return $xml;
381                }
382
383                // serialize
384                function serialize()
385                {
386                        return $this->serializeval($this);
387                }
388
389                function decode($soapval=false)
390                {
391                        if(!$soapval)
392                        {
393                                $soapval = $this;
394                        }
395                        // scalar decode
396                        if($soapval->type_code == 1)
397                        {
398                                return $soapval->value;
399                        // array decode
400                        }
401                        elseif($soapval->type_code == 2)
402                        {
403                                if(is_array($soapval->value))
404                                {
405                                        @reset($soapval->value);
406                                        while(list($null,$item) = @each($soapval->value))
407                                        /* foreach($soapval->value as $item) */
408                                        {
409                                                $return[] = $this->decode($item);
410                                        }
411                                        return $return;
412                                }
413                                else
414                                {
415                                        return array();
416                                }
417                        // struct decode
418                        }
419                        elseif($soapval->type_code == 3)
420                        {
421                                if(is_array($soapval->value))
422                                {
423                                        @reset($soapval->value);
424                                        while(list($null,$item) = @each($soapval->value))
425                                        /* foreach($soapval->value as $item) */
426                                        {
427                                                $return[$item->name] = $this->decode($item);
428                                        }
429                                        return $return;
430                                }
431                                else
432                                {
433                                        return array();
434                                }
435                        }
436                }
437
438                // verify type
439                function verify_type($type)
440                {
441                        if ($type)
442                        {
443//                              global $GLOBALS['namespaces'],$GLOBALS['soapTypes'],$GLOBALS['typemap'];
444//                              global $GLOBALS['namespaces'],$GLOBALS['typemap'];
445
446                                @reset($GLOBALS['typemap']);
447                                while(list($namespace,$types) = @each($GLOBALS['typemap']))
448                                /* foreach($GLOBALS['typemap'] as $namespace => $types) */
449                                {
450                                        if(in_array($type,$types))
451                                        {
452                                                return $GLOBALS['namespaces'][$namespace];
453                                        }
454                                }
455                        }
456                        return false;
457                }
458
459                // alias for verify_type() - pass it a type, and it returns it's prefix
460                function get_prefix($type)
461                {
462                        if($prefix = $this->verify_type($type))
463                        {
464                                return $prefix;
465                        }
466                        return false;
467                }
468
469                function debug($string)
470                {
471                        if($this->debug_flag)
472                        {
473                                $this->debug_str .= "$string\n";
474                        }
475                }
476        }
477?>
Note: See TracBrowser for help on using the repository browser.