/** * Will set the owner and group on the passed directory recursively. * * @param \SplFileInfo $targetDir The directory to set the rights for * @param string $user The user that has to own the passed directory * @param string $group The group that has to own the passed directory * * @return void */ public function setUserRights(\SplFileInfo $targetDir, $user = null, $group = null) { // Get our system configuration as it contains the user and group to set $systemConfiguration = $this->getInitialContext()->getSystemConfiguration(); // check for the existence of a user if ($user == null) { $user = $systemConfiguration->getParam('user'); } // check for the existence of a group if ($group == null) { $group = $systemConfiguration->getParam('group'); } // change the owner for the passed directory FileSystem::recursiveChown($targetDir, $user, $group); }
/** * Switches the setup mode to the passed value. * * @param string $newMode The mode to switch to * @param string $configurationFilename The path of the configuration filename * @param string $user The name of the user who started the application server * * @return void * @throws \Exception Is thrown for an invalid setup mode passed */ public function switchSetupMode($newMode, $configurationFilename, $user) { // log a message that we switch setup mode now $this->getInitialContext()->getSystemLogger()->info(sprintf('Now switch mode to %s!!!', $newMode)); // init setup context Setup::prepareContext($this->getBaseDirectory()); // init variable for the group $group = null; // pattern to replace the user in the etc/appserver/appserver.xml file $configurationUserReplacePattern = '/(<appserver[^>]+>[^<]+<params>.*<param name="user[^>]+>)([^<]+)/s'; // check setup modes switch ($newMode) { // prepares everything for developer mode case ContainerService::SETUP_MODE_DEV: // get defined group from configuration $group = Setup::getValue(SetupKeys::GROUP); // replace user in configuration file file_put_contents($configurationFilename, preg_replace($configurationUserReplacePattern, '${1}' . $user, file_get_contents($configurationFilename))); // add everyone write access to configuration files for dev mode FileSystem::recursiveChmod($this->getEtcDir(), 0777, 0777); break; // prepares everything for production mode // prepares everything for production mode case ContainerService::SETUP_MODE_PROD: // get defined user and group from configuration $user = Setup::getValue(SetupKeys::USER); $group = Setup::getValue(SetupKeys::GROUP); // replace user to be same as user in configuration file file_put_contents($configurationFilename, preg_replace($configurationUserReplacePattern, '${1}' . $user, file_get_contents($configurationFilename))); // set correct file permissions for configurations FileSystem::recursiveChmod($this->getEtcDir()); break; // prepares everything for first installation which is default mode // prepares everything for first installation which is default mode case ContainerService::SETUP_MODE_INSTALL: // load the flag marked the server as installed $isInstalledFlag = $this->getIsInstalledFlag(); // first check if it is a fresh installation if ($isInstalledFlag->isReadable() === false) { // set example app dodeploy flag to be deployed for a fresh installation touch($this->getDeployDir('example.phar.dodeploy')); } // create is installed flag for prevent further setup install mode calls touch($isInstalledFlag); // get defined user and group from configuration $user = Setup::getValue(SetupKeys::USER); $group = Setup::getValue(SetupKeys::GROUP); // set correct file permissions for configurations FileSystem::recursiveChmod($this->getEtcDir()); break; default: throw new \Exception(sprintf('Invalid setup mode %s given', $newMode)); } // check if user and group is set if (!is_null($user) && !is_null($group)) { // get needed files as accessable for all root files remove "." and ".." from the list $rootFiles = scandir($this->getBaseDirectory()); // iterate all files foreach ($rootFiles as $rootFile) { // we want just files on root dir if (is_file($rootFile) && !in_array($rootFile, array('.', '..'))) { FileSystem::chmod($rootFile, 0644); FileSystem::chown($rootFile, $user, $group); } } // ... and change own and mod of following directories FileSystem::chown($this->getBaseDirectory(), $user, $group); FileSystem::chown($this->getWebappsDir(), $user, $group); FileSystem::recursiveChown($this->getTmpDir(), $user, $group); FileSystem::recursiveChmod($this->getTmpDir()); FileSystem::recursiveChown($this->getDeployDir(), $user, $group); FileSystem::recursiveChmod($this->getDeployDir()); FileSystem::recursiveChown($this->getBaseDirectory('resources'), $user, $group); FileSystem::recursiveChmod($this->getBaseDirectory('resources')); FileSystem::recursiveChown($this->getBaseDirectory('src'), $user, $group); FileSystem::recursiveChmod($this->getBaseDirectory('src')); FileSystem::recursiveChown($this->getBaseDirectory('var'), $user, $group); FileSystem::recursiveChmod($this->getBaseDirectory('var')); FileSystem::recursiveChown($this->getBaseDirectory('tests'), $user, $group); FileSystem::recursiveChmod($this->getBaseDirectory('tests')); FileSystem::recursiveChown($this->getBaseDirectory('vendor'), $user, $group); FileSystem::recursiveChmod($this->getBaseDirectory('vendor')); // make server.php executable FileSystem::chmod($this->getBaseDirectory('server.php'), 0755); // log a message that we successfully switched to the new setup mode $this->getInitialContext()->getSystemLogger()->info(sprintf("Setup for mode '%s' done successfully!", $newMode)); } else { throw new \Exception('No user or group given'); } }