From 395d115d9b40d799dadc9f4c5b73cd54820112e3 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 20 Jan 2016 11:17:40 +0000 Subject: [PATCH] Resolve all dirs before initializing them, fixes #4802 --- src/Composer/Config.php | 4 +++- src/Composer/Factory.php | 21 +++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 92f91b117..c7d8efa8e 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -225,7 +225,9 @@ class Config return (int) $this->config['cache-ttl']; case 'home': - return rtrim($this->process($this->config[$key], $flags), '/\\'); + $val = preg_replace('#^(\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $this->config[$key]); + + return rtrim($this->process($val, $flags), '/\\'); case 'bin-compat': $value = $this->getComposerEnv('COMPOSER_BIN_COMPAT') ?: $this->config[$key]; diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 218ec77c2..60431229b 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -146,15 +146,21 @@ class Factory { $cwd = $cwd ?: getcwd(); - // determine home and cache dirs - $home = self::getHomeDir(); - $cacheDir = self::getCacheDir($home); - $dataDir = self::getDataDir($home); + $config = new Config(true, $cwd); + + // determine and add main dirs to the config + $home = self::getHomeDir(); + $config->merge(array('config' => array( + 'home' => $home, + 'cache-dir' => self::getCacheDir($home), + 'data-dir' => self::getDataDir($home), + ))); // Protect directory against web access. Since HOME could be // the www-data's user home and be web-accessible it is a // potential security risk - foreach (array($home, $cacheDir, $dataDir) as $dir) { + $dirs = array($config->get('home'), $config->get('cache-dir'), $config->get('data-dir')); + foreach ($dirs as $dir) { if (!file_exists($dir . '/.htaccess')) { if (!is_dir($dir)) { @mkdir($dir, 0777, true); @@ -163,11 +169,6 @@ class Factory } } - $config = new Config(true, $cwd); - - // add dirs to the config - $config->merge(array('config' => array('home' => $home, 'cache-dir' => $cacheDir, 'data-dir' => $dataDir))); - // load global config $file = new JsonFile($config->get('home').'/config.json'); if ($file->exists()) {