psyhvel = new Psyhvel(); } function _helper_recur(&$result, $node) { $name = $node->nodeName; $type = $node->nodeType; if ( $node->hasChildNodes() ) { $children = $node->childNodes; for ( $i=0; $i<$children->length; $i++ ) { $subresult = array(); $item = $children->item($i); $nodename = $item->nodeName; if ( $item->nodeType == XML_TEXT_NODE || $item->nodeType == XML_CDATA_SECTION_NODE) { $result[] = $item->nodeValue; } else { $this->_helper_recur($subresult, $item); if ( $item->hasAttributes() ) { $atts = $item->attributes; if ( !is_null($atts) ) { $subresult['attr'] = array(); foreach( $atts as $index=>$attr ) { $subresult['attr'][$attr->name] = $attr->value; } } } $result[$nodename][] = $subresult; } } } } function to_object($xmlstring) { $dom = new DOMDocument(); $dom->loadXML($xmlstring); $dom->normalizeDocument(); $result = array(); if ($dom->hasChildNodes() ) { $children = $dom->childNodes; for ( $i=0; $i<$children->length; $i++ ) { $this->_helper_recur($result, $children->item($i) ); if ($children->item($i)->hasAttributes() ) { $atts = $children->item($i)->attributes; if ( !is_null($atts) ) { $result['attr'] = array(); foreach( $atts as $index=>$attr ) { $result['attr'][$attr->name] = $attr->value; } } } } } return (object) $result; } function createObjectsR($children, $folder=0) { if (array_key_exists("child", $children)) { foreach ($children["child"] as $child) { $child_children = $child["children"][0]; $child_self = $this->xml_p[$child["attr"]["id"].".xml"]; if (in_array($child_self->type[0][0], $this->psyhvel->getImportableTypes())) { $obj = $this->psyhvel->loadType($child_self->type[0][0]); $obj_id = $obj->buildFromXML($child_self, $folder, $this->dir); if ($child_children) { $this->createObjectsR($child_children, $obj_id); } } } } } function createObjects() { $children = $this->xml_p["structure.xml"]->children[0]; $self = $this->xml_p[$this->xml_p["structure.xml"]->attr["id"].".xml"]; $type = $self->type[0][0]; if ($type == "TestFolder") { $folder = $this->psyhvel->loadType($type); $folder_id = $folder->buildFromXML($self); if ($folder_id && $children) { $this->parent = array('id'=>$folder_id, 'type'=>'TestFolder'); $this->createObjectsR($children, $folder_id); } } else { if ($this->folder) { $obj = $this->psyhvel->loadType($type); $parent_id = $obj->buildFromXML($self, $this->folder, $this->dir); if ($parent_id && $children) { $this->parent = array('id'=>$parent_id, 'type'=>$type); $this->createObjectsR($children, $parent_id); } } return false; //XXX find folder } } function delete_directory($dir) { if ($handle = opendir($dir)) { $array = array(); while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(is_dir($dir.$file)) { if(!@rmdir($dir.$file)) { delete_directory($dir.$file.'/'); } } else { @unlink($dir.$file); } } } closedir($handle); @rmdir($dir); } } function import($file = NULL) { $tmp_file = $file['tmp_name']; $this->dir = $tmp_file."_unzipped"; if (is_uploaded_file($tmp_file)) { $zip = new ZipArchive; $res = $zip->open($tmp_file); if ($res === TRUE) { $zip->extractTo($this->dir); $zip->close(); if ($handle = opendir($this->dir)) { while (false !== ($f = readdir($handle))) { if (!substr_compare($f, ".xml", -4, 4)) { $fc = file_get_contents($this->dir."/".$f); if (!empty($fc)) { $this->xml_p [$f]= $this->to_object(file_get_contents($this->dir."/".$f)); } } } closedir($handle); $this->createObjects($file); if (is_dir($this->dir)) { $this->delete_directory($this->dir); } return $this->parent; } } } return false; } } ?>