00001 <?php 00002 00003 /* 00004 $Id: class.nusoap_base.php,v 1.55 2008/03/14 20:52:07 snichol Exp $ 00005 00006 NuSOAP - Web Services Toolkit for PHP 00007 00008 Copyright (c) 2002 NuSphere Corporation 00009 00010 This library is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU Lesser General Public 00012 License as published by the Free Software Foundation; either 00013 version 2.1 of the License, or (at your option) any later version. 00014 00015 This library is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 Lesser General Public License for more details. 00019 00020 You should have received a copy of the GNU Lesser General Public 00021 License along with this library; if not, write to the Free Software 00022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 00024 The NuSOAP project home is: 00025 http://sourceforge.net/projects/nusoap/ 00026 00027 The primary support for NuSOAP is the Help forum on the project home page. 00028 00029 If you have any questions or comments, please email: 00030 00031 Dietrich Ayala 00032 dietrich@ganx4.com 00033 http://dietrich.ganx4.com/nusoap 00034 00035 NuSphere Corporation 00036 http://www.nusphere.com 00037 00038 */ 00039 00040 /* 00041 * Some of the standards implmented in whole or part by NuSOAP: 00042 * 00043 * SOAP 1.1 (http://www.w3.org/TR/2000/NOTE-SOAP-20000508/) 00044 * WSDL 1.1 (http://www.w3.org/TR/2001/NOTE-wsdl-20010315) 00045 * SOAP Messages With Attachments (http://www.w3.org/TR/SOAP-attachments) 00046 * XML 1.0 (http://www.w3.org/TR/2006/REC-xml-20060816/) 00047 * Namespaces in XML 1.0 (http://www.w3.org/TR/2006/REC-xml-names-20060816/) 00048 * XML Schema 1.0 (http://www.w3.org/TR/xmlschema-0/) 00049 * RFC 2045 Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies 00050 * RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1 00051 * RFC 2617 HTTP Authentication: Basic and Digest Access Authentication 00052 */ 00053 00054 /* load classes 00055 00056 // necessary classes 00057 require_once('class.soapclient.php'); 00058 require_once('class.soap_val.php'); 00059 require_once('class.soap_parser.php'); 00060 require_once('class.soap_fault.php'); 00061 00062 // transport classes 00063 require_once('class.soap_transport_http.php'); 00064 00065 // optional add-on classes 00066 require_once('class.xmlschema.php'); 00067 require_once('class.wsdl.php'); 00068 00069 // server class 00070 require_once('class.soap_server.php');*/ 00071 00072 // class variable emulation 00073 // cf. http://www.webkreator.com/php/techniques/php-static-class-variables.html 00074 $GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'] = 9; 00075 00085 class nusoap_base { 00092 var $title = 'NuSOAP'; 00099 var $version = '1.0rc1'; 00106 var $revision = '$Revision: 1.55 $'; 00113 var $error_str = ''; 00120 var $debug_str = ''; 00128 var $charencoding = true; 00135 var $debugLevel; 00136 00143 var $XMLSchemaVersion = 'http://www.w3.org/2001/XMLSchema'; 00144 00151 var $soap_defencoding = 'ISO-8859-1'; 00152 //var $soap_defencoding = 'UTF-8'; 00153 00162 var $namespaces = array( 00163 'SOAP-ENV' => 'http://schemas.xmlsoap.org/soap/envelope/', 00164 'xsd' => 'http://www.w3.org/2001/XMLSchema', 00165 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 00166 'SOAP-ENC' => 'http://schemas.xmlsoap.org/soap/encoding/' 00167 ); 00168 00175 var $usedNamespaces = array(); 00176 00184 var $typemap = array( 00185 'http://www.w3.org/2001/XMLSchema' => array( 00186 'string'=>'string','boolean'=>'boolean','float'=>'double','double'=>'double','decimal'=>'double', 00187 'duration'=>'','dateTime'=>'string','time'=>'string','date'=>'string','gYearMonth'=>'', 00188 'gYear'=>'','gMonthDay'=>'','gDay'=>'','gMonth'=>'','hexBinary'=>'string','base64Binary'=>'string', 00189 // abstract "any" types 00190 'anyType'=>'string','anySimpleType'=>'string', 00191 // derived datatypes 00192 'normalizedString'=>'string','token'=>'string','language'=>'','NMTOKEN'=>'','NMTOKENS'=>'','Name'=>'','NCName'=>'','ID'=>'', 00193 'IDREF'=>'','IDREFS'=>'','ENTITY'=>'','ENTITIES'=>'','integer'=>'integer','nonPositiveInteger'=>'integer', 00194 'negativeInteger'=>'integer','long'=>'integer','int'=>'integer','short'=>'integer','byte'=>'integer','nonNegativeInteger'=>'integer', 00195 'unsignedLong'=>'','unsignedInt'=>'','unsignedShort'=>'','unsignedByte'=>'','positiveInteger'=>''), 00196 'http://www.w3.org/2000/10/XMLSchema' => array( 00197 'i4'=>'','int'=>'integer','boolean'=>'boolean','string'=>'string','double'=>'double', 00198 'float'=>'double','dateTime'=>'string', 00199 'timeInstant'=>'string','base64Binary'=>'string','base64'=>'string','ur-type'=>'array'), 00200 'http://www.w3.org/1999/XMLSchema' => array( 00201 'i4'=>'','int'=>'integer','boolean'=>'boolean','string'=>'string','double'=>'double', 00202 'float'=>'double','dateTime'=>'string', 00203 'timeInstant'=>'string','base64Binary'=>'string','base64'=>'string','ur-type'=>'array'), 00204 'http://soapinterop.org/xsd' => array('SOAPStruct'=>'struct'), 00205 'http://schemas.xmlsoap.org/soap/encoding/' => array('base64'=>'string','array'=>'array','Array'=>'array'), 00206 'http://xml.apache.org/xml-soap' => array('Map') 00207 ); 00208 00217 var $xmlEntities = array('quot' => '"','amp' => '&', 00218 'lt' => '<','gt' => '>','apos' => "'"); 00219 00225 function nusoap_base() { 00226 $this->debugLevel = $GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel']; 00227 } 00228 00235 function getGlobalDebugLevel() { 00236 return $GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel']; 00237 } 00238 00245 function setGlobalDebugLevel($level) { 00246 $GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'] = $level; 00247 } 00248 00255 function getDebugLevel() { 00256 return $this->debugLevel; 00257 } 00258 00265 function setDebugLevel($level) { 00266 $this->debugLevel = $level; 00267 } 00268 00275 function debug($string){ 00276 if ($this->debugLevel > 0) { 00277 $this->appendDebug($this->getmicrotime().' '.get_class($this).": $string\n"); 00278 } 00279 } 00280 00287 function appendDebug($string){ 00288 if ($this->debugLevel > 0) { 00289 // it would be nice to use a memory stream here to use 00290 // memory more efficiently 00291 $this->debug_str .= $string; 00292 } 00293 } 00294 00300 function clearDebug() { 00301 // it would be nice to use a memory stream here to use 00302 // memory more efficiently 00303 $this->debug_str = ''; 00304 } 00305 00312 function &getDebug() { 00313 // it would be nice to use a memory stream here to use 00314 // memory more efficiently 00315 return $this->debug_str; 00316 } 00317 00325 function &getDebugAsXMLComment() { 00326 // it would be nice to use a memory stream here to use 00327 // memory more efficiently 00328 while (strpos($this->debug_str, '--')) { 00329 $this->debug_str = str_replace('--', '- -', $this->debug_str); 00330 } 00331 $ret = "<!--\n" . $this->debug_str . "\n-->"; 00332 return $ret; 00333 } 00334 00341 function expandEntities($val) { 00342 if ($this->charencoding) { 00343 $val = str_replace('&', '&', $val); 00344 $val = str_replace("'", ''', $val); 00345 $val = str_replace('"', '"', $val); 00346 $val = str_replace('<', '<', $val); 00347 $val = str_replace('>', '>', $val); 00348 } 00349 return $val; 00350 } 00351 00358 function getError(){ 00359 if($this->error_str != ''){ 00360 return $this->error_str; 00361 } 00362 return false; 00363 } 00364 00371 function setError($str){ 00372 $this->error_str = $str; 00373 } 00374 00382 function isArraySimpleOrStruct($val) { 00383 $keyList = array_keys($val); 00384 foreach ($keyList as $keyListValue) { 00385 if (!is_int($keyListValue)) { 00386 return 'arrayStruct'; 00387 } 00388 } 00389 return 'arraySimple'; 00390 } 00391 00407 function serialize_val($val,$name=false,$type=false,$name_ns=false,$type_ns=false,$attributes=false,$use='encoded',$soapval=false) { 00408 $this->debug("in serialize_val: name=$name, type=$type, name_ns=$name_ns, type_ns=$type_ns, use=$use, soapval=$soapval"); 00409 $this->appendDebug('value=' . $this->varDump($val)); 00410 $this->appendDebug('attributes=' . $this->varDump($attributes)); 00411 00412 if (is_object($val) && get_class($val) == 'soapval' && (! $soapval)) { 00413 $this->debug("serialize_val: serialize soapval"); 00414 $xml = $val->serialize($use); 00415 $this->appendDebug($val->getDebug()); 00416 $val->clearDebug(); 00417 $this->debug("serialize_val of soapval returning $xml"); 00418 return $xml; 00419 } 00420 // force valid name if necessary 00421 if (is_numeric($name)) { 00422 $name = '__numeric_' . $name; 00423 } elseif (! $name) { 00424 $name = 'noname'; 00425 } 00426 // if name has ns, add ns prefix to name 00427 $xmlns = ''; 00428 if($name_ns){ 00429 $prefix = 'nu'.rand(1000,9999); 00430 $name = $prefix.':'.$name; 00431 $xmlns .= " xmlns:$prefix=\"$name_ns\""; 00432 } 00433 // if type is prefixed, create type prefix 00434 if($type_ns != '' && $type_ns == $this->namespaces['xsd']){ 00435 // need to fix this. shouldn't default to xsd if no ns specified 00436 // w/o checking against typemap 00437 $type_prefix = 'xsd'; 00438 } elseif($type_ns){ 00439 $type_prefix = 'ns'.rand(1000,9999); 00440 $xmlns .= " xmlns:$type_prefix=\"$type_ns\""; 00441 } 00442 // serialize attributes if present 00443 $atts = ''; 00444 if($attributes){ 00445 foreach($attributes as $k => $v){ 00446 $atts .= " $k=\"".$this->expandEntities($v).'"'; 00447 } 00448 } 00449 // serialize null value 00450 if (is_null($val)) { 00451 $this->debug("serialize_val: serialize null"); 00452 if ($use == 'literal') { 00453 // TODO: depends on minOccurs 00454 $xml = "<$name$xmlns$atts/>"; 00455 $this->debug("serialize_val returning $xml"); 00456 return $xml; 00457 } else { 00458 if (isset($type) && isset($type_prefix)) { 00459 $type_str = " xsi:type=\"$type_prefix:$type\""; 00460 } else { 00461 $type_str = ''; 00462 } 00463 $xml = "<$name$xmlns$type_str$atts xsi:nil=\"true\"/>"; 00464 $this->debug("serialize_val returning $xml"); 00465 return $xml; 00466 } 00467 } 00468 // serialize if an xsd built-in primitive type 00469 if($type != '' && isset($this->typemap[$this->XMLSchemaVersion][$type])){ 00470 $this->debug("serialize_val: serialize xsd built-in primitive type"); 00471 if (is_bool($val)) { 00472 if ($type == 'boolean') { 00473 $val = $val ? 'true' : 'false'; 00474 } elseif (! $val) { 00475 $val = 0; 00476 } 00477 } else if (is_string($val)) { 00478 $val = $this->expandEntities($val); 00479 } 00480 if ($use == 'literal') { 00481 $xml = "<$name$xmlns$atts>$val</$name>"; 00482 $this->debug("serialize_val returning $xml"); 00483 return $xml; 00484 } else { 00485 $xml = "<$name$xmlns xsi:type=\"xsd:$type\"$atts>$val</$name>"; 00486 $this->debug("serialize_val returning $xml"); 00487 return $xml; 00488 } 00489 } 00490 // detect type and serialize 00491 $xml = ''; 00492 switch(true) { 00493 case (is_bool($val) || $type == 'boolean'): 00494 $this->debug("serialize_val: serialize boolean"); 00495 if ($type == 'boolean') { 00496 $val = $val ? 'true' : 'false'; 00497 } elseif (! $val) { 00498 $val = 0; 00499 } 00500 if ($use == 'literal') { 00501 $xml .= "<$name$xmlns$atts>$val</$name>"; 00502 } else { 00503 $xml .= "<$name$xmlns xsi:type=\"xsd:boolean\"$atts>$val</$name>"; 00504 } 00505 break; 00506 case (is_int($val) || is_long($val) || $type == 'int'): 00507 $this->debug("serialize_val: serialize int"); 00508 if ($use == 'literal') { 00509 $xml .= "<$name$xmlns$atts>$val</$name>"; 00510 } else { 00511 $xml .= "<$name$xmlns xsi:type=\"xsd:int\"$atts>$val</$name>"; 00512 } 00513 break; 00514 case (is_float($val)|| is_double($val) || $type == 'float'): 00515 $this->debug("serialize_val: serialize float"); 00516 if ($use == 'literal') { 00517 $xml .= "<$name$xmlns$atts>$val</$name>"; 00518 } else { 00519 $xml .= "<$name$xmlns xsi:type=\"xsd:float\"$atts>$val</$name>"; 00520 } 00521 break; 00522 case (is_string($val) || $type == 'string'): 00523 $this->debug("serialize_val: serialize string"); 00524 $val = $this->expandEntities($val); 00525 if ($use == 'literal') { 00526 $xml .= "<$name$xmlns$atts>$val</$name>"; 00527 } else { 00528 $xml .= "<$name$xmlns xsi:type=\"xsd:string\"$atts>$val</$name>"; 00529 } 00530 break; 00531 case is_object($val): 00532 $this->debug("serialize_val: serialize object"); 00533 if (get_class($val) == 'soapval') { 00534 $this->debug("serialize_val: serialize soapval object"); 00535 $pXml = $val->serialize($use); 00536 $this->appendDebug($val->getDebug()); 00537 $val->clearDebug(); 00538 } else { 00539 if (! $name) { 00540 $name = get_class($val); 00541 $this->debug("In serialize_val, used class name $name as element name"); 00542 } else { 00543 $this->debug("In serialize_val, do not override name $name for element name for class " . get_class($val)); 00544 } 00545 foreach(get_object_vars($val) as $k => $v){ 00546 $pXml = isset($pXml) ? $pXml.$this->serialize_val($v,$k,false,false,false,false,$use) : $this->serialize_val($v,$k,false,false,false,false,$use); 00547 } 00548 } 00549 if(isset($type) && isset($type_prefix)){ 00550 $type_str = " xsi:type=\"$type_prefix:$type\""; 00551 } else { 00552 $type_str = ''; 00553 } 00554 if ($use == 'literal') { 00555 $xml .= "<$name$xmlns$atts>$pXml</$name>"; 00556 } else { 00557 $xml .= "<$name$xmlns$type_str$atts>$pXml</$name>"; 00558 } 00559 break; 00560 break; 00561 case (is_array($val) || $type): 00562 // detect if struct or array 00563 $valueType = $this->isArraySimpleOrStruct($val); 00564 if($valueType=='arraySimple' || ereg('^ArrayOf',$type)){ 00565 $this->debug("serialize_val: serialize array"); 00566 $i = 0; 00567 if(is_array($val) && count($val)> 0){ 00568 foreach($val as $v){ 00569 if(is_object($v) && get_class($v) == 'soapval'){ 00570 $tt_ns = $v->type_ns; 00571 $tt = $v->type; 00572 } elseif (is_array($v)) { 00573 $tt = $this->isArraySimpleOrStruct($v); 00574 } else { 00575 $tt = gettype($v); 00576 } 00577 $array_types[$tt] = 1; 00578 // TODO: for literal, the name should be $name 00579 $xml .= $this->serialize_val($v,'item',false,false,false,false,$use); 00580 ++$i; 00581 } 00582 if(count($array_types) > 1){ 00583 $array_typename = 'xsd:anyType'; 00584 } elseif(isset($tt) && isset($this->typemap[$this->XMLSchemaVersion][$tt])) { 00585 if ($tt == 'integer') { 00586 $tt = 'int'; 00587 } 00588 $array_typename = 'xsd:'.$tt; 00589 } elseif(isset($tt) && $tt == 'arraySimple'){ 00590 $array_typename = 'SOAP-ENC:Array'; 00591 } elseif(isset($tt) && $tt == 'arrayStruct'){ 00592 $array_typename = 'unnamed_struct_use_soapval'; 00593 } else { 00594 // if type is prefixed, create type prefix 00595 if ($tt_ns != '' && $tt_ns == $this->namespaces['xsd']){ 00596 $array_typename = 'xsd:' . $tt; 00597 } elseif ($tt_ns) { 00598 $tt_prefix = 'ns' . rand(1000, 9999); 00599 $array_typename = "$tt_prefix:$tt"; 00600 $xmlns .= " xmlns:$tt_prefix=\"$tt_ns\""; 00601 } else { 00602 $array_typename = $tt; 00603 } 00604 } 00605 $array_type = $i; 00606 if ($use == 'literal') { 00607 $type_str = ''; 00608 } else if (isset($type) && isset($type_prefix)) { 00609 $type_str = " xsi:type=\"$type_prefix:$type\""; 00610 } else { 00611 $type_str = " xsi:type=\"SOAP-ENC:Array\" SOAP-ENC:arrayType=\"".$array_typename."[$array_type]\""; 00612 } 00613 // empty array 00614 } else { 00615 if ($use == 'literal') { 00616 $type_str = ''; 00617 } else if (isset($type) && isset($type_prefix)) { 00618 $type_str = " xsi:type=\"$type_prefix:$type\""; 00619 } else { 00620 $type_str = " xsi:type=\"SOAP-ENC:Array\" SOAP-ENC:arrayType=\"xsd:anyType[0]\""; 00621 } 00622 } 00623 // TODO: for array in literal, there is no wrapper here 00624 $xml = "<$name$xmlns$type_str$atts>".$xml."</$name>"; 00625 } else { 00626 // got a struct 00627 $this->debug("serialize_val: serialize struct"); 00628 if(isset($type) && isset($type_prefix)){ 00629 $type_str = " xsi:type=\"$type_prefix:$type\""; 00630 } else { 00631 $type_str = ''; 00632 } 00633 if ($use == 'literal') { 00634 $xml .= "<$name$xmlns$atts>"; 00635 } else { 00636 $xml .= "<$name$xmlns$type_str$atts>"; 00637 } 00638 foreach($val as $k => $v){ 00639 // Apache Map 00640 if ($type == 'Map' && $type_ns == 'http://xml.apache.org/xml-soap') { 00641 $xml .= '<item>'; 00642 $xml .= $this->serialize_val($k,'key',false,false,false,false,$use); 00643 $xml .= $this->serialize_val($v,'value',false,false,false,false,$use); 00644 $xml .= '</item>'; 00645 } else { 00646 $xml .= $this->serialize_val($v,$k,false,false,false,false,$use); 00647 } 00648 } 00649 $xml .= "</$name>"; 00650 } 00651 break; 00652 default: 00653 $this->debug("serialize_val: serialize unknown"); 00654 $xml .= 'not detected, got '.gettype($val).' for '.$val; 00655 break; 00656 } 00657 $this->debug("serialize_val returning $xml"); 00658 return $xml; 00659 } 00660 00673 function serializeEnvelope($body,$headers=false,$namespaces=array(),$style='rpc',$use='encoded',$encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'){ 00674 // TODO: add an option to automatically run utf8_encode on $body and $headers 00675 // if $this->soap_defencoding is UTF-8. Not doing this automatically allows 00676 // one to send arbitrary UTF-8 characters, not just characters that map to ISO-8859-1 00677 00678 $this->debug("In serializeEnvelope length=" . strlen($body) . " body (max 1000 characters)=" . substr($body, 0, 1000) . " style=$style use=$use encodingStyle=$encodingStyle"); 00679 $this->debug("headers:"); 00680 $this->appendDebug($this->varDump($headers)); 00681 $this->debug("namespaces:"); 00682 $this->appendDebug($this->varDump($namespaces)); 00683 00684 // serialize namespaces 00685 $ns_string = ''; 00686 foreach(array_merge($this->namespaces,$namespaces) as $k => $v){ 00687 $ns_string .= " xmlns:$k=\"$v\""; 00688 } 00689 if($encodingStyle) { 00690 $ns_string = " SOAP-ENV:encodingStyle=\"$encodingStyle\"$ns_string"; 00691 } 00692 00693 // serialize headers 00694 if($headers){ 00695 if (is_array($headers)) { 00696 $xml = ''; 00697 foreach ($headers as $k => $v) { 00698 if (is_object($v) && get_class($v) == 'soapval') { 00699 $xml .= $this->serialize_val($v, false, false, false, false, false, $use); 00700 } else { 00701 $xml .= $this->serialize_val($v, $k, false, false, false, false, $use); 00702 } 00703 } 00704 $headers = $xml; 00705 $this->debug("In serializeEnvelope, serialized array of headers to $headers"); 00706 } 00707 $headers = "<SOAP-ENV:Header>".$headers."</SOAP-ENV:Header>"; 00708 } 00709 // serialize envelope 00710 return 00711 '<?xml version="1.0" encoding="'.$this->soap_defencoding .'"?'.">". 00712 '<SOAP-ENV:Envelope'.$ns_string.">". 00713 $headers. 00714 "<SOAP-ENV:Body>". 00715 $body. 00716 "</SOAP-ENV:Body>". 00717 "</SOAP-ENV:Envelope>"; 00718 } 00719 00728 function formatDump($str){ 00729 $str = htmlspecialchars($str); 00730 return nl2br($str); 00731 } 00732 00740 function contractQname($qname){ 00741 // get element namespace 00742 //$this->xdebug("Contract $qname"); 00743 if (strrpos($qname, ':')) { 00744 // get unqualified name 00745 $name = substr($qname, strrpos($qname, ':') + 1); 00746 // get ns 00747 $ns = substr($qname, 0, strrpos($qname, ':')); 00748 $p = $this->getPrefixFromNamespace($ns); 00749 if ($p) { 00750 return $p . ':' . $name; 00751 } 00752 return $qname; 00753 } else { 00754 return $qname; 00755 } 00756 } 00757 00765 function expandQname($qname){ 00766 // get element prefix 00767 if(strpos($qname,':') && !ereg('^http://',$qname)){ 00768 // get unqualified name 00769 $name = substr(strstr($qname,':'),1); 00770 // get ns prefix 00771 $prefix = substr($qname,0,strpos($qname,':')); 00772 if(isset($this->namespaces[$prefix])){ 00773 return $this->namespaces[$prefix].':'.$name; 00774 } else { 00775 return $qname; 00776 } 00777 } else { 00778 return $qname; 00779 } 00780 } 00781 00790 function getLocalPart($str){ 00791 if($sstr = strrchr($str,':')){ 00792 // get unqualified name 00793 return substr( $sstr, 1 ); 00794 } else { 00795 return $str; 00796 } 00797 } 00798 00807 function getPrefix($str){ 00808 if($pos = strrpos($str,':')){ 00809 // get prefix 00810 return substr($str,0,$pos); 00811 } 00812 return false; 00813 } 00814 00822 function getNamespaceFromPrefix($prefix){ 00823 if (isset($this->namespaces[$prefix])) { 00824 return $this->namespaces[$prefix]; 00825 } 00826 //$this->setError("No namespace registered for prefix '$prefix'"); 00827 return false; 00828 } 00829 00838 function getPrefixFromNamespace($ns) { 00839 foreach ($this->namespaces as $p => $n) { 00840 if ($ns == $n || $ns == $p) { 00841 $this->usedNamespaces[$p] = $n; 00842 return $p; 00843 } 00844 } 00845 return false; 00846 } 00847 00854 function getmicrotime() { 00855 if (function_exists('gettimeofday')) { 00856 $tod = gettimeofday(); 00857 $sec = $tod['sec']; 00858 $usec = $tod['usec']; 00859 } else { 00860 $sec = time(); 00861 $usec = 0; 00862 } 00863 return strftime('%Y-%m-%d %H:%M:%S', $sec) . '.' . sprintf('%06d', $usec); 00864 } 00865 00873 function varDump($data) { 00874 ob_start(); 00875 var_dump($data); 00876 $ret_val = ob_get_contents(); 00877 ob_end_clean(); 00878 return $ret_val; 00879 } 00880 00887 function __toString() { 00888 return $this->varDump($this); 00889 } 00890 } 00891 00892 // XML Schema Datatype Helper Functions 00893 00894 //xsd:dateTime helpers 00895 00904 function timestamp_to_iso8601($timestamp,$utc=true){ 00905 $datestr = date('Y-m-d\TH:i:sO',$timestamp); 00906 $pos = strrpos($datestr, "+"); 00907 if ($pos === FALSE) { 00908 $pos = strrpos($datestr, "-"); 00909 } 00910 if ($pos !== FALSE) { 00911 if (strlen($datestr) == $pos + 5) { 00912 $datestr = substr($datestr, 0, $pos + 3) . ':' . substr($datestr, -2); 00913 } 00914 } 00915 if($utc){ 00916 $eregStr = 00917 '([0-9]{4})-'. // centuries & years CCYY- 00918 '([0-9]{2})-'. // months MM- 00919 '([0-9]{2})'. // days DD 00920 'T'. // separator T 00921 '([0-9]{2}):'. // hours hh: 00922 '([0-9]{2}):'. // minutes mm: 00923 '([0-9]{2})(\.[0-9]*)?'. // seconds ss.ss... 00924 '(Z|[+\-][0-9]{2}:?[0-9]{2})?'; // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's 00925 00926 if(ereg($eregStr,$datestr,$regs)){ 00927 return sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ',$regs[1],$regs[2],$regs[3],$regs[4],$regs[5],$regs[6]); 00928 } 00929 return false; 00930 } else { 00931 return $datestr; 00932 } 00933 } 00934 00942 function iso8601_to_timestamp($datestr){ 00943 $eregStr = 00944 '([0-9]{4})-'. // centuries & years CCYY- 00945 '([0-9]{2})-'. // months MM- 00946 '([0-9]{2})'. // days DD 00947 'T'. // separator T 00948 '([0-9]{2}):'. // hours hh: 00949 '([0-9]{2}):'. // minutes mm: 00950 '([0-9]{2})(\.[0-9]+)?'. // seconds ss.ss... 00951 '(Z|[+\-][0-9]{2}:?[0-9]{2})?'; // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's 00952 if(ereg($eregStr,$datestr,$regs)){ 00953 // not utc 00954 if($regs[8] != 'Z'){ 00955 $op = substr($regs[8],0,1); 00956 $h = substr($regs[8],1,2); 00957 $m = substr($regs[8],strlen($regs[8])-2,2); 00958 if($op == '-'){ 00959 $regs[4] = $regs[4] + $h; 00960 $regs[5] = $regs[5] + $m; 00961 } elseif($op == '+'){ 00962 $regs[4] = $regs[4] - $h; 00963 $regs[5] = $regs[5] - $m; 00964 } 00965 } 00966 return gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]); 00967 // return strtotime("$regs[1]-$regs[2]-$regs[3] $regs[4]:$regs[5]:$regs[6]Z"); 00968 } else { 00969 return false; 00970 } 00971 } 00972 00980 function usleepWindows($usec) 00981 { 00982 $start = gettimeofday(); 00983 00984 do 00985 { 00986 $stop = gettimeofday(); 00987 $timePassed = 1000000 * ($stop['sec'] - $start['sec']) 00988 + $stop['usec'] - $start['usec']; 00989 } 00990 while ($timePassed < $usec); 00991 } 00992 00993 00994 ?>
Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|