Example #1
0
 public function install($tmpfilepath, $updateOnce = false)
 {
     // Checks if the given path is a directory. This may happen when not
     // giving a file to the application installation page.
     if (is_dir($tmpfilepath)) {
         return false;
     }
     $result = false;
     $innomatic = $this->container;
     if ($innomatic->getState() == \Innomatic\Core\InnomaticContainer::STATE_DEBUG) {
         $innomatic->getLoadTimer()->Mark('applicationinstallstart');
     }
     if (file_exists($tmpfilepath)) {
         // Moves temp file to applications repository and extracts it
         //
         $fname = $this->container->getHome() . 'core/applications/' . basename($tmpfilepath);
         @copy($tmpfilepath, $fname);
         $basetmpdir = $tmpdir = $this->container->getHome() . 'core/temp/appinst/' . md5(microtime());
         @mkdir($tmpdir, 0755);
         $olddir = getcwd();
         @chdir($tmpdir);
         if (substr($fname, -4) == '.zip') {
         } else {
             try {
                 $appArchive = new \PharData($fname);
                 $tarFileName = substr($fname, 0, strpos($fname, '.')) . '.tar';
                 if (file_exists($tarFileName)) {
                     unlink($tarFileName);
                 }
                 $appArchive->decompress();
             } catch (\BadMethodCallException $e) {
             }
             try {
                 $appArchive->extractTo($tmpdir);
             } catch (Exception $e) {
             }
         }
         // Checks if the files are into a directory instead of the root
         //
         if (!@is_dir($tmpdir . '/setup')) {
             $dhandle = opendir($tmpdir);
             while (false != ($file = readdir($dhandle))) {
                 if ($file != '.' && $file != '..' && (is_dir($tmpdir . '/' . $file . '/setup') or is_dir($tmpdir . '/' . $file . '/innomatic/setup'))) {
                     if (is_dir($tmpdir . '/' . $file . '/setup')) {
                         $tmpdir = $tmpdir . '/' . $file;
                     } else {
                         $tmpdir = $tmpdir . '/' . $file . '/innomatic';
                     }
                     break;
                 }
             }
             closedir($dhandle);
         }
         $this->basedir = $tmpdir;
         // Checks for definition and structure files
         //
         if (file_exists($tmpdir . '/setup/bundle.ini')) {
             $applicationsArray = file($tmpdir . '/setup/bundle.ini');
             $result = true;
             while (list(, $application) = each($applicationsArray)) {
                 $application = trim($application);
                 if (strlen($application) and file_exists($tmpdir . '/applications/' . $application)) {
                     $tempApplication = new Application($this->rootda);
                     if (!$tempApplication->Install($tmpdir . '/applications/' . $application)) {
                         $result = false;
                     }
                 }
             }
         } elseif (file_exists($tmpdir . '/setup/application.xml')) {
             $genconfig = $this->parseApplicationDefinition($tmpdir . '/setup/application.xml');
             $this->appname = $genconfig['ApplicationIdName'];
             // Checks if the application has been already installed
             //
             $tmpquery = $this->rootda->execute('SELECT id,appfile FROM applications WHERE appid=' . $this->rootda->formatText($this->appname));
             if (!$tmpquery->getNumberRows()) {
                 // Application is new, so it will be installed
                 //
                 // Dependencies check
                 //
                 $this->unmetdeps = array();
                 $this->unmetsuggs = array();
                 $appdeps = new ApplicationDependencies();
                 $deps = $appdeps->explodeDependencies($genconfig['ApplicationDependencies']);
                 $suggs = $appdeps->explodeDependencies($genconfig['ApplicationSuggestions']);
                 if ($deps != false) {
                     $this->unmetdeps = $appdeps->checkApplicationDependencies(0, '', $deps);
                 } else {
                     $this->unmetdeps = false;
                 }
                 // Suggestions check
                 //
                 if ($suggs != false) {
                     $unmetsuggs = $appdeps->checkApplicationDependencies(0, '', $suggs);
                     if (is_array($unmetsuggs)) {
                         $this->unmetsuggs = $unmetsuggs;
                     }
                 }
                 // If dependencies are ok, go on
                 //
                 if ($this->unmetdeps == false) {
                     // Gets serial number for the application
                     //
                     $this->serial = $this->rootda->getNextSequenceValue('applications_id_seq');
                     $this->rootda->execute('INSERT INTO applications VALUES ( ' . $this->serial . ',' . $this->rootda->formatText($genconfig['ApplicationIdName']) . ',' . $this->rootda->formatText($genconfig['ApplicationVersion']) . ',' . $this->rootda->formatText($genconfig['ApplicationDate']) . ',' . $this->rootda->formatText($genconfig['ApplicationDescription']) . ',' . $this->rootda->formatText(basename($tmpfilepath)) . ',' . $this->rootda->formatText($this->rootda->fmtfalse) . ',' . $this->rootda->formatText($genconfig['ApplicationAuthor']) . ',' . $this->rootda->formatText($genconfig['ApplicationAuthorEmail']) . ',' . $this->rootda->formatText($genconfig['ApplicationAuthorWeb']) . ',' . $this->rootda->formatText($genconfig['ApplicationSupportEmail']) . ',' . $this->rootda->formatText($genconfig['ApplicationBugsEmail']) . ',' . $this->rootda->formatText($genconfig['ApplicationCopyright']) . ',' . $this->rootda->formatText($genconfig['ApplicationLicense']) . ',' . $this->rootda->formatText($genconfig['ApplicationLicenseFile']) . ',' . $this->rootda->formatText($genconfig['ApplicationChangesFile']) . ',' . $this->rootda->formatText($genconfig['ApplicationMaintainer']) . ',' . $this->rootda->formatText($genconfig['ApplicationMaintainerEmail']) . ',' . $this->rootda->formatText($genconfig['ApplicationCategory']) . ',' . $this->rootda->formatText($genconfig['ApplicationIconFile']) . ')');
                     // Application dir creation
                     //
                     @mkdir($this->container->getHome() . 'core/applications/' . $genconfig['ApplicationIdName'], 0755);
                     // Defs files
                     //
                     if ($dhandle = @opendir($tmpdir . '/setup')) {
                         while (false != ($file = readdir($dhandle))) {
                             if ($file != '.' && $file != '..' && is_file($tmpdir . '/setup/' . $file)) {
                                 @copy($tmpdir . '/setup/' . $file, $this->container->getHome() . 'core/applications/' . $genconfig['ApplicationIdName'] . '/' . $file);
                             }
                         }
                         closedir($dhandle);
                     }
                     // Adds applications dependencies
                     //
                     $appdeps->addDependenciesArray($genconfig['ApplicationIdName'], $deps, ApplicationDependencies::TYPE_DEPENDENCY);
                     $appdeps->addDependenciesArray($genconfig['ApplicationIdName'], $suggs, ApplicationDependencies::TYPE_SUGGESTION);
                     $this->setOptions(explode(',', trim($genconfig['ApplicationOptions'], ' ,')));
                     $this->HandleStructure($tmpdir . '/setup/application.xml', Application::INSTALL_MODE_INSTALL, $tmpdir);
                     if (strlen($genconfig['ApplicationLicenseFile']) and file_exists($tmpdir . '/setup/' . $genconfig['ApplicationLicenseFile'])) {
                         @copy($tmpdir . '/setup/' . $genconfig['ApplicationLicenseFile'], $this->container->getHome() . 'core/applications/' . $genconfig['ApplicationIdName'] . '/' . $genconfig['ApplicationLicenseFile']);
                     }
                     if (strlen($genconfig['ApplicationChangesFile']) and file_exists($tmpdir . '/setup/' . $genconfig['ApplicationChangesFile'])) {
                         @copy($tmpdir . '/setup/' . $genconfig['ApplicationChangesFile'], $this->container->getHome() . 'core/applications/' . $genconfig['ApplicationIdName'] . '/' . $genconfig['ApplicationChangesFile']);
                     }
                     if (strlen($genconfig['ApplicationIconFile']) and file_exists($tmpdir . '/setup/' . $genconfig['ApplicationIconFile'])) {
                         @copy($tmpdir . '/setup/' . $genconfig['ApplicationIconFile'], $this->container->getHome() . 'core/applications/' . $genconfig['ApplicationIdName'] . '/' . $genconfig['ApplicationIconFile']);
                     }
                     // Checks if it is an extension application
                     //
                     $genconfig = $this->parseApplicationDefinition($tmpdir . '/setup/application.xml');
                     $ext = $this->rootda->fmtfalse;
                     if ($genconfig['ApplicationIsExtension'] == 'y') {
                         $ext = $this->rootda->fmttrue;
                         $this->onlyextension = true;
                     } elseif ($genconfig['ApplicationIsExtension'] == 'n') {
                         $ext = $this->rootda->fmtfalse;
                         $this->onlyextension = false;
                     } elseif ($this->onlyextension) {
                         $ext = $this->rootda->fmttrue;
                     }
                     $this->rootda->execute('UPDATE applications SET onlyextension=' . $this->rootda->formatText($ext) . ' WHERE appid=' . $this->rootda->formatText($this->appname));
                     $result = true;
                     if ($this->container->getConfig()->Value('SecurityAlertOnApplicationOperation') == '1') {
                         $innomaticSecurity = new \Innomatic\Security\SecurityManager();
                         $innomaticSecurity->SendAlert('Application ' . $this->appname . ' has been installed');
                         unset($innomaticSecurity);
                     }
                     if ($result == true) {
                         if ($this->container->getEdition() == \Innomatic\Core\InnomaticContainer::EDITION_SINGLETENANT and $this->appname != 'innomatic' and $ext != $this->rootda->fmttrue) {
                             $domainsQuery = \Innomatic\Core\InnomaticContainer::instance('\\Innomatic\\Core\\InnomaticContainer')->getDataAccess()->execute('SELECT id FROM domains');
                             if ($domainsQuery->getNumberRows()) {
                                 $this->Enable($domainsQuery->getFields('id'));
                             }
                         }
                         $log = $this->container->getLogger();
                         $log->logEvent('Innomatic', 'Installed application ' . $this->appname, \Innomatic\Logging\Logger::NOTICE);
                     }
                 }
             } else {
                 $appdata = $tmpquery->getFields();
                 $this->serial = $appdata['id'];
                 // Application will be updated
                 //
                 if ($this->serial) {
                     // Dependencies check
                     //
                     $this->unmetdeps = array();
                     $this->unmetsuggs = array();
                     $appdeps = new ApplicationDependencies();
                     $deps = $appdeps->explodeDependencies($genconfig['ApplicationDependencies']);
                     $suggs = $appdeps->explodeDependencies($genconfig['ApplicationSuggestions']);
                     if ($deps != false) {
                         $this->unmetdeps = $appdeps->checkApplicationDependencies(0, '', $deps);
                     } else {
                         $this->unmetdeps = false;
                     }
                     // Suggestions check
                     //
                     if ($suggs != false) {
                         $unmetsuggs = $appdeps->checkApplicationDependencies(0, '', $suggs);
                         if (is_array($unmetsuggs)) {
                             $this->unmetsuggs = $unmetsuggs;
                         }
                     }
                     // If dependencies are ok, go on
                     //
                     if ($this->unmetdeps == false) {
                         // Creates lock file
                         //
                         touch($this->container->getHome() . 'core/temp/upgrading_system_lock');
                         // :WARNING: evil 20020506: possible problems on Windows systems
                         // It has a 'permission denied'.
                         // Removes old application file
                         //
                         if (basename($fname) != $appdata['appfile'] and file_exists($this->container->getHome() . 'core/applications/' . $appdata['appfile'])) {
                             @unlink($this->container->getHome() . 'core/applications/' . $appdata['appfile']);
                         }
                         // Updates applications table
                         //
                         $this->rootda->execute('UPDATE applications SET appversion=' . $this->rootda->formatText($genconfig['ApplicationVersion']) . ', appdate=' . $this->rootda->formatText($genconfig['ApplicationDate']) . ', appdesc=' . $this->rootda->formatText($genconfig['ApplicationDescription']) . ', appfile=' . $this->rootda->formatText(basename($tmpfilepath)) . ', author=' . $this->rootda->formatText($genconfig['ApplicationAuthor']) . ', authoremail=' . $this->rootda->formatText($genconfig['ApplicationAuthorEmail']) . ', authorsite=' . $this->rootda->formatText($genconfig['ApplicationAuthorWeb']) . ', supportemail=' . $this->rootda->formatText($genconfig['ApplicationSupportEmail']) . ', bugsemail=' . $this->rootda->formatText($genconfig['ApplicationBugsEmail']) . ', copyright=' . $this->rootda->formatText($genconfig['ApplicationCopyright']) . ', license=' . $this->rootda->formatText($genconfig['ApplicationLicense']) . ', licensefile=' . $this->rootda->formatText($genconfig['ApplicationLicenseFile']) . ', changesfile=' . $this->rootda->formatText($genconfig['ApplicationChangesFile']) . ', maintainer=' . $this->rootda->formatText($genconfig['ApplicationMaintainer']) . ', maintaineremail=' . $this->rootda->formatText($genconfig['ApplicationMaintainerEmail']) . ', category=' . $this->rootda->formatText($genconfig['ApplicationCategory']) . ', iconfile=' . $this->rootda->formatText($genconfig['ApplicationIconFile']) . ' WHERE id=' . (int) $this->serial);
                         $genconfig = $this->parseApplicationDefinition($tmpdir . '/setup/application.xml');
                         // Script files - only before handlestructure
                         //
                         if ($dhandle = @opendir($tmpdir . '/setup')) {
                             while (false != ($file = readdir($dhandle))) {
                                 if ($file != '.' and $file != '..' and $file != 'application.xml' and is_file($tmpdir . '/setup/' . $file)) {
                                     @copy($tmpdir . '/setup/' . $file, $this->container->getHome() . 'core/applications/' . $genconfig['ApplicationIdName'] . '/' . $file);
                                 }
                             }
                             closedir($dhandle);
                         }
                         $this->HandleStructure($tmpdir . '/setup/application.xml', Application::INSTALL_MODE_UPDATE, $tmpdir);
                         if (strlen($genconfig['ApplicationLicenseFile']) and file_exists($tmpdir . '/setup/' . $genconfig['ApplicationLicenseFile'])) {
                             @copy($tmpdir . '/setup/' . $genconfig['ApplicationLicenseFile'], $this->container->getHome() . 'core/applications/' . $genconfig['ApplicationIdName'] . '/' . $genconfig['ApplicationLicenseFile']);
                         }
                         if (strlen($genconfig['ApplicationChangesFile']) and file_exists($tmpdir . '/setup/' . $genconfig['ApplicationChangesFile'])) {
                             @copy($tmpdir . '/setup/' . $genconfig['ApplicationChangesFile'], $this->container->getHome() . 'core/applications/' . $genconfig['ApplicationIdName'] . '/' . $genconfig['ApplicationChangesFile']);
                         }
                         if (strlen($genconfig['ApplicationIconFile']) and file_exists($tmpdir . '/setup/' . $genconfig['ApplicationIconFile'])) {
                             @copy($tmpdir . '/setup/' . $genconfig['ApplicationIconFile'], $this->container->getHome() . 'core/applications/' . $genconfig['ApplicationIdName'] . '/' . $genconfig['ApplicationIconFile']);
                         }
                         // setup files - only after handlestructure
                         //
                         @copy($tmpdir . '/setup/application.xml', $this->container->getHome() . 'core/applications/' . $genconfig['ApplicationIdName'] . '/application.xml');
                         // Checks if it is an extension application
                         //
                         $ext = $this->rootda->fmtfalse;
                         if ($genconfig['ApplicationIsExtension'] == 'y') {
                             $ext = $this->rootda->fmttrue;
                             $this->onlyextension = true;
                         } elseif ($genconfig['ApplicationIsExtension'] == 'n') {
                             $ext = $this->rootda->fmtfalse;
                             $this->onlyextension = false;
                         } elseif ($this->onlyextension) {
                             $ext = $this->rootda->fmttrue;
                         }
                         $this->rootda->execute('UPDATE applications SET onlyextension=' . $this->rootda->formatText($ext) . ' WHERE appid=' . $this->rootda->formatText($this->appname));
                         $this->setOptions(explode(',', trim($genconfig['ApplicationOptions'], ' ,')));
                         if ($this->appname != 'innomatic') {
                             // Remove old dependencies
                             //
                             $appdeps->removeAllDependencies($this->serial);
                             // Adds new Applications dependencies
                             //
                             $appdeps->addDependenciesArray($genconfig['ApplicationIdName'], $deps, ApplicationDependencies::TYPE_DEPENDENCY);
                             $appdeps->addDependenciesArray($genconfig['ApplicationIdName'], $suggs, ApplicationDependencies::TYPE_SUGGESTION);
                         }
                         $result = true;
                         if (function_exists('apc_reset_cache')) {
                             apc_reset_cache();
                         }
                         if ($updateOnce == false) {
                             $this->Install($tmpfilepath, true);
                             // Removes lock file
                             //
                             unlink($this->container->getHome() . 'core/temp/upgrading_system_lock');
                             if (\Innomatic\Core\InnomaticContainer::instance('\\Innomatic\\Core\\InnomaticContainer')->getConfig()->Value('SecurityAlertOnApplicationOperation') == '1') {
                                 $innomaticSecurity = new \Innomatic\Security\SecurityManager();
                                 $innomaticSecurity->SendAlert('Application ' . $this->appname . ' has been updated');
                                 unset($innomaticSecurity);
                             }
                             if ($result == true) {
                                 $log = $this->container->getLogger();
                                 $log->logEvent('Innomatic', 'Updated application ' . $this->appname, \Innomatic\Logging\Logger::NOTICE);
                             }
                         }
                     }
                     /*
                     else $this->mLog->logEvent( 'innomatic.applications.applications.install',
                     'Structure definition file for application '.$this->appname.
                     ' does not exists', \Innomatic\Logging\Logger::ERROR );
                     */
                 } else {
                     $log = $this->container->getLogger();
                     $log->logEvent('innomatic.applications.applications.install', 'Empty application serial', \Innomatic\Logging\Logger::ERROR);
                 }
             }
         } else {
             $log = $this->container->getLogger();
             if (!file_exists($tmpdir . '/setup/application.xml')) {
                 $log->logEvent('innomatic.applications.applications.install', 'Application structure file ' . $tmpdir . '/setup/application.xml' . ' not found', \Innomatic\Logging\Logger::ERROR);
             }
         }
         // Cleans up temp stuff
         //
         @chdir($olddir);
         \Innomatic\Io\Filesystem\DirectoryUtils::unlinkTree($basetmpdir);
         if (file_exists($tmpfilepath)) {
             @unlink($tmpfilepath);
         }
     } else {
         if (!file_exists($tmpfilepath)) {
             $log = $this->container->getLogger();
             $log->logEvent('innomatic.applications.applications.install', 'Temporary application file (' . $tmpfilepath . ') does not exists', \Innomatic\Logging\Logger::ERROR);
         }
     }
     if ($innomatic->getState() == \Innomatic\Core\InnomaticContainer::STATE_DEBUG) {
         $innomatic->getLoadTimer()->Mark('applicationinstallend');
         $log = $this->container->getLogger();
         $log->logEvent('innomatic.applications.application.install', 'Application installation load time: ' . $innomatic->getLoadTimer()->getSectionLoad('applicationinstallend'), \Innomatic\Logging\Logger::DEBUG);
     }
     return $result;
 }