View of /mambo/branches/4.5.4/administrator/components/com_installer/installer.class.php
Parent Directory
|
Revision Log
Revision 155 -
(download)
(annotate)
Thu Jan 19 22:18:23 2006 UTC (7 years, 4 months ago) by counterpoint
File size: 22749 byte(s)
Thu Jan 19 22:18:23 2006 UTC (7 years, 4 months ago) by counterpoint
File size: 22749 byte(s)
Revert to having database.php in /includes only. Fix language install/remove. Optimisation.
<?php /** * @version $Id: installer.class.php,v 1.1 2005/07/22 01:52:29 eddieajau Exp $ * @package Mambo * @subpackage Installer * @copyright (C) 2000 - 2005 Miro International Pty Ltd * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL * Mambo is Free Software */ /** ensure this file is being included by a parent file */ defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); class mosCommonInstallXML extends mosXMLDescription { var $type = ''; var $name = ''; var $special_attr = ''; var $group = ''; var $userdir = ''; var $admindir = ''; var $files = array(); var $images = array(); var $admin_files = array(); var $admin_images = array(); var $querytext = ''; var $queryid = 0; var $queries = array(); var $mainmenu = ''; var $subtext = ''; var $subact = ''; var $submenus = array(); var $submenuordering = 0; var $currentfile = ''; var $special_filetitle = ''; var $isspecial = false; var $special_file = null; var $installfile = ''; var $uninstallfile = ''; var $client = ''; var $media = array(); var $css = array(); var $user_dir = ''; var $admin_dir = ''; function tagNestIs ($next, $last=null) { if ($next == $this->opentags[1] AND ($last == null OR $last == $this->opentags[2])) return true; else return false; } function element_mosinstall ($attrs) { if (isset($attrs['TYPE'])) { $this->type = $attrs['TYPE']; $method = 'init_'.$attrs['TYPE']; $setup = array (&$this, $method); if (is_callable($setup)) $this->$method($attrs); else trigger_error ("XML error - no handler $method"); } else { $this->terminalError = true; trigger_error("XML error - mosinstall has no type"); } } function end_element_mosinstall () { if ($this->name = $this->getName($this->type)); else { $this->errors[] = 'XML Error - install has no name'; $this->terminalError = true; } } function element_filename ($attrs) { if (isset($attrs[$this->special_attr])) $this->special_filetitle = $attrs[$this->special_attr]; } function end_element_filename () { if ($this->special_filetitle) $this->special_file = array($this->special_filetitle, $this->currentfile); $this->special_filetitle = ''; $this->currentfile = ''; } function element_query ($attrs) { if (isset($attrs['ID'])) $this->queryid = $attrs['ID']; } function end_element_query () { if ($this->querytext) $this->queries[] = array ($this->querytext, $this->queryid); $this->querytext = ''; $this->queryid = 0; } function element_menu ($attrs) { if ($this->type != 'component') { $this->errors[] = 'XML error - MENU in XML not for component'; return; } if ($this->tagNestIs('SUBMENU', 'ADMINISTRATION')) { if (isset($attrs['ACT'])) $this->subact = '&act='.$attrs['ACT']; elseif (isset($attrs['TASK'])) $this->subact = '&task='.$attrs['ACT']; elseif (isset($attrs['LINK'])) $this->subact = $attrs['ACT']; } } function end_element_menu (){ if ($this->tagNestIs('SUBMENU', 'ADMINISTRATION')) $this->submenus[] = array ($this->subtext, $this->subact); $this->subact = $this->subtext = ''; } function init_component ($attrs) { } function init_module ($attrs) { $this->special_attr = 'MODULE'; if (isset($attrs['CLIENT'])) $this->client = $attrs['CLIENT']; } function init_mambot ($attrs) { $this->special_attr = 'MAMBOT'; if (isset($attrs['GROUP'])) { $this->group = $attrs['GROUP']; } else { $this->errors[] = 'XML Error - Mambot does not have a group'; $this->terminalError = true; } } function init_template ($attrs) { if (isset($attrs['CLIENT'])) $this->client = $attrs['CLIENT']; } function init_language ($attrs) { } } class mosInstallXML extends mosCommonInstallXML { function install () { $method = 'install_'.$this->type; $setup = array (&$this, $method); if (is_callable($setup)) $this->$method(); else trigger_error ("XML error - no installer $method"); } function character_data ($parser, $data) { $data = trim($data); if ($this->terminalError) return; $this->topLevelCharacterData($data); switch ($this->opentags[0]) { case 'FILENAME': $this->currentfile = $data; if ($this->tagNestIs('FILES', 'MOSINSTALL')) $this->files[] = $data; elseif ($this->tagNestIs('IMAGES', 'MOSINSTALL')) $this->images[] = $data; elseif ($this->tagNestIs('FILES', 'ADMINISTRATION')) $this->admin_files[] = $data; elseif ($this->tagNestIs('IMAGES', 'ADMINISTRATION')) $this->admin_images[] = $data; elseif ($this->tagNestIs('MEDIA', 'MOSINSTALL')) $this->media[] = $data; elseif ($this->tagNestIs('CSS', 'MOSINSTALL')) $this->css[] = $data; break; case 'QUERY': if ($this->tagNestIs('QUERIES', 'INSTALL')) { $this->querytext .= $data; } break; case 'MENU': if ($this->tagNestIs('ADMINISTRATION')) $this->mainmenu = $data; elseif ($this->tagNestIs('SUBMENU', 'ADMINISTRATION')) { $this->subtext .= $data; } break; case 'INSTALLFILE': $this->installfile = $data; break; case 'UNINSTALLFILE': $this->uninstallfile = $data; } } function createDirectories ($name) { if ($this->admin_dir) { $adirectory = new mosDirectory ($this->admin_dir); $type = $this->type; if (!$adirectory->createFresh()) { $this->errors[] = "Installer error - unable to create admin directory for $type $name"; return false; } } if ($this->user_dir) { $udirectory = new mosDirectory ($this->user_dir); if (!$udirectory->createFresh()) { $this->errors[] = "Installer error - unable to create user directory for $type $name"; return false; } return true; } } function createComponentMenu ($item, $name, $toplevel=0) { $text = $item[0]; $database = mamboDatabase::getInstance(); $component = new mosComponent($database); $component->name = $component->admin_menu_alt = $text; if ($toplevel == 0) $component->link = "option=$name"; $component->menuid = 0; $component->parent = $toplevel; $component->iscore = 0; $component->admin_menu_link = "option=$name"; if ($toplevel AND ($linkdetail = $item[1])) { if ($linkdetail[0] == '&') $component->admin_menu_link .= $linkdetail; else $component->admin_menu_link = $linkdetail; } $component->option = $name; $component->ordering = $toplevel ? $this->submenuordering++ : 0; $component->admin_menu_img = 'js/ThemeOffice/component.png'; $component->params = ''; if ($component->store()) return $component->id; $this->errors[] = 'Installer error- writing component menu SQL error'.$database->stderr(true); return 0; } function install_component (){ $com_name = 'com_'.strtolower($this->getName('component')); $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/components/'.$com_name); $this->admin_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/administrator/components/'.$com_name); $database = mamboDatabase::getInstance(); $sql = "SELECT COUNT(id) FROM #__components WHERE `option`='$com_name'"; $database->setQuery($sql); if ($count = $database->loadResult()) { $this->errors[] = "Installer error - component $com_name already installed"; $this->terminalError = true; return; } if (!$this->createDirectories($com_name)) return; $fmanager = mosFileManager::getInstance(); $here = mosPathName(dirname($this->xmlfile)); $fmanager->forceCopy($this->xmlfile, $this->admin_dir.basename($this->xmlfile)); foreach ($this->files as $ufile) $fmanager->forceCopy($here.$ufile, $this->user_dir.$ufile); foreach ($this->images as $uimage) $fmanager->forceCopy($here.$uimage, $this->user_dir.$uimage); foreach ($this->admin_files as $afile) $fmanager->forceCopy($here.$afile, $this->admin_dir.$afile); foreach ($this->admin_images as $aimage) $fmanager->forceCopy($here.$aimage, $this->admin_dir.$aimage); if ($this->installfile) $fmanager->forceCopy($here.$this->installfile, $this->admin_dir.$this->installfile); if ($this->uninstallfile) $fmanager->forceCopy($here.$this->uninstallfile, $this->admin_dir.$this->uninstallfile); foreach ($this->queries as $query) { $database->setQuery($query[0]); if (!$database->query()) { $this->errors[] = "Installer error - component $com_name SQL error".$database->stderr(true); } } if ($this->mainmenu) { $topid = $this->createComponentMenu (array($this->mainmenu), $com_name); foreach ($this->submenus as $submenu) { $this->createComponentMenu ($submenu, $com_name, $topid); } } if ($this->installfile) { $ifile = mosPathName($this->admin_dir).$this->installfile; if (is_file($ifile)) { require_once($ifile); com_install(); } } } function install_module () { if (!is_array($this->special_file)) { $this->errors[] = "Installer error - no file identified as the module"; $this->terminalError = true; return; } $special = $this->special_file[0]; $database = mamboDatabase::getInstance(); $name = $this->getName('module'); $client_id = $this->client == 'administrator' ? 1 : 0; $sql = "SELECT COUNT(id) FROM #__modules WHERE module='$special' AND client_id='$client_id'"; $database->setQuery($sql); if ($database->loadResult()) { $this->errors[] = "Installer error - module $name already installed"; $this->terminalError = true; return; } if ($client_id) $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/administrator/modules/'); else $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/modules'); $fmanager = mosFileManager::getInstance(); $here = mosPathName(dirname($this->xmlfile)); $fmanager->forceCopy($this->xmlfile, $this->user_dir.basename($this->xmlfile)); foreach ($this->files as $ufile) $fmanager->forceCopy($here.$ufile, $this->user_dir.$ufile); foreach ($this->images as $uimage) $fmanager->forceCopy($here.$uimage, $this->user_dir.$uimage); $module =& new mosModule($database); $module->title = $name; $module->ordering = 99; $module->position = 'left'; $module->showtitle = 1; $module->iscore = 0; $module->access = $client_id ? 99 : 0; $module->client_id = $client_id; $module->module = $special; $module->store(); $database->setQuery("INSERT INTO #__modules_menu VALUES ('$module->id', 0)"); if (!$database->query()) { $this->errors[] = "Installer error - SQL error ".$database->stderr(true); $this->terminalError = true; } $this->errors[] = $this->getDescription('module'); } function install_mambot () { if (!is_array($this->special_file)) { $this->errors[] = "Installer error - no file identified as the mambot"; $this->terminalError = true; return; } $database = mamboDatabase::getInstance(); $name = $this->getName('mambot'); $sql = "SELECT COUNT(id) FROM #__mambots WHERE element='$name'"; $database->setQuery($sql); if ($database->loadResult()) { $this->errors[] = "Installer error - mambot $name already installed"; $this->terminalError = true; return; } $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/mambots/'.$this->group); $fmanager = mosFileManager::getInstance(); $here = mosPathName(dirname($this->xmlfile)); $fmanager->forceCopy($this->xmlfile, $this->user_dir.basename($this->xmlfile)); foreach ($this->files as $ufile) $fmanager->forceCopy($here.$ufile, $this->user_dir.$ufile); foreach ($this->images as $uimage) $fmanager->forceCopy($here.$uimage, $this->user_dir.$uimage); $mambot =& new mosMambot($database); $mambot->name = $name; $mambot->ordering = 0; $mambot->folder = $this->group; $mambot->iscore = 0; $mambot->access = 0; $mambot->client_id = 0; $mambot->element = $this->special_file[0]; if ($mambot->store()) $this->errors[] = $this->getDescription('mambot'); else { $this->errors[] = "Installer error - SQL error ".$mambot->getError(); $this->terminalError = true; } $this->errors[] = $this->getDescription('mambot'); } function install_template () { $name = $this->getName('template'); $client_id = $this->client == 'administrator' ? 1 : 0; if ($client_id) $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/administrator/templates/'); else $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/templates/'.$name); if (file_exists($this->user_dir)) { $this->errors[] = "Installer error - template $name already installed"; $this->terminalError = true; return; } $fmanager = mosFileManager::getInstance(); $here = mosPathName(dirname($this->xmlfile)); $fmanager->forceCopy($this->xmlfile, $this->user_dir.basename($this->xmlfile)); foreach ($this->files as $ufile) $fmanager->forceCopy($here.$ufile, $this->user_dir.$ufile); foreach ($this->images as $uimage) $fmanager->forceCopy($here.$uimage, $this->user_dir.$uimage); foreach ($this->css as $acss) $fmanager->forceCopy($here.$acss, $this->user_dir.$acss); $mediadir = mamboCore::get('mosConfig_absolute_path').'/images/stories/'; foreach ($this->media as $simage) $fmanager->forceCopy($here.$simage, $mediadir.$simage); $this->errors[] = $this->getDescription('module'); } function install_language () { $name = $this->getName('language'); $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/language/'); $fmanager = mosFileManager::getInstance(); $here = mosPathName(dirname($this->xmlfile)); $fmanager->forceCopy($this->xmlfile, $this->user_dir.basename($this->xmlfile)); foreach ($this->files as $ufile) $fmanager->forceCopy($here.$ufile, $this->user_dir.$ufile); $this->errors[] = $this->getDescription('module'); } } class mosUninstallXML extends mosCommonInstallXML { function uninstall () { $method = 'uninstall_'.$this->type; $setup = array (&$this, $method); if (is_callable($setup)) $this->$method(); else trigger_error ("XML error - no uninstaller $method"); } function character_data ($parser, $data) { $data = trim($data); if ($this->terminalError) return; $this->topLevelCharacterData($data); switch ($this->opentags[0]) { case 'FILENAME': $this->currentfile = $data; if ($this->tagNestIs('FILES', 'MOSINSTALL')) $this->files[] = $data; elseif ($this->tagNestIs('IMAGES', 'MOSINSTALL')) $this->images[] = $data; elseif ($this->tagNestIs('FILES', 'ADMINISTRATION')) $this->admin_files[] = $data; elseif ($this->tagNestIs('IMAGES', 'ADMINISTRATION')) $this->admin_images[] = $data; elseif ($this->tagNestIs('MEDIA', 'MOSINSTALL')) $this->media[] = $data; elseif ($this->tagNestIs('CSS', 'MOSINSTALL')) $this->css[] = $data; break; case 'QUERY': if ($this->tagNestIs('QUERIES', 'UNINSTALL')) { $this->querytext .= $data; } break; case 'MENU': if ($this->tagNestIs('ADMINISTRATION')) $this->mainmenu = $data; elseif ($this->tagNestIs('SUBMENU', 'ADMINISTRATION')) { $this->subtext .= $data; } break; case 'INSTALLFILE': $this->installfile = $data; break; case 'UNINSTALLFILE': $this->uninstallfile = $data; } } function deleteFileSet ($files, $rootdir) { $dirs = array(); $fmanager = mosFileManager::getInstance(); foreach ($files as $file) { $parts = split ('/', $file); if (count($parts) > 1 AND !in_array($parts[0], $dirs)) $dirs[] = $parts[0]; $fmanager->deleteFile($rootdir.$file); } foreach ($dirs as $dir) $fmanager->deleteDirectory ($rootdir.$dir); } function uninstall_component (){ $com_name = 'com_'.strtolower($this->getName('component')); $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/components/'.$com_name); $this->admin_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/administrator/components/'.$com_name); if ($this->uninstallfile) { $ufile = mosPathName($this->admin_dir).$this->uninstallfile; if (is_file($ufile)) { require_once($ufile); com_uninstall(); } } $database = mamboDatabase::getInstance(); foreach ($this->queries as $query) { $database->setQuery($query[0]); if (!$database->query()) { $this->errors[] = "Installer error - component $com_name SQL error".$database->stderr(true); } } $udir = new mosDirectory($this->user_dir); $udir->deleteAll(); $adir = new mosDirectory($this->admin_dir); $adir->deleteAll(); $sql = "DELETE FROM #__components WHERE `option`='$com_name'"; $database->setQuery($sql); if (!$database->query()) { $this->errors[] = "Uninstaller error - component $com_name not fully deleted from database"; $this->terminalError = true; return; } } function uninstall_module () { $client_id = $this->client == 'administrator' ? 1 : 0; if ($client_id) $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/administrator/modules/'); else $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/modules'); $this->deleteFileSet($this->files, $this->user_dir); $this->deleteFileSet($this->images, $this->user_dir); if (!is_array($this->special_file)) { $this->errors[] = "Uninstaller error - no file identified as the module"; $this->terminalError = true; return; } $special = $this->special_file[0]; $database = mamboDatabase::getInstance(); $name = $this->getName('module'); $sql = "DELETE FROM #__modules WHERE module='$special' AND client_id='$client_id'"; $database->setQuery($sql); if (!$database->query()) { $this->errors[] = "Uninstaller error - module $name not deleted from database"; $this->terminalError = true; return; } $fmanager = mosFileManager::getInstance(); $fmanager->deleteFile($this->xmlfile); $this->errors[] = $this->getDescription('module'); } function uninstall_mambot () { $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/mambots/'.$this->group); $this->deleteFileSet($this->files, $this->user_dir); $this->deleteFileSet($this->images, $this->user_dir); $database = mamboDatabase::getInstance(); $name = $this->getName('mambot'); $element = $this->special_file[0]; $group = $this->group; $sql = "DELETE FROM #__mambots WHERE element='$element' AND folder='$group'"; $database->setQuery($sql); if (!$database->query()) { $this->errors[] = "Uninstaller error - mambot $name not removed from database"; $this->terminalError = true; return; } $fmanager = mosFileManager::getInstance(); $fmanager->deleteFile($this->xmlfile); $this->errors[] = $this->getDescription('mambot'); } /** * This routine is not called - uninstalling templates is done by just deleting * the whole directory. It might be better to manage the uninstall via the XML. * Just deleting the template leaves all the files in the "media" directory. * But the following code has never been tested. **/ function uninstall_template () { $name = $this->getName('template'); $client_id = $this->client == 'administrator' ? 1 : 0; if ($client_id) $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/administrator/templates/'); else $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/templates/'.$name); $userdir = new mosDirectory($this->user_dir); $userdir->deleteAll(); $mediadir = mamboCore::get('mosConfig_absolute_path').'/images/stories/'; $this->deleteFileSet($this->media, $mediadir); $fmanager = mosFileManager::getInstance(); $fmanager->deleteFile($this->xmlfile); $this->errors[] = $this->getDescription('module'); } function uninstall_language () { $this->user_dir = mosPathName(mamboCore::get('mosConfig_absolute_path').'/language/'); $fmanager = mosFileManager::getInstance(); $here = mosPathName(dirname($this->xmlfile)); $fmanager->deleteFile($this->user_dir.basename($this->xmlfile)); foreach ($this->files as $ufile) $fmanager->deleteFile($this->user_dir.$ufile); $this->errors[] = $this->getDescription('module'); } } /** * Installer class * @package Mambo * @subpackage Installer * @abstract */ class mosInstaller { var $archiveName = ''; var $extractDir = ''; var $cleanDir = ''; var $errors = array(); /** * Extracts the package archive file * @return boolean True on success, False on error */ function extractArchive($filename) { $mosConfig_absolute_path = mamboCore::get('mosConfig_absolute_path'); $base_Dir = mosPathName($mosConfig_absolute_path.'/media'); $this->archiveName = $base_Dir.$filename; $tmpdir = uniqid('install_'); $this->extractDir = $this->cleanDir = mosPathName($base_Dir.uniqid('install_')); if (eregi( '.zip$', $filename )) { // Extract functions require_once( $mosConfig_absolute_path . '/administrator/includes/pcl/pclzip.lib.php' ); require_once( $mosConfig_absolute_path . '/administrator/includes/pcl/pclerror.lib.php' ); //require_once( $mosConfig_absolute_path . '/administrator/includes/pcl/pcltrace.lib.php' ); //require_once( $mosConfig_absolute_path . '/administrator/includes/pcl/pcltar.lib.php' ); $zipfile = new PclZip( $this->archiveName ); $ret = $zipfile->extract( PCLZIP_OPT_PATH, $this->extractDir ); if($ret == 0) { $this->errors[] = 'Unrecoverable error "'.$zipfile->errorName(true).'"'; return false; } } else { require_once( $mosConfig_absolute_path . '/includes/Archive/Tar.php' ); $archive =& new Archive_Tar( $this->archiveName ); $archive->setErrorHandling( PEAR_ERROR_PRINT ); if (!$archive->extractModify( $this->extractDir, '' )) { $this->errors[] = 'Extract TAR Error'; return false; } } // Try to find the correct install dir. in case that the package have subdirs // Save the install dir for later cleanup $dir =& new mosDirectory($this->extractDir); $singledir = $dir->soleDir(); if ($singledir) $this->extractDir = mosPathName($this->extractDir.$singledir); return true; } /** * Custom install method * @param boolean True if installing from directory */ function install($p_fromdir = null) { if (!is_null($p_fromdir)) $this->extractDir = $p_fromdir; $here = $this->extractDir; $installdir =& new mosDirectory($here); $xmlfiles = $installdir->listFiles('.xml$'); foreach ($xmlfiles as $file) { $parser = new mosInstallXML ($here.$file); $parser->install(); $this->errors = array_merge($this->errors, $parser->errors); if ($parser->terminalError) return false; } return true; } function cleanUpInstall () { if ($this->archiveName) { $fmanager = mosFileManager::getInstance(); $fmanager->deleteFile($this->archiveName); } if ($this->cleanDir) { $edir = new mosDirectory ($this->cleanDir); $edir->deleteAll(); } } function getError () { if (count($this->errors)) return $this->errors[0]; else return 'No errors'; } } ?>
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

