|
From: <jhe...@us...> - 2002-11-12 11:01:03
|
Update of /cvsroot/upcase-project/UpCase/lib
In directory usw-pr-cvs1:/tmp/cvs-serv23071
Added Files:
uc_installer.php
Log Message:
First package installer class
--- NEW FILE: uc_installer.php ---
<?php
include_once("lib/uc_sql.php");
include_once("lib/uc_utils.php");
include_once("lib/Tar.php");
define("MYSQL_QUERIES", "/upcase/mysql.inc");
define("PACKAGE_SETUP", "/upcase/setup.inc");
define("PACKAGE_NOT_INSTALLED", 0);
define("PACKAGE_FULLY_INSTALLED", 1);
define("PACKAGE_PARTIALY_INSTALLED", -1);
class UcPackageDBInfo
{
var $name;
var $host;
var $user;
var $passwd;
var $prefix;
}
class UcInstaller
{
var $metaBackup;
var $db;
function UcInstaller()
{
$cfg = $GLOBALS["ucConfig"];
$this->metaBackup = $cfg->filesRoot . "/packages/db";
$this->db = new UcSql();
$this->tmpDir = $cfg->filesRoot . "/packages/dl";
}
// Return an array of
// [pkgversion, pkgurl, pkgstatus]
function getStatus($package)
{
$query = sprintf($GLOBALS["ucsql_packagestatus"], $package->name);
$res = $this->db->Execute($query)
or die("Unable to look up package: " . $this->db->ErrorMsg()
. "QUERY: " . $query);
if ($res->RowCount() == 0)
return array();
else if ($res->RowCount() > 0)
{
$ar = array();
while ($o = $res->FetchNextObject(true))
{
$pkg = array();
$pkg["version"] = $o->PKGVERSION;
$pkg["url"] = $o->PKGPATH;
if ($o->INSTALLED == 1)
$pkg["status"] = PACKAGE_FULLY_INSTALLED;
$ar[] = $pkg;
}
return $ar;
}
else
die("Error while counting: " . $this->db->ErrorMsg());
}
function saveMetaData($package)
{
$srcFile = $package->metaDataFile;
$dstFile = $this->metaBackup . "/" . $package->name
. "-" . $package->version . ".inc";
if (!file_exists($dstFile))
copy($srcFile, $dstFile);
}
function download($package)
{
print("Downloading<br>");
$res = download($package->sourceDir . "/" . $package->source,
$this->tmpDir . "/" . $package->source);
if ($res < 0)
return false;
return true;
}
// Check if a path does not contains '..' and symlinks
// Used to check the validity of the module URL given
// to the package installer.
function checkPathValidity($absolutePath)
{
// Absolute path ?
if (preg_match("/^\//", $absolutePath) < 1)
return false;
// Path contains .. ?
if (preg_match("/\.\./", $absolutePath) > 0)
return false;
// Check for symlinks
$ar = explode("/", $absolutePath);
$newPath = "";
foreach ($ar as $a)
{
if (!empty($a))
{
$newPath .= "/" . $a;
if (!file_exists($newPath))
break;
if (is_link($newPath))
return false;
}
}
return true;
}
function unpack($package, $path)
{
print("$path<br>");
$archive = new Archive_Tar($this->tmpDir
. "/" . $package->source, true);
return $archive->extractModify($path, $package->name . "-"
. $package->version
. "-uc");
}
function setupMySQL($package, $path, $tblPrefix)
{
$tblprefix = $tblPrefix . "_";
include($path . MYSQL_QUERIES);
foreach($queries as $query)
{
$this->db->Execute($query)
or die("Unable to execute query: " . $query . "<br>\n"
. "Error: " . $this->db->ErrorMsg());
}
return true;
}
function setupPackage($package, $path, $tblPrefix)
{
$cfg = $GLOBALS["ucConfig"];
$dbinf = new UcPackageDBInfo();
$dbinf->name = $cfg->dbName;
$dbinf->user = $cfg->dbUser;
$dbinf->passwd = $cfg->dbPasswd;
$dbinf->host = $cfg->dbHost;
$dbinf->prefix = $tblPrefix . "_";
include_once($path . PACKAGE_SETUP);
setup($dbinf, $cfg->wwwModules . "/" . basename($path),
$path)
or die("Unable to setup package");
return true;
}
function registerPackage($package, $path, $tblPrefix)
{
$query = sprintf($GLOBALS["ucsql_packageregister"],
$package->name, $package->version,
$path, $tblPrefix);
$this->db->Execute($query)
or die("Unable to register package: " . $this->db->ErrorMsg());
return true;
}
function setInstallComplete($package, $path, $tblPrefix)
{
$query = sprintf($GLOBALS["ucsql_packageinstcomp"],
$package->name, $package->version,
$path, $tblPrefix);
print("$query<br>");
$this->db->Execute($query)
or die("Unable to flag package as installed: " .
$this->db->ErrorMsg());
}
function install($package, $pkgRelUrl, $tblPrefix)
{
$cfg = $GLOBALS["ucConfig"];
$pkgPath = $cfg->modulesRoot . "/" . $pkgRelUrl;
$this->download($package)
or die("Unable to get package archive");
$this->checkPathValidity($pkgPath)
or die("Invalid URL");
$this->registerPackage($package, $pkgRelUrl, $tblPrefix)
or die("Unable to register package to be installed");
createDirectory($pkgPath)
or die("Unable to create directory $pkgPath");
$this->unpack($package, $pkgPath)
or die("Unable to unpack package");
$this->setupMySQL($package, $pkgPath, $tblPrefix)
or die("Unable to set up MySQL");
$this->setupPackage($package, $pkgPath, $tblPrefix)
or die("Unable to set up package");
$this->setInstallComplete($package, $pkgRelUrl, $tblPrefix);
}
function uninstall()
{
}
}
?>
|