function Install($tmpfilepath, $updateOnce = false) { $result = FALSE; import('com.solarix.ampoliros.core.Ampoliros'); $amp = Ampoliros::instance('Ampoliros'); if ($amp->GetState() == Ampoliros::STATE_DEBUG) { $GLOBALS['gEnv']['runtime']['debug']['loadtime']->Mark('moduleinstallstart'); } if (file_exists($tmpfilepath)) { import('carthag.io.archive.Archive'); // Moves temp file to modules repository and extracts it // $fname = MODULE_PATH . basename($tmpfilepath); @copy($tmpfilepath, $fname); $basetmpdir = $tmpdir = TMP_PATH . 'modinst/' . md5(microtime()); @mkdir($tmpdir, 0755); $olddir = getcwd(); @chdir($tmpdir); //@system( escapeshellcmd( 'tar zxf '.$fname ) ); $archive_format = ARCHIVE_TGZ; if (substr($fname, -4) == '.zip') { $archive_format = ARCHIVE_ZIP; } $mod_archive = new Archive($fname, $archive_format); $mod_archive->Extract($tmpdir); // Checks if the files are into a directory instead of the root // if (!@is_dir($tmpdir . '/defs')) { $dhandle = opendir($tmpdir); while (FALSE != ($file = readdir($dhandle))) { if ($file != '.' && $file != '..' && is_dir($tmpdir . '/' . $file . '/defs')) { $tmpdir = $tmpdir . '/' . $file; } } closedir($dhandle); } $this->basedir = $tmpdir; // Checks for definition and structure files // if (file_exists($tmpdir . '/defs/' . Module::BUNDLEDEF_FILE)) { $modules_array = file($tmpdir . '/defs/' . Module::BUNDLEDEF_FILE); $result = TRUE; while (list(, $module) = each($modules_array)) { $module = trim($module); if (strlen($module) and file_exists($tmpdir . '/modules/' . $module)) { $temp_module = new Module($this->ampdb); if (!$temp_module->Install($tmpdir . '/modules/' . $module)) { $result = FALSE; } } } } else { if (file_exists($tmpdir . '/defs/' . Module::STRUCTURE_FILE) and file_exists($tmpdir . '/defs/' . Module::GENERALDEF_FILE)) { $genconfig = new ConfigFile($tmpdir . '/defs/' . Module::GENERALDEF_FILE); $this->modname = $genconfig->Value('MODULEIDNAME'); // Checks if the module has been already installed // $tmpquery = $this->ampdb->Execute('SELECT id,modfile FROM modules WHERE modid=' . $this->ampdb->Format_Text($this->modname)); if (!$tmpquery->NumRows()) { // Module is new, so it will be installed // // Dependencies check // $this->unmetdeps = array(); $this->unmetsuggs = array(); $moddeps = new ModuleDep($this->ampdb); $deps = $moddeps->ExplodeDeps($genconfig->Value('MODULEDEPENDENCIES')); $suggs = $moddeps->ExplodeDeps($genconfig->Value('MODULESUGGESTIONS')); if ($deps != FALSE) { $this->unmetdeps = $moddeps->CheckModuleDeps(0, '', $deps); } else { $this->unmetdeps == FALSE; } // Suggestions check // if ($suggs != FALSE) { $unmetsuggs = $moddeps->CheckModuleDeps(0, '', $suggs); if (is_array($unmetsuggs)) { $this->unmetsuggs = $unmetsuggs; } } // If dependencies are ok, go on // if ($this->unmetdeps == FALSE) { // Gets serial number for the module // $this->serial = $this->ampdb->NextSeqValue('modules_id_seq'); $this->ampdb->Execute('INSERT INTO modules VALUES ( ' . $this->serial . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULEIDNAME')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULEVERSION')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULEDATE')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULEDESCRIPTION')) . ',' . $this->ampdb->Format_Text(basename($tmpfilepath)) . ',' . $this->ampdb->Format_Text($this->ampdb->fmtfalse) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULE_AUTHOR')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULE_AUTHOR_EMAIL')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULE_AUTHOR_SITE')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULE_SUPPORT_EMAIL')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULE_BUGS_EMAIL')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULE_COPYRIGHT')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULE_LICENSE')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULE_LICENSE_FILE')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULE_MAINTAINER')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULE_MAINTAINER_EMAIL')) . ',' . $this->ampdb->Format_Text($genconfig->Value('MODULE_CATEGORY')) . ')'); // Module dir creation // @mkdir(MODULE_PATH . $genconfig->Value('MODULEIDNAME'), 0755); // Defs files // if ($dhandle = @opendir($tmpdir . '/defs')) { while (FALSE != ($file = readdir($dhandle))) { if ($file != '.' && $file != '..' && is_file($tmpdir . '/defs/' . $file)) { @copy($tmpdir . '/defs/' . $file, MODULE_PATH . $genconfig->Value('MODULEIDNAME') . '/' . $file); } } closedir($dhandle); } // Adds modules dependencies // $moddeps->AddDepsArray($genconfig->Value('MODULEIDNAME'), $deps, DEPTYPE_DEP); $moddeps->AddDepsArray($genconfig->Value('MODULEIDNAME'), $suggs, DEPTYPE_SUGG); $this->SetSubModules(explode(',', trim($genconfig->Value('MODULE_SUBMODULES'), ' ,'))); $this->HandleStructure($tmpdir . '/defs/' . Module::STRUCTURE_FILE, Module::INSTALL_MODE_INSTALL, $tmpdir); if (strlen($genconfig->Value('MODULE_LICENSE_FILE')) and file_exists($tmpdir . '/' . $genconfig->Value('MODULE_LICENSE_FILE'))) { @copy($tmpdir . '/' . $genconfig->Value('MODULE_LICENSE_FILE'), MODULE_PATH . $genconfig->Value('MODULEIDNAME') . '/' . $genconfig->Value('MODULE_LICENSE_FILE')); } // Checks if it is an extension module // $genconfig = new ConfigFile($tmpdir . '/defs/' . Module::GENERALDEF_FILE); $ext = $this->ampdb->fmtfalse; if ($genconfig->Value('ONLYEXTENSION') == 'y') { $ext = $this->ampdb->fmttrue; $this->onlyextension = TRUE; } else { if ($genconfig->Value('ONLYEXTENSION') == 'n') { $ext = $this->ampdb->fmtfalse; $this->onlyextension = FALSE; } else { if ($this->onlyextension) { $ext = $this->ampdb->fmttrue; } } } $this->ampdb->Execute('UPDATE modules SET onlyextension=' . $this->ampdb->Format_Text($ext) . ' WHERE modid=' . $this->ampdb->Format_Text($this->modname)); $result = TRUE; if ($GLOBALS['gEnv']['core']['config']->Value('ALERT_ON_MODULE_OPERATION') == '1') { import('com.solarix.ampoliros.security.SecurityLayer'); $amp_security = new SecurityLayer(); $amp_security->SendAlert('Module ' . $this->modname . ' has been installed'); unset($amp_security); } if ($result == true) { if ($GLOBALS['gEnv']['core']['edition'] == AMP_EDITION_ENTERPRISE and $this->modname != 'ampoliros' and $ext != $this->ampdb->fmttrue) { $sites_query = $GLOBALS['gEnv']['root']['db']->Execute('SELECT id FROM sites'); if ($sites_query->NumRows()) { $this->Enable($sites_query->Fields('id')); } } import('com.solarix.ampoliros.io.log.Logger'); $log = new Logger(AMP_LOG); $log->LogEvent('Ampoliros', 'Installed module ' . $this->modname, LOGGER_NOTICE); } } } else { $moddata = $tmpquery->Fields(); $this->serial = $moddata['id']; // Module will be updated // if ($this->serial) { // Dependencies check // $this->unmetdeps = array(); $this->unmetsuggs = array(); $moddeps = new ModuleDep($this->ampdb); $deps = $moddeps->ExplodeDeps($genconfig->Value('MODULEDEPENDENCIES')); $suggs = $moddeps->ExplodeDeps($genconfig->Value('MODULESUGGESTIONS')); if ($deps != FALSE) { $this->unmetdeps = $moddeps->CheckModuleDeps(0, '', $deps); } else { $this->unmetdeps == FALSE; } // Suggestions check // if ($suggs != FALSE) { $unmetsuggs = $moddeps->CheckModuleDeps(0, '', $suggs); if (is_array($unmetsuggs)) { $this->unmetsuggs = $unmetsuggs; } } // If dependencies are ok, go on // if ($this->unmetdeps == FALSE) { // Creates lock file // touch(TMP_PATH . '.upgrading_system'); // :WARNING: evil 20020506: possible problems on Windows systems // It has a 'permission denied'. // Removes old module file // if (basename($fname) != $moddata['modfile'] and file_exists(MODULE_PATH . $moddata['modfile'])) { @unlink(MODULE_PATH . $moddata['modfile']); } // Updates modules table // $this->ampdb->Execute('UPDATE modules SET modversion=' . $this->ampdb->Format_Text($genconfig->Value('MODULEVERSION')) . ', moddate=' . $this->ampdb->Format_Text($genconfig->Value('MODULEDATE')) . ', moddesc=' . $this->ampdb->Format_Text($genconfig->Value('MODULEDESCRIPTION')) . ', modfile=' . $this->ampdb->Format_Text(basename($tmpfilepath)) . ', author=' . $this->ampdb->Format_Text($genconfig->Value('MODULE_AUTHOR')) . ', authoremail=' . $this->ampdb->Format_Text($genconfig->Value('MODULE_AUTHOR_EMAIL')) . ', authorsite=' . $this->ampdb->Format_Text($genconfig->Value('MODULE_AUTHOR_SITE')) . ', supportemail=' . $this->ampdb->Format_Text($genconfig->Value('MODULE_SUPPORT_EMAIL')) . ', bugsemail=' . $this->ampdb->Format_Text($genconfig->Value('MODULE_BUGS_EMAIL')) . ', copyright=' . $this->ampdb->Format_Text($genconfig->Value('MODULE_COPYRIGHT')) . ', license=' . $this->ampdb->Format_Text($genconfig->Value('MODULE_LICENSE')) . ', licensefile=' . $this->ampdb->Format_Text($genconfig->Value('MODULE_LICENSE_FILE')) . ', maintainer=' . $this->ampdb->Format_Text($genconfig->Value('MODULE_MAINTAINER')) . ', maintaineremail=' . $this->ampdb->Format_Text($genconfig->Value('MODULE_MAINTAINER_EMAIL')) . ', category=' . $this->ampdb->Format_Text($genconfig->Value('MODULE_CATEGORY')) . ' WHERE id=' . (int) $this->serial); $genconfig = new ConfigFile($tmpdir . '/defs/' . Module::GENERALDEF_FILE); // Script files - only before handlestructure // if ($dhandle = @opendir($tmpdir . '/defs')) { while (FALSE != ($file = readdir($dhandle))) { if ($file != '.' and $file != '..' and $file != Module::STRUCTURE_FILE and $file != Module::GENERALDEF_FILE and is_file($tmpdir . '/defs/' . $file)) { @copy($tmpdir . '/defs/' . $file, MODULE_PATH . $genconfig->Value('MODULEIDNAME') . '/' . $file); } } closedir($dhandle); } $this->HandleStructure($tmpdir . '/defs/' . Module::STRUCTURE_FILE, Module::INSTALL_MODE_UPDATE, $tmpdir); if (strlen($genconfig->Value('MODULE_LICENSE_FILE')) and file_exists($tmpdir . '/' . $genconfig->Value('MODULE_LICENSE_FILE'))) { @copy($tmpdir . '/' . $genconfig->Value('MODULE_LICENSE_FILE'), MODULE_PATH . $genconfig->Value('MODULEIDNAME') . '/' . $genconfig->Value('MODULE_LICENSE_FILE')); } // Defs files - only after handlestructure // @copy($tmpdir . '/defs/' . Module::STRUCTURE_FILE, MODULE_PATH . $genconfig->Value('MODULEIDNAME') . '/' . Module::STRUCTURE_FILE); @copy($tmpdir . '/defs/' . Module::GENERALDEF_FILE, MODULE_PATH . $genconfig->Value('MODULEIDNAME') . '/' . Module::GENERALDEF_FILE); // Checks if it is an extension module // $ext = $this->ampdb->fmtfalse; if ($genconfig->Value('ONLYEXTENSION') == 'y') { $ext = $this->ampdb->fmttrue; $this->onlyextension = TRUE; } else { if ($genconfig->Value('ONLYEXTENSION') == 'n') { $ext = $this->ampdb->fmtfalse; $this->onlyextension = FALSE; } else { if ($this->onlyextension) { $ext = $this->ampdb->fmttrue; } } } $this->ampdb->Execute('UPDATE modules SET onlyextension=' . $this->ampdb->Format_Text($ext) . ' WHERE modid=' . $this->ampdb->Format_Text($this->modname)); $this->SetSubModules(explode(',', trim($genconfig->Value('MODULE_SUBMODULES'), ' ,'))); if ($this->modname != 'ampoliros') { // Remove old dependencies // $moddeps->RemAllDep($this->serial); // Adds new modules dependencies // $moddeps->AddDepsArray($genconfig->Value('MODULEIDNAME'), $deps, DEPTYPE_DEP); $moddeps->AddDepsArray($genconfig->Value('MODULEIDNAME'), $suggs, DEPTYPE_SUGG); } $result = TRUE; if (function_exists('apc_reset_cache')) { apc_reset_cache(); } if ($updateOnce == FALSE) { $this->Install($tmpfilepath, true); // Removes lock file // unlink(TMP_PATH . '.upgrading_system'); if ($GLOBALS['gEnv']['core']['config']->Value('ALERT_ON_MODULE_OPERATION') == '1') { Carthag::import('com.solarix.ampoliros.security.SecurityLayer'); $amp_security = new SecurityLayer(); $amp_security->SendAlert('Module ' . $this->modname . ' has been updated'); unset($amp_security); } if ($result == TRUE) { import('com.solarix.ampoliros.io.log.Logger'); $log = new Logger(AMP_LOG); $log->LogEvent('Ampoliros', 'Updated module ' . $this->modname, LOGGER_NOTICE); } } } /* else $this->mLog->LogEvent( 'ampoliros.modules_library.modules_class.install', 'Structure definition file for module '.$this->modname.' does not exists', LOGGER_ERROR ); */ } else { import('com.solarix.ampoliros.io.log.Logger'); $log = new Logger(AMP_LOG); $log->LogEvent('ampoliros.modules_library.modules_class.install', 'Empty module serial', LOGGER_ERROR); } } } else { import('com.solarix.ampoliros.io.log.Logger'); $log = new Logger(AMP_LOG); if (!file_exists($tmpdir . '/defs/' . Module::STRUCTURE_FILE)) { $log->LogEvent('ampoliros.modules_library.modules_class.install', 'Module structure file ' . $tmpdir . '/defs/' . Module::STRUCTURE_FILE . ' not found', LOGGER_ERROR); } if (!file_exists($tmpdir . '/defs/' . Module::GENERALDEF_FILE)) { $log->LogEvent('ampoliros.modules_library.modules_class.install', 'Module definition file ' . $tmpdir . '/defs/' . Module::GENERALDEF_FILE . ' not found', LOGGER_ERROR); } } } // Cleans up temp stuff // @chdir($olddir); RecRemoveDir($basetmpdir); if (file_exists($tmpfilepath)) { @unlink($tmpfilepath); } } else { if (!file_exists($tmpfilepath)) { import('com.solarix.ampoliros.io.log.Logger'); $log = new Logger(AMP_LOG); $log->LogEvent('ampoliros.modules_library.modules_class.install', 'Temporary module file (' . $tmpfilepath . ') does not exists', LOGGER_ERROR); } } if ($amp->getState() == Ampoliros::STATE_DEBUG) { $GLOBALS['gEnv']['runtime']['debug']['loadtime']->Mark('moduleinstallend'); import('com.solarix.ampoliros.io.log.Logger'); $log = new Logger(AMP_LOG); $log->LogEvent('ampoliros.modules_library.module_class.install', 'Module installation load time: ' . $GLOBALS['gEnv']['runtime']['debug']['loadtime']->GetSectionLoad('moduleinstallend'), LOGGER_DEBUG); } return $result; }