1 | <?php |
---|
2 | |
---|
3 | /*************************************************************************** |
---|
4 | * Expresso Livre * |
---|
5 | * http://www.expressolivre.org * |
---|
6 | * -------------------------------------------- * |
---|
7 | * This program is free software; you can redistribute it and/or modify it * |
---|
8 | * under the terms of the GNU General Public License as published by the * |
---|
9 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
10 | * option) any later version. * |
---|
11 | \**************************************************************************/ |
---|
12 | // by Edd Dumbill (C) 1999-2001 |
---|
13 | // <edd@usefulinc.com> |
---|
14 | // xmlrpc.inc,v 1.18 2001/07/06 18:23:57 edmundd |
---|
15 | |
---|
16 | // License is granted to use or modify this software ("XML-RPC for PHP") |
---|
17 | // for commercial or non-commercial use provided the copyright of the author |
---|
18 | // is preserved in any distributed or derivative work. |
---|
19 | |
---|
20 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED OR |
---|
21 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
22 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
23 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
24 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
25 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
29 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
30 | |
---|
31 | class xmlrpcval |
---|
32 | { |
---|
33 | var $me = array(); |
---|
34 | var $mytype = 0; |
---|
35 | |
---|
36 | function xmlrpcval($val = -1, $type = '') |
---|
37 | { |
---|
38 | $this->me = array(); |
---|
39 | $this->mytype = 0; |
---|
40 | |
---|
41 | if ($val != -1 || $type != '') |
---|
42 | { |
---|
43 | if ($type=='') |
---|
44 | { |
---|
45 | $type='string'; |
---|
46 | } |
---|
47 | if ($GLOBALS['xmlrpcTypes'][$type]==1) |
---|
48 | { |
---|
49 | $this->addScalar($val,$type); |
---|
50 | } |
---|
51 | elseif ($GLOBALS['xmlrpcTypes'][$type]==2) |
---|
52 | { |
---|
53 | $this->addArray($val); |
---|
54 | } |
---|
55 | elseif ($GLOBALS['xmlrpcTypes'][$type]==3) |
---|
56 | { |
---|
57 | $this->addStruct($val); |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | function addScalar($val, $type='string') |
---|
63 | { |
---|
64 | if ($this->mytype==1) |
---|
65 | { |
---|
66 | echo '<B>xmlrpcval</B>: scalar can have only one value<BR>'; |
---|
67 | return 0; |
---|
68 | } |
---|
69 | $typeof=$GLOBALS['xmlrpcTypes'][$type]; |
---|
70 | if ($typeof!=1) |
---|
71 | { |
---|
72 | echo '<B>xmlrpcval</B>: not a scalar type ('.$typeof.')<BR>'; |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | if ($type==xmlrpcBoolean) |
---|
77 | { |
---|
78 | if (strcasecmp($val,'true')==0 || |
---|
79 | $val==1 || ($val==true && |
---|
80 | strcasecmp($val,'false'))) |
---|
81 | { |
---|
82 | $val=1; |
---|
83 | } |
---|
84 | else |
---|
85 | { |
---|
86 | $val=0; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | if ($this->mytype==2) |
---|
91 | { |
---|
92 | // we're adding to an array here |
---|
93 | $ar=$this->me['array']; |
---|
94 | $ar[] = CreateObject('phpgwapi.xmlrpcval',$val, $type); |
---|
95 | $this->me['array']=$ar; |
---|
96 | } |
---|
97 | else |
---|
98 | { |
---|
99 | // a scalar, so set the value and remember we're scalar |
---|
100 | $this->me[$type]=$val; |
---|
101 | $this->mytype=$typeof; |
---|
102 | } |
---|
103 | return 1; |
---|
104 | } |
---|
105 | |
---|
106 | function addArray($vals) |
---|
107 | { |
---|
108 | if ($this->mytype!=0) |
---|
109 | { |
---|
110 | echo '<B>xmlrpcval</B>: already initialized as a [' . $this->kindOf() . ']<BR>'; |
---|
111 | return 0; |
---|
112 | } |
---|
113 | |
---|
114 | $this->mytype=$GLOBALS['xmlrpcTypes']['array']; |
---|
115 | $this->me['array']=$vals; |
---|
116 | return 1; |
---|
117 | } |
---|
118 | |
---|
119 | function addStruct($vals) |
---|
120 | { |
---|
121 | // global $xmlrpcTypes; |
---|
122 | if ($this->mytype!=0) |
---|
123 | { |
---|
124 | echo '<B>xmlrpcval</B>: already initialized as a [' . $this->kindOf() . ']<BR>'; |
---|
125 | return 0; |
---|
126 | } |
---|
127 | $this->mytype=$GLOBALS['xmlrpcTypes']['struct']; |
---|
128 | $this->me['struct']=$vals; |
---|
129 | return 1; |
---|
130 | } |
---|
131 | |
---|
132 | function dump($ar) |
---|
133 | { |
---|
134 | reset($ar); |
---|
135 | while (list($key,$val) = each($ar)) |
---|
136 | { |
---|
137 | echo $key.' => '.$val.'<br>'; |
---|
138 | if ($key == 'array') |
---|
139 | { |
---|
140 | while (list($key2,$val2) = each($val)) |
---|
141 | { |
---|
142 | echo '-- '.$key2.' => '.$val2.'<br>'; |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | function kindOf() |
---|
149 | { |
---|
150 | switch($this->mytype) |
---|
151 | { |
---|
152 | case 3: |
---|
153 | return 'struct'; |
---|
154 | break; |
---|
155 | case 2: |
---|
156 | return 'array'; |
---|
157 | break; |
---|
158 | case 1: |
---|
159 | return 'scalar'; |
---|
160 | break; |
---|
161 | default: |
---|
162 | return 'undef'; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | function serializedata($typ, $val) |
---|
167 | { |
---|
168 | $rs=''; |
---|
169 | if($typ) |
---|
170 | { |
---|
171 | switch($GLOBALS['xmlrpcTypes'][$typ]) |
---|
172 | { |
---|
173 | case 3: |
---|
174 | // struct |
---|
175 | $rs .= '<struct>'."\n"; |
---|
176 | reset($val); |
---|
177 | while(list($key2, $val2)=each($val)) |
---|
178 | { |
---|
179 | $rs .= '<member><name>'.$key2.'</name>'."\n".$this->serializeval($val2).'</member>'."\n"; |
---|
180 | } |
---|
181 | $rs .= '</struct>'; |
---|
182 | break; |
---|
183 | case 2: |
---|
184 | // array |
---|
185 | $rs .= '<array>'."\n".'<data>'."\n"; |
---|
186 | for($i=0; $i<sizeof($val); $i++) |
---|
187 | { |
---|
188 | $rs .= $this->serializeval($val[$i]); |
---|
189 | } |
---|
190 | $rs .= '</data>'."\n".'</array>'; |
---|
191 | break; |
---|
192 | case 1: |
---|
193 | $rs .= '<'.$typ.'>'; |
---|
194 | switch ($typ) |
---|
195 | { |
---|
196 | case xmlrpcBase64: |
---|
197 | $rs.= base64_encode($val); |
---|
198 | break; |
---|
199 | case xmlrpcBoolean: |
---|
200 | $rs.= ($val ? '1' : '0'); |
---|
201 | break; |
---|
202 | case xmlrpcString: |
---|
203 | $rs.= htmlspecialchars($val); |
---|
204 | break; |
---|
205 | default: |
---|
206 | $rs.= $val; |
---|
207 | } |
---|
208 | $rs .= '</'.$typ.'>'; |
---|
209 | break; |
---|
210 | default: |
---|
211 | break; |
---|
212 | } |
---|
213 | } |
---|
214 | return $rs; |
---|
215 | } |
---|
216 | |
---|
217 | function serialize() |
---|
218 | { |
---|
219 | return $this->serializeval($this); |
---|
220 | } |
---|
221 | |
---|
222 | function serializeval($o) |
---|
223 | { |
---|
224 | $rs=''; |
---|
225 | $ar=$o->me; |
---|
226 | reset($ar); |
---|
227 | list($typ, $val) = each($ar); |
---|
228 | $rs.='<value>'; |
---|
229 | $rs.= @$this->serializedata($typ, $val); |
---|
230 | $rs.='</value>'."\n"; |
---|
231 | return $rs; |
---|
232 | } |
---|
233 | |
---|
234 | function structmem($m) |
---|
235 | { |
---|
236 | $nv=$this->me['struct'][$m]; |
---|
237 | return $nv; |
---|
238 | } |
---|
239 | |
---|
240 | function structreset() |
---|
241 | { |
---|
242 | reset($this->me['struct']); |
---|
243 | } |
---|
244 | |
---|
245 | function structeach() |
---|
246 | { |
---|
247 | return each($this->me['struct']); |
---|
248 | } |
---|
249 | |
---|
250 | function getval() |
---|
251 | { |
---|
252 | // UNSTABLE |
---|
253 | reset($this->me); |
---|
254 | list($a,$b)=each($this->me); |
---|
255 | // contributed by I Sofer, 2001-03-24 |
---|
256 | // add support for nested arrays to scalarval |
---|
257 | // i've created a new method here, so as to |
---|
258 | // preserve back compatibility |
---|
259 | |
---|
260 | if (is_array($b)) |
---|
261 | { |
---|
262 | @reset($b); |
---|
263 | while(list($id,$cont) = @each($b)) |
---|
264 | { |
---|
265 | $b[$id] = $cont->scalarval(); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | // add support for structures directly encoding php objects |
---|
270 | if (is_object($b)) |
---|
271 | { |
---|
272 | $t = get_object_vars($b); |
---|
273 | @reset($t); |
---|
274 | while(list($id,$cont) = @each($t)) |
---|
275 | { |
---|
276 | $t[$id] = $cont->scalarval(); |
---|
277 | } |
---|
278 | @reset($t); |
---|
279 | while(list($id,$cont) = @each($t)) |
---|
280 | { |
---|
281 | eval('$b->'.$id.' = $cont;'); |
---|
282 | } |
---|
283 | } |
---|
284 | // end contrib |
---|
285 | return $b; |
---|
286 | } |
---|
287 | |
---|
288 | function scalarval() |
---|
289 | { |
---|
290 | reset($this->me); |
---|
291 | list($a,$b)=each($this->me); |
---|
292 | return $b; |
---|
293 | } |
---|
294 | |
---|
295 | function scalartyp() |
---|
296 | { |
---|
297 | reset($this->me); |
---|
298 | list($a,$b)=each($this->me); |
---|
299 | if ($a==xmlrpcI4) |
---|
300 | { |
---|
301 | $a=xmlrpcInt; |
---|
302 | } |
---|
303 | return $a; |
---|
304 | } |
---|
305 | |
---|
306 | function arraymem($m) |
---|
307 | { |
---|
308 | $nv=@$this->me['array'][$m]; |
---|
309 | return $nv; |
---|
310 | } |
---|
311 | |
---|
312 | function arraysize() |
---|
313 | { |
---|
314 | reset($this->me); |
---|
315 | list($a,$b)=each($this->me); |
---|
316 | return sizeof($b); |
---|
317 | } |
---|
318 | } |
---|
319 | ?> |
---|