Location: PHPKode > projects > FiForms Framework > FiForms/scripts/update_install.php
<?php
/*  This file is part of the FiForms Framework
    Copyright (C) 2003-2008 by Daniel McFeeters,
    Licensed under the GNU GPL. 
    See LICENSE.txt for details. 
*/

function deltree($path) 
{
  if (is_dir($path)) 
  {
    if ($handle = opendir($path)) 
    {
      while (false !== ($file = readdir($handle))) 
      {
        if ($file != '.' && $file != '..') 
        {
          if(is_dir($path."/".$file))
          {
              deltree($path."/".$file);
          }
          else
          {
              unlink($path."/".$file);
          }
        }
      }
      closedir($handle);
      rmdir($path);
      return 1;
    }
  } // isdir
  return;
}  // function deltree


function installApp($overwrite)
{

    global $cfg;
    global $appbase;
    global $apppath;
    global $tmpdir;
    global $appscriptbase;
    global $appscript;
    
    if(!is_writable($appbase))
    {
        echo "$appbase not writable. Could not install.";
        return false;
    }
    if($cfg->hasScript())
    {
        if(!is_writable($appscriptbase))
        {
            echo "$appscriptbase not writable, and application contains scripts or web element. Could not install.";
            return false;
        }
        if(!is_dir($appscript))
        {
            mkdir($appscript);
        }
    }
    if(!is_dir($apppath))
    {
        mkdir($apppath);
    }
    rename($tmpdir."/xml/appinfo.xml",$apppath."/appinfo.xml");
    $dirs = $cfg->getDirectories();
    foreach($dirs as $dir)
    {
        if($dir->base == "app" && $dir->dir != "" && !is_dir($apppath."/".$dir->dir))
        {
            mkdir($apppath."/".$dir->dir);
        }
        else if($dir->base == "script" && $dir->dir != "" && !is_dir($appscript."/".$dir->dir))
        {
            mkdir($appscript."/".$dir->dir);
        }
        else
        {
            // ignore unknown bases
        }
    } // foreach dir
    $files = $cfg->getFiles();
    foreach($files as $file)
    {
        if($file->base == "app")
        {
            $dest = $apppath."/".$file->dir.$file->file;
            if($overwrite && file_exists($dest))
            {
                unlink($dest);
            }
            rename($tmpdir."/xml/".$file->dir.$file->file,$dest);
        }
        elseif($file->base == "script")
        {
            $dest = $appscript."/".$file->dir.$file->file;
            if($overwrite && file_exists($dest))
            {
                unlink($dest);
            }
            rename($tmpdir."/php/".$file->dir.$file->file,$dest);
        }
        else
        {
            continue;
            // Ignore files with unknown base.
        }
    }
    //remove everything that's left
    deltree($tmpdir);
    return true;
} // function installApp

        require_once('localconfig.php');
        if($FIFORMS_CONFIG['USE_COMBINED'])
        {
            include_once(dirname(__FILE__).'/FiForms_combined.inc.php');
        }
        else
        {
            include_once($FIFORMS_CONFIG['INCLUDE_PATH'].'FiForms_genericIcons.inc.php');
            include_once($FIFORMS_CONFIG['INCLUDE_PATH'].'FiForms_global.inc.php');
        }
        if(!isset($FIFORMS_CONFIG['AUTH_MODULE']))
            die('Authentication Module not set in localconfig.php');
        require_once($FIFORMS_CONFIG['AUTH_MODULE']);
        require_once($FIFORMS_CONFIG['INCLUDE_PATH'].'appconfig.inc.php');
        require_once('promptpage.php');
        ob_start();
        $auth = new FiFormsAuth();
        if(!@mysql_connect($FIFORMS_CONFIG['DEFAULT_SERVER'],$auth->username,$auth->passwd))
        {
                $auth->connectFailure();
                die('not authenticated');
        }
        mysql_close();
        
        if($FIFORMS_CONFIG['UPDATE_HOST'] !== "any" && ($FIFORMS_CONFIG['UPDATE_HOST'] !== "localhost" || $_SERVER['REMOTE_ADDR'] !== "127.0.0.1"))
        {
            die("This feature is not allowed in localconfig.php.");
        }
        
        if(!$_POST['password'] || sha1($_POST['password']) !== $FIFORMS_CONFIG['UPDATEPW_HASH'])
        {
            die("Password Incorrect.");
        }
        

    $tmpdir = $FIFORMS_CONFIG['TEMP_PATH']."/".((int) $_POST['randdir']); 
    $endgame = "</body></html>";
    if(!(int) $_POST['randdir'])
    {
        die("<p>You didn't specify an installation source. Cannot continue.</p>$endgame");
    }
    if(!is_dir($tmpdir))
    {
        die("<p>The specified directory doesn't exist. Unable to continue.</p>$endgame");
    }
    if(!file_exists($tmpdir."/xml/appinfo.xml"))
    {
        deltree($tmpdir);
        die('<p>This package doesn\'t have the required appinfo.xml file. Either it isn\'t a valid FiForms package, or it is too old, or it has been corrupted. Please check the package and try again.</p>'.$endgame);
    }
    
    $cfg = new FiFormAppConfig();
    if(!$cfg->check($tmpdir."/xml/appinfo.xml"))
    {
        deltree($tmpdir);
        promptPage('Unsupported Package','<p>This package is not supported by your current version of FiForms. Be sure you have the latest version of FiForms installed and verify that the package is valid.</p><p><a href="index.php">Return to Main Menu</a>','20%','20%','60%');
        die();
    }
    $cfg->load($tmpdir."/xml/appinfo.xml");
    $appbase = $FIFORMS_CONFIG['APP_BASE'];
    $apppath = $appbase."/".$cfg->getName();
    $appscriptbase = $FIFORMS_CONFIG['SCRIPT_PATH']."/app";
    $appscript = $appscriptbase."/".$cfg->getName();
    $existingApp = false;
    $viewLink = "<a href=\"list.php?app=".$cfg->getName()."&index=main.xml\">Open Application.</a>";
    //print_r($cfg);die();
    if(is_dir($apppath) && file_exists($apppath."/appinfo.xml"))
    {
        $existingApp = true;
        $oldApp = new FiFormAppConfig();
        $oldAppValid = $oldApp->check($cfg->check($apppath."/appinfo.xml"));
        if($oldAppValid)
        {
            $oldApp->load($apppath."/appinfo.xml");
        }
    }
    if(array_key_exists('action_install',$_POST))
    {
        //install package
        if(!is_dir($appbase))
        {
            die('There is a problem with your FiForms Configuration');
        }
        
        if(installApp(false))
        {
            echo "Installation Successful.".$viewLink;
        }
        else
        {
            echo "The installation failed.";
        }
    }
    elseif(array_key_exists('action_upgrade',$_POST))
    {
        if(installApp(true))
        {
            echo "Upgrade Successful.".$viewLink;
        }
        else
        {
           echo "The upgrade failed.";
        }
    }
    elseif(array_key_exists('action_cancel',$_POST))
    {
        //remove remporary files
        deltree($tmpdir);
        ?>
        <p>Installation Canceled</p>
        <p><a href="index.php">Return to Home</a></p>
<?php
    }
    elseif(array_key_exists('action_view',$_POST))
    {
        //display package info
?>
<table>
<tr>
<th colspan="2" class="tablehead">Package Information</th>
</tr>
<tr>
<th>Title</th>
<td><?php echo $cfg->getTitle(); ?></td>
</tr>
<tr>
<th>Summary</th>
<td><?php echo $cfg->getSummary(); ?></td>
</tr>
<tr>
<th>
Package Name</th>
<td><?php echo $cfg->getName(); ?></td>
</tr>
<tr>
<th>Package Version</th>
<td><?php echo $cfg->getVersion(); ?></td>
</tr>
<tr>
<tr>
<th>Currently Installed?</th>
<td><?php echo $existingApp ? "Yes" : "No"; ?></td>
</tr>
<tr>
<th>Package Status</th>
<td>
<?php
if($existingApp && $oldAppValid)
{
    $action_button = "<input type=\"submit\" name=\"action_upgrade\" value=\"Upgrade\" />";

    if($oldApp->getURL() === $cfg->getURL())
    {
        echo "Application is compatible with installed application<br />";
        if($cfg->compareVersion($oldApp->getVersion()) > 0)
        {
            echo "Package is an upgrade of current version.";
        }
        else if($cfg->compareVersion($oldApp->getVersion()) == 0)
        {
            echo "Package is the same as the installed version.";
        }
        else
        {
            echo "<span class=\"error\">Package is older than installed version. Downgrading is not recommended.</span>";
        }
    }
    else
    {
        echo "<span class=\"error\">Incompatible package is installed.</span><br />";
        $action_button = "<input type=\"submit\" name=\"action_force\" value=\"Force Overwrite/Install\" />";
    }
}
else if($existingApp)
{
    echo "Existing Package is old or corrupt. Version could not be determined.";
    $action_button = "<input type=\"submit\" name=\"action_force\" value=\"Force Overwrite/Install\" />";
}
else
{
    echo "Package is not installed.";
    $action_button = "<input type=\"submit\" name=\"action_install\" value=\"Install\" />";
}
?>
</td>
</tr>
<tr>
<th>Web Elements</th>
<td><?php echo $cfg->hasScript() ? "Yes" : "No"; ?></td>
</tr>
<tr>
<th>Website</th>
<td><a href="<?php echo $cfg->getURL(); ?>"><?php echo $cfg->getURL(); ?></a></td>
</tr>
<tr>
<th>Database</th>
<td><?php echo $cfg->getDB(); ?></td>
</tr>
<tr><td colspan="2">
<form action="update_install.php" method="post">
<input type="hidden" name="password" value="<?php echo $_POST['password'];?>" />
<input type="hidden" name="randdir" value="<?php echo $_POST['randdir'];?>" />
<?php echo $action_button; ?>
<input type="submit" name="action_cancel" value="Cancel" />

</form>
</td>
</tr>
</table>
<?php
    }
        $output = ob_get_contents();
        ob_end_clean();
        $output .= "</body></html>";
        promptPage("Update and Application Installer",<<<EOD
<style type="text/css">
        .error {color: red;}
</style>
$output
EOD
,"2%","2%","96%");

?>
Return current item: FiForms Framework