/**
  * Load data from XML to the structure
  */
 function arr2XMLDBStructure($xmlarr)
 {
     global $CFG;
     $result = true;
     /// Debug the structure
     /// traverse_xmlize($xmlarr);                   //Debug
     /// print_object ($GLOBALS['traverse_array']);  //Debug
     /// $GLOBALS['traverse_array']="";              //Debug
     /// Process structure attributes (path, comment and version)
     if (isset($xmlarr['XMLDB']['@']['PATH'])) {
         $this->path = trim($xmlarr['XMLDB']['@']['PATH']);
     } else {
         $this->errormsg = 'Missing PATH attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['XMLDB']['@']['VERSION'])) {
         $this->version = trim($xmlarr['XMLDB']['@']['VERSION']);
     } else {
         $this->errormsg = 'Missing VERSION attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['XMLDB']['@']['COMMENT'])) {
         $this->comment = trim($xmlarr['XMLDB']['@']['COMMENT']);
     } else {
         if (!empty($CFG->xmldbdisablecommentchecking)) {
             $this->comment = '';
         } else {
             $this->errormsg = 'Missing COMMENT attribute';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     }
     /// Iterate over tables
     if (isset($xmlarr['XMLDB']['#']['TABLES']['0']['#']['TABLE'])) {
         foreach ($xmlarr['XMLDB']['#']['TABLES']['0']['#']['TABLE'] as $xmltable) {
             if (!$result) {
                 //Skip on error
                 continue;
             }
             $name = trim($xmltable['@']['NAME']);
             $table = new XMLDBTable($name);
             $table->arr2XMLDBTable($xmltable);
             $this->tables[] = $table;
             if (!$table->isLoaded()) {
                 $this->errormsg = 'Problem loading table ' . $name;
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
     } else {
         $this->errormsg = 'Missing TABLES section';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     /// Perform some general checks over tables
     if ($result && $this->tables) {
         /// Check tables names are ok (lowercase, a-z _-)
         if (!$this->checkNameValues($this->tables)) {
             $this->errormsg = 'Some TABLES name values are incorrect';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// Check previous & next are ok (duplicates and existing tables)
         if ($result && !$this->checkPreviousNextValues($this->tables)) {
             $this->errormsg = 'Some TABLES previous/next values are incorrect';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// Order tables
         if ($result && !$this->orderTables($this->tables)) {
             $this->errormsg = 'Error ordering the tables';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     }
     /// Iterate over statements
     if (isset($xmlarr['XMLDB']['#']['STATEMENTS']['0']['#']['STATEMENT'])) {
         foreach ($xmlarr['XMLDB']['#']['STATEMENTS']['0']['#']['STATEMENT'] as $xmlstatement) {
             if (!$result) {
                 //Skip on error
                 continue;
             }
             $name = trim($xmlstatement['@']['NAME']);
             $statement = new XMLDBStatement($name);
             $statement->arr2XMLDBStatement($xmlstatement);
             $this->statements[] = $statement;
             if (!$statement->isLoaded()) {
                 $this->errormsg = 'Problem loading statement ' . $name;
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
     }
     /// Perform some general checks over statements
     if ($result && $this->statements) {
         /// Check statements names are ok (lowercase, a-z _-)
         if (!$this->checkNameValues($this->statements)) {
             $this->errormsg = 'Some STATEMENTS name values are incorrect';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// Check previous & next are ok (duplicates and existing statements)
         if ($result && !$this->checkPreviousNextValues($this->statements)) {
             $this->errormsg = 'Some STATEMENTS previous/next values are incorrect';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// Order statements
         if ($result && !$this->orderStatements($this->statements)) {
             $this->errormsg = 'Error ordering the statements';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     }
     /// Set some attributes
     if ($result) {
         $this->loaded = true;
     }
     $this->calculateHash();
     return $result;
 }
 /**
  * Load data from XML to the key
  */
 function arr2XMLDBKey($xmlarr)
 {
     $result = true;
     /// Debug the table
     /// traverse_xmlize($xmlarr);                   //Debug
     /// print_object ($GLOBALS['traverse_array']);  //Debug
     /// $GLOBALS['traverse_array']="";              //Debug
     /// Process key attributes (name, type, fields, reftable,
     /// reffields, comment, previous, next)
     if (isset($xmlarr['@']['NAME'])) {
         $this->name = trim($xmlarr['@']['NAME']);
     } else {
         $this->errormsg = 'Missing NAME attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['@']['TYPE'])) {
         /// Check for valid type
         $type = $this->getXMLDBKeyType(trim($xmlarr['@']['TYPE']));
         if ($type) {
             $this->type = $type;
         } else {
             $this->errormsg = 'Invalid TYPE attribute';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     } else {
         $this->errormsg = 'Missing TYPE attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['@']['FIELDS'])) {
         $fields = strtolower(trim($xmlarr['@']['FIELDS']));
         if ($fields) {
             $fieldsarr = explode(',', $fields);
             if ($fieldsarr) {
                 foreach ($fieldsarr as $key => $element) {
                     $fieldsarr[$key] = trim($element);
                 }
             } else {
                 $this->errormsg = 'Incorrect FIELDS attribute (comma separated of fields)';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         } else {
             $this->errormsg = 'Empty FIELDS attribute';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     } else {
         $this->errormsg = 'Missing FIELDS attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     /// Finally, set the array of fields
     $this->fields = $fieldsarr;
     if (isset($xmlarr['@']['REFTABLE'])) {
         /// Check we are in a FK
         if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
             $reftable = strtolower(trim($xmlarr['@']['REFTABLE']));
             if (!$reftable) {
                 $this->errormsg = 'Empty REFTABLE attribute';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         } else {
             $this->errormsg = 'Wrong REFTABLE attribute (only FK can have it)';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     } else {
         if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
             $this->errormsg = 'Missing REFTABLE attribute';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     }
     /// Finally, set the reftable
     if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
         $this->reftable = $reftable;
     }
     if (isset($xmlarr['@']['REFFIELDS'])) {
         /// Check we are in a FK
         if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
             $reffields = strtolower(trim($xmlarr['@']['REFFIELDS']));
             if ($reffields) {
                 $reffieldsarr = explode(',', $reffields);
                 if ($reffieldsarr) {
                     foreach ($reffieldsarr as $key => $element) {
                         $reffieldsarr[$key] = trim($element);
                     }
                 } else {
                     $this->errormsg = 'Incorrect REFFIELDS attribute (comma separated of fields)';
                     xmldb_dbg($this->errormsg);
                     $result = false;
                 }
             } else {
                 $this->errormsg = 'Empty REFFIELDS attribute';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         } else {
             $this->errormsg = 'Wrong REFFIELDS attribute (only FK can have it)';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     } else {
         if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
             $this->errormsg = 'Missing REFFIELDS attribute';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     }
     /// Finally, set the array of reffields
     if ($this->type == XMLDB_KEY_FOREIGN || $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
         $this->reffields = $reffieldsarr;
     }
     if (isset($xmlarr['@']['COMMENT'])) {
         $this->comment = trim($xmlarr['@']['COMMENT']);
     }
     if (isset($xmlarr['@']['PREVIOUS'])) {
         $this->previous = trim($xmlarr['@']['PREVIOUS']);
     }
     if (isset($xmlarr['@']['NEXT'])) {
         $this->next = trim($xmlarr['@']['NEXT']);
     }
     /// Set some attributes
     if ($result) {
         $this->loaded = true;
     }
     $this->calculateHash();
     return $result;
 }
Esempio n. 3
0
 /**
  * Load data from XML to the table
  */
 function arr2XMLDBField($xmlarr)
 {
     $result = true;
     /// Debug the table
     /// traverse_xmlize($xmlarr);                   //Debug
     /// print_object ($GLOBALS['traverse_array']);  //Debug
     /// $GLOBALS['traverse_array']="";              //Debug
     /// Process table attributes (name, type, length, unsigned,
     /// notnull, sequence, enum, enumvalues, decimals, comment,
     /// previous, next)
     if (isset($xmlarr['@']['NAME'])) {
         $this->name = trim($xmlarr['@']['NAME']);
     } else {
         $this->errormsg = 'Missing NAME attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['@']['TYPE'])) {
         /// Check for valid type
         $type = $this->getXMLDBFieldType(trim($xmlarr['@']['TYPE']));
         if ($type) {
             $this->type = $type;
         } else {
             $this->errormsg = 'Invalid TYPE attribute';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     } else {
         $this->errormsg = 'Missing TYPE attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['@']['LENGTH'])) {
         $length = trim($xmlarr['@']['LENGTH']);
         /// Check for integer values
         if ($this->type == XMLDB_TYPE_INTEGER || $this->type == XMLDB_TYPE_NUMBER || $this->type == XMLDB_TYPE_CHAR) {
             if (!(is_numeric($length) && intval($length) == floatval($length))) {
                 $this->errormsg = 'Incorrect LENGTH attribute for int, number or char fields';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             } else {
                 if (!$length) {
                     $this->errormsg = 'Zero LENGTH attribute';
                     xmldb_dbg($this->errormsg);
                     $result = false;
                 }
             }
         }
         /// Check for big, medium, small to be applied to text and binary
         if ($this->type == XMLDB_TYPE_TEXT || $this->type == XMLDB_TYPE_BINARY) {
             if (!$length) {
                 $length == 'big';
             }
             if ($length != 'big' && $length != 'medium' && $length != 'small') {
                 $this->errormsg = 'Incorrect LENGTH attribute for text and binary fields (only big, medium and small allowed)';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
         /// Finally, set the length
         $this->length = $length;
     }
     if (isset($xmlarr['@']['UNSIGNED'])) {
         $unsigned = strtolower(trim($xmlarr['@']['UNSIGNED']));
         if ($unsigned == 'true') {
             $this->unsigned = true;
         } else {
             if ($unsigned == 'false') {
                 $this->unsigned = false;
             } else {
                 $this->errormsg = 'Incorrect UNSIGNED attribute (true/false allowed)';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
     }
     if (isset($xmlarr['@']['NOTNULL'])) {
         $notnull = strtolower(trim($xmlarr['@']['NOTNULL']));
         if ($notnull == 'true') {
             $this->notnull = true;
         } else {
             if ($notnull == 'false') {
                 $this->notnull = false;
             } else {
                 $this->errormsg = 'Incorrect NOTNULL attribute (true/false allowed)';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
     }
     if (isset($xmlarr['@']['SEQUENCE'])) {
         $sequence = strtolower(trim($xmlarr['@']['SEQUENCE']));
         if ($sequence == 'true') {
             $this->sequence = true;
         } else {
             if ($sequence == 'false') {
                 $this->sequence = false;
             } else {
                 $this->errormsg = 'Incorrect SEQUENCE attribute (true/false allowed)';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
     }
     if (isset($xmlarr['@']['DEFAULT'])) {
         $this->default = trim($xmlarr['@']['DEFAULT']);
     }
     if (isset($xmlarr['@']['ENUM'])) {
         $enum = strtolower(trim($xmlarr['@']['ENUM']));
         if ($enum == 'true') {
             $this->enum = true;
         } else {
             if ($enum == 'false') {
                 $this->enum = false;
             } else {
                 $this->errormsg = 'Incorrect ENUM attribute (true/false allowed)';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
     }
     if (isset($xmlarr['@']['ENUMVALUES'])) {
         $enumvalues = strtolower(trim($xmlarr['@']['ENUMVALUES']));
         if (!$this->enum) {
             $this->errormsg = 'Wrong ENUMVALUES attribute (not ENUM)';
             xmldb_dbg($this->errormsg);
             $result = false;
             $this->enumvalues = $enumvalues;
         } else {
             /// Check we have a valid list (comma separated of quoted values)
             $enumarr = explode(',', $enumvalues);
             if ($enumarr) {
                 foreach ($enumarr as $key => $enumelement) {
                     /// Clear some spaces
                     $enumarr[$key] = trim($enumelement);
                     $enumelement = trim($enumelement);
                     /// Skip if under error
                     if (!$result) {
                         continue;
                     }
                     /// Look for quoted strings
                     if (substr($enumelement, 0, 1) != "'" || substr($enumelement, -1, 1) != "'") {
                         $this->errormsg = 'Incorrect ENUMVALUES attribute (some value is not properly quoted)';
                         xmldb_dbg($this->errormsg);
                         $result = false;
                     }
                 }
             } else {
                 $this->errormsg = 'Incorrect ENUMVALUES attribute (comma separated of quoted values)';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
     } else {
         if ($this->enum) {
             $this->errormsg = 'Incorrect ENUMVALUES attribute (field is not declared as ENUM)';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     }
     /// Finally, set the value
     if ($this->enum) {
         $this->enumvalues = $enumarr;
     }
     $decimals = NULL;
     if (isset($xmlarr['@']['DECIMALS'])) {
         $decimals = trim($xmlarr['@']['DECIMALS']);
         /// Check for integer values
         if ($this->type == XMLDB_TYPE_NUMBER || $this->type == XMLDB_TYPE_FLOAT) {
             if (!(is_numeric($decimals) && intval($decimals) == floatval($decimals))) {
                 $this->errormsg = 'Incorrect DECIMALS attribute for number field';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             } else {
                 if ($this->length <= $decimals) {
                     $this->errormsg = 'Incorrect DECIMALS attribute (bigget than length)';
                     xmldb_dbg($this->errormsg);
                     $result = false;
                 }
             }
         } else {
             $this->errormsg = 'Incorrect DECIMALS attribute for non-number field';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     } else {
         if ($this->type == XMLDB_TYPE_NUMBER) {
             $decimals = 0;
         }
     }
     // Finally, set the decimals
     if ($this->type == XMLDB_TYPE_NUMBER || $this->type == XMLDB_TYPE_FLOAT) {
         $this->decimals = $decimals;
     }
     if (isset($xmlarr['@']['COMMENT'])) {
         $this->comment = trim($xmlarr['@']['COMMENT']);
     }
     if (isset($xmlarr['@']['PREVIOUS'])) {
         $this->previous = trim($xmlarr['@']['PREVIOUS']);
     }
     if (isset($xmlarr['@']['NEXT'])) {
         $this->next = trim($xmlarr['@']['NEXT']);
     }
     /// Set some attributes
     if ($result) {
         $this->loaded = true;
     }
     $this->calculateHash();
     return $result;
 }
 /**
  * Load data from XML to the index
  */
 function arr2XMLDBStatement($xmlarr)
 {
     $result = true;
     /// Debug the table
     /// traverse_xmlize($xmlarr);                   //Debug
     /// print_object ($GLOBALS['traverse_array']);  //Debug
     /// $GLOBALS['traverse_array']="";              //Debug
     /// Process key attributes (table, type, comment, previous, next)
     if (isset($xmlarr['@']['TABLE'])) {
         $this->table = strtolower(trim($xmlarr['@']['TABLE']));
     } else {
         $this->errormsg = 'Missing TABLE attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['@']['TYPE'])) {
         /// Check for valid type
         $type = $this->getXMLDBStatementType(trim($xmlarr['@']['TYPE']));
         if ($type) {
             $this->type = $type;
         } else {
             $this->errormsg = 'Invalid TYPE attribute';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     } else {
         $this->errormsg = 'Missing TYPE attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     /// Look for sentences
     $sentencesarr = array();
     if (isset($xmlarr['#']['SENTENCES'])) {
         $sentences = $xmlarr['#']['SENTENCES'][0]['#']['SENTENCE'];
         if ($sentences) {
             foreach ($sentences as $sentence) {
                 if (isset($sentence['@']['TEXT'])) {
                     $sentencesarr[] = trim($sentence['@']['TEXT']);
                 } else {
                     $this->errormsg = 'Missing TEXT attribute in sentence';
                     xmldb_dbg($this->errormsg);
                     $result = false;
                 }
             }
         }
     }
     /// Finally, set the array of sentences
     $this->sentences = $sentencesarr;
     /// Now, perform some validations over sentences
     /// XMLDB_STATEMENT_INSERT checks
     if ($this->type == XMLDB_STATEMENT_INSERT) {
         /// Separate fields and values into two arrays
         if ($this->sentences) {
             foreach ($this->sentences as $sentence) {
                 $fields = $this->getFieldsFromInsertSentence($sentence);
                 $values = $this->getValuesFromInsertSentence($sentence);
                 /// Check that we aren't inserting the id field
                 if (in_array('id', $fields)) {
                     $this->errormsg = 'Cannot insert the "id" field. It is an autonumeric column';
                     xmldb_dbg($this->errormsg);
                     $result = false;
                 }
                 if ($result && count($fields) == 0) {
                     $this->errormsg = 'Missing fields in sentence "' . $sentence . '"';
                     xmldb_dbg($this->errormsg);
                     $result = false;
                 }
                 if ($result && count($values) == 0) {
                     $this->errormsg = 'Missing values in sentence "' . $sentence . '"';
                     xmldb_dbg($this->errormsg);
                     $result = false;
                 }
                 if ($result && count($fields) != count($values)) {
                     $this->errormsg = 'Incorrect number of fields (' . implode(', ', $fields) . ') or values (' . implode(', ', $values) . ')';
                     xmldb_dbg($this->errormsg);
                     $result = false;
                 }
             }
         }
     } else {
         /// Sentences different from INSERT are not valid for now
         $this->errormsg = 'Only INSERT statements are supported';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['@']['COMMENT'])) {
         $this->comment = trim($xmlarr['@']['COMMENT']);
     }
     if (isset($xmlarr['@']['PREVIOUS'])) {
         $this->previous = trim($xmlarr['@']['PREVIOUS']);
     }
     if (isset($xmlarr['@']['NEXT'])) {
         $this->next = trim($xmlarr['@']['NEXT']);
     }
     /// Set some attributes
     if ($result) {
         $this->loaded = true;
     }
     $this->calculateHash();
     return $result;
 }
Esempio n. 5
0
 /**
  * Load data from XML to the table
  */
 function arr2XMLDBTable($xmlarr)
 {
     global $CFG;
     $result = true;
     /// Debug the table
     /// traverse_xmlize($xmlarr);                   //Debug
     /// print_object ($GLOBALS['traverse_array']);  //Debug
     /// $GLOBALS['traverse_array']="";              //Debug
     /// Process table attributes (name, comment, previoustable and nexttable)
     if (isset($xmlarr['@']['NAME'])) {
         $this->name = trim($xmlarr['@']['NAME']);
     } else {
         $this->errormsg = 'Missing NAME attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['@']['COMMENT'])) {
         $this->comment = trim($xmlarr['@']['COMMENT']);
     } else {
         if (!empty($CFG->xmldbdisablecommentchecking)) {
             $this->comment = '';
         } else {
             $this->errormsg = 'Missing COMMENT attribute';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     }
     if (isset($xmlarr['@']['PREVIOUS'])) {
         $this->previous = trim($xmlarr['@']['PREVIOUS']);
     }
     if (isset($xmlarr['@']['NEXT'])) {
         $this->next = trim($xmlarr['@']['NEXT']);
     }
     /// Iterate over fields
     if (isset($xmlarr['#']['FIELDS']['0']['#']['FIELD'])) {
         foreach ($xmlarr['#']['FIELDS']['0']['#']['FIELD'] as $xmlfield) {
             if (!$result) {
                 //Skip on error
                 continue;
             }
             $name = trim($xmlfield['@']['NAME']);
             $field = new XMLDBField($name);
             $field->arr2XMLDBField($xmlfield);
             $this->fields[] = $field;
             if (!$field->isLoaded()) {
                 $this->errormsg = 'Problem loading field ' . $name;
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
     } else {
         $this->errormsg = 'Missing FIELDS section';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     /// Perform some general checks over fields
     if ($result && $this->fields) {
         /// Check field names are ok (lowercase, a-z _-)
         if (!$this->checkNameValues($this->fields)) {
             $this->errormsg = 'Some FIELDS name values are incorrect';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// Check previous & next are ok (duplicates and existing fields)
         if ($result && !$this->checkPreviousNextValues($this->fields)) {
             $this->errormsg = 'Some FIELDS previous/next values are incorrect';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// Order fields
         if ($result && !$this->orderFields($this->fields)) {
             $this->errormsg = 'Error ordering the fields';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     }
     /// Iterate over keys
     if (isset($xmlarr['#']['KEYS']['0']['#']['KEY'])) {
         foreach ($xmlarr['#']['KEYS']['0']['#']['KEY'] as $xmlkey) {
             if (!$result) {
                 //Skip on error
                 continue;
             }
             $name = trim($xmlkey['@']['NAME']);
             $key = new XMLDBKey($name);
             $key->arr2XMLDBKey($xmlkey);
             $this->keys[] = $key;
             if (!$key->isLoaded()) {
                 $this->errormsg = 'Problem loading key ' . $name;
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
     } else {
         $this->errormsg = 'Missing KEYS section (at least one PK must exist)';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     /// Perform some general checks over keys
     if ($result && $this->keys) {
         /// Check keys names are ok (lowercase, a-z _-)
         if (!$this->checkNameValues($this->keys)) {
             $this->errormsg = 'Some KEYS name values are incorrect';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// Check previous & next are ok (duplicates and existing keys)
         if ($result && !$this->checkPreviousNextValues($this->keys)) {
             $this->errormsg = 'Some KEYS previous/next values are incorrect';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// Order keys
         if ($result && !$this->orderKeys($this->keys)) {
             $this->errormsg = 'Error ordering the keys';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// TODO: Only one PK
         /// TODO: Not keys with repeated fields
         /// TODO: Check fields and reffieds exist in table
     }
     /// Iterate over indexes
     if (isset($xmlarr['#']['INDEXES']['0']['#']['INDEX'])) {
         foreach ($xmlarr['#']['INDEXES']['0']['#']['INDEX'] as $xmlindex) {
             if (!$result) {
                 //Skip on error
                 continue;
             }
             $name = trim($xmlindex['@']['NAME']);
             $index = new XMLDBIndex($name);
             $index->arr2XMLDBIndex($xmlindex);
             $this->indexes[] = $index;
             if (!$index->isLoaded()) {
                 $this->errormsg = 'Problem loading index ' . $name;
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
     }
     /// Perform some general checks over indexes
     if ($result && $this->indexes) {
         /// Check field names are ok (lowercase, a-z _-)
         if (!$this->checkNameValues($this->indexes)) {
             $this->errormsg = 'Some INDEXES name values are incorrect';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// Check previous & next are ok (duplicates and existing INDEXES)
         if ($result && !$this->checkPreviousNextValues($this->indexes)) {
             $this->errormsg = 'Some INDEXES previous/next values are incorrect';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// Order indexes
         if ($result && !$this->orderIndexes($this->indexes)) {
             $this->errormsg = 'Error ordering the indexes';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
         /// TODO: Not indexes with repeated fields
         /// TODO: Check fields exist in table
     }
     /// Set some attributes
     if ($result) {
         $this->loaded = true;
     }
     $this->calculateHash();
     return $result;
 }
 /**
  * Load data from XML to the index
  */
 function arr2XMLDBIndex($xmlarr)
 {
     $result = true;
     /// Debug the table
     /// traverse_xmlize($xmlarr);                   //Debug
     /// print_object ($GLOBALS['traverse_array']);  //Debug
     /// $GLOBALS['traverse_array']="";              //Debug
     /// Process key attributes (name, unique, fields, comment, previous, next)
     if (isset($xmlarr['@']['NAME'])) {
         $this->name = trim($xmlarr['@']['NAME']);
     } else {
         $this->errormsg = 'Missing NAME attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['@']['UNIQUE'])) {
         $unique = strtolower(trim($xmlarr['@']['UNIQUE']));
         if ($unique == 'true') {
             $this->unique = true;
         } else {
             if ($unique == 'false') {
                 $this->unique = false;
             } else {
                 $this->errormsg = 'Incorrect UNIQUE attribute (true/false allowed)';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         }
     } else {
         $this->errormsg = 'Undefined UNIQUE attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['@']['FIELDS'])) {
         $fields = strtolower(trim($xmlarr['@']['FIELDS']));
         if ($fields) {
             $fieldsarr = explode(',', $fields);
             if ($fieldsarr) {
                 foreach ($fieldsarr as $key => $element) {
                     $fieldsarr[$key] = trim($element);
                 }
             } else {
                 $this->errormsg = 'Incorrect FIELDS attribute (comma separated of fields)';
                 xmldb_dbg($this->errormsg);
                 $result = false;
             }
         } else {
             $this->errormsg = 'Empty FIELDS attribute';
             xmldb_dbg($this->errormsg);
             $result = false;
         }
     } else {
         $this->errormsg = 'Missing FIELDS attribute';
         xmldb_dbg($this->errormsg);
         $result = false;
     }
     /// Finally, set the array of fields
     $this->fields = $fieldsarr;
     if (isset($xmlarr['@']['COMMENT'])) {
         $this->comment = trim($xmlarr['@']['COMMENT']);
     }
     if (isset($xmlarr['@']['PREVIOUS'])) {
         $this->previous = trim($xmlarr['@']['PREVIOUS']);
     }
     if (isset($xmlarr['@']['NEXT'])) {
         $this->next = trim($xmlarr['@']['NEXT']);
     }
     /// Set some attributes
     if ($result) {
         $this->loaded = true;
     }
     $this->calculateHash();
     return $result;
 }