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