<?
    
function datasave($array$filename) {
        
$serialised serialize($array);

        
$f fopen($filename"w+");
        if (!
$f)  {
            echo 
"\nCould not open $filename!";
            return;
        }

        
$size fwrite($f$serialised);
        
fclose($f);
    }

    function &
dataload($filename) {
        if (
file_exists($filename) ) {
            
$f fopen($filename"r");
            
$serialised fread($ffilesize($filename) );
            
fclose($f);

            if (
$serialised 0) {
                return 
null;
            }
            return 
unserialize($serialised);
        }
        return 
null;
    } 
function 
GetCurlPage ($pageSpec$refnum=0) {
    
//CURLOPT_PROXY - use later
    
$ch curl_init($pageSpec);
    
curl_setopt($chCURLOPT_FOLLOWLOCATION1);
    
curl_setopt($chCURLOPT_FAILONERROR1);
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);
    
curl_setopt($chCURLOPT_MUTE1); //or can ask for pass if gets email

    
curl_setopt($chCURLOPT_TIMEOUT8);
    
curl_setopt($chCURLOPT_USERAGENT"Mozilla/3.0 Linux");
    
$tmp curl_exec ($ch);
    
curl_close ($ch);
    return 
$tmp;
}
// end function

class XMLParser {
    var 
$data;        // Input XML data buffer
    
var $vals;        // Struct created by xml_parse_into_struct
    
var $collapse_dups;    // If there is only one tag of a given name,
                //   shall we store as scalar or array?
    
var $index_numeric;    // Index tags by numeric position, not name.
                //   useful for ordered XML like CallXML.

    // Read in XML on object creation.
    // We can take raw XML data, a stream, a filename, or a url.
    
function XMLParser($data_source$data_source_type='raw'$collapse_dups=0$index_numeric=0) {
        
$this->collapse_dups $collapse_dups;
        
$this->index_numeric $index_numeric;
        
$this->data '';
        if (
$data_source_type == 'raw')
            
$this->data $data_source;

        elseif (
$data_source_type == 'stream') {
            while (!
feof($data_source))
                
$this->data .= fread($data_source1000);

        
// try filename, then if that fails...
        
} elseif (file_exists($data_source))
            
$this->data implode(''file($data_source)); 

        
// try url
        
else {
            
$fp fopen($data_source,'r');
            while (!
feof($fp))
                
$this->data .= fread($fp1000);
            
fclose($fp);
        }
    }

    
// Parse the XML file into a verbose, flat array struct.
    // Then, coerce that into a simple nested array.
    
function getTree() {
        
$parser xml_parser_create('ISO-8859-1');
        
xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1);
        
xml_parse_into_struct($parser$this->data$vals$index); 
        
xml_parser_free($parser);

        
$i = -1;
        return 
$this->getchildren($vals$i);
    }

    
// internal function: build a node of the tree
    
function buildtag($thisvals$vals, &$i$type) {

        if (isset(
$thisvals['attributes']))
            
$tag['ATTRIBUTES'] = $thisvals['attributes']; 

        
// complete tag, just return it for storage in array
        
if ($type === 'complete')
            
$tag['VALUE'] = $thisvals['value'];

        
// open tag, recurse
        
else
            
$tag array_merge($tag$this->getchildren($vals$i));

        return 
$tag;
    }

    
// internal function: build an nested array representing children
    
function getchildren($vals, &$i) { 
        
$children = array();     // Contains node data

        // Node has CDATA before it's children
                
if ($i > -&& isset($vals[$i]['value']))
            
$children['VALUE'] = $vals[$i]['value'];

        
// Loop through children, until hit close tag or run out of tags
        
while (++$i count($vals)) { 

            
$type $vals[$i]['type'];

            
// 'cdata':    Node has CDATA after one of it's children
            //         (Add to cdata found before in this case)
            
if ($type === 'cdata')
                
$children['VALUE'] .= $vals[$i]['value'];

            
// 'complete':    At end of current branch
            // 'open':    Node has children, recurse
            
elseif ($type === 'complete' || $type === 'open') {
                
$tag $this->buildtag($vals[$i], $vals$i$type);
                if (
$this->index_numeric) {
                    
$tag['TAG'] = $vals[$i]['tag'];
                    
$children[] = $tag;
                } else
                    
$children[$vals[$i]['tag']][] = $tag;
            }

            
// 'close:    End of node, return collected data
            //        Do not increment $i or nodes disappear!
            
elseif ($type === 'close')
                break;
        } 
        if (
$this->collapse_dups)
            foreach(
$children as $key => $value)
                if (
is_array($value) && (count($value) == 1))
                    
$children[$key] = $value[0];
        return 
$children;
    } 
}

#http://schema.peoplesdns.com/menow/menow.rdf
#http://peoplesdns.com/schema/owl
#$data = GetCurlPage("http://peoplesdns.com/schema/owl");
#http://schema.peoplesdns.com/menow/example.rdf

echo "<pre>\n";

if(
$do == ""){
    
$do "menow.rdf";
}

  
$parser = new XMLParser('http://peoplesdns.com/schema/menow/'.$do'url'0);#schema/rdf-schema
  
$tree $parser->getTree();

  
$classes $tree['RDF:RDF'][0]['RDFS:CLASS'];
  
$properties $tree['RDF:RDF'][0]['RDF:PROPERTY'];
  
$intclass $tree['RDF:RDF'][0]['CLASS'];
  
$loop = array("classes""properties","intclass");

