/** * Creates an Image object from a file using a mock resource (in order to avoid a database resource pointer entry) * @param string $imagePathAndFilename * @return PersistentResource */ protected function getMockResourceByImagePath($imagePathAndFilename) { $imagePathAndFilename = Files::getUnixStylePath($imagePathAndFilename); $hash = sha1_file($imagePathAndFilename); copy($imagePathAndFilename, 'resource://' . $hash); return $mockResource = $this->createMockResourceAndPointerFromHash($hash); }
/** * Expands the given $patterns by adding an array element for each $replacement * replacing occurrences of $search. * * @param array $patterns * @param string $search * @param array $replacements * @return void */ protected function expandPatterns(array $patterns, $search, array $replacements) { if ($replacements === []) { return $patterns; } $patternsWithReplacements = []; foreach ($patterns as $pattern) { foreach ($replacements as $replacement) { $patternsWithReplacements[] = Files::getUnixStylePath(str_replace($search, $replacement, $pattern)); } } return $patternsWithReplacements; }
/** * Setup * * @return void */ public function setUp() { $this->unixStylePath = Files::getUnixStylePath(__DIR__); $this->unixStylePathAndFilename = Files::getUnixStylePath(__FILE__); vfsStream::setup('testDirectory'); }
/** * @param array $settings The Neos.Flow settings * @return string A command line command for PHP, which can be extended and then exec()uted */ public static function buildPhpCommand(array $settings) { $subRequestEnvironmentVariables = ['FLOW_ROOTPATH' => FLOW_PATH_ROOT, 'FLOW_PATH_TEMPORARY_BASE' => FLOW_PATH_TEMPORARY_BASE, 'FLOW_CONTEXT' => $settings['core']['context']]; if (isset($settings['core']['subRequestEnvironmentVariables'])) { $subRequestEnvironmentVariables = array_merge($subRequestEnvironmentVariables, $settings['core']['subRequestEnvironmentVariables']); } $command = ''; foreach ($subRequestEnvironmentVariables as $argumentKey => $argumentValue) { if (DIRECTORY_SEPARATOR === '/') { $command .= sprintf('%s=%s ', $argumentKey, escapeshellarg($argumentValue)); } else { // SET does not parse out quotes, hence we need escapeshellcmd here instead $command .= sprintf('SET %s=%s&', $argumentKey, escapeshellcmd($argumentValue)); } } if (DIRECTORY_SEPARATOR === '/') { $phpBinaryPathAndFilename = '"' . escapeshellcmd(Files::getUnixStylePath($settings['core']['phpBinaryPathAndFilename'])) . '"'; } else { $phpBinaryPathAndFilename = escapeshellarg(Files::getUnixStylePath($settings['core']['phpBinaryPathAndFilename'])); } $command .= $phpBinaryPathAndFilename; if (!isset($settings['core']['subRequestPhpIniPathAndFilename']) || $settings['core']['subRequestPhpIniPathAndFilename'] !== false) { if (!isset($settings['core']['subRequestPhpIniPathAndFilename'])) { $useIniFile = php_ini_loaded_file(); } else { $useIniFile = $settings['core']['subRequestPhpIniPathAndFilename']; } $command .= ' -c ' . escapeshellarg($useIniFile); } return $command; }
/** * Tries to find out a package key which the Version belongs to. If no * package could be found, an empty string is returned. * * @param Version $version * @return string */ protected function getPackageKeyFromMigrationVersion(Version $version) { $sortedAvailablePackages = $this->packageManager->getAvailablePackages(); usort($sortedAvailablePackages, function (PackageInterface $packageOne, PackageInterface $packageTwo) { return strlen($packageTwo->getPackagePath()) - strlen($packageOne->getPackagePath()); }); $reflectedClass = new \ReflectionClass($version->getMigration()); $classPathAndFilename = Files::getUnixStylePath($reflectedClass->getFileName()); /** @var $package PackageInterface */ foreach ($sortedAvailablePackages as $package) { $packagePath = Files::getUnixStylePath($package->getPackagePath()); if (strpos($classPathAndFilename, $packagePath) === 0) { return $package->getPackageKey(); } } return ''; }
/** * Adds the specified directory to the list of directories to be monitored. * All files in these directories will be monitored too. * * @param string $path Absolute path of the directory to monitor * @param string $filenamePattern A pattern for filenames to consider for file monitoring (regular expression) * @return void * @throws \InvalidArgumentException * @api */ public function monitorDirectory($path, $filenamePattern = null) { if (!is_string($path)) { throw new \InvalidArgumentException('String expected, ' . gettype($path), ' given.', 1231171810); } $path = Files::getNormalizedPath(Files::getUnixStylePath($path)); if (!array_key_exists($path, $this->monitoredDirectories)) { $this->monitoredDirectories[$path] = $filenamePattern; } }
<?php use Neos\Utility\Files; /** * Entry Point (Router) for PHP's embedded HTTP server. Use ./flow server:run to execute. */ if (strpos($_SERVER['REQUEST_URI'], '_Resources/') !== false) { // published resources shall be served directly return false; } require __DIR__ . '/../Classes/Core/Bootstrap.php'; define('FLOW_PATH_ROOT', Files::getUnixStylePath(realpath(__DIR__ . '/../../../../')) . '/'); // Script filename and script name must "emulate" index.php, to not break routing $_SERVER['SCRIPT_FILENAME'] = FLOW_PATH_ROOT . 'Web/index.php'; $_SERVER['SCRIPT_NAME'] = '/index.php'; // we want to have nice URLs putenv('FLOW_REWRITEURLS=1'); $context = \Neos\Flow\Core\Bootstrap::getEnvironmentConfigurationSetting('FLOW_CONTEXT') ?: 'Development'; $bootstrap = new \Neos\Flow\Core\Bootstrap($context); $bootstrap->run();
/** * Creates Flow's temporary directory - or at least asserts that it exists and is * writable. * * For each Flow Application Context, we create an extra temporary folder, * and for nested contexts, the folders are prefixed with "SubContext" to * avoid ambiguity, and look like: Data/Temporary/Production/SubContextLive * * @param string $temporaryDirectoryBase Full path to the base for the temporary directory * @return string The full path to the temporary directory * @throws UtilityException if the temporary directory could not be created or is not writable */ protected function createTemporaryDirectory($temporaryDirectoryBase) { $temporaryDirectoryBase = Files::getUnixStylePath($temporaryDirectoryBase); if (substr($temporaryDirectoryBase, -1, 1) !== '/') { $temporaryDirectoryBase .= '/'; } $temporaryDirectory = $temporaryDirectoryBase . str_replace('/', '/SubContext', (string) $this->context) . '/'; if (!is_dir($temporaryDirectory) && !is_link($temporaryDirectory)) { try { Files::createDirectoryRecursively($temporaryDirectory); } catch (ErrorException $exception) { throw new UtilityException('The temporary directory "' . $temporaryDirectory . '" could not be created. Please make sure permissions are correct for this path or define another temporary directory in your Settings.yaml with the path "Neos.Flow.utility.environment.temporaryDirectoryBase".', 1335382361); } } if (!is_writable($temporaryDirectory)) { throw new UtilityException('The temporary directory "' . $temporaryDirectory . '" is not writable. Please make this directory writable or define another temporary directory in your Settings.yaml with the path "Neos.Flow.utility.environment.temporaryDirectoryBase".', 1216287176); } return $temporaryDirectory; }
/** * Checks if PHP binary file exists bypassing open_basedir violation. * * If PHP binary is not within open_basedir path, * it is impossible to access this binary in any other way than exec() or system(). * So we must check existence of this file with system tools. * * @param string $phpBinaryPathAndFilename * @return boolean */ protected function phpBinaryExistsAndIsExecutableFile($phpBinaryPathAndFilename) { $phpBinaryPathAndFilename = escapeshellarg(Files::getUnixStylePath($phpBinaryPathAndFilename)); if (DIRECTORY_SEPARATOR === '/') { $command = sprintf('test -f %s && test -x %s', $phpBinaryPathAndFilename, $phpBinaryPathAndFilename); } else { $command = sprintf('IF EXIST %s (IF NOT EXIST %s\\* (EXIT 0) ELSE (EXIT 1)) ELSE (EXIT 1)', $phpBinaryPathAndFilename, $phpBinaryPathAndFilename); } exec($command, $outputLines, $exitCode); if ($exitCode === 0) { return true; } return false; }
/** * Copies any distribution files to their place if needed. * * @param string $installerResourcesDirectory Path to the installer directory that contains the Distribution/Essentials and/or Distribution/Defaults directories. * @return void */ protected static function copyDistributionFiles($installerResourcesDirectory) { $essentialsPath = $installerResourcesDirectory . 'Distribution/Essentials'; if (is_dir($essentialsPath)) { Files::copyDirectoryRecursively($essentialsPath, Files::getUnixStylePath(getcwd()) . '/', false, true); } $defaultsPath = $installerResourcesDirectory . 'Distribution/Defaults'; if (is_dir($defaultsPath)) { Files::copyDirectoryRecursively($defaultsPath, Files::getUnixStylePath(getcwd()) . '/', true, true); } }
/** * @test * @param string $path * @param string $expected * @dataProvider pathsWithProtocol */ public function getUnixStylePathWorksForPathWithProtocol($path, $expected) { $this->assertEquals($expected, Files::getUnixStylePath($path)); }
/** * Defines various path constants used by Flow and if no root path or web root was * specified by an environment variable, exits with a respective error message. * * @return void */ protected function defineConstants() { if (defined('FLOW_SAPITYPE')) { return; } define('FLOW_SAPITYPE', PHP_SAPI === 'cli' ? 'CLI' : 'Web'); if (!defined('FLOW_PATH_FLOW')) { define('FLOW_PATH_FLOW', str_replace('//', '/', str_replace('\\', '/', __DIR__ . '/../../'))); } if (!defined('FLOW_PATH_ROOT')) { $rootPath = isset($_SERVER['FLOW_ROOTPATH']) ? $_SERVER['FLOW_ROOTPATH'] : false; if ($rootPath === false && isset($_SERVER['REDIRECT_FLOW_ROOTPATH'])) { $rootPath = $_SERVER['REDIRECT_FLOW_ROOTPATH']; } if (FLOW_SAPITYPE === 'CLI' && $rootPath === false) { $rootPath = getcwd(); if (realpath(__DIR__) !== realpath($rootPath . '/Packages/Framework/Neos.Flow/Classes/Core')) { echo 'Neos Flow: Invalid root path. (Error #1301225173)' . PHP_EOL . 'You must start Neos Flow from the root directory or set the environment variable FLOW_ROOTPATH correctly.' . PHP_EOL; exit(1); } } if ($rootPath !== false) { $rootPath = Files::getUnixStylePath(realpath($rootPath)) . '/'; $testPath = Files::getUnixStylePath(realpath(Files::concatenatePaths([$rootPath, 'Packages/Framework/Neos.Flow']))) . '/'; $expectedPath = Files::getUnixStylePath(realpath(FLOW_PATH_FLOW)) . '/'; if ($testPath !== $expectedPath) { echo 'Flow: Invalid root path. (Error #1248964375)' . PHP_EOL . '"' . $testPath . '" does not lead to' . PHP_EOL . '"' . $expectedPath . '"' . PHP_EOL; exit(1); } define('FLOW_PATH_ROOT', $rootPath); unset($rootPath); unset($testPath); } } if (FLOW_SAPITYPE === 'CLI') { if (!defined('FLOW_PATH_ROOT')) { echo 'Flow: No root path defined in environment variable FLOW_ROOTPATH (Error #1248964376)' . PHP_EOL; exit(1); } if (!defined('FLOW_PATH_WEB')) { if (isset($_SERVER['FLOW_WEBPATH']) && is_dir($_SERVER['FLOW_WEBPATH'])) { define('FLOW_PATH_WEB', Files::getUnixStylePath(realpath($_SERVER['FLOW_WEBPATH'])) . '/'); } else { define('FLOW_PATH_WEB', FLOW_PATH_ROOT . 'Web/'); } } } else { if (!defined('FLOW_PATH_ROOT')) { define('FLOW_PATH_ROOT', Files::getUnixStylePath(realpath(dirname($_SERVER['SCRIPT_FILENAME']) . '/../')) . '/'); } define('FLOW_PATH_WEB', Files::getUnixStylePath(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/'); } define('FLOW_PATH_CONFIGURATION', FLOW_PATH_ROOT . 'Configuration/'); define('FLOW_PATH_DATA', FLOW_PATH_ROOT . 'Data/'); define('FLOW_PATH_PACKAGES', FLOW_PATH_ROOT . 'Packages/'); if (!defined('FLOW_PATH_TEMPORARY_BASE')) { define('FLOW_PATH_TEMPORARY_BASE', self::getEnvironmentConfigurationSetting('FLOW_PATH_TEMPORARY_BASE') ?: FLOW_PATH_DATA . '/Temporary'); $temporaryDirectoryPath = Files::concatenatePaths([FLOW_PATH_TEMPORARY_BASE, str_replace('/', '/SubContext', (string) $this->context)]) . '/'; define('FLOW_PATH_TEMPORARY', $temporaryDirectoryPath); } define('FLOW_VERSION_BRANCH', '4.0'); }
/** * Create a package, given the package key * * @param string $packageKey The package key of the new package * @param array $manifest A composer manifest as associative array. This is a preparation for the signature change in Flow 4.0. If you use this argument, then $packageMetaData and $packageType will be ignored. * @param string $packagesPath If specified, the package will be created in this path, otherwise the default "Application" directory is used * @return PackageInterface The newly created package * * @throws Exception\PackageKeyAlreadyExistsException * @throws Exception\InvalidPackageKeyException * @throws Exception\PackageKeyAlreadyExistsException * @api */ public function createPackage($packageKey, array $manifest = [], $packagesPath = null) { if (!$this->isPackageKeyValid($packageKey)) { throw new Exception\InvalidPackageKeyException('The package key "' . $packageKey . '" is invalid', 1220722210); } if ($this->isPackageAvailable($packageKey)) { throw new Exception\PackageKeyAlreadyExistsException('The package key "' . $packageKey . '" already exists', 1220722873); } if ($packagesPath === null) { $packagesPath = 'Application'; $packageType = isset($manifest['type']) ? $manifest['type'] : PackageInterface::DEFAULT_COMPOSER_TYPE; if (is_array($this->settings['package']['packagesPathByType']) && isset($this->settings['package']['packagesPathByType'][$packageType])) { $packagesPath = $this->settings['package']['packagesPathByType'][$packageType]; } $packagesPath = Files::getUnixStylePath(Files::concatenatePaths([$this->packagesBasePath, $packagesPath])); } $packagePath = Files::concatenatePaths([$packagesPath, $packageKey]) . '/'; Files::createDirectoryRecursively($packagePath); foreach ([PackageInterface::DIRECTORY_CLASSES, PackageInterface::DIRECTORY_CONFIGURATION, PackageInterface::DIRECTORY_RESOURCES, PackageInterface::DIRECTORY_TESTS_UNIT, PackageInterface::DIRECTORY_TESTS_FUNCTIONAL] as $path) { Files::createDirectoryRecursively(Files::concatenatePaths([$packagePath, $path])); } $manifest = ComposerUtility::writeComposerManifest($packagePath, $packageKey, $manifest); $refreshedPackageStatesConfiguration = $this->rescanPackages(false); $this->packageStatesConfiguration = $refreshedPackageStatesConfiguration; $this->registerPackageFromStateConfiguration($manifest['name'], $this->packageStatesConfiguration['packages'][$manifest['name']]); return $this->packages[$packageKey]; }