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

Revision 5509, 11.0 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        class soap_parser
13        {
14                function soap_parser($xml='',$encoding='UTF-8')
15                {
16                        global $soapTypes;
17
18                        $this->soapTypes = $soapTypes;
19                        $this->xml = $xml;
20                        $this->xml_encoding = $encoding;
21                        $this->root_struct = "";
22                        // options: envelope,header,body,method
23                        // determines where in the message we are (envelope,header,body,method)
24                        $this->status = '';
25                        $this->position = 0;
26                        $this->pos_stat = 0;
27                        $this->depth = 0;
28                        $this->default_namespace = '';
29                        $this->namespaces = array();
30                        $this->message = array();
31                        $this->fault = false;
32                        $this->fault_code = '';
33                        $this->fault_str = '';
34                        $this->fault_detail = '';
35                        $this->eval_str = '';
36                        $this->depth_array = array();
37                        $this->debug_flag = True;
38                        $this->debug_str = '';
39                        $this->previous_element = '';
40
41                        $this->entities = array (
42                                '&' => '&amp;',
43                                '<' => '&lt;',
44                                '>' => '&gt;',
45                                "'" => '&apos;',
46                                '"' => '&quot;'
47                        );
48
49                        // Check whether content has been read.
50                        if(!empty($xml))
51                        {
52                                $this->debug('Entering soap_parser()');
53                                //$this->debug("DATA DUMP:\n\n$xml");
54                                // Create an XML parser.
55                                $this->parser = xml_parser_create($this->xml_encoding);
56                                // Set the options for parsing the XML data.
57                                //xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
58                                xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
59                                // Set the object for the parser.
60                                xml_set_object($this->parser, &$this);
61                                // Set the element handlers for the parser.
62                                xml_set_element_handler($this->parser, 'start_element','end_element');
63                                xml_set_character_data_handler($this->parser,'character_data');
64                                xml_set_default_handler($this->parser, 'default_handler');
65
66                                // Parse the XML file.
67                                if(!xml_parse($this->parser,$xml,true))
68                                {
69                                        // Display an error message.
70                                        $this->debug(sprintf("XML error on line %d: %s",
71                                                xml_get_current_line_number($this->parser),
72                                                xml_error_string(xml_get_error_code($this->parser))));
73                                        $this->fault = true;
74                                }
75                                else
76                                {
77                                        // get final eval string
78                                        $this->eval_str = "\$response = ".trim($this->build_eval($this->root_struct)).";";
79                                }
80                                xml_parser_free($this->parser);
81                        }
82                        else
83                        {
84                                $this->debug("xml was empty, didn't parse!");
85                        }
86                }
87
88                // loop through msg, building eval_str
89                function build_eval($pos)
90                {
91                        $this->debug("inside build_eval() for $pos: ".$this->message[$pos]["name"]);
92                        $eval_str = $this->message[$pos]['eval_str'];
93                        // loop through children, building...
94                        if($this->message[$pos]['children'] != '')
95                        {
96                                $this->debug('children string = '.$this->message[$pos]['children']);
97                                $children = explode('|',$this->message[$pos]['children']);
98                                $this->debug('it has '.count($children).' children');
99                                @reset($children);
100                                while(list($c,$child_pos) = @each($children))
101                                /* foreach($children as $c => $child_pos) */
102                                {
103                                        //$this->debug("child pos $child_pos: ".$this->message[$child_pos]["name"]);
104                                        if($this->message[$child_pos]['eval_str'] != '')
105                                        {
106                                                $this->debug('entering build_eval() for '.$this->message[$child_pos]['name'].", array pos $c, pos: $child_pos");
107                                                $eval_str .= $this->build_eval($child_pos).', ';
108                                        }
109                                }
110                                $eval_str = substr($eval_str,0,strlen($eval_str)-2);
111                        }
112                        // add current node's eval_str
113                        $eval_str .= $this->message[$pos]['end_eval_str'];
114                        return $eval_str;
115                }
116
117                // start-element handler
118                function start_element($parser, $name, $attrs)
119                {
120                        // position in a total number of elements, starting from 0
121                        // update class level pos
122                        $pos = $this->position++;
123                        // and set mine
124                        $this->message[$pos]['pos'] = $pos;
125
126                        // parent/child/depth determinations
127
128                        // depth = how many levels removed from root?
129                        // set mine as current global depth and increment global depth value
130                        $this->message[$pos]['depth'] = $this->depth++;
131
132                        // else add self as child to whoever the current parent is
133                        if($pos != 0)
134                        {
135                                $this->message[$this->parent]['children'] .= "|$pos";
136                        }
137                        // set my parent
138                        $this->message[$pos]['parent'] = $this->parent;
139                        // set self as current value for this depth
140                        $this->depth_array[$this->depth] = $pos;
141                        // set self as current parent
142                        $this->parent = $pos;
143
144                        // set status
145                        if(ereg(":Envelope$",$name))
146                        {
147                                $this->status = 'envelope';
148                        }
149                        elseif(ereg(":Header$",$name))
150                        {
151                                $this->status = 'header';
152                        }
153                        elseif(ereg(":Body$",$name))
154                        {
155                                $this->status = 'body';
156                        // set method
157                        }
158                        elseif($this->status == 'body')
159                        {
160                                $this->status = 'method';
161                                if(ereg(':',$name))
162                                {
163                                        $this->root_struct_name = substr(strrchr($name,':'),1);
164                                }
165                                else
166                                {
167                                        $this->root_struct_name = $name;
168                                }
169                                $this->root_struct = $pos;
170                                $this->message[$pos]['type'] = 'struct';
171                        }
172                        // set my status
173                        $this->message[$pos]['status'] = $this->status;
174
175                        // set name
176                        $this->message[$pos]['name'] = htmlspecialchars($name);
177                        // set attrs
178                        $this->message[$pos]['attrs'] = $attrs;
179                        // get namespace
180                        if(ereg(":",$name))
181                        {
182                                $namespace = substr($name,0,strpos($name,':'));
183                                $this->message[$pos]['namespace'] = $namespace;
184                                $this->default_namespace = $namespace;
185                        }
186                        else
187                        {
188                                $this->message[$pos]['namespace'] = $this->default_namespace;
189                        }
190                        // loop through atts, logging ns and type declarations
191                        @reset($attrs);
192                        while (list($key,$value) = @each($attrs))
193                        /* foreach($attrs as $key => $value) */
194                        {
195                                // if ns declarations, add to class level array of valid namespaces
196                                if(ereg('xmlns:',$key))
197                                {
198                                        $namespaces[substr(strrchr($key,':'),1)] = $value;
199                                        if($name == $this->root_struct_name)
200                                        {
201                                                $this->methodNamespace = $value;
202                                        }
203                                }
204                                // if it's a type declaration, set type
205                                elseif($key == 'xsi:type')
206                                {
207                                        // then get attname and set $type
208                                        $type = substr(strrchr($value,':'),1);
209                                }
210                        }
211
212                        // set type if available
213                        if($type)
214                        {
215                                $this->message[$pos]['type'] = $type;
216                        }
217
218                        // debug
219                        //$this->debug("parsed $name start, eval = '".$this->message[$pos]["eval_str"]."'");
220                }
221
222                // end-element handler
223                function end_element($parser, $name)
224                {
225                        // position of current element is equal to the last value left in depth_array for my depth
226                        $pos = $this->depth_array[$this->depth];
227                        // bring depth down a notch
228                        $this->depth--;
229               
230                        // get type if not set already
231                        if($this->message[$pos]['type'] == '')
232                        {
233//                              if($this->message[$pos]['cdata'] == '' && $this->message[$pos]['children'] != '')
234                                if($this->message[$pos]['children'] != '')
235                                {
236                                        $this->message[$pos]['type'] = 'SOAPStruct';
237                                }
238                                else
239                                {
240                                        $this->message[$pos]['type'] = 'string';
241                                }
242                        }
243
244                        // set eval str start if it has a valid type and is inside the method
245                        if($pos >= $this->root_struct)
246                        {
247                                $this->message[$pos]['eval_str'] .= "\n CreateObject(\"phpgwapi.soapval\",\"".htmlspecialchars($name)."\", \"".$this->message[$pos]["type"]."\" ";
248                                $this->message[$pos]['end_eval_str'] = ')';
249                                $this->message[$pos]['inval'] = 'true';
250                                /*
251                                if($this->message[$pos]["name"] == $this->root_struct_name){
252                                        $this->message[$pos]["end_eval_str"] .= " ,\"$this->methodNamespace\"";
253                                }
254                                */
255                                if($this->message[$pos]['children'] != '')
256                                {
257                                        $this->message[$pos]['eval_str'] .= ', array( ';
258                                        $this->message[$pos]['end_eval_str'] .= ' )';
259                                }
260                        }
261
262                        // if i have no children and have cdata...then i must be a scalar value, so add my data to the eval_str
263                        if($this->status == 'method' && $this->message[$pos]['children'] == '')
264                        {
265                                // add cdata w/ no quotes if only int/float/dbl
266                                if($this->message[$pos]['type'] == 'string')
267                                {
268                                        $this->message[$pos]['eval_str'] .= ", \"".$this->message[$pos]['cdata']."\"";
269                                }
270                                elseif($this->message[$pos]['type'] == 'int' || $this->message[$pos]['type'] == 'float' || $this->message[$pos]['type'] == 'double')
271                                {
272                                        //$this->debug("adding cdata w/o quotes");
273                                        $this->message[$pos]['eval_str'] .= ', '.trim($this->message[$pos]['cdata']);
274                                }
275                                elseif(is_string($this->message[$pos]['cdata']))
276                                {
277                                        //$this->debug("adding cdata w/ quotes");
278                                        $this->message[$pos]['eval_str'] .= ", \"".$this->message[$pos]['cdata']."\"";
279                                }
280                        }
281                        // if in the process of making a soap_val, close the parentheses and move on...
282                        if($this->message[$pos]['inval'] == 'true')
283                        {
284                                $this->message[$pos]['inval'] == 'false';
285                        }
286                        // if tag we are currently closing is the method wrapper
287                        if($pos == $this->root_struct)
288                        {
289                                $this->status = 'body';
290                        }
291                        elseif(ereg(':Body',$name))
292                        {
293                                $this->status = 'header';
294                        }
295                        elseif(ereg(':Header',$name))
296                        {
297                                $this->status = 'envelope';
298                        }
299                        // set parent back to my parent
300                        $this->parent = $this->message[$pos]['parent'];
301                        $this->debug("parsed $name end, type '".$this->message[$pos]['type']."'eval_str = '".trim($this->message[$pos]['eval_str'])."' and children = ".$this->message[$pos]['children']);
302                }
303
304                // element content handler
305                function character_data($parser, $data)
306                {
307                        $pos = $this->depth_array[$this->depth];
308                        $this->message[$pos]['cdata'] .= $data;
309                        //$this->debug("parsed ".$this->message[$pos]["name"]." cdata, eval = '$this->eval_str'");
310                }
311
312                // default handler
313                function default_handler($parser, $data)
314                {
315                        //$this->debug("DEFAULT HANDLER: $data");
316                }
317
318                // function to get fault code
319                function fault()
320                {
321                        if($this->fault)
322                        {
323                                return true;
324                        }
325                        else
326                        {
327                                return false;
328                        }
329                }
330
331                // have this return a soap_val object
332                function get_response()
333                {
334                        $this->debug("eval()ing eval_str: $this->eval_str");
335                        @eval("$this->eval_str");
336                        if($response)
337                        {
338                                $this->debug("successfully eval'd msg");
339                                return $response;
340                        }
341                        else
342                        {
343                                $this->debug('ERROR: did not successfully eval the msg');
344                                $this->fault = true;
345                                return CreateObject('phpgwapi.soapval',
346                                        'Fault',
347                                        'struct',
348                                        array(
349                                                CreateObject('phpgwapi.soapval',
350                                                        'faultcode',
351                                                        'string',
352                                                        'SOAP-ENV:Server'
353                                                ),
354                                                CreateObject('phpgwapi.soapval',
355                                                        'faultstring',
356                                                        'string',
357                                                        "couldn't eval \"$this->eval_str\""
358                                                )
359                                        )
360                                );
361                        }
362                }
363
364                function debug($string)
365                {
366                        if($this->debug_flag)
367                        {
368                                $this->debug_str .= "$string\n";
369                        }
370                }
371
372                function decode_entities($text)
373                {
374                        @reset($this->entities);
375                        while(list($entity,$encoded) = @each($this->entities))
376                        /* foreach($this->entities as $entity => $encoded) */
377                        {
378                                $text = str_replace($encoded,$entity,$text);
379                        }
380                        return $text;
381                }
382        }
383?>
Note: See TracBrowser for help on using the repository browser.