5 Modification information for LGPL compliance
7 r57813 - 2010-08-19 10:34:44 -0700 (Thu, 19 Aug 2010) - kjing - Author: John Mertic <jmertic@sugarcrm.com>
8 Bug 39085 - When loading the opposite search panel via ajax on the ListViews, call the index action instead of the ListView action to avoid touching pre-MVC code by accident.
10 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
12 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
14 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
16 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
18 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
20 r51443 - 2009-10-12 13:34:36 -0700 (Mon, 12 Oct 2009) - jmertic - Bug 33332 - Made application PHP 5.3 compliant with E_DEPRECATED warnings on by:
21 - Changing all ereg function to either preg or simple string based ones
22 - No more references to magic quotes.
23 - Change all the session_unregister() functions to just unset() the correct session variable instead.
25 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
27 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
29 r13782 - 2006-06-06 10:58:55 -0700 (Tue, 06 Jun 2006) - majed - changes entry point code
31 r11115 - 2006-01-17 14:54:45 -0800 (Tue, 17 Jan 2006) - majed - add entry point validation
33 r8846 - 2005-10-31 11:01:12 -0800 (Mon, 31 Oct 2005) - majed - new version of nusoap
35 r5462 - 2005-05-25 13:50:11 -0700 (Wed, 25 May 2005) - majed - upgraded nusoap to .6.9
37 r573 - 2004-09-04 13:03:32 -0700 (Sat, 04 Sep 2004) - sugarclint - undoing copyrights added in inadvertantly. --clint
39 r546 - 2004-09-03 11:49:38 -0700 (Fri, 03 Sep 2004) - sugarmsi - removed echo count
41 r354 - 2004-08-02 23:00:37 -0700 (Mon, 02 Aug 2004) - sugarjacob - Adding Soap
47 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
53 * parses an XML Schema, allows access to it's data, other utility methods.
54 * imperfect, no validation... yet, but quite functional.
56 * @author Dietrich Ayala <dietrich@ganx4.com>
57 * @author Scott Nichol <snichol@users.sourceforge.net>
61 class nusoap_xmlschema extends nusoap_base {
67 var $enclosingNamespaces;
69 var $schemaInfo = array();
70 var $schemaTargetNamespace = '';
71 // types, elements, attributes defined by the schema
72 var $attributes = array();
73 var $complexTypes = array();
74 var $complexTypeStack = array();
75 var $currentComplexType = null;
76 var $elements = array();
77 var $elementStack = array();
78 var $currentElement = null;
79 var $simpleTypes = array();
80 var $simpleTypeStack = array();
81 var $currentSimpleType = null;
83 var $imports = array();
88 var $depth_array = array();
89 var $message = array();
90 var $defaultNamespace = array();
95 * @param string $schema schema document URI
96 * @param string $xml xml document URI
97 * @param string $namespaces namespaces defined in enclosing XML
100 function nusoap_xmlschema($schema='',$xml='',$namespaces=array()){
101 parent::nusoap_base();
102 $this->debug('nusoap_xmlschema class instantiated, inside constructor');
104 $this->schema = $schema;
108 $this->enclosingNamespaces = $namespaces;
109 $this->namespaces = array_merge($this->namespaces, $namespaces);
113 $this->debug('initial schema file: '.$schema);
114 $this->parseFile($schema, 'schema');
119 $this->debug('initial xml file: '.$xml);
120 $this->parseFile($xml, 'xml');
128 * @param string $xml path/URL to XML file
129 * @param string $type (schema | xml)
133 function parseFile($xml,$type){
136 $xmlStr = @join("",@file($xml));
138 $msg = 'Error reading XML from '.$xml;
139 $this->setError($msg);
143 $this->debug("parsing $xml");
144 $this->parseString($xmlStr,$type);
145 $this->debug("done parsing $xml");
153 * parse an XML string
155 * @param string $xml path or URL
156 * @param string $type (schema|xml)
159 function parseString($xml,$type){
163 // Create an XML parser.
164 $this->parser = xml_parser_create();
165 // Set the options for parsing the XML data.
166 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
168 // Set the object for the parser.
169 xml_set_object($this->parser, $this);
171 // Set the element handlers for the parser.
172 if($type == "schema"){
173 xml_set_element_handler($this->parser, 'schemaStartElement','schemaEndElement');
174 xml_set_character_data_handler($this->parser,'schemaCharacterData');
175 } elseif($type == "xml"){
176 xml_set_element_handler($this->parser, 'xmlStartElement','xmlEndElement');
177 xml_set_character_data_handler($this->parser,'xmlCharacterData');
180 // Parse the XML file.
181 if(!xml_parse($this->parser,$xml,true)){
182 // Display an error message.
183 $errstr = sprintf('XML error parsing XML schema on line %d: %s',
184 xml_get_current_line_number($this->parser),
185 xml_error_string(xml_get_error_code($this->parser))
187 $this->debug($errstr);
188 $this->debug("XML payload:\n" . $xml);
189 $this->setError($errstr);
192 xml_parser_free($this->parser);
194 $this->debug('no xml passed to parseString()!!');
195 $this->setError('no xml passed to parseString()!!');
200 * gets a type name for an unnamed type
202 * @param string Element name
203 * @return string A type name for an unnamed type
206 function CreateTypeName($ename) {
208 for ($i = 0; $i < count($this->complexTypeStack); $i++) {
209 $scope .= $this->complexTypeStack[$i] . '_';
211 return $scope . $ename . '_ContainedType';
215 * start-element handler
217 * @param string $parser XML parser object
218 * @param string $name element name
219 * @param string $attrs associative array of attributes
222 function schemaStartElement($parser, $name, $attrs) {
224 // position in the total number of elements, starting from 0
225 $pos = $this->position++;
226 $depth = $this->depth++;
227 // set self as current value for this depth
228 $this->depth_array[$depth] = $pos;
229 $this->message[$pos] = array('cdata' => '');
231 $this->defaultNamespace[$pos] = $this->defaultNamespace[$this->depth_array[$depth - 1]];
233 $this->defaultNamespace[$pos] = false;
236 // get element prefix
237 if($prefix = $this->getPrefix($name)){
238 // get unqualified name
239 $name = $this->getLocalPart($name);
244 // loop thru attributes, expanding, and registering namespace declarations
245 if(count($attrs) > 0){
246 foreach($attrs as $k => $v){
247 // if ns declarations, add to class level array of valid namespaces
248 if(preg_match('/^xmlns/',$k)){
249 //$this->xdebug("$k: $v");
250 //$this->xdebug('ns_prefix: '.$this->getPrefix($k));
251 if($ns_prefix = substr(strrchr($k,':'),1)){
252 //$this->xdebug("Add namespace[$ns_prefix] = $v");
253 $this->namespaces[$ns_prefix] = $v;
255 $this->defaultNamespace[$pos] = $v;
256 if (! $this->getPrefixFromNamespace($v)) {
257 $this->namespaces['ns'.(count($this->namespaces)+1)] = $v;
260 if($v == 'http://www.w3.org/2001/XMLSchema' || $v == 'http://www.w3.org/1999/XMLSchema' || $v == 'http://www.w3.org/2000/10/XMLSchema'){
261 $this->XMLSchemaVersion = $v;
262 $this->namespaces['xsi'] = $v.'-instance';
266 foreach($attrs as $k => $v){
267 // expand each attribute
268 $k = strpos($k,':') ? $this->expandQname($k) : $k;
269 $v = strpos($v,':') ? $this->expandQname($v) : $v;
276 // find status, register data
278 case 'all': // (optional) compositor content for a complexType
282 //$this->xdebug("compositor $name for currentComplexType: $this->currentComplexType and currentElement: $this->currentElement");
283 $this->complexTypes[$this->currentComplexType]['compositor'] = $name;
284 //if($name == 'all' || $name == 'sequence'){
285 // $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
288 case 'attribute': // complexType attribute
289 //$this->xdebug("parsing attribute $attrs[name] $attrs[ref] of value: ".$attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']);
290 $this->xdebug("parsing attribute:");
291 $this->appendDebug($this->varDump($attrs));
292 if (!isset($attrs['form'])) {
293 // TODO: handle globals
294 $attrs['form'] = $this->schemaInfo['attributeFormDefault'];
296 if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
297 $v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
298 if (!strpos($v, ':')) {
299 // no namespace in arrayType attribute value...
300 if ($this->defaultNamespace[$pos]) {
301 // ...so use the default
302 $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'] = $this->defaultNamespace[$pos] . ':' . $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
306 if(isset($attrs['name'])){
307 $this->attributes[$attrs['name']] = $attrs;
308 $aname = $attrs['name'];
309 } elseif(isset($attrs['ref']) && $attrs['ref'] == 'http://schemas.xmlsoap.org/soap/encoding/:arrayType'){
310 if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
311 $aname = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
315 } elseif(isset($attrs['ref'])){
316 $aname = $attrs['ref'];
317 $this->attributes[$attrs['ref']] = $attrs;
320 if($this->currentComplexType){ // This should *always* be
321 $this->complexTypes[$this->currentComplexType]['attrs'][$aname] = $attrs;
323 // arrayType attribute
324 if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']) || $this->getLocalPart($aname) == 'arrayType'){
325 $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
326 $prefix = $this->getPrefix($aname);
327 if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])){
328 $v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
332 if(strpos($v,'[,]')){
333 $this->complexTypes[$this->currentComplexType]['multidimensional'] = true;
335 $v = substr($v,0,strpos($v,'[')); // clip the []
336 if(!strpos($v,':') && isset($this->typemap[$this->XMLSchemaVersion][$v])){
337 $v = $this->XMLSchemaVersion.':'.$v;
339 $this->complexTypes[$this->currentComplexType]['arrayType'] = $v;
342 case 'complexContent': // (optional) content for a complexType
343 $this->xdebug("do nothing for element $name");
346 array_push($this->complexTypeStack, $this->currentComplexType);
347 if(isset($attrs['name'])){
348 // TODO: what is the scope of named complexTypes that appear
349 // nested within other c complexTypes?
350 $this->xdebug('processing named complexType '.$attrs['name']);
351 //$this->currentElement = false;
352 $this->currentComplexType = $attrs['name'];
353 $this->complexTypes[$this->currentComplexType] = $attrs;
354 $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
355 // This is for constructs like
356 // <complexType name="ListOfString" base="soap:Array">
358 // <element name="string" type="xsd:string"
359 // minOccurs="0" maxOccurs="unbounded" />
362 if(isset($attrs['base']) && preg_match('/:Array$/',$attrs['base'])){
363 $this->xdebug('complexType is unusual array');
364 $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
366 $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
369 $name = $this->CreateTypeName($this->currentElement);
370 $this->xdebug('processing unnamed complexType for element ' . $this->currentElement . ' named ' . $name);
371 $this->currentComplexType = $name;
372 //$this->currentElement = false;
373 $this->complexTypes[$this->currentComplexType] = $attrs;
374 $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
375 // This is for constructs like
376 // <complexType name="ListOfString" base="soap:Array">
378 // <element name="string" type="xsd:string"
379 // minOccurs="0" maxOccurs="unbounded" />
382 if(isset($attrs['base']) && preg_match('/:Array$/',$attrs['base'])){
383 $this->xdebug('complexType is unusual array');
384 $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
386 $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
389 $this->complexTypes[$this->currentComplexType]['simpleContent'] = 'false';
392 array_push($this->elementStack, $this->currentElement);
393 if (!isset($attrs['form'])) {
394 if ($this->currentComplexType) {
395 $attrs['form'] = $this->schemaInfo['elementFormDefault'];
398 $attrs['form'] = 'qualified';
401 if(isset($attrs['type'])){
402 $this->xdebug("processing typed element ".$attrs['name']." of type ".$attrs['type']);
403 if (! $this->getPrefix($attrs['type'])) {
404 if ($this->defaultNamespace[$pos]) {
405 $attrs['type'] = $this->defaultNamespace[$pos] . ':' . $attrs['type'];
406 $this->xdebug('used default namespace to make type ' . $attrs['type']);
409 // This is for constructs like
410 // <complexType name="ListOfString" base="soap:Array">
412 // <element name="string" type="xsd:string"
413 // minOccurs="0" maxOccurs="unbounded" />
416 if ($this->currentComplexType && $this->complexTypes[$this->currentComplexType]['phpType'] == 'array') {
417 $this->xdebug('arrayType for unusual array is ' . $attrs['type']);
418 $this->complexTypes[$this->currentComplexType]['arrayType'] = $attrs['type'];
420 $this->currentElement = $attrs['name'];
421 $ename = $attrs['name'];
422 } elseif(isset($attrs['ref'])){
423 $this->xdebug("processing element as ref to ".$attrs['ref']);
424 $this->currentElement = "ref to ".$attrs['ref'];
425 $ename = $this->getLocalPart($attrs['ref']);
427 $type = $this->CreateTypeName($this->currentComplexType . '_' . $attrs['name']);
428 $this->xdebug("processing untyped element " . $attrs['name'] . ' type ' . $type);
429 $this->currentElement = $attrs['name'];
430 $attrs['type'] = $this->schemaTargetNamespace . ':' . $type;
431 $ename = $attrs['name'];
433 if (isset($ename) && $this->currentComplexType) {
434 $this->xdebug("add element $ename to complexType $this->currentComplexType");
435 $this->complexTypes[$this->currentComplexType]['elements'][$ename] = $attrs;
436 } elseif (!isset($attrs['ref'])) {
437 $this->xdebug("add element $ename to elements array");
438 $this->elements[ $attrs['name'] ] = $attrs;
439 $this->elements[ $attrs['name'] ]['typeClass'] = 'element';
442 case 'enumeration': // restriction value list member
443 $this->xdebug('enumeration ' . $attrs['value']);
444 if ($this->currentSimpleType) {
445 $this->simpleTypes[$this->currentSimpleType]['enumeration'][] = $attrs['value'];
446 } elseif ($this->currentComplexType) {
447 $this->complexTypes[$this->currentComplexType]['enumeration'][] = $attrs['value'];
450 case 'extension': // simpleContent or complexContent type extension
451 $this->xdebug('extension ' . $attrs['base']);
452 if ($this->currentComplexType) {
453 $ns = $this->getPrefix($attrs['base']);
455 $this->complexTypes[$this->currentComplexType]['extensionBase'] = $this->schemaTargetNamespace . ':' . $attrs['base'];
457 $this->complexTypes[$this->currentComplexType]['extensionBase'] = $attrs['base'];
460 $this->xdebug('no current complexType to set extensionBase');
464 if (isset($attrs['schemaLocation'])) {
465 $this->xdebug('import namespace ' . $attrs['namespace'] . ' from ' . $attrs['schemaLocation']);
466 $this->imports[$attrs['namespace']][] = array('location' => $attrs['schemaLocation'], 'loaded' => false);
468 $this->xdebug('import namespace ' . $attrs['namespace']);
469 $this->imports[$attrs['namespace']][] = array('location' => '', 'loaded' => true);
470 if (! $this->getPrefixFromNamespace($attrs['namespace'])) {
471 $this->namespaces['ns'.(count($this->namespaces)+1)] = $attrs['namespace'];
476 if (isset($attrs['schemaLocation'])) {
477 $this->xdebug('include into namespace ' . $this->schemaTargetNamespace . ' from ' . $attrs['schemaLocation']);
478 $this->imports[$this->schemaTargetNamespace][] = array('location' => $attrs['schemaLocation'], 'loaded' => false);
480 $this->xdebug('ignoring invalid XML Schema construct: include without schemaLocation attribute');
483 case 'list': // simpleType value list
484 $this->xdebug("do nothing for element $name");
486 case 'restriction': // simpleType, simpleContent or complexContent value restriction
487 $this->xdebug('restriction ' . $attrs['base']);
488 if($this->currentSimpleType){
489 $this->simpleTypes[$this->currentSimpleType]['type'] = $attrs['base'];
490 } elseif($this->currentComplexType){
491 $this->complexTypes[$this->currentComplexType]['restrictionBase'] = $attrs['base'];
492 if(strstr($attrs['base'],':') == ':Array'){
493 $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
498 $this->schemaInfo = $attrs;
499 $this->schemaInfo['schemaVersion'] = $this->getNamespaceFromPrefix($prefix);
500 if (isset($attrs['targetNamespace'])) {
501 $this->schemaTargetNamespace = $attrs['targetNamespace'];
503 if (!isset($attrs['elementFormDefault'])) {
504 $this->schemaInfo['elementFormDefault'] = 'unqualified';
506 if (!isset($attrs['attributeFormDefault'])) {
507 $this->schemaInfo['attributeFormDefault'] = 'unqualified';
510 case 'simpleContent': // (optional) content for a complexType
511 if ($this->currentComplexType) { // This should *always* be
512 $this->complexTypes[$this->currentComplexType]['simpleContent'] = 'true';
514 $this->xdebug("do nothing for element $name because there is no current complexType");
518 array_push($this->simpleTypeStack, $this->currentSimpleType);
519 if(isset($attrs['name'])){
520 $this->xdebug("processing simpleType for name " . $attrs['name']);
521 $this->currentSimpleType = $attrs['name'];
522 $this->simpleTypes[ $attrs['name'] ] = $attrs;
523 $this->simpleTypes[ $attrs['name'] ]['typeClass'] = 'simpleType';
524 $this->simpleTypes[ $attrs['name'] ]['phpType'] = 'scalar';
526 $name = $this->CreateTypeName($this->currentComplexType . '_' . $this->currentElement);
527 $this->xdebug('processing unnamed simpleType for element ' . $this->currentElement . ' named ' . $name);
528 $this->currentSimpleType = $name;
529 //$this->currentElement = false;
530 $this->simpleTypes[$this->currentSimpleType] = $attrs;
531 $this->simpleTypes[$this->currentSimpleType]['phpType'] = 'scalar';
534 case 'union': // simpleType type list
535 $this->xdebug("do nothing for element $name");
538 $this->xdebug("do not have any logic to process element $name");
543 * end-element handler
545 * @param string $parser XML parser object
546 * @param string $name element name
549 function schemaEndElement($parser, $name) {
550 // bring depth down a notch
552 // position of current element is equal to the last value left in depth_array for my depth
553 if(isset($this->depth_array[$this->depth])){
554 $pos = $this->depth_array[$this->depth];
556 // get element prefix
557 if ($prefix = $this->getPrefix($name)){
558 // get unqualified name
559 $name = $this->getLocalPart($name);
564 if($name == 'complexType'){
565 $this->xdebug('done processing complexType ' . ($this->currentComplexType ? $this->currentComplexType : '(unknown)'));
566 $this->xdebug($this->varDump($this->complexTypes[$this->currentComplexType]));
567 $this->currentComplexType = array_pop($this->complexTypeStack);
568 //$this->currentElement = false;
570 if($name == 'element'){
571 $this->xdebug('done processing element ' . ($this->currentElement ? $this->currentElement : '(unknown)'));
572 $this->currentElement = array_pop($this->elementStack);
574 if($name == 'simpleType'){
575 $this->xdebug('done processing simpleType ' . ($this->currentSimpleType ? $this->currentSimpleType : '(unknown)'));
576 $this->xdebug($this->varDump($this->simpleTypes[$this->currentSimpleType]));
577 $this->currentSimpleType = array_pop($this->simpleTypeStack);
582 * element content handler
584 * @param string $parser XML parser object
585 * @param string $data element content
588 function schemaCharacterData($parser, $data){
589 $pos = $this->depth_array[$this->depth - 1];
590 $this->message[$pos]['cdata'] .= $data;
594 * serialize the schema
598 function serializeSchema(){
600 $schemaPrefix = $this->getPrefixFromNamespace($this->XMLSchemaVersion);
603 if (sizeof($this->imports) > 0) {
604 foreach($this->imports as $ns => $list) {
605 foreach ($list as $ii) {
606 if ($ii['location'] != '') {
607 $xml .= " <$schemaPrefix:import location=\"" . $ii['location'] . '" namespace="' . $ns . "\" />\n";
609 $xml .= " <$schemaPrefix:import namespace=\"" . $ns . "\" />\n";
615 foreach($this->complexTypes as $typeName => $attrs){
617 // serialize child elements
618 if(isset($attrs['elements']) && (count($attrs['elements']) > 0)){
619 foreach($attrs['elements'] as $element => $eParts){
620 if(isset($eParts['ref'])){
621 $contentStr .= " <$schemaPrefix:element ref=\"$element\"/>\n";
623 $contentStr .= " <$schemaPrefix:element name=\"$element\" type=\"" . $this->contractQName($eParts['type']) . "\"";
624 foreach ($eParts as $aName => $aValue) {
625 // handle, e.g., abstract, default, form, minOccurs, maxOccurs, nillable
626 if ($aName != 'name' && $aName != 'type') {
627 $contentStr .= " $aName=\"$aValue\"";
630 $contentStr .= "/>\n";
633 // compositor wraps elements
634 if (isset($attrs['compositor']) && ($attrs['compositor'] != '')) {
635 $contentStr = " <$schemaPrefix:$attrs[compositor]>\n".$contentStr." </$schemaPrefix:$attrs[compositor]>\n";
639 if(isset($attrs['attrs']) && (count($attrs['attrs']) >= 1)){
640 foreach($attrs['attrs'] as $attr => $aParts){
641 $contentStr .= " <$schemaPrefix:attribute";
642 foreach ($aParts as $a => $v) {
643 if ($a == 'ref' || $a == 'type') {
644 $contentStr .= " $a=\"".$this->contractQName($v).'"';
645 } elseif ($a == 'http://schemas.xmlsoap.org/wsdl/:arrayType') {
646 $this->usedNamespaces['wsdl'] = $this->namespaces['wsdl'];
647 $contentStr .= ' wsdl:arrayType="'.$this->contractQName($v).'"';
649 $contentStr .= " $a=\"$v\"";
652 $contentStr .= "/>\n";
656 if (isset($attrs['restrictionBase']) && $attrs['restrictionBase'] != ''){
657 $contentStr = " <$schemaPrefix:restriction base=\"".$this->contractQName($attrs['restrictionBase'])."\">\n".$contentStr." </$schemaPrefix:restriction>\n";
658 // complex or simple content
659 if ((isset($attrs['elements']) && count($attrs['elements']) > 0) || (isset($attrs['attrs']) && count($attrs['attrs']) > 0)){
660 $contentStr = " <$schemaPrefix:complexContent>\n".$contentStr." </$schemaPrefix:complexContent>\n";
663 // finalize complex type
664 if($contentStr != ''){
665 $contentStr = " <$schemaPrefix:complexType name=\"$typeName\">\n".$contentStr." </$schemaPrefix:complexType>\n";
667 $contentStr = " <$schemaPrefix:complexType name=\"$typeName\"/>\n";
672 if(isset($this->simpleTypes) && count($this->simpleTypes) > 0){
673 foreach($this->simpleTypes as $typeName => $eParts){
674 $xml .= " <$schemaPrefix:simpleType name=\"$typeName\">\n <$schemaPrefix:restriction base=\"".$this->contractQName($eParts['type'])."\">\n";
675 if (isset($eParts['enumeration'])) {
676 foreach ($eParts['enumeration'] as $e) {
677 $xml .= " <$schemaPrefix:enumeration value=\"$e\"/>\n";
680 $xml .= " </$schemaPrefix:restriction>\n </$schemaPrefix:simpleType>";
684 if(isset($this->elements) && count($this->elements) > 0){
685 foreach($this->elements as $element => $eParts){
686 $xml .= " <$schemaPrefix:element name=\"$element\" type=\"".$this->contractQName($eParts['type'])."\"/>\n";
690 if(isset($this->attributes) && count($this->attributes) > 0){
691 foreach($this->attributes as $attr => $aParts){
692 $xml .= " <$schemaPrefix:attribute name=\"$attr\" type=\"".$this->contractQName($aParts['type'])."\"\n/>";
697 foreach ($this->schemaInfo as $k => $v) {
698 if ($k == 'elementFormDefault' || $k == 'attributeFormDefault') {
699 $attr .= " $k=\"$v\"";
702 $el = "<$schemaPrefix:schema$attr targetNamespace=\"$this->schemaTargetNamespace\"\n";
703 foreach (array_diff($this->usedNamespaces, $this->enclosingNamespaces) as $nsp => $ns) {
704 $el .= " xmlns:$nsp=\"$ns\"";
706 $xml = $el . ">\n".$xml."</$schemaPrefix:schema>\n";
711 * adds debug data to the clas level debug string
713 * @param string $string debug data
716 function xdebug($string){
717 $this->debug('<' . $this->schemaTargetNamespace . '> '.$string);
721 * get the PHP type of a user defined type in the schema
722 * PHP type is kind of a misnomer since it actually returns 'struct' for assoc. arrays
723 * returns false if no type exists, or not w/ the given namespace
724 * else returns a string that is either a native php type, or 'struct'
726 * @param string $type name of defined type
727 * @param string $ns namespace of type
732 function getPHPType($type,$ns){
733 if(isset($this->typemap[$ns][$type])){
734 //print "found type '$type' and ns $ns in typemap<br>";
735 return $this->typemap[$ns][$type];
736 } elseif(isset($this->complexTypes[$type])){
737 //print "getting type '$type' and ns $ns from complexTypes array<br>";
738 return $this->complexTypes[$type]['phpType'];
744 * returns an associative array of information about a given type
745 * returns false if no type exists by the given name
747 * For a complexType typeDef = array(
748 * 'restrictionBase' => '',
750 * 'compositor' => '(sequence|all)',
751 * 'elements' => array(), // refs to elements array
752 * 'attrs' => array() // refs to attributes array
753 * ... and so on (see addComplexType)
756 * For simpleType or element, the array has different keys.
758 * @param string $type
761 * @see addComplexType
765 function getTypeDef($type){
766 //$this->debug("in getTypeDef for type $type");
767 if (substr($type, -1) == '^') {
769 $type = substr($type, 0, -1);
774 if((! $is_element) && isset($this->complexTypes[$type])){
775 $this->xdebug("in getTypeDef, found complexType $type");
776 return $this->complexTypes[$type];
777 } elseif((! $is_element) && isset($this->simpleTypes[$type])){
778 $this->xdebug("in getTypeDef, found simpleType $type");
779 if (!isset($this->simpleTypes[$type]['phpType'])) {
780 // get info for type to tack onto the simple type
781 // TODO: can this ever really apply (i.e. what is a simpleType really?)
782 $uqType = substr($this->simpleTypes[$type]['type'], strrpos($this->simpleTypes[$type]['type'], ':') + 1);
783 $ns = substr($this->simpleTypes[$type]['type'], 0, strrpos($this->simpleTypes[$type]['type'], ':'));
784 $etype = $this->getTypeDef($uqType);
786 $this->xdebug("in getTypeDef, found type for simpleType $type:");
787 $this->xdebug($this->varDump($etype));
788 if (isset($etype['phpType'])) {
789 $this->simpleTypes[$type]['phpType'] = $etype['phpType'];
791 if (isset($etype['elements'])) {
792 $this->simpleTypes[$type]['elements'] = $etype['elements'];
796 return $this->simpleTypes[$type];
797 } elseif(isset($this->elements[$type])){
798 $this->xdebug("in getTypeDef, found element $type");
799 if (!isset($this->elements[$type]['phpType'])) {
800 // get info for type to tack onto the element
801 $uqType = substr($this->elements[$type]['type'], strrpos($this->elements[$type]['type'], ':') + 1);
802 $ns = substr($this->elements[$type]['type'], 0, strrpos($this->elements[$type]['type'], ':'));
803 $etype = $this->getTypeDef($uqType);
805 $this->xdebug("in getTypeDef, found type for element $type:");
806 $this->xdebug($this->varDump($etype));
807 if (isset($etype['phpType'])) {
808 $this->elements[$type]['phpType'] = $etype['phpType'];
810 if (isset($etype['elements'])) {
811 $this->elements[$type]['elements'] = $etype['elements'];
813 if (isset($etype['extensionBase'])) {
814 $this->elements[$type]['extensionBase'] = $etype['extensionBase'];
816 } elseif ($ns == 'http://www.w3.org/2001/XMLSchema') {
817 $this->xdebug("in getTypeDef, element $type is an XSD type");
818 $this->elements[$type]['phpType'] = 'scalar';
821 return $this->elements[$type];
822 } elseif(isset($this->attributes[$type])){
823 $this->xdebug("in getTypeDef, found attribute $type");
824 return $this->attributes[$type];
825 } elseif (preg_match('/_ContainedType$/', $type)) {
826 $this->xdebug("in getTypeDef, have an untyped element $type");
827 $typeDef['typeClass'] = 'simpleType';
828 $typeDef['phpType'] = 'scalar';
829 $typeDef['type'] = 'http://www.w3.org/2001/XMLSchema:string';
832 $this->xdebug("in getTypeDef, did not find $type");
837 * returns a sample serialization of a given type, or false if no type by the given name
839 * @param string $type name of type
844 function serializeTypeDef($type){
845 //print "in sTD() for type $type<br>";
846 if($typeDef = $this->getTypeDef($type)){
848 if(is_array($typeDef['attrs'])){
849 foreach($typeDef['attrs'] as $attName => $data){
850 $str .= " $attName=\"{type = ".$data['type']."}\"";
853 $str .= " xmlns=\"".$this->schema['targetNamespace']."\"";
854 if(count($typeDef['elements']) > 0){
856 foreach($typeDef['elements'] as $element => $eData){
857 $str .= $this->serializeTypeDef($element);
860 } elseif($typeDef['typeClass'] == 'element') {
871 * returns HTML form elements that allow a user
872 * to enter values for creating an instance of the given type.
874 * @param string $name name for type instance
875 * @param string $type name of type
880 function typeToForm($name,$type){
882 if($typeDef = $this->getTypeDef($type)){
884 if($typeDef['phpType'] == 'struct'){
885 $buffer .= '<table>';
886 foreach($typeDef['elements'] as $child => $childDef){
888 <tr><td align='right'>$childDef[name] (type: ".$this->getLocalPart($childDef['type'])."):</td>
889 <td><input type='text' name='parameters[".$name."][$childDef[name]]'></td></tr>";
891 $buffer .= '</table>';
893 } elseif($typeDef['phpType'] == 'array'){
894 $buffer .= '<table>';
895 for($i=0;$i < 3; $i++){
897 <tr><td align='right'>array item (type: $typeDef[arrayType]):</td>
898 <td><input type='text' name='parameters[".$name."][]'></td></tr>";
900 $buffer .= '</table>';
903 $buffer .= "<input type='text' name='parameters[$name]'>";
906 $buffer .= "<input type='text' name='parameters[$name]'>";
912 * adds a complex type to the schema
922 * array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'string[]'),
926 * example: PHP associative array ( SOAP Struct )
933 * array('myVar'=> array('name'=>'myVar','type'=>'string')
937 * @param typeClass (complexType|simpleType|attribute)
938 * @param phpType: currently supported are array and struct (php assoc array)
939 * @param compositor (all|sequence|choice)
940 * @param restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
941 * @param elements = array ( name = array(name=>'',type=>'') )
942 * @param attrs = array(
944 * 'ref' => "http://schemas.xmlsoap.org/soap/encoding/:arrayType",
945 * "http://schemas.xmlsoap.org/wsdl/:arrayType" => "string[]"
948 * @param arrayType: namespace:name (http://www.w3.org/2001/XMLSchema:string)
952 function addComplexType($name,$typeClass='complexType',$phpType='array',$compositor='',$restrictionBase='',$elements=array(),$attrs=array(),$arrayType=''){
953 $this->complexTypes[$name] = array(
955 'typeClass' => $typeClass,
956 'phpType' => $phpType,
957 'compositor'=> $compositor,
958 'restrictionBase' => $restrictionBase,
959 'elements' => $elements,
961 'arrayType' => $arrayType
964 $this->xdebug("addComplexType $name:");
965 $this->appendDebug($this->varDump($this->complexTypes[$name]));
969 * adds a simple type to the schema
971 * @param string $name
972 * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
973 * @param string $typeClass (should always be simpleType)
974 * @param string $phpType (should always be scalar)
975 * @param array $enumeration array of values
977 * @see nusoap_xmlschema
980 function addSimpleType($name, $restrictionBase='', $typeClass='simpleType', $phpType='scalar', $enumeration=array()) {
981 $this->simpleTypes[$name] = array(
983 'typeClass' => $typeClass,
984 'phpType' => $phpType,
985 'type' => $restrictionBase,
986 'enumeration' => $enumeration
989 $this->xdebug("addSimpleType $name:");
990 $this->appendDebug($this->varDump($this->simpleTypes[$name]));
994 * adds an element to the schema
996 * @param array $attrs attributes that must include name and type
997 * @see nusoap_xmlschema
1000 function addElement($attrs) {
1001 if (! $this->getPrefix($attrs['type'])) {
1002 $attrs['type'] = $this->schemaTargetNamespace . ':' . $attrs['type'];
1004 $this->elements[ $attrs['name'] ] = $attrs;
1005 $this->elements[ $attrs['name'] ]['typeClass'] = 'element';
1007 $this->xdebug("addElement " . $attrs['name']);
1008 $this->appendDebug($this->varDump($this->elements[ $attrs['name'] ]));
1013 * Backward compatibility
1015 class XMLSchema extends nusoap_xmlschema {