while(list(
$k,$v)=each($loop)){
  
#echo "Trying $v<br>";
  
while(list($key,$val)=each(${$v})){
      
#echo "loop";
    
$class $val;

    if(
$class['RDFS:LABEL'][0]['VALUE'] == ""){
        if(
$class['ATTRIBUTES']['RDF:ABOUT']<>""){
            
$attr substr(strrchr($class['ATTRIBUTES']['RDF:ABOUT'], '/'), );
            if(
substr_count($attr"#")>0){$attr substr(strrchr($attr'#'), );}
            
$owl['rdfs:class'][] = $attr;
        }elseif(
$class['ATTRIBUTES']['RDF:ID']<>""){
            
$attr $class['ATTRIBUTES']['RDF:ID'];
            
$owl['rdfs:class'][] = $attr;
        }else{
            
$attr $class['ATTRIBUTES']['RDFS:LABEL'];
            
$owl['rdfs:class'][] = $attr;
        }
    }else{
        if(
$class['ATTRIBUTES']['RDF:ID']<>""){
            
$attr $class['ATTRIBUTES']['RDF:ID'];
            
$owl['rdfs:class'][] = $attr;
        }elseif(
$class['ATTRIBUTES']['RDF:ABOUT']<>""){
                        
$attr substr(strrchr($class['ATTRIBUTES']['RDF:ABOUT'], '/'), );
            if(
substr_count($attr"#")>0){$attr substr(strrchr($attr'#'), );}
                        
$owl['rdfs:class'][] = $attr;            
        }else{
            
$attr $class['RDFS:LABEL'][0]['VALUE'];
                
$owl['rdfs:class'][] = $attr;
        }
    }

        if(
is_array($class['RDFS:COMMENT'])){
                
$owl['rdfs:comment'][] = $class['RDFS:COMMENT'][0]['VALUE'];
    }elseif(
$class['ATTRIBUTES']['RDFS:COMMENT'] <> ""){
        
$owl['rdfs:comment'][] = $class['ATTRIBUTES']['RDFS:COMMENT'];
        }else{
                
$owl['rdfs:comment'][] = 0;
        }



    if(
is_array($class['RDFS:SUBCLASSOF'])){
        if(
$class['RDFS:SUBCLASSOF'][0]['ATTRIBUTES']['RDF:RESOURCE'] == ""){
            
$owl['rdfs:subclassof'][] = $class['RDFS:SUBCLASSOF'][0]['ATTRIBUTES']['RDFS:LABEL'];
        }else{
            
$owl['rdfs:subclassof'][] = $class['RDFS:SUBCLASSOF'][0]['ATTRIBUTES']['RDF:RESOURCE'];
         }
    }else{
        
$owl['rdfs:subclassof'][] = 0;
    }
        if(
is_array($class['RDFS:DOMAIN'])){
                
$owl['rdfs:domain'][] = $class['RDFS:DOMAIN'][0]['ATTRIBUTES']['RDF:RESOURCE'];
        }else{
                
$owl['rdfs:domain'][] = 0;
        }
        if(
is_array($class['RDFS:RANGE'])){
                
$owl['rdfs:range'][] = $class['RDFS:RANGE'][0]['ATTRIBUTES']['RDF:RESOURCE'];
        }else{
                
$owl['rdfs:range'][] = 0;
        }    
        if(
is_array($class['RDFS:SUBPROPERTYOF'])){
                
$owl['rdfs:subpropertyof'][] = $class['RDFS:SUBPROPERTYOF'][0]['ATTRIBUTES']['RDF:RESOURCE'];
        }else{
                
$owl['rdfs:subpropertyof'][] = 0;
        }
        if(
is_array($class['RDFS:TYPE'])){
                
$owl['rdfs:type'][] = $class['RDFS:TYPE'][0]['ATTRIBUTES']['RDF:RESOURCE'];
        }else{
                
$owl['rdfs:type'][] = 0;
        }

        if(
is_array($class['UNIONOF'])){
                
#$owl['class'][$key] = array();
        
$name $attr;
        
$owl['class'][$name] = $class['UNIONOF'][0]['ATTRIBUTES']['RDF:PARSETYPE'];
        
        }else{
        
$name $attr;
                
$owl['class'][$name] = 0;
        }
    
  }
$nowl[$v] = $owl;
}
  
datasave($nowl"/home/people/www/parse/ns/$do");

print_r($nowl);
echo 
"<hr>";
print_r($tree);
#echo "<br>";
#print_r($nowl);
#print_r($properties);
#print_r($tree);
// for testing uncomment
#print_r(foaf("http://peoplesdns.com/foaf.rdf.1"));
#print_r(foaf("http://peoplesdns.com/foaf.rdf"));

//ontology parsing
#print_r(foaf("http://peoplesdns.com/schema/foaf"));
#print_r(foaf("http://peoplesdns.com/schema/owl"));
#print_r(foaf("http://peoplesdns.com/schema/rdf-schema"));
#print_r(foaf("http://peoplesdns.com/schema/ns"));

#print_r(foaf("http://peoplesdns.com/schema/pdns.rdf"));
#print_r(foaf("http://blogs.it/0100198/gems/FOAF.rdf"));

/*
#
# the following snippet uses the foaf() function to loop through Marc Canters
# acquaintanceOf lists and snags their foaf files.
#
$marc = foaf("http://blogs.it/0100198/gems/FOAF.rdf");
$acquaintancefoafs = $marc['RDF']['Person']['acquaintanceOf']['Person']['seeAlso'];
while(list($key,$val)=each($acquaintancefoafs)){
    $a = foaf(trim($val));
    print_r($a);
}
*/
#echo "</pre>\n";


/*
echo "<B>Classes</b><pre>\n";
print_r(unserialize($owl_rdfs_class));
echo "</pre><B>Properties</b><pre>\n";
print_r(unserialize($owl_rdfs_properties));
echo "</pre><B>Internal Classes</b><pre>\n";
print_r(unserialize($owl_class));
*/
?>