Location: PHPKode > projects > FiForms Framework > FiForms/FiForms-includes/appconfig.inc.php
<?php
/*
*******************************************************************************

    FiForms -- A collection of PHP classes designed 
    to facilitate rapid development of web-database software

    Copyright (C) 2003 - 2005 Daniel McFeeters

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


    The original author of this library can be contacted at the following 
    address:

    Daniel McFeeters
    182 Baker Rd.
    Faubush, KY 42544-6526
    email:databases [at] fiforms [dot] org
    http://www.fiforms.org/


Project Started May 4, 2003
*******************************************************************************
appconfig.inc.php

Class to access XML Application configuration

*******************************************************************************
*/
/* ?><code><?php */

class FiDir
{
    public $base;
    public $dir;
    private $type; //file or directory

    function FiDir($base,$dir)
    {
        $this->base = $base;
        $this->dir = $dir;
        $this->type = "dir";
    }
}

class FiFile extends FiDir
{
    public $file;

    function FiFile($base,$dir,$file)
    {
        $this->base = $base;
        $this->dir = $dir == "" ? "" : $dir."/";
        $this->file = $file;
        $this->type = "file";
    }
}

class FiFormAppConfig
{
    private $config;
    private $doc;
    private $meta;
    
    
    function FiFormAppConfig()
    {
        $this->doc = new DOMDocument;
    }
    
    function load($configfile)
    {
        $this->doc->load($configfile);
        $this->doc->preserveWhiteSpace = false;
        $this->meta = new DOMXPath($this->doc);
        $this->meta->registerNamespace("m","http://xml.fiforms.org/FiFormsApplication/");

    }
    
    function getName()
    {
        return @$this->meta->query("/m:application/@name")->item(0)->nodeValue;
    }
    
    function getTitle()
    {
        return @$this->meta->query("/m:application/m:title")->item(0)->nodeValue;
    }

    function getSummary()
    {
        return @$this->meta->query("/m:application/m:summary")->item(0)->nodeValue;
    }

    function getDB()
    {
        return @$this->meta->query("/m:application/m:schema[@default='default']")->item(0)->nodeValue;
    }

    function getHost()
    {
        return @$this->meta->query("/m:application/m:database/@host")->item(0)->nodeValue;
    }

    function getXMLVersion()
    {
        $version = @$this->meta->query("/m:application/@version")->item(0)->nodeValue;
        if($version == '')
        {
            $version = "1.0";
        }
        return $version;
    }

    function getVersion()
    {
        $version = @$this->meta->query("/m:application/@appversion")->item(0)->nodeValue;
        if($version == '')
        {
            $version = "1.0";
        }
        return $version;
    }

    function compareVersion($newVersion)
    {
        // Compare decimal-dotted version numbers
        // like 1.0 vs 1.0.1 vs. 1.1 vs 1.5 vs 2
        // and return 0 if equal, 1 if newVersion
        // is newer, and -1 if this is newer
        $oldVersion = $this->getVersion();
        $oldA = explode(".",$oldVersion);
        $newA = explode(".",$newVersion);
        $diff = 0;
        $step = 0;
        while($diff == 0)
        {
            if(!array_key_exists($step,$oldA) && !array_key_exists($step,$newA))
            {
                return 0;
            }
            if(!array_key_exists($step,$oldA))
            {
                $oldA[$step] = 0;
            }
            if(!array_key_exists($step,$newA))
            {
                $newA[$step] = 0;
            }
            $old = (int)$oldA[$step];
            $new = (int)$newA[$step];
            if($old > $new)
            {
                $diff = -1;
            }
            elseif($old < $new)
            {
                $diff = 1;
            } // if
            $step++;
        } // while
        return $diff;
    } // function compareVersion
    
    function getURL()
    {
        return @$this->meta->query("/m:application/@url")->item(0)->nodeValue;
    }
    
    function check($configfile)
    {
        //validate the specified file
        //FIXME: Should validate before proceeding.
        $this->load($configfile);
        $version = $this->getXMLVersion();
        if(in_array($version,$GLOBALS['FIFORMS_CONFIG']['SUPPORTED_XML_VERSIONS']))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    function getDirectories()
    {
        $directories = array();
        $xmldir = $this->meta->query("//m:directory");
        foreach($xmldir as $dir)
        {
            $directories[] = new FiDir($dir->getAttribute('base'), $dir->getAttribute('path'));
        }
        return $directories;
    }

    function hasScript()
    {
        $scriptDir = $this->meta->query("//m:directory[@base='script']");
        if(count($scriptDir))
        {
            return true;
        }
        return false;
    }
    
    function getFiles()
    {
        $files = array();
        $xmldir = $this->meta->query("//m:xml|//m:file");
        foreach($xmldir as $file)
        {
            $files[] = new FiFile($file->parentNode->getAttribute('base'),
                    $file->parentNode->getAttribute('path'),
                    $file->getAttribute('name'));
        }
        return $files;
    }
}
/* ?></code><?php */

?>
Return current item: FiForms Framework